ctkColorDialog.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // QT includes
  15. #include <QDebug>
  16. #include <QVBoxLayout>
  17. #include <QHBoxLayout>
  18. #include <QTabWidget>
  19. #include <QVariant>
  20. // CTK includes
  21. #include "ctkColorDialog.h"
  22. QList<QWidget*> ctkColorDialog::DefaultTabs;
  23. int ctkColorDialog::DefaultTab = -1;
  24. //------------------------------------------------------------------------------
  25. class ctkColorDialogPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkColorDialog);
  28. protected:
  29. ctkColorDialog* const q_ptr;
  30. public:
  31. ctkColorDialogPrivate(ctkColorDialog& object);
  32. void init();
  33. QTabWidget* LeftTabWidget;
  34. QWidget* BasicTab;
  35. };
  36. //------------------------------------------------------------------------------
  37. ctkColorDialogPrivate::ctkColorDialogPrivate(ctkColorDialog& object)
  38. :q_ptr(&object)
  39. {
  40. this->LeftTabWidget = 0;
  41. }
  42. //------------------------------------------------------------------------------
  43. void ctkColorDialogPrivate::init()
  44. {
  45. Q_Q(ctkColorDialog);
  46. QVBoxLayout* mainLay = qobject_cast<QVBoxLayout*>(q->layout());
  47. QHBoxLayout* topLay = qobject_cast<QHBoxLayout*>(mainLay->itemAt(0)->layout());
  48. QVBoxLayout* leftLay = qobject_cast<QVBoxLayout*>(topLay->takeAt(0)->layout());
  49. leftLay->setParent(0);
  50. this->BasicTab = new QWidget(q);
  51. this->BasicTab->setLayout(leftLay);
  52. this->LeftTabWidget = new QTabWidget(q);
  53. topLay->insertWidget(0, this->LeftTabWidget);
  54. this->LeftTabWidget->addTab(this->BasicTab, QObject::tr("Basic"));
  55. // If you use a ctkColorDialog, it's probably because you have tabs to add
  56. // into. Which means that you are likely to want to resize the dialog as
  57. // well.
  58. q->setSizeGripEnabled(true);
  59. q->layout()->setSizeConstraint(QLayout::SetDefaultConstraint);
  60. }
  61. //------------------------------------------------------------------------------
  62. ctkColorDialog::ctkColorDialog(QWidget* parent)
  63. : QColorDialog(parent)
  64. , d_ptr(new ctkColorDialogPrivate(*this))
  65. {
  66. Q_D(ctkColorDialog);
  67. d->init();
  68. }
  69. //------------------------------------------------------------------------------
  70. ctkColorDialog::ctkColorDialog(const QColor& initial, QWidget* parent)
  71. : QColorDialog(initial, parent)
  72. , d_ptr(new ctkColorDialogPrivate(*this))
  73. {
  74. Q_D(ctkColorDialog);
  75. d->init();
  76. }
  77. //------------------------------------------------------------------------------
  78. ctkColorDialog::~ctkColorDialog()
  79. {
  80. }
  81. //------------------------------------------------------------------------------
  82. void ctkColorDialog::insertTab(int tabIndex, QWidget* widget, const QString& label)
  83. {
  84. Q_D(ctkColorDialog);
  85. d->LeftTabWidget->insertTab(tabIndex, widget, label);
  86. }
  87. //------------------------------------------------------------------------------
  88. void ctkColorDialog::setCurrentTab(int index)
  89. {
  90. Q_D(ctkColorDialog);
  91. d->LeftTabWidget->setCurrentIndex(index);
  92. }
  93. //------------------------------------------------------------------------------
  94. void ctkColorDialog::removeTab(int index)
  95. {
  96. Q_D(ctkColorDialog);
  97. d->LeftTabWidget->removeTab(index);
  98. }
  99. //------------------------------------------------------------------------------
  100. int ctkColorDialog::indexOf(QWidget* widget)const
  101. {
  102. Q_D(const ctkColorDialog);
  103. return d->LeftTabWidget->indexOf(widget);
  104. }
  105. //------------------------------------------------------------------------------
  106. QWidget* ctkColorDialog::widget(int index)const
  107. {
  108. Q_D(const ctkColorDialog);
  109. return d->LeftTabWidget->widget(index);
  110. }
  111. //------------------------------------------------------------------------------
  112. QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title,
  113. ColorDialogOptions options)
  114. {
  115. ctkColorDialog dlg(parent);
  116. if (!title.isEmpty())
  117. {
  118. dlg.setWindowTitle(title);
  119. }
  120. dlg.setOptions(options | QColorDialog::DontUseNativeDialog);
  121. dlg.setCurrentColor(initial);
  122. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  123. {
  124. dlg.insertTab(tab->property("tabIndex").toInt(), tab, tab->windowTitle());
  125. if (tab->property("signal").isValid())
  126. {
  127. QObject::connect(tab, tab->property("signal").toString().toLatin1(),
  128. &dlg, SLOT(setColor(QColor)));
  129. }
  130. }
  131. dlg.setCurrentTab(ctkColorDialog::DefaultTab);
  132. dlg.exec();
  133. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  134. {
  135. dlg.removeTab(dlg.indexOf(tab));
  136. if (tab->property("signal").isValid())
  137. {
  138. QObject::disconnect(tab, tab->property("signal").toString().toLatin1(),
  139. &dlg, SLOT(setColor(QColor)));
  140. }
  141. tab->setParent(0);
  142. tab->hide();
  143. }
  144. return dlg.selectedColor();
  145. }
  146. //------------------------------------------------------------------------------
  147. void ctkColorDialog::insertDefaultTab(int tabIndex, QWidget* widget, const QString& label, const char* signal)
  148. {
  149. widget->setWindowTitle(label);
  150. widget->setProperty("signal", signal);
  151. widget->setProperty("tabIndex", tabIndex);
  152. ctkColorDialog::DefaultTabs << widget;
  153. widget->setParent(0);
  154. }
  155. //------------------------------------------------------------------------------
  156. void ctkColorDialog::setDefaultTab(int index)
  157. {
  158. ctkColorDialog::DefaultTab = index;
  159. }
  160. //------------------------------------------------------------------------------
  161. void ctkColorDialog::setColor(const QColor& color)
  162. {
  163. this->QColorDialog::setCurrentColor(color);
  164. }