51 #include <QVBoxLayout> 52 #include <QPushButton> 55 #include <QFileDialog> 58 #include <QMessageBox> 60 #include <QApplication> 62 #include <QScriptEngine> 64 #include <QDomDocument> 67 #include <QXmlResultItems> 71 #include "baseWidget.hh" 72 #include "toolBoxElement.hh" 73 #include "scene/sceneElement.hh" 75 #include "parser/context.hh" 87 BaseWidget * BaseWidget::base_ = NULL;
92 BaseWidget::BaseWidget (Context *_ctx, QWidget *_parent) :
93 QMainWindow (_parent),
96 changedContent_ (false)
98 setWindowIcon (OpenFlipper::Options::OpenFlipperIcon ());
101 splitter_ =
new QSplitter (Qt::Horizontal,
this);
102 toolbox_ =
new QToolBox ();
103 views_ =
new QStackedWidget ();
106 toolbox_->setMinimumWidth (275);
108 QVBoxLayout *layout =
new QVBoxLayout;
110 layout->addWidget (toolbox_);
112 QPushButton *execute =
new QPushButton (
"Execute");
113 layout->addWidget (execute);
115 QWidget *w =
new QWidget;
116 w->setLayout (layout);
118 mainScene_ =
new GraphicsScene (_ctx);
119 views_->addWidget(mainScene_->graphicsView ());
121 splitter_->addWidget (w);
122 splitter_->addWidget (views_);
124 sizes << 275 << 10000;
125 splitter_->setSizes (sizes);
127 setCentralWidget (splitter_);
133 connect (execute, SIGNAL (clicked (
bool)),
this, SLOT (executeCode ()));
135 connect (mainScene_, SIGNAL (contentChanged()),
this, SLOT (contentChanged()));
138 QMenu *menu =
new QMenu (tr(
"&File"));
141 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"add-empty-object.png");
142 menu->addAction (icon, tr(
"New"),
this, SLOT (newFile()), QKeySequence::New);
144 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"document-open.png");
145 menu->addAction (icon, tr(
"Open ..."),
this, SLOT (load()), QKeySequence::Open);
147 menu->addSeparator ();
149 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"document-save.png");
150 menu->addAction (icon, tr(
"Save"),
this, SLOT (save()), QKeySequence::Save);
152 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"document-save-as.png");
153 menu->addAction (icon, tr(
"Save as ..."),
this, SLOT (saveAs()), QKeySequence::SaveAs);
154 menu->addSeparator ();
156 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"window-close.png");
157 menu->addAction (icon, tr(
"Close"),
this, SLOT (close()), QKeySequence::Close);
159 menuBar()->addMenu (menu);
161 menu =
new QMenu (tr(
"&Script"));
162 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"scriptEditor.png");
163 menu->addAction (icon, tr(
"Open in script editor"),
this, SLOT (codeToScript()));
165 menuBar()->addMenu (menu);
173 BaseWidget::~BaseWidget ()
181 void BaseWidget::setupUi ()
184 foreach (
const QString &c, ctx_->categories ())
186 QWidget *widget =
new QWidget (toolbox_);
187 QVBoxLayout *layout =
new QVBoxLayout ();
188 foreach (
Element *e, ctx_->elements (c))
190 if (!(e->
flags () & ELEMENT_FLAG_SKIP_TOOLBOX))
193 layout->addWidget (t);
196 layout->addStretch ();
197 widget->setLayout (layout);
198 toolbox_->addItem (widget, c);
205 void BaseWidget::executeCode ()
209 QString code = mainScene_->generateCode (errors);
211 if (errors.isEmpty ())
213 ctx_->scriptEngine ()->pushContext ();
214 ctx_->scriptEngine ()->evaluate (code);
215 ctx_->scriptEngine ()->popContext ();
219 errors =
"<h3> " + tr(
"Following Elements could not be processed:") +
"</h3>" + errors;
221 QMessageBox::warning (
this, tr(
"Error during code generation"), errors,
222 QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
228 void BaseWidget::codeToScript()
233 QString code = mainScene_->generateCode (errors);
235 if (errors.isEmpty ())
237 emit codeToScriptEditor (code);
241 errors =
"<qt> " + tr(
"Following Elements could not be processed:") + errors +
"</qt>";
243 QMessageBox::warning (
this, tr(
"Error during code generation"), errors,
244 QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
250 bool BaseWidget::saveAs()
260 if (!saveIfChanged ())
263 QString filename = QFileDialog::getOpenFileName (
this,
264 tr(
"Load Visual Script"),
266 tr(
"Visual Script File (*.ofvs)"));
268 if (filename.isEmpty ())
272 QFileInfo fileInfo(filename);
276 if (!f.open (QFile::ReadOnly))
279 msgBox.setText(tr(
"Unable to open file."));
280 msgBox.setInformativeText(filename);
285 while (!scenes_.isEmpty())
287 views_->setCurrentWidget (mainScene_->graphicsView());
292 query.setQuery (
"VisualScript");
294 QXmlResultItems root;
296 if (query.isValid ())
298 query.evaluateTo (&root);
300 QXmlItem item (root.next ());
305 mainScene_->loadFromXml (q);
309 changedContent_ =
false;
310 fileName_ = filename;
317 bool BaseWidget::save(
bool _newName)
322 if (fileName_.isEmpty () || _newName)
324 QFileDialog *d =
new QFileDialog (
this, tr(
"Save Visual Script"),
326 tr(
"Visual Script File (*.ofvs)"));
328 d->setAcceptMode (QFileDialog::AcceptSave);
329 d->setDefaultSuffix (
"ofvs");
331 if (QDialog::Accepted == d->exec ())
332 filename = d->selectedFiles ()[0];
335 filename = fileName_;
337 if (filename.isEmpty ())
341 QFileInfo fileInfo(filename);
345 if (!f.open (QFile::WriteOnly))
348 msgBox.setText(tr(
"Unable to write file."));
349 msgBox.setInformativeText(filename);
354 QDomDocument doc(
"VisualScript");
355 QDomElement root = doc.createElement(
"VisualScript");
356 doc.appendChild(root);
358 mainScene_->saveToXml (doc, root);
360 f.write (doc.toString().toUtf8 ());
363 changedContent_ =
false;
364 fileName_ = filename;
373 void BaseWidget::newFile()
375 if (!saveIfChanged ())
378 while (!scenes_.isEmpty())
380 views_->setCurrentWidget (mainScene_->graphicsView());
382 fileName_ = QString ();
383 mainScene_->clearScene ();
384 changedContent_ =
false;
391 void BaseWidget::updateTitle()
393 if (fileName_.isEmpty ())
394 setWindowTitle (tr(
"Untitled") + (changedContent_ ?
" [" + tr(
"Modified") +
"] " :
"") +
" - Visual Script Editor");
396 setWindowTitle (fileName_ + (changedContent_ ?
" [" + tr(
"Modified") +
"] " :
"") +
" - Visual Script Editor");
402 void BaseWidget::contentChanged()
404 if (!changedContent_)
406 changedContent_ =
true;
414 bool BaseWidget::saveIfChanged()
419 msgBox.setText(tr(
"The visual script has been modified."));
420 msgBox.setInformativeText(tr(
"Do you want to save your changes?"));
421 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
422 msgBox.setDefaultButton(QMessageBox::Save);
423 int ret = msgBox.exec();
425 case QMessageBox::Save:
430 case QMessageBox::Discard:
433 case QMessageBox::Cancel:
450 void BaseWidget::closeEvent(QCloseEvent *_event)
452 if (!saveIfChanged ())
481 scenes_.push (_scene);
490 if (!scenes_.isEmpty())
493 if (!scenes_.isEmpty())
494 views_->setCurrentWidget (scenes_.top ()->graphicsView());
496 views_->setCurrentWidget (mainScene_->graphicsView());
unsigned int flags() const
Flags.
void pushScene(GraphicsScene *_scene)
show a new scene (function) in editor
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
GraphicsView * graphicsView()
Graphics view of the scene.
void removeScene(GraphicsScene *_scene)
remove a scene
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
void addScene(GraphicsScene *_scene)
add a new scene
void popScene()
go back to last scene (function)