Factorize some code in GridAccount

Set default font for everybody
Fix a bug in DateDelegate (bad month)
Fix a bug in GridAccount : Tab added for sub operations when saving
Fix a bug in GridAccount : credit was considered as a debit
This commit is contained in:
Grégory Soutadé 2011-11-05 18:28:05 +01:00
parent 63b235d8db
commit c94bf51007
5 changed files with 117 additions and 129 deletions

View File

@ -97,7 +97,7 @@ int User::GetCategoryId(const QString& catName)
const QFont User::GetCategoryFont(int catId)
{
QFont f;
DEFAULT_FONT(f);
Category cat;
for (unsigned int i=0; i<_categories.size(); i++)

View File

@ -17,9 +17,6 @@
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
*/
// #include "grid/wxGridCellBitmapRenderer.hpp"
// #include "GenerateDialog.hpp"
#include <QHeaderView>
#include <QGroupBox>
#include <QMessageBox>
@ -45,11 +42,10 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, pare
User* user = _kiss->GetUser();
std::vector<Account>::iterator accountIt;
std::vector<Category>::iterator categoryIt;
DEFAULT_FONT(font);
int nbCategories;
setLayout(hbox);
_tree = new QTreeWidget(this);
_tree->headerItem()->setHidden(true);
_tree->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
@ -65,7 +61,6 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, pare
_calendar->setFirstDayOfWeek(Qt::Monday);
_calendar->setNavigationBarVisible(false);
_calendar->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
_calendar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
_accounts = new QString[user->GetAccountsNumber()];
@ -215,7 +210,7 @@ void AccountPanel::InitStatsGrid(User* user)
{
int i;
int nb_categories = user->GetCategoriesNumber();
QFont font;
DEFAULT_FONT(font);
if (!_statsGrid->rowCount())
{
@ -228,11 +223,8 @@ void AccountPanel::InitStatsGrid(User* user)
}
_statsGrid->setRowCount(nb_categories+CATS_STATS+1); // Headers + blank + categories + non fix
// _statsGrid->SetDefaultCellFont(font);
_statsGrid->setItem(TOTAL_CREDIT, 0, new QTableWidgetItem(_("Total Credit")));
_statsGrid->setItem(TOTAL_DEBIT, 0, new QTableWidgetItem(_("Total Debit")));
font = _statsGrid->item(TOTAL_DEBIT, 0)->font();
for(i=0; i<nb_categories; i++)
{
@ -372,7 +364,6 @@ void AccountPanel::ShowMonth(int month, int year)
// std::vector<Operation> operations;
// _fixCosts = 0;
User* user = _kiss->GetUser();
// DEFAULT_FONT(font);
// std::vector<Category>::iterator categoryIt;
std::map<unsigned int, std::vector<Operation> >::iterator monthIt;
QDate curDate = QDate::currentDate();
@ -445,7 +436,9 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year)
int i, a;
QTableWidgetItem* item;
QString v;
QFont font;
DEFAULT_FONT(font);
font.setBold(true);
if (_accountsGrid->rowCount())
_accountsGrid->clear();
@ -458,8 +451,7 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year)
for(i=0; i<NUMBER_COLS_ACCOUNTS; i++)
{
QFont font = _accountsGrid->horizontalHeaderItem(i)->font();
font.setBold(true);
_accountsGrid->horizontalHeaderItem(i)->font();
_accountsGrid->horizontalHeaderItem(i)->setFont(font);
}
@ -487,11 +479,7 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year)
if (a != ACCOUNT_INIT)
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
if (a == ACCOUNT_CUR)
{
font = item->font();
font.setBold(true);
item->setFont(font);
}
}
_accountsInitValues[it->id] = value;
_accountsGrid->item(curLine, ACCOUNT_INIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);

View File

@ -38,7 +38,7 @@ void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
QComboBox *combo = qobject_cast<QComboBox *>(editor);
QString s ;
s = s.sprintf("%02d/%02d/%04d", combo->currentIndex()+1, _month+1, _year);
s = s.sprintf("%02d/%02d/%04d", combo->currentIndex()+1, _month, _year);
model->setData(index, qVariantFromValue(s));
}

