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 "Teuchos_StandardParameterEntryValidators.hpp"
00036 
00037 namespace Optika{
00038 
00039 Delegate::Delegate(QObject *parent):QItemDelegate(parent){}
00040 
00041 QWidget* Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/ , const QModelIndex &index ) const{
00042   QWidget *editor = 0;
00043   if(index.column() != 1)
00044     return editor;
00045 
00046   RCP<const ParameterEntryValidator> paramValidator = ((TreeModel*)(index.model()))->getValidator(index);
00047   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00048 
00049   if(itemType == intId){
00050     editor = new QSpinBox(parent);
00051     RCP<const EnhancedNumberValidator <int> > intValidator;
00052     if(!is_null(paramValidator)){
00053       intValidator = rcp_dynamic_cast<const EnhancedNumberValidator<int> >(paramValidator);
00054     }
00055     ValidatorApplier<int>::applyToSpinBox(intValidator, (QSpinBox*)editor);
00056   }
00057   else if(itemType == shortId){
00058     editor = new QSpinBox(parent);
00059     RCP<const EnhancedNumberValidator<short> > shortValidator;
00060     if(!is_null(paramValidator)){
00061       shortValidator = rcp_dynamic_cast<const EnhancedNumberValidator<short> >(paramValidator);
00062     }
00063     ValidatorApplier<short>::applyToSpinBox(shortValidator, (QSpinBox*)editor);
00064   }
00065 /*  else if(itemType == longlongId){
00066     editor = new QwwLongSpinBox(parent);
00067     RCP<const EnhancedNumberValidator<long long> > longlongValidator;
00068     if(!is_null(paramValidator)){
00069       longlongValidator = rcp_dynamic_cast<const EnhancedNumberValidator<long long> >(paramValidator);
00070     }
00071     EnhancedNumberValidator<long long>::applyToSpinBox(longlongValidator, (QDoubleSpinBox*)editor);
00072   }*/
00073   else if(itemType == doubleId){
00074     //editor = new QLineEdit(parent);
00075     editor = new QLineEdit(parent);
00076     RCP<const EnhancedNumberValidator<double> > doubleValidator;
00077     if(!is_null(paramValidator)){
00078       doubleValidator = rcp_dynamic_cast<const EnhancedNumberValidator<double> >(paramValidator);
00079     }
00080     ValidatorApplier<double>::applyToLineEdit(doubleValidator, (QLineEdit*)editor);
00081   }
00082   else if(itemType == floatId){
00083     editor = new QLineEdit(parent);
00084     RCP<const EnhancedNumberValidator<float> > floatValidator; 
00085     if(!is_null(paramValidator)){
00086       floatValidator = rcp_dynamic_cast<const EnhancedNumberValidator<float> >(paramValidator);
00087     }
00088     ValidatorApplier<float>::applyToLineEdit(floatValidator, (QLineEdit*)editor);
00089   }
00090   else if(itemType == boolId){
00091     editor = new QComboBox(parent);
00092     static_cast<QComboBox*>(editor)->addItem(getBoolEditorTrue());
00093     static_cast<QComboBox*>(editor)->addItem(getBoolEditorFalse());
00094   }
00095   else if(itemType == stringId){
00096     if(is_null(paramValidator)){
00097       editor = new QLineEdit(parent);
00098     }
00099     else if(!is_null(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(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       RCP<const Array<std::string> > options = paramValidator->validStringValues();
00119       editor = new QComboBox(parent);
00120       for(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     editor = getArrayEditor(index, getArrayType(itemType), parent);
00130   }
00131   else if(itemType.contains(twoDArrayId)){
00132     editor = getArrayEditor(index, getArrayType(itemType), parent, true);
00133   }
00134 
00135   return editor;
00136 }
00137 
00138 void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
00139   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00140   QVariant value = index.model()->data(index);
00141   if(itemType == intId){
00142     static_cast<QSpinBox*>(editor)->setValue(value.toInt());
00143   }
00144   else if(itemType == shortId){
00145     static_cast<QSpinBox*>(editor)->setValue(value.toInt());
00146   }
00147   else if(itemType == doubleId){
00148     static_cast<QLineEdit*>(editor)->setText(value.toString());
00149   }
00150   else if(itemType == floatId){
00151     static_cast<QLineEdit*>(editor)->setText(value.toString());
00152   }
00153   else if(itemType == boolId){
00154     static_cast<QComboBox*>(editor)->setEditText(value.toString());
00155   }
00156   else if(itemType == stringId){
00157     RCP<const ParameterEntryValidator> validator = ((TreeModel*)(index.model()))->getValidator(index);
00158     if(is_null(validator) || validator->validStringValues()->size()==0)
00159       static_cast<QLineEdit*>(editor)->setText(value.toString());
00160     else
00161       static_cast<QComboBox*>(editor)->setEditText(value.toString());
00162   }
00163   else if(itemType.contains(arrayId)){
00164     setArrayWidgetData(editor, getArrayType(itemType), index);
00165   }
00166   else if(itemType.contains(twoDArrayId)){
00167     setArrayWidgetData(editor, getArrayType(itemType), index, true);
00168   }
00169 }
00170 
00171 void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
00172   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00173   if(itemType == intId){
00174     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
00175     spinBox->interpretText();
00176     model->setData(index, spinBox->value(), Qt::EditRole);
00177   }
00178   else if(itemType == shortId){
00179     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
00180     spinBox->interpretText();
00181     model->setData(index, (short)spinBox->value(), Qt::EditRole);
00182   }
00183   else if(itemType == doubleId){
00184     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
00185     model->setData(index, lineEdit->text(), Qt::EditRole);
00186   }
00187   else if(itemType == floatId){
00188     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
00189     model->setData(index, lineEdit->text(), Qt::EditRole);
00190   }
00191   else if(itemType == boolId){
00192     bool value = static_cast<QComboBox*>(editor)->currentText() 
00193       == getBoolEditorTrue(); 
00194     model->setData(index, value, Qt::EditRole);
00195   }
00196   else if(itemType == stringId){
00197     RCP<const ParameterEntryValidator> validator = 
00198       static_cast<const TreeModel*>(index.model())->getValidator(index);
00199     QString value;
00200     if(is_null(validator)){
00201       value = static_cast<QLineEdit*>(editor)->text();
00202     }
00203     else{
00204       value = static_cast<QComboBox*>(editor)->currentText(); 
00205     }
00206     model->setData(index, value, Qt::EditRole);
00207   }
00208   else if(itemType.contains(arrayId)){
00209     QVariant value = extractValueFromArray(editor, getArrayType(itemType));
00210     model->setData(index, value, Qt::EditRole);
00211   }
00212   else if(itemType.contains(twoDArrayId)){
00213     QVariant value = extractValueFromArray(editor, getArrayType(itemType), true);
00214     model->setData(index, value, Qt::EditRole);
00215   }
00216 }
00217 
00218  
00219 
00220 void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const{
00221   editor->setGeometry(option.rect);
00222 }
00223 
00224 QWidget* Delegate::getArrayEditor(const QModelIndex& index, QString type, QWidget *parent, bool isTwoD) const{
00225   TreeModel* model = (TreeModel*)index.model();
00226   QString name = model->data(
00227     index.sibling(index.row(),0),Qt::DisplayRole).toString();
00228   RCP<const ParameterEntryValidator> validator = 
00229     model->getValidator(index);
00230   if(type == intId){
00231     if(isTwoD){
00232       return new Int2DArrayWidget(name, type, validator, parent);
00233     }
00234     else{
00235       return new IntArrayWidget(name, type, validator, parent);
00236     }
00237   }
00238   else if(type == shortId){
00239     if(isTwoD){
00240       return new Short2DArrayWidget(name, type, validator, parent);
00241     }
00242     else{
00243       return new ShortArrayWidget(name, type, validator, parent);
00244     }
00245   }
00246   else if(type == doubleId){
00247     if(isTwoD){
00248       return new Double2DArrayWidget(name, type, validator, parent);
00249     }
00250     else{
00251       return new DoubleArrayWidget(name, type, validator, parent);
00252     }
00253   }
00254   else if(type == floatId){
00255     if(isTwoD){
00256       return new Float2DArrayWidget(name, type, validator, parent);
00257     }
00258     else{
00259       return new FloatArrayWidget(name, type, validator, parent);
00260     }
00261   }
00262   else if(type == stringId){
00263     if(isTwoD){
00264       return new String2DArrayWidget(name, type, validator, parent);
00265     }
00266     else{
00267       return new StringArrayWidget(name, type, validator, parent);
00268     }
00269   }
00270   else{
00271     return 0;
00272   }
00273 }
00274 
00275 void Delegate::setArrayWidgetData(QWidget* editor, QString type, const QModelIndex& index, bool isTwoD) const{
00276   QVariant newData = index.model()->data(index, TreeModel::getRawDataRole());
00277   if(type == intId){
00278     isTwoD ?
00279     ((Int2DArrayWidget*)editor)->initData(newData.value<TwoDArray<int> >())
00280     :
00281     ((IntArrayWidget*)editor)->initData(newData.value<Array<int> >());
00282   }
00283   else if(type == shortId){
00284     isTwoD ?
00285     ((Short2DArrayWidget*)editor)->initData(newData.value<TwoDArray<short> >())
00286     :
00287     ((ShortArrayWidget*)editor)->initData(newData.value<Array<short> >());
00288   }
00289   else if(type == doubleId){
00290     isTwoD ?
00291     ((Double2DArrayWidget*)editor)->initData(newData.value<TwoDArray<double> >())
00292     :
00293     ((DoubleArrayWidget*)editor)->initData(newData.value<Array<double> >());
00294   }
00295   else if(type == floatId){
00296     isTwoD ?
00297     ((Float2DArrayWidget*)editor)->initData(newData.value<TwoDArray<float> >())
00298     :
00299     ((FloatArrayWidget*)editor)->initData(newData.value<Array<float> >());
00300   }
00301   else if(type == stringId){
00302     isTwoD ?
00303     ((String2DArrayWidget*)editor)->initData(newData.value<TwoDArray<std::string> >())
00304     :
00305     ((StringArrayWidget*)editor)->initData(newData.value<Array<std::string> >());
00306   }
00307 }
00308 
00309 QVariant Delegate::extractValueFromArray(QWidget* editor, QString type, bool isTwoD) const
00310 {
00311   if(type == intId){
00312     return (isTwoD ?
00313     QVariant::fromValue(((Int2DArrayWidget*)editor)->getData())
00314     :
00315     QVariant::fromValue(((IntArrayWidget*)editor)->getData()));
00316   }
00317   else if(type == shortId){
00318     return (isTwoD ?
00319     QVariant::fromValue(((Short2DArrayWidget*)editor)->getData())
00320     :
00321     QVariant::fromValue(((ShortArrayWidget*)editor)->getData()));
00322   }
00323   else if(type == doubleId){
00324     return (isTwoD ?
00325     QVariant::fromValue(((Double2DArrayWidget*)editor)->getData())
00326     :
00327     QVariant::fromValue(((DoubleArrayWidget*)editor)->getData()));
00328   }
00329   else if(type == floatId){
00330     return (isTwoD ?
00331     QVariant::fromValue(((Float2DArrayWidget*)editor)->getData())
00332     :
00333     QVariant::fromValue(((FloatArrayWidget*)editor)->getData()));
00334   }
00335   else if(type == stringId){
00336     return (isTwoD ?
00337     QVariant::fromValue(((String2DArrayWidget*)editor)->getData())
00338     :
00339     QVariant::fromValue(((StringArrayWidget*)editor)->getData()));
00340   }
00341   else{
00342     return QVariant();
00343   }
00344 }
00345 
00346 } //End namespace
00347 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines