Procházet zdrojové kódy

Add ctkMatrixWidgetTest2 for ctkMatrixWidget

ctkMatrixWidget was not fully covered for test coverage. Add a more
exhaustive list of tests (editing is not tested) and fix discovered bugs.
Julien Finet před 14 roky
rodič
revize
0cd33abd7e

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

@@ -18,6 +18,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkFileDialogTest1.cpp
   ctkFittedTextBrowserTest1.cpp
   ctkMatrixWidgetTest1.cpp
+  ctkMatrixWidgetTest2.cpp
   ctkRangeSliderTest1.cpp
   ctkRangeWidgetTest1.cpp
   ctkDateRangeWidgetTest1.cpp
@@ -80,6 +81,7 @@ SIMPLE_TEST( ctkDoubleSliderTest1 )
 SIMPLE_TEST( ctkFileDialogTest1 )
 SIMPLE_TEST( ctkFittedTextBrowserTest1 )
 SIMPLE_TEST( ctkMatrixWidgetTest1 )
+SIMPLE_TEST( ctkMatrixWidgetTest2 )
 SIMPLE_TEST( ctkRangeSliderTest1 )
 SIMPLE_TEST( ctkRangeWidgetTest1 )
 SIMPLE_TEST( ctkDateRangeWidgetTest1 )

+ 212 - 0
Libs/Widgets/Testing/Cpp/ctkMatrixWidgetTest2.cpp

