tuchulcha  0.10.1
Graphical Workflow Configuration Editor
WorkflowComments.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Eric Koenigs
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  */
18 
26 #include "WorkflowComments.h"
27 
28 #include <QVBoxLayout>
29 #include <QRegExp>
30 #include <QTextEdit>
31 #include <QMutex>
32 
33 #include "QParameterFile.h"
34 #include "ParameterFileModel.h"
35 #include "ObjectInspector.h"
36 
38  QTextEdit(myParent),
39  _model(0),
40  _textChangeLock(new QMutex())
41 {
42  setAcceptRichText(false);
43  connect(this, SIGNAL(textChanged()), SLOT(save()));
44  setEnabled(false);
45 }
46 
47 WorkflowComments::~WorkflowComments() {
48  delete _textChangeLock;
49 }
50 
52  // Get the comment from the editor and escape the newlines to HTML
53  QString comment = toPlainText();
54  comment.replace(QRegExp("\n"), "<br>");
55 
56  if (_model && isEnabled() && _textChangeLock->tryLock()) {
57  _model -> setValue("editorcomment", comment);
58  _textChangeLock->unlock();
59  }
60 
61 }
62 
64  // Don't do anything if save() caused load() to be called.
65  if (_model && _textChangeLock->tryLock()) {
66  // Get the current model
67  QString comment = _model -> getValue("editorcomment");
68 
69  // Replace HTML newlines with escaped newlines
70  comment.replace(QRegExp("<br\\s*/?>", Qt::CaseInsensitive), "\n");
71 
72  // Update the text field if it has changed
73  if ( toPlainText() != comment ) {
74  setPlainText( comment );
75  }
76  _textChangeLock->unlock();
77  }
78 }
79 
80 
82  if (_model == model) {
83  // model didn't change, NOP
84  return;
85  }
86  // if a model exists, disconnect it,
87  // and set the new model as the active model
88  if (_model) {
89  disconnect(_model, 0, this, 0);
90  }
91 
92  // assign new model
93  _model = model;
94 
95  // clear old content
96  _textChangeLock->lock();
97  clear();
98  _textChangeLock->unlock();
99 
100  // reconnect the load slot to the new model
101  if (_model) {
102  connect(_model, SIGNAL(modelReset()), SLOT(load()));
103  load();
104  }
105  setEnabled(_model);
106 }
ParameterFileModel * _model
The currently active model.
void update(ParameterFileModel *model)
change the active model and call load()
Declaration of class ParameterFileModel.
void load()
load the comment from the model and display it in the text field
Declaration of class QParameterFile.
This model serves to provide a model frontend to access a ParameterFile instance. ...
WorkflowComments(QWidget *parent=0)
Default Constructor.
QMutex * _textChangeLock
Stop load() from doing anything while save() is executing.
Declaration of class ObjectInspector.
Declarations of class WorkflowComments.
void save()
save the comment to the model