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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //=============================================================================
51 //
52 // CLASS VsiPlugin - IMPLEMENTATION
53 //
54 //=============================================================================
55 
56 //== INCLUDES =================================================================
57 
58 #include <QAction>
59 #include <QScriptEngine>
60 #include <QMessageBox>
61 
63 
65 
66 #include "vsiPlugin.hh"
67 
68 #include "baseWidget.hh"
69 #include "parser/context.hh"
70 #include "parser/input.hh"
71 #include "config/dynamicDialog.hh"
72 
73 //------------------------------------------------------------------------------
74 
77  context_ (0),
78  baseWidget_ (0)
79 {
80 }
81 
82 //------------------------------------------------------------------------------
83 
86 {
87  if (context_)
88  delete context_;
89  if (baseWidget_)
90  delete baseWidget_;
91 }
92 
93 //------------------------------------------------------------------------------
94 
97 {
98  if (OpenFlipper::Options::nogui ())
99  return;
100 
101  QMenu *scriptingMenu;
102 
103  emit emit getMenubarMenu(tr("&Scripting"), scriptingMenu, true );
104 
105  QAction* showEditor = scriptingMenu->addAction ("Visual script editor");
106  showEditor->setIcon( QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"vsi_vsiEditor.png") );
107 
108  connect (showEditor, SIGNAL(triggered()) ,
109  this , SLOT(showScriptEditor()));
110 
111  emit addMenubarAction(showEditor, TOOLSMENU );
112 
113 }
114 
115 //------------------------------------------------------------------------------
116 
119 {
120  initContext ();
121  if (!baseWidget_)
122  {
123  baseWidget_ = VSI::BaseWidget::createBaseWidget (context_);
124  connect (baseWidget_, SIGNAL (codeToScriptEditor (QString)),
125  this, SLOT (showInScriptEditor(QString)));
126  }
127 
128  baseWidget_->show ();
129 }
130 
133 {
134  if (context_)
135  return;
136 
137  QScriptEngine *engine;
138  emit getScriptingEngine (engine);
139 
140  // empty context
141  context_ = new VSI::Context (engine);
142 
143  // parse all metadata xml files
144  QDir dir = OpenFlipper::Options::dataDir ();
145 
146  dir.cd ("VsiMetadata");
147 
148  if (!dir.exists ())
149  return;
150 
151  foreach (QString sub, dir.entryList(QDir::Dirs))
152  {
153  if (sub == "..")
154  continue;
155 
156  QDir subdir = dir;
157  subdir.cd (sub);
158  subdir.makeAbsolute();
159 
160  foreach (QString file, subdir.entryList (QStringList("*.xml"), QDir::Files))
161  {
162  QFile f (subdir.filePath (file));
163  if (!f.open (QIODevice::ReadOnly))
164  continue;
165 
166  context_->parse (f.readAll ());
167  }
168  }
169 
170 }
171 
172 //------------------------------------------------------------------------------
173 
175 QScriptValue VsiPlugin::askForInputs(QString _element, QString _inputs)
176 {
177  initContext ();
178 
179  VSI::Element *e = context_->element (_element);
180 
181  if (!e)
182  return QScriptValue ();
183 
184  QVector<VSI::Input *> inputs;
185 
186  foreach (QString s, _inputs.split (",", QString::SkipEmptyParts))
187  foreach (VSI::Input *i, e->inputs ())
188  {
189  if (i->name () == s)
190  {
191  inputs.append (i);
192  break;
193  }
194  }
195 
196  if (inputs.isEmpty ())
197  return QScriptValue ();
198 
199  VSI::DynamicDialog d (inputs);
200  d.setWindowTitle (e->shortDescription () + " Input Configuration");
201  d.exec ();
202 
203  QMap<QString, QString> results = d.getResults ();
204 
205  QString script = "inputs = { ";
206 
207  foreach (QString s, _inputs.split (",", QString::SkipEmptyParts))
208  script += s + ": " + results[s] + ",";
209 
210  script.remove (script.length () - 1, 1);
211 
212  script += "};";
213 
214  context_->scriptEngine ()->pushContext ();
215  QScriptValue rv = context_->scriptEngine ()->evaluate (script);
216  context_->scriptEngine ()->globalObject ().setProperty ("inputs", rv);
217  //rv = context_->scriptEngine ()->globalObject ().property ("inputs");
218  context_->scriptEngine ()->popContext ();
219 
220  return rv;
221 }
222 
223 //------------------------------------------------------------------------------
224 
226 void VsiPlugin::showInScriptEditor(QString _script)
227 {
228  bool ok;
229 
230  emit functionExists ("scripting", "showScriptInEditor(QString)", ok);
231 
232  if (!ok)
233  return;
234 
235  RPC::callFunction ("scripting", "showScriptInEditor", _script);
236 }
237 
238 //------------------------------------------------------------------------------
239 
241 void VsiPlugin::messageBox (QString _message)
242 {
243  QMessageBox msgBox;
244  msgBox.setText(_message);
245  msgBox.exec();
246 }
247 
248 //------------------------------------------------------------------------------
249 
251 bool VsiPlugin::questionBox (QString _message)
252 {
253  QMessageBox msgBox;
254  msgBox.setText(_message);
255  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
256  msgBox.setDefaultButton(QMessageBox::Yes);
257  int ret = msgBox.exec();
258  if (ret == QMessageBox::Yes)
259  return true;
260  return false;
261 }
262 
264 bool VsiPlugin::continueBox (QString _message)
265 {
266  static QContinueBox* msgBox = NULL;
267 
268  if ( !msgBox ) {
269  msgBox = new QContinueBox(_message);
270  msgBox->show();
271  } else {
272 
273  if ( msgBox->continueBox() ) {
274  return true;
275  } else {
276  delete msgBox;
277  msgBox = NULL;
278  return false;
279  }
280  }
281 
282  return true;
283 }
284 
285 //------------------------------------------------------------------------------
286 
287 #if QT_VERSION < 0x050000
288  Q_EXPORT_PLUGIN2(Vsiplugin,VsiPlugin)
289 #endif
const QVector< Input * > & inputs() const
Inputs.
Definition: element.hh:100
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:461
void showInScriptEditor(QString _script)
Opens the text based script editor with the given script.
Definition: vsiPlugin.cc:226
const QString & shortDescription() const
Short description.
Definition: element.hh:94
#define TOOLSMENU
The Menu will be added inside the Tools Menu.
bool continueBox(QString _message)
Shows a non blocking stop box for use inside loops.
Definition: vsiPlugin.cc:264
VsiPlugin()
Constructor.
Definition: vsiPlugin.cc:76
void parse(QByteArray _xml)
Parse xml content.
Definition: context.cc:188
Element * element(QString _name)
Returns the element with a given name.
Definition: context.cc:163
const QString & name() const
Name.
Definition: inout.hh:76
void messageBox(QString _message)
Scripting function, that displays a message box.
Definition: vsiPlugin.cc:241
QScriptEngine * scriptEngine()
Return script engine pointer.
Definition: context.hh:113
~VsiPlugin()
Destructor.
Definition: vsiPlugin.cc:85
void showScriptEditor()
Shows visual script editor.
Definition: vsiPlugin.cc:118
void pluginsInitialized()
Register in menubar.
Definition: vsiPlugin.cc:96
bool questionBox(QString _message)
Scripting function, that displays a Yes/No message box.
Definition: vsiPlugin.cc:251
void initContext()
initalisation
Definition: vsiPlugin.cc:132
QMap< QString, QString > getResults()
Results.
QScriptValue askForInputs(QString _element, QString _inputs)
Scripting function, that allows to ask the user for inputs during script execution.
Definition: vsiPlugin.cc:175
QScriptValue callFunction(QString _plugin, QString _functionName, std::vector< QScriptValue > _parameters)
Call a function provided by a plugin getting multiple parameters.
Definition: RPCWrappers.cc:63