ctkXnatTreeModel.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <QList>
  18. ctkXnatTreeModel::ctkXnatTreeModel()
  19. : m_RootItem(new ctkXnatTreeItem())
  20. {
  21. }
  22. ctkXnatTreeModel::~ctkXnatTreeModel()
  23. {
  24. delete m_RootItem;
  25. }
  26. // returns name (project, subject, etc.) for row and column of
  27. // parent in index if role is Qt::DisplayRole
  28. QVariant ctkXnatTreeModel::data(const QModelIndex& index, int role) const
  29. {
  30. if (!index.isValid())
  31. {
  32. return QVariant();
  33. }
  34. if (role == Qt::TextAlignmentRole)
  35. {
  36. return QVariant(int(Qt::AlignTop | Qt::AlignLeft));
  37. }
  38. else if (role == Qt::DisplayRole)
  39. {
  40. ctkXnatObject* xnatObject = this->xnatObject(index);
  41. QString displayData = xnatObject->name();
  42. if (displayData.isEmpty())
  43. {
  44. displayData = xnatObject->id();
  45. }
  46. return displayData;
  47. }
  48. else if (role == Qt::ToolTipRole)
  49. {
  50. return this->xnatObject(index)->description();
  51. }
  52. else if (role == Qt::UserRole)
  53. {
  54. return QVariant::fromValue<ctkXnatObject*>(this->xnatObject(index));
  55. }
  56. return QVariant();
  57. }
  58. QModelIndex ctkXnatTreeModel::index(int row, int column, const QModelIndex& index) const
  59. {
  60. if (!this->hasIndex(row, column, index))
  61. {
  62. return QModelIndex();
  63. }
  64. ctkXnatTreeItem* item;
  65. if (!index.isValid())
  66. {
  67. item = m_RootItem;
  68. }
  69. else
  70. {
  71. item = this->itemAt(index);
  72. }
  73. ctkXnatTreeItem* childItem = item->child(row);
  74. if (childItem)
  75. {
  76. return this->createIndex(row, column, childItem);
  77. }
  78. return QModelIndex();
  79. }
  80. QModelIndex ctkXnatTreeModel::parent(const QModelIndex& index) const
  81. {
  82. if (!index.isValid())
  83. {
  84. return QModelIndex();
  85. }
  86. ctkXnatTreeItem* item = this->itemAt(index);
  87. ctkXnatTreeItem* parentItem = item->parent();
  88. if (parentItem == m_RootItem)
  89. {
  90. return QModelIndex();
  91. }
  92. return this->createIndex(parentItem->row(), 0, parentItem);
  93. }
  94. int ctkXnatTreeModel::rowCount(const QModelIndex& index) const
  95. {
  96. if (index.column() > 0)
  97. {
  98. return 0;
  99. }
  100. ctkXnatTreeItem* item;
  101. if (!index.isValid())
  102. {
  103. item = m_RootItem;
  104. }
  105. else
  106. {
  107. item = this->itemAt(index);
  108. }
  109. return item->childCount();
  110. }
  111. int ctkXnatTreeModel::columnCount(const QModelIndex& index) const
  112. {
  113. Q_UNUSED(index);
  114. return 1;
  115. }
  116. // defer request for children until actually needed by QTreeView object
  117. bool ctkXnatTreeModel::hasChildren(const QModelIndex& index) const
  118. {
  119. if (!index.isValid())
  120. {
  121. return m_RootItem->childCount() > 0;
  122. }
  123. ctkXnatTreeItem* item = this->itemAt(index);
  124. return !item->xnatObject()->isFetched() || !item->xnatObject()->children().isEmpty();
  125. }
  126. bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
  127. {
  128. if (!index.isValid())
  129. {
  130. return false;
  131. }
  132. ctkXnatTreeItem* item = this->itemAt(index);
  133. return !(item->childCount() > 0);
  134. }
  135. void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
  136. {
  137. if (!index.isValid())
  138. {
  139. return;
  140. }
  141. ctkXnatTreeItem* item = this->itemAt(index);
  142. ctkXnatObject* xnatObject = item->xnatObject();
  143. xnatObject->fetch();
  144. QList<ctkXnatObject*> children = xnatObject->children();
  145. if (!children.isEmpty())
  146. {
  147. beginInsertRows(index, 0, children.size() - 1);
  148. foreach (ctkXnatObject* child, children)
  149. {
  150. item->appendChild(new ctkXnatTreeItem(child, item));
  151. }
  152. endInsertRows();
  153. }
  154. }
  155. ctkXnatObject* ctkXnatTreeModel::xnatObject(const QModelIndex& index) const
  156. {
  157. return this->itemAt(index)->xnatObject();
  158. }
  159. void ctkXnatTreeModel::addDataModel(ctkXnatDataModel* dataModel)
  160. {
  161. m_RootItem->appendChild(new ctkXnatTreeItem(dataModel, m_RootItem));
  162. }
  163. ctkXnatTreeItem* ctkXnatTreeModel::itemAt(const QModelIndex& index) const
  164. {
  165. return static_cast<ctkXnatTreeItem*>(index.internalPointer());
  166. }
  167. bool ctkXnatTreeModel::removeAllRows(const QModelIndex& parent)
  168. {
  169. // do nothing for the root
  170. if ( !parent.isValid() )
  171. {
  172. return false;
  173. }
  174. ctkXnatObject* xnatObject = this->xnatObject(parent);
  175. // nt: not sure why the parent.row() is used here instead of the first item in list
  176. // that is xnatObject->children()[0];
  177. ctkXnatObject* child = xnatObject->children()[parent.row()];
  178. if ( child == NULL )
  179. {
  180. return false;
  181. }
  182. int numberofchildren = child->children().size();
  183. if (numberofchildren > 0)
  184. {
  185. beginRemoveRows(parent, 0, numberofchildren - 1);
  186. // xnatObject->removeChild(parent.row());
  187. // nt: not sure if this is the right implementation here, should iterate ?
  188. xnatObject->remove(child);
  189. endRemoveRows();
  190. }
  191. else
  192. {
  193. // xnatObject->removeChild(parent.row());
  194. // nt: not sure if this is the right implementation here, should iterate ?
  195. xnatObject->remove(child);
  196. }
  197. return true;
  198. }
  199. void ctkXnatTreeModel::downloadFile(const QModelIndex& index, const QString& zipFileName)
  200. {
  201. if (!index.isValid())
  202. {
  203. return;
  204. }
  205. this->xnatObject(index)->download(zipFileName);
  206. return;
  207. }
  208. void ctkXnatTreeModel::uploadFile(const QModelIndex& index, const QString& zipFileName)
  209. {
  210. if (!index.isValid())
  211. {
  212. return;
  213. }
  214. ctkXnatObject* xnatObject = this->xnatObject(index);
  215. ctkXnatObject* child = xnatObject->children()[index.row()];
  216. child->upload(zipFileName);
  217. }