ctkVTKTextPropertyWidgetTest1.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <QApplication>
  16. #include <QDebug>
  17. #include <QTimer>
  18. // CTK includes
  19. #include "ctkVTKTextPropertyWidget.h"
  20. // VTK includes
  21. #include <vtkTextProperty.h>
  22. // STD includes
  23. #include <iostream>
  24. //-----------------------------------------------------------------------------
  25. int ctkVTKTextPropertyWidgetTest1(int argc, char * argv [] )
  26. {
  27. QApplication app(argc, argv);
  28. ctkVTKTextPropertyWidget textPropertyWidget(0);
  29. if (textPropertyWidget.isEnabled())
  30. {
  31. std::cerr << "No vtkTextProperty provided, should be disabled."
  32. << std::endl;
  33. return EXIT_FAILURE;
  34. }
  35. vtkTextProperty* textProperty = vtkTextProperty::New();
  36. double color[3];
  37. textProperty->GetColor(color);
  38. double opacity = textProperty->GetOpacity();
  39. QString fontFamily = textProperty->GetFontFamilyAsString();
  40. bool bold = textProperty->GetBold();
  41. bool italic = textProperty->GetItalic();
  42. bool shadow = textProperty->GetShadow();
  43. textPropertyWidget.setTextProperty(textProperty);
  44. textProperty->Delete();
  45. if (textPropertyWidget.textProperty() != textProperty)
  46. {
  47. std::cerr << "ctkVTKTextPropertyWidget::setTextProperty() failed."
  48. << textPropertyWidget.textProperty() << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. if (textPropertyWidget.color() != QColor::fromRgbF(color[0],color[1],color[2]))
  52. {
  53. std::cerr << "Wrong color" << std::endl;
  54. return EXIT_FAILURE;
  55. }
  56. if (textPropertyWidget.opacity() != opacity)
  57. {
  58. std::cerr << "Wrong opacity" << textPropertyWidget.opacity() << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. if (textPropertyWidget.font() != fontFamily)
  62. {
  63. std::cerr << "Wrong font" << textPropertyWidget.font().toStdString() << std::endl;
  64. return EXIT_FAILURE;
  65. }
  66. if (textPropertyWidget.isBold() != bold)
  67. {
  68. std::cerr << "Wrong bold" << textPropertyWidget.isBold() << std::endl;
  69. return EXIT_FAILURE;
  70. }
  71. if (textPropertyWidget.isItalic() != italic)
  72. {
  73. std::cerr << "Wrong italic" << textPropertyWidget.isItalic() << std::endl;
  74. return EXIT_FAILURE;
  75. }
  76. if (textPropertyWidget.hasShadow() != shadow)
  77. {
  78. std::cerr << "Wrong shadow" << textPropertyWidget.hasShadow() << std::endl;
  79. return EXIT_FAILURE;
  80. }
  81. textProperty->SetColor(0., 0.5, 1.);
  82. if (textPropertyWidget.color() != QColor::fromRgbF(0., 0.5, 1.))
  83. {
  84. std::cerr << "vtkTextProperty::SetColor() failed" << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. // QColor handles floating points on 16bit integers
  88. textPropertyWidget.setColor(QColor::fromRgbF(0.333, 0.666, 0.999));
  89. textProperty->GetColor(color);
  90. if (color[0] < 0.3329 || color[0] > 0.3331 ||
  91. color[1] < 0.6659 || color[1] > 0.6661 ||
  92. color[2] < 0.9989 || color[2] > 0.9991)
  93. {
  94. std::cerr << "ctkVTKTextPropertyWidget::setColor() failed: "
  95. << color[0]<< " " << color[1] << " " << color[2] << std::endl;
  96. return EXIT_FAILURE;
  97. }
  98. textProperty->SetOpacity(0.2);
  99. if (textPropertyWidget.opacity() != 0.2)
  100. {
  101. std::cerr << "vtkTextProperty::SetOpacity() failed" << std::endl;
  102. return EXIT_FAILURE;
  103. }
  104. textPropertyWidget.setOpacity(0.6);
  105. if (textProperty->GetOpacity() != 0.6)
  106. {
  107. std::cerr << "ctkVTKTextPropertyWidget::setOpacity() failed" << std::endl;
  108. return EXIT_FAILURE;
  109. }
  110. textProperty->SetFontFamilyToCourier();
  111. if (textPropertyWidget.font() != "Courier")
  112. {
  113. std::cerr << "vtkTextProperty::SetFontFamily() failed" << std::endl;
  114. return EXIT_FAILURE;
  115. }
  116. textPropertyWidget.setFont("Arial");
  117. if (QString(textProperty->GetFontFamilyAsString()) != "Arial")
  118. {
  119. std::cerr << "ctkVTKTextPropertyWidget::setFont() failed" << std::endl;
  120. return EXIT_FAILURE;
  121. }
  122. textProperty->SetBold(!bold);
  123. if (textPropertyWidget.isBold() == bold)
  124. {
  125. std::cerr << "vtkTextProperty::SetBold() failed" << std::endl;
  126. return EXIT_FAILURE;
  127. }
  128. textPropertyWidget.setBold(bold);
  129. if ((textProperty->GetBold() != 0) != bold)
  130. {
  131. std::cerr << "ctkVTKTextPropertyWidget::setBold() failed" << std::endl;
  132. return EXIT_FAILURE;
  133. }
  134. textProperty->SetItalic(!italic);
  135. if (textPropertyWidget.isItalic() == italic)
  136. {
  137. std::cerr << "vtkTextProperty::SetItalic() failed" << std::endl;
  138. return EXIT_FAILURE;
  139. }
  140. textPropertyWidget.setItalic(italic);
  141. if ((textProperty->GetItalic() != 0) != italic)
  142. {
  143. std::cerr << "ctkVTKTextPropertyWidget::setItalic() failed" << std::endl;
  144. return EXIT_FAILURE;
  145. }
  146. textProperty->SetShadow(!shadow);
  147. if (textPropertyWidget.hasShadow() == shadow)
  148. {
  149. std::cerr << "vtkTextProperty::SetShadow() failed" << std::endl;
  150. return EXIT_FAILURE;
  151. }
  152. textPropertyWidget.setShadow(shadow);
  153. if ((textProperty->GetShadow() != 0) != shadow)
  154. {
  155. std::cerr << "ctkVTKTextPropertyWidget::setShadow() failed" << std::endl;
  156. return EXIT_FAILURE;
  157. }
  158. textPropertyWidget.setText("My custom VTK text");
  159. if (textPropertyWidget.text() != "My custom VTK text")
  160. {
  161. std::cerr << "ctkVTKTextPropertyWidget::setText() failed"
  162. << textPropertyWidget.text().toStdString() << std::endl;
  163. return EXIT_FAILURE;
  164. }
  165. textPropertyWidget.setTextLabel("My text:");
  166. if (textPropertyWidget.textLabel() != "My text:")
  167. {
  168. std::cerr << "ctkVTKTextPropertyWidget::setTextLabel() failed"
  169. << textPropertyWidget.textLabel().toStdString() << std::endl;
  170. return EXIT_FAILURE;
  171. }
  172. textPropertyWidget.show();
  173. if (argc < 2 || QString(argv[1]) != "-I" )
  174. {
  175. QTimer::singleShot(200, &app, SLOT(quit()));
  176. }
  177. return app.exec();
  178. }