ctkMatrixWidget.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // CTK includes
00022 #include "ctkMatrixWidget.h"
00023 
00024 // Qt includes
00025 #include <Qt>
00026 #include <QHeaderView>
00027 #include <QVariant>
00028 #include <QTableWidgetItem>
00029 #include <QResizeEvent>
00030 #include <QDebug>
00031 
00032 //-----------------------------------------------------------------------------
00033 class ctkMatrixWidgetPrivate: public ctkPrivate<ctkMatrixWidget>
00034 {
00035 };
00036 
00037 // --------------------------------------------------------------------------
00038 ctkMatrixWidget::ctkMatrixWidget(QWidget* _parent) : Superclass(4, 4, _parent)
00039 {
00040   CTK_INIT_PRIVATE(ctkMatrixWidget);
00041 
00042   // Set Read-only
00043   this->setEditTriggers(ctkMatrixWidget::NoEditTriggers);
00044 
00045   // Hide headers
00046   this->verticalHeader()->hide();
00047   this->horizontalHeader()->hide();
00048 
00049   // Disable scrollBars
00050   this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00051   this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00052 
00053   // Define prototype item
00054   QTableWidgetItem* _item = new QTableWidgetItem();
00055   _item->setData(Qt::DisplayRole, QVariant(0.0));
00056   _item->setTextAlignment(Qt::AlignCenter);
00057 
00058   // The table takes ownership of the prototype.
00059   this->setItemPrototype(_item);
00060 
00061   // Initialize
00062   this->reset();
00063 }
00064 
00065 // --------------------------------------------------------------------------
00066 QSize ctkMatrixWidget::minimumSizeHint () const
00067 {
00068   return QSize(this->columnCount() * 25, this->rowCount() * 25);
00069 }
00070 
00071 // --------------------------------------------------------------------------
00072 QSize ctkMatrixWidget::sizeHint () const
00073 {
00074   return this->minimumSizeHint();
00075 }
00076 
00077 // --------------------------------------------------------------------------
00078 void ctkMatrixWidget::resizeEvent(QResizeEvent * _event)
00079 {
00080   this->Superclass::resizeEvent(_event);
00081   this->adjustRowsColumnsSize(_event->size().width(), _event->size().height());
00082 }
00083 
00084 // --------------------------------------------------------------------------
00085 void ctkMatrixWidget::adjustRowsColumnsSize(int _width, int _height)
00086 {
00087   int colwidth = _width / this->columnCount();
00088   int lastColwidth = colwidth + (_width - colwidth * this->columnCount());
00089   //qDebug() << "width:" << width << ",col-width:" << colwidth;
00090   for (int j=0; j < this->columnCount(); j++)
00091     {
00092     bool lastColumn = (j==(this->columnCount()-1));
00093     this->setColumnWidth(j, lastColumn ? lastColwidth : colwidth);
00094     }
00095 
00096   int rowheight = _height / this->rowCount();
00097   int lastRowheight = rowheight + (_height - rowheight * this->rowCount());
00098   //qDebug() << "height:" << height << ", row-height:" << rowheight;
00099   for (int i=0; i < this->rowCount(); i++)
00100     {
00101     bool lastRow = (i==(this->rowCount()-1));
00102     this->setRowHeight(i, lastRow ? lastRowheight : rowheight);
00103     }
00104 }
00105 
00106 // --------------------------------------------------------------------------
00107 void ctkMatrixWidget::reset()
00108 {
00109   // Initialize 4x4 matrix
00110   for (int i=0; i < this->rowCount(); i++)
00111     {
00112     for (int j=0; j < this->columnCount(); j++)
00113       {
00114       this->setItem(i, j, this->itemPrototype()->clone());
00115       if (i == j)
00116         {
00117         this->setValue(i, j, 1);
00118         }
00119       }
00120     }
00121 }
00122 
00123 // --------------------------------------------------------------------------
00124 double ctkMatrixWidget::value(int i, int j)
00125 {
00126   if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return 0; }
00127 
00128   return this->item(i, j)->data(Qt::DisplayRole).toDouble();
00129 }
00130 
00131 // --------------------------------------------------------------------------
00132 void ctkMatrixWidget::setValue(int i, int j, double _value)
00133 {
00134   if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return; }
00135 
00136   this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
00137 }
00138 
00139 // --------------------------------------------------------------------------
00140 void ctkMatrixWidget::setVector(const QVector<double> & vector)
00141 {
00142   for (int i=0; i < this->rowCount(); i++)
00143     {
00144     for (int j=0; j < this->columnCount(); j++)
00145       {
00146       this->item(i,j)->setData(Qt::DisplayRole, QVariant(vector.at(i * this->columnCount() + j)));
00147       }
00148     }
00149 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines