KissCount/src/view/grid/GridAccount.cpp

323 lines
10 KiB
C++
Raw Normal View History

2010-07-14 16:25:19 +02:00
/*
Copyright 2010 Grégory Soutadé
2010-07-14 16:25:19 +02:00
This file is part of KissCount.
2010-07-14 16:25:19 +02:00
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.
2010-07-14 16:25:19 +02:00
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.
2010-07-14 16:25:19 +02:00
You should have received a copy of the GNU General Public License
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
2010-07-14 16:25:19 +02:00
*/
#include "GridAccount.h"
#define SET_ROW_COLOR(row, backcolor, forecolor) for(int i=0; i<NUMBER_COLS_OPS; i++) \
{ \
SetCellBackgroundColour(row, i, backcolor); \
SetCellTextColour(row, i, forecolor); \
}
2010-09-08 11:02:03 +02:00
#define SET_ROW_FONT(row, font) for(int i=0; i<NUMBER_COLS_OPS; i++) \
{ \
SetCellFont(row, i, font); \
}
#define UNESCAPE_CHARS(s) { \
s.Replace(wxT("\\\""), wxT("\""), true); \
s.Replace(wxT("\\\'"), wxT("\'"), true); \
}
BEGIN_EVENT_TABLE(GridAccount, wxGrid)
EVT_GRID_CELL_LEFT_CLICK(GridAccount::OnCellLeftClick )
END_EVENT_TABLE()
GridAccount::GridAccount(KissCount* kiss, wxWindow *parent, wxWindowID id) : wxGrid(parent, id),
_fixCosts(0), _week1(0), _week2(0), _week3(0), _week4(0), _kiss(kiss)
{
wxBitmap deleteBitmap(wxT(DELETE_ICON));
wxBitmap checkedBitmap(wxT(CHECKED_ICON));
DEFAULT_FONT(font);
int i;
User* user = _kiss->GetUser();
std::vector<Account>::iterator accountIt;
std::vector<Category>::iterator categoryIt;
CreateGrid(1, NUMBER_COLS_OPS);
SetColLabelSize(0);
SetRowLabelSize(0);
SetColSize (0, GetColSize(0)*3);
SetDefaultCellFont(font);
font.SetWeight(wxFONTWEIGHT_BOLD);
wxString colsName[] = {_("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), wxT(""), wxT("")};
for(i=0; i<NUMBER_COLS_OPS; i++)
{
SetCellValue(0, i, colsName[i]);
SetCellBackgroundColour(0, i, OWN_CYAN);
SetCellFont(0, i, font);
SetReadOnly(0, i, true);
SetCellAlignment(0, i, wxALIGN_CENTRE, wxALIGN_CENTRE);
}
SetCellRenderer(0, DELETE, new wxGridCellBitmapRenderer(deleteBitmap));
SetCellRenderer(0, CHECKED, new wxGridCellBitmapRenderer(checkedBitmap));
_accounts = new wxString[user->GetAccountsNumber()];
for (i=0,
accountIt = user->_accounts.begin();
accountIt != user->_accounts.end();
accountIt++, i++)
_accounts[i] = accountIt->name;
_categories = new wxString[user->GetCategoriesNumber()] ;
for(i=0, categoryIt = user->_categories.begin();
categoryIt != user->_categories.end();
categoryIt++, i++)
{
_categories[i] = categoryIt->name ;
}
}
GridAccount::~GridAccount()
{
delete[] _categories;
delete[] _accounts;
}
wxPen GridAccount::GetColGridLinePen (int col)
{return wxPen(*wxBLACK, 1, wxSOLID);}
wxPen GridAccount::GetRowGridLinePen (int row) {
if (row == 0 || row == _fixCosts ||
row == _week1 ||
row == _week2 ||
row == _week3 ||
row == _week4)
return wxPen(*wxBLACK, 1, wxSOLID);
return GetCellBackgroundColour(row, 0);
}
void GridAccount::SetWeek(int week, int line) {
switch (week) {
case 1: _week1 = line; break;
case 2: _week2 = line; break;
case 3: _week3 = line; break;
case 4: _week4 = line; break;
}
}
2010-09-12 18:10:40 +02:00
void GridAccount::LoadOperations(std::vector<Operation>* operations, bool canAddOperation, bool setWeek, int month, int year)
{
std::vector<Operation>::iterator it;
User* user = _kiss->GetUser();
int curLine = 0;
_operations = operations;
_canAddOperation = canAddOperation;
_fixCosts = 1;
it = _operations->begin();
for (;it != _operations->end() && it->fix_cost; it++)
if (setWeek)
InsertOperationWithWeek(user, &(*it), ++curLine, true, it->month, it->year);
else
InsertOperation(user, &(*it), ++curLine, true, it->month, it->year);
if (canAddOperation)
InsertOperation(user, NULL, ++curLine, true, month, year);
for (; it != _operations->end(); it++)
if (setWeek)
InsertOperationWithWeek(user, &(*it), ++curLine, false, it->month, it->year);
else
InsertOperation(user, &(*it), ++curLine, false, it->month, it->year);
if (canAddOperation)
InsertOperation(user, NULL, ++curLine, false, month, year);
AutoSizeColumn(CATEGORY, false);
AutoSizeColumn(DATE, false);
AutoSizeColumn(ACCOUNT, false);
AutoSizeColumn(DELETE, false);
AutoSizeColumn(CHECKED, false);
}
void GridAccount::InsertOperationWithWeek(User* user, Operation* op, int line, bool fix, int month, int year)
{
std::vector<Operation>::iterator it;
int curLine, curWeek, week, i;
InsertOperation(user, op, line, fix, month, year);
if (fix)
_fixCosts++;
if (op && !fix)
{
for (it = _operations->begin(), curLine=1;
it->fix_cost && it != _operations->end();
it++, curLine++) ;
if (it == _operations->end()) return;
curLine++;
curWeek = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth();
for (i=1, it++; it != _operations->end(); it++, curLine++)
{
week = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth();
if (week != curWeek)
{
SetWeek(i++, curLine);
curWeek = week;
}
}
}
}
void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, int month, int year)
{
std::vector<Operation>::iterator it;
int r, g, b;
wxColour color;
wxDateTime curDate;
wxString description;
2010-09-08 11:02:03 +02:00
wxFont font;
Category cat ;
curDate.SetToCurrent();
if (!op && !user->_accounts.size()) return;
InsertRows(line, 1);
SetCellEditor(line, DEBIT, new wxGridCellFloatEditor(wxID_ANY, 2));
SetCellEditor(line, CREDIT, new wxGridCellFloatEditor(wxID_ANY, 2));
wxGridCellChoiceEditor* accountEditor = new wxGridCellChoiceEditor(user->GetAccountsNumber(), _accounts, false);
SetCellEditor(line, ACCOUNT, accountEditor);
wxGridCellChoiceEditor* categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()-1, _categories+1, false);
SetCellEditor(line, CATEGORY, categoryEditor);
if (fix)
{
SetCellValue(line, CATEGORY, _("Fix"));
SetReadOnly(line, CATEGORY);
}
if (op)
{
2010-09-08 11:02:03 +02:00
cat = user->GetCategory(op->category);
2010-09-12 18:10:40 +02:00
SetCellEditor(line, DATE, new CalendarEditor(op->day, month, year));
description = op->description;
UNESCAPE_CHARS(description);
SetCellValue(line, DESCRIPTION, description);
2010-09-12 18:10:40 +02:00
SetCellValue(line, DATE, wxString::Format(wxT("%02d/%02d/%d"), op->day+1, month+1, year));
if (op->amount < 0)
SetCellValue(line, DEBIT, wxString::Format(wxT("%.2lf"), -op->amount));
else
SetCellValue(line, CREDIT, wxString::Format(wxT("%.2lf"), op->amount));
SetCellValue(line, ACCOUNT, user->GetAccountName(op->account));
if (!fix)
2010-09-12 18:10:40 +02:00
SetCellValue(line, CATEGORY, cat.name);
SetCellRenderer(line, DELETE, new wxGridCellBoolRenderer ());
SetCellEditor(line, DELETE, new wxGridCellBoolEditor ());
SetCellRenderer(line, CHECKED, new wxGridCellBoolRenderer ());
SetCellEditor(line, CHECKED, new wxGridCellFastBoolEditor ());
color = cat.backcolor;
if (op->checked)
{
r = ((color.Red()*1.5) >= 0xFF) ? 0xFF : color.Red()*1.5 ;
g = ((color.Green()*1.5) >= 0xFF) ? 0xFF : color.Green()*1.5 ;
b = ((color.Blue()*1.5) >= 0xFF) ? 0xFF : color.Blue()*1.5 ;
color.Set(r, g, b, color.Alpha());
SetCellValue(line, CHECKED, wxT("1"));
}
SET_ROW_COLOR(line, color, cat.forecolor);
2010-09-08 11:02:03 +02:00
if (cat.font.Length())
{
font = user->GetCategoryFont(cat.id);
SET_ROW_FONT(line, font);
}
}
else
{
2010-09-12 18:10:40 +02:00
SetCellEditor(line, DATE, new CalendarEditor(0, month, year));
if (!fix &&
2010-09-12 18:10:40 +02:00
curDate.GetMonth() == month &&
curDate.GetYear() == year)
{
2010-09-12 18:10:40 +02:00
SetCellValue(line, DATE, wxString::Format(wxT("%02d/%02d/%d"), curDate.GetDay(), month+1, year));
SetCellEditor(line, DATE, new CalendarEditor(curDate.GetDay()-1, month, year));
}
else
2010-09-12 18:10:40 +02:00
SetCellEditor(line, DATE, new CalendarEditor(0, month, year));
if (fix)
2010-09-08 11:02:03 +02:00
{
SET_ROW_COLOR(line, OWN_YELLOW, *wxBLACK);
2010-09-08 11:02:03 +02:00
}
else
{
SET_ROW_COLOR(line, OWN_GREEN, *wxBLACK);
2010-09-08 11:02:03 +02:00
}
SetReadOnly(line, CHECKED, true);
SetReadOnly(line, DELETE, true);
}
SetCellAlignment(line, DEBIT, wxALIGN_RIGHT, wxALIGN_CENTRE);
SetCellAlignment(line, CREDIT, wxALIGN_RIGHT, wxALIGN_CENTRE);
SetCellAlignment(line, DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE);
SetCellAlignment(line, CHECKED, wxALIGN_CENTRE, wxALIGN_CENTRE);
2010-09-08 11:02:03 +02:00
AutoSizeRow(line);
Layout();
SetMinSize(GetMinSize());
}
// From http://nomadsync.cvs.sourceforge.net/nomadsync/nomadsync/src/
void GridAccount::OnCellLeftClick(wxGridEvent& evt)
{
if (evt.GetCol() != DELETE && evt.GetCol() != CHECKED) { evt.Skip() ; return;}
// This forces the cell to go into edit mode directly
//m_waitForSlowClick = TRUE;
SetGridCursor(evt.GetRow(), evt.GetCol());
// Store the click co-ordinates in the editor if possible
// if an editor has created a ClientData area, we presume it's
// a wxPoint and we store the click co-ordinates
wxGridCellEditor* pEditor = GetCellEditor(evt.GetRow(), evt.GetCol());
wxPoint* pClickPoint = (wxPoint*)pEditor->GetClientData();
if (pClickPoint)
{
*pClickPoint = ClientToScreen(evt.GetPosition());
#ifndef __WINDOWS__
EnableCellEditControl(true);
#endif
}
// hack to prevent selection from being lost when click combobox
if (evt.GetCol() == 0 && IsInSelection(evt.GetRow(), evt.GetCol()))
{
//m_selTemp = m_selection;
m_selection = NULL;
}
pEditor->DecRef();
evt.Skip();
}