Optika GUI Toolik  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Optika_treeitem.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Optika: A Tool For Developing Parameter Obtaining GUIs
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, with Sandia Corporation, the
8 // U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com)
38 //
39 // ***********************************************************************
40 // @HEADER
41 #include <QStringList>
42 #include <QFile>
43 #include <QTextStream>
44 #include <QSize>
45 #include "Optika_treeitem.hpp"
46 #include "Optika_treemodel.hpp"
47 
48 namespace Optika{
49 
50 TreeItem::TreeItem(const QString& name, RCP<ParameterEntry> parameter, TreeItem *parent, bool isHeader):
51  name(name),
52  parentItem(parent),
53  parameterEntry(parameter),
54  isHeader(isHeader)
55 {
56  myTypeId = getTypeId(parameter);
57  if(isHeader){
58  return;
59  }
61  this->docString = "Sorry, but we don't recognize the type of the " + name + " parameter.\n"
62  + "No worries though. Everything should be fine.\n"
63  "We'll just go ahead and set this parameter to its default value for you."
64  "\n\nActual Documentation:\n" + QString::fromStdString(parameter->docString());
65  }
66  else if(nonnull(parameter)){
67  this->docString = QString::fromStdString(parameter->docString());
68  }
69  else{
70  this->docString = "";
71  }
72 }
73 
75  qDeleteAll(childItems);
76 }
77 
78 void TreeItem::printOut() const{
79  std::cout << name.toStdString() << ": ";
80  for(int i=0; i<childItems.size(); ++i){
81  childItems.at(i)->printOut();
82  }
83 }
84 
86  childItems.append(item);
87 }
88 
90  return childItems.value(row);
91 }
92 
94  return childItems.count();
95 }
96 
97 const QList<TreeItem*> TreeItem::getChildItems(){
98  return childItems;
99 }
100 
102  return 3;
103 }
104 
105 QVariant TreeItem::data(int column, int role) const{
106  if(role == Qt::ToolTipRole){
107  if(name.compare(QString("Kurtis is awesome!"), Qt::CaseInsensitive) == 0){
108  return QString("I know! I think I'm awesome too!\n"
109  "You're pretty awesome yourself! You should send\n"
110  "me an e-mail letting me know you found the easter egg.\n"
111  "I'd enjoy that.\n"
112  "kob0724@gmail.com or klnusbaum@gmail.com");
113  }
114  else if(name.compare(QString("Jim is awesome!"), Qt::CaseInsensitive) == 0){
115  return QString("I know! I think he's awesome too!\n"
116  "You're pretty awesome yourself! You should send\n"
117  "Jim an e-mail letting him know you think he's awesome.\n"
118  "He'd enjoy that.\n"
119  "Tell him Kurtis sent you. jmwille@sandia.gov");
120  }
121  else if(name.compare(QString("Dr. Heroux is awesome!"), Qt::CaseInsensitive) == 0){
122  return QString("I know! I think he's awesome too!\n"
123  "You're pretty awesome yourself! You should send\n"
124  "Dr. Heroux an e-mail letting him know you think he's awesome.\n"
125  "He'd enjoy that.\n"
126  "Tell him Kurtis sent you. maherou@sandia.gov");
127  }
128  return docString;
129  }
130  else if(role == Qt::DisplayRole && isHeader){
131  if(column == 0){
132  return "Parameter";
133  }
134  else if (column == 1){
135  return "Value";
136  }
137  else if(column == 2){
138  return "Type";
139  }
140  }
141  else if(role == Qt::DisplayRole && myTypeId == unrecognizedId){
142  if(column == 0){
143  return name;
144  }
145  else if (column == 1){
146  return QVariant("N/A");
147  }
148  else if(column == 2){
149  return QVariant(unrecognizedId);
150  }
151  }
152  else if(role == Qt::DisplayRole){
153  if(column == 0){
154  return name;
155  }
156  else if(column == 1 &&
158  parameterEntry->isTwoDArray()
159  )
160  {
161  return QString("Click to view 2D Array");
162  }
163  else if(column == 1 &&
165  !parameterEntry->isList()
166  )
167  {
168  std::string str = toString(parameterEntry->getAny());
169  if(parameterEntry->isType<bool>()) {
170  if(str == "0")
171  str = "false";
172  else if(str == "1")
173  str = "true";
174  }
175  return QString::fromStdString(str);
176  }
177  else if(column == 2){
178  return myTypeId;
179  }
180  }
181  else if(role == TreeModel::getRawDataRole()){
182  if(column == 1 && nonnull(parameterEntry) && parameterEntry->isArray()){
185  }
186  else if(column == 1 && nonnull(parameterEntry) && parameterEntry->isTwoDArray()){
188  getArrayType(myTypeId), true);
189  }
190  /*else{
191  return parameterEntry->getAny();
192  }*/
193  }
194  return QVariant();
195 }
196 
198  return parentItem;
199 }
200 
201 int TreeItem::row() const{
202  if(parentItem){
203  return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
204  }
205  return 0;
206 }
207 
209  if(is_null(parameterEntry->validator())){
210  return true;
211  }
212  else{
213  try{
214  parameterEntry->validator()->validate(*parameterEntry, data(0).toString().toStdString(),
215  parentItem->data(0,Qt::DisplayRole).toString().toStdString());
216  return true;
217  }
218  catch(std::exception& /*e*/){
219  return false;
220  }
221  }
222  //should never get here
223  return true;
224 
225 }
226 
228  if(parameterEntry->validator() == null){
229  return "";
230  }
231  try{
232  parameterEntry->validator()->validate(*parameterEntry, data(0).toString().toStdString(),
233  parentItem->data(0,Qt::DisplayRole).toString().toStdString());
234  return "";
235  }
236  catch(std::exception& e){
237  return QString::fromStdString(e.what());
238  }
239 }
240 
241 
242 bool TreeItem::changeValue(QVariant value){
243  if(myTypeId == intId){
244  int newValue = value.value<int>();
245  if(newValue != getValue<int>(*parameterEntry)){
246  parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
247  }
248  }
249  else if(myTypeId == shortId){
250  short newValue = value.value<short>();
251  if(newValue != getValue<short>(*parameterEntry)){
252  parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
253  }
254  }
255  else if(myTypeId == doubleId){
256  double newValue = value.value<double>();
257  if(newValue != getValue<double>(*parameterEntry)){
258  parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
259  }
260  }
261  else if(myTypeId == floatId){
262  float newValue = value.value<float>();
263  if(newValue != getValue<float>(*parameterEntry)){
264  parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
265  }
266  }
267  else if(myTypeId == boolId){
268  bool newValue = value.value<bool>();
269  if(newValue != getValue<bool>(*parameterEntry)){
270  parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
271  }
272  }
273  else if(myTypeId == stringId){
274  std::string newValue = value.toString().toStdString();
275  if(newValue != getValue<std::string>(*parameterEntry)){
276  parameterEntry->setValue(newValue, false, parameterEntry->docString(), parameterEntry->validator());
277  }
278  }
279  else if(myTypeId.contains(arrayId)){
281  }
282  else if(myTypeId.contains(twoDArrayId)){
284  }
285 
286  return true;
287 }
288 
290  parameterEntry->setValidator(validator);
291 }
292 
293 void TreeItem::changeValueForArray(QVariant value, QString type, bool twoD){
294  if(type == intId){
295  twoD ?
296  parameterEntry->setValue(value.value<TwoDArray<int> >(), false,
297  parameterEntry->docString(), parameterEntry->validator())
298  :
299  parameterEntry->setValue(value.value<Array<int> >(), false,
300  parameterEntry->docString(), parameterEntry->validator());
301  }
302  else if(type == shortId){
303  twoD ?
304  parameterEntry->setValue(value.value<TwoDArray<short> >(), false,
305  parameterEntry->docString(), parameterEntry->validator())
306  :
307  parameterEntry->setValue(value.value<Array<short> >(), false,
308  parameterEntry->docString(), parameterEntry->validator());
309  }
310  else if(type == doubleId){
311  twoD ?
312  parameterEntry->setValue(value.value<TwoDArray<double> >(), false,
313  parameterEntry->docString(), parameterEntry->validator())
314  :
315  parameterEntry->setValue(value.value<Array<double> >(), false,
316  parameterEntry->docString(), parameterEntry->validator());
317  }
318  else if(type == floatId){
319  twoD ?
320  parameterEntry->setValue(value.value<TwoDArray<float> >(), false,
321  parameterEntry->docString(), parameterEntry->validator())
322  :
323  parameterEntry->setValue(value.value<Array<float> >(), false,
324  parameterEntry->docString(), parameterEntry->validator());
325  }
326  else if(type == stringId){
327  twoD ?
328  parameterEntry->setValue(value.value<TwoDArray<std::string> >(), false,
329  parameterEntry->docString(), parameterEntry->validator())
330  :
331  parameterEntry->setValue(value.value<Array<std::string> >(), false,
332  parameterEntry->docString(), parameterEntry->validator());
333  }
334 }
335 
337  if(parameter.is_null()){
338  return unrecognizedId;
339  }
340  else if(parameter->isList()){
341  return listId;
342  }
343  else if(parameter->isType<int>()){
344  return intId;
345  }
346  else if(parameter->isType<short>()){
347  return shortId;
348  }
349  else if(parameter->isType<double>()){
350  return doubleId;
351  }
352  else if(parameter->isType<float>()){
353  return floatId;
354  }
355  else if(parameter->isType<bool>()){
356  return boolId;
357  }
358  else if(parameter->isType<std::string>()){
359  return stringId;
360  }
361  else if(parameter->isArray()){
362  QString determinedId = determineArrayType(parameter);
363  if( determinedId != unrecognizedId){
364  return QString(arrayId + " "+ determinedId);
365  }
366  else{
367  return unrecognizedId;
368  }
369  }
370  else if(parameter->isTwoDArray()){
371  QString determinedId = determineArrayType(parameter, true);
372  if(determinedId != unrecognizedId){
373  return QString(twoDArrayId + " "+ determinedId);
374  }
375  else{
376  return unrecognizedId;
377  }
378  }
379  else{
380  return unrecognizedId;
381  }
382  //Should never get here
383  //This is here to avoid compiler warnings
384  return unrecognizedId;
385 }
386 
387 
388 } //end namespace
QString shortId
void appendChild(TreeItem *child)
Appends a child TreeItem to the TreeItem.
bool is_null(const boost::shared_ptr< T > &p)
QVariant data(int column, int role=Qt::DisplayRole) const
Returns the data located in a particular column.
QString intId
int childCount() const
Gets the number of child nodes this item has.
bool changeValue(QVariant value)
Changes the value of the TreeItem. Should only be used with TreeItems that represent Parameters...
QString getCurrentInvalidValueMessage() const
Gets a message desribing the error with the current value.
bool isHeader
Whether or not this is a header treeitem.
QString unrecognizedId
const char * toString(const EReductionType reductType)
TreeItem(const QString &name, RCP< ParameterEntry > parameterEntry, TreeItem *parent=0, bool isHeader=false)
Constructs a TreeItem object.
void printOut() const
Prints out the values in the TreeItem.
TreeItem * child(int row)
Returns the child treeitem in the row specified by the row argument.
const QString name
the name of the tree item.
QString determineArrayType(RCP< const ParameterEntry > parameter, bool twoD)
Determines the type of array stored in a parameter.
bool is_null(const RCP< T > &p)
QVariant arrayEntryToVariant(RCP< const ParameterEntry > arrayEntry, QString type, bool twoD)
Creates a QVariant containing the array that is in arrayEntry.
QString floatId
QString twoDArrayId
The TreeItem class is the item class used by the TreeModel class.
bool hasValidValue() const
Determines whether or not the current value associated with the TreeItem is valid.
QList< TreeItem * > childItems
The childitems of the TreeItem.
~TreeItem()
Deconstrcutor for the TreeItem.
TreeItem * parentItem
The parent TreeItem.
void changeValueForArray(QVariant value, QString type, bool twoD=false)
Changes the value of an array.
QString listId
int columnCount() const
How man columns the TreeItem has. Should always be 3.
QString myTypeId
The type id associated with this TreeItem.
QString doubleId
bool nonnull(const boost::shared_ptr< T > &p)
A item in the treemodel.
RCP< ParameterEntry > parameterEntry
The ParameterEntry being represented by the TreeItem.
QString stringId
static QString getTypeId(const RCP< const ParameterEntry > parameter)
Gets the type id to be used for the TreeItem.
QString boolId
int row() const
Returns the row in which this TreeItem is located.
const QList< TreeItem * > getChildItems()
Gets a list of all the child items.
QString arrayId
QString docString
The docString for the TreeItem.
TreeItem * parent()
Gets the parent TreeItem.
void setValidator(RCP< const ParameterEntryValidator > validator)
Sets the validator for the parameter the TreeItem represents.
QString getArrayType(QString itemType)
Given a type string, determines the template type of the Array.
static const int getRawDataRole()
Returns constant representing the RawDataRole.