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  foreach (QString s, _inputs.split (",", QString::SkipEmptyParts))
181  foreach (VSI::Input *i, e->inputs ())
182  {
183  if (i->name () == s)
184  {
185  inputs.append (i);
186  break;
187  }
188  }
189 
190  if (inputs.isEmpty ())
191  return QScriptValue ();
192 
193  VSI::DynamicDialog d (inputs);
194  d.setWindowTitle (e->shortDescription () + " Input Configuration");
195  d.exec ();
196 
197  QMap<QString, QString> results = d.getResults ();
198 
199  QString script = "inputs = { ";
200 
201  foreach (QString s, _inputs.split (",", QString::SkipEmptyParts))
202  script += s + ": " + results[s] + ",";
203 
204  script.remove (script.length () - 1, 1);
205 
206  script += "};";
207 
208  context_->scriptEngine ()->pushContext ();
209  QScriptValue rv = context_->scriptEngine ()->evaluate (script);
210  context_->scriptEngine ()->globalObject ().setProperty ("inputs", rv);
211  //rv = context_->scriptEngine ()->globalObject ().property ("inputs");
212  context_->scriptEngine ()->popContext ();
213 
214  return rv;
215 }
216 
217 //------------------------------------------------------------------------------
218 
220 void VsiPlugin::showInScriptEditor(QString _script)
221 {
222  bool ok;
223 
224  emit functionExists ("scripting", "showScriptInEditor(QString)", ok);
225 
226  if (!ok)
227  return;
228 
229  RPC::callFunction ("scripting", "showScriptInEditor", _script);
230 }
231 
232 //------------------------------------------------------------------------------
233 
235 void VsiPlugin::messageBox (QString _message)
236 {
237  QMessageBox msgBox;
238  msgBox.setText(_message);
239  msgBox.exec();
240 }
241 
242 //------------------------------------------------------------------------------
243 
245 bool VsiPlugin::questionBox (QString _message)
246 {
247  QMessageBox msgBox;
248  msgBox.setText(_message);
249  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
250  msgBox.setDefaultButton(QMessageBox::Yes);
251  int ret = msgBox.exec();
252  if (ret == QMessageBox::Yes)
253  return true;
254  return false;
255 }
256 
258 bool VsiPlugin::continueBox (QString _message)
259 {
260  static QContinueBox* msgBox = NULL;
261 
262  if ( !msgBox ) {
263  msgBox = new QContinueBox(_message);
264  msgBox->show();
265  } else {
266 
267  if ( msgBox->continueBox() ) {
268  return true;
269  } else {
270  delete msgBox;
271  msgBox = NULL;
272  return false;
273  }
274  }
275 
276  return true;
277 }
278 
279 //------------------------------------------------------------------------------
280 
const QString & name() const
Name.
Definition: inout.hh: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 initContext()
initalisation
Definition: vsiPlugin.cc:126
void messageBox(QString _message)
Scripting function, that displays a message box.
Definition: vsiPlugin.cc:235
QMap< QString, QString > getResults()
Results.
void showInScriptEditor(QString _script)
Opens the text based script editor with the given script.
Definition: vsiPlugin.cc:220
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 continueBox(QString _message)
Shows a non blocking stop box for use inside loops.
Definition: vsiPlugin.cc:258
void showScriptEditor()
Shows visual script editor.
Definition: vsiPlugin.cc:112
#define TOOLSMENU
The Menu will be added inside the Tools Menu.
void pluginsInitialized()
Register in menubar.
Definition: vsiPlugin.cc:90
VsiPlugin()
Constructor.
Definition: vsiPlugin.cc:70
static BaseWidget * createBaseWidget(Context *_ctx, QWidget *_parent=NULL)
Singleton constructor.
Definition: baseWidget.cc:455
bool questionBox(QString _message)
Scripting function, that displays a Yes/No message box.
Definition: vsiPlugin.cc:245
void parse(QByteArray _xml)
Parse xml content.
Definition: context.cc:182
virtual void addMenubarAction(QAction *_action, QString _name)
Adds an action to the menubar.
const QVector< Input * > & inputs() const
Inputs.
Definition: element.hh:94
QScriptEngine * scriptEngine()
Return script engine pointer.
Definition: context.hh:107
Element * element(QString _name)
Returns the element with a given name.
Definition: context.cc:157
const QString & shortDescription() const
Short description.
Definition: element.hh:88