tuchulcha  0.10.1
Graphical Workflow Configuration Editor
ObjectInspector.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 #include <QDir>
25 #include <QFile>
26 #include <QItemEditorCreator>
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QMutex>
30 #include <QSettings>
31 #include <set>
32 #include <stack>
33 #include "ObjectInspector.h"
34 #include "ParameterFileModel.h"
35 #include "SplitStream.h"
36 #include "PrefixValidator.h"
37 #include "FileManager.h"
38 #include "InspectorDelegate.h"
39 #include "QParameterFile.h"
40 #include "PriorityDialog.h"
41 #include "PriorityBox.h"
42 
43 #include "ui_ObjectInspector.h"
44 
46  QWidget* myParent, ParameterFileModel* newModel, bool hideTools) :
47  QWidget(myParent), _model(0)
48 {
49  _ui = new Ui::ObjectInspector;
50  _ui->setupUi(this);
51 
52  _commentFieldMutex = new QMutex(QMutex::NonRecursive);
53 
54  // handle header context menu
55  _ui->view->horizontalHeader()->setContextMenuPolicy(Qt::ActionsContextMenu);
56  _hidePrio = new QAction(tr("hide priority column"), this);
57  //_hidePrio->setIcon(QIcon::fromTheme("edit-table-delete-column",
58  // QIcon(":/icons/edit-table-delete-column.png")));
59  _hidePrio->setCheckable(true);
60  _hidePrio->setChecked(false);
61  _ui->view->horizontalHeader()->insertAction(0,_hidePrio);
62  connect(_hidePrio,SIGNAL(triggered(bool)),this,SLOT(setPrioColumnHidden(bool)));
63 
64  // use temporary model to set up view
65  ParameterFileModel tempModel;
66  setModel(&tempModel);
67 
68  // set up delegates
69  InspectorDelegate* delegate1 = new InspectorDelegate(this);
70  _ui->view->setItemDelegateForColumn(1, delegate1);
71 
72  QStyledItemDelegate* delegate2 = new QStyledItemDelegate(this);
73  QItemEditorCreatorBase* creator =
74  new QStandardItemEditorCreator<PriorityBox>();
75  QItemEditorFactory* ef = new QItemEditorFactory(
76  *QItemEditorFactory::defaultFactory());
77  ef->registerEditor(QVariant::UInt, creator);
78  delegate2->setItemEditorFactory(ef);
79  _ui->view->setItemDelegateForColumn(2, delegate2);
80 
81  // unregister temporary model
82  setModel(newModel);
83 
84  // disable prefix editing and hide buttonFrame
85  setEdit(false);
86 
87  if (hideTools) {
88  _ui->tools->hide();
89  }
90 }
91 
94  delete _commentFieldMutex;
95  delete _ui;
96 }
97 
99  return _ui->view;
100 }
101 
102 void ObjectInspector::openFile(QString fName) {
103  if (!_model)
104  return;
105  _model->load(fName);
106 }
107 
109  if (!_model || !isEnabled())
110  return;
111  if (_model->fileName().isEmpty())
112  saveFileAs();
113  else
114  _model->save();
115 }
116 
118  if (!_model || !isEnabled())
119  return;
120  QString oldPath = _model->fileName();
121  if (oldPath.isEmpty())
122  oldPath = QDir::currentPath();
123  QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"),
124  oldPath, tr("ParameterFile (*.wrp)"));
125  if (!fileName.isEmpty())
126  _model->save(fileName);
127 }
128 
130  if (_ui->view && _ui->view->model()) {
131  QSettings settings;
132  settings.beginGroup("ObjectInspector");
133 
134  // store column widths
135  settings.setValue("column0", _ui->view->columnWidth(0));
136  settings.setValue("column1", _ui->view->columnWidth(1));
137  settings.setValue("column2", _ui->view->columnWidth(2));
138 
139  // store column visibility
140  settings.setValue("column2hidden", _hidePrio->isChecked());
141 
142  settings.endGroup();
143  }
144 }
145 
147  if (_ui->view->model()) {
148  QSettings settings;
149  settings.beginGroup("ObjectInspector");
150 
151  _hidePrio->setChecked(settings.value("column2hidden",false).toBool());
152  setPrioColumnHidden(_hidePrio->isChecked());
153  _ui->view->setColumnWidth(0, settings.value("column0", 250).toInt());
154  _ui->view->setColumnWidth(1, settings.value("column1", 250).toInt());
155  _ui->view->setColumnWidth(2, settings.value("column2", 90).toInt());
156 
157  settings.endGroup();
158  }
159 }
160 
162  if (newModel == _model)
163  return;
164 
165  if (_model) {
166  // store view settings
168 
169  // disconnect everything from the old model
170  disconnect(_model, 0, this, 0);
171  }
172 
173  _ui->view->setModel(newModel);
174 
175  // commit changes
176  _model = newModel;
177  setEnabled(_model);
178 
179  if (_model) {
180  // init view
182 
183  // update validator
184  PrefixValidator* newValidator =
185  new PrefixValidator(_model->parameterFile(),_ui->prefix);
186  QValidator* oldValidator = (QValidator*) _ui->prefix->validator();
187  _ui->prefix->setValidator(newValidator);
188  if (oldValidator) {
189  oldValidator->deleteLater();
190  }
191 
192  // update textEdit content
194  _ui->filterBox->setCurrentIndex(_model->minPriority());
195 
196  // update values
197  _ui->useMetadata->setEnabled(_model->metaInfoValid());
198  _ui->useMetadata->setChecked(_model->useMetaInfo());
199  _ui->onlyParams->setChecked(_model->onlyParams());
200 
201  // update connections
202  connect(_model, SIGNAL(statusMessage(QString)),
203  SIGNAL(statusMessage(QString)));
204  connect(_model, SIGNAL(prefixChanged(QString)),
205  SLOT(handle_model_prefixChanged(QString)));
206  connect(_model, SIGNAL(metaInfoChanged(bool)),
207  SLOT(handle_model_metaInfoChanged(bool)));
208  connect(_model, SIGNAL(useMetaInfoChanged(bool)),
209  SLOT(handle_model_useMetaInfoChanged(bool)));
210  connect(_model, SIGNAL(onlyParamsChanged(bool)),
211  SLOT(handle_model_onlyParamsChanged(bool)));
212  }
213  else {
214  _ui->prefix->clear();
215  _ui->comment->clear();
216  }
217 
218  emit modelChanged(_model);
219 }
220 
222  return _model;
223 }
224 
226  _ui->prefix->setEnabled(on);
227  _ui->buttonGroupBox->setVisible(on);
228 }
229 
230 void ObjectInspector::openMetaData(QString fname) {
231  if (_model) {
232  if (!QFileInfo(fname).exists()) {
233  fname = QFileDialog::getOpenFileName(0, tr("Open File"),
234  FileManager::instance().classesFile(),
235  tr("ParameterFile (*.*)"));
236  }
237  _model->loadMetaInfo(fname);
238  }
239 }
240 
243 }
244 
246  if (_model) {
247  _model->insertRow(model()->rowCount(QModelIndex()), QModelIndex());
248  }
249 }
251  _model->reactivate();
252 }
253 
255  if (_model) {
256  QItemSelectionModel* selectionModel = _ui->view->selectionModel();
257  uint rows = _model->rowCount();
258 
259  // collect selected rows
260  std::stack<int> rowStack;
261  for (uint i = 0; i < rows; i++)
262  if (selectionModel->rowIntersectsSelection(i, QModelIndex()))
263  rowStack.push(i);
264 
265  if (!rowStack.size()) {
266  QMessageBox::warning(this, tr("delete failed"), tr(
267  "Please select an item to delete first."));
268  return;
269  }
270 
271  // delete rows, reversed to avoid row movement below
272  while (rowStack.size() > 0) {
273  _model->removeRow(rowStack.top(), QModelIndex());
274  rowStack.pop();
275  }
276  }
277 }
278 
280  if (_model) {
281  QMessageBox mbox(QMessageBox::Question, tr("confirm delete"), tr(
282  "Do you really want to delete the model content?"));
283  mbox.addButton(QMessageBox::Yes);
284  mbox.addButton(QMessageBox::No);
285  mbox.setDefaultButton(QMessageBox::No);
286  mbox.setEscapeButton(QMessageBox::No);
287  mbox.exec();
288  if (mbox.result() == QMessageBox::Yes) {
289  _model->clear();
290  emit statusMessage(tr("cleared model content"));
291  }
292  }
293 }
294 
296  if (_model) {
297  QItemSelectionModel* selectionModel = _ui->view->selectionModel();
298  uint rows = _ui->view->model()->rowCount();
299 
300  // collect selected rows
301  std::stack<int> rowStack;
302  for (uint i = 0; i < rows; i++)
303  if (selectionModel->rowIntersectsSelection(i, QModelIndex()))
304  rowStack.push(i);
305 
306  if (!rowStack.size()) {
307  return;
308  }
309 
310  // old value, if only one row selected
311  int before = 0;
312  if (rowStack.size() == 1) {
313  before = _model->data(_model->index(rowStack.top(), 2), Qt::EditRole).toInt();
314  }
315 
316  // open dialog
317  PriorityDialog dlg(this, before);
318 
319  if (dlg.exec() == QDialog::Rejected)
320  return;
321 
322  int priority = dlg.selection();
323 
324  // determine if change needed
325  if (priority != before || rowStack.size() > 1) {
326  // change priority
327  while (rowStack.size() > 0) {
328  _model->setData(_model->index(rowStack.top(), 2), QVariant(priority));
329  rowStack.pop();
330  }
331  }
332  }
333 }
334 
336  if (_model) {
337  _model->setMinPriority(index);
338  }
339 }
340 
342  if (_model) {
343  _ui->filterBox->setCurrentIndex(0);
345  _model->setPrefix("");
346  }
347 }
348 
350  if (_model && _model->onlyParams()) {
352  }
353 }
354 
356  if (_model && isEnabled() && _commentFieldMutex->tryLock()) {
357  QString comment = _ui->comment->toPlainText();
358  comment.replace(QRegExp("\n"), "<br/>");
359  _model->setValue(_model->prefix()+".editorcomment",comment);
360  _commentFieldMutex->unlock();
361  }
362 }
363 
365  _ui->view->setColumnHidden(2,h);
366  if (!h) {
367  _ui->view->resizeColumnsToContents();
368  _ui->view->setColumnWidth(0,_ui->view->width()/2.5);
369  _ui->view->setColumnWidth(1,_ui->view->width()/2.5);
370  }
371 }
372 
373 void ObjectInspector::handle_model_prefixChanged(const QString& prefix) {
374  // update comment text area
375  QMutexLocker locker (_commentFieldMutex);
376  _ui->prefix->setText(prefix);
377  if (!prefix.isEmpty() && _model->prefixValid()) {
378  QString comment = _model->getValue(prefix + ".editorcomment");
379  comment.replace(QRegExp("<br/?>"), "\n");
380  _ui->comment->setPlainText(comment);
381  _ui->pageComment->setEnabled(true);
382  }
383  else {
384  _ui->comment->setPlainText(tr("no node selected"));
385  _ui->pageComment->setEnabled(false);
386  }
387  locker.unlock();
388 }
389 
391  if (_model) {
392  _model->setPrefix(text);
393  }
394 }
395 
397  if (!state) {
398  _ui->onlyParams->setChecked(false);
399  }
400  if (_model) {
401  _model->setUseMetaInfo(state);
402  }
403  _ui->onlyParams->setEnabled(state);
404 }
405 
407  if (_model) {
408  _model->setOnlyParams(state);
409  }
410 }
411 
413  _ui->useMetadata->setEnabled(state);
414 }
415 
417  _ui->useMetadata->setChecked(state);
418 }
419 
421  _ui->onlyParams->setChecked(state);
422 }
void setOnlyParams(bool value)
Set property _onlyparams.
void reactivate()
Reactivate all following plugins.
Declaration of class FileManager.
Priority Dialog.
void _storeViewSettings() const
save settings like column widths
virtual void openMetaData(QString fName=QString())
Open file containing metadata.
virtual void save(const QString &fileName="")
Save model content to a text file.
Declaration of class PriorityBox.
int selection() const
Get selected index.
void setMinPriority(int value)
Set property _minPriority.
void on_ActivateAllButton_clicked()
Reactivate all consecutive plugins.
Declaration of class PrefixValidator.
Ui::ObjectInspector * _ui
designer gui
QAction * _hidePrio
hide priority column
virtual QString setPrefix(const QString &prefix)
Change prefix.
Declaration of class ParameterFileModel.
void handle_model_useMetaInfoChanged(bool state)
update useMetadata checkbox state
delegate to handle parameter types and offer specialized editors
bool metaInfoValid() const
check for available metaInfos
void on_setPriorityButton_clicked()
set priority
QString getValue(QString parName) const
Get a parameter from the underlying parameter file.
static const FileManager & instance()
get a reference to the file Manager instance
Definition: FileManager.cpp:70
ParameterFileModel * _model
Used model to display.
void statusMessage(const QString &msg, int timeout=3000)
Send status message.
const QParameterFile & parameterFile() const
Get const pointer of ParameterFile.
Declaration of class InspectorDelegate.
void on_clearButton_clicked()
Clear model.
virtual void saveFileAs() const
Show SaveAs-Dialog and save File to the selected location.
bool charon_core_DLL_PUBLIC exists(const std::string &file)
Declaration of class QParameterFile.
QString prefix() const
Get property _prefix.
void on_comment_textChanged()
handle comment changes
void on_onlyParams_toggled(bool state)
handle onlyParams checkbox changes
virtual void loadMetaInfo(const QString &fileName)
load metaFile
void modelChanged(ParameterFileModel *model)
Inform about changed model.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Return number of table rows.
void handle_model_prefixChanged(const QString &text)
update prefix & comment text
void delParam()
delete parameter
void on_filterBox_activated(int index)
filter by priority
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.
ParameterFileModel * model() const
Get value of property _model.
void on_useMetadata_toggled(bool state)
handle useMetadata checkbox changes
bool prefixValid() const
Check prefix.
virtual ~ObjectInspector()
default destructor
virtual bool load(const QString &fName="")
Load model content from parameterFile.
QString fileName() const
Get property _fileName.
void on_resetParamButton_clicked()
reset parameter
void on_deleteButton_clicked()
Delete selected parameter.
virtual void saveFile() const
Save File to it's old location.
virtual void setEdit(bool on)
Enable/Disable structural editing.
virtual void openFile(QString fName=QString())
Open given file.
void setValue(QString parName, QString value)
Set a parameter in the underlying parameter file.
Validator for prefix edit widgets.
ObjectInspector(QWidget *parent=0, ParameterFileModel *model=0, bool hideTools=false)
Default constructor, setting parent widget.
bool useMetaInfo() const
Get property _useMetadata;.
QWidget * getViewer()
get inspector viewer widget
void _loadViewSettings()
restore settings like column widths
bool onlyParams() const
Get property _onlyparams.
void setUseMetaInfo(bool value)
Set property _useMetadata.
QMutex * _commentFieldMutex
lock for changes of comments
virtual void clear()
Clear ParameterFile content.
void setPrioColumnHidden(bool hide)
handle context menu of horizontal header
void on_prefix_textChanged(QString text)
handle prefix text field changes
Declaration of class ObjectInspector.
void handle_model_onlyParamsChanged(bool state)
update onlyParams checkbox state
Declaration of class PriorityDialog.
virtual void setModel(ParameterFileModel *model)
Change used model.
void on_resetFilterButton_clicked()
reset filter
int minPriority() const
Get property _minPriority.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Set item content.
void on_addButton_clicked()
Add new Parameter.
void handle_model_metaInfoChanged(bool state)
enable/disable useMetadata checkbox