ctkColorDialog.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // CTK includes
  20. #include "ctkColorDialog.h"
  21. QList<QWidget*> ctkColorDialog::DefaultTabs;
  22. //------------------------------------------------------------------------------
  23. class ctkColorDialogPrivate
  24. {
  25. Q_DECLARE_PUBLIC(ctkColorDialog);
  26. protected:
  27. ctkColorDialog* const q_ptr;
  28. public:
  29. ctkColorDialogPrivate(ctkColorDialog& object);
  30. void init();
  31. QTabWidget* LeftTabWidget;
  32. QWidget* BasicTab;
  33. };
  34. //------------------------------------------------------------------------------
  35. ctkColorDialogPrivate::ctkColorDialogPrivate(ctkColorDialog& object)
  36. :q_ptr(&object)
  37. {
  38. this->LeftTabWidget = 0;
  39. }
  40. //------------------------------------------------------------------------------
  41. void ctkColorDialogPrivate::init()
  42. {
  43. Q_Q(ctkColorDialog);
  44. QVBoxLayout* mainLay = qobject_cast<QVBoxLayout*>(q->layout());
  45. QHBoxLayout* topLay = qobject_cast<QHBoxLayout*>(mainLay->itemAt(0)->layout());
  46. QVBoxLayout* leftLay = qobject_cast<QVBoxLayout*>(topLay->takeAt(0)->layout());
  47. leftLay->setParent(0);
  48. this->BasicTab = new QWidget(q);
  49. this->BasicTab->setLayout(leftLay);
  50. this->LeftTabWidget = new QTabWidget(q);
  51. topLay->insertWidget(0, this->LeftTabWidget);
  52. this->LeftTabWidget->addTab(this->BasicTab, QObject::tr("Basic"));
  53. }
  54. //------------------------------------------------------------------------------
  55. ctkColorDialog::ctkColorDialog(QWidget* parent)
  56. : QColorDialog(parent)
  57. , d_ptr(new ctkColorDialogPrivate(*this))
  58. {
  59. Q_D(ctkColorDialog);
  60. d->init();
  61. }
  62. //------------------------------------------------------------------------------
  63. ctkColorDialog::ctkColorDialog(const QColor& initial, QWidget* parent)
  64. : QColorDialog(initial, parent)
  65. , d_ptr(new ctkColorDialogPrivate(*this))
  66. {
  67. Q_D(ctkColorDialog);
  68. d->init();
  69. }
  70. //------------------------------------------------------------------------------
  71. ctkColorDialog::~ctkColorDialog()
  72. {
  73. }
  74. //------------------------------------------------------------------------------
  75. void ctkColorDialog::addTab(QWidget* widget, const QString& label)
  76. {
  77. Q_D(ctkColorDialog);
  78. d->LeftTabWidget->addTab(widget, label);
  79. }
  80. //------------------------------------------------------------------------------
  81. void ctkColorDialog::removeTab(int index)
  82. {
  83. Q_D(ctkColorDialog);
  84. if (index < 0)
  85. {
  86. return;
  87. }
  88. d->LeftTabWidget->removeTab(index + 1);
  89. }
  90. //------------------------------------------------------------------------------
  91. int ctkColorDialog::indexOf(QWidget* widget)const
  92. {
  93. Q_D(const ctkColorDialog);
  94. int index = d->LeftTabWidget->indexOf(widget);
  95. return index >= 0 ? index - 1 : -1;
  96. }
  97. //------------------------------------------------------------------------------
  98. QWidget* ctkColorDialog::widget(int index)const
  99. {
  100. Q_D(const ctkColorDialog);
  101. if (index < 0)
  102. {
  103. return 0;
  104. }
  105. return d->LeftTabWidget->widget(index+1);
  106. }
  107. //------------------------------------------------------------------------------
  108. QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title,
  109. ColorDialogOptions options)
  110. {
  111. ctkColorDialog dlg(parent);
  112. if (!title.isEmpty())
  113. {
  114. dlg.setWindowTitle(title);
  115. }
  116. dlg.setOptions(options | QColorDialog::DontUseNativeDialog);
  117. dlg.setCurrentColor(initial);
  118. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  119. {
  120. dlg.addTab(tab, tab->windowTitle());
  121. if (!tab->accessibleDescription().isEmpty())
  122. {
  123. QObject::connect(tab, tab->accessibleDescription().toLatin1(),
  124. &dlg, SLOT(setColor(QColor)));
  125. }
  126. }
  127. dlg.exec();
  128. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  129. {
  130. dlg.removeTab(dlg.indexOf(tab));
  131. if (!tab->accessibleDescription().isEmpty())
  132. {
  133. QObject::disconnect(tab, tab->accessibleDescription().toLatin1(),
  134. &dlg, SLOT(setColor(QColor)));
  135. }
  136. tab->setParent(0);
  137. tab->hide();
  138. }
  139. return dlg.selectedColor();
  140. }
  141. //------------------------------------------------------------------------------
  142. void ctkColorDialog::addDefaultTab(QWidget* widget, const QString& label, const char* signal)
  143. {
  144. // I'm a bit lazy here and the label+ signal should probably be stored in a
  145. // separate structure
  146. widget->setWindowTitle(label);
  147. widget->setAccessibleDescription(signal);
  148. ctkColorDialog::DefaultTabs << widget;
  149. widget->setParent(0);
  150. }
  151. //------------------------------------------------------------------------------
  152. void ctkColorDialog::setColor(const QColor& color)
  153. {
  154. this->QColorDialog::setCurrentColor(color);
  155. }