tuchulcha  0.10.1
Graphical Workflow Configuration Editor
InspectorDelegate.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 Jens-Malte Gottfried
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17 */
24 // The filename editor widgets are based on CMake code:
25 //
26 // Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
27 //
28 // See CMakeCopyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
29 //
30 // This software is distributed WITHOUT ANY WARRANTY; without even
31 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
32 // PURPOSE. See the above copyright notices for more information.
33 
34 #include "InspectorDelegate.h"
35 #include "ParameterFileModel.h"
36 #include "MetaData.h"
37 #include <QDirModel>
38 #include <QFileInfo>
39 #include <QFileDialog>
40 #include <QToolButton>
41 #include <QComboBox>
42 #include <QResizeEvent>
43 #include <QDoubleSpinBox>
44 #include <QSettings>
45 #include <QDir>
46 #include "QDirEdit.h"
47 
49  QStyledItemDelegate(p), _fileDialogFlag(false) {
50  connect(this,
51  SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
52  SLOT(_handleCloseEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
53 }
54 
55 QWidget* InspectorDelegate::createEditor(QWidget* p,
56  const QStyleOptionViewItem& opt, const QModelIndex& ind) const {
57 
58  const ParameterFileModel* model =
59  qobject_cast<const ParameterFileModel*>(ind.model());
60  if (model && QFileInfo(model->fileName()).exists()) {
61  *const_cast<QString*>(&_workingDir) = QDir::currentPath();
62  QDir::setCurrent(QFileInfo(model->fileName()).path());
63  }
64  else {
65  qDebug("InspectorDelegate::setModelData: no parameter file model");
66  }
67 
68  // detect parameter type
69  if (model && model->onlyParams()) {
70  QString param = model->data(ind.sibling(ind.row(),0)).toString();
71  if (!model->prefix().isEmpty())
72  param = model->prefix() + "." + param;
73  // do not cast to lowercase since
74  // e.g. combo box entries would get messed up
75  QString type = model->getType(param);
76 
77  if (type.compare("OpenFile",Qt::CaseInsensitive)==0 ||
78  type.compare("FileOpen",Qt::CaseInsensitive)==0) {
79  QDirEdit* editor = new QDirEdit(param, p);
80  editor->acceptFiles(true, false);
81  connect(
82  editor, SIGNAL(dialogOpen(bool)),
83  this, SLOT(_setFileDialogFlag(bool)));
84  return editor;
85  }
86  if (type.compare("WriteFile",Qt::CaseInsensitive)==0 ||
87  type.compare("FileWrite",Qt::CaseInsensitive)==0 ||
88  type.compare("FileName",Qt::CaseInsensitive)==0) {
89  QDirEdit* editor = new QDirEdit(param, p);
90  editor->acceptFiles(true, true);
91  connect(
92  editor, SIGNAL(dialogOpen(bool)),
93  this, SLOT(_setFileDialogFlag(bool)));
94  return editor;
95  }
96  if (type.compare("Path",Qt::CaseInsensitive)==0) {
97  QDirEdit* editor = new QDirEdit(param, p);
98  connect(
99  editor, SIGNAL(dialogOpen(bool)),
100  this, SLOT(_setFileDialogFlag(bool)));
101  return editor;
102  }
103  if (type.contains(QRegExp("^\\{\\s*\\w.*\\}\\s*$"))) {
104  QComboBox* editor = new QComboBox(p);
105  editor->setObjectName("selectBox");
106  QStringList options = type.trimmed().mid(
107  1,type.length()-2).trimmed().split(
108  ";",QString::SkipEmptyParts);
109  editor->addItems(options);
110  QString cur = ind.model()->data(ind).toString();
111  int curInd = options.indexOf(cur);
112  if (curInd >= 0)
113  editor->setCurrentIndex(curInd);
114  return editor;
115  }
116  // fix decimals for double editors
117  // (also handles parameters of type T)
118  if (ind.model()->data(ind).type() == QVariant::Double) {
119  QLineEdit* editor = new QLineEdit(p) ;
120  QDoubleValidator* validator = new QDoubleValidator(editor) ;
121  validator->setNotation(QDoubleValidator::ScientificNotation) ;
122  editor->setValidator(validator) ;
123  editor->setObjectName("doubleLineEdit") ;
124  editor->setText(QString("%1").arg(ind.model()->data(ind).toDouble(),0,'G',16)) ;
125  Q_ASSERT(editor);
126  return editor;
127  }
128  }
129  return QStyledItemDelegate::createEditor(p,opt,ind);
130 }
131 
132 void InspectorDelegate::setModelData (QWidget* editor,
133  QAbstractItemModel* model, const QModelIndex & index ) const {
134  QDirEdit* dirEdit = 0;
135  if(editor && (editor->objectName() == "selectBox")) {
136  QComboBox* box = qobject_cast<QComboBox*>(editor);
137  model->setData(index,box->currentText());
138  return;
139  }
140  else if(editor && (editor->objectName() == "doubleLineEdit")) {
141  QLineEdit* line = qobject_cast<QLineEdit*>(editor);
142  model->setData(index,line->text().toDouble());
143  return;
144  }
145  else if ( (dirEdit = qobject_cast<QDirEdit*>(editor)) ) {
146  QSettings s;
147  ParameterFileModel* pmodel = qobject_cast<ParameterFileModel*>(model);
148  if (pmodel) {
149  if (s.value("relativePaths",true).toBool()) {
150  QStringList eData = dirEdit->text().split(";"), rData;
151  QDir wDir = QFileInfo(pmodel->fileName()).dir();
152  foreach(QString cur, eData) {
153  rData << wDir.relativeFilePath(cur);
154  }
155  model->setData(index,rData.join(";"));
156  return;
157  }
158  }
159  else {
160  qDebug("InspectorDelegate::setModelData: no parameter file model");
161  }
162  }
163  QStyledItemDelegate::setModelData(editor, model, index);
164 }
165 
167  _fileDialogFlag = f;
168 }
169 
170 bool InspectorDelegate::eventFilter(QObject* object, QEvent* ev) {
171  if(ev->type() == QEvent::FocusOut && _fileDialogFlag)
172  return false;
173  return QStyledItemDelegate::eventFilter(object, ev);
174 }
175 
176 void InspectorDelegate::_handleCloseEditor(QWidget*, QAbstractItemDelegate::EndEditHint) {
177  if (!_workingDir.isEmpty()) {
178  QDir::setCurrent(_workingDir);
179  _workingDir = QString();
180  }
181 }
Declaration of class DirEdit.
void _handleCloseEditor(QWidget *, QAbstractItemDelegate::EndEditHint)
reset working dir
Declaration of class ParameterFileModel.
Implementation of class ParameterFileModel.
Declaration of class InspectorDelegate.
Line edit with completer for directories and dialog option.
Definition: QDirEdit.h:31
bool charon_core_DLL_PUBLIC exists(const std::string &file)
QString prefix() const
Get property _prefix.
This model serves to provide a model frontend to access a ParameterFile instance. ...
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Access to item content.
bool eventFilter(QObject *object, QEvent *event)
filter editor events
InspectorDelegate(QObject *parent=0)
standart constructor
void _setFileDialogFlag(bool flag)
set _fileDialogFlag
QString fileName() const
Get property _fileName.
void acceptFiles(bool files=true, bool write=true)
allow files instead of directories
Definition: QDirEdit.cpp:86
charon_core_DLL_PUBLIC std::string type(const std::string &typeInfo)
bool onlyParams() const
Get property _onlyparams.
QString _workingDir
cache
QString getType(QString parName, bool applyTmplType=true) const
Get type of some parameter or slot.
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Sets the data for the item at the given index in the model to the contents of the given editor...
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
create own editors, if necessary
bool _fileDialogFlag
file dialog opened