45 #include <QVBoxLayout> 46 #include <QPushButton> 49 #include <QFileDialog> 52 #include <QMessageBox> 54 #include <QApplication> 56 #include <QScriptEngine> 58 #include <QDomDocument> 61 #include <QXmlResultItems> 65 #include "baseWidget.hh" 66 #include "toolBoxElement.hh" 67 #include "scene/sceneElement.hh" 69 #include "parser/context.hh" 81 BaseWidget * BaseWidget::base_ = NULL;
86 BaseWidget::BaseWidget (Context *_ctx, QWidget *_parent) :
87 QMainWindow (_parent),
90 changedContent_ (false)
92 setWindowIcon (OpenFlipper::Options::OpenFlipperIcon ());
95 splitter_ =
new QSplitter (Qt::Horizontal,
this);
96 toolbox_ =
new QToolBox ();
97 views_ =
new QStackedWidget ();
100 toolbox_->setMinimumWidth (275);
102 QVBoxLayout *layout =
new QVBoxLayout;
104 layout->addWidget (toolbox_);
106 QPushButton *execute =
new QPushButton (
"Execute");
107 layout->addWidget (execute);
109 QWidget *w =
new QWidget;
110 w->setLayout (layout);
112 mainScene_ =
new GraphicsScene (_ctx);
113 views_->addWidget(mainScene_->graphicsView ());
115 splitter_->addWidget (w);
116 splitter_->addWidget (views_);
118 sizes << 275 << 10000;
119 splitter_->setSizes (sizes);
121 setCentralWidget (splitter_);
127 connect (execute, SIGNAL (clicked (
bool)),
this, SLOT (executeCode ()));
129 connect (mainScene_, SIGNAL (contentChanged()),
this, SLOT (contentChanged()));
132 QMenu *menu =
new QMenu (tr(
"&File"));
135 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"add-empty-object.png");
136 menu->addAction (icon, tr(
"New"),
this, SLOT (newFile()), QKeySequence::New);
138 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"document-open.png");
139 menu->addAction (icon, tr(
"Open ..."),
this, SLOT (load()), QKeySequence::Open);
141 menu->addSeparator ();
143 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"document-save.png");
144 menu->addAction (icon, tr(
"Save"),
this, SLOT (save()), QKeySequence::Save);
146 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"document-save-as.png");
147 menu->addAction (icon, tr(
"Save as ..."),
this, SLOT (saveAs()), QKeySequence::SaveAs);
148 menu->addSeparator ();
150 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"window-close.png");
151 menu->addAction (icon, tr(
"Close"),
this, SLOT (close()), QKeySequence::Close);
153 menuBar()->addMenu (menu);
155 menu =
new QMenu (tr(
"&Script"));
156 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"scriptEditor.png");
157 menu->addAction (icon, tr(
"Open in script editor"),
this, SLOT (codeToScript()));
159 menuBar()->addMenu (menu);
167 BaseWidget::~BaseWidget ()
175 void BaseWidget::setupUi ()
178 foreach (
const QString &c, ctx_->categories ())
180 QWidget *widget =
new QWidget (toolbox_);
181 QVBoxLayout *layout =
new QVBoxLayout ();
182 foreach (
Element *e, ctx_->elements (c))
184 if (!(e->
flags () & ELEMENT_FLAG_SKIP_TOOLBOX))
187 layout->addWidget (t);
190 layout->addStretch ();
191 widget->setLayout (layout);
192 toolbox_->addItem (widget, c);
199 void BaseWidget::executeCode ()
203 QString code = mainScene_->generateCode (errors);
205 if (errors.isEmpty ())
207 ctx_->scriptEngine ()->pushContext ();
208 ctx_->scriptEngine ()->evaluate (code);
209 ctx_->scriptEngine ()->popContext ();
213 errors =
"<h3> " + tr(
"Following Elements could not be processed:") +
"</h3>" + errors;
215 QMessageBox::warning (
this, tr(
"Error during code generation"), errors,
216 QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
222 void BaseWidget::codeToScript()
227 QString code = mainScene_->generateCode (errors);
229 if (errors.isEmpty ())
231 emit codeToScriptEditor (code);
235 errors =
"<qt> " + tr(
"Following Elements could not be processed:") + errors +
"</qt>";
237 QMessageBox::warning (
this, tr(
"Error during code generation"), errors,
238 QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
244 bool BaseWidget::saveAs()
254 if (!saveIfChanged ())
257 QString filename = QFileDialog::getOpenFileName (
this,
258 tr(
"Load Visual Script"),
260 tr(
"Visual Script File (*.ofvs)"));
262 if (filename.isEmpty ())
266 QFileInfo fileInfo(filename);
270 if (!f.open (QFile::ReadOnly))
273 msgBox.setText(tr(
"Unable to open file."));
274 msgBox.setInformativeText(filename);
279 while (!scenes_.isEmpty())
281 views_->setCurrentWidget (mainScene_->graphicsView());
286 query.setQuery (
"VisualScript");
288 QXmlResultItems root;
290 if (query.isValid ())
292 query.evaluateTo (&root);
294 QXmlItem item (root.next ());
299 mainScene_->loadFromXml (q);
303 changedContent_ =
false;
304 fileName_ = filename;
311 bool BaseWidget::save(
bool _newName)
316 if (fileName_.isEmpty () || _newName)
318 QFileDialog *d =
new QFileDialog (
this, tr(
"Save Visual Script"),
320 tr(
"Visual Script File (*.ofvs)"));
322 d->setAcceptMode (QFileDialog::AcceptSave);
323 d->setDefaultSuffix (
"ofvs");
325 if (QDialog::Accepted == d->exec ())
326 filename = d->selectedFiles ()[0];
329 filename = fileName_;
331 if (filename.isEmpty ())
335 QFileInfo fileInfo(filename);
339 if (!f.open (QFile::WriteOnly))
342 msgBox.setText(tr(
"Unable to write file."));
343 msgBox.setInformativeText(filename);
348 QDomDocument doc(
"VisualScript");
349 QDomElement root = doc.createElement(
"VisualScript");
350 doc.appendChild(root);
352 mainScene_->saveToXml (doc, root);
354 f.write (doc.toString().toUtf8 ());
357 changedContent_ =
false;
358 fileName_ = filename;
367 void BaseWidget::newFile()
369 if (!saveIfChanged ())
372 while (!scenes_.isEmpty())
374 views_->setCurrentWidget (mainScene_->graphicsView());
376 fileName_ = QString ();
377 mainScene_->clearScene ();
378 changedContent_ =
false;
385 void BaseWidget::updateTitle()
387 if (fileName_.isEmpty ())
388 setWindowTitle (tr(
"Untitled") + (changedContent_ ?
" [" + tr(
"Modified") +
"] " :
"") +
" - Visual Script Editor");
390 setWindowTitle (fileName_ + (changedContent_ ?
" [" + tr(
"Modified") +
"] " :
"") +
" - Visual Script Editor");
396 void BaseWidget::contentChanged()
398 if (!changedContent_)
400 changedContent_ =
true;
408 bool BaseWidget::saveIfChanged()
413 msgBox.setText(tr(
"The visual script has been modified."));
414 msgBox.setInformativeText(tr(
"Do you want to save your changes?"));
415 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
416 msgBox.setDefaultButton(QMessageBox::Save);
417 int ret = msgBox.exec();
419 case QMessageBox::Save:
424 case QMessageBox::Discard:
427 case QMessageBox::Cancel:
444 void BaseWidget::closeEvent(QCloseEvent *_event)
446 if (!saveIfChanged ())
475 scenes_.push (_scene);
484 if (!scenes_.isEmpty())
487 if (!scenes_.isEmpty())
488 views_->setCurrentWidget (scenes_.top ()->graphicsView());
490 views_->setCurrentWidget (mainScene_->graphicsView());
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
GraphicsView * graphicsView()
Graphics view of the scene.
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
void popScene()
go back to last scene (function)
void pushScene(GraphicsScene *_scene)
show a new scene (function) in editor
void removeScene(GraphicsScene *_scene)
remove a scene
unsigned int flags() const
Flags.
void addScene(GraphicsScene *_scene)
add a new scene