Ver código fonte

Add test to ctkVTKTextPropertyWidget

Create ctkVTKTextPropertyWidgetTest1 and fix bugs found while writting the
test.
Julien Finet 14 anos atrás
pai
commit
80ed3dfa07

+ 2 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/CMakeLists.txt

@@ -10,6 +10,7 @@ SET(TEST_SOURCES
   ctkTransferFunctionViewTest3.cpp
   ctkTransferFunctionViewTest4.cpp
   ctkTransferFunctionViewTest5.cpp
+  ctkVTKTextPropertyWidgetTest1.cpp
   )
 
 IF(CTK_USE_CHARTS)
@@ -71,6 +72,7 @@ IF (CTK_USE_CHARTS)
   SIMPLE_TEST( ctkVTKScalarsToColorsViewTest4 )
   SIMPLE_TEST( ctkVTKScalarsToColorsWidgetTest1 )
 ENDIF(CTK_USE_CHARTS)
+SIMPLE_TEST( ctkVTKTextPropertyWidgetTest1 )
 
 #
 # Add Tests expecting CTKData to be set

+ 230 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKTextPropertyWidgetTest1.cpp

@@ -0,0 +1,230 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QTimer>
+
+// CTK includes
+#include "ctkVTKTextPropertyWidget.h"
+
+// VTK includes
+#include <vtkTextProperty.h>
+
+// STD includes
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkVTKTextPropertyWidgetTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkVTKTextPropertyWidget textPropertyWidget(0);
+  
+  if (textPropertyWidget.isEnabled())
+    {
+    std::cerr << "No vtkTextProperty provided, should be disabled."
+              << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  vtkTextProperty* textProperty = vtkTextProperty::New();
+  
+  double color[3];
+  textProperty->GetColor(color);
+  double opacity = textProperty->GetOpacity();
+  QString fontFamily = textProperty->GetFontFamilyAsString();
+  bool bold = textProperty->GetBold();
+  bool italic = textProperty->GetItalic();
+  bool shadow = textProperty->GetShadow();
+  
+  textPropertyWidget.setTextProperty(textProperty);
+  //textProperty->Delete();
+  
+  if (textPropertyWidget.textProperty() != textProperty)
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setTextProperty() failed."
+              << textPropertyWidget.textProperty() << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  if (textPropertyWidget.color() != QColor::fromRgbF(color[0],color[1],color[2]))
+    {
+    std::cerr << "Wrong color" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (textPropertyWidget.opacity() != opacity)
+    {
+    std::cerr << "Wrong opacity" << textPropertyWidget.opacity() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (textPropertyWidget.font() != fontFamily)
+    {
+    std::cerr << "Wrong font" << textPropertyWidget.font().toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (textPropertyWidget.isBold() != bold)
+    {
+    std::cerr << "Wrong bold" << textPropertyWidget.isBold() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (textPropertyWidget.isItalic() != italic)
+    {
+    std::cerr << "Wrong italic" << textPropertyWidget.isItalic() << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  if (textPropertyWidget.hasShadow() != shadow)
+    {
+    std::cerr << "Wrong shadow" << textPropertyWidget.hasShadow() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textProperty->SetColor(0., 0.5, 1.);
+
+  if (textPropertyWidget.color() != QColor::fromRgbF(0., 0.5, 1.))
+    {
+    std::cerr << "vtkTextProperty::SetColor() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setColor(QColor::fromRgbF(0.333, 0.666, 0.999));
+  textProperty->GetColor(color);
+
+  if (qFuzzyCompare(color[0], 0.333) ||
+      qFuzzyCompare(color[1], 0.666) ||
+      qFuzzyCompare(color[2], 0.999))
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setColor() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textProperty->SetOpacity(0.2);
+
+  if (textPropertyWidget.opacity() != 0.2)
+    {
+    std::cerr << "vtkTextProperty::SetOpacity() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setOpacity(0.6);
+
+  if (textProperty->GetOpacity() != 0.6)
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setOpacity() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textProperty->SetFontFamilyToCourier();
+
+  if (textPropertyWidget.font() != "Courier")
+    {
+    std::cerr << "vtkTextProperty::SetFontFamily() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setFont("Arial");
+
+  if (QString(textProperty->GetFontFamilyAsString()) != "Arial")
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setFont() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textProperty->SetBold(!bold);
+
+  if (textPropertyWidget.isBold() == bold)
+    {
+    std::cerr << "vtkTextProperty::SetBold() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setBold(bold);
+
+  if ((textProperty->GetBold() != 0) != bold)
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setBold() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textProperty->SetItalic(!italic);
+
+  if (textPropertyWidget.isItalic() == italic)
+    {
+    std::cerr << "vtkTextProperty::SetItalic() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setItalic(italic);
+
+  if ((textProperty->GetItalic() != 0) != italic)
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setItalic() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  textProperty->SetShadow(!shadow);
+
+  if (textPropertyWidget.hasShadow() == shadow)
+    {
+    std::cerr << "vtkTextProperty::SetShadow() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setShadow(shadow);
+
+  if ((textProperty->GetShadow() != 0) != shadow)
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setShadow() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setText("My custom VTK text");
+
+  if (textPropertyWidget.text() != "My custom VTK text")
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setText() failed"
+              << textPropertyWidget.text().toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.setTextLabel("My text:");
+
+  if (textPropertyWidget.textLabel() != "My text:")
+    {
+    std::cerr << "ctkVTKTextPropertyWidget::setTextLabel() failed"
+              << textPropertyWidget.textLabel().toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  textPropertyWidget.show();
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}
+

+ 48 - 6
Libs/Visualization/VTK/Widgets/ctkVTKTextPropertyWidget.cpp

@@ -177,10 +177,17 @@ QString ctkVTKTextPropertyWidget::textLabel()const
 }
 
 //-----------------------------------------------------------------------------
+QColor ctkVTKTextPropertyWidget::color()const
+{
+  Q_D(const ctkVTKTextPropertyWidget);
+  return d->ColorPickerButton->color();
+}
+
+//-----------------------------------------------------------------------------
 void ctkVTKTextPropertyWidget::setColor(const QColor& color)
 {
   Q_D(const ctkVTKTextPropertyWidget);
-  if (d->TextProperty)
+  if (!d->TextProperty)
     {
     return;
     }
@@ -188,10 +195,17 @@ void ctkVTKTextPropertyWidget::setColor(const QColor& color)
 }
 
 //-----------------------------------------------------------------------------
+double ctkVTKTextPropertyWidget::opacity()const
+{
+  Q_D(const ctkVTKTextPropertyWidget);
+  return d->OpacitySlider->value();
+}
+
+//-----------------------------------------------------------------------------
 void ctkVTKTextPropertyWidget::setOpacity(double opacity)
 {
   Q_D(const ctkVTKTextPropertyWidget);
-  if (d->TextProperty)
+  if (!d->TextProperty)
     {
     return;
     }
@@ -199,10 +213,17 @@ void ctkVTKTextPropertyWidget::setOpacity(double opacity)
 }
 
 //-----------------------------------------------------------------------------
+QString ctkVTKTextPropertyWidget::font()const
+{
+  Q_D(const ctkVTKTextPropertyWidget);
+  return d->FontComboBox->currentText();
+}
+
+//-----------------------------------------------------------------------------
 void ctkVTKTextPropertyWidget::setFont(const QString& font)
 {
   Q_D(const ctkVTKTextPropertyWidget);
-  if (d->TextProperty)
+  if (!d->TextProperty)
     {
     return;
     }
@@ -210,10 +231,17 @@ void ctkVTKTextPropertyWidget::setFont(const QString& font)
 }
 
 //-----------------------------------------------------------------------------
+bool ctkVTKTextPropertyWidget::isBold()const
+{
+  Q_D(const ctkVTKTextPropertyWidget);
+  return d->BoldCheckBox->isChecked();
+}
+
+//-----------------------------------------------------------------------------
 void ctkVTKTextPropertyWidget::setBold(bool enable)
 {
   Q_D(const ctkVTKTextPropertyWidget);
-  if (d->TextProperty)
+  if (!d->TextProperty)
     {
     return;
     }
@@ -221,10 +249,17 @@ void ctkVTKTextPropertyWidget::setBold(bool enable)
 }
 
 //-----------------------------------------------------------------------------
+bool ctkVTKTextPropertyWidget::isItalic()const
+{
+  Q_D(const ctkVTKTextPropertyWidget);
+  return d->ItalicCheckBox->isChecked();
+}
+
+//-----------------------------------------------------------------------------
 void ctkVTKTextPropertyWidget::setItalic(bool enable)
 {
   Q_D(const ctkVTKTextPropertyWidget);
-  if (d->TextProperty)
+  if (!d->TextProperty)
     {
     return;
     }
@@ -232,10 +267,17 @@ void ctkVTKTextPropertyWidget::setItalic(bool enable)
 }
 
 //-----------------------------------------------------------------------------
+bool ctkVTKTextPropertyWidget::hasShadow()const
+{
+  Q_D(const ctkVTKTextPropertyWidget);
+  return d->ShadowCheckBox->isChecked();
+}
+
+//-----------------------------------------------------------------------------
 void ctkVTKTextPropertyWidget::setShadow(bool enable)
 {
   Q_D(const ctkVTKTextPropertyWidget);
-  if (d->TextProperty)
+  if (!d->TextProperty)
     {
     return;
     }

+ 13 - 0
Libs/Visualization/VTK/Widgets/ctkVTKTextPropertyWidget.h

@@ -53,6 +53,19 @@ public:
 
   QString text()const;
   QString textLabel()const;
+  
+  /// Color of vtkTextProperty
+  QColor color()const;
+  /// Opacity of vtkTextProperty
+  double opacity()const;
+  /// Font of vtkTextProperty
+  QString font()const;
+  /// Is the text bold
+  bool isBold()const;
+  /// Is the text italic
+  bool isItalic()const;
+  /// Has the text shadows
+  bool hasShadow()const;
 
 public slots:
   void setTextProperty(vtkTextProperty* textProperty);