From f035692ea32dace17e80080e63b0a9c8e512260f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sat, 22 Oct 2011 13:13:57 +0200 Subject: [PATCH] GridAccount rendering is now like wxWidgets GridAccount --- src/view/grid/ChoiceDelegate.hpp | 6 +-- src/view/grid/DateDelegate.hpp | 6 +-- src/view/grid/FloatDelegate.hpp | 6 +-- src/view/grid/FormulaDelegate.hpp | 6 +-- src/view/grid/GridAccount.cpp | 58 ++++++++--------------- src/view/grid/GridAccount.hpp | 3 +- src/view/grid/TabDelegate.hpp | 7 ++- src/view/grid/TableViewDelegate.hpp | 73 +++++++++++++++++++++++++++++ 8 files changed, 109 insertions(+), 56 deletions(-) create mode 100644 src/view/grid/TableViewDelegate.hpp diff --git a/src/view/grid/ChoiceDelegate.hpp b/src/view/grid/ChoiceDelegate.hpp index d097e6d..8e90967 100644 --- a/src/view/grid/ChoiceDelegate.hpp +++ b/src/view/grid/ChoiceDelegate.hpp @@ -20,15 +20,15 @@ #ifndef CHOICEDELEGATE_H #define CHOICEDELEGATE_H -#include +#include "TableViewDelegate.hpp" #include -class ChoiceDelegate : public QItemDelegate +class ChoiceDelegate : public TableViewDelegate { Q_OBJECT; public: - ChoiceDelegate(QWidget *parent = 0, QString* values=0, int nbValues=0) : QItemDelegate(parent), _values(values), _nbValues(nbValues) {} + ChoiceDelegate(QWidget *parent = 0, QString* values=0, int nbValues=0) : TableViewDelegate(parent), _values(values), _nbValues(nbValues) {} QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; diff --git a/src/view/grid/DateDelegate.hpp b/src/view/grid/DateDelegate.hpp index 68d7405..1378f64 100644 --- a/src/view/grid/DateDelegate.hpp +++ b/src/view/grid/DateDelegate.hpp @@ -20,15 +20,15 @@ #ifndef DATEDELEGATE_H #define DATEDELEGATE_H -#include +#include "TableViewDelegate.hpp" #include -class DateDelegate : public QItemDelegate +class DateDelegate : public TableViewDelegate { Q_OBJECT; public: - DateDelegate(QWidget *parent = 0, int month=0, int year=0) : QItemDelegate(parent), _day(1), _month(month), _year(year) {} + DateDelegate(QWidget *parent = 0, int month=0, int year=0) : TableViewDelegate(parent), _day(1), _month(month), _year(year) {} QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; diff --git a/src/view/grid/FloatDelegate.hpp b/src/view/grid/FloatDelegate.hpp index 7d94a8d..d77d89d 100644 --- a/src/view/grid/FloatDelegate.hpp +++ b/src/view/grid/FloatDelegate.hpp @@ -20,15 +20,15 @@ #ifndef FLOATDELEGATE_H #define FLOATDELEGATE_H -#include +#include "TableViewDelegate.hpp" #include -class FloatDelegate : public QItemDelegate +class FloatDelegate : public TableViewDelegate { Q_OBJECT; public: - FloatDelegate(QWidget *parent = 0) : QItemDelegate(parent) {} + FloatDelegate(QWidget *parent = 0) : TableViewDelegate(parent) {} QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; diff --git a/src/view/grid/FormulaDelegate.hpp b/src/view/grid/FormulaDelegate.hpp index e2a8539..1059824 100644 --- a/src/view/grid/FormulaDelegate.hpp +++ b/src/view/grid/FormulaDelegate.hpp @@ -20,16 +20,16 @@ #ifndef FORMULADELEGATE_H #define FORMULADELEGATE_H -#include +#include "TableViewDelegate.hpp" #include #include -class FormulaDelegate : public QItemDelegate +class FormulaDelegate : public TableViewDelegate { Q_OBJECT; public: - FormulaDelegate(QWidget *parent = 0, std::vector* operations=0) : QItemDelegate(parent), _operations(operations) {} + FormulaDelegate(QWidget *parent = 0, std::vector* operations=0) : TableViewDelegate(parent), _operations(operations) {} QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index d4fb0c3..f9ad9e9 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -23,6 +23,7 @@ #include #include "GridAccount.hpp" +#include "TableViewDelegate.hpp" #include "ChoiceDelegate.hpp" #include "DateDelegate.hpp" #include "FloatDelegate.hpp" @@ -47,8 +48,8 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent, bool canAddOperation, bool setWeek, bool synchronizeWithDatabase) : QTableWidget(parent), _fixCosts(0), _week1(0), - _week2(0), _week3(0), _week4(0), _parent(parent), _kiss(kiss), - _canAddOperation(canAddOperation), _setWeek(setWeek), + _week2(0), _week3(0), _week4(0), _canAddOperation(canAddOperation), + _parent(parent), _kiss(kiss), _setWeek(setWeek), _databaseSynchronization(synchronizeWithDatabase), _loadOperations(false), _curMonth(0), _curYear(0), _signalMapper(this) { @@ -65,6 +66,7 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent, setRowCount(1); verticalHeader()->setHidden(true); horizontalHeader()->setHidden(true); + setShowGrid(false); setColumnWidth (DESCRIPTION, columnWidth(DESCRIPTION)*3); //SetDefaultCellFont(font); @@ -116,6 +118,8 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent, resizeColumnToContents(CHECKED); connect(&_signalMapper, SIGNAL(mapped(int)), this, SLOT(OnMetaClicked(int))); + + setItemDelegate(new TableViewDelegate(this)); } GridAccount::~GridAccount() @@ -124,26 +128,6 @@ GridAccount::~GridAccount() delete[] _accounts; } -// wxPen GridAccount::GetColGridLinePen (int col) -// {return wxPen(*wxBLACK, 1, wxSOLID);} - -// wxPen GridAccount::GetRowGridLinePen (int row) { -// if (_canAddOperation) -// { -// if (row == 0 || row == _fixCosts || -// row == _week1 || -// row == _week2 || -// row == _week3 || -// row == _week4) -// return wxPen(*wxBLACK, 1, wxSOLID); -// } -// else -// if (row == 0) -// return wxPen(*wxBLACK, 1, wxSOLID); - -// return GetCellBackgroundColour(row, 0); -// } - void GridAccount::ResetWeeks() { _week1 = _week2 = _week3 = _week4 = 0; @@ -280,7 +264,7 @@ void GridAccount::LoadOperations(std::vector* operations, int month, void GridAccount::ComputeWeeks() { std::vector::iterator it; - int curLine;//, curWeek, week, i; + int curLine, curWeek, week, i; if (!_canAddOperation) return; @@ -295,18 +279,18 @@ void GridAccount::ComputeWeeks() ResetWeeks(); - // curWeek = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); - // it++; - // for (i=1; it != _displayedOperations.end(); it++, curLine++) - // { - // if (!it->id || it->parent) continue; - // week = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); - // if (week != curWeek) - // { - // SetWeek(i++, curLine); - // curWeek = week; - // } - // } + curWeek = QDate(it->year, it->month+1, it->day+1).weekNumber(); + it++; + for (i=1; it != _displayedOperations.end(); it++, curLine++) + { + if (!it->id || it->parent) continue; + week = QDate(it->year, it->month+1, it->day+1).weekNumber(); + if (week != curWeek) + { + SetWeek(i++, curLine); + curWeek = week; + } + } } void GridAccount::InsertOperationWithWeek(User* user, Operation& op, int line, bool fix, int month, int year) @@ -451,12 +435,8 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, _signalMapper.setMapping(button, op.id); connect(button, SIGNAL(clicked()), &_signalMapper, SLOT(map())); - //button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); - //button->resize(button->minimumSize()); button->setMaximumSize(QSize(height, height)); setCellWidget(line, TREE, button); - // SetCellRenderer(line, TREE, new wxGridCellTreeButtonRenderer()); - // SetCellEditor(line, TREE, new wxGridCellTreeButtonEditor()); SET_READ_ONLY(this->item(line, OP_DATE)); SET_READ_ONLY(this->item(line, CREDIT)); diff --git a/src/view/grid/GridAccount.hpp b/src/view/grid/GridAccount.hpp index 575eadf..f9cb306 100644 --- a/src/view/grid/GridAccount.hpp +++ b/src/view/grid/GridAccount.hpp @@ -70,6 +70,7 @@ public: int _fixCosts; int _week1, _week2, _week3, _week4; + bool _canAddOperation; std::vector _displayedOperations; private slots: @@ -80,7 +81,7 @@ private: QWidget* _parent; KissCount* _kiss; bool _displayLines; - bool _canAddOperation, _setWeek; + bool _setWeek; bool _databaseSynchronization; QString* _categories, *_accounts; std::vector* _operations; diff --git a/src/view/grid/TabDelegate.hpp b/src/view/grid/TabDelegate.hpp index 6341042..0da38b8 100644 --- a/src/view/grid/TabDelegate.hpp +++ b/src/view/grid/TabDelegate.hpp @@ -20,16 +20,15 @@ #ifndef TABDELEGATE_H #define TABDELEGATE_H -#include +#include "TableViewDelegate.hpp" #include -#include -class TabDelegate : public QItemDelegate +class TabDelegate : public TableViewDelegate { Q_OBJECT; public: - TabDelegate(QWidget *parent = 0, std::vector* operations=0) : QItemDelegate(parent), _operations(operations) {} + TabDelegate(QWidget *parent = 0, std::vector* operations=0) : TableViewDelegate(parent), _operations(operations) {} QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; diff --git a/src/view/grid/TableViewDelegate.hpp b/src/view/grid/TableViewDelegate.hpp new file mode 100644 index 0000000..cd08136 --- /dev/null +++ b/src/view/grid/TableViewDelegate.hpp @@ -0,0 +1,73 @@ +/* + Copyright 2010-2011 Grégory Soutadé + + This file is part of KissCount. + + KissCount is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + KissCount is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with KissCount. If not, see . +*/ + +#ifndef TABLEVIEWDELEGATE_H +#define TABLEVIEWDELEGATE_H + +#include +#include "GridAccount.hpp" + +class TableViewDelegate : public QStyledItemDelegate +{ +private: + GridAccount* _grid; + +public: + // "Nomme" les rôles utilisés par ce delegate + // pour récupèrer les nouvelles propriétés d'un item + enum Roles { + BorderWidthRole = Qt::UserRole, + BorderColorRole, + }; + + TableViewDelegate(QObject *parent = 0) : QStyledItemDelegate(parent), _grid(qobject_cast(parent)) + { + } + + void paint (QPainter * painter, + const QStyleOptionViewItem & option, + const QModelIndex & index ) const + { + + QStyledItemDelegate::paint(painter, option, index); + + if (!_grid) return; + + QPen pen(Qt::black); + int row = index.row(); + painter->setPen(pen); + + // Always paint column + painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft()); + + if (row == 0) + painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); + else if (_grid->_canAddOperation) + { + if (row == _grid->_fixCosts || + row == _grid->_week1 || + row == _grid->_week2 || + row == _grid->_week3 || + row == _grid->_week4) + painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); + } + } +}; + +#endif