ctkDirectoryListView.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. /*==============================================================================
  15. Program: 3D Slicer
  16. Copyright (c) Kitware Inc.
  17. See COPYRIGHT.txt
  18. or http://www.slicer.org/copyright/copyright.txt for details.
  19. Unless required by applicable law or agreed to in writing, software
  20. distributed under the License is distributed on an "AS IS" BASIS,
  21. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. See the License for the specific language governing permissions and
  23. limitations under the License.
  24. This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
  25. and was partially funded by NIH grant 3P41RR013218-12S1
  26. ==============================================================================*/
  27. // Qt includes
  28. #include <QFileInfo>
  29. #include <QHBoxLayout>
  30. #include <QListView>
  31. #include <QStandardItemModel>
  32. // QtGUI includes
  33. #include "ctkDirectoryListView.h"
  34. // --------------------------------------------------------------------------
  35. // ctkDirectoryListViewPrivate
  36. //-----------------------------------------------------------------------------
  37. class ctkDirectoryListViewPrivate
  38. {
  39. Q_DECLARE_PUBLIC(ctkDirectoryListView);
  40. protected:
  41. ctkDirectoryListView* const q_ptr;
  42. public:
  43. ctkDirectoryListViewPrivate(ctkDirectoryListView& object);
  44. void init();
  45. void addDirectory(const QString& path);
  46. enum
  47. {
  48. AbsolutePathRole = Qt::UserRole + 1
  49. };
  50. QListView* ListView;
  51. QStandardItemModel DirectoryListModel;
  52. };
  53. // --------------------------------------------------------------------------
  54. // ctkDirectoryListViewPrivate methods
  55. // --------------------------------------------------------------------------
  56. ctkDirectoryListViewPrivate::ctkDirectoryListViewPrivate(ctkDirectoryListView& object)
  57. :q_ptr(&object)
  58. {
  59. }
  60. // --------------------------------------------------------------------------
  61. void ctkDirectoryListViewPrivate::init()
  62. {
  63. Q_Q(ctkDirectoryListView);
  64. this->ListView = new QListView();
  65. this->ListView->setSelectionBehavior(QAbstractItemView::SelectRows);
  66. this->ListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
  67. this->ListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
  68. QHBoxLayout * layout = new QHBoxLayout();
  69. layout->setContentsMargins(0, 0, 0, 0);
  70. layout->addWidget(this->ListView);
  71. q->setLayout(layout);
  72. this->ListView->setModel(&this->DirectoryListModel);
  73. }
  74. // --------------------------------------------------------------------------
  75. void ctkDirectoryListViewPrivate::addDirectory(const QString& path)
  76. {
  77. Q_Q(ctkDirectoryListView);
  78. QString absolutePath = QFileInfo(path).absoluteFilePath();
  79. if (!QFile::exists(absolutePath) || q->hasDirectory(absolutePath))
  80. {
  81. return;
  82. }
  83. QStandardItem * item = new QStandardItem(path);
  84. item->setData(QVariant(absolutePath), Qt::ToolTipRole);
  85. item->setData(QVariant(absolutePath), ctkDirectoryListViewPrivate::AbsolutePathRole);
  86. this->DirectoryListModel.appendRow(item);
  87. }
  88. // --------------------------------------------------------------------------
  89. // ctkDirectoryListView methods
  90. // --------------------------------------------------------------------------
  91. ctkDirectoryListView::ctkDirectoryListView(QWidget* _parent)
  92. : Superclass(_parent)
  93. , d_ptr(new ctkDirectoryListViewPrivate(*this))
  94. {
  95. Q_D(ctkDirectoryListView);
  96. d->init();
  97. }
  98. // --------------------------------------------------------------------------
  99. ctkDirectoryListView::~ctkDirectoryListView()
  100. {
  101. }
  102. // --------------------------------------------------------------------------
  103. QStringList ctkDirectoryListView::directoryList(bool absolutePath)const
  104. {
  105. Q_D(const ctkDirectoryListView);
  106. QStringList directoryList;
  107. int role = Qt::DisplayRole;
  108. if (absolutePath)
  109. {
  110. role = ctkDirectoryListViewPrivate::AbsolutePathRole;
  111. }
  112. for(int i = 0; i < d->DirectoryListModel.rowCount(); ++i)
  113. {
  114. directoryList << d->DirectoryListModel.data(d->DirectoryListModel.index(i, 0), role).toString();
  115. }
  116. return directoryList;
  117. }
  118. // --------------------------------------------------------------------------
  119. QStringList ctkDirectoryListView::selectedDirectoryList(bool absolutePath)const
  120. {
  121. Q_D(const ctkDirectoryListView);
  122. QStringList directoryList;
  123. int role = Qt::DisplayRole;
  124. if (absolutePath)
  125. {
  126. role = ctkDirectoryListViewPrivate::AbsolutePathRole;
  127. }
  128. QModelIndexList selectedIndexes = d->ListView->selectionModel()->selectedRows();
  129. foreach(const QModelIndex& index, selectedIndexes)
  130. {
  131. directoryList << d->DirectoryListModel.data(index, role).toString();
  132. }
  133. return directoryList;
  134. }
  135. // --------------------------------------------------------------------------
  136. bool ctkDirectoryListView::hasDirectory(const QString& path)const
  137. {
  138. Q_D(const ctkDirectoryListView);
  139. QString absolutePath = QFileInfo(path).absoluteFilePath();
  140. QModelIndexList foundIndexes = d->DirectoryListModel.match(
  141. d->DirectoryListModel.index(0, 0), ctkDirectoryListViewPrivate::AbsolutePathRole,
  142. QVariant(absolutePath));
  143. Q_ASSERT(foundIndexes.size() < 2);
  144. return (foundIndexes.size() != 0);
  145. }
  146. // --------------------------------------------------------------------------
  147. void ctkDirectoryListView::addDirectory(const QString& path)
  148. {
  149. Q_D(ctkDirectoryListView);
  150. d->addDirectory(path);
  151. emit this->directoryListChanged();
  152. }
  153. // --------------------------------------------------------------------------
  154. void ctkDirectoryListView::removeDirectory(const QString& path)
  155. {
  156. Q_D(ctkDirectoryListView);
  157. QList<QStandardItem*> foundItems = d->DirectoryListModel.findItems(path);
  158. Q_ASSERT(foundItems.count() < 2);
  159. if (foundItems.count() == 1)
  160. {
  161. d->DirectoryListModel.removeRow(foundItems.at(0)->row());
  162. emit this->directoryListChanged();
  163. }
  164. }
  165. // --------------------------------------------------------------------------
  166. void ctkDirectoryListView::removeSelectedDirectories()
  167. {
  168. Q_D(ctkDirectoryListView);
  169. QModelIndexList selectedIndexes = d->ListView->selectionModel()->selectedRows();
  170. bool selectedCount = selectedIndexes.count();
  171. while(selectedIndexes.count() > 0)
  172. {
  173. d->DirectoryListModel.removeRow(selectedIndexes.at(0).row());
  174. selectedIndexes = d->ListView->selectionModel()->selectedRows();
  175. }
  176. if (selectedCount)
  177. {
  178. emit this->directoryListChanged();
  179. }
  180. }
  181. // --------------------------------------------------------------------------
  182. void ctkDirectoryListView::selectAllDirectories()
  183. {
  184. Q_D(ctkDirectoryListView);
  185. d->ListView->selectAll();
  186. }
  187. // --------------------------------------------------------------------------
  188. void ctkDirectoryListView::clearDirectorySelection()
  189. {
  190. Q_D(ctkDirectoryListView);
  191. d->ListView->clearSelection();
  192. }
  193. // --------------------------------------------------------------------------
  194. void ctkDirectoryListView::setDirectoryList(const QStringList& paths)
  195. {
  196. Q_D(ctkDirectoryListView);
  197. if (paths.count() == this->directoryList().count())
  198. {
  199. int found = 0;
  200. foreach(const QString& path, paths)
  201. {
  202. if (this->hasDirectory(path))
  203. {
  204. ++found;
  205. }
  206. }
  207. if (found == paths.count())
  208. {
  209. return;
  210. }
  211. }
  212. d->DirectoryListModel.removeRows(0, d->DirectoryListModel.rowCount());
  213. foreach(const QString& path, paths)
  214. {
  215. d->addDirectory(path);
  216. }
  217. emit this->directoryListChanged();
  218. }