Przeglądaj źródła

Cleanup ctkMatrixWidget

ctkMatrixWidget is now editable by default
ctkMatrixWidget doesn't support selection by default
ctkMatrixWidget::reset() is renamed into ctkMatrixWidget::identity()
Julien Finet 14 lat temu
rodzic
commit
5bb212407e

+ 10 - 5
Libs/Widgets/Testing/Cpp/ctkMatrixWidgetTest1.cpp

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) Kitware Inc.
   Copyright (c) Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,17 +15,17 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
 // Qt includes
 // Qt includes
 #include <QApplication>
 #include <QApplication>
+#include <QTimer>
 
 
 // CTK includes
 // CTK includes
 #include "ctkMatrixWidget.h"
 #include "ctkMatrixWidget.h"
 
 
 // STD includes
 // STD includes
-#include <cstdlib>
 #include <iostream>
 #include <iostream>
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -33,9 +33,14 @@ int ctkMatrixWidgetTest1(int argc, char * argv [] )
 {
 {
   QApplication app(argc, argv);
   QApplication app(argc, argv);
 
 
-  ctkMatrixWidget ctkObject;
+  ctkMatrixWidget matrixWidget;
+  matrixWidget.show();
 
 
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
 
 
-  return EXIT_SUCCESS;
+  return app.exec();
 }
 }
 
 

+ 22 - 20
Libs/Widgets/ctkMatrixWidget.cpp

