ctkDirectoryButton.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. /// ctkDirectoryButton is a QPushButton to select a directory path.
  24. /// The absolute path is displayed on the button. When clicked, a
  25. /// file dialog pops up to select a new directory path.
  26. /// \sa QPushButton, QDir
  27. class CTK_WIDGETS_EXPORT ctkDirectoryButton: public QWidget
  28. {
  29. Q_OBJECT
  30. Q_PROPERTY(QString directory READ directory WRITE setDirectory)
  31. Q_PROPERTY(QString caption READ caption WRITE setCaption)
  32. #if QT_VERSION >= 0x040700
  33. Q_PROPERTY(QFileDialog::Options options READ options WRITE setOptions)
  34. #else
  35. Q_PROPERTY(Options options READ options WRITE setOptions)
  36. // QFileDialog::Options is not a meta-type, we need to create our own.
  37. Q_FLAGS(Option Options);
  38. #endif
  39. public:
  40. #if QT_VERSION < 0x040700
  41. // QFileDialog::Options is not a meta-type, we need to create our own.
  42. enum Option
  43. {
  44. ShowDirsOnly = 0x00000001,
  45. DontResolveSymlinks = 0x00000002,
  46. DontConfirmOverwrite = 0x00000004,
  47. DontUseSheet = 0x00000008,
  48. DontUseNativeDialog = 0x00000010,
  49. ReadOnly = 0x00000020,
  50. HideNameFilterDetails = 0x00000040
  51. };
  52. Q_DECLARE_FLAGS(Options, Option)
  53. #endif
  54. /// Constructor
  55. /// Creates a default ctkDirectoryButton that points to the application
  56. /// current directory.
  57. ctkDirectoryButton(QWidget * parent = 0);
  58. /// Constructor
  59. /// Creates a ctkDirectoryButton that points to the given directory path
  60. ctkDirectoryButton(const QString& directory, QWidget * parent = 0);
  61. ctkDirectoryButton(const QIcon& icon, const QString& directory, QWidget * parent = 0);
  62. /// Set/get the current directory
  63. void setDirectory(const QString& directory);
  64. QString directory()const;
  65. ///
  66. /// The title of the file dialog used to select a new directory
  67. /// If caption is not set, internally use QWidget::tooltip()
  68. void setCaption(const QString& caption);
  69. const QString& caption()const;
  70. /// Options of the file dialog pop up.
  71. /// \sa QFileDialog::getExistingDirectory
  72. #if QT_VERSION >= 0x040700
  73. void setOptions(const QFileDialog::Options& options);
  74. const QFileDialog::Options& options()const;
  75. #else
  76. void setOptions(const Options& options);
  77. const Options& options()const;
  78. #endif
  79. public slots:
  80. /// browse() opens a pop up where the user can select a new directory for the
  81. /// button. browse() is automatically called when the button is clicked.
  82. void browse();
  83. signals:
  84. /// directoryChanged is emitted when the current directory changes.
  85. /// Programatically or by the user via the file dialog that pop up when
  86. /// clicking on the button.
  87. void directoryChanged(const QString&);
  88. /// directorySelected() is emitted anytime the current directory is set
  89. /// (even if the new directory is the same than the current value)
  90. void directorySelected(const QString&);
  91. private:
  92. CTK_DECLARE_PRIVATE(ctkDirectoryButton);
  93. };
  94. #if QT_VERSION < 0x040700
  95. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkDirectoryButton::Options);
  96. #endif
  97. #endif