From a3dc4deff103332fe2e7064099c5b43f42917236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Tue, 27 Mar 2012 21:12:55 +0200 Subject: [PATCH] Display date in locale format Add GetLocale(), GetDateFormat() and FormatDate() --- ChangeLog | 3 ++- src/controller/KissCount.cpp | 15 +++++++++++++++ src/controller/KissCount.hpp | 4 ++++ src/view/SupportedLanguages.hpp | 5 +++-- src/view/grid/DateDelegate.cpp | 7 +++++-- src/view/grid/DateDelegate.hpp | 3 ++- src/view/grid/GridAccount.cpp | 14 +++++++------- src/view/wxUI.cpp | 6 ++++++ src/view/wxUI.hpp | 5 +++++ 9 files changed, 49 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0582227..60b666c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -v0.3 (20/03/2012) +v0.3 (27/03/2012) ** User ** New interface in Qt Use BDD file from .local/share/kisscount @@ -6,6 +6,7 @@ v0.3 (20/03/2012) New account attribute : hidden Description is now auto-completed Snapshot feature + Display date in locale format ** Dev ** Version 3 of database (account hidden item added) diff --git a/src/controller/KissCount.cpp b/src/controller/KissCount.cpp index ff94bb0..fca39f5 100644 --- a/src/controller/KissCount.cpp +++ b/src/controller/KissCount.cpp @@ -649,3 +649,18 @@ bool KissCount::ChangeDatabase(QString filename) { return _db->ChangeDatabase(filename); } + +QLocale* KissCount::GetLocale() +{ + return _wxUI->GetLocale(); +} + +QString KissCount::GetDateFormat() +{ + return _wxUI->GetDateFormat(); +} + +QString KissCount::FormatDate(int day, int month, int year) +{ + return QDate(year, month, day).toString(_wxUI->GetDateFormat()); +} diff --git a/src/controller/KissCount.hpp b/src/controller/KissCount.hpp index 2678e05..304de0d 100644 --- a/src/controller/KissCount.hpp +++ b/src/controller/KissCount.hpp @@ -139,6 +139,10 @@ public: bool ChangeDatabase(QString filename); + QLocale* GetLocale(); + QString GetDateFormat(); + QString FormatDate(int day, int month, int year); + private: wxUI* _wxUI; Database* _db; diff --git a/src/view/SupportedLanguages.hpp b/src/view/SupportedLanguages.hpp index 9315304..f4a97c0 100644 --- a/src/view/SupportedLanguages.hpp +++ b/src/view/SupportedLanguages.hpp @@ -30,11 +30,12 @@ namespace SupportedLanguages { QString icon; QLocale::Language language; QString filename; + QString dateFormat; } language ; static const language languages[NB_SUPPORTED_LANGUAGES] = { - { "English", ICONS_PATH "/United Kingdom.png", QLocale::English, "english"}, - { "Français",ICONS_PATH "/France.png", QLocale::French, "french"} + { "English", ICONS_PATH "/United Kingdom.png", QLocale::English, "english", "yyyy/MM/dd"}, + { "Français",ICONS_PATH "/France.png", QLocale::French, "french", "dd/MM/yyyy"} }; } diff --git a/src/view/grid/DateDelegate.cpp b/src/view/grid/DateDelegate.cpp index 73c3f10..c7091a8 100644 --- a/src/view/grid/DateDelegate.cpp +++ b/src/view/grid/DateDelegate.cpp @@ -38,7 +38,7 @@ void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, QComboBox *combo = qobject_cast(editor); QString s ; - s = s.sprintf("%02d/%02d/%04d", combo->currentIndex()+1, _month, _year); + s = QDate(_year, _month, combo->currentIndex()+1).toString(_dateFormat); model->setData(index, qVariantFromValue(s)); } @@ -46,6 +46,9 @@ void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { + int day; + QComboBox* combo = qobject_cast(editor); - combo->setCurrentIndex(qVariantValue(index.data()).left(2).toInt()-1); + day = QDate::fromString(qVariantValue(index.data()), _dateFormat).day()-1; + combo->setCurrentIndex(day); } diff --git a/src/view/grid/DateDelegate.hpp b/src/view/grid/DateDelegate.hpp index f10e1e5..3627f14 100644 --- a/src/view/grid/DateDelegate.hpp +++ b/src/view/grid/DateDelegate.hpp @@ -28,7 +28,7 @@ class DateDelegate : public TableViewDelegate Q_OBJECT; public: - DateDelegate(QWidget *parent = 0, int month=0, int year=0) : TableViewDelegate(parent), _day(1), _month(month), _year(year) {} + DateDelegate(QWidget *parent = 0, int month=0, int year=0, QString dateFormat="") : TableViewDelegate(parent), _day(1), _month(month), _year(year), _dateFormat(dateFormat) {} QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; @@ -37,6 +37,7 @@ public: private: int _day, _month, _year; + QString _dateFormat; }; #endif diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index 107c0cb..3da19cc 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -236,7 +236,7 @@ void GridAccount::LoadOperations(std::vector* operations, int month, ChoiceDelegate* accountEditor = new ChoiceDelegate(this, _accounts, _nbAccounts); setItemDelegateForColumn(ACCOUNT, accountEditor); - DateDelegate* dateEditor = new DateDelegate(this, month+1, year); + DateDelegate* dateEditor = new DateDelegate(this, month+1, year, _kiss->GetDateFormat()); setItemDelegateForColumn(OP_DATE, dateEditor); FormulaDelegate* formulaEditor = new FormulaDelegate(this, &_displayedOperations); @@ -328,7 +328,7 @@ void GridAccount::InsertOperationWithWeek(User* user, Operation& op, int line, b if (op.id && !fix) ComputeWeeks(); } -#include + void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, int month, int year) { std::vector::iterator it; @@ -373,7 +373,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, else setItem(line, DESCRIPTION, new QTableWidgetItem(description)); item = new QTableWidgetItem(); - setItem(line, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", op.day+1, month+1, year))); + setItem(line, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(op.day+1, month+1, year))); if (op.amount < 0) { setItem(line, DEBIT, new QTableWidgetItem(v.sprintf("%.2lf", -op.amount))); @@ -440,7 +440,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, } else { - setItem(line, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", curDate.day(), month+1, year))); + setItem(line, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(curDate.day(), month+1, year))); SET_ROW_COLOR(line, view::OWN_GREEN, Qt::black); } @@ -914,7 +914,7 @@ void GridAccount::OnOperationModified(int row, int col) value = item(row, OP_DATE)->text(); if (value.length()) { - date = QDate::fromString(value, "dd/MM/yyyy"); + date = QDate::fromString(value, _kiss->GetDateFormat()); new_op.day = date.day()-1; new_op.month = date.month()-1; new_op.year = date.year(); @@ -1050,7 +1050,7 @@ void GridAccount::OnOperationModified(int row, int col) { if (_curMonth+1 == curDate.month() && _curYear == curDate.year()) - setItem(row, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", curDate.day(), _curMonth+1, _curYear))); + setItem(row, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(curDate.day(), _curMonth+1, _curYear))); } DEFAULT_FONT(font); @@ -1119,7 +1119,7 @@ void GridAccount::OnOperationModified(int row, int col) else checkBox->setCheckState(Qt::Unchecked); - setItem(row, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", new_op.day+1, _curMonth+1, _curYear))); + setItem(row, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(new_op.day+1, _curMonth+1, _curYear))); if (!_displayedOperations[row].amount) { diff --git a/src/view/wxUI.cpp b/src/view/wxUI.cpp index 52974cc..40787bf 100644 --- a/src/view/wxUI.cpp +++ b/src/view/wxUI.cpp @@ -147,6 +147,7 @@ bool wxUI::SetLanguage(QString language) } _language = SupportedLanguages::languages[i].language; + _curLanguage = SupportedLanguages::languages[i]; _locale = new QLocale(SupportedLanguages::languages[i].language); QLocale::setDefault(*_locale); @@ -331,3 +332,8 @@ void wxUI::NeedReload() { _needReload = true; } + +QString wxUI::GetDateFormat() +{ + return _curLanguage.dateFormat; +} diff --git a/src/view/wxUI.hpp b/src/view/wxUI.hpp index 7062e60..1b77956 100644 --- a/src/view/wxUI.hpp +++ b/src/view/wxUI.hpp @@ -33,6 +33,7 @@ class ImportEngine; #include #include "view.hpp" +#include "SupportedLanguages.hpp" #include #include @@ -73,7 +74,11 @@ public: void ShowPanel(KissPanel* panel); void NeedReload(); + QLocale* GetLocale() {return _locale;} + QString GetDateFormat(); + QString _language; + SupportedLanguages::language _curLanguage; private slots: void OnButtonClicked(int id);