@@ -0,0 +1,212 @@
+/*=========================================================================
+
+  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 <QHBoxLayout>
+#include <QMouseEvent>
+#include <QTimer>
+
+// CTK includes
+#include "ctkMatrixWidget.h"
+
+// STD includes
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkMatrixWidgetTest2(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  QWidget topLevel;
+  QHBoxLayout* layout = new QHBoxLayout(&topLevel);
+  ctkMatrixWidget matrixWidget;
+  // 4x4 by default, if not anymore, change the documentation
+  if (matrixWidget.rowCount() != 4 ||
+      matrixWidget.columnCount() != 4)
+    {
+    std::cerr << "Default constructor doesn't create a 4x4 matrix" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  for (int i = 0; i != 4; ++i)
+    {
+    for (int j = 0; j != 4; ++j)
+      {
+      if ((i == j && matrixWidget.value(i,j) != 1.) ||
+          (i != j && matrixWidget.value(i,j) != 0.))
+        {
+        std::cerr << "Not an identity matrix: (" << i << "," << j << ") = "
+                  << matrixWidget.value(i,j) << std::endl;
+        return EXIT_FAILURE;
+        }
+      }
+    }
+  matrixWidget.setValue(2,3, 15.352);
+
+  if (matrixWidget.value(2,3) != 15.352)
+    {
+    std::cerr << "ctkMatrixWidget::setValue() failed: "
+              << matrixWidget.value(2,3) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // resize the matrix:
+  matrixWidget.setRowCount(8);
+
+  if (matrixWidget.rowCount() != 8 ||
+      matrixWidget.columnCount() != 4)
+    {
+    std::cerr << "ctkMatrixWidget::setRowCount() failed "
+              << matrixWidget.rowCount() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setColumnCount(6);
+
+  if (matrixWidget.rowCount() != 8 ||
+      matrixWidget.columnCount() != 6)
+    {
+    std::cerr << "ctkMatrixWidget::setColumnCount() failed: "
+              << matrixWidget.columnCount() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setEditable(false);
+
+  if (matrixWidget.isEditable())
+    {
+    std::cerr << "ctkMatrixWidget::setEditable() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setMinimum(0.5);
+
+  if (matrixWidget.minimum() != 0.5 ||
+      matrixWidget.value(1,0) != 0.5 )
+    {
+    std::cerr << "ctkMatrixWidget::setMinimum() failed:"
+              << matrixWidget.value(1,0) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setMaximum(0.7);
+
+  if (matrixWidget.maximum() != 0.7 ||
+      matrixWidget.value(1,1) != 0.7 )
+    {
+    std::cerr << "ctkMatrixWidget::setMaximum() failed:"
+              << matrixWidget.value(1,1) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setMinimum(40.);
+
+  if (matrixWidget.minimum() != 40. ||
+      matrixWidget.maximum() != 40. ||
+      matrixWidget.value(2,2) != 40. ||
+      matrixWidget.value(1,2) != 40.)
+    {
+    std::cerr << "ctkMatrixWidget::setMinimum() failed:"
+              << matrixWidget.maximum() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setRowCount(3);
+  matrixWidget.setColumnCount(3);
+
+  if (matrixWidget.minimum() != 40. ||
+      matrixWidget.maximum() != 40. ||
+      matrixWidget.value(2,2) != 40. ||
+      matrixWidget.value(1,2) != 40.)
+    {
+    std::cerr << "ctkMatrixWidget::setMinimum() failed:"
+              << matrixWidget.maximum() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setRange(250., 10.);
+
+  if (matrixWidget.minimum() != 10. ||
+      matrixWidget.maximum() != 250. ||
+      matrixWidget.value(0,0) != 40. ||
+      matrixWidget.value(2,1) != 40.)
+    {
+    std::cerr << "ctkMatrixWidget::setRange() failed:"
+              << matrixWidget.minimum() << " "
+              << matrixWidget.maximum() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  QVector<double> items;
+  items.push_back(200.);
+  items.push_back(201.);
+  items.push_back(202.);
+  items.push_back(203.);
+  items.push_back(204.);
+  items.push_back(205.);
+  items.push_back(206.);
+  items.push_back(207.);
+  items.push_back(208.);
+  matrixWidget.setVector(items);
+
+  if (matrixWidget.value(2,1) != 207.)
+    {
+    std::cerr << "ctkMatrixWidget::setVector() failed:"
+              << matrixWidget.value(2,1) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  matrixWidget.setSingleStep(1.);
+  if (matrixWidget.singleStep() != 1.)
+    {
+    std::cerr << "ctkMatrixWidget::setSingleStep() failed:"
+              << matrixWidget.singleStep() << std::endl;
+    }
+
+  matrixWidget.setDecimals(5);
+  if (matrixWidget.decimals() != 5)
+    {
+    std::cerr << "ctkMatrixWidget::setDecimals() failed:"
+              << matrixWidget.decimals() << std::endl;
+    }
+
+
+  matrixWidget.setDecimals(-1);
+  if (matrixWidget.decimals() != 0)
+    {
+    std::cerr << "ctkMatrixWidget::setDecimals() failed:"
+              << matrixWidget.decimals() << std::endl;
+    }
+
+  matrixWidget.setEditable(true);
+
+  layout->addWidget(&matrixWidget);
+  topLevel.setLayout(layout);
+  topLevel.show();
+  topLevel.resize(300, 401);
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}

+ 12 - 4
Libs/Widgets/ctkMatrixWidget.cpp

@@ -283,7 +283,7 @@ void ctkMatrixWidget::setRowCount(int rc)
 }
 
 // --------------------------------------------------------------------------
-bool ctkMatrixWidget::editable()const
+bool ctkMatrixWidget::isEditable()const
 {
   Q_D(const ctkMatrixWidget);
   return d->Table->editTriggers();
@@ -303,13 +303,13 @@ CTK_GET_CXX(ctkMatrixWidget, double, maximum, Maximum);
 CTK_GET_CXX(ctkMatrixWidget, double, singleStep, SingleStep);
 CTK_SET_CXX(ctkMatrixWidget, double, setSingleStep, SingleStep);
 CTK_GET_CXX(ctkMatrixWidget, int, decimals, Decimals);
-CTK_SET_CXX(ctkMatrixWidget, int, setDecimals, Decimals);
 
 // --------------------------------------------------------------------------
 void ctkMatrixWidget::setMinimum(double newMinimum)
 {
   Q_D(ctkMatrixWidget);
   d->Minimum = newMinimum;
+  d->Maximum = qMax(newMinimum, d->Maximum);
   d->validateItems();
 }
 
@@ -317,6 +317,7 @@ void ctkMatrixWidget::setMinimum(double newMinimum)
 void ctkMatrixWidget::setMaximum(double newMaximum)
 {
   Q_D(ctkMatrixWidget);
+  d->Minimum = qMin(d->Minimum, newMaximum);
   d->Maximum = newMaximum;
   d->validateItems();
 }
@@ -325,12 +326,19 @@ void ctkMatrixWidget::setMaximum(double newMaximum)
 void ctkMatrixWidget::setRange(double newMinimum, double newMaximum)
 {
   Q_D(ctkMatrixWidget);
-  d->Minimum = newMinimum;
-  d->Maximum = newMaximum;
+  d->Minimum = qMin(newMinimum, newMaximum);
+  d->Maximum = qMax(newMinimum, newMaximum);
   d->validateItems();
 }
 
 // --------------------------------------------------------------------------
+void ctkMatrixWidget::setDecimals(int decimals)
+{
+  Q_D(ctkMatrixWidget);
+  d->Decimals = qMax(0, decimals);
+}
+
+// --------------------------------------------------------------------------
 QSize ctkMatrixWidget::minimumSizeHint() const
 {
   Q_D(const ctkMatrixWidget);

+ 5 - 4
Libs/Widgets/ctkMatrixWidget.h

@@ -40,7 +40,7 @@ class CTK_WIDGETS_EXPORT ctkMatrixWidget: public QWidget
   Q_OBJECT
   Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)
   Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount)
-  Q_PROPERTY(bool editable READ editable WRITE setEditable)
+  Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
   Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
   Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
   Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
@@ -50,7 +50,7 @@ public:
   /// Superclass typedef
   typedef QWidget Superclass;
 
-  /// Constructor, builds a 4x4 matrix
+  /// Constructor, builds a 4x4 identity matrix
   explicit ctkMatrixWidget(QWidget* parent = 0);
   /// Constructor, builds a custom rowsXcolumns matrix
   explicit ctkMatrixWidget(int rows, int columns, QWidget* parent = 0);
@@ -78,8 +78,9 @@ public:
   void setVector(const QVector<double> & vector);
 
   ///
-  /// This property determines whether the user can edit values
-  bool editable()const;
+  /// This property determines whether the user can edit values by
+  /// double clicking on the items. True by default
+  bool isEditable()const;
   void setEditable(bool newEditable);
 
   ///