ctkXnatTreeModel.cpp 8.8 KB

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