ctkDirectoryButton.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.commontk.org/LICENSE
  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. // CTK includes
  20. #include <ctkPimpl.h>
  21. #include "ctkWidgetsExport.h"
  22. class ctkDirectoryButtonPrivate;
  23. // QFileDialog::Options can be used since Qt 4.7.0 (QT_VERSION >= 0x040700)
  24. // it is disabled to support older Qt versions
  25. //#define USE_QFILEDIALOG_OPTIONS 1
  26. /// ctkDirectoryButton is a QPushButton to select a directory path.
  27. /// The absolute path is displayed on the button. When clicked, a
  28. /// file dialog pops up to select a new directory path.
  29. /// \sa QPushButton, QDir
  30. class CTK_WIDGETS_EXPORT ctkDirectoryButton: public QWidget
  31. {
  32. Q_OBJECT
  33. Q_PROPERTY(QString directory READ directory WRITE setDirectory)
  34. Q_PROPERTY(QString caption READ caption WRITE setCaption)
  35. /// Qt versions prior to 4.7.0 didn't expose QFileDialog::Options in the
  36. /// public API. We need to create a custom property that will be used when
  37. /// instanciating a QFileDialog in ctkDirectoryButton::browse()
  38. #ifdef USE_QFILEDIALOG_OPTIONS
  39. Q_PROPERTY(QFileDialog::Options options READ options WRITE setOptions)
  40. #else
  41. Q_PROPERTY(Options options READ options WRITE setOptions)
  42. Q_FLAGS(Option Options);
  43. #endif
  44. public:
  45. #ifndef USE_QFILEDIALOG_OPTIONS
  46. // Same options than QFileDialog::Options
  47. enum Option
  48. {
  49. ShowDirsOnly = 0x00000001,
  50. DontResolveSymlinks = 0x00000002,
  51. DontConfirmOverwrite = 0x00000004,
  52. DontUseSheet = 0x00000008,
  53. DontUseNativeDialog = 0x00000010,
  54. ReadOnly = 0x00000020,
  55. HideNameFilterDetails = 0x00000040
  56. };
  57. Q_DECLARE_FLAGS(Options, Option)
  58. #endif
  59. /// Constructor
  60. /// Creates a default ctkDirectoryButton that points to the application
  61. /// current directory.
  62. ctkDirectoryButton(QWidget * parent = 0);
  63. /// Constructor
  64. /// Creates a ctkDirectoryButton that points to the given directory path
  65. ctkDirectoryButton(const QString& directory, QWidget * parent = 0);
  66. ctkDirectoryButton(const QIcon& icon, const QString& directory, QWidget * parent = 0);
  67. /// Destructor
  68. virtual ~ctkDirectoryButton();
  69. /// Set/get the current directory
  70. void setDirectory(const QString& directory);
  71. QString directory()const;
  72. ///
  73. /// The title of the file dialog used to select a new directory
  74. /// If caption is not set, internally use QWidget::tooltip()
  75. void setCaption(const QString& caption);
  76. const QString& caption()const;
  77. /// Options of the file dialog pop up.
  78. /// \sa QFileDialog::getExistingDirectory
  79. #ifdef USE_QFILEDIALOG_OPTIONS
  80. void setOptions(const QFileDialog::Options& options);
  81. const QFileDialog::Options& options()const;
  82. #else
  83. void setOptions(const Options& options);
  84. const Options& options()const;
  85. #endif
  86. public slots:
  87. /// browse() opens a pop up where the user can select a new directory for the
  88. /// button. browse() is automatically called when the button is clicked.
  89. void browse();
  90. signals:
  91. /// directoryChanged is emitted when the current directory changes.
  92. /// Programatically or by the user via the file dialog that pop up when
  93. /// clicking on the button.
  94. void directoryChanged(const QString&);
  95. /// directorySelected() is emitted anytime the current directory is set
  96. /// (even if the new directory is the same than the current value)
  97. void directorySelected(const QString&);
  98. protected:
  99. QScopedPointer<ctkDirectoryButtonPrivate> d_ptr;
  100. private:
  101. Q_DECLARE_PRIVATE(ctkDirectoryButton);
  102. Q_DISABLE_COPY(ctkDirectoryButton);
  103. };
  104. #ifndef USE_QFILEDIALOG_OPTIONS
  105. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkDirectoryButton::Options);
  106. #endif
  107. #endif