docu and some starting points of formatter object
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 19 Jun 2010 13:42:01 +0000 (13:42 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 19 Jun 2010 13:42:01 +0000 (13:42 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@506 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/misc/misc.cpp
src/misc/misc.h

index 5e5fb79..391d8f9 100644 (file)
@@ -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()
+                       <<QCoreApplication::translate("MLocalFormat","Monday")
+                       <<QCoreApplication::translate("MLocalFormat","Tuesday")
+                       <<QCoreApplication::translate("MLocalFormat","Wednesday")
+                       <<QCoreApplication::translate("MLocalFormat","Thursday")
+                       <<QCoreApplication::translate("MLocalFormat","Friday")
+                       <<QCoreApplication::translate("MLocalFormat","Saturday")
+                       <<QCoreApplication::translate("MLocalFormat","Sunday");
+       }else
+       if(w.size()==7)
+               m_day=w;
+}
+void MLocalFormat::setShortWeekDays(const QStringList&w)
+{
+       if(w.size()==0){
+               m_sday=QStringList()
+                       <<QCoreApplication::translate("MLocalFormat","Mon","short weekday")
+                       <<QCoreApplication::translate("MLocalFormat","Tue","short weekday")
+                       <<QCoreApplication::translate("MLocalFormat","Wed","short weekday")
+                       <<QCoreApplication::translate("MLocalFormat","Thu","short weekday")
+                       <<QCoreApplication::translate("MLocalFormat","Fri","short weekday")
+                       <<QCoreApplication::translate("MLocalFormat","Sat","short weekday")
+                       <<QCoreApplication::translate("MLocalFormat","Sun","short weekday");
+       }else
+       if(w.size()==7)
+               m_sday=w;
+}
+void MLocalFormat::setMonths(const QStringList&m)
+{
+       if(m.size()==0){
+               m_month=QStringList()
+                       <<QCoreApplication::translate("MLocalFormat","January")
+                       <<QCoreApplication::translate("MLocalFormat","February")
+                       <<QCoreApplication::translate("MLocalFormat","March")
+                       <<QCoreApplication::translate("MLocalFormat","April")
+                       <<QCoreApplication::translate("MLocalFormat","May")
+                       <<QCoreApplication::translate("MLocalFormat","June")
+                       <<QCoreApplication::translate("MLocalFormat","July")
+                       <<QCoreApplication::translate("MLocalFormat","August")
+                       <<QCoreApplication::translate("MLocalFormat","September")
+                       <<QCoreApplication::translate("MLocalFormat","October")
+                       <<QCoreApplication::translate("MLocalFormat","November")
+                       <<QCoreApplication::translate("MLocalFormat","December");
+       }else
+       if(m.size()==12)
+               m_month=m;
+}
+void MLocalFormat::setShortMonths(const QStringList&m)
+{
+       if(m.size()==0){
+               m_smonth=QStringList()
+                       <<QCoreApplication::translate("MLocalFormat","Jan","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Feb","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Mar","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Apr","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","May","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Jun","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Jul","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Aug","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Sep","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Oct","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Nov","short month name")
+                       <<QCoreApplication::translate("MLocalFormat","Dec","short month name");
+       }else
+       if(m.size()==12)
+               m_smonth=m;
+}
+void MLocalFormat::setMoneyFormat(QString c,int  n)
+{
+       m_currency=c;
+       m_moneydecimals=n;
+}
+
+void MLocalFormat::setNumberFormat(QChar dec,QChar thou,int dig)
+{
+       if(dec.isNull())m_decimal=QCoreApplication::translate("MLocalFormat",".","decimal dot")[0];
+       else m_decimal=dec;
+       
+       if(thou.isNull())m_thousand=QCoreApplication::translate("MLocalFormat",",","thousand division")[0];
+       else m_thousand=thou;
+       
+       if(dig<0)m_thousanddigits=QCoreApplication::translate("MLocalFormat","0","digits between thousand division chars").toInt();
+       else m_thousanddigits=dig;
+}
+QString MLocalFormat::formatDate(const QDate&date,QString format)const
+{
+       if(format=="")format=QCoreApplication::translate("MLocalFormat","%Y-%M-%D","date format");
+       return formatDateTime(date,format);
+}
+QString MLocalFormat::formatDate(qint64 date,QString format)const
+{
+       return formatDate(unix2localTime(date).date(),format);
+}
+QString MLocalFormat::formatTime(const QTime&time,QString format=QString())const
+{
+       if(format=="")format=QCoreApplication::translate("MLocalFormat","%h:%I","time format");
+       return formatDateTime(QDateTime(QDate::currentDate(),time),format);
+}
+QString MLocalFormat::formatTime(qint64 time,QString format)const
+{
+       return formatTime(unix2localTime(time).time(),format);
+}
+QString MLocalFormat::formatDateTime(const QDateTime&time,QString format)const
+{
+       if(format=="")format=QCoreApplication::translate("MLocalFormat","%Y-%M-%D %H:%I","date and time format");
+       //parse
+       ...
+}
+QString MLocalFormat::formatDateTime(qint64 time,QString format)const
+{
+       return formatDateTime(unix2localTime(time),format);
+}
+QDateTime MLocalFormat::unix2localTime(qint64 time)
+{
+       //TODO: use proper time zones
+       return QDateTime::fromTime_t(time);
+}
index 6d9766f..40a1fa0 100644 (file)
@@ -13,7 +13,9 @@
 #ifndef MAGICSMOKE_MISC_H
 #define MAGICSMOKE_MISC_H
 
+#include <QDateTime>
 #include <QString>
+#include <QStringList>
 #include <QRegExp>
 
 /**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:
+<table>
+<tr><td>%%y<td>two-digit year (eg. 08)</tr>
+<tr><td>%%Y<td>four-digit year (eg. 1908)</tr>
+<tr><td>%%m<td>month as simple number (eg. 7)</tr>
+<tr><td>%%M<td>month as two digit number (eg. 07)</tr>
+<tr><td>%%n<td>short name of the month (eg. Jul)</tr>
+<tr><td>%%N<td>long name of the month (eg. July)</tr>
+<tr><td>%%d<td>day of the month as simple number (eg. 5)</tr>
+<tr><td>%%D<td>day of the month as two digit number (eg. 05)</tr>
+<tr><td>%%w<td>day of the week as short name (eg. Wed)</tr>
+<tr><td>%%W<td>day of the week as long name (eg. Wednesday)</tr>
+</table>
+
+time values can be formatted with the following variables:
+<table>
+<tr><td>%%h<td>hour as simple number (eg. 7) in 24-hour format</tr>
+<tr><td>%%H<td>hour as two digit number (eg. 07) in 24-hour format</tr>
+<tr><td>%%a<td>hour as simple number (eg. 7) in 12-hour format</tr>
+<tr><td>%%A<td>hour as two digit number (eg. 07) in 12-hour format</tr>
+<tr><td>%%i<td>minute</tr>
+<tr><td>%%I<td>minute</tr>
+<tr><td>%%s<td>second</tr>
+<tr><td>%%S<td>second</tr>
+<tr><td>%%z<td>milli-seconds</tr>
+<tr><td>%%Z<td>milli-seconds</tr>
+<tr><td>%%p<td>"am" or "pm" according to current time</tr>
+<tr><td>%%P<td>"AM" or "PM" according to current time</tr>
+</table>
+
+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