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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 #include <QtGui>
00042 #include <QSpinBox>
00043 #include <QDoubleSpinBox>
00044 #include <QLineEdit>
00045 #include <QComboBox>
00046 #include <QFileDialog>
00047 #include "Optika_delegate.hpp"
00048 #include "Teuchos_StandardParameterEntryValidators.hpp"
00049 
00050 namespace Optika{
00051 
00052 Delegate::Delegate(QObject *parent):QItemDelegate(parent){}
00053 
00054 QWidget* Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/ , const QModelIndex &index ) const{
00055   QWidget *editor = 0;
00056   if(index.column() != 1)
00057     return editor;
00058 
00059   RCP<const ParameterEntryValidator> paramValidator = ((TreeModel*)(index.model()))->getValidator(index);
00060   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00061 
00062   if(itemType == intId){
00063     editor = new QSpinBox(parent);
00064     RCP<const EnhancedNumberValidator <int> > intValidator;
00065     if(!is_null(paramValidator)){
00066       intValidator = rcp_dynamic_cast<const EnhancedNumberValidator<int> >(paramValidator);
00067     }
00068     ValidatorApplier<int>::applyToSpinBox(intValidator, (QSpinBox*)editor);
00069   }
00070   else if(itemType == shortId){
00071     editor = new QSpinBox(parent);
00072     RCP<const EnhancedNumberValidator<short> > shortValidator;
00073     if(!is_null(paramValidator)){
00074       shortValidator = rcp_dynamic_cast<const EnhancedNumberValidator<short> >(paramValidator);
00075     }
00076     ValidatorApplier<short>::applyToSpinBox(shortValidator, (QSpinBox*)editor);
00077   }
00078 /*  else if(itemType == longlongId){
00079     editor = new QwwLongSpinBox(parent);
00080     RCP<const EnhancedNumberValidator<long long> > longlongValidator;
00081     if(!is_null(paramValidator)){
00082       longlongValidator = rcp_dynamic_cast<const EnhancedNumberValidator<long long> >(paramValidator);
00083     }
00084     EnhancedNumberValidator<long long>::applyToSpinBox(longlongValidator, (QDoubleSpinBox*)editor);
00085   }*/
00086   else if(itemType == doubleId){
00087     //editor = new QLineEdit(parent);
00088     editor = new QLineEdit(parent);
00089     RCP<const EnhancedNumberValidator<double> > doubleValidator;
00090     if(!is_null(paramValidator)){
00091       doubleValidator = rcp_dynamic_cast<const EnhancedNumberValidator<double> >(paramValidator);
00092     }
00093     ValidatorApplier<double>::applyToLineEdit(doubleValidator, (QLineEdit*)editor);
00094   }
00095   else if(itemType == floatId){
00096     editor = new QLineEdit(parent);
00097     RCP<const EnhancedNumberValidator<float> > floatValidator; 
00098     if(!is_null(paramValidator)){
00099       floatValidator = rcp_dynamic_cast<const EnhancedNumberValidator<float> >(paramValidator);
00100     }
00101     ValidatorApplier<float>::applyToLineEdit(floatValidator, (QLineEdit*)editor);
00102   }
00103   else if(itemType == boolId){
00104     editor = new QComboBox(parent);
00105     static_cast<QComboBox*>(editor)->addItem(getBoolEditorTrue());
00106     static_cast<QComboBox*>(editor)->addItem(getBoolEditorFalse());
00107   }
00108   else if(itemType == stringId){
00109     if(is_null(paramValidator)){
00110       editor = new QLineEdit(parent);
00111     }
00112     else if(!is_null(rcp_dynamic_cast<const FileNameValidator>(paramValidator))){
00113       QString paramName = 
00114         ((TreeModel*)(index.model()))->data(index.sibling(index.row(), 0),Qt::DisplayRole).toString();
00115       QString currentPath = ((TreeModel*)(index.model()))->data(index,Qt::DisplayRole).toString();
00116       if(currentPath.size() == 0){
00117         currentPath = QDir::homePath();
00118       }
00119       QString filename;
00120       if(rcp_dynamic_cast<const FileNameValidator>(paramValidator)->fileMustExist()){
00121         filename = QFileDialog::getOpenFileName(parent, paramName, currentPath);
00122       }
00123       else{
00124         filename = QFileDialog::getSaveFileName(parent, paramName, currentPath);
00125       }
00126       if(filename != ""){
00127         ((TreeModel*)(index.model()))->setData(index, filename);
00128       }
00129     }
00130     else if(paramValidator->validStringValues()->size() != 0){
00131       RCP<const Array<std::string> > options = paramValidator->validStringValues();
00132       editor = new QComboBox(parent);
00133       for(Array<std::string>::const_iterator itr = options->begin(); itr != options->end(); ++itr){
00134         static_cast<QComboBox*>(editor)->addItem(QString::fromStdString(*itr));
00135       }
00136     }
00137     else{
00138       editor = new QLineEdit(parent);
00139     }
00140   }
00141   else if(itemType.contains(arrayId)){
00142     editor = getArrayEditor(index, getArrayType(itemType), parent);
00143   }
00144   else if(itemType.contains(twoDArrayId)){
00145     editor = getArrayEditor(index, getArrayType(itemType), parent, true);
00146   }
00147 
00148   return editor;
00149 }
00150 
00151 void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
00152   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00153   QVariant value = index.model()->data(index);
00154   if(itemType == intId){
00155     static_cast<QSpinBox*>(editor)->setValue(value.toInt());
00156   }
00157   else if(itemType == shortId){
00158     static_cast<QSpinBox*>(editor)->setValue(value.toInt());
00159   }
00160   else if(itemType == doubleId){
00161     static_cast<QLineEdit*>(editor)->setText(value.toString());
00162   }
00163   else if(itemType == floatId){
00164     static_cast<QLineEdit*>(editor)->setText(value.toString());
00165   }
00166   else if(itemType == boolId){
00167     static_cast<QComboBox*>(editor)->setEditText(value.toString());
00168   }
00169   else if(itemType == stringId){
00170     RCP<const ParameterEntryValidator> validator = ((TreeModel*)(index.model()))->getValidator(index);
00171     if(is_null(validator) || validator->validStringValues()->size()==0)
00172       static_cast<QLineEdit*>(editor)->setText(value.toString());
00173     else
00174       static_cast<QComboBox*>(editor)->setEditText(value.toString());
00175   }
00176   else if(itemType.contains(arrayId)){
00177     setArrayWidgetData(editor, getArrayType(itemType), index);
00178   }
00179   else if(itemType.contains(twoDArrayId)){
00180     setArrayWidgetData(editor, getArrayType(itemType), index, true);
00181   }
00182 }
00183 
00184 void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
00185   QString itemType = ((TreeModel*)(index.model()))->itemType(index);
00186   if(itemType == intId){
00187     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
00188     spinBox->interpretText();
00189     model->setData(index, spinBox->value(), Qt::EditRole);
00190   }
00191   else if(itemType == shortId){
00192     QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
00193     spinBox->interpretText();
00194     model->setData(index, (short)spinBox->value(), Qt::EditRole);
00195   }
00196   else if(itemType == doubleId){
00197     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
00198     model->setData(index, lineEdit->text(), Qt::EditRole);
00199   }
00200   else if(itemType == floatId){
00201     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
00202     model->setData(index, lineEdit->text(), Qt::EditRole);
00203   }
00204   else if(itemType == boolId){
00205     bool value = static_cast<QComboBox*>(editor)->currentText() 
00206       == getBoolEditorTrue(); 
00207     model->setData(index, value, Qt::EditRole);
00208   }
00209   else if(itemType == stringId){
00210     RCP<const ParameterEntryValidator> validator = 
00211       static_cast<const TreeModel*>(index.model())->getValidator(index);
00212     QString value;
00213     if(is_null(validator)){
00214       value = static_cast<QLineEdit*>(editor)->text();
00215     }
00216     else{
00217       value = static_cast<QComboBox*>(editor)->currentText(); 
00218     }
00219     model->setData(index, value, Qt::EditRole);
00220   }
00221   else if(itemType.contains(arrayId)){
00222     QVariant value = extractValueFromArray(editor, getArrayType(itemType));
00223     model->setData(index, value, Qt::EditRole);
00224   }
00225   else if(itemType.contains(twoDArrayId)){
00226     QVariant value = extractValueFromArray(editor, getArrayType(itemType), true);
00227     model->setData(index, value, Qt::EditRole);
00228   }
00229 }
00230 
00231  
00232 
00233 void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const{
00234   editor->setGeometry(option.rect);
00235 }
00236 
00237 QWidget* Delegate::getArrayEditor(const QModelIndex& index, QString type, QWidget *parent, bool isTwoD) const{
00238   TreeModel* model = (TreeModel*)index.model();
00239   QString name = model->data(
00240     index.sibling(index.row(),0),Qt::DisplayRole).toString();
00241   RCP<const ParameterEntryValidator> validator = 
00242     model->getValidator(index);
00243   if(type == intId){
00244     if(isTwoD){
00245       return new Int2DArrayWidget(name, type, validator, parent);
00246     }
00247     else{
00248       return new IntArrayWidget(name, type, validator, parent);
00249     }
00250   }
00251   else if(type == shortId){
00252     if(isTwoD){
00253       return new Short2DArrayWidget(name, type, validator, parent);
00254     }
00255     else{
00256       return new ShortArrayWidget(name, type, validator, parent);
00257     }
00258   }
00259   else if(type == doubleId){
00260     if(isTwoD){
00261       return new Double2DArrayWidget(name, type, validator, parent);
00262     }
00263     else{
00264       return new DoubleArrayWidget(name, type, validator, parent);
00265     }
00266   }
00267   else if(type == floatId){
00268     if(isTwoD){
00269       return new Float2DArrayWidget(name, type, validator, parent);
00270     }
00271     else{
00272       return new FloatArrayWidget(name, type, validator, parent);
00273     }
00274   }
00275   else if(type == stringId){
00276     if(isTwoD){
00277       return new String2DArrayWidget(name, type, validator, parent);
00278     }
00279     else{
00280       return new StringArrayWidget(name, type, validator, parent);
00281     }
00282   }
00283   else{
00284     return 0;
00285   }
00286 }
00287 
00288 void Delegate::setArrayWidgetData(QWidget* editor, QString type, const QModelIndex& index, bool isTwoD) const{
00289   QVariant newData = index.model()->data(index, TreeModel::getRawDataRole());
00290   if(type == intId){
00291     isTwoD ?
00292     ((Int2DArrayWidget*)editor)->initData(newData.value<TwoDArray<int> >())
00293     :
00294     ((IntArrayWidget*)editor)->initData(newData.value<Array<int> >());
00295   }
00296   else if(type == shortId){
00297     isTwoD ?
00298     ((Short2DArrayWidget*)editor)->initData(newData.value<TwoDArray<short> >())
00299     :
00300     ((ShortArrayWidget*)editor)->initData(newData.value<Array<short> >());
00301   }
00302   else if(type == doubleId){
00303     isTwoD ?
00304     ((Double2DArrayWidget*)editor)->initData(newData.value<TwoDArray<double> >())
00305     :
00306     ((DoubleArrayWidget*)editor)->initData(newData.value<Array<double> >());
00307   }
00308   else if(type == floatId){
00309     isTwoD ?
00310     ((Float2DArrayWidget*)editor)->initData(newData.value<TwoDArray<float> >())
00311     :
00312     ((FloatArrayWidget*)editor)->initData(newData.value<Array<float> >());
00313   }
00314   else if(type == stringId){
00315     isTwoD ?
00316     ((String2DArrayWidget*)editor)->initData(newData.value<TwoDArray<std::string> >())
00317     :
00318     ((StringArrayWidget*)editor)->initData(newData.value<Array<std::string> >());
00319   }
00320 }
00321 
00322 QVariant Delegate::extractValueFromArray(QWidget* editor, QString type, bool isTwoD) const
00323 {
00324   if(type == intId){
00325     return (isTwoD ?
00326     QVariant::fromValue(((Int2DArrayWidget*)editor)->getData())
00327     :
00328     QVariant::fromValue(((IntArrayWidget*)editor)->getData()));
00329   }
00330   else if(type == shortId){
00331     return (isTwoD ?
00332     QVariant::fromValue(((Short2DArrayWidget*)editor)->getData())
00333     :
00334     QVariant::fromValue(((ShortArrayWidget*)editor)->getData()));
00335   }
00336   else if(type == doubleId){
00337     return (isTwoD ?
00338     QVariant::fromValue(((Double2DArrayWidget*)editor)->getData())
00339     :
00340     QVariant::fromValue(((DoubleArrayWidget*)editor)->getData()));
00341   }
00342   else if(type == floatId){
00343     return (isTwoD ?
00344     QVariant::fromValue(((Float2DArrayWidget*)editor)->getData())
00345     :
00346     QVariant::fromValue(((FloatArrayWidget*)editor)->getData()));
00347   }
00348   else if(type == stringId){
00349     return (isTwoD ?
00350     QVariant::fromValue(((String2DArrayWidget*)editor)->getData())
00351     :
00352     QVariant::fromValue(((StringArrayWidget*)editor)->getData()));
00353   }
00354   else{
00355     return QVariant();
00356   }
00357 }
00358 
00359 } //End namespace
00360 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines