ctkVTKScalarBarWidget.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // CTK includes
  16. #include "ctkVTKScalarBarWidget.h"
  17. #include "ui_ctkVTKScalarBarWidget.h"
  18. // VTK includes
  19. #include <vtkScalarBarActor.h>
  20. #include <vtkScalarBarWidget.h>
  21. //-----------------------------------------------------------------------------
  22. class ctkVTKScalarBarWidgetPrivate: public Ui_ctkVTKScalarBarWidget
  23. {
  24. Q_DECLARE_PUBLIC(ctkVTKScalarBarWidget);
  25. protected:
  26. ctkVTKScalarBarWidget* const q_ptr;
  27. public:
  28. ctkVTKScalarBarWidgetPrivate(ctkVTKScalarBarWidget& object);
  29. void init();
  30. void updateFromScalarBarWidget();
  31. vtkScalarBarWidget* ScalarBarWidget;
  32. };
  33. //-----------------------------------------------------------------------------
  34. ctkVTKScalarBarWidgetPrivate::ctkVTKScalarBarWidgetPrivate(ctkVTKScalarBarWidget& object)
  35. :q_ptr(&object)
  36. {
  37. this->ScalarBarWidget = 0;
  38. }
  39. //-----------------------------------------------------------------------------
  40. void ctkVTKScalarBarWidgetPrivate::init()
  41. {
  42. Q_Q(ctkVTKScalarBarWidget);
  43. this->setupUi(q);
  44. q->setEnabled(this->ScalarBarWidget != 0);
  45. QObject::connect(this->DisplayScalarBarCheckBox, SIGNAL(toggled(bool)),
  46. q, SLOT(setDisplay(bool)));
  47. QObject::connect(this->MaxNumberOfColorsSpinBox, SIGNAL(valueChanged(int)),
  48. q, SLOT(setMaxNumberOfColors(int)));
  49. QObject::connect(this->NumberOfLabelsSpinBox, SIGNAL(valueChanged(int)),
  50. q, SLOT(setNumberOfLabels(int)));
  51. QObject::connect(this->TitleTextPropertyWidget, SIGNAL(textChanged(QString)),
  52. q, SLOT(setTitle(QString)));
  53. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(textChanged(QString)),
  54. q, SLOT(setLabelsFormat(QString)));
  55. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(colorChanged(QColor)),
  56. q, SIGNAL(modified()));
  57. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(opacityChanged(double)),
  58. q, SIGNAL(modified()));
  59. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(fontFamilyChanged(QString)),
  60. q, SIGNAL(modified()));
  61. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(boldChanged(bool)),
  62. q, SIGNAL(modified()));
  63. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(italicChanged(bool)),
  64. q, SIGNAL(modified()));
  65. QObject::connect(this->LabelsTextPropertyWidget, SIGNAL(shadowChanged(bool)),
  66. q, SIGNAL(modified()));
  67. }
  68. //-----------------------------------------------------------------------------
  69. void ctkVTKScalarBarWidgetPrivate::updateFromScalarBarWidget()
  70. {
  71. Q_Q(ctkVTKScalarBarWidget);
  72. vtkScalarBarActor* actor =
  73. this->ScalarBarWidget ? this->ScalarBarWidget->GetScalarBarActor() : 0;
  74. q->setEnabled(actor != 0);
  75. if (actor == 0)
  76. {
  77. return;
  78. }
  79. this->DisplayScalarBarCheckBox->setChecked(this->ScalarBarWidget->GetEnabled() != 0);
  80. this->MaxNumberOfColorsSpinBox->setValue(actor->GetMaximumNumberOfColors());
  81. this->NumberOfLabelsSpinBox->setValue(actor->GetNumberOfLabels());
  82. this->TitleTextPropertyWidget->setTextProperty(
  83. actor->GetTitleTextProperty());
  84. this->LabelsTextPropertyWidget->setTextProperty(
  85. actor->GetLabelTextProperty());
  86. this->TitleTextPropertyWidget->setText(actor->GetTitle());
  87. this->LabelsTextPropertyWidget->setText(actor->GetLabelFormat());
  88. }
  89. //-----------------------------------------------------------------------------
  90. ctkVTKScalarBarWidget::ctkVTKScalarBarWidget(QWidget* parentWidget)
  91. :QWidget(parentWidget)
  92. , d_ptr(new ctkVTKScalarBarWidgetPrivate(*this))
  93. {
  94. Q_D(ctkVTKScalarBarWidget);
  95. d->init();
  96. }
  97. //-----------------------------------------------------------------------------
  98. ctkVTKScalarBarWidget::ctkVTKScalarBarWidget(vtkScalarBarWidget* scalarBarWidget, QWidget* parentWidget)
  99. :QWidget(parentWidget)
  100. , d_ptr(new ctkVTKScalarBarWidgetPrivate(*this))
  101. {
  102. Q_D(ctkVTKScalarBarWidget);
  103. d->init();
  104. this->setScalarBarWidget(scalarBarWidget);
  105. }
  106. //-----------------------------------------------------------------------------
  107. ctkVTKScalarBarWidget::~ctkVTKScalarBarWidget()
  108. {
  109. }
  110. //-----------------------------------------------------------------------------
  111. void ctkVTKScalarBarWidget::setScalarBarWidget(vtkScalarBarWidget* scalarBarWidget)
  112. {
  113. Q_D(ctkVTKScalarBarWidget);
  114. if (scalarBarWidget == d->ScalarBarWidget)
  115. {
  116. return;
  117. }
  118. vtkScalarBarActor* oldActor =
  119. d->ScalarBarWidget ? d->ScalarBarWidget->GetScalarBarActor() : 0;
  120. vtkScalarBarActor* newActor =
  121. scalarBarWidget ? scalarBarWidget->GetScalarBarActor() : 0;
  122. qvtkReconnect(d->ScalarBarWidget, scalarBarWidget, vtkCommand::EnableEvent,
  123. this, SLOT(onScalarBarModified()));
  124. qvtkReconnect(d->ScalarBarWidget, scalarBarWidget, vtkCommand::DisableEvent,
  125. this, SLOT(onScalarBarModified()));
  126. qvtkReconnect(oldActor, newActor, vtkCommand::ModifiedEvent,
  127. this, SLOT(onScalarBarModified()));
  128. d->ScalarBarWidget = scalarBarWidget;
  129. this->onScalarBarModified();
  130. }
  131. //-----------------------------------------------------------------------------
  132. vtkScalarBarWidget* ctkVTKScalarBarWidget::scalarBarWidget()const
  133. {
  134. Q_D(const ctkVTKScalarBarWidget);
  135. return d->ScalarBarWidget;
  136. }
  137. //-----------------------------------------------------------------------------
  138. void ctkVTKScalarBarWidget::onScalarBarModified()
  139. {
  140. Q_D(ctkVTKScalarBarWidget);
  141. d->updateFromScalarBarWidget();
  142. emit modified();
  143. }
  144. //-----------------------------------------------------------------------------
  145. void ctkVTKScalarBarWidget::setDisplay(bool visible)
  146. {
  147. Q_D(ctkVTKScalarBarWidget);
  148. if (d->ScalarBarWidget == 0)
  149. {
  150. return;
  151. }
  152. d->ScalarBarWidget->SetEnabled(visible);
  153. // calling SetEnabled might fail, make sure the checkbox is up-to-date
  154. d->DisplayScalarBarCheckBox->setChecked(d->ScalarBarWidget->GetEnabled());
  155. }
  156. //-----------------------------------------------------------------------------
  157. bool ctkVTKScalarBarWidget::display()const
  158. {
  159. Q_D(const ctkVTKScalarBarWidget);
  160. return d->DisplayScalarBarCheckBox->isChecked();
  161. }
  162. //-----------------------------------------------------------------------------
  163. void ctkVTKScalarBarWidget::setMaxNumberOfColors(int colorCount)
  164. {
  165. Q_D(ctkVTKScalarBarWidget);
  166. vtkScalarBarActor* actor =
  167. d->ScalarBarWidget ? d->ScalarBarWidget->GetScalarBarActor() : 0;
  168. if (actor == 0)
  169. {
  170. return;
  171. }
  172. actor->SetMaximumNumberOfColors(colorCount);
  173. }
  174. //-----------------------------------------------------------------------------
  175. int ctkVTKScalarBarWidget::maxNumberOfColors()const
  176. {
  177. Q_D(const ctkVTKScalarBarWidget);
  178. return d->MaxNumberOfColorsSpinBox->value();
  179. }
  180. //-----------------------------------------------------------------------------
  181. void ctkVTKScalarBarWidget::setNumberOfLabels(int labelCount)
  182. {
  183. Q_D(ctkVTKScalarBarWidget);
  184. vtkScalarBarActor* actor =
  185. d->ScalarBarWidget ? d->ScalarBarWidget->GetScalarBarActor() : 0;
  186. if (actor == 0)
  187. {
  188. return;
  189. }
  190. actor->SetNumberOfLabels(labelCount);
  191. }
  192. //-----------------------------------------------------------------------------
  193. int ctkVTKScalarBarWidget::numberOfLabels()const
  194. {
  195. Q_D(const ctkVTKScalarBarWidget);
  196. return d->NumberOfLabelsSpinBox->value();
  197. }
  198. //-----------------------------------------------------------------------------
  199. void ctkVTKScalarBarWidget::setTitle(const QString& title)
  200. {
  201. Q_D(ctkVTKScalarBarWidget);
  202. vtkScalarBarActor* actor =
  203. d->ScalarBarWidget ? d->ScalarBarWidget->GetScalarBarActor() : 0;
  204. if (actor == 0)
  205. {
  206. return;
  207. }
  208. actor->SetTitle(title.toLatin1());
  209. }
  210. //-----------------------------------------------------------------------------
  211. QString ctkVTKScalarBarWidget::title()const
  212. {
  213. Q_D(const ctkVTKScalarBarWidget);
  214. return d->TitleTextPropertyWidget->text();
  215. }
  216. //-----------------------------------------------------------------------------
  217. void ctkVTKScalarBarWidget::setLabelsFormat(const QString& format)
  218. {
  219. Q_D(ctkVTKScalarBarWidget);
  220. vtkScalarBarActor* actor =
  221. d->ScalarBarWidget ? d->ScalarBarWidget->GetScalarBarActor() : 0;
  222. if (actor == 0)
  223. {
  224. return;
  225. }
  226. actor->SetLabelFormat(format.toLatin1());
  227. }
  228. //-----------------------------------------------------------------------------
  229. QString ctkVTKScalarBarWidget::labelsFormat()const
  230. {
  231. Q_D(const ctkVTKScalarBarWidget);
  232. return d->LabelsTextPropertyWidget->text();
  233. }