ctkColorDialog.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. QString ctkColorDialog::LastColorName = QString();
  25. //------------------------------------------------------------------------------
  26. class ctkColorDialogPrivate
  27. {
  28. Q_DECLARE_PUBLIC(ctkColorDialog);
  29. protected:
  30. ctkColorDialog* const q_ptr;
  31. public:
  32. ctkColorDialogPrivate(ctkColorDialog& object);
  33. void init();
  34. QTabWidget* LeftTabWidget;
  35. QWidget* BasicTab;
  36. QString ColorName;
  37. };
  38. //------------------------------------------------------------------------------
  39. ctkColorDialogPrivate::ctkColorDialogPrivate(ctkColorDialog& object)
  40. :q_ptr(&object)
  41. {
  42. this->LeftTabWidget = 0;
  43. }
  44. //------------------------------------------------------------------------------
  45. void ctkColorDialogPrivate::init()
  46. {
  47. Q_Q(ctkColorDialog);
  48. QVBoxLayout* mainLay = qobject_cast<QVBoxLayout*>(q->layout());
  49. QHBoxLayout* topLay = qobject_cast<QHBoxLayout*>(mainLay->itemAt(0)->layout());
  50. QVBoxLayout* leftLay = qobject_cast<QVBoxLayout*>(topLay->takeAt(0)->layout());
  51. leftLay->setParent(0);
  52. this->BasicTab = new QWidget(q);
  53. this->BasicTab->setLayout(leftLay);
  54. this->LeftTabWidget = new QTabWidget(q);
  55. topLay->insertWidget(0, this->LeftTabWidget);
  56. this->LeftTabWidget->addTab(this->BasicTab, QObject::tr("Basic"));
  57. // If you use a ctkColorDialog, it's probably because you have tabs to add
  58. // into. Which means that you are likely to want to resize the dialog as
  59. // well.
  60. q->setSizeGripEnabled(true);
  61. q->layout()->setSizeConstraint(QLayout::SetDefaultConstraint);
  62. QObject::connect(q, SIGNAL(currentColorChanged(QColor)),
  63. q, SLOT(resetColorName()));
  64. }
  65. //------------------------------------------------------------------------------
  66. ctkColorDialog::ctkColorDialog(QWidget* parent)
  67. : QColorDialog(parent)
  68. , d_ptr(new ctkColorDialogPrivate(*this))
  69. {
  70. Q_D(ctkColorDialog);
  71. d->init();
  72. }
  73. //------------------------------------------------------------------------------
  74. ctkColorDialog::ctkColorDialog(const QColor& initial, QWidget* parent)
  75. : QColorDialog(initial, parent)
  76. , d_ptr(new ctkColorDialogPrivate(*this))
  77. {
  78. Q_D(ctkColorDialog);
  79. d->init();
  80. }
  81. //------------------------------------------------------------------------------
  82. ctkColorDialog::~ctkColorDialog()
  83. {
  84. }
  85. //------------------------------------------------------------------------------
  86. void ctkColorDialog::insertTab(int tabIndex, QWidget* widget, const QString& label)
  87. {
  88. Q_D(ctkColorDialog);
  89. d->LeftTabWidget->insertTab(tabIndex, widget, label);
  90. }
  91. //------------------------------------------------------------------------------
  92. void ctkColorDialog::setCurrentTab(int index)
  93. {
  94. Q_D(ctkColorDialog);
  95. d->LeftTabWidget->setCurrentIndex(index);
  96. }
  97. //------------------------------------------------------------------------------
  98. void ctkColorDialog::removeTab(int index)
  99. {
  100. Q_D(ctkColorDialog);
  101. d->LeftTabWidget->removeTab(index);
  102. }
  103. //------------------------------------------------------------------------------
  104. int ctkColorDialog::indexOf(QWidget* widget)const
  105. {
  106. Q_D(const ctkColorDialog);
  107. return d->LeftTabWidget->indexOf(widget);
  108. }
  109. //------------------------------------------------------------------------------
  110. QWidget* ctkColorDialog::widget(int index)const
  111. {
  112. Q_D(const ctkColorDialog);
  113. return d->LeftTabWidget->widget(index);
  114. }
  115. //------------------------------------------------------------------------------
  116. QColor ctkColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title,
  117. ColorDialogOptions options)
  118. {
  119. ctkColorDialog dlg(parent);
  120. if (!title.isEmpty())
  121. {
  122. dlg.setWindowTitle(title);
  123. }
  124. dlg.setOptions(options | QColorDialog::DontUseNativeDialog);
  125. dlg.setCurrentColor(initial);
  126. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  127. {
  128. dlg.insertTab(tab->property("tabIndex").toInt(), tab, tab->windowTitle());
  129. if (tab->property("colorSignal").isValid())
  130. {
  131. QObject::connect(tab, tab->property("colorSignal").toString().toLatin1(),
  132. &dlg, SLOT(setColor(QColor)));
  133. }
  134. if (tab->property("nameSignal").isValid())
  135. {
  136. QObject::connect(tab, tab->property("nameSignal").toString().toLatin1(),
  137. &dlg, SLOT(setColorName(QString)));
  138. }
  139. }
  140. dlg.setCurrentTab(ctkColorDialog::DefaultTab);
  141. dlg.exec();
  142. foreach(QWidget* tab, ctkColorDialog::DefaultTabs)
  143. {
  144. dlg.removeTab(dlg.indexOf(tab));
  145. if (tab->property("colorSignal").isValid())
  146. {
  147. QObject::disconnect(tab, tab->property("colorSignal").toString().toLatin1(),
  148. &dlg, SLOT(setColor(QColor)));
  149. }
  150. if (tab->property("nameSignal").isValid())
  151. {
  152. QObject::disconnect(tab, tab->property("nameSignal").toString().toLatin1(),
  153. &dlg, SLOT(setColorName(QString)));
  154. }
  155. tab->setParent(0);
  156. tab->hide();
  157. }
  158. ctkColorDialog::LastColorName = dlg.colorName();
  159. return dlg.selectedColor();
  160. }
  161. //------------------------------------------------------------------------------
  162. QString ctkColorDialog::getColorName()
  163. {
  164. return ctkColorDialog::LastColorName;
  165. }
  166. //------------------------------------------------------------------------------
  167. void ctkColorDialog::insertDefaultTab(int tabIndex, QWidget* widget,
  168. const QString& label,
  169. const char* colorSignal,
  170. const char* nameSignal)
  171. {
  172. widget->setWindowTitle(label);
  173. widget->setProperty("colorSignal", colorSignal);
  174. widget->setProperty("nameSignal", nameSignal);
  175. widget->setProperty("tabIndex", tabIndex);
  176. ctkColorDialog::DefaultTabs << widget;
  177. widget->setParent(0);
  178. }
  179. //------------------------------------------------------------------------------
  180. void ctkColorDialog::setDefaultTab(int index)
  181. {
  182. ctkColorDialog::DefaultTab = index;
  183. }
  184. //------------------------------------------------------------------------------
  185. void ctkColorDialog::setColor(const QColor& color)
  186. {
  187. this->QColorDialog::setCurrentColor(color);
  188. }
  189. //------------------------------------------------------------------------------
  190. void ctkColorDialog::setColorName(const QString& name)
  191. {
  192. Q_D(ctkColorDialog);
  193. if (d->ColorName == name)
  194. {
  195. return;
  196. }
  197. d->ColorName = name;
  198. emit currentColorNameChanged(d->ColorName);
  199. }
  200. //------------------------------------------------------------------------------
  201. QString ctkColorDialog::colorName()const
  202. {
  203. Q_D(const ctkColorDialog);
  204. return d->ColorName;
  205. }
  206. //------------------------------------------------------------------------------
  207. void ctkColorDialog::resetColorName()
  208. {
  209. this->setColorName(QString());
  210. }