From e4c25bb0bb7e45d401d6c03e7348915086bb53aa Mon Sep 17 00:00:00 2001 From: Guido Ranzuglia granzuglia Date: Fri, 20 Aug 2010 08:52:50 +0000 Subject: [PATCH] first code sketch to support scripting engine --- src/meshlab/filterScriptDialog.cpp | 39 ++++++++++++++++++++ src/meshlab/filterScriptDialog.h | 18 ++++++++++ src/meshlab/mainwindow.h | 10 ++++++ src/meshlab/mainwindow_Init.cpp | 7 +++- src/meshlab/mainwindow_RunTime.cpp | 57 +++++++++++++++++++----------- src/meshlab/meshlab.pro | 5 ++- 6 files changed, 114 insertions(+), 22 deletions(-) diff --git a/src/meshlab/filterScriptDialog.cpp b/src/meshlab/filterScriptDialog.cpp index f392ea772..c2109ff1c 100644 --- a/src/meshlab/filterScriptDialog.cpp +++ b/src/meshlab/filterScriptDialog.cpp @@ -24,6 +24,7 @@ #include #include "ui_filterScriptDialog.h" +#include "ui_scriptEditor.h" #include "filterScriptDialog.h" #include "mainwindow.h" @@ -235,3 +236,41 @@ FilterScriptDialog::~FilterScriptDialog() } + +EditorScriptDialog::EditorScriptDialog( QWidget *parent /*= 0*/ ) +:code() +{ + ui = new Ui::scriptEditor(); + EditorScriptDialog::ui->setupUi(this); + connect(ui->okButton, SIGNAL(clicked()), this, SLOT(applyScript())); + connect(ui->saveScriptButton, SIGNAL(clicked()), this, SLOT(saveScript())); + connect(ui->openScriptButton, SIGNAL(clicked()), this, SLOT(openScript())); +} + +EditorScriptDialog::~EditorScriptDialog() +{ + delete ui; +} + +void EditorScriptDialog::applyScript() +{ + code.clear(); + code = ui->textEditor->toPlainText(); + accept(); +} + + +void EditorScriptDialog::saveScript() +{ + +} + +void EditorScriptDialog::openScript() +{ + +} + +const QString& EditorScriptDialog::scriptCode() const +{ + return code; +} \ No newline at end of file diff --git a/src/meshlab/filterScriptDialog.h b/src/meshlab/filterScriptDialog.h index ad34237d8..dd3b90990 100644 --- a/src/meshlab/filterScriptDialog.h +++ b/src/meshlab/filterScriptDialog.h @@ -26,6 +26,7 @@ namespace Ui { class scriptDialog; + class scriptEditor; } class FilterScriptDialog : public QDialog @@ -62,3 +63,20 @@ private: FilterScript *scriptPtr; }; + +class EditorScriptDialog : public QDialog +{ + Q_OBJECT +public: + EditorScriptDialog(QWidget *parent = 0); + ~EditorScriptDialog(); + private slots: + void applyScript(); + void saveScript(); + void openScript(); + public: + const QString& scriptCode() const; +private: + Ui::scriptEditor* ui; + QString code; +}; \ No newline at end of file diff --git a/src/meshlab/mainwindow.h b/src/meshlab/mainwindow.h index e28699ce5..3c11c58c2 100644 --- a/src/meshlab/mainwindow.h +++ b/src/meshlab/mainwindow.h @@ -27,6 +27,7 @@ //None of this should happen if we are compiling c, not c++ #ifdef __cplusplus #include +#include #include #include @@ -93,6 +94,7 @@ private slots: void applyLastFilter(); void runFilterScript(); void showFilterScript(); + void showScriptEditor(); void showTooltip(QAction*); /////////// Slot Menu Render ///////////////////// void renderBbox(); @@ -185,6 +187,13 @@ private: LayerDialog *layerDialog; QSignalMapper *windowMapper; + + + /******************************************************************REMOVE IT!!!!************************************************/ + QScriptEngine eng; + /******************************************************************REMOVE IT!!!!************************************************/ + + PluginManager PM; /* @@ -304,6 +313,7 @@ private: QAction *lastFilterAct; QAction *runFilterScriptAct; QAction *showFilterScriptAct; + QAction* showScriptEditAct; QAction *recentFileActs[MAXRECENTFILES]; QAction *separatorAct; QAction *exitAct; diff --git a/src/meshlab/mainwindow_Init.cpp b/src/meshlab/mainwindow_Init.cpp index b9a21ed34..579a03da2 100644 --- a/src/meshlab/mainwindow_Init.cpp +++ b/src/meshlab/mainwindow_Init.cpp @@ -64,7 +64,7 @@ MainWindow::MainWindow() icon.addPixmap(QPixmap(":images/eye48.png")); setWindowIcon(icon); - PM.loadPlugins(defaultGlobalParams); + PM.loadPlugins(defaultGlobalParams,&eng); // Now load from the registry the settings and merge the hardwired values got from the PM.loadPlugins with the ones found in the registry. loadMeshLabSettings(); createActions(); @@ -318,6 +318,10 @@ void MainWindow::createActions() showFilterScriptAct->setEnabled(true); connect(showFilterScriptAct, SIGNAL(triggered()), this, SLOT(showFilterScript())); + showScriptEditAct = new QAction(tr("Script Editor"),this); + showScriptEditAct->setEnabled(true); + connect(showScriptEditAct, SIGNAL(triggered()), this, SLOT(showScriptEditor())); + //////////////Action Menu Preferences ///////////////////////////////////////////////////////////////////// setCustomizeAct = new QAction(tr("&Options..."),this); connect(setCustomizeAct, SIGNAL(triggered()), this, SLOT(setCustomize())); @@ -430,6 +434,7 @@ void MainWindow::createMenus() filterMenu = menuBar()->addMenu(tr("Fi<ers")); filterMenu->addAction(lastFilterAct); filterMenu->addAction(showFilterScriptAct); + filterMenu->addAction(showScriptEditAct); filterMenu->addSeparator(); filterMenuSelect = filterMenu->addMenu(tr("Selection")); filterMenuClean = filterMenu->addMenu(tr("Cleaning and Repairing")); diff --git a/src/meshlab/mainwindow_RunTime.cpp b/src/meshlab/mainwindow_RunTime.cpp index cd1496401..b6bddc35a 100644 --- a/src/meshlab/mainwindow_RunTime.cpp +++ b/src/meshlab/mainwindow_RunTime.cpp @@ -587,6 +587,24 @@ void MainWindow::showFilterScript() } +void MainWindow::showScriptEditor() +{ + EditorScriptDialog dialog(this); + if (dialog.exec()==QDialog::Accepted) + { + QScriptValue val = eng.newQObject(meshDoc()); + eng.globalObject().setProperty("md",val); + QScriptValue result = eng.evaluate(dialog.scriptCode()); + if (result.isError()) + { + meshDoc()->Log.Logf(GLLogStream::SYSTEM,"Interpreter Error: line %i: %s",result.property("lineNumber").toInt32(),qPrintable(result.toString())); + layerDialog->updateLog(meshDoc()->Log); + } + else + this->updateGL(); + } +} + void MainWindow::runFilterScript() { FilterScript::iterator ii; @@ -940,24 +958,6 @@ void MainWindow::applyDecorateMode() } -bool MainWindow::QCallBack(const int pos, const char * str) -{ - int static lastPos=-1; - if(pos==lastPos) return true; - lastPos=pos; - - static QTime currTime; - if(currTime.elapsed()< 100) return true; - currTime.start(); - MainWindow::globalStatusBar()->showMessage(str,5000); - qb->show(); - qb->setEnabled(true); - qb->setValue(pos); - MainWindow::globalStatusBar()->update(); - qApp->processEvents(); - return true; -} - void MainWindow::setLight() { GLA()->setLight(!GLA()->getCurrentRenderMode().lighting); @@ -1087,11 +1087,10 @@ bool MainWindow::open(QString fileName, GLArea *gla) { // Opening files in a transparent form (IO plugins contribution is hidden to user) QStringList filters; - + // HashTable storing all supported formats together with // the (1-based) index of first plugin which is able to open it QHash allKnownFormats; - PM.LoadFormats(filters, allKnownFormats,PluginManager::IMPORT); filters.push_back("ALN project ( *.aln)"); filters.front().chop(1); @@ -1496,3 +1495,21 @@ void MainWindow::keyPressEvent(QKeyEvent *e) } else e->ignore(); } + +bool MainWindow::QCallBack(const int pos, const char * str) +{ + int static lastPos=-1; + if(pos==lastPos) return true; + lastPos=pos; + + static QTime currTime; + if(currTime.elapsed()< 100) return true; + currTime.start(); + MainWindow::globalStatusBar()->showMessage(str,5000); + qb->show(); + qb->setEnabled(true); + qb->setValue(pos); + MainWindow::globalStatusBar()->update(); + qApp->processEvents(); + return true; +} \ No newline at end of file diff --git a/src/meshlab/meshlab.pro b/src/meshlab/meshlab.pro index f30e13b55..05cec94cf 100644 --- a/src/meshlab/meshlab.pro +++ b/src/meshlab/meshlab.pro @@ -52,7 +52,8 @@ FORMS = ui/layerDialog.ui \ ui/aboutDialog.ui \ ui/renametexture.ui \ ui/savemaskexporter.ui \ - ui/congratsDialog.ui + ui/congratsDialog.ui \ + ui/scriptEditor.ui RESOURCES = meshlab.qrc # to add windows icon @@ -71,6 +72,8 @@ ICON = images/meshlab.icns QT += opengl QT += xml QT += network +QT += script + # the following line is needed to avoid mismatch between # the awful min/max macros of windows and the limits max