ctkXnatTreeModel.cpp 7.7 KB

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