Optika GUI Toolik Version of the Day
Optika_treeitem.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 <QStringList>
00029 #include <QFile>
00030 #include <QTextStream>
00031 #include <QSize>
00032 #include "Optika_treeitem.hpp"
00033 #include "Optika_treemodel.hpp"
00034 
00035 namespace Optika{
00036 
00037 TreeItem::TreeItem(const QString& name, RCP<ParameterEntry> parameter, TreeItem *parent, bool isHeader):
00038   name(name),
00039   parentItem(parent),
00040   parameterEntry(parameter),
00041   isHeader(isHeader)
00042 {
00043   myTypeId = getTypeId(parameter);
00044   if(isHeader){
00045     return;
00046   }
00047   else if(myTypeId == unrecognizedId && nonnull(parameterEntry)){
00048     this->docString = "Sorry, but we don't recognize the type of the " + name + " parameter.\n"
00049      + "No worries though. Everything should be fine.\n"
00050      "We'll just go ahead and set this parameter to its default value for you."
00051      "\n\nActual Documentation:\n" + QString::fromStdString(parameter->docString());
00052   }
00053   else if(nonnull(parameter)){
00054     this->docString = QString::fromStdString(parameter->docString());
00055   }
00056   else{
00057     this->docString = "";
00058   }
00059 }
00060 
00061 TreeItem::~TreeItem(){
00062   qDeleteAll(childItems);
00063 }
00064 
00065 void TreeItem::printOut() const{
00066   std::cout << name.toStdString() <<  ":     ";
00067   for(int i=0; i<childItems.size(); ++i){
00068     childItems.at(i)->printOut();
00069   }
00070 }
00071 
00072 void TreeItem::appendChild(TreeItem *item){
00073   childItems.append(item);
00074 }
00075 
00076 TreeItem *TreeItem::child(int row){
00077   return childItems.value(row);
00078 }
00079 
00080 int TreeItem::childCount() const{
00081   return childItems.count();
00082 }
00083 
00084 const QList<TreeItem*> TreeItem::getChildItems(){
00085   return childItems;
00086 }
00087 
00088 int TreeItem::columnCount() const{
00089   return 3;
00090 }
00091 
00092 QVariant TreeItem::data(int column, int role) const{
00093   if(role == Qt::ToolTipRole){
00094     if(name.compare(QString("Kurtis is awesome!"), Qt::CaseInsensitive) == 0){
00095       return QString("I know! I think I'm awesome too!\n"
00096       "You're pretty awesome yourself! You should send\n"
00097       "me an e-mail letting me know you found the easter egg.\n"
00098       "I'd enjoy that.\n"
00099       "kob0724@gmail.com or klnusbaum@gmail.com");
00100     }
00101     else if(name.compare(QString("Jim is awesome!"), Qt::CaseInsensitive) == 0){
00102       return QString("I know! I think he's awesome too!\n"
00103       "You're pretty awesome yourself! You should send\n"
00104       "Jim an e-mail letting him know you think he's awesome.\n"
00105       "He'd enjoy that.\n"
00106       "Tell him Kurtis sent you. jmwille@sandia.gov");
00107     }
00108     else if(name.compare(QString("Dr. Heroux is awesome!"), Qt::CaseInsensitive) == 0){
00109       return QString("I know! I think he's awesome too!\n"
00110       "You're pretty awesome yourself! You should send\n"
00111       "Dr. Heroux an e-mail letting him know you think he's awesome.\n"
00112       "He'd enjoy that.\n"
00113       "Tell him Kurtis sent you. maherou@sandia.gov");
00114     }
00115     return docString;
00116   }
00117   else if(role == Qt::DisplayRole && isHeader){
00118     if(column == 0){
00119       return "Parameter";
00120     }
00121     else if (column == 1){
00122       return "Value";
00123     }
00124     else if(column == 2){
00125       return "Type";
00126     }
00127   }
00128   else if(role == Qt::DisplayRole && myTypeId == unrecognizedId){
00129     if(column == 0){
00130       return name;
00131     }
00132     else if (column == 1){
00133       return QVariant("N/A");
00134     }
00135     else if(column == 2){
00136       return QVariant(unrecognizedId);
00137     }
00138   }
00139   else if(role == Qt::DisplayRole){
00140     if(column == 0){
00141       return name;
00142     }
00143     else if(column == 1 && 
00144       nonnull(parameterEntry) && 
00145       parameterEntry->isTwoDArray()
00146     )
00147     {
00148       return QString("Click to view 2D Array");
00149     }
00150     else if(column == 1 && 
00151       nonnull(parameterEntry) &&
00152       !parameterEntry->isList()
00153     )
00154     {
00155       return QString::fromStdString(toString(parameterEntry->getAny()));
00156     }
00157     else if(column == 2){
00158       return myTypeId;
00159     }
00160   }
00161   else if(role == TreeModel::getRawDataRole()){
00162     if(column == 1 && nonnull(parameterEntry) && parameterEntry->isArray()){
00163       return arrayEntryToVariant(parameterEntry, 
00164         getArrayType(myTypeId));
00165     }
00166     else if(column == 1 && nonnull(parameterEntry) && parameterEntry->isTwoDArray()){
00167       return arrayEntryToVariant(parameterEntry,
00168         getArrayType(myTypeId), true);
00169     }
00170     /*else{
00171       return parameterEntry->getAny();
00172     }*/
00173   }
00174   return QVariant();
00175 }
00176 
00177 TreeItem* TreeItem::parent(){
00178   return parentItem;
00179 }
00180 
00181 int TreeItem::row() const{
00182   if(parentItem){
00183     return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
00184   }
00185   return 0;
00186 }
00187 
00188 bool TreeItem::hasValidValue() const{
00189   if(is_null(parameterEntry->validator())){
00190     return true;
00191   }
00192   else{
00193     try{
00194       parameterEntry->validator()->validate(*parameterEntry, data(0).toString().toStdString(),
00195                     parentItem->data(0,Qt::DisplayRole).toString().toStdString());
00196       return true;
00197     }
00198     catch(std::exception& /*e*/){
00199       return false;
00200     }
00201   }
00202   //should never get here
00203   return true;
00204 
00205 }
00206 
00207 QString TreeItem::getCurrentInvalidValueMessage() const{
00208   if(parameterEntry->validator() == null){
00209     return "";
00210   }
00211   try{
00212     parameterEntry->validator()->validate(*parameterEntry, data(0).toString().toStdString(),
00213                   parentItem->data(0,Qt::DisplayRole).toString().toStdString());
00214     return "";
00215   }
00216   catch(std::exception& e){
00217     return QString::fromStdString(e.what());
00218   }
00219 }
00220   
00221 
00222 bool TreeItem::changeValue(QVariant value){
00223   if(myTypeId == intId){
00224     int newValue = value.value<int>();
00225     if(newValue != getValue<int>(*parameterEntry)){
00226       parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
00227     }
00228   }
00229   else if(myTypeId == shortId){
00230     short newValue = value.value<short>();
00231     if(newValue != getValue<short>(*parameterEntry)){
00232       parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
00233     }
00234   }
00235   else if(myTypeId == doubleId){
00236     double newValue = value.value<double>();
00237     if(newValue != getValue<double>(*parameterEntry)){
00238       parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
00239     }
00240   }
00241   else if(myTypeId == floatId){
00242     float newValue = value.value<float>();
00243     if(newValue != getValue<float>(*parameterEntry)){
00244       parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
00245     }
00246   }
00247   else if(myTypeId == boolId){
00248     bool newValue = value.value<bool>();
00249     if(newValue != getValue<bool>(*parameterEntry)){
00250       parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
00251     }
00252   }
00253   else if(myTypeId == stringId){
00254     std::string newValue = value.toString().toStdString();
00255     if(newValue != getValue<std::string>(*parameterEntry)){
00256       parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
00257     }
00258   }
00259   else if(myTypeId.contains(arrayId)){
00260     changeValueForArray(value, getArrayType(myTypeId));
00261   }
00262   else if(myTypeId.contains(twoDArrayId)){
00263     changeValueForArray(value, getArrayType(myTypeId), true);
00264   }
00265 
00266   return true;
00267 }
00268 
00269 void TreeItem::setValidator(RCP<const ParameterEntryValidator> validator){
00270   parameterEntry->setValidator(validator);
00271 }
00272 
00273 void TreeItem::changeValueForArray(QVariant value, QString type, bool twoD){
00274   if(type == intId){
00275     twoD ? 
00276     parameterEntry->setValue(value.value<TwoDArray<int> >(), false,
00277            parameterEntry->docString(), parameterEntry->validator())
00278     :
00279     parameterEntry->setValue(value.value<Array<int> >(), false,
00280            parameterEntry->docString(), parameterEntry->validator());
00281   }
00282   else if(type == shortId){
00283     twoD ? 
00284     parameterEntry->setValue(value.value<TwoDArray<short> >(), false,
00285            parameterEntry->docString(), parameterEntry->validator())
00286     :
00287     parameterEntry->setValue(value.value<Array<short> >(), false,
00288            parameterEntry->docString(), parameterEntry->validator());
00289   }
00290   else if(type == doubleId){
00291     twoD ? 
00292     parameterEntry->setValue(value.value<TwoDArray<double> >(), false,
00293            parameterEntry->docString(), parameterEntry->validator())
00294     :
00295     parameterEntry->setValue(value.value<Array<double> >(), false,
00296            parameterEntry->docString(), parameterEntry->validator());
00297   }
00298   else if(type == floatId){
00299     twoD ? 
00300     parameterEntry->setValue(value.value<TwoDArray<float> >(), false,
00301            parameterEntry->docString(), parameterEntry->validator())
00302     :
00303     parameterEntry->setValue(value.value<Array<float> >(), false,
00304            parameterEntry->docString(), parameterEntry->validator());
00305   }
00306   else if(type == stringId){
00307     twoD ? 
00308     parameterEntry->setValue(value.value<TwoDArray<std::string> >(), false,
00309            parameterEntry->docString(), parameterEntry->validator())
00310     :
00311     parameterEntry->setValue(value.value<Array<std::string> >(), false,
00312            parameterEntry->docString(), parameterEntry->validator());
00313   }
00314 }
00315 
00316 QString TreeItem::getTypeId(const RCP<const ParameterEntry> parameter){
00317   if(parameter.is_null()){
00318     return unrecognizedId;
00319   }
00320   else if(parameter->isList()){
00321     return listId;
00322   }
00323   else if(parameter->isType<int>()){
00324     return intId;
00325   }
00326   else if(parameter->isType<short>()){
00327     return shortId;
00328   }
00329   else if(parameter->isType<double>()){
00330     return doubleId;
00331   }
00332   else if(parameter->isType<float>()){
00333     return floatId;
00334   }
00335   else if(parameter->isType<bool>()){
00336     return boolId;
00337   }
00338   else if(parameter->isType<std::string>()){
00339     return stringId;
00340   }
00341   else if(parameter->isArray()){
00342     QString determinedId = determineArrayType(parameter);
00343     if( determinedId != unrecognizedId){
00344       return QString(arrayId + " "+ determinedId);
00345     }
00346     else{
00347       return unrecognizedId;
00348     }
00349   }
00350   else if(parameter->isTwoDArray()){
00351     QString determinedId = determineArrayType(parameter, true);
00352     if(determinedId != unrecognizedId){
00353       return QString(twoDArrayId + " "+ determinedId);
00354     }
00355     else{
00356       return unrecognizedId;
00357     }
00358   }
00359   else{
00360     return unrecognizedId;
00361   }
00362   //Should never get here
00363   //This is here to avoid compiler warnings
00364   return unrecognizedId;
00365 }
00366 
00367 
00368 } //end namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines