ctkDirectoryButton.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #ifndef __ctkDirectoryButton_h
  15. #define __ctkDirectoryButton_h
  16. // Qt includes
  17. #include <QDir>
  18. #include <QFileDialog>
  19. #include <QIcon>
  20. // CTK includes
  21. #include <ctkPimpl.h>
  22. #include "ctkWidgetsExport.h"
  23. class ctkDirectoryButtonPrivate;
  24. // QFileDialog::Options can be used since Qt 4.7.0 (QT_VERSION >= 0x040700)
  25. // it is disabled to support older Qt versions
  26. //#define USE_QFILEDIALOG_OPTIONS 1
  27. /// \ingroup Widgets
  28. /// ctkDirectoryButton is a QPushButton to select a directory path.
  29. /// The absolute path is displayed on the button. When clicked, a
  30. /// file dialog pops up to select a new directory path.
  31. /// \sa ctkPathLineEdit, ctkPathListWidget, QPushButton, QDir
  32. class CTK_WIDGETS_EXPORT ctkDirectoryButton: public QWidget
  33. {
  34. Q_OBJECT
  35. /// This property holds the accept mode of the dialog.
  36. /// The action mode defines whether the dialog is for opening or saving files.
  37. /// By default, this property is set to AcceptOpen.
  38. /// If set to QFileDialog::AcceptSave mode, the regular behavior of QFileDialog will be extended
  39. /// to prevent user from selecting read-only folder. The caveat is that writable folder existing
  40. /// in a readonly one won't be selectable.
  41. /// AcceptOpen by default.
  42. Q_PROPERTY(QFileDialog::AcceptMode acceptMode READ acceptMode WRITE setAcceptMode)
  43. Q_PROPERTY(QString directory READ directory WRITE setDirectory NOTIFY directoryChanged USER true)
  44. /// This property holds the title of the file dialog used to select a new directory
  45. /// If caption is not set, internally use QWidget::tooltip()
  46. Q_PROPERTY(QString caption READ caption WRITE setCaption)
  47. /// This property holds the text to display on the button. If null (by
  48. /// default), the current directory path is displayed instead.
  49. Q_PROPERTY(QString text READ text WRITE setText)
  50. /// This property holds the icon displayed on the button. QStyle::SP_DirIcon
  51. /// by default.
  52. Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
  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 ctkDirectoryButton::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. #ifndef USE_QFILEDIALOG_OPTIONS
  64. // Same options than QFileDialog::Options
  65. enum Option
  66. {
  67. ShowDirsOnly = 0x00000001,
  68. DontResolveSymlinks = 0x00000002,
  69. DontConfirmOverwrite = 0x00000004,
  70. DontUseSheet = 0x00000008,
  71. DontUseNativeDialog = 0x00000010,
  72. ReadOnly = 0x00000020,
  73. HideNameFilterDetails = 0x00000040
  74. };
  75. Q_DECLARE_FLAGS(Options, Option)
  76. #endif
  77. /// Constructor
  78. /// Creates a default ctkDirectoryButton that points to the application
  79. /// current directory.
  80. ctkDirectoryButton(QWidget * parent = 0);
  81. /// Constructor
  82. /// Creates a ctkDirectoryButton that points to the given directory path
  83. ctkDirectoryButton(const QString& directory, QWidget * parent = 0);
  84. ctkDirectoryButton(const QIcon& icon, const QString& directory, QWidget * parent = 0);
  85. /// Destructor
  86. virtual ~ctkDirectoryButton();
  87. /// Set/get the current directory
  88. /// If path is empty, the program's working directory, ("."), is used.
  89. /// By default, \a directory is the current working directory.
  90. void setDirectory(const QString& path);
  91. QString directory()const;
  92. /// Set the caption of the directory dialog
  93. /// \sa caption
  94. void setCaption(const QString& caption);
  95. /// Get the caption of the directory dialog
  96. /// \sa setCaption
  97. const QString& caption()const;
  98. /// Set the text of the button. If null (not just empty), the directory path
  99. /// is used as text. This doesn't set the "directory", just the displayed text.
  100. /// \sa setDirectory
  101. void setText(const QString& text);
  102. /// Return the text of the button if any. Doesn't return the directory path.
  103. /// \sa directory
  104. const QString& text()const;
  105. ///
  106. /// The icon of the button
  107. /// By default use QStyle::SP_DirIcon
  108. void setIcon(const QIcon& icon);
  109. QIcon icon()const;
  110. /// Options of the file dialog pop up.
  111. /// \sa QFileDialog::getExistingDirectory
  112. #ifdef USE_QFILEDIALOG_OPTIONS
  113. void setOptions(const QFileDialog::Options& options);
  114. const QFileDialog::Options& options()const;
  115. #else
  116. void setOptions(const Options& options);
  117. const Options& options()const;
  118. #endif
  119. /// \sa setAcceptMode QFileDialog::AcceptMode
  120. QFileDialog::AcceptMode acceptMode() const;
  121. /// \sa acceptMode QFileDialog::AcceptMode
  122. void setAcceptMode(QFileDialog::AcceptMode mode);
  123. public Q_SLOTS:
  124. /// browse() opens a pop up where the user can select a new directory for the
  125. /// button. browse() is automatically called when the button is clicked.
  126. void browse();
  127. Q_SIGNALS:
  128. /// directoryChanged is emitted when the current directory changes.
  129. /// Programatically or by the user via the file dialog that pop up when
  130. /// clicking on the button.
  131. /// \sa directorySelected
  132. void directoryChanged(const QString&);
  133. /// directorySelected() is emitted anytime the current directory is set
  134. /// (even if the new directory is the same than the current value). This is
  135. /// particularly useful when the browse dialog is accepted without changing
  136. /// the current directory.
  137. /// \sa directoryChanged
  138. void directorySelected(const QString&);
  139. protected:
  140. QScopedPointer<ctkDirectoryButtonPrivate> d_ptr;
  141. private:
  142. Q_DECLARE_PRIVATE(ctkDirectoryButton);
  143. Q_DISABLE_COPY(ctkDirectoryButton);
  144. };
  145. #ifndef USE_QFILEDIALOG_OPTIONS
  146. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkDirectoryButton::Options);
  147. #endif
  148. #endif