From e7a2b0c988dc6a4ff7cfd5846a4d1ce0c4b4abc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Tue, 14 Sep 2010 21:11:41 +0200 Subject: [PATCH] Add grid editor/renderer for multiple operations --- src/view/AccountPanel.cpp | 11 --- src/view/SearchPanel.cpp | 5 -- src/view/grid/GridAccount.cpp | 9 +- src/view/grid/GridAccount.h | 4 +- src/view/grid/wxGridCellTreeButtonEditor.cpp | 74 ++++++++++++++++ src/view/grid/wxGridCellTreeButtonEditor.h | 50 +++++++++++ .../grid/wxGridCellTreeButtonRenderer.cpp | 85 +++++++++++++++++++ src/view/grid/wxGridCellTreeButtonRenderer.h | 41 +++++++++ 8 files changed, 259 insertions(+), 20 deletions(-) create mode 100644 src/view/grid/wxGridCellTreeButtonEditor.cpp create mode 100644 src/view/grid/wxGridCellTreeButtonEditor.h create mode 100644 src/view/grid/wxGridCellTreeButtonRenderer.cpp create mode 100644 src/view/grid/wxGridCellTreeButtonRenderer.h diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index 909e7e4..c8bdfdd 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -277,17 +277,6 @@ void AccountPanel::LoadYear(int year, bool showMonth) _wxUI->Layout(); } -#define SET_ROW_COLOR(row, backcolor, forecolor) for(int i=0; iSetCellBackgroundColour(row, i, backcolor); \ - _grid->SetCellTextColour(row, i, forecolor); \ - } - -#define SET_ROW_FONT(row, font) for(int i=0; iSetCellFont(row, i, font); \ - } - void AccountPanel::ShowMonth(int month, int year) { std::vector operations; diff --git a/src/view/SearchPanel.cpp b/src/view/SearchPanel.cpp index e92aba4..f029238 100644 --- a/src/view/SearchPanel.cpp +++ b/src/view/SearchPanel.cpp @@ -29,11 +29,6 @@ EVT_GRID_CMD_CELL_CHANGE(GRID_ID, SearchPanel::OnOperationModified) EVT_SHOW(SearchPanel::OnShow) END_EVENT_TABLE() -#define SET_ROW_COLOR(row, color) for(int i=0; iSetCellBackgroundColour(row, i, color); \ - } - #define UNESCAPE_CHARS(s) { \ s.Replace(wxT("\\\""), wxT("\""), true); \ s.Replace(wxT("\\\'"), wxT("\'"), true); \ diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index 3e5cc30..560012d 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -56,11 +56,11 @@ GridAccount::GridAccount(KissCount* kiss, wxWindow *parent, wxWindowID id) : wxG SetColLabelSize(0); SetRowLabelSize(0); - SetColSize (0, GetColSize(0)*3); + SetColSize (DESCRIPTION, GetColSize(DESCRIPTION)*3); SetDefaultCellFont(font); font.SetWeight(wxFONTWEIGHT_BOLD); - wxString colsName[] = {_("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), wxT(""), wxT("")}; + wxString colsName[] = {wxT(""), _("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), wxT(""), wxT("")}; for(i=0; i* operations, bool canAdd if (canAddOperation) InsertOperation(user, NULL, ++curLine, false, month, year); + AutoSizeColumn(TREE, false); AutoSizeColumn(CATEGORY, false); AutoSizeColumn(DATE, false); AutoSizeColumn(ACCOUNT, false); @@ -223,6 +224,8 @@ void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, InsertRows(line, 1); + SetCellRenderer(line, TREE, new wxGridCellTreeButtonRenderer()); + SetCellEditor(line, TREE, new wxGridCellTreeButtonEditor()); SetCellEditor(line, DEBIT, new wxGridCellFloatEditor(wxID_ANY, 2)); SetCellEditor(line, CREDIT, new wxGridCellFloatEditor(wxID_ANY, 2)); wxGridCellChoiceEditor* accountEditor = new wxGridCellChoiceEditor(user->GetAccountsNumber(), _accounts, false); @@ -315,7 +318,7 @@ void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, // From http://nomadsync.cvs.sourceforge.net/nomadsync/nomadsync/src/ void GridAccount::OnCellLeftClick(wxGridEvent& evt) { - if (evt.GetCol() != DELETE && evt.GetCol() != CHECKED) { evt.Skip() ; return;} + if (evt.GetCol() != TREE && evt.GetCol() != DELETE && evt.GetCol() != CHECKED) { evt.Skip() ; return;} // This forces the cell to go into edit mode directly //m_waitForSlowClick = TRUE; diff --git a/src/view/grid/GridAccount.h b/src/view/grid/GridAccount.h index 2c3c7c1..89aab74 100644 --- a/src/view/grid/GridAccount.h +++ b/src/view/grid/GridAccount.h @@ -27,10 +27,12 @@ #include #include #include "wxGridCellFastBoolEditor.h" +#include "wxGridCellTreeButtonRenderer.h" +#include "wxGridCellTreeButtonEditor.h" class KissCount; -enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, DELETE, CHECKED, NUMBER_COLS_OPS}; +enum {TREE, DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, DELETE, CHECKED, NUMBER_COLS_OPS}; class GridAccount : public wxGrid { diff --git a/src/view/grid/wxGridCellTreeButtonEditor.cpp b/src/view/grid/wxGridCellTreeButtonEditor.cpp new file mode 100644 index 0000000..37394e0 --- /dev/null +++ b/src/view/grid/wxGridCellTreeButtonEditor.cpp @@ -0,0 +1,74 @@ +/* + Copyright 2010 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 . +*/ + +#include "wxGridCellTreeButtonEditor.h" + +wxGridCellEditor* wxGridCellTreeButtonEditor::Clone () const +{ + return new wxGridCellTreeButtonEditor(); +} + +void wxGridCellTreeButtonEditor::BeginEdit (int row, int col, wxGrid *grid) +{ + wxGridEvent evt(0, 0, NULL, row, col); + + _row = row; + _col = col; + _grid = grid; + + wxGridCellRenderer* pRenderer = _grid->GetCellRenderer(_row, _col); + ((wxGridCellTreeButtonRenderer*) pRenderer)->Invert(); + + pRenderer->DecRef(); + + ((GridAccount*)_grid)->OnCellLeftClick(evt); +} + +void wxGridCellTreeButtonEditor::Create (wxWindow *parent, wxWindowID id, wxEvtHandler *evtHandler) +{ +} + +bool wxGridCellTreeButtonEditor::EndEdit (int row, int col, /*const*/ wxGrid *grid/*, const wxString &oldval, wxString *newval*/) +{ + return true; +} + +void wxGridCellTreeButtonEditor::ApplyEdit (int row, int col, wxGrid *grid) +{} + +void wxGridCellTreeButtonEditor::Reset () +{} + +wxString wxGridCellTreeButtonEditor::GetValue() const +{ + wxGridCellRenderer* pRenderer = _grid->GetCellRenderer(_row, _col); + wxString res = ((wxGridCellTreeButtonRenderer*) pRenderer)->IsCollapsed() ? wxT("1") : wxT("0"); + + pRenderer->DecRef(); + + return res; +} + +void wxGridCellTreeButtonEditor::SetSize (const wxRect &rect) +{ +} + +void wxGridCellTreeButtonEditor::Show (bool show, wxGridCellAttr *attr=NULL) +{ +} diff --git a/src/view/grid/wxGridCellTreeButtonEditor.h b/src/view/grid/wxGridCellTreeButtonEditor.h new file mode 100644 index 0000000..992008e --- /dev/null +++ b/src/view/grid/wxGridCellTreeButtonEditor.h @@ -0,0 +1,50 @@ +/* + Copyright 2010 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 WXGRIDCELLTREEBUTTONEDITOR_H +#define WXGRIDCELLTREEBUTTONEDITOR_H + +#include +#include +#include + +#include "GridAccount.h" +#include "wxGridCellTreeButtonRenderer.h" + +class wxGridCellTreeButtonEditor : public wxGridCellEditor +{ +public: + + wxGridCellEditor* Clone () const; + + void BeginEdit (int row, int col, wxGrid *grid); + void Create (wxWindow *parent, wxWindowID id, wxEvtHandler *evtHandler); + bool EndEdit (int row, int col, wxGrid *grid/*, const wxString &oldval, wxString *newval*/); + void ApplyEdit (int row, int col, wxGrid *grid); + void Reset () ; + wxString GetValue() const ; + void SetSize (const wxRect &rect); + void Show (bool show, wxGridCellAttr *attr); + +private: + int _row, _col; + wxGrid* _grid; +}; + +#endif diff --git a/src/view/grid/wxGridCellTreeButtonRenderer.cpp b/src/view/grid/wxGridCellTreeButtonRenderer.cpp new file mode 100644 index 0000000..846cc5f --- /dev/null +++ b/src/view/grid/wxGridCellTreeButtonRenderer.cpp @@ -0,0 +1,85 @@ +/* + Copyright 2010 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 . +*/ + +#include "wxGridCellTreeButtonRenderer.h" + +wxGridCellTreeButtonRenderer::wxGridCellTreeButtonRenderer(bool collapsed) : _collapsed(collapsed) +{ + +} + +wxGridCellRenderer* wxGridCellTreeButtonRenderer::Clone () const +{ + return new wxGridCellTreeButtonRenderer(_collapsed); +} + +bool wxGridCellTreeButtonRenderer::IsCollapsed() +{ + return _collapsed; +} + +void wxGridCellTreeButtonRenderer::Invert() +{ + _collapsed = !_collapsed; +} + +void wxGridCellTreeButtonRenderer::Draw (wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &_rect, int row, int col, bool isSelected) +{ + wxRect rect = _rect; + + wxBrush originalBrush = dc.GetBrush(); + wxPen originalPen = dc.GetPen(); + + dc.SetPen(wxPen(grid.GetCellBackgroundColour(row, col))); + dc.SetBrush(wxBrush(grid.GetCellBackgroundColour(row, col))); + dc.DrawRectangle(rect); + + rect.x += 4 ; + rect.y += 4 ; + rect.width -= 8 ; + rect.height -= 8 ; + + if (rect.width > rect.height) + rect.width = rect.height; + else if (rect.height > rect.width) + rect.height = rect.width; + + dc.SetPen(wxPen(*wxBLACK)); + + dc.DrawLine(rect.x, rect.y, rect.x + rect.width, rect.y); + dc.DrawLine(rect.x, rect.y, rect.x, rect.y + rect.height); + + dc.DrawLine(rect.x, rect.y + rect.height, rect.x + rect.width + 1, rect.y + rect.height); + dc.DrawLine(rect.x + rect.width, rect.y, rect.x + rect.width, rect.y + rect.height); + + rect.x += 2 ; + rect.y += 2 ; + rect.width -= 4 ; + rect.height -= 4 ; + + dc.DrawLine(rect.x+1, rect.y + rect.height/2, rect.x + rect.width + 1, rect.y + rect.height/2); + + if (!_collapsed) + dc.DrawLine(rect.x + rect.width/2 + 1, rect.y, rect.x + rect.width/2 + 1, rect.y + rect.height); +} + +wxSize wxGridCellTreeButtonRenderer::GetBestSize (wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) +{ + return wxSize(8, 8); +} diff --git a/src/view/grid/wxGridCellTreeButtonRenderer.h b/src/view/grid/wxGridCellTreeButtonRenderer.h new file mode 100644 index 0000000..13f29e8 --- /dev/null +++ b/src/view/grid/wxGridCellTreeButtonRenderer.h @@ -0,0 +1,41 @@ +/* + Copyright 2010 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 WXGRIDCELLTREEBUTTONRENDERER_H +#define WXGRIDCELLTREEBUTTONRENDERER_H + +#include +#include + +class wxGridCellTreeButtonRenderer : public wxGridCellRenderer +{ +public: + wxGridCellTreeButtonRenderer(bool collapsed=false); + + virtual wxGridCellRenderer* Clone () const; + virtual void Draw (wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected); + virtual wxSize GetBestSize (wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col); + + bool IsCollapsed(); + void Invert(); +private: + bool _collapsed; +}; + +#endif