ctkVTKTextPropertyWidget.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. // CTK includes
  17. #include "ctkVTKTextPropertyWidget.h"
  18. #include "ui_ctkVTKTextPropertyWidget.h"
  19. // VTK includes
  20. #include <vtkTextProperty.h>
  21. //-----------------------------------------------------------------------------
  22. class ctkVTKTextPropertyWidgetPrivate: public Ui_ctkVTKTextPropertyWidget
  23. {
  24. Q_DECLARE_PUBLIC(ctkVTKTextPropertyWidget);
  25. protected:
  26. ctkVTKTextPropertyWidget* const q_ptr;
  27. public:
  28. void init();
  29. ctkVTKTextPropertyWidgetPrivate(ctkVTKTextPropertyWidget& object);
  30. vtkTextProperty* TextProperty;
  31. };
  32. //-----------------------------------------------------------------------------
  33. ctkVTKTextPropertyWidgetPrivate::ctkVTKTextPropertyWidgetPrivate(ctkVTKTextPropertyWidget& object)
  34. :q_ptr(&object)
  35. {
  36. this->TextProperty = 0;
  37. }
  38. //-----------------------------------------------------------------------------
  39. ctkVTKTextPropertyWidget::~ctkVTKTextPropertyWidget()
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. void ctkVTKTextPropertyWidgetPrivate::init()
  44. {
  45. Q_Q(ctkVTKTextPropertyWidget);
  46. this->setupUi(q);
  47. q->setEnabled(this->TextProperty != 0);
  48. QObject::connect(this->TextLineEdit, SIGNAL(textChanged(const QString&)),
  49. q, SIGNAL(textChanged(const QString&)));
  50. QObject::connect(this->ColorPickerButton, SIGNAL(colorChanged(QColor)),
  51. q, SLOT(setColor(const QColor&)));
  52. QObject::connect(this->OpacitySlider, SIGNAL(valueChanged(double)),
  53. q, SLOT(setOpacity(double)));
  54. QObject::connect(this->FontComboBox, SIGNAL(currentIndexChanged(const QString&)),
  55. q, SLOT(setFont(const QString&)));
  56. QObject::connect(this->BoldCheckBox, SIGNAL(toggled(bool)),
  57. q, SLOT(setBold(bool)));
  58. QObject::connect(this->ItalicCheckBox, SIGNAL(toggled(bool)),
  59. q, SLOT(setItalic(bool)));
  60. QObject::connect(this->ShadowCheckBox, SIGNAL(toggled(bool)),
  61. q, SLOT(setShadow(bool)));
  62. }
  63. //-----------------------------------------------------------------------------
  64. ctkVTKTextPropertyWidget::ctkVTKTextPropertyWidget(QWidget* parentWidget)
  65. :QWidget(parentWidget)
  66. , d_ptr(new ctkVTKTextPropertyWidgetPrivate(*this))
  67. {
  68. Q_D(ctkVTKTextPropertyWidget);
  69. d->init();
  70. }
  71. //-----------------------------------------------------------------------------
  72. ctkVTKTextPropertyWidget::ctkVTKTextPropertyWidget(vtkTextProperty* textProperty, QWidget* parentWidget)
  73. :QWidget(parentWidget)
  74. , d_ptr(new ctkVTKTextPropertyWidgetPrivate(*this))
  75. {
  76. Q_D(ctkVTKTextPropertyWidget);
  77. d->init();
  78. this->setTextProperty(textProperty);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void ctkVTKTextPropertyWidget::setTextProperty(vtkTextProperty* textProperty)
  82. {
  83. Q_D(ctkVTKTextPropertyWidget);
  84. qvtkReconnect(d->TextProperty, textProperty, vtkCommand::ModifiedEvent,
  85. this, SLOT(updateFromTextProperty()));
  86. d->TextProperty = textProperty;
  87. this->updateFromTextProperty();
  88. }
  89. //-----------------------------------------------------------------------------
  90. vtkTextProperty* ctkVTKTextPropertyWidget::textProperty()const
  91. {
  92. Q_D(const ctkVTKTextPropertyWidget);
  93. return d->TextProperty;
  94. }
  95. //-----------------------------------------------------------------------------
  96. void ctkVTKTextPropertyWidget::updateFromTextProperty()
  97. {
  98. Q_D(ctkVTKTextPropertyWidget);
  99. this->setEnabled(d->TextProperty != 0);
  100. if (d->TextProperty == 0)
  101. {
  102. return;
  103. }
  104. double* color = d->TextProperty->GetColor();
  105. d->ColorPickerButton->setColor(
  106. QColor::fromRgbF(color[0],color[1],color[2]));
  107. d->OpacitySlider->setValue(d->TextProperty->GetOpacity());
  108. d->FontComboBox->setCurrentIndex(
  109. d->FontComboBox->findText(d->TextProperty->GetFontFamilyAsString()));
  110. d->BoldCheckBox->setChecked(d->TextProperty->GetBold() != 0);
  111. d->ItalicCheckBox->setChecked(d->TextProperty->GetItalic() != 0);
  112. d->ShadowCheckBox->setChecked(d->TextProperty->GetShadow() != 0);
  113. }
  114. //-----------------------------------------------------------------------------
  115. void ctkVTKTextPropertyWidget::setTextVisible(bool visible)
  116. {
  117. Q_D(ctkVTKTextPropertyWidget);
  118. d->TextLabel->setVisible(visible);
  119. d->TextLineEdit->setVisible(visible);
  120. }
  121. //-----------------------------------------------------------------------------
  122. bool ctkVTKTextPropertyWidget::isTextVisible()const
  123. {
  124. Q_D(const ctkVTKTextPropertyWidget);
  125. Q_ASSERT(d->TextLabel->isVisibleTo(const_cast<ctkVTKTextPropertyWidget*>(this))
  126. == d->TextLineEdit->isVisibleTo(const_cast<ctkVTKTextPropertyWidget*>(this)));
  127. return d->TextLineEdit->isVisibleTo(const_cast<ctkVTKTextPropertyWidget*>(this));
  128. }
  129. //-----------------------------------------------------------------------------
  130. void ctkVTKTextPropertyWidget::setText(const QString& textString)
  131. {
  132. Q_D(ctkVTKTextPropertyWidget);
  133. d->TextLineEdit->setText(textString);
  134. }
  135. //-----------------------------------------------------------------------------
  136. QString ctkVTKTextPropertyWidget::text()const
  137. {
  138. Q_D(const ctkVTKTextPropertyWidget);
  139. return d->TextLineEdit->text();
  140. }
  141. //-----------------------------------------------------------------------------
  142. void ctkVTKTextPropertyWidget::setTextLabel(const QString& label)
  143. {
  144. Q_D(ctkVTKTextPropertyWidget);
  145. d->TextLabel->setText(label);
  146. }
  147. //-----------------------------------------------------------------------------
  148. QString ctkVTKTextPropertyWidget::textLabel()const
  149. {
  150. Q_D(const ctkVTKTextPropertyWidget);
  151. return d->TextLabel->text();
  152. }
  153. //-----------------------------------------------------------------------------
  154. void ctkVTKTextPropertyWidget::setColor(const QColor& color)
  155. {
  156. Q_D(const ctkVTKTextPropertyWidget);
  157. if (d->TextProperty)
  158. {
  159. return;
  160. }
  161. d->TextProperty->SetColor(color.redF(), color.greenF(), color.blueF());
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ctkVTKTextPropertyWidget::setOpacity(double opacity)
  165. {
  166. Q_D(const ctkVTKTextPropertyWidget);
  167. if (d->TextProperty)
  168. {
  169. return;
  170. }
  171. d->TextProperty->SetOpacity(opacity);
  172. }
  173. //-----------------------------------------------------------------------------
  174. void ctkVTKTextPropertyWidget::setFont(const QString& font)
  175. {
  176. Q_D(const ctkVTKTextPropertyWidget);
  177. if (d->TextProperty)
  178. {
  179. return;
  180. }
  181. d->TextProperty->SetFontFamilyAsString(font.toStdString().data());
  182. }
  183. //-----------------------------------------------------------------------------
  184. void ctkVTKTextPropertyWidget::setBold(bool enable)
  185. {
  186. Q_D(const ctkVTKTextPropertyWidget);
  187. if (d->TextProperty)
  188. {
  189. return;
  190. }
  191. d->TextProperty->SetBold(enable);
  192. }
  193. //-----------------------------------------------------------------------------
  194. void ctkVTKTextPropertyWidget::setItalic(bool enable)
  195. {
  196. Q_D(const ctkVTKTextPropertyWidget);
  197. if (d->TextProperty)
  198. {
  199. return;
  200. }
  201. d->TextProperty->SetItalic(enable);
  202. }
  203. //-----------------------------------------------------------------------------
  204. void ctkVTKTextPropertyWidget::setShadow(bool enable)
  205. {
  206. Q_D(const ctkVTKTextPropertyWidget);
  207. if (d->TextProperty)
  208. {
  209. return;
  210. }
  211. d->TextProperty->SetShadow(enable);
  212. }