ctkXnatTreeModel.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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* xnatObject = this->xnatObject(index);
  43. QString displayData = xnatObject->name();
  44. if (displayData.isEmpty())
  45. {
  46. displayData = xnatObject->id();
  47. }
  48. return displayData;
  49. }
  50. else if (role == Qt::ToolTipRole)
  51. {
  52. return this->xnatObject(index)->description();
  53. }
  54. return QVariant();
  55. }
  56. QModelIndex ctkXnatTreeModel::index(int row, int column, const QModelIndex& index) const
  57. {
  58. if (!this->hasIndex(row, column, index))
  59. {
  60. return QModelIndex();
  61. }
  62. ctkXnatTreeItem* item;
  63. if (!index.isValid())
  64. {
  65. item = m_RootItem;
  66. }
  67. else
  68. {
  69. item = this->itemAt(index);
  70. }
  71. ctkXnatTreeItem* childItem = item->child(row);
  72. if (childItem)
  73. {
  74. return this->createIndex(row, column, childItem);
  75. }
  76. return QModelIndex();
  77. }
  78. QModelIndex ctkXnatTreeModel::parent(const QModelIndex& index) const
  79. {
  80. if (!index.isValid())
  81. {
  82. return QModelIndex();
  83. }
  84. ctkXnatTreeItem* item = this->itemAt(index);
  85. ctkXnatTreeItem* parentItem = item->parent();
  86. if (parentItem == m_RootItem)
  87. {
  88. return QModelIndex();
  89. }
  90. return this->createIndex(parentItem->row(), 0, parentItem);
  91. }
  92. int ctkXnatTreeModel::rowCount(const QModelIndex& index) const
  93. {
  94. if (index.column() > 0)
  95. {
  96. return 0;
  97. }
  98. ctkXnatTreeItem* item;
  99. if (!index.isValid())
  100. {
  101. item = m_RootItem;
  102. }
  103. else
  104. {
  105. item = this->itemAt(index);
  106. }
  107. return item->childCount();
  108. }
  109. int ctkXnatTreeModel::columnCount(const QModelIndex& index) const
  110. {
  111. Q_UNUSED(index);
  112. return 1;
  113. }
  114. // defer request for children until actually needed by QTreeView object
  115. bool ctkXnatTreeModel::hasChildren(const QModelIndex& index) const
  116. {
  117. if (!index.isValid())
  118. {
  119. return m_RootItem->childCount() > 0;
  120. }
  121. ctkXnatTreeItem* item = this->itemAt(index);
  122. return !item->xnatObject()->isFetched() || (item->childCount() > 0);
  123. }
  124. bool ctkXnatTreeModel::canFetchMore(const QModelIndex& index) const
  125. {
  126. if (!index.isValid())
  127. {
  128. return false;
  129. }
  130. return !this->xnatObject(index)->isFetched();
  131. }
  132. void ctkXnatTreeModel::fetchMore(const QModelIndex& index)
  133. {
  134. if (!index.isValid())
  135. {
  136. return;
  137. }
  138. ctkXnatTreeItem* item = this->itemAt(index);
  139. ctkXnatObject* xnatObject = item->xnatObject();
  140. xnatObject->fetch();
  141. QList<ctkXnatObject*> children = xnatObject->children();
  142. if (!children.isEmpty())
  143. {
  144. beginInsertRows(index, 0, children.size() - 1);
  145. foreach (ctkXnatObject* child, children)
  146. {
  147. item->appendChild(new ctkXnatTreeItem(child, item));
  148. }
  149. endInsertRows();
  150. }
  151. }
  152. ctkXnatObject* ctkXnatTreeModel::xnatObject(const QModelIndex& index) const
  153. {
  154. return this->itemAt(index)->xnatObject();
  155. }
  156. void ctkXnatTreeModel::addServer(ctkXnatServer* 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. ctkXnatObject* xnatObject = this->xnatObject(parent);
  172. // nt: not sure why the parent.row() is used here instead of the first item in list
  173. // that is xnatObject->children()[0];
  174. ctkXnatObject* child = xnatObject->children()[parent.row()];
  175. if ( child == NULL )
  176. {
  177. return false;
  178. }
  179. int numberofchildren = child->children().size();
  180. if (numberofchildren > 0)
  181. {
  182. beginRemoveRows(parent, 0, numberofchildren - 1);
  183. // xnatObject->removeChild(parent.row());
  184. // nt: not sure if this is the right implementation here, should iterate ?
  185. xnatObject->remove(child);
  186. endRemoveRows();
  187. }
  188. else
  189. {
  190. // xnatObject->removeChild(parent.row());
  191. // nt: not sure if this is the right implementation here, should iterate ?
  192. xnatObject->remove(child);
  193. }
  194. return true;
  195. }
  196. void ctkXnatTreeModel::downloadFile(const QModelIndex& index, const QString& zipFileName)
  197. {
  198. if (!index.isValid())
  199. {
  200. return;
  201. }
  202. this->xnatObject(index)->download(zipFileName);
  203. return;
  204. }
  205. void ctkXnatTreeModel::uploadFile(const QModelIndex& index, const QString& zipFileName)
  206. {
  207. if (!index.isValid())
  208. {
  209. return;
  210. }
  211. ctkXnatObject* xnatObject = this->xnatObject(index);
  212. ctkXnatObject* child = xnatObject->children()[index.row()];
  213. child->upload(zipFileName);
  214. }