ctkColorDialog.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.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. // 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. }
  56. //------------------------------------------------------------------------------
  57. ctkColorDialog::ctkColorDialog(QWidget* parent)
  58. : QColorDialog(parent)
  59. , d_ptr(new ctkColorDialogPrivate(*this))
  60. {
  61. Q_D(ctkColorDialog);
  62. d->init();
  63. }
  64. //------------------------------------------------------------------------------
  65. ctkColorDialog::ctkColorDialog(const QColor& initial, QWidget* parent)
  66. : QColorDialog(initial, parent)
  67. , d_ptr(new ctkColorDialogPrivate(*this))
  68. {
  69. Q_D(ctkColorDialog);
  70. d->init();
  71. }
  72. //------------------------------------------------------------------------------
  73. ctkColorDialog::~ctkColorDialog()
  74. {
  75. }
  76. //------------------------------------------------------------------------------
  77. void ctkColorDialog::insertTab(int tabIndex, QWidget* widget, const QString& label)
  78. {
  79. Q_D(ctkColorDialog);
  80. d->LeftTabWidget->insertTab(tabIndex, widget, label);
  81. }
  82. //------------------------------------------------------------------------------
  83. void ctkColorDialog::setCurrentTab(int index)
  84. {
  85. Q_D(ctkColorDialog);
  86. d->LeftTabWidget->setCurrentIndex(index);
  87. }
  88. //------------------------------------------------------------------------------
  89. void ctkColorDialog::removeTab(int index)
  90. {
  91. Q_D(ctkColorDialog);
  92. d->LeftTabWidget->removeTab(index);
  93. }
  94. //------------------------------------------------------------------------------
  95. int ctkColorDialog::indexOf(QWidget* widget)const
  96. {
  97. Q_D(const ctkColorDialog);
  98. return d->LeftTabWidget->indexOf(widget);
  99. }
  100. //------------------------------------------------------------------------------
  101. QWidget* ctkColorDialog::widget(int index)const
  102. {
  103. Q_D(const ctkColorDialog);
  104. return d->LeftTabWidget->widget(index);
  105. }
  106. //------------------------------------------------------------------------------
  107. QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title,
  108. ColorDialogOptions options)
  109. {
  110. ctkColorDialog dlg(parent);
  111. if (!title.isEmpty())
  112. {
  113. dlg.setWindowTitle(title);
  114. }
  115. dlg.setOptions(options | QColorDialog::DontUseNativeDialog);
  116. dlg.setCurrentColor(initial);
  117. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  118. {
  119. dlg.insertTab(tab->property("tabIndex").toInt(), tab, tab->windowTitle());
  120. if (!tab->property("signal").isValid())
  121. {
  122. QObject::connect(tab, tab->property("signal").toString().toLatin1(),
  123. &dlg, SLOT(setColor(QColor)));
  124. }
  125. }
  126. dlg.setCurrentTab(ctkColorDialog::DefaultTab);
  127. dlg.exec();
  128. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  129. {
  130. dlg.removeTab(dlg.indexOf(tab));
  131. if (tab->property("signal").isValid())
  132. {
  133. QObject::disconnect(tab, tab->property("signal").toString().toLatin1(),
  134. &dlg, SLOT(setColor(QColor)));
  135. }
  136. tab->setParent(0);
  137. tab->hide();
  138. }
  139. return dlg.selectedColor();
  140. }
  141. //------------------------------------------------------------------------------
  142. void ctkColorDialog::insertDefaultTab(int tabIndex, QWidget* widget, const QString& label, const char* signal)
  143. {
  144. widget->setWindowTitle(label);
  145. widget->setProperty("signal", signal);
  146. widget->setProperty("tabIndex", tabIndex);
  147. ctkColorDialog::DefaultTabs << widget;
  148. widget->setParent(0);
  149. }
  150. //------------------------------------------------------------------------------
  151. void ctkColorDialog::setDefaultTab(int index)
  152. {
  153. ctkColorDialog::DefaultTab = index;
  154. }
  155. //------------------------------------------------------------------------------
  156. void ctkColorDialog::setColor(const QColor& color)
  157. {
  158. this->QColorDialog::setCurrentColor(color);
  159. }