@@ -38,16 +38,18 @@
 namespace
 namespace
 {
 {
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-  class CustomDoubleSpinBox : public QDoubleSpinBox
+  class ctkMatrixDoubleSpinBox : public QDoubleSpinBox
   {
   {
   public:
   public:
-    CustomDoubleSpinBox(QWidget * newParent):QDoubleSpinBox(newParent)
+    ctkMatrixDoubleSpinBox(QWidget * parentWidget)
+      : QDoubleSpinBox(parentWidget)
     {
     {
-      // We know that the parentWidget of newParent will be a ctkMatrixWidget because this object is
+      // We know that the parentWidget() of parentWidget will be a
+      // ctkMatrixWidget because this object is
       // created by the QItemEditorFactory
       // created by the QItemEditorFactory
-      ctkMatrixWidget* matrixWidget = qobject_cast<ctkMatrixWidget*>(newParent->parentWidget());
+      ctkMatrixWidget* matrixWidget =
+        qobject_cast<ctkMatrixWidget*>(parentWidget->parentWidget());
       Q_ASSERT(matrixWidget);
       Q_ASSERT(matrixWidget);
-      
       this->setMinimum(matrixWidget->minimum());
       this->setMinimum(matrixWidget->minimum());
       this->setMaximum(matrixWidget->maximum());
       this->setMaximum(matrixWidget->maximum());
       this->setDecimals(matrixWidget->decimals());
       this->setDecimals(matrixWidget->decimals());
@@ -69,9 +71,10 @@ public:
   void validateElements();
   void validateElements();
 
 
   // Parameters for the spinbox used to change the value of matrix elements
   // Parameters for the spinbox used to change the value of matrix elements
+  /// TODO: use a QDoubleSpinBox directly ?
   double Minimum;
   double Minimum;
   double Maximum;
   double Maximum;
-  int Decimals;
+  int    Decimals;
   double SingleStep;
   double SingleStep;
 };
 };
 
 
@@ -85,8 +88,6 @@ ctkMatrixWidgetPrivate::ctkMatrixWidgetPrivate(ctkMatrixWidget& object)
 void ctkMatrixWidgetPrivate::init()
 void ctkMatrixWidgetPrivate::init()
 {
 {
   Q_Q(ctkMatrixWidget);
   Q_Q(ctkMatrixWidget);
-  // Set Read-only
-  q->setEditable(false);
 
 
   // Set parameters for the spinbox
   // Set parameters for the spinbox
   this->Minimum = -100;
   this->Minimum = -100;
@@ -94,6 +95,9 @@ void ctkMatrixWidgetPrivate::init()
   this->Decimals = 2;
   this->Decimals = 2;
   this->SingleStep = 0.01;
   this->SingleStep = 0.01;
 
 
+  // Don't select the items
+  q->setSelectionMode(QAbstractItemView::NoSelection);
+
   // Hide headers
   // Hide headers
   q->verticalHeader()->hide();
   q->verticalHeader()->hide();
   q->horizontalHeader()->hide();
   q->horizontalHeader()->hide();
@@ -110,7 +114,7 @@ void ctkMatrixWidgetPrivate::init()
 
 
   // Register custom editors
   // Register custom editors
   QItemEditorFactory *editorFactory = new QItemEditorFactory;
   QItemEditorFactory *editorFactory = new QItemEditorFactory;
-  editorFactory->registerEditor(QVariant::Double, new QStandardItemEditorCreator<CustomDoubleSpinBox>);
+  editorFactory->registerEditor(QVariant::Double, new QStandardItemEditorCreator<ctkMatrixDoubleSpinBox>);
 
 
   QStyledItemDelegate* defaultItemDelegate =
   QStyledItemDelegate* defaultItemDelegate =
     qobject_cast<QStyledItemDelegate*>(q->itemDelegate());
     qobject_cast<QStyledItemDelegate*>(q->itemDelegate());
@@ -125,8 +129,11 @@ void ctkMatrixWidgetPrivate::init()
   // The table takes ownership of the prototype.
   // The table takes ownership of the prototype.
   q->setItemPrototype(_item);
   q->setItemPrototype(_item);
 
 
+  // Set Read-only
+  q->setEditable(true);
+
   // Initialize
   // Initialize
-  q->reset();
+  q->identity();
 }
 }
 
 
 // --------------------------------------------------------------------------
 // --------------------------------------------------------------------------
@@ -289,7 +296,7 @@ void ctkMatrixWidget::updateGeometries()
 }
 }
 
 
 // --------------------------------------------------------------------------
 // --------------------------------------------------------------------------
-void ctkMatrixWidget::reset()
+void ctkMatrixWidget::identity()
 {
 {
   Q_D(ctkMatrixWidget);
   Q_D(ctkMatrixWidget);
   // Initialize 4x4 matrix
   // Initialize 4x4 matrix
@@ -333,10 +340,8 @@ void ctkMatrixWidget::setValue(int i, int j, double _value)
   Q_ASSERT( i>=0 && i<this->rowCount() &&
   Q_ASSERT( i>=0 && i<this->rowCount() &&
             j>=0 && j<this->columnCount());
             j>=0 && j<this->columnCount());
 
 
-  if (_value >= d->Minimum && _value <= d->Maximum)
-    {
-    this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
-    }
+  _value = qBound(d->Minimum, _value, d->Maximum);
+  this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
 }
 }
 
 
 // --------------------------------------------------------------------------
 // --------------------------------------------------------------------------
@@ -347,11 +352,8 @@ void ctkMatrixWidget::setVector(const QVector<double> & vector)
     {
     {
     for (int j=0; j < this->columnCount(); j++)
     for (int j=0; j < this->columnCount(); j++)
       {
       {
-      double value = vector.at(i * this->columnCount() + j);
-      if  (value >= d->Minimum && value <= d->Maximum)
-        {
-        this->item(i,j)->setData(Qt::DisplayRole, QVariant(value));
-        }
+      double value = qBound(d->Minimum, vector.at(i * this->columnCount() + j), d->Maximum);
+      this->item(i,j)->setData(Qt::DisplayRole, QVariant(value));
       }
       }
     }
     }
 }
 }

+ 1 - 1
Libs/Widgets/ctkMatrixWidget.h

@@ -114,7 +114,7 @@ public:
 public slots:
 public slots:
   ///
   ///
   /// Reset the matrix to identity
   /// Reset the matrix to identity
-  void reset();
+  void identity();
 
 
 protected:
 protected:
   ///
   ///