ctkXnatTreeModel.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*=============================================================================
  2. Plugin: org.commontk.xnat
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkXnatTreeModel.h"
  16. #include "ctkXnatObject.h"
  17. #include "ctkXnatSubject.h"
  18. #include <QList>
  19. ctkXnatTreeModel::ctkXnatTreeModel()
  20. : m_RootItem(new ctkXnatTreeItem())
  21. {
  22. }
  23. ctkXnatTreeModel::~ctkXnatTreeModel()
  24. {
  25. delete m_RootItem;
  26. }
  27. // returns name (project, subject, etc.) for row and column of
  28. // parent in index if role is Qt::DisplayRole
  29. QVariant ctkXnatTreeModel::data(const QModelIndex& index, int role) const
  30. {
  31. if (!index.isValid())
  32. {
  33. return QVariant();
  34. }
  35. if (role == Qt::TextAlignmentRole)
  36. {
  37. return QVariant(int(Qt::AlignTop | Qt::AlignLeft));
  38. }
  39. else if (role == Qt::DisplayRole)
  40. {
  41. ctkXnatObject* xnatObject = this->xnatObject(index);
  42. QString displayData = xnatObject->name();
  43. if (displayData.isEmpty())
  44. {
  45. displayData = xnatObject->id();
  46. }
  47. return displayData;
  48. }
  49. else if (role == Qt::ToolTipRole)
  50. {
  51. return this->xnatObject(index)->description();
  52. }
  53. return QVariant();
  54. }
  55. QModelIndex ctkXnatTreeModel::index(int row, int column, const QModelIndex& index) const
  56. {
  57. if (!this->hasIndex(row, column, index))
  58. {
  59. return QModelIndex();
  60. }
  61. ctkXnatTreeItem* item;
  62. if (!index.isValid())
  63. {
  64. item = m_RootItem;
  65. }
  66. else
  67. {
  68. item = this->itemAt(index);
  69. }
  70. ctkXnatTreeItem* childItem = item->child(row);
  71. if (childItem)
  72. {
  73. return this->createIndex(row, column, childItem);
  74. }
  75. return QModelIndex();
  76. }
  77. QModelIndex ctkXnatTreeModel::parent(const QModelIndex& index) const
  78. {
  79. if (!index.isValid())
  80. {
  81. return QModelIndex();
  82. }
  83. ctkXnatTreeItem* item = this->itemAt(index);
  84. ctkXnatTreeItem* parentItem = item->parent();
  85. if (parentItem == m_RootItem)
  86. {
  87. return QModelIndex();
  88. }
  89. return this->createIndex(parentItem->row(), 0, parentItem);
  90. }
  91. int ctkXnatTreeModel::rowCount(const QModelIndex& index) const
  92. {
  93. if (index.column() > 0)
  94. {
  95. return 0;
  96. }
  97. ctkXnatTreeItem* item;
  98. if (!index.isValid())
  99. {
  100. item = m_RootItem;
  101. }
  102. else
  103. {
  104. item = this->itemAt(index);
  105. }
  106. return item->childCount();
  107. }
  108. int ctkXnatTreeModel::columnCount(const QModelIndex& index) const
  109. {
  110. Q_UNUSED(index);
  111. return 1;
  112. }
  113. // defer request for children until actually needed by QTreeView object
  114. bool ctkXnatTreeModel::hasChildren(const QModelIndex& index) const
  115. {
  116. if (!index.isValid())
  117. {
  118. return m_RootItem->childCount() > 0;
  119. }
  120. ctkXnatTreeItem* item = this->itemAt(index);
  121. return !item->xnatObject()->isFetched() || (item->childCount() > 0);
  122. }
  123. bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
  124. {
  125. if (!index.isValid())
  126. {
  127. return false;
  128. }
  129. return !this->xnatObject(index)->isFetched();
  130. }
  131. void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
  132. {
  133. if (!index.isValid())
  134. {
  135. return;
  136. }
  137. ctkXnatTreeItem* item = this->itemAt(index);
  138. ctkXnatObject* xnatObject = item->xnatObject();
  139. xnatObject->fetch();
  140. QList<ctkXnatObject*> children = xnatObject->children();
  141. if (!children.isEmpty())
  142. {
  143. beginInsertRows(index, 0, children.size() - 1);
  144. foreach (ctkXnatObject* child, children)
  145. {
  146. item->appendChild(new ctkXnatTreeItem(child, item));
  147. }
  148. endInsertRows();
  149. }
  150. }
  151. ctkXnatObject* ctkXnatTreeModel::xnatObject(const QModelIndex& index) const
  152. {
  153. return this->itemAt(index)->xnatObject();
  154. }
  155. void ctkXnatTreeModel::addServer(ctkXnatServer* server)
  156. {
  157. m_RootItem->appendChild(new ctkXnatTreeItem(server, m_RootItem));
  158. }
  159. ctkXnatTreeItem* ctkXnatTreeModel::itemAt(const QModelIndex& index) const
  160. {
  161. return static_cast<ctkXnatTreeItem*>(index.internalPointer());
  162. }
  163. bool ctkXnatTreeModel::removeAllRows(const QModelIndex& parent)
  164. {
  165. // do nothing for the root
  166. if ( !parent.isValid() )
  167. {
  168. return false;
  169. }
  170. ctkXnatObject* xnatObject = this->xnatObject(parent);
  171. // nt: not sure why the parent.row() is used here instead of the first item in list
  172. // that is xnatObject->children()[0];
  173. ctkXnatObject* child = xnatObject->children()[parent.row()];
  174. if ( child == NULL )
  175. {
  176. return false;
  177. }
  178. int numberofchildren = child->children().size();
  179. if (numberofchildren > 0)
  180. {
  181. beginRemoveRows(parent, 0, numberofchildren - 1);
  182. // xnatObject->removeChild(parent.row());
  183. // nt: not sure if this is the right implementation here, should iterate ?
  184. xnatObject->remove(child);
  185. endRemoveRows();
  186. }
  187. else
  188. {
  189. // xnatObject->removeChild(parent.row());
  190. // nt: not sure if this is the right implementation here, should iterate ?
  191. xnatObject->remove(child);
  192. }
  193. return true;
  194. }
  195. void ctkXnatTreeModel::downloadFile(const QModelIndex& index, const QString& zipFileName)
  196. {
  197. if (!index.isValid())
  198. {
  199. return;
  200. }
  201. this->xnatObject(index)->download(zipFileName);
  202. return;
  203. }
  204. void ctkXnatTreeModel::uploadFile(const QModelIndex& index, const QString& zipFileName)
  205. {
  206. if (!index.isValid())
  207. {
  208. return;
  209. }
  210. ctkXnatObject* xnatObject = this->xnatObject(index);
  211. ctkXnatObject* child = xnatObject->children()[index.row()];
  212. child->upload(zipFileName);
  213. }