ctkColorDialog.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 __ctkColorDialog_h
  15. #define __ctkColorDialog_h
  16. // Qt includes
  17. #include <QColorDialog>
  18. // CTK includes
  19. #include "ctkWidgetsExport.h"
  20. class ctkColorDialogPrivate;
  21. /// \ingroup Widgets
  22. /// Customizable QColorDialog.
  23. /// Extra widgets can be added to the left of the dialog into a QStackedWidget
  24. class CTK_WIDGETS_EXPORT ctkColorDialog : public QColorDialog
  25. {
  26. Q_OBJECT
  27. public:
  28. /// Constructor
  29. /// By default, behaves like a QColorDialog
  30. /// \sa QColorDialog()
  31. explicit ctkColorDialog(QWidget* parent = 0);
  32. explicit ctkColorDialog(const QColor& initial, QWidget* parent = 0);
  33. virtual ~ctkColorDialog();
  34. /// Add an extra widget under the file format combobox. If a label is
  35. /// given, it will appear in the first column.
  36. /// The widget is reparented to ctkColorDialog
  37. /// The ownership of the widget is taken.
  38. /// You must manually connect the color changed signal of the widget
  39. /// to ctkColorDialog::setColor(QColor)
  40. /// Same apply if you want to specify a color name, you must connect the
  41. /// color name changed signal to ctkColorDialog::setColorName(QString) but
  42. /// you have to make sure the color name is set after setColor as it always
  43. /// resets the color name.
  44. inline void addTab(QWidget* widget, const QString& label);
  45. /// Same as addTab(), in addition, \a tabIndex control the tab index of the widget.
  46. /// If index is -1, the tab is appended (same as addDefaultTab). The last
  47. /// tab added with an index of 0 will be the first tab open
  48. void insertTab(int tabIndex, QWidget* widget, const QString& label);
  49. /// The ownership of widget remains the same. The widget is not deleted,
  50. /// but simply removed from the widget's stacked layout, causing it to be
  51. /// hidden.
  52. void removeTab(int index);
  53. /// Set the current tab index. 0 ("Basic" tab) by default.
  54. void setCurrentTab(int index);
  55. /// Return the extra widget if any
  56. /// Be careful with the "Basic" tab.
  57. QWidget* widget(int index)const;
  58. /// Returns the index position of the page occupied by the widget w,
  59. /// or -1 if the widget cannot be found
  60. int indexOf(QWidget* widget)const;
  61. /// Return the current color name if any has been set.
  62. QString colorName()const;
  63. /// Pops up a modal color dialog with the given window \a title (or "Select Color" if none is
  64. /// specified), lets the user choose a color, and returns that color. The color is initially set
  65. /// to \a initial. The dialog is a child of \a parent. It returns an invalid (see
  66. /// QColor::isValid()) color if the user cancels the dialog.
  67. ///
  68. /// The \a options argument allows you to customize the dialog;
  69. /// QColorDialog::DontUseNativeDialog is forced
  70. static QColor getColor(const QColor &initial, QWidget *parent,
  71. const QString &title, ColorDialogOptions options = 0);
  72. /// Return the last selected color name if any. getColorName() call is only
  73. /// valid after a getColor() call.
  74. /// \sa getColor
  75. static QString getColorName();
  76. /// Add a custom widget as an additional tab of the color dialog created by
  77. /// ctkColorDialog::getColor. \a label is title of the tab and \a signal is the signal fired by
  78. /// the widget whenever a QColor is changed, typically: SIGNAL(currentColorChanged(QColor)). It
  79. /// is internally connected to set the current color of the dialog
  80. static inline void addDefaultTab(QWidget* widget, const QString& label,
  81. const char* colorSignal = 0,
  82. const char* nameSignal = 0);
  83. /// Same as addDefaultTab, in addition, \a tabIndex control the tab index of the widget.
  84. /// If index is -1, the tab is appended (same as addDefaultTab). The last
  85. /// tab added with an index of 0 will be the first tab open
  86. static void insertDefaultTab(int tabIndex, QWidget* widget, const QString& label,
  87. const char* colorSignal = 0,
  88. const char* nameSignal = 0);
  89. /// Index of the tab to make default (active when getColor is called).
  90. /// -1 for the "Basic Colors", it's the default behavior
  91. static void setDefaultTab(int index);
  92. public Q_SLOTS:
  93. /// Slot-ify QColorDialog::setCurrentColor(QColor)
  94. void setColor(const QColor& color);
  95. /// Set the color name.
  96. /// Note that each time the color is changed the name is reset.
  97. void setColorName(const QString& name);
  98. Q_SIGNALS:
  99. void currentColorNameChanged(const QString& colorName);
  100. protected Q_SLOTS:
  101. void resetColorName();
  102. protected:
  103. QScopedPointer<ctkColorDialogPrivate> d_ptr;
  104. static QList<QWidget*> DefaultTabs;
  105. static int DefaultTab;
  106. static QString LastColorName;
  107. private:
  108. Q_DECLARE_PRIVATE(ctkColorDialog);
  109. Q_DISABLE_COPY(ctkColorDialog);
  110. };
  111. //------------------------------------------------------------------------------
  112. void ctkColorDialog::addTab(QWidget* widget, const QString& label)
  113. {
  114. this->insertTab(-1, widget, label);
  115. }
  116. //------------------------------------------------------------------------------
  117. void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label,
  118. const char* colorSignal,
  119. const char* nameSignal)
  120. {
  121. ctkColorDialog::insertDefaultTab(-1, widget, label, colorSignal, nameSignal);
  122. }
  123. #endif