Diffstat (limited to 'noncore/apps/qashmoney/accountdisplay.cpp') (more/less context) (show whitespace changes)
-rwxr-xr-x | noncore/apps/qashmoney/accountdisplay.cpp | 432 |
1 files changed, 432 insertions, 0 deletions
diff --git a/noncore/apps/qashmoney/accountdisplay.cpp b/noncore/apps/qashmoney/accountdisplay.cpp new file mode 100755 index 0000000..46ab1db --- a/dev/null +++ b/noncore/apps/qashmoney/accountdisplay.cpp | |||
@@ -0,0 +1,432 @@ | |||
1 | #include <qdatetime.h> | ||
2 | #include <qmessagebox.h> | ||
3 | #include <qheader.h> | ||
4 | |||
5 | #include "accountdisplay.h" | ||
6 | #include "newaccount.h" | ||
7 | #include "transaction.h" | ||
8 | #include "transferdialog.h" | ||
9 | #include "preferences.h" | ||
10 | #include "transfer.h" | ||
11 | |||
12 | extern Account *account; | ||
13 | extern Transaction *transaction; | ||
14 | extern Transfer *transfer; | ||
15 | extern Preferences *preferences; | ||
16 | |||
17 | AccountDisplay::AccountDisplay ( QWidget *parent ) : QWidget ( parent ) | ||
18 | { | ||
19 | cleared = 0; | ||
20 | |||
21 | firstline = new QHBox ( this ); | ||
22 | firstline->setSpacing ( 2 ); | ||
23 | |||
24 | newaccount = new QPushButton ( firstline ); | ||
25 | newaccount->setPixmap ( QPixmap ("/opt/QtPalmtop/pics/new.png") ); | ||
26 | connect ( newaccount, SIGNAL ( released () ), this, SLOT ( addAccount () ) ); | ||
27 | |||
28 | editaccount = new QPushButton ( firstline ); | ||
29 | editaccount->setPixmap ( QPixmap ("/opt/QtPalmtop/pics/edit.png") ); | ||
30 | connect ( editaccount, SIGNAL ( released () ), this, SLOT ( editAccount () ) ); | ||
31 | |||
32 | deleteaccount = new QPushButton ( firstline ); | ||
33 | deleteaccount->setPixmap( QPixmap ( "/opt/QtPalmtop/pics/delete.png") ); | ||
34 | connect ( deleteaccount, SIGNAL ( released () ), this, SLOT ( deleteAccount () ) ); | ||
35 | |||
36 | transferbutton = new QPushButton ( firstline ); | ||
37 | transferbutton->setPixmap( QPixmap ( "/opt/QtPalmtop/pics/transfer.png") ); | ||
38 | transferbutton->setToggleButton ( TRUE ); | ||
39 | connect ( transferbutton, SIGNAL ( toggled ( bool ) ), this, SLOT ( accountTransfer ( bool ) ) ); | ||
40 | |||
41 | listview = new QListView ( this ); | ||
42 | listview->setAllColumnsShowFocus ( TRUE ); | ||
43 | listview->setShowSortIndicator ( TRUE ); | ||
44 | listview->setRootIsDecorated ( TRUE ); | ||
45 | listview->setMultiSelection ( FALSE ); | ||
46 | connect ( listview, SIGNAL ( expanded ( QListViewItem * ) ), this, SLOT ( setAccountExpanded ( QListViewItem * ) ) ); | ||
47 | connect ( listview, SIGNAL ( collapsed ( QListViewItem * ) ), this, SLOT ( setAccountCollapsed ( QListViewItem * ) ) ); | ||
48 | |||
49 | listview->header()->setTracking ( FALSE ); | ||
50 | connect ( listview->header(), SIGNAL ( sizeChange ( int, int, int ) ), this, SLOT ( saveColumnSize ( int, int, int ) ) ); | ||
51 | |||
52 | layout = new QVBoxLayout ( this, 2, 5 ); | ||
53 | layout->addWidget ( firstline ); | ||
54 | layout->addWidget ( listview ); | ||
55 | } | ||
56 | |||
57 | void AccountDisplay::setTabs ( QWidget *newtab2, QTabWidget *newtabs ) | ||
58 | { | ||
59 | tab2 = newtab2; | ||
60 | maintabs = newtabs; | ||
61 | } | ||
62 | |||
63 | void AccountDisplay::addAccount () | ||
64 | { | ||
65 | // initialize local variables | ||
66 | int parentid = 0; | ||
67 | type = 0; | ||
68 | QString parentlist [ listview->childCount() + 1 ] [ 3 ] ; | ||
69 | |||
70 | // create new account window for entering data | ||
71 | NewAccount *newaccount = new NewAccount ( this ); | ||
72 | int width = this->width(); | ||
73 | newaccount->accountbox->setMaximumWidth ( ( int ) ( width * 0.5 ) ); | ||
74 | newaccount->datebox->setMaximumWidth ( ( int ) ( width * 0.4 ) ); | ||
75 | newaccount->childbox->setMaximumWidth ( ( int ) ( width * 0.5 ) ); | ||
76 | newaccount->balancebox->setMaximumWidth ( ( int ) ( width * 0.4 ) ); | ||
77 | newaccount->creditlimitbox->setMaximumWidth ( ( int ) ( width * 0.4 ) ); | ||
78 | |||
79 | // if there are no accounts, disable the child check box | ||
80 | if ( account->getNumberOfAccounts () == 0 ) | ||
81 | newaccount->childcheckbox->setEnabled ( FALSE ); | ||
82 | |||
83 | // if there are accounts, fill up the pulldown menu for | ||
84 | // selecting a parent account. We should only add those parents without transactions | ||
85 | else | ||
86 | { | ||
87 | int c = 0; | ||
88 | QListViewItemIterator it ( listview ); | ||
89 | for ( ; it.current(); ++it ) | ||
90 | { | ||
91 | int id = it.current()->text ( getIDColumn() ).toInt(); | ||
92 | // iterate through accountdisplay listview and add parents with no transactions | ||
93 | // add this item to the list box only if it is a parent and has no transactions | ||
94 | if ( transfer->getNumberOfTransfers ( id ) == 0 && transaction->getNumberOfTransactions ( id ) == 0 && it.current()->parent() == 0 ) | ||
95 | { | ||
96 | newaccount->childbox->insertItem ( it.current()->text ( 0 ) ); | ||
97 | parentlist [ c ] [ 0 ] = it.current()->text ( 0 ); | ||
98 | parentlist [ c ] [ 1 ] = it.current()->text ( getIDColumn() ); | ||
99 | parentlist [ c ] [ 2 ] = QString::number ( c ); | ||
100 | c++; | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | if ( preferences->getPreference ( 4 ) == 0 ) | ||
106 | newaccount->currencybox->setEnabled ( FALSE ); | ||
107 | |||
108 | // enter today's date in the date box as default | ||
109 | QDate today = QDate::currentDate (); | ||
110 | int defaultday = today.day(); | ||
111 | int defaultmonth = today.month(); | ||
112 | int defaultyear = today.year(); | ||
113 | newaccount->startdate->setText ( preferences->getDate ( defaultyear, defaultmonth, defaultday ) ); | ||
114 | |||
115 | //add account information if user pushes OK button | ||
116 | if ( newaccount->exec() == QDialog::Accepted ) | ||
117 | { | ||
118 | if ( newaccount->childcheckbox->isChecked () == TRUE ) // set a parent id and type for a child account | ||
119 | { | ||
120 | // go through the parentlist we created and determine the parent accountid | ||
121 | // we can't use the name of the account because there may be two accounts | ||
122 | // with the same name. This function does it all by accountid | ||
123 | int counter; | ||
124 | for ( counter = 0; counter < listview->childCount() + 1; counter++ ) | ||
125 | if ( ( parentlist [ counter ] [ 2 ].toInt() ) == newaccount->childbox->currentItem() ) | ||
126 | { | ||
127 | parentid = parentlist [ counter ] [ 1 ].toInt(); | ||
128 | break; | ||
129 | } | ||
130 | type = ( newaccount->accounttype->currentItem() ) + 6; // sets account ids for child accounts. See accountdisplay.h for types | ||
131 | } | ||
132 | else | ||
133 | { | ||
134 | parentid = -1; | ||
135 | type = newaccount->accounttype->currentItem(); // sets account ids for parent accounts | ||
136 | } | ||
137 | |||
138 | // add the new account | ||
139 | if ( newaccount->getDateEdited () == TRUE ) | ||
140 | account->addAccount ( newaccount->accountname->text(), parentid, newaccount->accountbalance->text().toFloat(), type, | ||
141 | newaccount->getDescription(), newaccount->creditlimit->text().toFloat(), newaccount->getYear(), | ||
142 | newaccount->getMonth(), newaccount->getDay(), newaccount->accountbalance->text().toFloat(), newaccount->currencybox->currencybox->currentText() ); | ||
143 | else | ||
144 | account->addAccount ( newaccount->accountname->text (), parentid, newaccount->accountbalance->text().toFloat(), type, | ||
145 | newaccount->getDescription(), newaccount->creditlimit->text().toFloat(), defaultyear, | ||
146 | defaultmonth, defaultday, newaccount->accountbalance->text().toFloat(), newaccount->currencybox->currencybox->currentText() ); | ||
147 | |||
148 | if ( parentid != -1 ) | ||
149 | account->changeParentAccountBalance ( parentid ); | ||
150 | |||
151 | // redisplay accounts | ||
152 | // this function clears the account display first | ||
153 | account->displayAccounts ( listview ); | ||
154 | setToggleButton(); | ||
155 | } | ||
156 | maintabs->setTabEnabled ( tab2, FALSE ); | ||
157 | } | ||
158 | |||
159 | void AccountDisplay::deleteAccount () | ||
160 | { | ||
161 | if ( listview->selectedItem() == 0 ) | ||
162 | QMessageBox::warning ( this, "QashMoney", "Please select an account\nto delete."); | ||
163 | else if ( listview->selectedItem()->parent() == 0 && listview->selectedItem()->childCount() != 0 ) | ||
164 | QMessageBox::warning ( this, "QashMoney", "Can't delete parent accounts\nwith children"); | ||
165 | else | ||
166 | { | ||
167 | QMessageBox mb ( "Delete Account", "This will delete all transactions\nand transfers for this account.", QMessageBox::Information, QMessageBox::Ok, QMessageBox::Cancel, QMessageBox::NoButton ); | ||
168 | if ( mb.exec() == QMessageBox::Ok ) | ||
169 | { | ||
170 | int accountid = listview->selectedItem()->text ( getIDColumn() ).toInt (); | ||
171 | int parentid = account->getParentAccountID ( accountid ); | ||
172 | |||
173 | // delete all the transactions and transfers for the account | ||
174 | transaction->deleteAllTransactions ( accountid ); | ||
175 | transfer->deleteAllTransfers ( accountid ); | ||
176 | |||
177 | // delete the account | ||
178 | account->deleteAccount ( accountid ); | ||
179 | |||
180 | // update account balances | ||
181 | if ( parentid != -1 ) | ||
182 | account->changeParentAccountBalance ( parentid ); | ||
183 | |||
184 | //redisplay accounts | ||
185 | account->displayAccounts ( listview ); | ||
186 | |||
187 | //remove all the columns from the accountdisplay if there are not any accounts | ||
188 | if ( account->getNumberOfAccounts() == 0 ) | ||
189 | { | ||
190 | int columns = listview->columns(); | ||
191 | int counter; | ||
192 | for ( counter = 0; counter <= columns; counter++ ) | ||
193 | listview->removeColumn ( 0 ); | ||
194 | } | ||
195 | |||
196 | setToggleButton(); | ||
197 | } | ||
198 | } | ||
199 | maintabs->setTabEnabled ( tab2, FALSE ); | ||
200 | } | ||
201 | |||
202 | void AccountDisplay::setToggleButton () | ||
203 | { | ||
204 | // iterate through account display and determine how many "transferable" accounts we have | ||
205 | // if there are less than two, disable the transfer button | ||
206 | QListViewItemIterator it ( listview ); | ||
207 | int counter = 0; | ||
208 | for ( ; it.current(); ++it ) | ||
209 | { | ||
210 | // add one to counter if we find a transferable account | ||
211 | if ( it.current()->parent() != 0 || ( it.current()->childCount() ) == 0 ) | ||
212 | counter++; | ||
213 | } | ||
214 | if ( counter > 1 ) | ||
215 | transferbutton->show(); | ||
216 | else | ||
217 | transferbutton->hide(); | ||
218 | } | ||
219 | |||
220 | void AccountDisplay::accountTransfer ( bool state ) | ||
221 | { | ||
222 | if ( state == TRUE ) | ||
223 | { | ||
224 | firstaccountid = -1; | ||
225 | secondaccountid = -1; | ||
226 | listview->clearSelection (); | ||
227 | listview->setMultiSelection ( TRUE ); | ||
228 | disableParentsWithChildren (); | ||
229 | connect ( listview, SIGNAL ( clicked ( QListViewItem * ) ), this, SLOT ( getTransferAccounts ( QListViewItem * ) ) ); | ||
230 | } | ||
231 | else | ||
232 | { | ||
233 | firstaccountid = -1; | ||
234 | secondaccountid = -1; | ||
235 | listview->clearSelection (); | ||
236 | listview->setMultiSelection ( FALSE ); | ||
237 | enableAccounts (); | ||
238 | disconnect ( listview, SIGNAL ( clicked ( QListViewItem * ) ), this, SLOT ( getTransferAccounts ( QListViewItem * ) ) ); | ||
239 | } | ||
240 | } | ||
241 | |||
242 | void AccountDisplay::getTransferAccounts ( QListViewItem * item ) | ||
243 | { | ||
244 | if ( item->parent() != 0 || item->childCount() == 0 ) // only set an account for transfer if its a child or parent with no children | ||
245 | { | ||
246 | if ( firstaccountid == -1 ) | ||
247 | firstaccountid = item->text ( getIDColumn() ).toInt(); // set first account if we've selected a valid account | ||
248 | else | ||
249 | if ( item->text ( getIDColumn() ).toInt() != firstaccountid ) // set the second account if its not equal to the first | ||
250 | secondaccountid = item->text ( getIDColumn() ).toInt(); | ||
251 | } | ||
252 | |||
253 | // open transfer window if both accounts are set | ||
254 | if ( firstaccountid != -1 && secondaccountid != -1 ) | ||
255 | { | ||
256 | // construct the transferdialog window | ||
257 | TransferDialog *td = new TransferDialog ( this, firstaccountid, secondaccountid ); | ||
258 | |||
259 | // enter today's date in the date box as default | ||
260 | QDate today = QDate::currentDate (); | ||
261 | int defaultday = today.day(); | ||
262 | int defaultmonth = today.month(); | ||
263 | int defaultyear = today.year(); | ||
264 | td->date->setText ( preferences->getDate ( defaultyear, defaultmonth, defaultday ) ); | ||
265 | |||
266 | if ( td->exec() == QDialog::Accepted ) | ||
267 | { | ||
268 | // set the cleared integer if the checkbox is checked | ||
269 | if ( td->clearedcheckbox->isChecked() == TRUE ) | ||
270 | cleared = 1; | ||
271 | |||
272 | // add the transfer with a new date if its been edited or use the default date | ||
273 | if ( td->getDateEdited () == TRUE ) | ||
274 | transfer->addTransfer ( firstaccountid, account->getParentAccountID ( firstaccountid ), secondaccountid, | ||
275 | account->getParentAccountID ( secondaccountid ), td->getDay(), td->getMonth(), td->getYear(), td->amount->text().toFloat(), cleared ); | ||
276 | else | ||
277 | transfer->addTransfer ( firstaccountid, account->getParentAccountID ( firstaccountid ), secondaccountid, | ||
278 | account->getParentAccountID ( secondaccountid ), defaultday, defaultmonth, defaultyear, td->amount->text().toFloat(), cleared ); | ||
279 | |||
280 | // update account balances of both accounts and parents if necessary | ||
281 | account->updateAccountBalance ( firstaccountid ); | ||
282 | if ( account->getParentAccountID ( firstaccountid ) != -1 ) | ||
283 | account->changeParentAccountBalance ( account->getParentAccountID ( firstaccountid ) ); | ||
284 | account->updateAccountBalance ( secondaccountid ); | ||
285 | if ( account->getParentAccountID ( secondaccountid ) != -1 ) | ||
286 | account->changeParentAccountBalance ( account->getParentAccountID ( secondaccountid ) ); | ||
287 | |||
288 | // redisplay accounts | ||
289 | account->displayAccounts ( listview ); | ||
290 | } | ||
291 | else | ||
292 | { | ||
293 | firstaccountid = -1; | ||
294 | secondaccountid = -1; | ||
295 | listview->clearSelection (); | ||
296 | listview->setMultiSelection ( FALSE ); | ||
297 | disconnect ( listview, SIGNAL ( clicked ( QListViewItem * ) ), this, SLOT ( getTransferAccounts ( QListViewItem * ) ) ); | ||
298 | } | ||
299 | |||
300 | // reset the accounts display window | ||
301 | transferbutton->toggle(); // toggling this button with clear the window as well | ||
302 | |||
303 | // reenable all the accounts so the transaction tab will be properly set | ||
304 | enableAccounts (); | ||
305 | } | ||
306 | } | ||
307 | |||
308 | void AccountDisplay::disableParentsWithChildren () | ||
309 | { | ||
310 | // iterate through accountdisplay listview and disable all the parents that have children | ||
311 | QListViewItemIterator it ( listview ); | ||
312 | for ( ; it.current(); ++it ) | ||
313 | { | ||
314 | if ( it.current()->parent() == 0 && it.current()->childCount() != 0 ) | ||
315 | it.current()->setSelectable ( FALSE ); | ||
316 | } | ||
317 | } | ||
318 | |||
319 | void AccountDisplay::enableAccounts () | ||
320 | { | ||
321 | // iterate through accountdisplay listview and enable all accounts | ||
322 | QListViewItemIterator it ( listview ); | ||
323 | for ( ; it.current(); ++it ) | ||
324 | it.current()->setSelectable ( TRUE ); | ||
325 | } | ||
326 | |||
327 | void AccountDisplay::saveColumnSize ( int column, int oldsize, int newsize ) | ||
328 | { | ||
329 | switch ( column ) | ||
330 | { | ||
331 | case 0: | ||
332 | if ( listview->columns() == 3 ) | ||
333 | preferences->changeColumnPreference ( 1, newsize ); | ||
334 | else | ||
335 | preferences->changeColumnPreference ( 10, newsize ); | ||
336 | break; | ||
337 | case 1: | ||
338 | if ( listview->columns() == 3 ) | ||
339 | preferences->changeColumnPreference ( 2, newsize ); | ||
340 | else | ||
341 | preferences->changeColumnPreference ( 11, newsize ); | ||
342 | break; | ||
343 | case 2: | ||
344 | preferences->changeColumnPreference ( 12, newsize ); | ||
345 | break; | ||
346 | } | ||
347 | |||
348 | } | ||
349 | |||
350 | int AccountDisplay::getIDColumn () | ||
351 | { | ||
352 | int counter; | ||
353 | int columns = listview->columns(); | ||
354 | for ( counter = 0; counter <= columns; counter++ ) | ||
355 | if ( listview->header()->label ( counter ).length() == 0 ) | ||
356 | return counter; | ||
357 | } | ||
358 | |||
359 | void AccountDisplay::editAccount () | ||
360 | { | ||
361 | if ( listview->selectedItem() == 0 ) | ||
362 | QMessageBox::warning ( this, "QashMoney", "Please select an account\nto edit."); | ||
363 | else | ||
364 | { | ||
365 | // set the accountid | ||
366 | int accountid = listview->selectedItem()->text ( getIDColumn() ).toInt(); | ||
367 | |||
368 | //construct new dialog box | ||
369 | QDialog *editaccountwindow = new QDialog ( this, 0, TRUE ); | ||
370 | editaccountwindow->setCaption ( "Edit Account" ); | ||
371 | |||
372 | // construct the items which will go in the dialog bix | ||
373 | QLabel *namelabel = new QLabel ( "Account Name", editaccountwindow ); | ||
374 | QLineEdit *accountname = new QLineEdit ( editaccountwindow ); | ||
375 | QLabel *descriptionlabel = new QLabel ( "Account Description", editaccountwindow ); | ||
376 | QLineEdit *accountdescription = new QLineEdit ( editaccountwindow ); | ||
377 | Currency *currencybox = new Currency ( editaccountwindow ); | ||
378 | |||
379 | QVBoxLayout *layout = new QVBoxLayout ( editaccountwindow, 5, 2 ); | ||
380 | layout->addWidget ( namelabel ); | ||
381 | layout->addWidget ( accountname ); | ||
382 | layout->addWidget ( descriptionlabel ); | ||
383 | layout->addWidget ( accountdescription ); | ||
384 | layout->addWidget ( currencybox ); | ||
385 | |||
386 | //set the account name | ||
387 | accountname->setText ( listview->selectedItem()->text ( 0 ) ); | ||
388 | |||
389 | //set the account description | ||
390 | accountdescription->setText ( account->getAccountDescription ( accountid ) ); | ||
391 | |||
392 | if ( preferences->getPreference ( 4 ) == 1 ) | ||
393 | { | ||
394 | // get currency code for this account then iterate through the currency box | ||
395 | // to find the one we want | ||
396 | int count = currencybox->currencybox->count(); | ||
397 | QString code = account->getCurrencyCode ( accountid ); | ||
398 | for ( int counter = 0; count - 1; counter++ ) | ||
399 | { | ||
400 | if ( QString::compare ( currencybox->currencybox->text ( counter ), code ) == 0 ) | ||
401 | { | ||
402 | currencybox->currencybox->setCurrentItem ( counter ); | ||
403 | break; | ||
404 | } | ||
405 | } | ||
406 | } | ||
407 | else | ||
408 | currencybox->setEnabled ( FALSE ); | ||
409 | |||
410 | //execute the dialog box | ||
411 | int response = editaccountwindow->exec(); | ||
412 | if ( response == 1 ) | ||
413 | { | ||
414 | account->updateAccount ( accountname->text(), accountdescription->text(), currencybox->currencybox->currentText(), accountid ); | ||
415 | account->displayAccounts ( listview ); | ||
416 | } | ||
417 | } | ||
418 | } | ||
419 | |||
420 | void AccountDisplay::setAccountExpanded ( QListViewItem *item ) | ||
421 | { | ||
422 | int accountid = item->text ( getIDColumn() ).toInt(); | ||
423 | account->setAccountExpanded ( 1, accountid ); | ||
424 | } | ||
425 | |||
426 | void AccountDisplay::setAccountCollapsed ( QListViewItem *item ) | ||
427 | { | ||
428 | int accountid = item->text ( getIDColumn() ).toInt(); | ||
429 | account->setAccountExpanded ( 0, accountid ); | ||
430 | } | ||
431 | |||
432 | |||