ctkCoordinatesWidget.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 // Qt includes
00022 #include <QHBoxLayout>
00023 #include <QDoubleSpinBox>
00024 
00025 // CTK includes
00026 #include "ctkCoordinatesWidget.h"
00027 
00028 //------------------------------------------------------------------------------
00029 ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
00030 {
00031   this->Minimum = -100000.;
00032   this->Maximum = 100000.;
00033   this->SingleStep = 1.;
00034   this->Dimension = 3;
00035   this->Coordinates = new double [this->Dimension];
00036   
00037   QHBoxLayout* hboxLayout = new QHBoxLayout(this);
00038   this->setLayout(hboxLayout);
00039   for (int i = 0; i < this->Dimension; ++i)
00040     {
00041     this->Coordinates[i] = 0.;
00042     this->addSpinBox();
00043     }
00044   hboxLayout->setContentsMargins(0, 0, 0, 0);
00045 }
00046 
00047 //------------------------------------------------------------------------------
00048 ctkCoordinatesWidget::~ctkCoordinatesWidget()
00049 {
00050   delete [] this->Coordinates;
00051 }
00052 
00053 //------------------------------------------------------------------------------
00054 void ctkCoordinatesWidget::addSpinBox()
00055 {
00056   QDoubleSpinBox* spinBox = new QDoubleSpinBox(this);
00057   spinBox->setMinimum(this->Minimum);
00058   spinBox->setMaximum(this->Maximum);
00059   spinBox->setSingleStep(this->SingleStep);
00060   connect( spinBox, SIGNAL(valueChanged(double)), 
00061            this, SLOT(coordinateChanged(double)));
00062   this->layout()->addWidget(spinBox);
00063 }
00064 
00065 //------------------------------------------------------------------------------
00066 void ctkCoordinatesWidget::setDimension(int dim)
00067 {
00068   if (dim < 1)
00069     {
00070     return;
00071     }
00072   double* newPos = new double[dim];
00073   if (dim > this->Dimension)
00074     {
00075     memcpy(newPos, this->Coordinates, this->Dimension * sizeof(double));
00076     for (int i = this->Dimension; i < dim; ++i)
00077       {
00078       newPos[i] = 0.;
00079       this->addSpinBox();
00080       }
00081     }
00082   else
00083     {
00084     memcpy(newPos, this->Coordinates, dim * sizeof(double));
00085     for (int i = this->Dimension - 1 ; i >= dim; --i)
00086       {
00087       QLayoutItem* item = this->layout()->takeAt(i);
00088       QWidget* widget = item ? item->widget() : 0;
00089       delete item;
00090       delete widget;
00091       }
00092     }
00093   delete [] this->Coordinates;
00094   this->Coordinates = newPos;
00095   this->Dimension = dim;
00096 
00097   this->updateGeometry();
00098   
00099   this->coordinatesChanged();
00100 }
00101 
00102 //------------------------------------------------------------------------------
00103 int ctkCoordinatesWidget::dimension() const
00104 {
00105   return this->Dimension;
00106 }
00107 
00108 //------------------------------------------------------------------------------
00109 void ctkCoordinatesWidget::setMinimum(double min)
00110 {
00111   for (int i = 0; this->layout()->itemAt(i); ++i)
00112     {
00113     QLayoutItem* item = this->layout()->itemAt(i);
00114     QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
00115       item->widget()) : 0;
00116     if (spinBox)
00117       {
00118       spinBox->setMinimum(min);
00119       }
00120     }
00121   this->Minimum = min;
00122 }
00123 
00124 //------------------------------------------------------------------------------
00125 double ctkCoordinatesWidget::minimum() const
00126 {
00127   return this->Minimum;
00128 }
00129 
00130 //------------------------------------------------------------------------------
00131 void ctkCoordinatesWidget::setMaximum(double max)
00132 {
00133   for (int i = 0; this->layout()->itemAt(i); ++i)
00134     {
00135     QLayoutItem* item = this->layout()->itemAt(i);
00136     QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
00137       item->widget()) : 0;
00138     if (spinBox)
00139       {
00140       spinBox->setMaximum(max);
00141       }
00142     }
00143   this->Maximum = max;
00144 }
00145 
00146 //------------------------------------------------------------------------------
00147 double ctkCoordinatesWidget::maximum() const
00148 {
00149   return this->Maximum;
00150 }
00151 
00152 //------------------------------------------------------------------------------
00153 void ctkCoordinatesWidget::setSingleStep(double step)
00154 {
00155   for (int i = 0; this->layout()->itemAt(i); ++i)
00156     {
00157     QLayoutItem* item = this->layout()->itemAt(i);
00158     QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
00159       item->widget()) : 0;
00160     if (spinBox)
00161       {
00162       spinBox->setSingleStep(step);
00163       }
00164     }
00165   this->SingleStep = step;
00166 }
00167 
00168 //------------------------------------------------------------------------------
00169 double ctkCoordinatesWidget::singleStep() const
00170 {
00171   return this->SingleStep;
00172 }
00173 
00174 //------------------------------------------------------------------------------
00175 void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
00176 {
00177   QStringList posList = _pos.split(',');
00178   if (posList.count() != this->Dimension)
00179     {
00180     return;
00181     }
00182   double* newPos = new double[this->Dimension];
00183   for (int i = 0; i < this->Dimension; ++i)
00184     {
00185     newPos[i] = posList[i].toDouble();
00186     }
00187   this->setCoordinates(newPos);
00188   delete [] newPos;
00189 }
00190 
00191 //------------------------------------------------------------------------------
00192 QString ctkCoordinatesWidget::coordinatesAsString()const
00193 {
00194   QString res;
00195   for (int i = 0; i < this->Dimension; ++i)
00196     {
00197     if (i != 0)
00198       {
00199       res += ",";
00200       }
00201     res += QString::number(this->Coordinates[i]);
00202     }
00203   return res;
00204 }
00205 
00206 //------------------------------------------------------------------------------
00207 void ctkCoordinatesWidget::setCoordinates(double* _pos)
00208 {
00209   for (int i = 0; i < this->Dimension; ++i)
00210     {
00211     this->Coordinates[i] = _pos[i];
00212     }
00213   bool blocked = this->blockSignals(true);
00214   for (int i = 0; i < this->Dimension; ++i)
00215     {
00216     QLayoutItem* item = this->layout()->itemAt(i);
00217     QDoubleSpinBox* spinBox = 
00218       item ? dynamic_cast<QDoubleSpinBox*>(item->widget()) : 0;
00219     if (spinBox)
00220       {
00221       spinBox->setValue(this->Coordinates[i]);
00222       }
00223     }
00224   this->blockSignals(blocked);
00225   emit valueChanged(this->Coordinates);
00226 }
00227 
00228 //------------------------------------------------------------------------------
00229 double* ctkCoordinatesWidget::coordinates()const
00230 {
00231   return this->Coordinates;
00232 }
00233 
00234 //------------------------------------------------------------------------------
00235 void ctkCoordinatesWidget::coordinateChanged(double coordinate)
00236 {
00237   for (int i = 0; i < this->Dimension; ++i)
00238     {
00239     QLayoutItem* item = this->layout()->itemAt(i);
00240     QObject* spinBox = 
00241       item ? dynamic_cast<QObject*>(item->widget()) : 0;
00242     if ( spinBox && spinBox == this->sender())
00243       {
00244       this->Coordinates[i] = coordinate;
00245       }
00246     }
00247   emit valueChanged(this->Coordinates);
00248 }
00249 
00250 //------------------------------------------------------------------------------
00251 void ctkCoordinatesWidget::coordinatesChanged()
00252 {
00253   for (int i = 0; i < this->Dimension; ++i)
00254     {
00255     QLayoutItem* item = this->layout()->itemAt(i);
00256     QSpinBox* spinBox = 
00257       item ? dynamic_cast<QSpinBox*>(item->widget()) : 0;
00258     if ( spinBox)
00259       {
00260       this->Coordinates[i] = spinBox->value();
00261       }
00262     }
00263   emit valueChanged(this->Coordinates);
00264 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1