ctkVTKTextPropertyWidget.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. // 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(QString)),
  49. q, SIGNAL(textChanged(QString)));
  50. QObject::connect(this->ColorPickerButton, SIGNAL(colorChanged(QColor)),
  51. q, SLOT(setColor(QColor)));
  52. QObject::connect(this->OpacitySlider, SIGNAL(valueChanged(double)),
  53. q, SLOT(setOpacity(double)));
  54. QObject::connect(this->FontComboBox, SIGNAL(currentIndexChanged(QString)),
  55. q, SLOT(setFont(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. emit modified();
  175. }
  176. //-----------------------------------------------------------------------------
  177. double ctkVTKTextPropertyWidget::opacity()const
  178. {
  179. Q_D(const ctkVTKTextPropertyWidget);
  180. return d->OpacitySlider->value();
  181. }
  182. //-----------------------------------------------------------------------------
  183. void ctkVTKTextPropertyWidget::setOpacity(double opacity)
  184. {
  185. Q_D(const ctkVTKTextPropertyWidget);
  186. if (d->TextProperty.GetPointer() == 0)
  187. {
  188. return;
  189. }
  190. d->TextProperty->SetOpacity(opacity);
  191. emit modified();
  192. }
  193. //-----------------------------------------------------------------------------
  194. QString ctkVTKTextPropertyWidget::font()const
  195. {
  196. Q_D(const ctkVTKTextPropertyWidget);
  197. return d->FontComboBox->currentText();
  198. }
  199. //-----------------------------------------------------------------------------
  200. void ctkVTKTextPropertyWidget::setFont(const QString& font)
  201. {
  202. Q_D(const ctkVTKTextPropertyWidget);
  203. if (d->TextProperty.GetPointer() == 0)
  204. {
  205. return;
  206. }
  207. d->TextProperty->SetFontFamilyAsString(font.toStdString().data());
  208. emit modified();
  209. }
  210. //-----------------------------------------------------------------------------
  211. bool ctkVTKTextPropertyWidget::isBold()const
  212. {
  213. Q_D(const ctkVTKTextPropertyWidget);
  214. return d->BoldCheckBox->isChecked();
  215. }
  216. //-----------------------------------------------------------------------------
  217. void ctkVTKTextPropertyWidget::setBold(bool enable)
  218. {
  219. Q_D(const ctkVTKTextPropertyWidget);
  220. if (d->TextProperty.GetPointer() == 0)
  221. {
  222. return;
  223. }
  224. d->TextProperty->SetBold(enable);
  225. emit modified();
  226. }
  227. //-----------------------------------------------------------------------------
  228. bool ctkVTKTextPropertyWidget::isItalic()const
  229. {
  230. Q_D(const ctkVTKTextPropertyWidget);
  231. return d->ItalicCheckBox->isChecked();
  232. }
  233. //-----------------------------------------------------------------------------
  234. void ctkVTKTextPropertyWidget::setItalic(bool enable)
  235. {
  236. Q_D(const ctkVTKTextPropertyWidget);
  237. if (d->TextProperty.GetPointer() == 0)
  238. {
  239. return;
  240. }
  241. d->TextProperty->SetItalic(enable);
  242. emit modified();
  243. }
  244. //-----------------------------------------------------------------------------
  245. bool ctkVTKTextPropertyWidget::hasShadow()const
  246. {
  247. Q_D(const ctkVTKTextPropertyWidget);
  248. return d->ShadowCheckBox->isChecked();
  249. }
  250. //-----------------------------------------------------------------------------
  251. void ctkVTKTextPropertyWidget::setShadow(bool enable)
  252. {
  253. Q_D(const ctkVTKTextPropertyWidget);
  254. if (d->TextProperty.GetPointer() == 0)
  255. {
  256. return;
  257. }
  258. d->TextProperty->SetShadow(enable);
  259. emit modified();
  260. }