ctkPathLineEdit.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <QWidget>
  36. #include <QDir>
  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. public:
  63. enum Filter { Dirs = 0x001,
  64. Files = 0x002,
  65. Drives = 0x004,
  66. NoSymLinks = 0x008,
  67. AllEntries = Dirs | Files | Drives,
  68. TypeMask = 0x00f,
  69. Readable = 0x010,
  70. Writable = 0x020,
  71. Executable = 0x040,
  72. PermissionMask = 0x070,
  73. Modified = 0x080,
  74. Hidden = 0x100,
  75. System = 0x200,
  76. AccessMask = 0x3F0,
  77. AllDirs = 0x400,
  78. CaseSensitive = 0x800,
  79. NoDotAndDotDot = 0x1000, // ### Qt5 NoDotAndDotDot = NoDot|NoDotDot
  80. NoDot = 0x2000,
  81. NoDotDot = 0x4000,
  82. NoFilter = -1
  83. };
  84. Q_DECLARE_FLAGS(Filters, Filter)
  85. #ifndef USE_QFILEDIALOG_OPTIONS
  86. // Same options than QFileDialog::Options
  87. enum Option
  88. {
  89. ShowDirsOnly = 0x00000001,
  90. DontResolveSymlinks = 0x00000002,
  91. DontConfirmOverwrite = 0x00000004,
  92. DontUseSheet = 0x00000008,
  93. DontUseNativeDialog = 0x00000010,
  94. ReadOnly = 0x00000020,
  95. HideNameFilterDetails = 0x00000040
  96. };
  97. Q_DECLARE_FLAGS(Options, Option)
  98. #endif
  99. /** Default constructor
  100. */
  101. ctkPathLineEdit(QWidget *parent = 0);
  102. /** Constructor
  103. * /param label Used in file dialogs
  104. * /param nameFilters Regular expression (in wildcard mode) used to help the user to complete the line,
  105. * example: "Images (*.jpg *.gif *.png)"
  106. * /param parent Parent widget
  107. */
  108. ctkPathLineEdit( const QString& label,
  109. const QStringList& nameFilters,
  110. Filters filters = ctkPathLineEdit::AllEntries,
  111. QWidget *parent=0 );
  112. virtual ~ctkPathLineEdit();
  113. QString currentPath()const;
  114. void setLabel(const QString &label);
  115. const QString& label()const;
  116. void setNameFilters(const QStringList &nameFilters);
  117. const QStringList& nameFilters()const;
  118. void setFilters(const Filters& filters);
  119. Filters filters()const;
  120. /// Options of the file dialog pop up.
  121. /// \sa QFileDialog::getExistingDirectory
  122. #ifdef USE_QFILEDIALOG_OPTIONS
  123. void setOptions(const QFileDialog::Options& options);
  124. const QFileDialog::Options& options()const;
  125. #else
  126. void setOptions(const Options& options);
  127. const Options& options()const;
  128. #endif
  129. /** Change the current extension of the edit line.
  130. * If there is no extension yet, set it
  131. */
  132. void setCurrentFileExtension(const QString& extension);
  133. /// Return the combo box internally used by the path line edit
  134. QComboBox* comboBox() const;
  135. Q_SIGNALS:
  136. /** the signal is emit when the state of hasValidInput changed
  137. */
  138. void validInputChanged(bool);
  139. void currentPathChanged(const QString& path);
  140. public Q_SLOTS:
  141. void setCurrentPath(const QString& path);
  142. /** Open a QFileDialog to select a file or directory and set current text to it
  143. * You would probably connect a browse push button like this:
  144. * connect(myPushButton,SIGNAL(clicked()),myPathLineEdit,SLOT(browse()))
  145. */
  146. void browse();
  147. /** Load the history of the paths. To be restored the inputs must have been saved by
  148. * saveCurrentPathInHistory().
  149. */
  150. void retrieveHistory();
  151. /** Save the current value (this->currentPath()) into the history. That value will be retrieved
  152. * next time the retrieveHistory()
  153. */
  154. void addCurrentPathToHistory();
  155. protected Q_SLOTS:
  156. void setCurrentDirectory(const QString& directory);
  157. void updateHasValidInput();
  158. protected:
  159. QScopedPointer<ctkPathLineEditPrivate> d_ptr;
  160. private:
  161. Q_DECLARE_PRIVATE(ctkPathLineEdit);
  162. Q_DISABLE_COPY(ctkPathLineEdit);
  163. };
  164. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathLineEdit::Filters)
  165. #ifndef USE_QFILEDIALOG_OPTIONS
  166. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkPathLineEdit::Options);
  167. #endif
  168. #endif // __ctkPathLineEdit_h