瀏覽代碼

ENH: Added ctkMatrixWidget and corresponding test skeleton

Jean-Christophe Fillion-Robin 15 年之前
父節點
當前提交
713de945e5

+ 4 - 1
Libs/Widgets/CMakeLists.txt

@@ -12,12 +12,15 @@ SET(KIT_include_directories
   
 # Source files
 SET(KIT_SRCS
+  ctkMatrixWidget.cpp
+  ctkMatrixWidget.h
   ctkSettings.cpp
   ctkSettings.h
   )
 
 # Headers that should run through moc
 SET(KIT_MOC_SRCS
+  ctkMatrixWidget.h
   ctkSettings.h
   )
 
@@ -50,5 +53,5 @@ ctkMacroBuildQtLib(
 
 # Testing
 IF(BUILD_TESTING)
-  #ADD_SUBDIRECTORY(Testing)
+  ADD_SUBDIRECTORY(Testing)
 ENDIF(BUILD_TESTING)

+ 3 - 0
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -1,6 +1,8 @@
 SET(KIT ${PROJECT_NAME})
 
 CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
+  ctkMatrixWidgetTest1.cxx
+  #EXTRA_INCLUDE TestingMacros.h
   )
 
 SET (TestsToRun ${Tests})
@@ -25,3 +27,4 @@ ENDMACRO( SIMPLE_TEST  )
 # Add Tests
 #
 
+SIMPLE_TEST( ctkMatrixWidgetTest1 )

+ 34 - 0
Libs/Widgets/Testing/Cpp/ctkMatrixWidgetTest1.cxx

@@ -0,0 +1,34 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkMatrixWidget.h"
+
+// QT includes
+#include <QApplication>
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+int ctkMatrixWidgetTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkMatrixWidget ctkObject;
+
+
+  return EXIT_SUCCESS;
+}
+

+ 143 - 0
Libs/Widgets/ctkMatrixWidget.cpp

@@ -0,0 +1,143 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkMatrixWidget.h"
+
+// QT includes
+#include <Qt>
+#include <QHeaderView>
+#include <QVariant>
+#include <QTableWidgetItem>
+#include <QResizeEvent>
+#include <QDebug>
+
+//-----------------------------------------------------------------------------
+class ctkMatrixWidgetPrivate: public ctkPrivate<ctkMatrixWidget>
+{
+};
+
+// --------------------------------------------------------------------------
+ctkMatrixWidget::ctkMatrixWidget(QWidget* _parent) : Superclass(4, 4, _parent)
+{
+  CTK_INIT_PRIVATE(ctkMatrixWidget);
+
+  // Set Read-only
+  this->setEditTriggers(ctkMatrixWidget::NoEditTriggers);
+
+  // Hide headers
+  this->verticalHeader()->hide();
+  this->horizontalHeader()->hide();
+
+  // Disable scrollBars
+  this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+  this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+  // Define prototype item
+  QTableWidgetItem* _item = new QTableWidgetItem();
+  _item->setData(Qt::DisplayRole, QVariant(0.0));
+  _item->setTextAlignment(Qt::AlignCenter);
+
+  // The table takes ownership of the prototype.
+  this->setItemPrototype(_item);
+
+  // Initialize
+  this->reset();
+}
+
+// --------------------------------------------------------------------------
+QSize ctkMatrixWidget::minimumSizeHint () const
+{
+  return QSize(this->columnCount() * 25, this->rowCount() * 25);
+}
+
+// --------------------------------------------------------------------------
+QSize ctkMatrixWidget::sizeHint () const
+{
+  return this->minimumSizeHint();
+}
+
+// --------------------------------------------------------------------------
+void ctkMatrixWidget::resizeEvent(QResizeEvent * _event)
+{
+  this->Superclass::resizeEvent(_event);
+  this->adjustRowsColumnsSize(_event->size().width(), _event->size().height());
+}
+
+// --------------------------------------------------------------------------
+void ctkMatrixWidget::adjustRowsColumnsSize(int _width, int _height)
+{
+  int colwidth = _width / this->columnCount();
+  int lastColwidth = colwidth + (_width - colwidth * this->columnCount());
+  //qDebug() << "width:" << width << ",col-width:" << colwidth;
+  for (int j=0; j < this->columnCount(); j++)
+    {
+    bool lastColumn = (j==(this->columnCount()-1));
+    this->setColumnWidth(j, lastColumn ? lastColwidth : colwidth);
+    }
+
+  int rowheight = _height / this->rowCount();
+  int lastRowheight = rowheight + (_height - rowheight * this->rowCount());
+  //qDebug() << "height:" << height << ", row-height:" << rowheight;
+  for (int i=0; i < this->rowCount(); i++)
+    {
+    bool lastRow = (i==(this->rowCount()-1));
+    this->setRowHeight(i, lastRow ? lastRowheight : rowheight);
+    }
+}
+
+// --------------------------------------------------------------------------
+void ctkMatrixWidget::reset()
+{
+  // Initialize 4x4 matrix
+  for (int i=0; i < this->rowCount(); i++)
+    {
+    for (int j=0; j < this->columnCount(); j++)
+      {
+      this->setItem(i, j, this->itemPrototype()->clone());
+      if (i == j)
+        {
+        this->setValue(i, j, 1);
+        }
+      }
+    }
+}
+
+// --------------------------------------------------------------------------
+double ctkMatrixWidget::value(int i, int j)
+{
+  if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return 0; }
+
+  return this->item(i, j)->data(Qt::DisplayRole).toDouble();
+}
+
+// --------------------------------------------------------------------------
+void ctkMatrixWidget::setValue(int i, int j, double _value)
+{
+  if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return; }
+
+  this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
+}
+
+// --------------------------------------------------------------------------
+void ctkMatrixWidget::setVector(const QVector<double> & vector)
+{
+  for (int i=0; i < this->rowCount(); i++)
+    {
+    for (int j=0; j < this->columnCount(); j++)
+      {
+      this->item(i,j)->setData(Qt::DisplayRole, QVariant(vector.at(i * this->columnCount() + j)));
+      }
+    }
+}

+ 70 - 0
Libs/Widgets/ctkMatrixWidget.h

@@ -0,0 +1,70 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkMatrixWidget_h
+#define __ctkMatrixWidget_h
+
+/// QT includes
+#include <QTableWidget>
+
+/// CTK includes
+#include "ctkPimpl.h"
+#include "CTKWidgetsExport.h"
+
+class ctkMatrixWidgetPrivate;
+
+class CTK_WIDGETS_EXPORT ctkMatrixWidget : public QTableWidget
+{
+  Q_OBJECT
+
+public:
+  /// Superclass typedef
+  typedef QTableWidget Superclass;
+
+  /// Constructors
+  explicit ctkMatrixWidget(QWidget* parent = 0);
+  virtual ~ctkMatrixWidget(){}
+
+  /// 
+  /// Set / Get values
+  double value(int i, int j);
+  void setValue(int i, int j, double value);
+  void setVector(const QVector<double> & vector);
+
+  /// 
+  /// Overloaded - See QWidget
+  virtual QSize minimumSizeHint () const;
+  virtual QSize sizeHint () const;
+
+
+public slots:
+
+  /// 
+  /// Reset to zero
+  void reset();
+
+protected slots:
+  /// 
+  /// Adjust columns/rows size according to width/height
+  void adjustRowsColumnsSize(int width, int height);
+
+protected:
+  /// 
+  virtual void resizeEvent(QResizeEvent * event);
+
+private:
+  CTK_DECLARE_PRIVATE(ctkMatrixWidget);
+};
+
+#endif