| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | /*=========================================================================  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)));      }    }}
 |