KissCount/src/view/grid/GridAccount.cpp

629 lines
18 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()
enum {GRID_ID};
GridAccount::GridAccount(KissCount* kiss, wxWindow *parent, wxWindowID id) : wxGrid(parent, id), _fixCosts(0), _week1(0),
_week2(0), _week3(0), _week4(0), _kiss(kiss),
_curMonth(0), _curYear(0)
{
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 (DESCRIPTION, GetColSize(DESCRIPTION)*3);
SetDefaultCellFont(font);
font.SetWeight(wxFONTWEIGHT_BOLD);
wxString colsName[] = {wxT(""), _("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 ;
}
Connect(id, wxEVT_GRID_CELL_CHANGE, wxGridEventHandler(GridAccount::OnOperationModified), NULL, this);
}
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::ResetWeeks()
{
_week1 = _week2 = _week3 = _week4 = 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;
_curMonth = month;
_curYear = year;
_displayedOperations.clear();
_displayedOperations.push_back(NULL); // Header
2010-09-12 18:10:40 +02:00
_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(TREE, false);
2010-09-12 18:10:40 +02:00
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;
2010-09-12 18:10:40 +02:00
int curLine, curWeek, week, i;
InsertOperation(user, op, line, fix, month, year);
if (fix)
_fixCosts++;
if (op && !fix)
{
for (it = _displayedOperations.begin(), curLine=0;
it != _displayedOperations.end();
it++, curLine++)
{
if (*it && !(*it)->fix_cost) break;
}
if (it == _displayedOperations.end()) return;
2010-09-12 18:10:40 +02:00
ResetWeeks();
2010-09-12 18:10:40 +02:00
curWeek = wxDateTime((*it)->day+1, (wxDateTime::Month)(*it)->month, (*it)->year).GetWeekOfMonth();
it++;
for (i=1; it != _displayedOperations.end(); it++, curLine++)
2010-09-12 18:10:40 +02:00
{
if (!*it) continue;
week = wxDateTime((*it)->day+1, (wxDateTime::Month)(*it)->month, (*it)->year).GetWeekOfMonth();
2010-09-12 18:10:40 +02:00
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();
// // First is header
// if (op)
_displayedOperations.insert(_displayedOperations.begin()+line, op);
if (!user->_accounts.size()) return;
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);
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() != TREE && 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();
}
void GridAccount::DeleteOperation(const Operation& op)
{
std::vector<Operation>::iterator it;
for (it=_operations->begin(); it!=_operations->end(); it++)
if (it->id == op.id)
{
_operations->erase(it);
break;
}
}
void GridAccount::OnOperationModified(wxGridEvent& event)
{
User* user = _kiss->GetUser();
int row = event.GetRow();
int col = event.GetCol();
Operation new_op, cur_op, op_tmp;
int op_complete = 6, i;
wxString value ;
wxDateTime date;
bool need_insertion = false, fix_op=false;
static bool inModification = false ;
wxColour color ;
unsigned char r, g, b;
// Avoid recursives calls
if (inModification) return;
inModification = true ;
if (col == DEBIT)
SetCellValue(row, CREDIT, wxT(""));
else if (col == CREDIT)
SetCellValue(row, DEBIT, wxT(""));
value = GetCellValue(row, DESCRIPTION);
if (value.Length())
{
new_op.description = value;
op_complete--;
}
value = GetCellValue(row, DATE);
if (value.Length())
{
date.ParseFormat(value, wxT("%d/%m/%Y"));
new_op.day = date.GetDay()-1;
new_op.month = date.GetMonth();
new_op.year = date.GetYear();
op_complete--;
}
value = GetCellValue(row, DEBIT);
if (value.Length())
{
value.ToDouble(&new_op.amount);
new_op.amount *= -1.0;
op_complete--;
}
value = GetCellValue(row, CREDIT);
if (value.Length())
{
value.ToDouble(&new_op.amount);
op_complete--;
}
value = GetCellValue(row, CATEGORY);
if (value.Length())
{
new_op.category = user->GetCategoryId(value);
op_complete--;
}
value = GetCellValue(row, ACCOUNT);
if (value.Length())
{
new_op.account = user->GetAccountId(value);
op_complete--;
}
value = GetCellValue(row, CHECKED);
if (value.Length() && value != wxT("0"))
new_op.checked = true;
else
new_op.checked = false;
op_complete--;
if (col == DESCRIPTION &&
(!GetCellValue(row, CATEGORY).Length() ||
!GetCellValue(row, ACCOUNT).Length()))
{
if (_kiss->SearchPreviousOperation(&op_tmp, new_op.description, _curMonth, _curYear))
{
new_op.category = op_tmp.category;
new_op.account = op_tmp.account;
SetCellValue(row, CATEGORY, user->GetCategoryName(new_op.category));
SetCellValue(row, ACCOUNT, user->GetAccountName(new_op.account));
op_complete -= 2;
}
}
if (col == CHECKED || col == CATEGORY)
{
color = user->GetCategory(new_op.category).backcolor;
if (new_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());
}
SET_ROW_COLOR(row, color, user->GetCategory(new_op.category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(new_op.category));
}
if (col == DELETE)
{
wxMessageDialog dialog(this, _("Are you sure want to delete : \n")+new_op.description, wxT("KissCount"), wxYES_NO);
if (dialog.ShowModal() == wxID_NO)
{
SetCellValue(row, col, wxT("0"));
inModification = false;
return;
}
}
// Modify a fix operation
if (row < _fixCosts)
{
cur_op = *(_displayedOperations)[row] ;
if (col == DELETE)
{
DeleteRows(row, 1);
DeleteOperation(cur_op);
_kiss->DeleteOperation(cur_op);
_displayedOperations.erase(_displayedOperations.begin()+row);
_fixCosts = _fixCosts--;
inModification = false ;
event.Skip();
return ;
}
new_op.id = cur_op.id;
new_op.fix_cost = true;
new_op.transfert = cur_op.transfert;
if (cur_op.day != new_op.day)
{
need_insertion = true;
DeleteRows(row, 1);
DeleteOperation(cur_op);
_displayedOperations.erase(_displayedOperations.begin()+row);
_fixCosts--;
_kiss->UpdateOperation(new_op);
}
else
{
_kiss->UpdateOperation(new_op);
*(_displayedOperations)[row] = new_op;
}
fix_op = true;
}
// Add a fixCost
else if (row == _fixCosts)
{
if (op_complete) {
inModification = false ;
return ;
}
need_insertion = true;
fix_op = true;
new_op.fix_cost = true;
for(i=0; i<NUMBER_COLS_OPS; i++)
{
if (i == CATEGORY) continue;
SetCellValue(row, i, wxT(""));
}
DEFAULT_FONT(font);
SET_ROW_COLOR(row, OWN_GREEN, *wxBLACK);
SET_ROW_FONT(row, font);
new_op.id = _kiss->AddOperation(new_op);
}
// Modify an operation
else if (row < (int)(_displayedOperations.size()-1))
{
cur_op = *(_displayedOperations)[row] ;
new_op.id = cur_op.id;
new_op.fix_cost = false;
new_op.transfert = cur_op.transfert;
if (col == DELETE)
{
DeleteRows(row, 1);
DeleteOperation(cur_op);
_displayedOperations.erase(_displayedOperations.begin()+row);
_kiss->DeleteOperation(cur_op);
inModification = false ;
event.Skip();
return ;
}
if (cur_op.day != new_op.day)
{
need_insertion = true;
DeleteRows(row, 1);
DeleteOperation(cur_op);
_displayedOperations.erase(_displayedOperations.begin()+row);
_kiss->UpdateOperation(new_op);
}
else
{
_kiss->UpdateOperation(new_op);
*(_displayedOperations)[row] = new_op;
}
}
// Add an operation
else
{
if (op_complete) {
inModification = false ;
return ;
}
need_insertion = true;
fix_op = false;
new_op.fix_cost = false;
for(i=0; i<NUMBER_COLS_OPS; i++)
{
SetCellValue(row, i, wxT(""));
}
DEFAULT_FONT(font);
SET_ROW_COLOR(row, OWN_GREEN, *wxBLACK);
SET_ROW_FONT(row, font);
new_op.id = _kiss->AddOperation(new_op);
}
if (need_insertion)
{
for(i=0; i<(int)_displayedOperations.size(); i++)
{
if (_displayedOperations[i] == NULL) continue;
if ((_displayedOperations)[i]->fix_cost && !fix_op) continue;
if (!(_displayedOperations)[i]->fix_cost && fix_op) break;
if (user->_preferences[wxT("operation_order")] == wxT("ASC"))
{
if ((_displayedOperations)[i]->day > new_op.day)
break;
}
else
{
if ((_displayedOperations)[i]->day < new_op.day)
break;
}
}
if (i == (int)_displayedOperations.size()) i--;
_operations->push_back(new_op);
InsertOperationWithWeek(user, &((*_operations)[_operations->size()-1]), i, fix_op, _curMonth, _curYear);
if (fix_op)
_fixCosts = _fixCosts+1;
}
inModification = false ;
event.Skip();
}