ctkDirectoryButton.h 4.6 KB

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