* Group/UnGroup seems to work

This commit is contained in:
Grégory Soutadé 2010-10-07 20:24:35 +02:00
parent 31c7b6f585
commit 477d155c3f
3 changed files with 170 additions and 16 deletions

View File

@ -124,7 +124,7 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxScrolledWindow(&(*
_tree.SetIndent(5);
wxButton* buttonGroup = new wxButton(this, GROUP_ID, _("Group"));
wxButton* buttonUnGroup = new wxButton(this, GROUP_ID, _("UnGroup"));
wxButton* buttonUnGroup = new wxButton(this, UNGROUP_ID, _("UnGroup"));
vbox3->Add(&_tree, 0);
vbox3->Add(-1, 30);

View File

@ -138,6 +138,7 @@ Operation& GridAccount::GetOperation(const wxString& id)
for(it=_operations->begin(); it!=_operations->end(); it++)
if (it->id == id)
return *it;
// I'm aware about the warning, but the method may not fail
}
void GridAccount::UpdateOperation(Operation& op)
@ -442,6 +443,7 @@ void GridAccount::InsertIntoGrid(Operation& op)
for(i=0; i<(int)_displayedOperations.size(); i++)
{
if (!_displayedOperations[i].id.Length()) continue;
if (_displayedOperations[i].parent.Length()) continue;
if ((_displayedOperations)[i].fix_cost && !op.fix_cost) continue;
if (!(_displayedOperations)[i].fix_cost && op.fix_cost) break;
if (user->_preferences[wxT("operation_order")] == wxT("ASC"))
@ -930,9 +932,57 @@ void GridAccount::UpdateMeta(Operation& op)
UpdateOperation(op);
}
void GridAccount::GetSelectedOperations(std::vector<int>* rows)
{
std::vector<int>::iterator it;
rows->clear();
// Blocks. We always expect btl and bbr to have the same size, since their
// entries are supposed to correspond.
const wxGridCellCoordsArray& btl(GetSelectionBlockTopLeft());
const wxGridCellCoordsArray& bbr(GetSelectionBlockBottomRight());
size_t blockCount = btl.size();
if (blockCount == bbr.size())
{
for (size_t i = 0; i < blockCount; ++i)
{
const wxGridCellCoords& tl = btl[i];
const wxGridCellCoords& br = bbr[i];
for (int row = tl.GetRow(); row <= br.GetRow(); ++row)
{
for (it=rows->begin(); it!=rows->end(); it++)
if (*it == row)
break;
if (it != rows->end()) continue;
rows->push_back(row);
}
}
}
// Singly selected cells.
const wxGridCellCoordsArray& cells(GetSelectedCells());
for (size_t i = 0; i < cells.size(); ++i)
{
const wxGridCellCoords& c = cells[i];
for (it=rows->begin(); it!=rows->end(); it++)
if (*it == c.GetRow())
break;
if (it != rows->end()) continue;
rows->push_back(c.GetRow());
}
}
void GridAccount::Group()
{
std::vector<int> rows;
std::vector<int> selected, rows;
std::vector<int>::iterator it;
std::vector<Operation> ops;
std::vector<Operation>::iterator it2;
@ -941,19 +991,11 @@ void GridAccount::Group()
Operation op, op2;
int fix = -1, i;
// Singly selected cells.
const wxGridCellCoordsArray& cells(GetSelectedCells());
for (size_t i = 0; i < cells.size(); ++i)
GetSelectedOperations(&selected);
for (size_t i = 0; i < selected.size(); ++i)
{
const wxGridCellCoords& c = cells[i];
for (it=rows.begin(); it!=rows.end(); it++)
if (*it == c.GetRow())
break;
if (it != rows.end()) continue;
op = _displayedOperations[c.GetRow()] ;
op = _displayedOperations[selected[i]] ;
if (op.id.Length())
{
@ -986,10 +1028,12 @@ void GridAccount::Group()
fix = op.fix_cost ? 1 : 0;
ops.push_back(op);
rows.push_back(c.GetRow());
rows.push_back(selected[i]);
}
}
if (!ops.size()) return;
if (!parent.Length())
{
if (rows.size() < 2) return;
@ -1080,7 +1124,116 @@ void GridAccount::Group()
InsertIntoGrid(op);
}
void GridAccount::UnGroup()
{
std::vector<int> rows, selected;
std::vector<int>::iterator it;
std::vector<Operation> ops;
std::vector<wxString> ops2;
std::vector<Operation>::iterator it2;
std::vector<wxString>::iterator it3;
wxString parent = wxT("");
Operation op, op2;
int fix = -1, i, a, line;
GetSelectedOperations(&selected);
for (size_t i = 0; i < selected.size(); ++i)
{
op = _displayedOperations[selected[i]] ;
if (op.id.Length())
{
if ((parent.Length() && op.parent != parent)
|| (!op.parent.Length() && !op.meta))
{
wxMessageBox(_("Cannot ungroup these operations"), _("Error"), wxICON_ERROR | wxOK);
return ;
}
if (fix != -1 && ((!fix && op.fix_cost) || (fix && !op.fix_cost)))
{
wxMessageBox(_("Cannot ungroup these operations"), _("Error"), wxICON_ERROR | wxOK);
return ;
}
if (fix == -1)
fix = op.fix_cost ? 1 : 0;
if(op.meta)
{
parent = op.id;
continue;
}
if (!parent.Length() && op.parent.Length())
parent = op.parent;
ops.push_back(op);
rows.push_back(selected[i]);
}
}
if (!ops.size() && !parent.Length()) return;
removeLastGroup:
// Only one meta is selected
if (!ops.size())
{
line = GetDisplayedRow(parent);
op = _displayedOperations[line];
ops2 = op.childs;
RemoveMeta(op, line, true, false);
for(i=0; i<(int)ops2.size(); i++)
{
op2 = GetOperation(ops2[i]);
op2.parent = wxT("");
_kiss->UpdateOperation(op2);
if (op2.fix_cost) _fixCosts--;
InsertIntoGrid(op2);
}
_kiss->DeleteOperation(op);
DeleteOperation(op);
if (op.fix_cost) _fixCosts--;
}
else
{
if (!parent.Length()) return;
line = GetDisplayedRow(parent);
op2 = _displayedOperations[line];
for(i=0; i<(int)ops.size(); i++)
{
op = ops[i];
op.parent = wxT("");
_kiss->UpdateOperation(op);
line = GetDisplayedRow(op.id);
DeleteRows(line, 1);
_displayedOperations.erase(_displayedOperations.begin()+line);
InsertIntoGrid(GetOperation(op.id)); // Don't use temp variable
if (op.fix_cost) _fixCosts--;
for (a=0; a<(int)op2.childs.size(); a++)
if (op2.childs[a] == op.id)
{
op2.childs.erase(op2.childs.begin()+a);
break;
}
}
line = GetDisplayedRow(parent);
_displayedOperations[line] = op2;
if (op2.childs.size() < 2)
{
ops.clear();
// Sorry ...
goto removeLastGroup;
}
_kiss->UpdateOperation(op2);
}
}

View File

@ -75,6 +75,7 @@ private:
Operation& GetOperation(const wxString& id);
void UpdateOperation(Operation& op);
int GetDisplayedRow(const wxString& id);
void GetSelectedOperations(std::vector<int>* rows);
DECLARE_EVENT_TABLE();
};