FreeDebks  1.0.3
 All Classes Files Functions Variables Friends Pages
FdCommandsCoa.cpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------
2 // Copyright © 2011-2013 Mathieu Schopfer
3 //
4 // This file is part of FreeDebks.
5 //
6 // FreeDebks is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // FreeDebks is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with FreeDebks. If not, see <http://www.gnu.org/licenses/>.
18 // --------------------------------------------------------------------
19 
20 #include "FdCommandsCoa.hpp"
21 #include "FdModelCoa.hpp"
22 #include "FdItemCoa.hpp"
23 
24 FdCoaAddItem::FdCoaAddItem(FdModelCoa *coa, FdItemCoa_p parent, FdItemCoa_p item, int row) :
25  QUndoCommand(), mCoa(coa), mParent(parent), mItem(item), mRow(row)
26 {
27  mWasAccount = 0;
28  setText(QObject::tr("add item", "Add a new item to the chart of accounts, ie a new row."));
29 }
30 
31 void FdCoaAddItem::redo()
32 {
33  if(mParent->type() == AccountType)
34  {
35  mWasAccount = mParent->initialBalance();
36  mParent->setInitialBalance(0);
37  }
39 }
40 
41 void FdCoaAddItem::undo()
42 {
44  if(mWasAccount)
45  mParent->setInitialBalance(mWasAccount);
46 }
47 
48 
49 FdCoaDragDrop::FdCoaDragDrop(FdModelCoa *coa, QList<FdItemCoa_p> items, FdItemCoa_p newParent, int row) :
50  QUndoCommand(), mCoa(coa), mItems(items), mNewParent(newParent), mRow(row)
51 {
52  mWasAccount = 0;
53  QString text;
54  int count = mItems.size();
55  if(count == 1)
56  {
57  if(items[0]->type() == CategoryType)
58  text = QObject::tr("move category %1", "Move an item in a drag and drop action. %1 is the item label").arg(items[0]->displayLabel());
59  else
60  text = QObject::tr("move account %1", "Move an item in a drag and drop action. %1 is the item label").arg(items[0]->displayLabel());
61  }
62  else
63  text = QObject::tr("move %n item(s)", "Move items in a drag and drop action.", count);
64  setText(text);
65 
66  mRemove = new FdCoaRemove(mCoa, mItems);
67 
68  // Manages the case when items are droped in last position in the children list of their own parent.
69  for(int i = 0; i < mItems.size(); ++i)
70  {
71  FdItemCoa_p oldParent = mItems[i]->parent();
72  if(oldParent == mNewParent && row == newParent->childrenCount())
73  mRow = mRow-1;
74  }
75 }
76 
78 {
79  mRemove->redo();
80  if(mNewParent->type() == AccountType)
81  mWasAccount = mNewParent->initialBalance();
83 }
84 
86 {
87  for(int i = 0; i < mItems.size(); ++i)
88  {
90  }
91  if(mWasAccount)
92  mNewParent->setInitialBalance(mWasAccount);
93  mRemove->undo();
94 }
95 
96 
97 FdCoaSetData::FdCoaSetData(FdModelCoa *coa, FdItemCoa_p item, CoaColumn column, QVariant newValue) :
98  QUndoCommand(), mCoa(coa), mItem(item), mColumn(column), mNewValue(newValue)
99 {
100  mOldValue = mCoa->data(mItem, mColumn);
101  switch(mColumn)
102  {
103  case CoaId:
104  setText(QObject::tr("change id of %1", "Will appear like this : Undo/Redo change id of ITEMLABEL.").arg(mItem->displayLabel()));
105  break;
106  case CoaLabel:
107  setText(QObject::tr("change label of %1", "Will appear like this : Undo/Redo change label of ITEMLABEL.").arg(mItem->displayLabel()));
108  break;
109  case CoaBalanceSheet:
110  setText(QObject::tr("change state (is on balance sheet) of %1", "Will appear like this : Undo/Redo change state (is on balance sheet) of ITEMLABEL.").arg(mItem->displayLabel()));
111  break;
112  case CoaSigns:
113  setText(QObject::tr("change account signs %1", "Will appear like this : Undo/Redo change signs of ITEMLABEL.").arg(mItem->displayLabel()));
114  break;
115  case CoaInitial:
116  setText(QObject::tr("change initial balance %1", "Will appear like this : Undo/Redo change initial balance of ITEMLABEL.").arg(mItem->displayLabel()));
117  break;
118  }
119 }
120 
121 void FdCoaSetData::redo()
122 {
124 }
125 
126 void FdCoaSetData::undo()
127 {
129 }
130 
131 
132 FdCoaRemove::FdCoaRemove(FdModelCoa *coa, QList<FdItemCoa_p> selected) :
133  QUndoCommand(), mCoa(coa)
134 {
135  QString text;
136  int count = selected.size();
137  if(count == 1)
138  {
139  if(selected[0]->type() == CategoryType)
140  text = QObject::tr("remove category %1", "Will appear like this : Undo/Redo remove category ITEMLABEL.").arg(selected[0]->displayLabel());
141  else
142  text = QObject::tr("remove account %1", "Will appear like this : Undo/Redo remove account ITEMLABEL.").arg(selected[0]->displayLabel());
143  }
144  else
145  text = QObject::tr("remove %n item(s)", "Remove items from the chart of accounts.", count);
146  setText(text);
147 
148  for(int i = 0; i < count; ++i)
149  {
150  struct locationCoa location;
151  location.item = selected[i];
152  location.parent = location.item->parent();
153  location.row = location.item->row();
154  mMap << location;
155  }
156 }
157 
158 void FdCoaRemove::redo()
159 {
160  for(int i = 0; i < mMap.size(); ++i)
161  mCoa->removeItem(mMap[i].item, mMap[i].parent);
162 
164 }
165 
166 void FdCoaRemove::undo()
167 {
168  for(int i = 0; i < mMap.size(); ++i)
169  mCoa->insertItem(mMap[i].item, mMap[i].row, mMap[i].parent);
170 
172 }