ossp-pkg/as/as-gui/as_amount.cpp
1.3
#include <qregexp.h>
#include "titamount.h"
#include "titconst.h"
// Sets a text formatted representation
void AmountBox::setText(const QString &Strval)
{
int nTotal = 0; // The total amount of minutes
QRegExp Strexpr("(\\d+):(\\d+)"); // Pattern in amount text
QString Stramount = QRegExp::escape(Strval); // Incoming string escaped
if (Strval.isEmpty()) { // Shortcircuit in the
this->setValue(0); // case of empty string
}
else { // Do the real work then
Strexpr.search(Stramount);
nTotal = Strexpr.cap(Strexpr.numCaptures() - 1).toInt() * TITRAQ_MINSINHOUR;
nTotal += Strexpr.cap(Strexpr.numCaptures()).toInt();
this->setValue(nTotal);
}
}
// Return a text formatted representation
QString AmountBox::text(void) const
{
QString Strfirst, Strsecond;
int nHours = 0;
int nMins = this->value();
nHours = nMins / TITRAQ_MINSINHOUR; // Calculate total hours
nMins = nMins % TITRAQ_MINSINHOUR; // Calculate total minutes
Strfirst = trUtf8("%1:").arg(nHours); // Format the first part
Strsecond = trUtf8("%1").arg(nMins); // Format the second part
// Pad the resulting concatination before sending it out the back
return Strfirst.rightJustify(3, '0') + Strsecond.rightJustify(2, '0');
}