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:
2011-11-05 18:28:05 +01:00
parent 63b235d8db
commit c94bf51007
5 changed files with 117 additions and 129 deletions

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);