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