From: konrad Date: Sun, 21 Dec 2008 14:58:28 +0000 (+0000) Subject: use special spinbox for editing prices X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=1edda6a561f6fd387c41554f6fa107a3f19f4cbc;p=konrad%2Fsmoke.git use special spinbox for editing prices git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@221 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/src/centbox.cpp b/src/centbox.cpp new file mode 100644 index 0000000..c33377b --- /dev/null +++ b/src/centbox.cpp @@ -0,0 +1,126 @@ +// +// C++ Implementation: centbox +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2008 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#include "centbox.h" +#include "misc.h" + +#include +#include +#include +#include + +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(mvalmmax)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(msetText(cent2str(m)); + emit valueChanged(m); +} + +MCentSpinBox::StepEnabled MCentSpinBox::stepEnabled () const +{ + return (mval>mmin?StepDownEnabled:StepNone)|(mvalma)return; + mmin=mi; + mmax=ma; + if(mval>mmax){ + setValue(mmax); + }else + if(mvaladdWidget(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 index 0000000..b1b6256 --- /dev/null +++ b/src/centbox.h @@ -0,0 +1,59 @@ +// +// C++ Interface: centbox +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2008 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#ifndef MAGICSMOKE_CENTBOX_H +#define MAGICSMOKE_CENTBOX_H + +#include +#include +#include + +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 diff --git a/src/misc.cpp b/src/misc.cpp index a055f29..07f08cc 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -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 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) diff --git a/src/orderwin.cpp b/src/orderwin.cpp index c16b336..5976a80 100644 --- a/src/orderwin.cpp +++ b/src/orderwin.cpp @@ -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()); } diff --git a/src/orderwin.h b/src/orderwin.h index f08e6ac..f1686f3 100644 --- a/src/orderwin.h +++ b/src/orderwin.h @@ -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 all; QComboBox*opt; - QDoubleSpinBox*prc; + MCentSpinBox*prc; }; #endif diff --git a/src/shipping.cpp b/src/shipping.cpp index 1476a45..0796ca4 100644 --- a/src/shipping.cpp +++ b/src/shipping.cpp @@ -13,6 +13,7 @@ #include "misc.h" #include "shipping.h" #include "webrequest.h" +#include "centbox.h" #include #include @@ -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<