use special spinbox for editing prices
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 21 Dec 2008 14:58:28 +0000 (14:58 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 21 Dec 2008 14:58:28 +0000 (14:58 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@221 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/centbox.cpp [new file with mode: 0644]
src/centbox.h [new file with mode: 0644]
src/misc.cpp
src/orderwin.cpp
src/orderwin.h
src/shipping.cpp
src/smoke.pro

diff --git a/src/centbox.cpp b/src/centbox.cpp
new file mode 100644 (file)
index 0000000..c33377b
--- /dev/null
@@ -0,0 +1,126 @@
+//
+// C++ Implementation: centbox
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "centbox.h"
+#include "misc.h"
+
+#include <QBoxLayout>
+#include <QLabel>
+#include <QLineEdit>
+#include <QPushButton>
+
+MCentSpinBox::MCentSpinBox(QWidget*parent,int value,int maxValue)
+       :QAbstractSpinBox(parent),mvalid(priceRegExp(),this)
+{
+       mmin=0;
+       mmax=maxValue;
+       if(mmin>mmax)mmin=mmax;
+       mval=value;
+       if(mval<mmin)mval=mmin;
+       if(mval>mmax)mval=mmax;
+       
+       setButtonSymbols(UpDownArrows);
+       lineEdit()->setText(cent2str(mval));
+       connect(this,SIGNAL(editingFinished()),this,SLOT(fixup()));
+}
+
+int MCentSpinBox::value()const
+{
+       qDebug("current value: %i",mval);
+       return mval;
+}
+
+void MCentSpinBox::setValue(int m)
+{
+       if(m>mmax)m=mmax;
+       if(m<mmin)m=mmin;
+       if(mval==m)return;
+       qDebug("setval %i",m);
+       mval=m;
+       lineEdit()->setText(cent2str(m));
+       emit valueChanged(m);
+}
+
+MCentSpinBox::StepEnabled MCentSpinBox::stepEnabled () const
+{
+       return (mval>mmin?StepDownEnabled:StepNone)|(mval<mmax?StepUpEnabled:StepNone);
+}
+
+void MCentSpinBox::setRange(int mi,int ma)
+{
+       if(mi>ma)return;
+       mmin=mi;
+       mmax=ma;
+       if(mval>mmax){
+               setValue(mmax);
+       }else
+       if(mval<mmin){
+               setValue(mmin);
+       }
+}
+
+int MCentSpinBox::minimumValue()const{return mmin;}
+int MCentSpinBox::maximumValue()const{return mmax;}
+
+void MCentSpinBox::stepBy(int x)
+{
+       int m=mval+x;
+       setValue(m);
+}
+
+QValidator::State MCentSpinBox::validate(QString &input, int &pos) const
+{
+       return mvalid.validate(input,pos);
+}
+
+void MCentSpinBox::fixup()
+{
+       int m=str2cent(text());
+       setValue(m);
+}
+
+/*****************************************************/
+
+MCentDialog::MCentDialog(QWidget*parent,QString title,QString label,int val,int maxValue)
+       :QDialog(parent)
+{
+       setWindowTitle(title);
+       QVBoxLayout*vl;
+       setLayout(vl=new QVBoxLayout);
+       vl->addWidget(new QLabel(label));
+       vl->addWidget(box=new MCentSpinBox(this,val,maxValue));
+       QHBoxLayout *hl;
+       vl->addLayout(hl=new QHBoxLayout);
+       hl->addStretch(10);
+       QPushButton*p;
+       hl->addWidget(p=new QPushButton(tr("OK")));
+       p->setDefault(true);
+       connect(p,SIGNAL(clicked()),this,SLOT(accept()));
+       hl->addWidget(p=new QPushButton(tr("Cancel")));
+       connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+}
+
+int MCentDialog::value()const
+{
+       return box->value();
+}
+
+int MCentDialog::getCents(QWidget*parent,QString title,QString label,int val,int maxValue,bool*ok)
+{
+       MCentDialog d(parent,title,label,val,maxValue);
+       bool b=d.exec()==QDialog::Accepted;
+       if(ok)*ok=b;
+       if(b)
+               return d.value();
+       else
+               return 0;
+}
diff --git a/src/centbox.h b/src/centbox.h
new file mode 100644 (file)
index 0000000..b1b6256
--- /dev/null
@@ -0,0 +1,59 @@
+//
+// C++ Interface: centbox
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_CENTBOX_H
+#define MAGICSMOKE_CENTBOX_H
+
+#include <QAbstractSpinBox>
+#include <QDialog>
+#include <QRegExpValidator>
+
+class MCentSpinBox:public QAbstractSpinBox
+{
+       Q_OBJECT
+       public:
+               MCentSpinBox(QWidget*parent=0,int value=0,int maxValue=2147483647);
+               
+               virtual int value()const;
+               virtual void setValue(int);
+               
+               virtual void setRange(int,int);
+               
+               virtual int minimumValue()const;
+               virtual int maximumValue()const;
+               
+               void stepBy(int);
+       protected:
+               virtual QValidator::State validate(QString &input, int &pos) const;
+               virtual StepEnabled stepEnabled () const;
+       signals:
+               void valueChanged(int);
+       private slots:
+               void fixup();
+       private:
+               int mval,mmax,mmin;
+               QRegExpValidator mvalid;
+};
+
+class MCentDialog:public QDialog
+{
+       public:
+               MCentDialog(QWidget*parent=0,QString title=QString(),QString label=QString(),int value=0,int maxValue=2147483647);
+               
+               virtual int value()const;
+               
+               static int getCents(QWidget*parent=0,QString title=QString(),QString label=QString(),int value=0,int maxValue=2147483647,bool *ok=0);
+       private:
+               MCentSpinBox*box;
+};
+
+#endif
index a055f29..07f08cc 100644 (file)
@@ -56,9 +56,28 @@ QString cent2str(int c,bool localize)
 
 int str2cent(QString s,bool localize)
 {
+       //convert local decimal dot by international decimal dot
        if(localize)s=s.replace(QCoreApplication::translate("misc",".","decimal dot in price"),".");
-       double c=s.toDouble()*100.;
-       return int(c);
+       //calculate price
+       //pp->price part euro/dollar/...; pc->price part cent; pm=multiplier for cent; md->mode
+       int pp=0,pc=0,pm=10,md=0;
+       for(int i=0;i<s.size();i++){
+               QChar c=s[i];
+               if(c.isDigit()){
+                       if(md==0){//mode==0 -> before the dot
+                               pp*=10;
+                               pp+=c.digitValue();
+                       }else{//mode==1 -> after the dot
+                               pc+=c.digitValue()*pm;
+                               if(pm==1)break;
+                               pm=1;
+                       }
+               }else
+               if(c=='.')md=1;//reached the dot, switch mode
+               //ignore everything else
+       }
+       //return result
+       return pp*100+pc;
 }
 
 QRegExp priceRegExp(bool localize)
index c16b336..5976a80 100644 (file)
@@ -10,6 +10,7 @@
 //
 //
 
+#include "centbox.h"
 #include "event.h"
 #include "labeldlg.h"
 #include "misc.h"
@@ -637,9 +638,8 @@ void MOrderWindow::payment()
        if(!m_order.isValid())return;
        //get value
        bool ok;
-       double rt=QInputDialog::getDouble(this,tr("Enter Payment"),tr("Please enter the amount that has been paid:"),m_order.amountToPay()/100.0,0,m_order.amountToPay()/100.0,2,&ok);
+       int pay=MCentDialog::getCents(this,tr("Enter Payment"),tr("Please enter the amount that has been paid:"),m_order.amountToPay(),m_order.amountToPay(),&ok);
        if(!ok)return;
-       int pay=int(rt*100.0);
        if(pay<=0)return;
        //submit
        QByteArray rq=QByteArray::number(m_order.orderID())+" "+QByteArray::number(pay);
@@ -660,9 +660,8 @@ void MOrderWindow::refund()
        if(!m_order.isValid())return;
        //get value
        bool ok;
-       double rt=QInputDialog::getDouble(this,tr("Enter Refund"),tr("Please enter the amount that will be refunded:"),m_order.amountToRefund()/100.0,0,m_order.amountToRefund()/100.0,2,&ok);
+       int pay=MCentDialog::getCents(this,tr("Enter Refund"),tr("Please enter the amount that will be refunded:"),m_order.amountToRefund(),m_order.amountToRefund(),&ok);
        if(!ok)return;
-       int pay=int(rt*100.0);
        if(pay<=0)return;
        //submit
        QByteArray rq=QByteArray::number(m_order.orderID())+" "+QByteArray::number(pay);
@@ -698,9 +697,8 @@ void MOrderWindow::changeItem()
                if(!tick.isValid())return;
                //get value
                bool ok;
-               double rt=QInputDialog::getDouble(this,tr("Enter Price"),tr("Please enter the new price for the ticket:"),tick.price()/100.0,0,1000000,2,&ok);
+               int pay=MCentDialog::getCents(this,tr("Enter Price"),tr("Please enter the new price for the ticket:"),tick.price(),100000000,&ok);
                if(!ok)return;
-               int pay=int(rt*100.0);
                if(pay<0)return;
                //submit
                m_order.updateTicketPrice(tick.ticketID(),pay);
@@ -976,7 +974,7 @@ MShippingChange::MShippingChange(QWidget*pa,MWebRequest*r,MShipping s)
        gl->addWidget(new QLabel(tr("Method:")),0,0);
        gl->addWidget(opt=new QComboBox,0,1);
        gl->addWidget(new QLabel(tr("Price:")),1,0);
-       gl->addWidget(prc=new QDoubleSpinBox,1,1);
+       gl->addWidget(prc=new MCentSpinBox,1,1);
        gl->setRowMinimumHeight(2,15);
        gl->setRowStretch(2,1);
        QHBoxLayout*hl;
@@ -988,9 +986,8 @@ MShippingChange::MShippingChange(QWidget*pa,MWebRequest*r,MShipping s)
        hl->addWidget(p=new QPushButton(tr("Cancel")));
        connect(p,SIGNAL(clicked()),this,SLOT(reject()));
        
-       prc->setRange(0.,1.0e9);//hmm, even in Yen this should be big enough
-       prc->setDecimals(2);
-       prc->setValue(s.price()/100.);
+       prc->setRange(0,1000000000);//hmm, even in Yen this should be big enough
+       prc->setValue(s.price());
        prc->setEnabled(req->hasRole("orderchangeshipping"));
        
        opt->addItem(tr("(None)","shipping method"));
@@ -1015,11 +1012,11 @@ MShipping MShippingChange::selection()const
 
 int MShippingChange::price()const
 {
-       return int(prc->value()*100.);
+       return prc->value();
 }
 
 void MShippingChange::changeItem(int cid)
 {
        if(cid==0)prc->setValue(0);
-       else prc->setValue(double(all[cid-1].price())/100.0);
+       else prc->setValue(all[cid-1].price());
 }
index f08e6ac..f1686f3 100644 (file)
@@ -154,7 +154,7 @@ class MOrderItemView:public QDialog
                MVoucherRenderer*vrender;
 };
 
-class QDoubleSpinBox;
+class MCentSpinBox;
 class QComboBox;
 
 /**helper class: allows to change the shipping option*/
@@ -177,7 +177,7 @@ class MShippingChange:public QDialog
                MWebRequest*req;
                QList<MShipping> all;
                QComboBox*opt;
-               QDoubleSpinBox*prc;
+               MCentSpinBox*prc;
 };
 
 #endif
