ctkDirectoryButton.h 4.6 KB

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