ctkPathListWidget.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
  14. and was partially funded by NIH grant 3P41RR013218-12S1
  15. =========================================================================*/
  16. #ifndef __ctkPathListWidget_h
  17. #define __ctkPathListWidget_h
  18. // Qt includes
  19. #include <QListView>
  20. // QtGUI includes
  21. #include "ctkWidgetsExport.h"
  22. class ctkPathListWidgetPrivate;
  23. class QStandardItem;
  24. /// \ingroup Widgets
  25. ///
  26. /// \brief The ctkPathListWidget lists files and/or directories.
  27. ///
  28. /// The ctkPathListWidget is a QListView sub-class tailored specifically for displaying
  29. /// lists of file and/or directory entries. A \e path denotes either a file or a directory.
  30. /// Paths can be relative or absolute and the range of valid paths can be constrained
  31. /// by setting file and directory options.
  32. ///
  33. class CTK_WIDGETS_EXPORT ctkPathListWidget : public QListView
  34. {
  35. Q_OBJECT
  36. /// The current list of paths.
  37. Q_PROPERTY(QStringList paths READ paths WRITE setPaths NOTIFY pathsChanged)
  38. /// The mode for this ctkPathListWidget.
  39. Q_PROPERTY(Mode mode READ mode WRITE setMode)
  40. /// Constraints on the file type.
  41. Q_PROPERTY(PathOptions fileOptions READ fileOptions WRITE setFileOptions)
  42. /// Constraints on the directory type.
  43. Q_PROPERTY(PathOptions directoryOptions READ directoryOptions WRITE setDirectoryOptions)
  44. /// The icon to be shown for a file entry.
  45. Q_PROPERTY(QIcon fileIcon READ fileIcon WRITE setFileIcon RESET unsetFileIcon)
  46. /// The icon to be shown for a directory entry.
  47. Q_PROPERTY(QIcon directoryIcon READ directoryIcon WRITE setDirectoryIcon RESET unsetDirectoryIcon)
  48. Q_FLAGS(PathOption PathOptions)
  49. Q_ENUMS(Mode)
  50. public:
  51. enum
  52. {
  53. /// A role for getting the absolute path from items in this list views model.
  54. AbsolutePathRole = Qt::UserRole + 1
  55. };
  56. /// Describes constraints on paths.
  57. enum PathOption
  58. {
  59. /// No constraints
  60. None = 0x00,
  61. /// The path must exist in the file system.
  62. Exists = 0x01,
  63. /// The path must be readable by the current user.
  64. Readable = 0x02,
  65. /// The path must be writable by the current user.
  66. Writable = 0x04,
  67. /// The path must be executable by the current user.
  68. Executable = 0x08
  69. };
  70. Q_DECLARE_FLAGS(PathOptions, PathOption)
  71. enum Mode
  72. {
  73. /// Allow all paths.
  74. Any = 0,
  75. /// Allow only file entries.
  76. FilesOnly,
  77. /// Allow only directory entries.
  78. DirectoriesOnly
  79. };
  80. /// Superclass typedef
  81. typedef QListView Superclass;
  82. /// Constructor
  83. explicit ctkPathListWidget(QWidget* parent = 0);
  84. /// Destructor
  85. virtual ~ctkPathListWidget();
  86. /// \return The current widget mode.
  87. Mode mode() const;
  88. /// \return The QIcon used for file entries.
  89. QIcon fileIcon() const;
  90. /// Sets a QIcon to be used for file entries.
  91. /// \param icon The new file entry icon.
  92. void setFileIcon(const QIcon& icon);
  93. /// Un-set any custom file icon.
  94. void unsetFileIcon();
  95. /// \return The QIcon used for directory entries.
  96. QIcon directoryIcon() const;
  97. /// Sets a QIcon to be used for directory entries.
  98. /// \param icon The new directory entry icon.
  99. void setDirectoryIcon(const QIcon& icon);
  100. /// Un-set any custom directory icon.
  101. void unsetDirectoryIcon();
  102. /// \return The file entry constraints.
  103. PathOptions fileOptions() const;
  104. /// Set new file entry constraints.
  105. /// \param fileOptions The file entry constraints.
  106. void setFileOptions(PathOptions fileOptions);
  107. /// \return The directory entry constraints.
  108. PathOptions directoryOptions() const;
  109. /// Set new directory entry constraints.
  110. /// \param directoryOptions The directory entry constraints.
  111. void setDirectoryOptions(PathOptions directoryOptions);
  112. /// Checks if an entry with the given \a path already exists.
  113. /// \return <code>true</code> if the \a path has already been added, <code>false</code> otherwise.
  114. bool contains(const QString& path)const;
  115. /// Get all file entries.
  116. /// \param absolutePath If <code>true</code>, resolve all entries to absolute paths.
  117. /// \return A list of all file entries.
  118. QStringList files(bool absolutePath = false) const;
  119. /// Get all directory entries.
  120. /// \param absolutePath If <code>true</code>, resolve all entries to absolute paths.
  121. /// \return A list of all directory entries.
  122. QStringList directories(bool absolutePath = false) const;
  123. /// Get all path entries.
  124. /// \param absolutePath If <code>true</code>, resolve all entries to absolute paths.
  125. /// \return A list of all entries.
  126. QStringList paths(bool absolutePath = false) const;
  127. /// Get all selected path entries.
  128. /// \param absolutePath If <code>true</code>, resolve all entries to absolute paths.
  129. /// \return A list of all selected entries.
  130. QStringList selectedPaths(bool absolutePath = false) const;
  131. /// Get the currently focused path entry.
  132. /// \param absolutePath If <code>true</code>, resolve all entries to absolute paths.
  133. /// \return The focused path entry or a null QString if no entry is focused.
  134. QString currentPath(bool absolutePath = false) const;
  135. /// \return The current entry count.
  136. int count() const;
  137. /// \return The absolute path for \a row or a null QString if \a row is out of range.
  138. QString path(int row) const;
  139. /// \return The item for \a row or NULL if \a row is out of range.
  140. QStandardItem* item(int row) const;
  141. /// \return The item for the given absolute path or NULL if the the path is not known.
  142. QStandardItem* item(const QString& absolutePath) const;
  143. /// \return The absolute path for the entry located at the point \a point (in the
  144. /// widget coordinate system) or a null QString if no entry could be found for \a point.
  145. QString pathAt(const QPoint& point) const;
  146. /// \return The item for the entry located at the point \a point (in the widget
  147. /// coordinate system) or NULL if no ite could be found for \a point.
  148. QStandardItem* itemAt(const QPoint& point) const;
  149. /// \see pathAt(const QPoint&)
  150. QString pathAt(int x, int y) const { return pathAt(QPoint(x, y)); }
  151. /// \see itemAt(const QPoint&)
  152. QStandardItem* itemAt(int x, int y) const { return itemAt(QPoint(x, y)); }
  153. /// \return The row number for the given \a path or -1 if \a path is not in the list of current
  154. /// entries.
  155. int row(const QString& path) const;
  156. /// Changes \a oldPath to the new value given by \a newPath. Does nothing if \a oldPath is not
  157. /// in the list or \a newPath does not fullfill the current path options (constraints).
  158. /// \param oldPath The path to be edited.
  159. /// \param newPath The new path replacing \a oldPath.
  160. /// \return <code>true</code> if the old path was successfully changed, <code>false</code> otherwise.
  161. bool editPath(const QString& oldPath, const QString& newPath);
  162. /// Changes the path value of \a index to \a newPath.
  163. /// \param index The model index for which the path will be changed.
  164. /// \param newPath The new path replacing the path value of \a index.
  165. ///
  166. /// \sa editPath(const QString&, const QString&)
  167. bool editPath(const QModelIndex& index, const QString& newPath);
  168. /// \return Returns <code>true</code> if the given path is treated as a file,
  169. /// <code>false</code> otherwise.
  170. bool isFile(const QString& path) const;
  171. /// \return Returns <code>true</code> if the given path is treated as a directory,
  172. /// <code>false</code> otherwise.
  173. bool isDirectory(const QString& path) const;
  174. public Q_SLOTS:
  175. /// Set the mode for controlling the path type.
  176. /// \param mode The path mode.
  177. void setMode(Mode mode);
  178. /// Depending on the mode and path constraints, add \a path to the entry list and emit signal pathListChanged().
  179. /// \param path The path to add.
  180. /// \return <code>true</code> if the path was added, <code>false</code> otherwise.
  181. ///
  182. /// \sa pathListChanged()
  183. bool addPath(const QString& path);
  184. /// Depending on the mode and path constraints, add \a paths to the entry list and emit signal pathListChanged().
  185. /// \param paths The paths to add.
  186. /// \return The absolute paths of all added entries from \a paths.
  187. ///
  188. /// \sa pathListChanged()
  189. QStringList addPaths(const QStringList& paths);
  190. /// Remove all entries and set all valid entries in \a paths as the current list.
  191. /// The signal pathListChanged() is emitted if the old list of paths is
  192. /// different from the provided one.
  193. /// \param paths The new path list.
  194. ///
  195. /// \sa addPaths(), pathListChanged()
  196. void setPaths(const QStringList& paths);
  197. /// Remove \a path from the list.
  198. /// The signal pathListChanged() is emitted if the path was in the list.
  199. /// \param path The path to remove.
  200. /// \return <code>true</code> if \a path was removed, <code>false</code> otherwise.
  201. ///
  202. /// \sa pathListChanged()
  203. bool removePath(const QString& path);
  204. /// Remove \a paths from the list.
  205. /// \param paths The paths to remove.
  206. /// \return The absolute paths of all removed entries from \a paths.
  207. QStringList removePaths(const QStringList& paths);
  208. /// Remove all currently selected paths from the list.
  209. void removeSelectedPaths();
  210. /// Remove all paths from the list.
  211. void clear();
  212. Q_SIGNALS:
  213. /// This signal is emitted when paths are added or removed to the list.
  214. /// \param added The newly added absolute paths.
  215. /// \param removed The removed absolute paths.
  216. void pathsChanged(const QStringList& added, const QStringList& removed);
  217. /// The user clicked on a path entry.
  218. void pathClicked(const QString& absolutePath);
  219. /// The user double-clicked on a path entry.
  220. void pathDoubleClicked(const QString& absolutePath);
  221. /// This signal is emitted when the \a absolutePath entry is activated. The entry is activated when the user
  222. /// clicks or double clicks on it, depending on the system configuration. It is also activated
  223. /// when the user presses the activation key (on Windows and X11 this is the Return key, on
  224. /// Mac OS X it is Ctrl+0).
  225. void pathActivated(const QString& absolutePath);
  226. /// This signal is emitted whenever the current item changes.
  227. /// \param currentAbsolutePath The new current path entry.
  228. /// \param previousAbsolutePath The path entry that previously had the focus.
  229. void currentPathChanged(const QString& currentAbsolutePath, const QString& previousAbsolutePath);
  230. protected:
  231. QScopedPointer<ctkPathListWidgetPrivate> d_ptr;
  232. private:
  233. void setModel(QAbstractItemModel *model);
  234. Q_DECLARE_PRIVATE(ctkPathListWidget)
  235. Q_DISABLE_COPY(ctkPathListWidget)
  236. Q_PRIVATE_SLOT(d_func(), void _q_emitPathClicked(const QModelIndex& index))
  237. Q_PRIVATE_SLOT(d_func(), void _q_emitPathDoubleClicked(const QModelIndex& index))
  238. Q_PRIVATE_SLOT(d_func(), void _q_emitPathActivated(const QModelIndex& index))
  239. Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentPathChanged(const QModelIndex &previous, const QModelIndex &current))
  240. };
  241. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathListWidget::PathOptions)
  242. #endif