index 1476a45..0796ca4 100644 (file)
@@ -13,6 +13,7 @@
 #include "misc.h"
 #include "shipping.h"
 #include "webrequest.h"
+#include "centbox.h"
 
 #include <QApplication>
 #include <QDomDocument>
@@ -207,9 +208,9 @@ void MShippingEditor::changePrice()
        MShipping s=all[idx.row()];
        //get new value
        bool b;
-       double r=QInputDialog::getDouble(this,tr("Shipping Option Price"),tr("Please select a new price for this shipping option:"),s.price()/100.,0,1e9,2,&b);
+       int r=MCentDialog::getCents(this,tr("Shipping Option Price"),tr("Please select a new price for this shipping option:"),s.price(),1000000000,&b);
        if(!b)return;
-       s.setPrice(int(r*100.));
+       s.setPrice(r);
        if(!s.save()){
                QMessageBox::warning(this,tr("Warning"),tr("Could not store the changes."));
                return;
@@ -249,7 +250,7 @@ void MShippingEditor::addNew()
        QString dsc=QInputDialog::getText(this,tr("Shipping Option Description"),tr("Please select a new description for this new shipping option:"));
        if(dsc=="")return;
        bool b;
-       double prc=QInputDialog::getDouble(this,tr("Shipping Option Price"),tr("Please select a new price for this new shipping option:"),0.,0,1e9,2,&b);
+       int prc=MCentDialog::getCents(this,tr("Shipping Option Price"),tr("Please select a new price for this new shipping option:"),0,1000000000,&b);
        if(!b)return;
        QStringList opt;
        opt<<tr("None")<<tr("Web Interface")<<tr("Any User")<<tr("Any User + Web Interface");
@@ -258,7 +259,7 @@ void MShippingEditor::addNew()
        //create the option
        MShipping s(req);
        s.setDescription(dsc);
-       s.setPrice(int(prc*100.));
+       s.setPrice(prc);
        int cav=4;
        for(int i=0;i<opt.size();i++)if(avl==opt[i])cav=i;
        s.setWebUsable(cav&1);
index c871282..a3dea82 100644 (file)
@@ -47,7 +47,8 @@ SOURCES = \
        version.cpp \
        templates.cpp \
        templatedlg.cpp \
-       office.cpp
+       office.cpp \
+       centbox.cpp
 
 HEADERS = \
        keygen.h \
@@ -73,7 +74,8 @@ HEADERS = \
        misc.h \
        templates.h \
        templatedlg.h \
-       office.h
+       office.h \
+       centbox.h
        
 #some PHP files are listed in this file to scan them for translatable items
 #use genphpscan.sh to regenerate it.