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 <QScriptEngine>
54 #include <QMessageBox>
55 
57 
59 
60 #include "vsiPlugin.hh"
61 
62 #include "baseWidget.hh"
63 #include "parser/context.hh"
64 #include "parser/input.hh"
65 #include "config/dynamicDialog.hh"
66 
67 //------------------------------------------------------------------------------
68 
71  context_ (0),
72  baseWidget_ (0)
73 {
74 }
75 
76 //------------------------------------------------------------------------------
77 
79 VsiPlugin::~ VsiPlugin()
80 {
81  if (context_)
82  delete context_;
83  if (baseWidget_)
84  delete baseWidget_;
85 }
86 
87 //------------------------------------------------------------------------------
88 
91 {
92  if (OpenFlipper::Options::nogui ())
93  return;
94 
95  QMenu *scriptingMenu;
96 
97  emit emit getMenubarMenu(tr("&Scripting"), scriptingMenu, true );
98 
99  QAction* showEditor = scriptingMenu->addAction ("Visual script editor");
100  showEditor->setIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"vsi_vsiEditor.png") );
101 
102  connect (showEditor, SIGNAL(triggered()) ,
103  this , SLOT(showScriptEditor()));
104 
105  emit addMenubarAction(showEditor, TOOLSMENU );
106 
107 }
108 
109 //------------------------------------------------------------------------------
110 
113 {
114  initContext ();
115  if (!baseWidget_)
116  {
117  baseWidget_ = VSI::BaseWidget::createBaseWidget (context_);
118  connect (baseWidget_, SIGNAL (codeToScriptEditor (QString)),
119  this, SLOT (showInScriptEditor(QString)));
120  }
121 
122  baseWidget_->show ();
123 }
124 
127 {
128  if (context_)
129  return;
130 
131  QScriptEngine *engine;
132  emit getScriptingEngine (engine);
133 
134  // empty context
135  context_ = new VSI::Context (engine);
136 
137  // parse all metadata xml files
138  QDir dir = OpenFlipper::Options::dataDir ();
139 
140  dir.cd ("VsiMetadata");
141 
142  if (!dir.exists ())
143  return;
144 
145  foreach (QString sub, dir.entryList(QDir::Dirs))
146  {
147  if (sub == "..")
148  continue;
149 
150  QDir subdir = dir;
151  subdir.cd (sub);
152  subdir.makeAbsolute();
153 
154  foreach (QString file, subdir.entryList (QStringList("*.xml"), QDir::Files))
155  {
156  QFile f (subdir.filePath (file));
157  if (!f.open (QIODevice::ReadOnly))
158  continue;
159 
160  context_->parse (f.readAll ());
161  }
162  }
163 
164 }
165 
166 //------------------------------------------------------------------------------
167 
169 QScriptValue VsiPlugin::askForInputs(QString _element, QString _inputs)
170 {
171  initContext ();
172 
173  VSI::Element *e = context_->element (_element);
174 
175  if (!e)
176  return QScriptValue ();
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 QScriptValue ();
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 script = "inputs = { ";
204 
205 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
206  foreach (QString s, _inputs.split (",", QString::SkipEmptyParts))
207  script += s + ": " + results[s] + ",";
208 #else
209  foreach (QString s, _inputs.split (",", Qt::SkipEmptyParts))
210  script += s + ": " + results[s] + ",";
211 #endif
212 
213  script.remove (script.length () - 1, 1);
214 
215  script += "};";
216 
217  context_->scriptEngine ()->pushContext ();
218  QScriptValue rv = context_->scriptEngine ()->evaluate (script);
219  context_->scriptEngine ()->globalObject ().setProperty ("inputs", rv);
220  //rv = context_->scriptEngine ()->globalObject ().property ("inputs");
221  context_->scriptEngine ()->popContext ();
222 
223  return rv;
224 }
225 
226 //------------------------------------------------------------------------------
227 
229 void VsiPlugin::showInScriptEditor(QString _script)
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 
244 void VsiPlugin::messageBox (QString _message)
245 {
246  QMessageBox msgBox;
247  msgBox.setText(_message);
248  msgBox.exec();
249 }
250 
251 //------------------------------------------------------------------------------
252 
254 bool 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 
267 bool 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 
QScriptValue callFunction(QString _plugin, QString _functionName, std::vector< QScriptValue > _parameters)
Call a function provided by a plugin getting multiple parameters.
Definition: RPCWrappers.cc:55
bool questionBox(QString _message)
Scripting function, that displays a Yes/No message box.
Definition: vsiPlugin.cc:254
const QString & name() const
Name.
Definition: inout.hh:70
void initContext()
initalisation
Definition: vsiPlugin.cc:126
static BaseWidget * createBaseWidget(Context *_ctx, QWidget *_parent=NULL)
Singleton constructor.
Definition: baseWidget.cc:455
const QVector< Input * > & inputs() const
Inputs.
Definition: element.hh:94
virtual void addMenubarAction(QAction *_action, QString _name)
Adds an action to the menubar.
QMap< QString, QString > getResults()
Results.
void messageBox(QString _message)
Scripting function, that displays a message box.
Definition: vsiPlugin.cc:244
void parse(QByteArray _xml)
Parse xml content.
Definition: context.cc:182
const QString & shortDescription() const
Short description.
Definition: element.hh:88
void pluginsInitialized()
Register in menubar.
Definition: vsiPlugin.cc:90
Element * element(QString _name)
Returns the element with a given name.
Definition: context.cc:157
QScriptEngine * scriptEngine()
Return script engine pointer.
Definition: context.hh:107
bool continueBox(QString _message)
Shows a non blocking stop box for use inside loops.
Definition: vsiPlugin.cc:267
VsiPlugin()
Constructor.
Definition: vsiPlugin.cc:70
QScriptValue askForInputs(QString _element, QString _inputs)
Scripting function, that allows to ask the user for inputs during script execution.
Definition: vsiPlugin.cc:169
void showInScriptEditor(QString _script)
Opens the text based script editor with the given script.
Definition: vsiPlugin.cc:229
void showScriptEditor()
Shows visual script editor.
Definition: vsiPlugin.cc:112