Просмотр исходного кода

Added a Q_PROPERTY values/setValues to the ctkMatrixWidget.

This change enables getting and setting the values of a ctkMatrixWidget
as a QVector<double>.

Instead of accessing the widget itself, use the existing
ctkMatrixWidget::value(i,j) functionality.
Daniel Haehn лет назад: 14
Родитель
Сommit
79c4f2916c

+ 2 - 2
Libs/Widgets/Testing/Cpp/ctkMatrixWidgetTest2.cpp

@@ -165,11 +165,11 @@ int ctkMatrixWidgetTest2(int argc, char * argv [] )
   items.push_back(206.);
   items.push_back(207.);
   items.push_back(208.);
-  matrixWidget.setVector(items);
+  matrixWidget.setValues(items);
 
   if (matrixWidget.value(2,1) != 207.)
     {
-    std::cerr << "ctkMatrixWidget::setVector() failed:"
+    std::cerr << "ctkMatrixWidget::setValues() failed:"
               << matrixWidget.value(2,1) << std::endl;
     return EXIT_FAILURE;
     }

+ 18 - 1
Libs/Widgets/ctkMatrixWidget.cpp

@@ -412,7 +412,24 @@ void ctkMatrixWidget::setValue(int i, int j, double _value)
 }
 
 // --------------------------------------------------------------------------
-void ctkMatrixWidget::setVector(const QVector<double> & vector)
+QVector<double> ctkMatrixWidget::values()const
+{
+  Q_D(const ctkMatrixWidget);
+  QVector<double> values;
+
+  for (int i=0; i < this->rowCount(); i++)
+    {
+    for (int j=0; j < this->columnCount(); j++)
+      {
+      values.push_back(this->value(i,j));
+      }
+    }
+
+  return values;
+}
+
+// --------------------------------------------------------------------------
+void ctkMatrixWidget::setValues(const QVector<double> & vector)
 {
   Q_D(ctkMatrixWidget);
   bool blocked = this->blockSignals(true);

+ 4 - 1
Libs/Widgets/ctkMatrixWidget.h

@@ -22,6 +22,7 @@
 #define __ctkMatrixWidget_h
 
 /// Qt includes
+#include <QVector>
 #include <QWidget>
 
 /// CTK includes
@@ -42,6 +43,7 @@ class CTK_WIDGETS_EXPORT ctkMatrixWidget: public QWidget
   Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
   Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
   Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
+  Q_PROPERTY(QVector<double> values READ values WRITE setValues)
 
 public:
   /// Superclass typedef
@@ -72,7 +74,8 @@ public:
   /// that is less than the minimum or greater than the maximum.
   double value(int i, int j)const;
   void setValue(int i, int j, double value);
-  void setVector(const QVector<double> & vector);
+  QVector<double> values()const;
+  void setValues(const QVector<double> & vector);
 
   ///
   /// This property determines whether the user can edit values by