ctkPathLineEdit.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: Maverick
  16. Module: $RCSfile: config.h,v $
  17. Copyright (c) Kitware Inc. 28 Corporate Drive,
  18. Clifton Park, NY, 12065, USA.
  19. All rights reserved. No part of this software may be reproduced, distributed,
  20. or modified, in any form or by any means, without permission in writing from
  21. Kitware Inc.
  22. IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
  23. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  24. OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
  25. EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
  27. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  28. PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
  29. "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
  30. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  31. =========================================================================*/
  32. #ifndef __ctkPathLineEdit_h
  33. #define __ctkPathLineEdit_h
  34. // Qt includes
  35. #include <QDir>
  36. #include <QWidget>
  37. class QComboBox;
  38. // CTK includes
  39. #include "ctkWidgetsExport.h"
  40. class ctkPathLineEditPrivate;
  41. /// \ingroup Widgets
  42. /// \brief Advanced line edit to select a file or directory.
  43. /// \sa ctkDirectoryButton, ctkPathListWidget
  44. ///
  45. class CTK_WIDGETS_EXPORT ctkPathLineEdit: public QWidget
  46. {
  47. Q_OBJECT
  48. Q_FLAGS(Filters)
  49. Q_PROPERTY ( QString label READ label WRITE setLabel )
  50. Q_PROPERTY ( Filters filters READ filters WRITE setFilters)
  51. Q_PROPERTY ( QString currentPath READ currentPath WRITE setCurrentPath USER true )
  52. /// Qt versions prior to 4.7.0 didn't expose QFileDialog::Options in the
  53. /// public API. We need to create a custom property that will be used when
  54. /// instanciating a QFileDialog in ctkPathLineEdit::browse()
  55. #ifdef USE_QFILEDIALOG_OPTIONS
  56. Q_PROPERTY(QFileDialog::Options options READ options WRITE setOptions)
  57. #else
  58. Q_PROPERTY(Options options READ options WRITE setOptions)
  59. Q_FLAGS(Option Options)
  60. #endif
  61. /// This property controls the key used to search the settings for recorded
  62. /// paths.
  63. /// If multiple path line edits share the same key, their history is then
  64. /// shared.
  65. /// If an empty key string is given, the object name is used as key.
  66. /// Setting the key automatically retrieve the history from settings
  67. /// Empty by default.
  68. /// \sa retrieveHistory(), addCurrentPathToHistory(), showHistoryButton
  69. Q_PROPERTY(QString settingKey READ settingKey WRITE setSettingKey )
  70. /// This property controls whether the browse ("...") button is visible or
  71. /// not. Clicking on the button calls opens a dialog to select the current path.
  72. /// True by default
  73. /// \sa browse()
  74. Q_PROPERTY(bool showBrowseButton READ showBrowseButton WRITE setShowBrowseButton)
  75. /// This property controls whether the history button (arrow button that opens
  76. /// the history menu) is visible or not.
  77. /// True by default.
  78. /// \sa retrieveHistory(), addCurrentPathToHistory(), settingKey
  79. Q_PROPERTY(bool showHistoryButton READ showHistoryButton WRITE setShowHistoryButton)
  80. /// This property holds the policy describing how the size of the path line edit widget
  81. /// changes when the content changes.
  82. /// The default value is AdjustToContentsOnFirstShow.
  83. Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy)
  84. /// This property holds the minimum number of characters that should fit into
  85. /// the path line edit.
  86. /// The default value is 0.
  87. /// If this property is set to a positive value, the minimumSizeHint() and sizeHint() take it into account.
  88. Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
  89. /// This property holds the list of regular expressions (in wildcard mode) used to help the user
  90. /// complete a line.
  91. /// For example: "Images (*.jpg *.gif *.png)"
  92. Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters)
  93. public:
  94. enum Filter { Dirs = 0x001,
  95. Files = 0x002,
  96. Drives = 0x004,
  97. NoSymLinks = 0x008,
  98. AllEntries = Dirs | Files | Drives,
  99. TypeMask = 0x00f,
  100. Readable = 0x010,
  101. Writable = 0x020,
  102. Executable = 0x040,
  103. PermissionMask = 0x070,
  104. Modified = 0x080,
  105. Hidden = 0x100,
  106. System = 0x200,
  107. AccessMask = 0x3F0,
  108. AllDirs = 0x400,
  109. CaseSensitive = 0x800,
  110. NoDotAndDotDot = 0x1000, // ### Qt5 NoDotAndDotDot = NoDot|NoDotDot
  111. NoDot = 0x2000,
  112. NoDotDot = 0x4000,
  113. NoFilter = -1
  114. };
  115. Q_DECLARE_FLAGS(Filters, Filter)
  116. #ifndef USE_QFILEDIALOG_OPTIONS
  117. // Same options than QFileDialog::Options
  118. enum Option
  119. {
  120. ShowDirsOnly = 0x00000001,
  121. DontResolveSymlinks = 0x00000002,
  122. DontConfirmOverwrite = 0x00000004,
  123. DontUseSheet = 0x00000008,
  124. DontUseNativeDialog = 0x00000010,
  125. ReadOnly = 0x00000020,
  126. HideNameFilterDetails = 0x00000040
  127. };
  128. Q_DECLARE_FLAGS(Options, Option)
  129. #endif
  130. enum SizeAdjustPolicy
  131. {
  132. /// The path line edit will always adjust to the contents.
  133. AdjustToContents,
  134. /// The path line edit will adjust to its contents the first time it is shown.
  135. AdjustToContentsOnFirstShow,
  136. /// The combobox will adjust to minimumContentsLength. For performance reasons
  137. /// use this policy on large models.
  138. AdjustToMinimumContentsLength
  139. };
  140. /** Default constructor
  141. */
  142. ctkPathLineEdit(QWidget *parent = 0);
  143. /** Constructor
  144. * /param label Used in file dialogs
  145. * /param nameFilters Regular expression (in wildcard mode) used to help the user to complete the line,
  146. * example: "Images (*.jpg *.gif *.png)"
  147. * /param parent Parent widget
  148. */
  149. ctkPathLineEdit( const QString& label,
  150. const QStringList& nameFilters,
  151. Filters filters = ctkPathLineEdit::AllEntries,
  152. QWidget *parent=0 );
  153. virtual ~ctkPathLineEdit();
  154. QString currentPath()const;
  155. void setLabel(const QString &label);
  156. const QString& label()const;
  157. void setNameFilters(const QStringList &nameFilters);
  158. const QStringList& nameFilters()const;
  159. void setFilters(const Filters& filters);
  160. Filters filters()const;
  161. /// Options of the file dialog pop up.
  162. /// \sa QFileDialog::getExistingDirectory
  163. #ifdef USE_QFILEDIALOG_OPTIONS
  164. void setOptions(const QFileDialog::Options& options);
  165. const QFileDialog::Options& options()const;
  166. #else
  167. void setOptions(const Options& options);
  168. const Options& options()const;
  169. #endif
  170. /// Change the current extension of the edit line.
  171. /// If there is no extension yet, set it
  172. void setCurrentFileExtension(const QString& extension);
  173. QString settingKey()const;
  174. void setSettingKey(const QString& key);
  175. bool showBrowseButton()const;
  176. void setShowBrowseButton(bool visible);
  177. bool showHistoryButton()const;
  178. void setShowHistoryButton(bool visible);
  179. /// the policy describing how the size of the combobox changes
  180. /// when the content changes
  181. ///
  182. /// The default value is \c AdjustToContentsOnFirstShow.
  183. ///
  184. /// \sa SizeAdjustPolicy
  185. SizeAdjustPolicy sizeAdjustPolicy() const;
  186. void setSizeAdjustPolicy(SizeAdjustPolicy policy);
  187. int minimumContentsLength()const;
  188. void setMinimumContentsLength(int lenght);
  189. /// Return the combo box internally used by the path line edit
  190. QComboBox* comboBox() const;
  191. /// The width returned, in pixels, is the length of the file name (with no
  192. /// path) if any. Otherwise, it's enough for 15 to 20 characters.
  193. virtual QSize minimumSizeHint()const;
  194. /// The width returned, in pixels, is the entire length of the current path
  195. /// if any. Otherwise, it's enough for 15 to 20 characters.
  196. virtual QSize sizeHint()const;
  197. Q_SIGNALS:
  198. /** the signal is emit when the state of hasValidInput changed
  199. */
  200. void validInputChanged(bool);
  201. void currentPathChanged(const QString& path);
  202. public Q_SLOTS:
  203. void setCurrentPath(const QString& path);
  204. /// Open a QFileDialog to select a file or directory and set current text to it
  205. /// You would probably connect a browse push button like this:
  206. /// connect(myPushButton,SIGNAL(clicked()),myPathLineEdit,SLOT(browse()))
  207. /// As a conveniency, such button is provided by default via the browseButton
  208. /// \sa showBrowseButton
  209. void browse();
  210. /// Load the history of the paths that have been saved in the application
  211. /// settings with addCurrentPathToHistory().
  212. /// The history is identified using \a settingKey
  213. /// \sa addCurrentPathToHistory(), showHistoryButton, settingKey
  214. void retrieveHistory();
  215. /// Save the current value (this->currentPath()) into the history. That value
  216. /// will be retrieved next time retrieveHistory() is called.
  217. /// \sa retrieveHistory(), showHistoryButton, settingKey
  218. void addCurrentPathToHistory();
  219. protected Q_SLOTS:
  220. void setCurrentDirectory(const QString& directory);
  221. void updateHasValidInput();
  222. protected:
  223. QScopedPointer<ctkPathLineEditPrivate> d_ptr;
  224. private:
  225. Q_DECLARE_PRIVATE(ctkPathLineEdit);
  226. Q_DISABLE_COPY(ctkPathLineEdit);
  227. Q_PRIVATE_SLOT(d_ptr, void _q_recomputeCompleterPopupSize())
  228. };
  229. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathLineEdit::Filters)
  230. #ifndef USE_QFILEDIALOG_OPTIONS
  231. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathLineEdit::Options);
  232. #endif
  233. #endif // __ctkPathLineEdit_h