ctkXnatTreeModel.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include <QDebug>
  21. ctkXnatTreeModel::ctkXnatTreeModel()
  22. : m_RootItem(new ctkXnatTreeItem())
  23. {
  24. }
  25. ctkXnatTreeModel::~ctkXnatTreeModel()
  26. {
  27. delete m_RootItem;
  28. }
  29. // returns name (project, subject, etc.) for row and column of
  30. // parent in index if role is Qt::DisplayRole
  31. QVariant ctkXnatTreeModel::data(const QModelIndex& index, int role) const
  32. {
  33. if (!index.isValid())
  34. {
  35. return QVariant();
  36. }
  37. if (role == Qt::TextAlignmentRole)
  38. {
  39. return QVariant(int(Qt::AlignTop | Qt::AlignLeft));
  40. }
  41. else if (role == Qt::DisplayRole)
  42. {
  43. ctkXnatObject::Pointer xnatObject = this->itemAt(index)->xnatObject();
  44. QString displayData = xnatObject->getName();
  45. if (displayData.isEmpty())
  46. {
  47. displayData = xnatObject->getId();
  48. }
  49. return displayData;
  50. }
  51. else if (role == Qt::ToolTipRole)
  52. {
  53. ctkXnatObject::Pointer xnatObject = this->itemAt(index)->xnatObject();
  54. return xnatObject->getDescription();
  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. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  125. return !xnatObject->isFetched() || (item->childCount() > 0);
  126. }
  127. bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
  128. {
  129. if (!index.isValid())
  130. {
  131. return false;
  132. }
  133. ctkXnatTreeItem* item = this->itemAt(index);
  134. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  135. return !xnatObject->isFetched();
  136. }
  137. void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
  138. {
  139. if (!index.isValid())
  140. {
  141. return;
  142. }
  143. ctkXnatTreeItem* item = this->itemAt(index);
  144. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  145. xnatObject->fetch();
  146. QList<ctkXnatObject::Pointer> children = xnatObject->getChildren();
  147. if (!children.isEmpty())
  148. {
  149. beginInsertRows(index, 0, children.size() - 1);
  150. foreach (ctkXnatObject::Pointer child, children)
  151. {
  152. item->appendChild(new ctkXnatTreeItem(child, item));
  153. }
  154. endInsertRows();
  155. }
  156. }
  157. void ctkXnatTreeModel::addServer(ctkXnatServer::Pointer server)
  158. {
  159. m_RootItem->appendChild(new ctkXnatTreeItem(server, m_RootItem));
  160. }
  161. ctkXnatTreeItem* ctkXnatTreeModel::itemAt(const QModelIndex& index) const
  162. {
  163. return static_cast<ctkXnatTreeItem*>(index.internalPointer());
  164. }
  165. bool ctkXnatTreeModel::removeAllRows(const QModelIndex& parent)
  166. {
  167. // do nothing for the root
  168. if ( !parent.isValid() )
  169. {
  170. return false;
  171. }
  172. ctkXnatTreeItem* item = this->itemAt(parent);
  173. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  174. // nt: not sure why the parent.row() is used here instead of the first item in list
  175. // that is xnatObject->getChildren()[0];
  176. ctkXnatObject::Pointer child = xnatObject->getChildren()[parent.row()];
  177. if ( child == NULL )
  178. {
  179. return false;
  180. }
  181. int numberofchildren = child->getChildren().size();
  182. if (numberofchildren > 0)
  183. {
  184. beginRemoveRows(parent, 0, numberofchildren - 1);
  185. // xnatObject->removeChild(parent.row());
  186. // nt: not sure if this is the right implementation here, should iterate ?
  187. xnatObject->removeChild(child);
  188. endRemoveRows();
  189. }
  190. else
  191. {
  192. // xnatObject->removeChild(parent.row());
  193. // nt: not sure if this is the right implementation here, should iterate ?
  194. xnatObject->removeChild(child);
  195. }
  196. return true;
  197. }
  198. void ctkXnatTreeModel::downloadFile(const QModelIndex& index, const QString& zipFilename)
  199. {
  200. if ( !index.isValid() )
  201. {
  202. return;
  203. }
  204. ctkXnatTreeItem* item = this->itemAt(index);
  205. ctkXnatObject::Pointer object = item->xnatObject();
  206. // ctkXnatObject::Pointer child = object->getChildren()[index.row()];
  207. qDebug() << "object is null ? " << object.isNull();
  208. qDebug() << "object is file ? " << object->isFile();
  209. object->download (zipFilename);
  210. return;
  211. }
  212. void ctkXnatTreeModel::uploadFile(const QModelIndex& index, const QString& zipFilename)
  213. {
  214. if ( !index.isValid() )
  215. {
  216. return;
  217. }
  218. ctkXnatTreeItem* item = this->itemAt(index);
  219. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  220. ctkXnatObject::Pointer child = xnatObject->getChildren()[index.row()];
  221. child->upload(zipFilename);
  222. }
  223. void ctkXnatTreeModel::addEntry(const QModelIndex& index, const QString& name)
  224. {
  225. if ( !index.isValid() )
  226. {
  227. return;
  228. }
  229. ctkXnatTreeItem* item = this->itemAt(index);
  230. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  231. ctkXnatObject::Pointer child = xnatObject->getChildren()[index.row()];
  232. child->add(name);
  233. }
  234. void ctkXnatTreeModel::removeEntry(const QModelIndex& index)
  235. {
  236. if ( !index.isValid() )
  237. {
  238. return;
  239. }
  240. ctkXnatTreeItem* item = this->itemAt(index);
  241. ctkXnatObject::Pointer xnatObject = item->xnatObject();
  242. ctkXnatObject::Pointer child = xnatObject->getChildren()[index.row()];
  243. child->remove();
  244. }