ctkDirectoryButton.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. Q_PROPERTY(QString directory READ directory WRITE setDirectory NOTIFY directoryChanged USER true)
  36. /// This property holds the title of the file dialog used to select a new directory
  37. /// If caption is not set, internally use QWidget::tooltip()
  38. Q_PROPERTY(QString caption READ caption WRITE setCaption)
  39. /// This property holds the text to display on the button. If null (by
  40. /// default), the current directory path is displayed instead.
  41. Q_PROPERTY(QString text READ text WRITE setText)
  42. /// This property holds the icon displayed on the button. QStyle::SP_DirIcon
  43. /// by default.
  44. Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
  45. /// Qt versions prior to 4.7.0 didn't expose QFileDialog::Options in the
  46. /// public API. We need to create a custom property that will be used when
  47. /// instanciating a QFileDialog in ctkDirectoryButton::browse()
  48. #ifdef USE_QFILEDIALOG_OPTIONS
  49. Q_PROPERTY(QFileDialog::Options options READ options WRITE setOptions)
  50. #else
  51. Q_PROPERTY(Options options READ options WRITE setOptions)
  52. Q_FLAGS(Option Options);
  53. #endif
  54. public:
  55. #ifndef USE_QFILEDIALOG_OPTIONS
  56. // Same options than QFileDialog::Options
  57. enum Option
  58. {
  59. ShowDirsOnly = 0x00000001,
  60. DontResolveSymlinks = 0x00000002,
  61. DontConfirmOverwrite = 0x00000004,
  62. DontUseSheet = 0x00000008,
  63. DontUseNativeDialog = 0x00000010,
  64. ReadOnly = 0x00000020,
  65. HideNameFilterDetails = 0x00000040
  66. };
  67. Q_DECLARE_FLAGS(Options, Option)
  68. #endif
  69. /// Constructor
  70. /// Creates a default ctkDirectoryButton that points to the application
  71. /// current directory.
  72. ctkDirectoryButton(QWidget * parent = 0);
  73. /// Constructor
  74. /// Creates a ctkDirectoryButton that points to the given directory path
  75. ctkDirectoryButton(const QString& directory, QWidget * parent = 0);
  76. ctkDirectoryButton(const QIcon& icon, const QString& directory, QWidget * parent = 0);
  77. /// Destructor
  78. virtual ~ctkDirectoryButton();
  79. /// Set/get the current directory
  80. /// If path is empty, the program's working directory, ("."), is used.
  81. /// By default, \a directory is the current working directory.
  82. void setDirectory(const QString& path);
  83. QString directory()const;
  84. /// Set the caption of the directory dialog
  85. /// \sa caption
  86. void setCaption(const QString& caption);
  87. /// Get the caption of the directory dialog
  88. /// \sa setCaption
  89. const QString& caption()const;
  90. /// Set the text of the button. If null (not just empty), the directory path
  91. /// is used as text. This doesn't set the "directory", just the displayed text.
  92. /// \sa setDirectory
  93. void setText(const QString& text);
  94. /// Return the text of the button if any. Doesn't return the directory path.
  95. /// \sa directory
  96. const QString& text()const;
  97. ///
  98. /// The icon of the button
  99. /// By default use QStyle::SP_DirIcon
  100. void setIcon(const QIcon& icon);
  101. QIcon icon()const;
  102. /// Options of the file dialog pop up.
  103. /// \sa QFileDialog::getExistingDirectory
  104. #ifdef USE_QFILEDIALOG_OPTIONS
  105. void setOptions(const QFileDialog::Options& options);
  106. const QFileDialog::Options& options()const;
  107. #else
  108. void setOptions(const Options& options);
  109. const Options& options()const;
  110. #endif
  111. public Q_SLOTS:
  112. /// browse() opens a pop up where the user can select a new directory for the
  113. /// button. browse() is automatically called when the button is clicked.
  114. void browse();
  115. Q_SIGNALS:
  116. /// directoryChanged is emitted when the current directory changes.
  117. /// Programatically or by the user via the file dialog that pop up when
  118. /// clicking on the button.
  119. /// \sa directorySelected
  120. void directoryChanged(const QString&);
  121. /// directorySelected() is emitted anytime the current directory is set
  122. /// (even if the new directory is the same than the current value). This is
  123. /// particularly useful when the browse dialog is accepted without changing
  124. /// the current directory.
  125. /// \sa directoryChanged
  126. void directorySelected(const QString&);
  127. protected:
  128. QScopedPointer<ctkDirectoryButtonPrivate> d_ptr;
  129. private:
  130. Q_DECLARE_PRIVATE(ctkDirectoryButton);
  131. Q_DISABLE_COPY(ctkDirectoryButton);
  132. };
  133. #ifndef USE_QFILEDIALOG_OPTIONS
  134. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkDirectoryButton::Options);
  135. #endif
  136. #endif