Developer Documentation
vsiPlugin.cc
1/*===========================================================================*\
2* *
3* OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39* *
40\*===========================================================================*/
41
42
43
44//=============================================================================
45//
46// CLASS VsiPlugin - IMPLEMENTATION
47//
48//=============================================================================
49
50//== INCLUDES =================================================================
51
52#include <QAction>
53#include <QMessageBox>
54
56
58
59#include "vsiPlugin.hh"
60
61#include "baseWidget.hh"
62#include "parser/context.hh"
63#include "parser/input.hh"
64#include "config/dynamicDialog.hh"
65
66//------------------------------------------------------------------------------
67
70 context_ (0),
71 baseWidget_ (0)
72{
73}
74
75//------------------------------------------------------------------------------
76
78VsiPlugin::~ VsiPlugin()
79{
80 if (context_)
81 delete context_;
82 if (baseWidget_)
83 delete baseWidget_;
84}
85
86//------------------------------------------------------------------------------
87
90{
91 if (OpenFlipper::Options::nogui ())
92 return;
93
94 QMenu *scriptingMenu;
95
96 emit emit getMenubarMenu(tr("&Scripting"), scriptingMenu, true );
97
98 QAction* showEditor = scriptingMenu->addAction ("Visual script editor");
99 showEditor->setIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"vsi_vsiEditor.png") );
100
101 connect (showEditor, SIGNAL(triggered()) ,
102 this , SLOT(showScriptEditor()));
103
104 emit addMenubarAction(showEditor, TOOLSMENU );
105
106}
107
108//------------------------------------------------------------------------------
109
112{
113 initContext ();
114 if (!baseWidget_)
115 {
116 baseWidget_ = VSI::BaseWidget::createBaseWidget (context_);
117 connect (baseWidget_, SIGNAL (codeToScriptEditor (QString)),
118 this, SLOT (showInScriptEditor(QString)));
119 }
120
121 baseWidget_->show ();
122}
123
126{
127 if (context_)
128 return;
129
130 // empty context
131 context_ = new VSI::Context (this, this);
132
133 // parse all metadata xml files
134 QDir dir = OpenFlipper::Options::dataDir ();
135
136 bool ok = dir.cd ("VsiMetadata");
137
138 if (!ok || !dir.exists ())
139 return;
140
141 foreach (QString sub, dir.entryList(QDir::Dirs))
142 {
143 if (sub == "..")
144 continue;
145
146 QDir subdir = dir;
147 if ( !subdir.cd (sub) )
148 emit log(LOGERR,"Error entering directory!");
149
150 subdir.makeAbsolute();
151
152 foreach (QString file, subdir.entryList (QStringList("*.xml"), QDir::Files))
153 {
154 QFile f (subdir.filePath (file));
155 if (!f.open (QIODevice::ReadOnly))
156 continue;
157
158 context_->parse(f);
159
160 f.close();
161 }
162 }
163
164}
165
166//------------------------------------------------------------------------------
167
169QString VsiPlugin::askForInputs(QString _element, QString _inputs)
170{
171 initContext ();
172
173 VSI::Element *e = context_->element (_element);
174
175 if (!e)
176 return QString("{}");
177
178 QVector<VSI::Input *> inputs;
179
180#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
181 foreach (QString s, _inputs.split (",", QString::SkipEmptyParts))
182#else
183 foreach (QString s, _inputs.split (",", Qt::SkipEmptyParts))
184#endif
185 foreach (VSI::Input *i, e->inputs ())
186 {
187 if (i->name () == s)
188 {
189 inputs.append (i);
190 break;
191 }
192 }
193
194 if (inputs.isEmpty ())
195 return QString("{}");
196
197 VSI::DynamicDialog d (inputs);
198 d.setWindowTitle (e->shortDescription () + " Input Configuration");
199 d.exec ();
200
201 QMap<QString, QString> results = d.getResults ();
202
203 QString result("{");
204
205 bool first = true;
206#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
207 foreach (QString s, _inputs.split (",", QString::SkipEmptyParts)) {
208#else
209 foreach (QString s, _inputs.split (",", Qt::SkipEmptyParts)) {
210#endif
211 QString value(results[s]);
212 if (!first) result += ",";
213 // value.replace("\"", "\\\"");
214 result += "\""+s+"\":"+value;
215 first = false;
216 }
217
218 result += "}";
219
220 return result;
221}
222
223//------------------------------------------------------------------------------
224
226void VsiPlugin::showInScriptEditor(const QString& _script)
227{
228 emit openPythonScriptInEditor(_script);
229
230 /*
231 bool ok;
232
233 emit functionExists ("scripting", "showScriptInEditor(QString)", ok);
234
235 if (!ok)
236 return;
237
238 RPC::callFunction ("scripting", "showScriptInEditor", _script);*/
239}
240
241//------------------------------------------------------------------------------
242
244void VsiPlugin::messageBox (QString _message)
245{
246 QMessageBox msgBox;
247 msgBox.setText(_message);
248 msgBox.exec();
249}
250
251//------------------------------------------------------------------------------
252
254bool VsiPlugin::questionBox (QString _message)
255{
256 QMessageBox msgBox;
257 msgBox.setText(_message);
258 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
259 msgBox.setDefaultButton(QMessageBox::Yes);
260 int ret = msgBox.exec();
261 if (ret == QMessageBox::Yes)
262 return true;
263 return false;
264}
265
267bool VsiPlugin::continueBox (QString _message)
268{
269 static QContinueBox* msgBox = NULL;
270
271 if ( !msgBox ) {
272 msgBox = new QContinueBox(_message);
273 msgBox->show();
274 } else {
275
276 if ( msgBox->continueBox() ) {
277 return true;
278 } else {
279 delete msgBox;
280 msgBox = NULL;
281 return false;
282 }
283 }
284
285 return true;
286}
287
288//------------------------------------------------------------------------------
289
@ LOGERR
#define TOOLSMENU
The Menu will be added inside the Tools Menu.
virtual void addMenubarAction(QAction *_action, QString _name)
Adds an action to the menubar.
static BaseWidget * createBaseWidget(Context *_ctx, QWidget *_parent=NULL)
Singleton constructor.
Definition: baseWidget.cc:439
Element * element(const QString &_name)
Returns the element with a given name.
Definition: context.cc:155
void parse(QFile &_xml)
Parse xml content.
Definition: context.cc:180
QMap< QString, QString > getResults()
Results.
const QString & shortDescription() const
Short description.
Definition: element.hh:88
const QVector< Input * > & inputs() const
Inputs.
Definition: element.hh:94
const QString & name() const
Name.
Definition: inout.hh:70
void messageBox(QString _message)
Scripting function, that displays a message box.
Definition: vsiPlugin.cc:244
bool continueBox(QString _message)
Shows a non blocking stop box for use inside loops.
Definition: vsiPlugin.cc:267
void showScriptEditor()
Shows visual script editor.
Definition: vsiPlugin.cc:111
VsiPlugin()
Constructor.
Definition: vsiPlugin.cc:69
bool questionBox(QString _message)
Scripting function, that displays a Yes/No message box.
Definition: vsiPlugin.cc:254
void showInScriptEditor(const QString &_script)
Opens the text based script editor with the given script.
Definition: vsiPlugin.cc:226
QString askForInputs(QString _element, QString _inputs)
Scripting function, that allows to ask the user for inputs during script execution.
Definition: vsiPlugin.cc:169
void initContext()
initalisation
Definition: vsiPlugin.cc:125
void pluginsInitialized()
Register in menubar.
Definition: vsiPlugin.cc:89