ctkXnatTreeModel.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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() || !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. ctkXnatObject* xnatObject = item->xnatObject();
  195. // Do this just for xnatObjects that are already fetched.
  196. // Otherwise we would retrieve all data from XNAT
  197. if (xnatObject->isFetched())
  198. {
  199. // Force a fetch for current object (it might has changed on the server)
  200. xnatObject->fetch(true);
  201. int numChildren = rowCount(parent);
  202. bool addToTreeView (true);
  203. // For all children, check if they are already in the treeview,
  204. // if not -> add them
  205. // For all items of the treeview, check if they are still on the server
  206. // if not -> remove them
  207. QList<ctkXnatObject*> children = xnatObject->children();
  208. QMutableListIterator<ctkXnatObject*> iter (children);
  209. while (iter.hasNext())
  210. {
  211. ctkXnatObject* child = iter.next();
  212. for (int i = 0; i < numChildren; ++i)
  213. {
  214. ctkXnatObject* childItemObject = item->child(i)->xnatObject();
  215. // If the item was deleted from the server in the meantime
  216. // -> remove it from the treeview
  217. if (!childItemObject->exists())
  218. {
  219. beginRemoveRows(parent, item->child(i)->row(), item->child(i)->row());
  220. item->remove(childItemObject);
  221. xnatObject->remove(child);
  222. iter.remove();
  223. endRemoveRows();
  224. --numChildren;
  225. --i;
  226. addToTreeView = false;
  227. break;
  228. }
  229. if ((childItemObject->id().length() != 0 && childItemObject->id() == child->id()) ||
  230. (childItemObject->id().length() == 0 && childItemObject->name() == child->name()))
  231. {
  232. addToTreeView = false;
  233. break;
  234. }
  235. }
  236. // If the current xnatObject was created on the server in the meantime
  237. // -> add it to the treeview
  238. if (addToTreeView)
  239. {
  240. int last = qMax(0, numChildren - 1);
  241. beginInsertRows(parent, 0, last);
  242. item->appendChild(new ctkXnatTreeItem(child, item));
  243. endInsertRows();
  244. ++numChildren;
  245. }
  246. addToTreeView = true;
  247. }
  248. numChildren = rowCount(parent);
  249. for (int i=0; i<numChildren; i++)
  250. {
  251. refresh(index(i,0,parent));
  252. }
  253. }
  254. }
  255. //----------------------------------------------------------------------------
  256. ctkXnatObject* ctkXnatTreeModel::xnatObject(const QModelIndex& index) const
  257. {
  258. Q_D(const ctkXnatTreeModel);
  259. return d->itemAt(index)->xnatObject();
  260. }
  261. //----------------------------------------------------------------------------
  262. void ctkXnatTreeModel::addDataModel(ctkXnatDataModel* dataModel)
  263. {
  264. Q_D(ctkXnatTreeModel);
  265. d->m_RootItem->appendChild(new ctkXnatTreeItem(dataModel, d->m_RootItem.data()));
  266. }
  267. //----------------------------------------------------------------------------
  268. void ctkXnatTreeModel::removeDataModel(ctkXnatDataModel* dataModel)
  269. {
  270. Q_D(ctkXnatTreeModel);
  271. d->m_RootItem->remove(dataModel);
  272. }
  273. //----------------------------------------------------------------------------
  274. bool ctkXnatTreeModel::removeAllRows(const QModelIndex& parent)
  275. {
  276. // do nothing for the root
  277. if ( !parent.isValid() )
  278. {
  279. return false;
  280. }
  281. ctkXnatObject* xnatObject = this->xnatObject(parent);
  282. // nt: not sure why the parent.row() is used here instead of the first item in list
  283. // that is xnatObject->children()[0];
  284. ctkXnatObject* child = xnatObject->children()[parent.row()];
  285. if ( child == NULL )
  286. {
  287. return false;
  288. }
  289. int numberofchildren = child->children().size();
  290. if (numberofchildren > 0)
  291. {
  292. beginRemoveRows(parent, 0, numberofchildren - 1);
  293. // xnatObject->removeChild(parent.row());
  294. // nt: not sure if this is the right implementation here, should iterate ?
  295. xnatObject->remove(child);
  296. endRemoveRows();
  297. }
  298. else
  299. {
  300. // xnatObject->removeChild(parent.row());
  301. // nt: not sure if this is the right implementation here, should iterate ?
  302. xnatObject->remove(child);
  303. }
  304. return true;
  305. }
  306. //----------------------------------------------------------------------------
  307. void ctkXnatTreeModel::downloadFile(const QModelIndex& index, const QString& zipFileName)
  308. {
  309. if (!index.isValid())
  310. {
  311. return;
  312. }
  313. this->xnatObject(index)->download(zipFileName);
  314. return;
  315. }
  316. //----------------------------------------------------------------------------
  317. void ctkXnatTreeModel::addChildNode(const QModelIndex &index, ctkXnatObject* child)
  318. {
  319. Q_D(ctkXnatTreeModel);
  320. ctkXnatTreeItem* item = d->itemAt(index);
  321. beginInsertRows(index, 0, 1);
  322. item->appendChild(new ctkXnatTreeItem(child, item));
  323. endInsertRows();
  324. }