ctkPathLineEdit.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /**
  42. * \ingroup Widgets
  43. * \brief Advanced line edit to select file or directory
  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 ( QStringList nameFilters READ nameFilters WRITE setNameFilters)
  51. Q_PROPERTY ( Filters filters READ filters WRITE setFilters)
  52. Q_PROPERTY ( QString currentPath READ currentPath WRITE setCurrentPath USER true )
  53. /// Qt versions prior to 4.7.0 didn't expose QFileDialog::Options in the
  54. /// public API. We need to create a custom property that will be used when
  55. /// instanciating a QFileDialog in ctkPathLineEdit::browse()
  56. #ifdef USE_QFILEDIALOG_OPTIONS
  57. Q_PROPERTY(QFileDialog::Options options READ options WRITE setOptions)
  58. #else
  59. Q_PROPERTY(Options options READ options WRITE setOptions)
  60. Q_FLAGS(Option Options);
  61. #endif
  62. /// This property controls the key used to search the settings for recorded
  63. /// paths.
  64. /// If multiple path line edits share the same key, their history is then
  65. /// shared.
  66. /// If an empty key string is given, the object name is used as key.
  67. /// Setting the key automatically retrieve the history from settings
  68. /// Empty by default.
  69. /// \sa retrieveHistory(), addCurrentPathToHistory(), showHistoryButton
  70. Q_PROPERTY(QString settingKey READ settingKey WRITE setSettingKey )
  71. /// This property controls whether the browse ("...") button is visible or
  72. /// not. Clicking on the button calls opens a dialog to select the current path.
  73. /// True by default
  74. /// \sa browse()
  75. Q_PROPERTY(bool showBrowseButton READ showBrowseButton WRITE setShowBrowseButton);
  76. /// This property controls whether the history button (arrow button that opens
  77. /// the history menu) is visible or not.
  78. /// True by default.
  79. /// \sa retrieveHistory(), addCurrentPathToHistory(), settingKey
  80. Q_PROPERTY(bool showHistoryButton READ showHistoryButton WRITE setShowHistoryButton);
  81. /// This property holds the minimum number of characters that should fit into
  82. /// the path line edit.
  83. /// The default value is 17.
  84. Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
  85. public:
  86. enum Filter { Dirs = 0x001,
  87. Files = 0x002,
  88. Drives = 0x004,
  89. NoSymLinks = 0x008,
  90. AllEntries = Dirs | Files | Drives,
  91. TypeMask = 0x00f,
  92. Readable = 0x010,
  93. Writable = 0x020,
  94. Executable = 0x040,
  95. PermissionMask = 0x070,
  96. Modified = 0x080,
  97. Hidden = 0x100,
  98. System = 0x200,
  99. AccessMask = 0x3F0,
  100. AllDirs = 0x400,
  101. CaseSensitive = 0x800,
  102. NoDotAndDotDot = 0x1000, // ### Qt5 NoDotAndDotDot = NoDot|NoDotDot
  103. NoDot = 0x2000,
  104. NoDotDot = 0x4000,
  105. NoFilter = -1
  106. };
  107. Q_DECLARE_FLAGS(Filters, Filter)
  108. #ifndef USE_QFILEDIALOG_OPTIONS
  109. // Same options than QFileDialog::Options
  110. enum Option
  111. {
  112. ShowDirsOnly = 0x00000001,
  113. DontResolveSymlinks = 0x00000002,
  114. DontConfirmOverwrite = 0x00000004,
  115. DontUseSheet = 0x00000008,
  116. DontUseNativeDialog = 0x00000010,
  117. ReadOnly = 0x00000020,
  118. HideNameFilterDetails = 0x00000040
  119. };
  120. Q_DECLARE_FLAGS(Options, Option)
  121. #endif
  122. /** Default constructor
  123. */
  124. ctkPathLineEdit(QWidget *parent = 0);
  125. /** Constructor
  126. * /param label Used in file dialogs
  127. * /param nameFilters Regular expression (in wildcard mode) used to help the user to complete the line,
  128. * example: "Images (*.jpg *.gif *.png)"
  129. * /param parent Parent widget
  130. */
  131. ctkPathLineEdit( const QString& label,
  132. const QStringList& nameFilters,
  133. Filters filters = ctkPathLineEdit::AllEntries,
  134. QWidget *parent=0 );
  135. virtual ~ctkPathLineEdit();
  136. QString currentPath()const;
  137. void setLabel(const QString &label);
  138. const QString& label()const;
  139. void setNameFilters(const QStringList &nameFilters);
  140. const QStringList& nameFilters()const;
  141. void setFilters(const Filters& filters);
  142. Filters filters()const;
  143. /// Options of the file dialog pop up.
  144. /// \sa QFileDialog::getExistingDirectory
  145. #ifdef USE_QFILEDIALOG_OPTIONS
  146. void setOptions(const QFileDialog::Options& options);
  147. const QFileDialog::Options& options()const;
  148. #else
  149. void setOptions(const Options& options);
  150. const Options& options()const;
  151. #endif
  152. /// Change the current extension of the edit line.
  153. /// If there is no extension yet, set it
  154. void setCurrentFileExtension(const QString& extension);
  155. QString settingKey()const;
  156. void setSettingKey(const QString& key);
  157. bool showBrowseButton()const;
  158. void setShowBrowseButton(bool visible);
  159. bool showHistoryButton()const;
  160. void setShowHistoryButton(bool visible);
  161. int minimumContentsLength()const;
  162. void setMinimumContentsLength(int lenght);
  163. /// Return the combo box internally used by the path line edit
  164. QComboBox* comboBox() const;
  165. /// The width returned, in pixels, is the length of the file name (with no
  166. /// path) if any. Otherwise, it's enough for 15 to 20 characters.
  167. virtual QSize minimumSizeHint()const;
  168. /// The width returned, in pixels, is the entire length of the current path
  169. /// if any. Otherwise, it's enough for 15 to 20 characters.
  170. virtual QSize sizeHint()const;
  171. Q_SIGNALS:
  172. /** the signal is emit when the state of hasValidInput changed
  173. */
  174. void validInputChanged(bool);
  175. void currentPathChanged(const QString& path);
  176. public Q_SLOTS:
  177. void setCurrentPath(const QString& path);
  178. /// Open a QFileDialog to select a file or directory and set current text to it
  179. /// You would probably connect a browse push button like this:
  180. /// connect(myPushButton,SIGNAL(clicked()),myPathLineEdit,SLOT(browse()))
  181. /// As a conveniency, such button is provided by default via the browseButton
  182. /// \sa showBrowseButton
  183. void browse();
  184. /// Load the history of the paths that have been saved in the application
  185. /// settings with addCurrentPathToHistory().
  186. /// The history is identified using \a settingKey
  187. /// \sa addCurrentPathToHistory(), showHistoryButton, settingKey
  188. void retrieveHistory();
  189. /// Save the current value (this->currentPath()) into the history. That value
  190. /// will be retrieved next time retrieveHistory() is called.
  191. /// \sa retrieveHistory(), showHistoryButton, settingKey
  192. void addCurrentPathToHistory();
  193. protected Q_SLOTS:
  194. void setCurrentDirectory(const QString& directory);
  195. void updateHasValidInput();
  196. protected:
  197. QScopedPointer<ctkPathLineEditPrivate> d_ptr;
  198. private:
  199. Q_DECLARE_PRIVATE(ctkPathLineEdit);
  200. Q_DISABLE_COPY(ctkPathLineEdit);
  201. };
  202. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathLineEdit::Filters)
  203. #ifndef USE_QFILEDIALOG_OPTIONS
  204. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathLineEdit::Options);
  205. #endif
  206. #endif // __ctkPathLineEdit_h