ctkVTKTextPropertyWidget.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // CTK includes
  17. #include "ctkVTKTextPropertyWidget.h"
  18. #include "ui_ctkVTKTextPropertyWidget.h"
  19. // VTK includes
  20. #include <vtkSmartPointer.h>
  21. #include <vtkTextProperty.h>
  22. //-----------------------------------------------------------------------------
  23. class ctkVTKTextPropertyWidgetPrivate: public Ui_ctkVTKTextPropertyWidget
  24. {
  25. Q_DECLARE_PUBLIC(ctkVTKTextPropertyWidget);
  26. protected:
  27. ctkVTKTextPropertyWidget* const q_ptr;
  28. public:
  29. void init();
  30. ctkVTKTextPropertyWidgetPrivate(ctkVTKTextPropertyWidget& object);
  31. vtkSmartPointer<vtkTextProperty> TextProperty;
  32. };
  33. //-----------------------------------------------------------------------------
  34. ctkVTKTextPropertyWidgetPrivate::ctkVTKTextPropertyWidgetPrivate(ctkVTKTextPropertyWidget& object)
  35. :q_ptr(&object)
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. ctkVTKTextPropertyWidget::~ctkVTKTextPropertyWidget()
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. void ctkVTKTextPropertyWidgetPrivate::init()
  44. {
  45. Q_Q(ctkVTKTextPropertyWidget);
  46. this->setupUi(q);
  47. q->updateFromTextProperty();
  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.GetPointer();
  94. }
  95. //-----------------------------------------------------------------------------
  96. void ctkVTKTextPropertyWidget::updateFromTextProperty()
  97. {
  98. Q_D(ctkVTKTextPropertyWidget);
  99. this->setEnabled(d->TextProperty.GetPointer() != 0);
  100. if (d->TextProperty.GetPointer() == 0)
  101. {
  102. d->ColorPickerButton->setColor(QColor());
  103. d->OpacitySlider->setValue(1.);
  104. d->FontComboBox->setCurrentIndex(-1);
  105. d->BoldCheckBox->setChecked(false);
  106. d->ItalicCheckBox->setChecked(false);
  107. d->ShadowCheckBox->setChecked(false);
  108. return;
  109. }
  110. double* color = d->TextProperty->GetColor();
  111. d->ColorPickerButton->setColor(
  112. QColor::fromRgbF(color[0],color[1],color[2]));
  113. d->OpacitySlider->setValue(d->TextProperty->GetOpacity());
  114. d->FontComboBox->setCurrentIndex(
  115. d->FontComboBox->findText(d->TextProperty->GetFontFamilyAsString()));
  116. d->BoldCheckBox->setChecked(d->TextProperty->GetBold() != 0);
  117. d->ItalicCheckBox->setChecked(d->TextProperty->GetItalic() != 0);
  118. d->ShadowCheckBox->setChecked(d->TextProperty->GetShadow() != 0);
  119. }
  120. //-----------------------------------------------------------------------------
  121. void ctkVTKTextPropertyWidget::setTextVisible(bool visible)
  122. {
  123. Q_D(ctkVTKTextPropertyWidget);
  124. d->TextLabel->setVisible(visible);
  125. d->TextLineEdit->setVisible(visible);
  126. }
  127. //-----------------------------------------------------------------------------
  128. bool ctkVTKTextPropertyWidget::isTextVisible()const
  129. {
  130. Q_D(const ctkVTKTextPropertyWidget);
  131. Q_ASSERT(d->TextLabel->isVisibleTo(const_cast<ctkVTKTextPropertyWidget*>(this))
  132. == d->TextLineEdit->isVisibleTo(const_cast<ctkVTKTextPropertyWidget*>(this)));
  133. return d->TextLineEdit->isVisibleTo(const_cast<ctkVTKTextPropertyWidget*>(this));
  134. }
  135. //-----------------------------------------------------------------------------
  136. void ctkVTKTextPropertyWidget::setText(const QString& textString)
  137. {
  138. Q_D(ctkVTKTextPropertyWidget);
  139. d->TextLineEdit->setText(textString);
  140. }
  141. //-----------------------------------------------------------------------------
  142. QString ctkVTKTextPropertyWidget::text()const
  143. {
  144. Q_D(const ctkVTKTextPropertyWidget);
  145. return d->TextLineEdit->text();
  146. }
  147. //-----------------------------------------------------------------------------
  148. void ctkVTKTextPropertyWidget::setTextLabel(const QString& label)
  149. {
  150. Q_D(ctkVTKTextPropertyWidget);
  151. d->TextLabel->setText(label);
  152. }
  153. //-----------------------------------------------------------------------------
  154. QString ctkVTKTextPropertyWidget::textLabel()const
  155. {
  156. Q_D(const ctkVTKTextPropertyWidget);
  157. return d->TextLabel->text();
  158. }
  159. //-----------------------------------------------------------------------------
  160. QColor ctkVTKTextPropertyWidget::color()const
  161. {
  162. Q_D(const ctkVTKTextPropertyWidget);
  163. return d->ColorPickerButton->color();
  164. }
  165. //-----------------------------------------------------------------------------
  166. void ctkVTKTextPropertyWidget::setColor(const QColor& color)
  167. {
  168. Q_D(const ctkVTKTextPropertyWidget);
  169. if (d->TextProperty.GetPointer() == 0)
  170. {
  171. return;
  172. }
  173. d->TextProperty->SetColor(color.redF(), color.greenF(), color.blueF());
  174. }
  175. //-----------------------------------------------------------------------------
  176. double ctkVTKTextPropertyWidget::opacity()const
  177. {
  178. Q_D(const ctkVTKTextPropertyWidget);
  179. return d->OpacitySlider->value();
  180. }
  181. //-----------------------------------------------------------------------------
  182. void ctkVTKTextPropertyWidget::setOpacity(double opacity)
  183. {
  184. Q_D(const ctkVTKTextPropertyWidget);
  185. if (d->TextProperty.GetPointer() == 0)
  186. {
  187. return;
  188. }
  189. d->TextProperty->SetOpacity(opacity);
  190. }
  191. //-----------------------------------------------------------------------------
  192. QString ctkVTKTextPropertyWidget::font()const
  193. {
  194. Q_D(const ctkVTKTextPropertyWidget);
  195. return d->FontComboBox->currentText();
  196. }
  197. //-----------------------------------------------------------------------------
  198. void ctkVTKTextPropertyWidget::setFont(const QString& font)
  199. {
  200. Q_D(const ctkVTKTextPropertyWidget);
  201. if (d->TextProperty.GetPointer() == 0)
  202. {
  203. return;
  204. }
  205. d->TextProperty->SetFontFamilyAsString(font.toStdString().data());
  206. }
  207. //-----------------------------------------------------------------------------
  208. bool ctkVTKTextPropertyWidget::isBold()const
  209. {
  210. Q_D(const ctkVTKTextPropertyWidget);
  211. return d->BoldCheckBox->isChecked();
  212. }
  213. //-----------------------------------------------------------------------------
  214. void ctkVTKTextPropertyWidget::setBold(bool enable)
  215. {
  216. Q_D(const ctkVTKTextPropertyWidget);
  217. if (d->TextProperty.GetPointer() == 0)
  218. {
  219. return;
  220. }
  221. d->TextProperty->SetBold(enable);
  222. }
  223. //-----------------------------------------------------------------------------
  224. bool ctkVTKTextPropertyWidget::isItalic()const
  225. {
  226. Q_D(const ctkVTKTextPropertyWidget);
  227. return d->ItalicCheckBox->isChecked();
  228. }
  229. //-----------------------------------------------------------------------------
  230. void ctkVTKTextPropertyWidget::setItalic(bool enable)
  231. {
  232. Q_D(const ctkVTKTextPropertyWidget);
  233. if (d->TextProperty.GetPointer() == 0)
  234. {
  235. return;
  236. }
  237. d->TextProperty->SetItalic(enable);
  238. }
  239. //-----------------------------------------------------------------------------
  240. bool ctkVTKTextPropertyWidget::hasShadow()const
  241. {
  242. Q_D(const ctkVTKTextPropertyWidget);
  243. return d->ShadowCheckBox->isChecked();
  244. }
  245. //-----------------------------------------------------------------------------
  246. void ctkVTKTextPropertyWidget::setShadow(bool enable)
  247. {
  248. Q_D(const ctkVTKTextPropertyWidget);
  249. if (d->TextProperty.GetPointer() == 0)
  250. {
  251. return;
  252. }
  253. d->TextProperty->SetShadow(enable);
  254. }