From 57ebc03c6d1dd9335e9076c97aab55006ade5333 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 19 Jun 2010 13:42:01 +0000 Subject: [PATCH] docu and some starting points of formatter object git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@506 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- src/misc/misc.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/misc/misc.h | 103 +++++++++++++++++++++++++++++++++++++ 2 files changed, 250 insertions(+), 0 deletions(-) diff --git a/src/misc/misc.cpp b/src/misc/misc.cpp index 5e5fb79..391d8f9 100644 --- a/src/misc/misc.cpp +++ b/src/misc/misc.cpp @@ -112,3 +112,150 @@ QString unix2dateTime(int tm,bool localize) return QDateTime::fromTime_t(tm).toString(format); } + + +MLocalFormat::MLocalFormat() +{ + m_moneydecimals=2; + m_thousanddigits=3; + //set defaults + setWeekDays();setShortWeekDays(); + setMonths();setShortMonths(); + setMoneyFormat();setNumberFormat(); +} + +MLocalFormat::MLocalFormat(const MLocalFormat&f) +{ + m_day=f.m_day; + m_sday=f.m_sday; + m_month=f.m_month; + m_smonth=f.m_smonth; + m_currency=f.m_currency; + m_decimal=f.m_decimal; + m_thousand=f.m_thousand; + m_moneydecimals=f.m_moneydecimals; + m_thousanddigits=f.m_thousanddigits; +} + +MLocalFormat::~MLocalFormat(){} + +void MLocalFormat::setWeekDays(const QStringList&w) +{ + if(w.size()==0){ + m_day=QStringList() + < #include +#include #include /**converts special HTML characters into harmless &-codes, so the text can be included*/ @@ -40,4 +42,105 @@ QString unix2dateTime(int,bool localize=true); /**return a (localized) regular expression that validates prices*/ QRegExp priceRegExp(bool localize=true); +/**localized formatter class for timestamps, numbers and money; +per default it uses the local translation to format data, but can be overidden; + +date values can be formatted with the following variables: + + + + + + + + + + + +
%%ytwo-digit year (eg. 08)
%%Yfour-digit year (eg. 1908)
%%mmonth as simple number (eg. 7)
%%Mmonth as two digit number (eg. 07)
%%nshort name of the month (eg. Jul)
%%Nlong name of the month (eg. July)
%%dday of the month as simple number (eg. 5)
%%Dday of the month as two digit number (eg. 05)
%%wday of the week as short name (eg. Wed)
%%Wday of the week as long name (eg. Wednesday)
+ +time values can be formatted with the following variables: + + + + + + + + + + + + + +
%%hhour as simple number (eg. 7) in 24-hour format
%%Hhour as two digit number (eg. 07) in 24-hour format
%%ahour as simple number (eg. 7) in 12-hour format
%%Ahour as two digit number (eg. 07) in 12-hour format
%%iminute
%%Iminute
%%ssecond
%%Ssecond
%%zmilli-seconds
%%Zmilli-seconds
%%p"am" or "pm" according to current time
%%P"AM" or "PM" according to current time
+ +Any occurrence of "%%" will be translated to a literal "%" sign. + +For example "%w the %d'th of %N in the year of the lord %Y at %a:%I %P" will be formatted as "Sat 19'th of June in the year of the lord 2010 at 8:29 AM". +*/ +class MLocalFormat +{ + public: + /**constructs a formatter object*/ + MLocalFormat(); + /**copies a formatter object inheriting its overrides*/ + MLocalFormat(const MLocalFormat&); + /**deletes the formatter object*/ + virtual ~MLocalFormat(); + + /**overrides the full names of week days, an empty list resets to the local translation, otherwise the list must be exactly seven entries long and starts with Monday*/ + virtual void setWeekDays(const QStringList&l=QStringList()); + /**overrides the short week day names, an empty list resets to the local translation, otherwise the list must be exactly seven entries long and starts with Monday (Mon)*/ + virtual void setShortWeekDays(const QStringList&l=QStringList()); + + /**overrides the full names of months, an empty list resets to the local translation, otherwise the list must be exactly seven entries long and starts with January*/ + virtual void setMonths(const QStringList&l=QStringList()); + /**overrides the short names of months, an empty list resets to the local translation, otherwise the list must be exactly seven entries long and starts with January (Jan)*/ + virtual void setShortMonths(const QStringList&l=QStringList()); + + /**overrides the formatting of money - the settings of numbers are re-used; these settings have no translation fallbacks; + \param digitsCents defines how many digits are used for sub-amounts (cents), if zero no cents are used, normal are the values 0, 2 and 3 + \param currency defines the symbol of the currency used, the default is an empty string*/ + virtual void setMoneyFormat(QString currency=QString(),int digitsCents=2); + + /**overrides the formatting of numbers and money; + \param decimal defines the character used to separate sub-amounts (cents) from the main body, if QChar() is used it returns to the translation default + \param thousandDiv defines the character used to separate blocks of digits, if QChar() is used it returns to the translation default + \param digitsDiv defines the amount of digits in a block, eg. the value 3 will format 1000 as 1,000*/ + virtual void setNumberFormat(QChar decimal=QChar(),QChar thousandDiv=QChar(),int digitsDiv=-1); + + /**formats a date according to the given format, if none is given, the translation default is used + \param date a QDate used as input + \param format a format string as specified by QDate::toString */ + virtual QString formatDate(const QDate&date,QString format=QString())const; + /**formats a date according to the given format, if none is given, the translation default is used + \param date a unix timestamp, it is converted to local time*/ + virtual QString formatDate(qint64 date,QString format=QString())const; + + /**formats a time according to the given format, if none is given, the translation default is used + \param time a QTime used as input + \param format a format string as specified by QTime::toString */ + virtual QString formatTime(const QTime&time,QString format=QString())const; + /**formats a time according to the given format, if none is given, the translation default is used + \param time a unix timestamp, it is converted to local time*/ + virtual QString formatTime(qint64 time,QString format=QString())const; + + /**formats a date and time according to the given format, if none is given, the translation default is used + \param time a QDateTime used as input + \param format a format string as specified by QDateTime::toString */ + virtual QString formatDateTime(const QDateTime&time,QString format=QString())const; + /**formats a date and time according to the given format, if none is given, the translation default is used + \param time a unix timestamp, it is converted to local time*/ + virtual QString formatDateTime(qint64 time,QString format=QString())const; + protected: + QStringList m_day,m_sday,m_month,m_smonth; + QString m_currency; + QChar m_decimal,m_thousand; + int m_moneydecimals,m_thousanddigits; + + /** \internal helper to convert a unix timestamp to local time*/ + virtual QDateTime unix2localTime(qint64); +}; + #endif -- 1.7.2.5