Parcourir la source

Factorize ctkMatrixWidget::setValues

Reuse setValue inside setValues.
Julien Finet il y a 13 ans
Parent
commit
aacece773c
1 fichiers modifiés avec 12 ajouts et 11 suppressions
  1. 12 11
      Libs/Widgets/ctkMatrixWidget.cpp

+ 12 - 11
Libs/Widgets/ctkMatrixWidget.cpp

@@ -401,14 +401,14 @@ 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));
 }
 
 // --------------------------------------------------------------------------
@@ -432,19 +432,20 @@ QVector<double> ctkMatrixWidget::values()const
 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;
         }
       }