Optika GUI Toolik Version of the Day
Optika_delegate.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //         Optika: A Tool For Developing Parameter Obtaining GUIs
00005 //                Copyright (2009) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, with Sandia Corporation, the 
00008 // U.S. Government retains certain rights in this software.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 #include <QtGui>
00029 #include <QSpinBox>
00030 #include <QDoubleSpinBox>
00031 #include <QLineEdit>
00032 #include <QComboBox>
00033 #include <QFileDialog>
00034 #include "Optika_delegate.hpp"
00035 #include "float.h"
00036 #include <iostream>
00037 
00038 namespace Optika{
00039 
00040 Delegate::Delegate(QObject *parent):QItemDelegate(parent){}
00041 
00042 QWidget* Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/ , const QModelIndex &index ) const{
00043   QWidget *editor = 0;
00044   if(index.column() != 1)
00045     return editor;
00046 
00047   Teuchos::RCP<const Teuchos::ParameterEntryValidator> paramValidator = ((TreeModel*)(index.model()))->getValidator(index);
00048   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00049 
00050   if(itemType == intId){
00051     editor = new QSpinBox(parent);
00052     Teuchos::RCP<const EnhancedNumberValidator <int> > intValidator;
00053     if(!Teuchos::is_null(paramValidator)){
00054       intValidator = Teuchos::rcp_dynamic_cast<const EnhancedNumberValidator<int> >(paramValidator);
00055     }
00056     EnhancedNumberValidator<int>::applyToSpinBox(intValidator, (QSpinBox*)editor);
00057   }
00058   else if(itemType == shortId){
00059     editor = new QSpinBox(parent);
00060     Teuchos::RCP<const EnhancedNumberValidator<short> > shortValidator;
00061     if(!Teuchos::is_null(paramValidator)){
00062       shortValidator = Teuchos::rcp_dynamic_cast<const EnhancedNumberValidator<short> >(paramValidator);
00063     }
00064     EnhancedNumberValidator<short>::applyToSpinBox(shortValidator, (QSpinBox*)editor);
00065   }
00066 /*  else if(itemType == longlongId){
00067     editor = new QwwLongSpinBox(parent);
00068     Teuchos::RCP<const EnhancedNumberValidator<long long> > longlongValidator;
00069     if(!Teuchos::is_null(paramValidator)){
00070       longlongValidator = Teuchos::rcp_dynamic_cast<const EnhancedNumberValidator<long long> >(paramValidator);
00071     }
00072     EnhancedNumberValidator<long long>::applyToSpinBox(longlongValidator, (QDoubleSpinBox*)editor);
00073   }*/
00074   else if(itemType == doubleId){
00075     editor = new QDoubleSpinBox(parent);
00076     Teuchos::RCP<const EnhancedNumberValidator<double> > doubleValidator;
00077     if(!Teuchos::is_null(paramValidator)){
00078       doubleValidator = Teuchos::rcp_dynamic_cast<const EnhancedNumberValidator<double> >(paramValidator);
00079     }
00080     EnhancedNumberValidator<double>::applyToSpinBox(doubleValidator, (QDoubleSpinBox*)editor);
00081   }
00082   else if(itemType == floatId){
00083     editor = new QDoubleSpinBox(parent);
00084     Teuchos::RCP<const EnhancedNumberValidator<float> > floatValidator; 
00085     if(!Teuchos::is_null(paramValidator)){
00086       floatValidator = Teuchos::rcp_dynamic_cast<const EnhancedNumberValidator<float> >(paramValidator);
00087     }
00088     EnhancedNumberValidator<float>::applyToSpinBox(floatValidator, (QDoubleSpinBox*)editor);
00089   }
00090   else if(itemType == boolId){
00091     editor = new QComboBox(parent);
00092     static_cast<QComboBox*>(editor)->addItem("true");
00093     static_cast<QComboBox*>(editor)->addItem("false");
00094   }
00095   else if(itemType == stringId){
00096     if(Teuchos::is_null(paramValidator)){
00097       editor = new QLineEdit(parent);
00098     }
00099     else if(!Teuchos::is_null(Teuchos::rcp_dynamic_cast<const FileNameValidator>(paramValidator))){
00100       QString paramName = 
00101         ((TreeModel*)(index.model()))->data(index.sibling(index.row(), 0),Qt::DisplayRole).toString();
00102       QString currentPath = ((TreeModel*)(index.model()))->data(index,Qt::DisplayRole).toString();
00103       if(currentPath.size() == 0){
00104         currentPath = QDir::homePath();
00105       }
00106       QString filename;
00107       if(Teuchos::rcp_dynamic_cast<const FileNameValidator>(paramValidator)->fileMustExist()){
00108         filename = QFileDialog::getOpenFileName(parent, paramName, currentPath);
00109       }
00110       else{
00111         filename = QFileDialog::getSaveFileName(parent, paramName, currentPath);
00112       }
00113       if(filename != ""){
00114         ((TreeModel*)(index.model()))->setData(index, filename);
00115       }
00116     }
00117     else if(paramValidator->validStringValues()->size() != 0){
00118       Teuchos::RCP<const Teuchos::Array<std::string> > options = paramValidator->validStringValues();
00119       editor = new QComboBox(parent);
00120       for(Teuchos::Array<std::string>::const_iterator itr = options->begin(); itr != options->end(); ++itr){
00121         static_cast<QComboBox*>(editor)->addItem(QString::fromStdString(*itr));
00122       }
00123     }
00124     else{
00125       editor = new QLineEdit(parent);
00126     }
00127   }
00128   else if(itemType.contains(arrayId)){
00129     arrayHandler(index, itemType.section(" ", -1), parent);
00130   }
00131 
00132   return editor;
00133 }
00134 
00135 void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
00136   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00137   if(itemType == intId){
00138     int value = index.model()->data(index).toInt();
00139     static_cast<QSpinBox*>(editor)->setValue(value);
00140   }
00141   else if(itemType == shortId){
00142     short value = index.model()->data(index).toInt();
00143     static_cast<QSpinBox*>(editor)->setValue(value);
00144   }
00145   else if(itemType == doubleId){
00146     double value = index.model()->data(index).toDouble();
00147     static_cast<QDoubleSpinBox*>(editor)->setValue(value);
00148   }
00149   else if(itemType == floatId){
00150     float value = index.model()->data(index).toDouble();
00151     static_cast<QDoubleSpinBox*>(editor)->setValue(value);
00152   }
00153   else if(itemType == boolId){
00154     QString value = index.model()->data(index).toString();
00155     static_cast<QComboBox*>(editor)->setEditText(value);
00156   }
00157   else if(itemType == stringId){
00158     QString value = index.model()->data(index).toString();
00159     Teuchos::RCP<const Teuchos::ParameterEntryValidator> validator = ((TreeModel*)(index.model()))->getValidator(index);
00160     if(Teuchos::is_null(validator) || validator->validStringValues()->size()==0)
00161       static_cast<QLineEdit*>(editor)->setText(value);
00162     else
00163       static_cast<QComboBox*>(editor)->setEditText(value);
00164   }
00165 }
00166 
00167 void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
00168   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00169   if(itemType == intId){
00170     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
00171     spinBox->interpretText();
00172     model->setData(index, spinBox->value(), Qt::EditRole);
00173   }
00174   else if(itemType == shortId){
00175     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
00176     spinBox->interpretText();
00177     model->setData(index, (short)spinBox->value(), Qt::EditRole);
00178   }
00179   else if(itemType == doubleId){
00180     QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
00181     spinBox->interpretText();
00182     model->setData(index, spinBox->value(), Qt::EditRole);
00183   }
00184   else if(itemType == floatId){
00185     QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
00186     spinBox->interpretText();
00187     model->setData(index, (float)spinBox->value(), Qt::EditRole);
00188   }
00189   else if(itemType == boolId){
00190     bool value = static_cast<QComboBox*>(editor)->currentText() == "true"; 
00191     model->setData(index, value, Qt::EditRole);
00192   }
00193   else if(itemType == stringId){
00194     Teuchos::RCP<const Teuchos::ParameterEntryValidator> validator = 
00195       static_cast<const TreeModel*>(index.model())->getValidator(index);
00196     QString value;
00197     if(Teuchos::is_null(validator)){
00198       value = static_cast<QLineEdit*>(editor)->text();
00199     }
00200     else{
00201       value = static_cast<QComboBox*>(editor)->currentText(); 
00202     }
00203     model->setData(index, value, Qt::EditRole);
00204   }
00205 }
00206 
00207 void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const{
00208   editor->setGeometry(option.rect);
00209 }
00210 
00211 void Delegate::arrayHandler(const QModelIndex& index, QString type, QWidget *parent) const{
00212   if(type == intId){
00213     IntArrayWidget array(index, type, parent);
00214     array.exec();
00215   }
00216   else if(type == shortId){
00217     ShortArrayWidget array(index, type, parent);
00218     array.exec();
00219   }
00220   /*else if(type == longlongId){
00221     LongLongArrayWidget array(index, type, parent);
00222     array.exec();
00223   }*/
00224   else if(type == doubleId){
00225     DoubleArrayWidget array(index, type, parent);
00226     array.exec();
00227   }
00228   else if(type == floatId){
00229     FloatArrayWidget array(index, type, parent);
00230     array.exec();
00231   }
00232   else if(type == stringId){
00233     StringArrayWidget array(index, type, parent);
00234     array.exec();
00235   }
00236 }
00237 
00238 }
00239 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends