add new formula engine (ELAM based)
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 28 Nov 2010 22:44:33 +0000 (22:44 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 28 Nov 2010 22:44:33 +0000 (22:44 +0000)
some work on using it for calculating prices
some work on template editing

git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@642 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/dialogs/eventedit.cpp
src/dialogs/pricecatdlg.cpp
src/dialogs/pricecatdlg.h
src/misc/formula.cpp [new file with mode: 0644]
src/misc/formula.h [new file with mode: 0644]
src/misc/misc.pri
src/mwin/overview.cpp
src/script/jsengine.cpp
src/script/jsengine.h
src/templates/templatedlg.cpp
src/templates/templatedlg.h

index f45367a..8661775 100644 (file)
@@ -35,6 +35,7 @@
 #include "MTGetEvent"
 #include "centbox.h"
 #include "pricecatdlg.h"
+#include "formula.h"
 
 MEventEditor::MEventEditor(QWidget*pw,qint64 id)
        :QDialog(pw)
@@ -279,7 +280,13 @@ void MEventEditor::addPrice()
        ep.setpricecategory(cat);
        ep.setpricecategoryid(cat.pricecategoryid());
        ep.setflags(cat.flags());
-       //TODO: execute formula
+       //execute formula
+       MElamEngine ee;
+       QList<MOEventPrice> prc=event.price();
+       for(int i=0;i<prc.size();i++){
+               ee.setVariable(prc[i].pricecategory().value().abbreviation(), prc[i].price().value());
+       }
+       ep.setprice(ee.evaluate(cat.formula()).toInt());
        //get local properties
        MEEPriceEdit d(this,ep,capacity->value());
        if(d.exec()!=QDialog::Accepted)return;
index 6863bc8..0b5dcb1 100644 (file)
 
 #include "pricecatdlg.h"
 #include "msinterface.h"
+#include "flagedit.h"
 
 #include <QBoxLayout>
 #include <QFormLayout>
+#include <QLabel>
 #include <QLineEdit>
 #include <QListWidget>
 #include <QMessageBox>
@@ -25,6 +27,7 @@ MPriceCategoryDialog::MPriceCategoryDialog(QWidget*pw,bool showselect)
 {
        m_cat=req->queryGetAllPriceCategories().getpricecategories();
        setWindowTitle(tr("Select a Price Category"));
+       setSizeGripEnabled(true);
        QVBoxLayout*vl;
        setLayout(vl=new QVBoxLayout);
        vl->addWidget(m_list=new QListWidget,10);
@@ -115,25 +118,39 @@ MPCDEdit::MPCDEdit(QWidget*par,const MOPriceCategory&c)
        bool b=cat.pricecategoryid().isNull();
        setWindowTitle(b?tr("New Price Category"):tr("Change Price Category"));
        QVBoxLayout*vl;
+       QHBoxLayout*hl;
+       QPushButton*p;
        setLayout(vl=new QVBoxLayout);
        QFormLayout*fl;
        vl->addLayout(fl=new QFormLayout,1);
        fl->addRow(tr("Category Name:"),name=new QLineEdit(cat.name()));
        fl->addRow(tr("Category Abbreviation:"),abbr=new QLineEdit(cat.abbreviation()));
+       fl->addRow(tr("Formula:"),form=new QLineEdit(cat.formula()));
+       fl->addRow(tr("Flags:"),hl=new QHBoxLayout);
+       hl->addWidget(flags=new QLabel(cat.flags()),1);
+       hl->addWidget(p=new QPushButton("..."));
+       connect(p,SIGNAL(clicked(bool)),this,SLOT(editFlags()));
        vl->addSpacing(10);
-       QHBoxLayout*hl;
        vl->addLayout(hl=new QHBoxLayout,0);
        hl->addStretch(10);
-       QPushButton*p;
        hl->addWidget(p=new QPushButton(b?tr("Create"):tr("Save")),0);
        connect(p,SIGNAL(clicked()),this,SLOT(accept()));
        hl->addWidget(p=new QPushButton(tr("Cancel")),0);
        connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+       setSizeGripEnabled(true);
 }
+
+void MPCDEdit::editFlags()
+{
+       flags->setText(MFlagEditor::edit(this, flags->text(), tr("Flags of price category '%1':").arg(name->text())));
+}
+
 MOPriceCategory MPCDEdit::result()const
 {
        MOPriceCategory rcat(cat);
        rcat.setname(name->text());
        rcat.setabbreviation(abbr->text());
+       rcat.setformula(form->text());
+       rcat.setflags(flags->text());
        return rcat;
 }
index 8012dbe..c583bb4 100644 (file)
@@ -39,6 +39,7 @@ class MPriceCategoryDialog:public QDialog
 };
 
 class QLineEdit;
+class QLabel;
 
 /**helper class: edit a price category*/
 class MPCDEdit:public QDialog
@@ -47,9 +48,12 @@ class MPCDEdit:public QDialog
        public:
                MPCDEdit(QWidget*,const MOPriceCategory&);
                MOPriceCategory result()const;
+       private slots:
+               void editFlags();
        private:
                MOPriceCategory cat;
-               QLineEdit*name,*abbr;
+               QLineEdit*name,*abbr,*form;
+               QLabel*flags;
 };
 
 #endif
diff --git a/src/misc/formula.cpp b/src/misc/formula.cpp
new file mode 100644 (file)
index 0000000..e70afd5
--- /dev/null
@@ -0,0 +1,62 @@
+//
+// C++ Implementation: magicsmoke ELAM adaptation
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2010
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "formula.h"
+#include "misc.h"
+
+#include <QDebug>
+
+using namespace ELAM;
+
+static QVariant toMoneyFunc(const QList<QVariant>&lf,bool local)
+{
+       if(lf.size()!=1)
+               return Exception(Exception::ArgumentListError, "expecting exactly one argument");
+       if(!lf[0].canConvert<qlonglong>())
+               return Exception(Exception::TypeMismatchError,"cannot convert to int");
+       return cent2str(lf[0].toLongLong(),local);
+}
+static QVariant toMoneyFuncLoc(const QList<QVariant>&lf){return toMoneyFunc(lf,true);}
+static QVariant toMoneyFuncNLoc(const QList<QVariant>&lf){return toMoneyFunc(lf,false);}
+
+static QVariant fromMoneyFunc(const QList<QVariant>&lf,bool local)
+{
+       if(lf.size()!=1)
+               return Exception(Exception::ArgumentListError, "expecting exactly one argument");
+       if(!lf[0].canConvert<QString>())
+               return Exception(Exception::TypeMismatchError,"cannot convert to string");
+       return str2cent(lf[0].toString(),local);
+}
+static QVariant fromMoneyFuncLoc(const QList<QVariant>&lf){return fromMoneyFunc(lf,true);}
+static QVariant fromMoneyFuncNLoc(const QList<QVariant>&lf){return fromMoneyFunc(lf,false);}
+
+MElamEngine::MElamEngine(QObject* parent): Engine(parent)
+{
+       //configure character classes
+       QString alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
+       characterClasses().setNameClass(alpha,alpha+"0123456789.:");
+       if(!characterClasses().isConsistent())
+               qDebug()<<"Warning: built-in calculator is inconsistent.";
+       //load default engines
+       IntEngine::configureIntEngine(*this);
+       FloatEngine::configureFloatEngine(*this);
+       BoolEngine::configureBoolEngine(*this);
+       BoolEngine::configureLogicEngine(*this);
+       StringEngine::configureStringEngine(*this);
+       //add money class
+       setFunction("toMoney",toMoneyFuncLoc);
+       setFunction("toMoneyLocal",toMoneyFuncLoc);
+       setFunction("toMoneyNL",toMoneyFuncNLoc);
+       setFunction("fromMoney",fromMoneyFuncLoc);
+       setFunction("fromMoneyLocal",fromMoneyFuncLoc);
+       setFunction("fromMoneyNL",fromMoneyFuncNLoc);
+}
diff --git a/src/misc/formula.h b/src/misc/formula.h
new file mode 100644 (file)
index 0000000..49c915a
--- /dev/null
@@ -0,0 +1,24 @@
+//
+// C++ Interface: magicsmoke ELAM adaptation
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2010
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_ELAM_H
+#define MAGICSMOKE_ELAM_H
+
+#include "elam.h"
+
+class MElamEngine:public ELAM::Engine
+{
+       public:
+               MElamEngine(QObject*parent=0);
+};
+
+#endif
index 9f81207..a1fec82 100644 (file)
@@ -2,13 +2,15 @@ HEADERS += \
        misc/debug.h \
        misc/misc.h \
        misc/waitcursor.h \
-       misc/sclock.h
+       misc/sclock.h \
+       misc/formula.h
 
 SOURCES += \
        misc/code39.cpp \
        misc/debug.cpp \
        misc/misc.cpp \
        misc/waitcursor.cpp \
-       misc/sclock.cpp
+       misc/sclock.cpp \
+       misc/formula.cpp
 
 INCLUDEPATH += ./misc
\ No newline at end of file
index e3c7858..565dd02 100644 (file)
@@ -35,6 +35,7 @@
 #include <QBoxLayout>
 #include <QCheckBox>
 #include <QComboBox>
+#include <QDebug>
 #include <QFile>
 #include <QFileDialog>
 #include <QInputDialog>
@@ -180,6 +181,7 @@ void MOverview::closeEvent(QCloseEvent*ce)
 
 MOverview::~MOverview()
 {
+       qDebug()<<"destructing overview"<<hex<<(long)this;
        //free requestor
        req->deleteLater();
 }
index d1e034d..c1997ea 100644 (file)
@@ -42,6 +42,7 @@ QStringList MScriptEngine::m_basepath;
 MScriptEngine::MScriptEngine(MOverview* par)
        :QScriptEngine(par)
 {
+       qDebug()<<"creating script engine"<<hex<<(long)this;
        //register main window
        addObject("MainWindow",par);
        //init wob interface
@@ -62,6 +63,13 @@ MScriptEngine::MScriptEngine(MOverview* par)
        //load init script
        evalFile("init.js");
 }
+
+MScriptEngine::~MScriptEngine()
+{
+       qDebug()<<"deleting script engine"<<hex<<(long)this;
+}
+
+
 void MScriptEngine::addObject(QString name, QObject* object)
 {
        if(name.size()==0)return;
@@ -115,8 +123,31 @@ static inline bool normalizeFile(QString &fn)
        return true;
 }
 
+class MScriptEngine_DebugHelper
+{
+       QString h;
+       public:
+               MScriptEngine_DebugHelper(QString s){h=s;}
+               MScriptEngine_DebugHelper& operator<<(QString s)
+               {
+                       if(h.size()>0)h+=" ";
+                       h+=s;
+                       return *this;
+               }
+               MScriptEngine_DebugHelper& operator<<(long l)
+               {
+                       if(h.size()>0)h+=" ";
+                       h+=QString::number(l,16);
+                       return *this;
+               }
+               ~MScriptEngine_DebugHelper(){qDebug()<<h.toAscii().data();}
+};
+
 QScriptValue MScriptEngine::evalFile(QString fn)
 {
+       qDebug()<<"executing on script engine"<<hex<<(long)this<<"script file"<<fn;
+       MScriptEngine_DebugHelper dh("done executing script engine");
+       dh<<(long)this<<"script file"<<fn;
        QScriptContext*context=currentContext();
        //normalize file name
        if(!normalizeFile(fn)){
index 772af77..4e724f2 100644 (file)
@@ -27,6 +27,8 @@ class MScriptEngine:public QScriptEngine
        public:
                ///construct the engine with pointer to the main window
                MScriptEngine(MOverview*);
+               ///delete engine
+               virtual ~MScriptEngine();
                
                ///add another object and its meta object
                void addObject(QString name,QObject*object);
index 5b14dbb..3fd5b21 100644 (file)
@@ -157,6 +157,9 @@ void MTemplateEditor::updateView()
        }
        //expand all
        tree->expandAll();
+       tree->resizeColumnToContents(0);
+       tree->resizeColumnToContents(1);
+       tree->resizeColumnToContents(2);
        nochange=false;
 }
 void MTemplateEditor::deleteItem()
index b5a7bc8..2dc43b9 100644 (file)
@@ -31,6 +31,7 @@ class MTemplateChoice:public QDialog
        public:
                MTemplateChoice(const QString&,const QString&,const QStringList&,const QString&);
                
+               ///returns the choice made by the user
                MTemplate choice()const;
        private:
                //selection box