ctkXnatTreeModel.cpp 6.8 KB

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