ctkXnatTreeModel.cpp 6.2 KB

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