ctkColorDialog.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. inline void addTab(QWidget* widget, const QString& label);
  41. /// Same as addTab(), in addition, \a tabIndex control the tab index of the widget.
  42. /// If index is -1, the tab is appended (same as addDefaultTab). The last
  43. /// tab added with an index of 0 will be the first tab open
  44. void insertTab(int tabIndex, QWidget* widget, const QString& label);
  45. /// The ownership of widget remains the same. The widget is not deleted,
  46. /// but simply removed from the widget's stacked layout, causing it to be
  47. /// hidden.
  48. void removeTab(int index);
  49. /// Set the current tab index. 0 ("Basic" tab) by default.
  50. void setCurrentTab(int index);
  51. /// Return the extra widget if any
  52. /// Be careful with the "Basic" tab.
  53. QWidget* widget(int index)const;
  54. /// Returns the index position of the page occupied by the widget w,
  55. /// or -1 if the widget cannot be found
  56. int indexOf(QWidget* widget)const;
  57. /// Pops up a modal color dialog with the given window \a title (or "Select Color" if none is
  58. /// specified), lets the user choose a color, and returns that color. The color is initially set
  59. /// to \a initial. The dialog is a child of \a parent. It returns an invalid (see
  60. /// QColor::isValid()) color if the user cancels the dialog.
  61. ///
  62. /// The \a options argument allows you to customize the dialog;
  63. /// QColorDialog::DontUseNativeDialog is forced
  64. static QColor getColor(const QColor &initial, QWidget *parent,
  65. const QString &title, ColorDialogOptions options = 0);
  66. /// Add a custom widget as an additional tab of the color dialog created by
  67. /// ctkColorDialog::getColor. \a label is title of the tab and \a signal is the signal fired by
  68. /// the widget whenever a QColor is changed, typically: SIGNAL(currentColorChanged(QColor)). It
  69. /// is internally connected to set the current color of the dialog
  70. static inline void addDefaultTab(QWidget* widget, const QString& label, const char* signal = 0);
  71. /// Same as addDefaultTab, in addition, \a tabIndex control the tab index of the widget.
  72. /// If index is -1, the tab is appended (same as addDefaultTab). The last
  73. /// tab added with an index of 0 will be the first tab open
  74. static void insertDefaultTab(int tabIndex, QWidget* widget, const QString& label, const char* signal = 0);
  75. /// Index of the tab to make default (active when getColor is called).
  76. /// -1 for the "Basic Colors", it's the default behavior
  77. static void setDefaultTab(int index);
  78. public Q_SLOTS:
  79. /// Slotify QColorDialog::setCurrentColor(QColor)
  80. void setColor(const QColor& color);
  81. protected:
  82. QScopedPointer<ctkColorDialogPrivate> d_ptr;
  83. static QList<QWidget*> DefaultTabs;
  84. static int DefaultTab;
  85. private:
  86. Q_DECLARE_PRIVATE(ctkColorDialog);
  87. Q_DISABLE_COPY(ctkColorDialog);
  88. };
  89. //------------------------------------------------------------------------------
  90. void ctkColorDialog::addTab(QWidget* widget, const QString& label)
  91. {
  92. this->insertTab(-1, widget, label);
  93. }
  94. //------------------------------------------------------------------------------
  95. void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label, const char* signal)
  96. {
  97. ctkColorDialog::insertDefaultTab(-1, widget, label, signal);
  98. }
  99. #endif