浏览代码

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 年之前
父节点
当前提交
5bb212407e
共有 3 个文件被更改,包括 33 次插入26 次删除
  1. 10 5
      Libs/Widgets/Testing/Cpp/ctkMatrixWidgetTest1.cpp
  2. 22 20
      Libs/Widgets/ctkMatrixWidget.cpp
  3. 1 1
      Libs/Widgets/ctkMatrixWidget.h

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

@@ -1,7 +1,7 @@
 /*=========================================================================
 
   Library:   CTK
- 
+
   Copyright (c) Kitware Inc.
 
   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.
   See the License for the specific language governing permissions and
   limitations under the License.
- 
+
 =========================================================================*/
 
 // Qt includes
 #include <QApplication>
+#include <QTimer>
 
 // CTK includes
 #include "ctkMatrixWidget.h"
 
 // STD includes
-#include <cstdlib>
 #include <iostream>
 
 //-----------------------------------------------------------------------------
@@ -33,9 +33,14 @@ int ctkMatrixWidgetTest1(int argc, char * 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
 {
 //-----------------------------------------------------------------------------
-  class CustomDoubleSpinBox : public QDoubleSpinBox
+  class ctkMatrixDoubleSpinBox : public QDoubleSpinBox
   {
   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
-      ctkMatrixWidget* matrixWidget = qobject_cast<ctkMatrixWidget*>(newParent->parentWidget());
+      ctkMatrixWidget* matrixWidget =
+        qobject_cast<ctkMatrixWidget*>(parentWidget->parentWidget());
       Q_ASSERT(matrixWidget);
-      
       this->setMinimum(matrixWidget->minimum());
       this->setMaximum(matrixWidget->maximum());
       this->setDecimals(matrixWidget->decimals());
@@ -69,9 +71,10 @@ public:
   void validateElements();
 
   // Parameters for the spinbox used to change the value of matrix elements
+  /// TODO: use a QDoubleSpinBox directly ?
   double Minimum;
   double Maximum;
-  int Decimals;
+  int    Decimals;
   double SingleStep;
 };
 
@@ -85,8 +88,6 @@ ctkMatrixWidgetPrivate::ctkMatrixWidgetPrivate(ctkMatrixWidget& object)
 void ctkMatrixWidgetPrivate::init()
 {
   Q_Q(ctkMatrixWidget);
-  // Set Read-only
-  q->setEditable(false);
 
   // Set parameters for the spinbox
   this->Minimum = -100;
@@ -94,6 +95,9 @@ void ctkMatrixWidgetPrivate::init()
   this->Decimals = 2;
   this->SingleStep = 0.01;
 
+  // Don't select the items
+  q->setSelectionMode(QAbstractItemView::NoSelection);
+
   // Hide headers
   q->verticalHeader()->hide();
   q->horizontalHeader()->hide();
@@ -110,7 +114,7 @@ void ctkMatrixWidgetPrivate::init()
 
   // Register custom editors
   QItemEditorFactory *editorFactory = new QItemEditorFactory;
-  editorFactory->registerEditor(QVariant::Double, new QStandardItemEditorCreator<CustomDoubleSpinBox>);
+  editorFactory->registerEditor(QVariant::Double, new QStandardItemEditorCreator<ctkMatrixDoubleSpinBox>);
 
   QStyledItemDelegate* defaultItemDelegate =
     qobject_cast<QStyledItemDelegate*>(q->itemDelegate());
@@ -125,8 +129,11 @@ void ctkMatrixWidgetPrivate::init()
   // The table takes ownership of the prototype.
   q->setItemPrototype(_item);
 
+  // Set Read-only
+  q->setEditable(true);
+
   // Initialize
-  q->reset();
+  q->identity();
 }
 
 // --------------------------------------------------------------------------
@@ -289,7 +296,7 @@ void ctkMatrixWidget::updateGeometries()
 }
 
 // --------------------------------------------------------------------------
-void ctkMatrixWidget::reset()
+void ctkMatrixWidget::identity()
 {
   Q_D(ctkMatrixWidget);
   // Initialize 4x4 matrix
@@ -333,10 +340,8 @@ void ctkMatrixWidget::setValue(int i, int j, double _value)
   Q_ASSERT( i>=0 && i<this->rowCount() &&
             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++)
       {
-      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:
   ///
   /// Reset the matrix to identity
-  void reset();
+  void identity();
 
 protected:
   ///