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 
00034 namespace Optika{
00035 
00036 TreeItem::TreeItem(const QList<QVariant> &data, Teuchos::ParameterEntry *parameter, TreeItem *parent, bool unrecognized):
00037   unrecognized(unrecognized),
00038   itemData(data),
00039   parentItem(parent),
00040   parameterEntry(parameter)
00041 {
00042   if(unrecognized && parameter != 0){
00043     this->docString = "Sorry, but we don't recognize the type of the " + data.at(0).toString() + " parameter.\n"
00044      + "No worries though. Everything should be fine.\n"
00045      "We'll just go ahead and set this parameter to its default value for you."
00046      "\n\nActual Documentation:\n" + QString::fromStdString(parameter->docString());
00047   }
00048   else if(parameter != 0){
00049     this->docString = QString::fromStdString(parameter->docString());
00050   }
00051   else{
00052     this->docString = "";
00053   }
00054 }
00055 
00056 TreeItem::~TreeItem(){
00057   qDeleteAll(childItems);
00058 }
00059 
00060 void TreeItem::printOut() const{
00061   std::cout << itemData.at(0).toString().toStdString() <<  ":     ";
00062   for(int i=0; i < itemData.size(); ++i){
00063     std::cout << itemData.at(i).toString().toStdString() << " ";
00064   }
00065   std::cout << "\n";
00066   for(int i=0; i<childItems.size(); ++i){
00067     childItems.at(i)->printOut();
00068   }
00069 }
00070 
00071 void TreeItem::appendChild(TreeItem *item){
00072   childItems.append(item);
00073 }
00074 
00075 TreeItem *TreeItem::child(int row){
00076   return childItems.value(row);
00077 }
00078 
00079 int TreeItem::childCount() const{
00080   return childItems.count();
00081 }
00082 
00083 const QList<TreeItem*> TreeItem::getChildItems(){
00084   return childItems;
00085 }
00086 
00087 int TreeItem::columnCount() const{
00088   return itemData.size();
00089 }
00090 
00091 QVariant TreeItem::data(int column, int role) const{
00092   if(role == Qt::ToolTipRole){
00093     if(itemData.value(0).toString().compare(QString("Kurtis is awesome!"), Qt::CaseInsensitive) == 0){
00094       return QString("I know! I think I'm awesome too!\n"
00095       "You're pretty awesome yourself! You should send\n"
00096       "me an e-mail letting me know you found the easter egg.\n"
00097       "I'd enjoy that.\n"
00098       "kob0724@gmail.com or klnusbaum@gmail.com");
00099     }
00100     else if(itemData.value(0).toString().compare(QString("Jim is awesome!"), Qt::CaseInsensitive) == 0){
00101       return QString("I know! I think he's awesome too!\n"
00102       "You're pretty awesome yourself! You should send\n"
00103       "Jim an e-mail letting him know you think he's awesome.\n"
00104       "He'd enjoy that.\n"
00105       "Tell him Kurtis sent you. jmwille@sandia.gov");
00106     }
00107     else if(itemData.value(0).toString().compare(QString("Dr. Heroux is awesome!"), Qt::CaseInsensitive) == 0){
00108       return QString("I know! I think he's awesome too!\n"
00109       "You're pretty awesome yourself! You should send\n"
00110       "Dr. Heroux an e-mail letting him know you think he's awesome.\n"
00111       "He'd enjoy that.\n"
00112       "Tell him Kurtis sent you. maherou@sandia.gov");
00113     }
00114     return docString;
00115   }
00116   if(role == Qt::DisplayRole && unrecognized){
00117     if(column == 0){
00118       return itemData.at(0);
00119     }
00120     else if (column == 1){
00121       return QVariant("N/A");
00122     }
00123     else if(column == 2){
00124       return QVariant("Unrecognized type");
00125     }
00126   }
00127   if(role == Qt::DisplayRole){
00128     return itemData.value(column);
00129   }
00130   return QVariant();
00131 
00132 }
00133 
00134 TreeItem* TreeItem::parent(){
00135   return parentItem;
00136 }
00137 
00138 int TreeItem::row() const{
00139   if(parentItem){
00140     return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
00141   }
00142   return 0;
00143 }
00144 
00145 const Teuchos::ParameterEntry* TreeItem::entry(){
00146   return parameterEntry;
00147 }
00148 
00149 bool TreeItem::hasValidValue() const{
00150   if(Teuchos::is_null(parameterEntry->validator()))
00151     return true;
00152   else{
00153     try{
00154       parameterEntry->validator()->validate(*parameterEntry, data(0).toString().toStdString(),
00155                     parentItem->data(0,Qt::DisplayRole).toString().toStdString());
00156       return true;
00157     }
00158     catch(std::exception& e){
00159       return false;
00160     }
00161   }
00162   //should never get here
00163   return true;
00164 
00165 }
00166   
00167 
00168 bool TreeItem::changeValue(QVariant value){
00169   if(itemData[1].toString() == value.toString()){
00170     return false;
00171   }
00172   itemData[1] = value;
00173   if(data(2).toString() == intId){
00174     parameterEntry->setValue(value.toInt(), false, parameterEntry->docString(), parameterEntry->validator());
00175   }
00176   else if(data(2).toString() == shortId){
00177     parameterEntry->setValue((short)value.toInt(), false, parameterEntry->docString(), parameterEntry->validator());
00178   }
00179   else if(data(2).toString() == doubleId){
00180     parameterEntry->setValue(value.toDouble(), false, parameterEntry->docString(), parameterEntry->validator());
00181   }
00182   else if(data(2).toString() == floatId){
00183     parameterEntry->setValue((float)value.toDouble(), false, parameterEntry->docString(), parameterEntry->validator());
00184   }
00185   else if(data(2).toString() == boolId){
00186     parameterEntry->setValue(value.toBool(), false, parameterEntry->docString(), parameterEntry->validator());
00187   }
00188   else if(data(2).toString() == stringId){
00189     parameterEntry->setValue(value.toString().toStdString(), false, parameterEntry->docString(), parameterEntry->validator());
00190   }
00191   else if(data(2).toString().contains(arrayId)){
00192     changeValueForArray(value, data(2).toString().section(" ",-1));
00193   }
00194   return true;
00195 }
00196 
00197 void TreeItem::setValidator(Teuchos::RCP<const Teuchos::ParameterEntryValidator> validator){
00198   parameterEntry->setValidator(validator);
00199 }
00200 
00201 void TreeItem::changeValueForArray(QVariant value, QString type){
00202   if(type == intId){
00203     parameterEntry->setValue(Optika::fromStringToArray<int>(value.toString()), false,
00204            parameterEntry->docString(), parameterEntry->validator());
00205   }
00206   else if(type == shortId){
00207     parameterEntry->setValue(Optika::fromStringToArray<short>(value.toString()), false,
00208            parameterEntry->docString(), parameterEntry->validator());
00209   }
00210   else if(type == doubleId){
00211     parameterEntry->setValue(Optika::fromStringToArray<double>(value.toString()), false,
00212            parameterEntry->docString(), parameterEntry->validator());
00213   }
00214   else if(type == floatId){
00215     parameterEntry->setValue(Optika::fromStringToArray<float>(value.toString()), false,
00216            parameterEntry->docString(), parameterEntry->validator());
00217   }
00218   else if(type == stringId){
00219     parameterEntry->setValue(Optika::fromStringToArray<std::string>(value.toString()), false,
00220            parameterEntry->docString(), parameterEntry->validator());
00221   }
00222 }
00223 
00224 
00225 }
00226 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Generated on Wed Apr 13 10:05:58 2011 for Optika GUI Toolik by  doxygen 1.6.3