View File

@ -53,8 +53,7 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent,
_curMonth(0), _curYear(0), _treeSignalMapper(this), _checkSignalMapper(this),
_deleteSignalMapper(this), _inModification(false)
{
//DEFAULT_FONT(font);
QFont font;
DEFAULT_FONT(font);
int i;
User* user = _kiss->GetUser();
std::vector<Account>::iterator accountIt;
@ -69,16 +68,15 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent,
setShowGrid(false);
setColumnWidth (DESCRIPTION, columnWidth(DESCRIPTION)*3);
//SetDefaultCellFont(font);
setFont(font);
font.setBold(true);
QString colsName[] = {"", _("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), "", ""};
for(i=0; i<NUMBER_COLS_OPS; i++)
{
item = new QTableWidgetItem(colsName[i]);
item->setText(colsName[i]);
item->setBackground(view::OWN_CYAN);
font = item->font();
font.setBold(true);
item->setFont(font);
SET_READ_ONLY(item);
item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
@ -312,7 +310,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
QColor color;
QDate curDate = QDate::currentDate();
QString description, v;
QFont font;
DEFAULT_FONT(font);
Category cat ;
Operation op2;
QTableWidgetItem* item;
@ -473,6 +471,9 @@ void GridAccount::InsertIntoGrid(Operation& op)
User* user = _kiss->GetUser();
Operation parent;
if (op.parent)
parent = GetOperation(op.parent);
// No previous fix operations
if (op.fix_cost && !_displayedOperations[1].id)
i = 1;
@ -503,7 +504,6 @@ void GridAccount::InsertIntoGrid(Operation& op)
if (op.parent)
{
parent = GetOperation(op.parent);
if ((i-start) > (int)(parent.childs.size()))
i = start + parent.childs.size();
if (parent.day >= op.day)
@ -783,37 +783,48 @@ void GridAccount::OnDeleteClicked(int id)
_kiss->DeleteOperation(*it);
_displayedOperations.erase(_displayedOperations.begin()+row);
if (op.parent && op_tmp.childs.size() < 2)
if (op.parent)
{
if (op.childs.size() == 1)
if (op_tmp.childs.size() < 2)
{
op_tmp2 = GetOperation(op_tmp.childs[0]);
op_tmp2.parent = 0;
UpdateOperation(op_tmp2);
row = GetDisplayedRow(op_tmp2.id);
_displayedOperations[row] = op_tmp2;
}
row = GetDisplayedRow(op.parent);
removeRow(row);
DeleteOperation(op_tmp);
if (_databaseSynchronization)
_kiss->DeleteOperation(op_tmp);
_displayedOperations.erase(_displayedOperations.begin()+row);
if (op.fix_cost)
_fixCosts--;
setItem(row, DESCRIPTION, new QTableWidgetItem(op.description)); // Remove tabulation
color = user->GetCategory(op.category).backcolor;
if (op_tmp.childs.size() == 1)
{
op_tmp2 = GetOperation(op_tmp.childs[0]);
op_tmp2.parent = 0;
UpdateOperation(op_tmp2);
row = GetDisplayedRow(op_tmp2.id);
_displayedOperations[row] = op_tmp2;
}
if (op.checked)
row = GetDisplayedRow(op.parent);
removeRow(row);
DeleteOperation(op_tmp);
if (_databaseSynchronization)
_kiss->DeleteOperation(op_tmp);
_displayedOperations.erase(_displayedOperations.begin()+row);
if (op.fix_cost)
_fixCosts--;
setItem(row, DESCRIPTION, new QTableWidgetItem(op.description)); // Remove tabulation
color = user->GetCategory(op.category).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.setRgb(r, g, b);
}
SET_ROW_COLOR(row, color, user->GetCategory(op.category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(op.category));
}
else
{
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.setRgb(r, g, b);
UpdateMeta(op_tmp);
row = GetDisplayedRow(op_tmp.id);
RemoveMeta(op_tmp, row, true, false);
InsertIntoGrid(op_tmp);
}
SET_ROW_COLOR(row, color, user->GetCategory(op.category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(op.category));
}
if (op.fix_cost)
_fixCosts--;
@ -839,6 +850,7 @@ void GridAccount::OnOperationModified(int row, int col)
double amount;
QFont font;
Category cat ;
bool fix_cost;
// Avoid recursives calls
if (_inModification || _loadOperations) return;
@ -855,7 +867,7 @@ void GridAccount::OnOperationModified(int row, int col)
value = item(row, DESCRIPTION)->text();
if (value.length())
{
new_op.description = value;
new_op.description = value.trimmed();
op_complete--;
}
@ -910,7 +922,6 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.amount *= -1.0;
setItem(row, DEBIT, new QTableWidgetItem(value.sprintf("%.2lf", new_op.amount)));
}
if (new_op.amount != 0.0) new_op.amount *= -1.0;
op_complete--;
new_op.formula = _displayedOperations[row].formula;
}
@ -937,7 +948,7 @@ void GridAccount::OnOperationModified(int row, int col)
color = user->GetCategory(new_op.category).backcolor;
if (col == CATEGORY && new_op.checked)
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 ;
@ -948,11 +959,15 @@ void GridAccount::OnOperationModified(int row, int col)
SET_ROW_COLOR(row, color, user->GetCategory(new_op.category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(new_op.category));
// Modify a fix operation
if (row < _fixCosts || !_canAddOperation)
fix_cost = (row <= _fixCosts);
// Modify an operation
if (!_canAddOperation || (row < _fixCosts ||
(row > _fixCosts &&
row < (int)(_displayedOperations.size()-1))))
{
new_op.id = cur_op.id;
new_op.fix_cost = true;
new_op.fix_cost = fix_cost;
new_op.transfert = cur_op.transfert;
new_op.meta = cur_op.meta;
new_op.parent = cur_op.parent;
@ -965,62 +980,13 @@ void GridAccount::OnOperationModified(int row, int col)
removeRow(row);
DeleteOperation(cur_op);
_displayedOperations.erase(_displayedOperations.begin()+row);
_fixCosts--;
if (fix_cost)
_fixCosts--;
}
else
{
UpdateOperation(new_op);
(_displayedOperations)[row] = new_op;
}
}
// Add a fixCost
else if (row == _fixCosts)
{
if (op_complete) {
_inModification = false ;
return ;
}
need_insertion = true;
new_op.fix_cost = true;
new_op.meta = false;
new_op._virtual = false;
for(i=0; i<NUMBER_COLS_OPS; i++)
{
if (i == CATEGORY) continue;
setItem(row, i, new QTableWidgetItem(""));
}
DEFAULT_FONT(font);
SET_ROW_COLOR(row, view::OWN_YELLOW, Qt::black);
SET_ROW_FONT(row, font);
new_op.id = _kiss->AddOperation(new_op);
}
// Modify an operation
else if (row < (int)(_displayedOperations.size()-1))
{
new_op.id = cur_op.id;
new_op.fix_cost = false;
new_op.transfert = cur_op.transfert;
new_op.meta = cur_op.meta;
new_op.parent = cur_op.parent;
new_op.childs = cur_op.childs;
new_op._virtual = cur_op._virtual;
if (cur_op.day != new_op.day)
{
need_insertion = true;
removeRow(row);
DeleteOperation(cur_op);
_displayedOperations.erase(_displayedOperations.begin()+row);
}
else
{
UpdateOperation(new_op);
(_displayedOperations)[row] = new_op;
}
UpdateOperation(new_op);
}
// Add an operation
else
@ -1030,38 +996,59 @@ void GridAccount::OnOperationModified(int row, int col)
return ;
}
need_insertion = true;
new_op.fix_cost = false;
new_op.fix_cost = fix_cost;
new_op.meta = false;
new_op._virtual = false;
new_op.parent = 0;
for(i=0; i<NUMBER_COLS_OPS; i++)
{
if (fix_cost && i == CATEGORY) continue;
setItem(row, i, new QTableWidgetItem(""));
}
DEFAULT_FONT(font);
SET_ROW_COLOR(row, view::OWN_GREEN, Qt::black);
if (fix_cost)
{
SET_ROW_COLOR(row, view::OWN_YELLOW, Qt::black);
}
else
{
SET_ROW_COLOR(row, view::OWN_GREEN, Qt::black);
}
SET_ROW_FONT(row, font);
new_op.id = _kiss->AddOperation(new_op);
}
if (user->GetAccount(new_op.account).blocked && new_op.amount < 0)
if (!new_op.meta && user->GetAccount(new_op.account).blocked && new_op.amount < 0)
QMessageBox::warning(0, _("Warning"), _("You made a debit on a blocked account"));
if (need_insertion)
{
InsertIntoGrid(new_op);
UpdateOperation(new_op);
}
InsertIntoGrid(new_op);
if (new_op.parent)
{
row = GetDisplayedRow(new_op.parent);
last_day = new_op.day;
new_op = _displayedOperations[row];
it = std::find(new_op.childs.begin(), new_op.childs.end(), cur_op.id);
new_op.childs.erase(it);
i = 0;
for(it = new_op.childs.begin(); it != new_op.childs.end(); it++)
{
op2 = GetOperation(*it);
if (op2.day > last_day) break;
i++;
}
new_op.childs.insert(new_op.childs.begin()+i, cur_op.id);
last_day = new_op.day;
UpdateMeta(new_op);
@ -1075,7 +1062,7 @@ void GridAccount::OnOperationModified(int row, int col)
else
color = view::OWN_GREEN;
QCheckBox* checkBox = qobject_cast<QCheckBox*> (_checkSignalMapper.mapping(op.parent));
QCheckBox* checkBox = qobject_cast<QCheckBox*> (_checkSignalMapper.mapping(new_op.id));
if (new_op.checked)
{
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
@ -1087,15 +1074,6 @@ void GridAccount::OnOperationModified(int row, int col)
else
checkBox->setCheckState(Qt::Unchecked);
SET_ROW_COLOR(row, color, cat.forecolor);
if (new_op.category && cat.font.length())
{
font = user->GetCategoryFont(cat.id);
SET_ROW_FONT(row, font);
}
setItem(row, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", new_op.day+1, _curMonth+1, _curYear)));
if (!_displayedOperations[row].amount)
@ -1119,6 +1097,17 @@ void GridAccount::OnOperationModified(int row, int col)
}
}
this->item(row, OP_DATE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
this->item(row, DEBIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
this->item(row, CREDIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
SET_ROW_COLOR(row, color, cat.forecolor);
if (new_op.category && cat.font.length())
{
SET_ROW_FONT(row, user->GetCategoryFont(cat.id));
}
// Move updated meta
if ((int)new_op.day != last_day)
{
@ -1156,6 +1145,11 @@ void GridAccount::UpdateMeta(Operation& meta)
meta.checked = true;
meta.amount = 0;
op = GetOperation(meta.childs[0]);
meta.year = op.year;
meta.month = op.month;
meta.day = op.day;
for(it=meta.childs.begin(); it!=meta.childs.end(); it++)
{
op = GetOperation(*it);

View File

@ -32,6 +32,8 @@ class ImportEngine;
#include <controller/KissCount.hpp>
#include "view.hpp"
#include <qtranslator.h>
#include <qtextcodec.h>
@ -101,7 +103,11 @@ public:
_kiss(kiss),
_wxUI(parent),
_KissButton(0)
{hide();}
{
DEFAULT_FONT(font);
hide();
setFont(font);
}
virtual void OnShow()=0;
virtual KissPanel* CreatePanel()=0;