Browse Source

Merge branch 'ctkMatrixWidget-values-property'

* ctkMatrixWidget-values-property:
  Factorize ctkMatrixWidget::setValues
  Added a Q_PROPERTY values/setValues to the ctkMatrixWidget.
Julien Finet 14 years ago
parent
commit
5ebd60003b

+ 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;
     }

+ 30 - 12
Libs/Widgets/ctkMatrixWidget.cpp

@@ -401,33 +401,51 @@ double ctkMatrixWidget::value(int i, int j)const
 }
 
 // --------------------------------------------------------------------------
-void ctkMatrixWidget::setValue(int i, int j, double _value)
+void ctkMatrixWidget::setValue(int i, int j, double newValue)
 {
   Q_D(ctkMatrixWidget);
   Q_ASSERT( i>=0 && i<this->rowCount() &&
             j>=0 && j<this->columnCount());
 
-  d->Table->item(i, j)->setData(Qt::DisplayRole,
-                                QVariant(qBound(d->Minimum, _value, d->Maximum)));
+  newValue = qBound(d->Minimum, newValue, d->Maximum);
+  d->Table->item(i, j)->setData(Qt::DisplayRole, QVariant(newValue));
 }
 
 // --------------------------------------------------------------------------
-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);
+  Q_ASSERT(vector.size() == this->rowCount() * this->columnCount());
+  // As we are potentially making a lot of changes, just fire a unique
+  // signal at the end if at least one matrix value has been changed.
   bool blocked = this->blockSignals(true);
   bool modified = false;
-  for (int i=0; i < this->rowCount(); i++)
+  for (int row=0; row < this->rowCount(); ++row)
     {
-    for (int j=0; j < this->columnCount(); j++)
+    for (int column=0; column < this->columnCount(); ++column)
       {
-      double value = qBound(d->Minimum,
-                            vector.at(i * this->columnCount() + j),
-                            d->Maximum);
-      double oldValue = d->Table->item(i,j)->data(Qt::DisplayRole).toDouble();
-      d->Table->item(i,j)->setData(Qt::DisplayRole, QVariant(value));
-      if (oldValue != value)
+      double newValue = vector.at(row * this->columnCount() + column);
+      newValue = qBound(d->Minimum, newValue, d->Maximum);
+      if (newValue != this->value(row, column))
         {
+        this->setValue(row, column, newValue);
         modified = 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