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