00001 #include "accountdisplay.h"
00002 #include "newaccount.h"
00003 #include "transaction.h"
00004 #include "transferdialog.h"
00005 #include "transfer.h"
00006
00007
00008 #include <opie2/odebug.h>
00009 using namespace Opie::Core;
00010
00011
00012 #include <qmessagebox.h>
00013 #include <qheader.h>
00014
00015 extern Account *account;
00016 extern Transaction *transaction;
00017 extern Transfer *transfer;
00018 extern Preferences *preferences;
00019
00020 AccountDisplay::AccountDisplay ( QWidget *parent ) : QWidget ( parent )
00021 {
00022 cleared = 0;
00023
00024 firstline = new QHBox ( this );
00025 firstline->setSpacing ( 2 );
00026
00027 newaccount = new QPushButton ( firstline );
00028 newaccount->setPixmap ( QPixmap ("/opt/QtPalmtop/pics/new.png") );
00029 connect ( newaccount, SIGNAL ( released() ), this, SLOT ( addAccount() ) );
00030
00031 editaccount = new QPushButton ( firstline );
00032 editaccount->setPixmap ( QPixmap ("/opt/QtPalmtop/pics/edit.png") );
00033 connect ( editaccount, SIGNAL ( released() ), this, SLOT ( editAccount() ) );
00034
00035 deleteaccount = new QPushButton ( firstline );
00036 deleteaccount->setPixmap( QPixmap ( "/opt/QtPalmtop/pics/delete.png") );
00037 connect ( deleteaccount, SIGNAL ( released() ), this, SLOT ( deleteAccount() ) );
00038
00039 transferbutton = new QPushButton ( firstline );
00040 transferbutton->setPixmap( QPixmap ( "/opt/QtPalmtop/pics/transfer.png") );
00041 transferbutton->setToggleButton ( TRUE );
00042 connect ( transferbutton, SIGNAL ( toggled(bool) ), this, SLOT ( accountTransfer(bool) ) );
00043
00044 listview = new QListView ( this );
00045 listview->setAllColumnsShowFocus ( TRUE );
00046 listview->setShowSortIndicator ( TRUE );
00047 listview->setRootIsDecorated ( TRUE );
00048 listview->setMultiSelection ( FALSE );
00049 connect ( listview, SIGNAL ( expanded(QListViewItem*) ), this, SLOT ( setAccountExpanded(QListViewItem*) ) );
00050 connect ( listview, SIGNAL ( collapsed(QListViewItem*) ), this, SLOT ( setAccountCollapsed(QListViewItem*) ) );
00051
00052 listview->header()->setTracking ( FALSE );
00053 connect ( listview->header(), SIGNAL ( sizeChange(int,int,int) ), this, SLOT ( saveColumnSize(int,int,int) ) );
00054 connect ( listview->header(), SIGNAL ( clicked(int) ), this, SLOT ( saveSortingPreference(int) ) );
00055
00056 layout = new QVBoxLayout ( this, 2, 5 );
00057 layout->addWidget ( firstline );
00058 layout->addWidget ( listview );
00059 }
00060
00061 void AccountDisplay::setTabs ( QWidget *newtab2, QTabWidget *newtabs )
00062 {
00063 tab2 = newtab2;
00064 maintabs = newtabs;
00065 }
00066
00067 void AccountDisplay::addAccount ()
00068 {
00069
00070 int parentid = 0;
00071 type = 0;
00072 QString parentlist [ listview->childCount() + 1 ] [ 3 ] ;
00073
00074
00075 NewAccount *newaccount = new NewAccount ( this );
00076 int width = this->width();
00077 newaccount->accountbox->setMaximumWidth ( ( int ) ( width * 0.5 ) );
00078 newaccount->datebox->setMaximumWidth ( ( int ) ( width * 0.4 ) );
00079 newaccount->childbox->setMaximumWidth ( ( int ) ( width * 0.5 ) );
00080 newaccount->balancebox->setMaximumWidth ( ( int ) ( width * 0.4 ) );
00081 newaccount->creditlimitbox->setMaximumWidth ( ( int ) ( width * 0.4 ) );
00082
00083
00084 if ( account->getNumberOfAccounts () == 0 )
00085 newaccount->childcheckbox->setEnabled ( FALSE );
00086
00087
00088
00089 else
00090 {
00091 int c = 0;
00092 QListViewItemIterator it ( listview );
00093 for ( ; it.current(); ++it )
00094 {
00095 int id = it.current()->text ( getIDColumn() ).toInt();
00096
00097
00098 if ( transfer->getNumberOfTransfers ( id ) == 0 && transaction->getNumberOfTransactions ( id ) == 0 && it.current()->parent() == 0 )
00099 {
00100 newaccount->childbox->insertItem ( it.current()->text ( 0 ) );
00101 parentlist [ c ] [ 0 ] = it.current()->text ( 0 );
00102 parentlist [ c ] [ 1 ] = it.current()->text ( getIDColumn() );
00103 parentlist [ c ] [ 2 ] = QString::number ( c );
00104 c++;
00105 }
00106 }
00107 }
00108
00109 if ( preferences->getPreference ( 4 ) == 0 )
00110 newaccount->currencybox->setEnabled ( FALSE );
00111
00112
00113 QDate today = QDate::currentDate ();
00114 int defaultday = today.day();
00115 int defaultmonth = today.month();
00116 int defaultyear = today.year();
00117 newaccount->startdate->setText ( preferences->getDate ( defaultyear, defaultmonth, defaultday ) );
00118
00119
00120 if ( newaccount->exec() == QDialog::Accepted )
00121 {
00122 if ( newaccount->childcheckbox->isChecked () == TRUE )
00123 {
00124
00125
00126
00127 int counter;
00128 for ( counter = 0; counter < listview->childCount() + 1; counter++ )
00129 if ( ( parentlist [ counter ] [ 2 ].toInt() ) == newaccount->childbox->currentItem() )
00130 {
00131 parentid = parentlist [ counter ] [ 1 ].toInt();
00132 break;
00133 }
00134 type = ( newaccount->accounttype->currentItem() ) + 6;
00135 }
00136 else
00137 {
00138 parentid = -1;
00139 type = newaccount->accounttype->currentItem();
00140 }
00141
00142
00143 if ( newaccount->getDateEdited () == TRUE )
00144 account->addAccount ( newaccount->accountname->text(), parentid, newaccount->accountbalance->text().toFloat(), type,
00145 newaccount->getDescription(), newaccount->creditlimit->text().toFloat(), newaccount->getYear(),
00146 newaccount->getMonth(), newaccount->getDay(), newaccount->accountbalance->text().toFloat(), newaccount->currencybox->currencybox->currentText() );
00147 else
00148 account->addAccount ( newaccount->accountname->text (), parentid, newaccount->accountbalance->text().toFloat(), type,
00149 newaccount->getDescription(), newaccount->creditlimit->text().toFloat(), defaultyear,
00150 defaultmonth, defaultday, newaccount->accountbalance->text().toFloat(), newaccount->currencybox->currencybox->currentText() );
00151
00152 if ( parentid != -1 )
00153 account->changeParentAccountBalance ( parentid );
00154
00155
00156
00157 account->displayAccounts ( listview );
00158 setToggleButton();
00159 }
00160 maintabs->setTabEnabled ( tab2, FALSE );
00161 }
00162
00163 void AccountDisplay::deleteAccount ()
00164 {
00165 if ( listview->selectedItem() == 0 )
00166 QMessageBox::warning ( this, "QashMoney", "Please select an account\nto delete.");
00167 else if ( listview->selectedItem()->parent() == 0 && listview->selectedItem()->childCount() != 0 )
00168 QMessageBox::warning ( this, "QashMoney", "Can't delete parent accounts\nwith children");
00169 else
00170 {
00171 QMessageBox mb ( "Delete Account", "This will delete all transactions\nand transfers for this account.", QMessageBox::Information, QMessageBox::Ok, QMessageBox::Cancel, QMessageBox::NoButton );
00172 if ( mb.exec() == QMessageBox::Ok )
00173 {
00174 int accountid = listview->selectedItem()->text ( getIDColumn() ).toInt ();
00175 int parentid = account->getParentAccountID ( accountid );
00176
00177
00178 transaction->deleteAllTransactions ( accountid );
00179 transfer->deleteAllTransfers ( accountid );
00180
00181
00182 account->deleteAccount ( accountid );
00183
00184
00185 if ( parentid != -1 )
00186 account->changeParentAccountBalance ( parentid );
00187
00188
00189 account->displayAccounts ( listview );
00190
00191
00192 if ( account->getNumberOfAccounts() == 0 )
00193 {
00194 int columns = listview->columns();
00195 int counter;
00196 for ( counter = 0; counter <= columns; counter++ )
00197 listview->removeColumn ( 0 );
00198 }
00199
00200 setToggleButton();
00201 }
00202 }
00203 maintabs->setTabEnabled ( tab2, FALSE );
00204 }
00205
00206 void AccountDisplay::setToggleButton ()
00207 {
00208
00209
00210 QListViewItemIterator it ( listview );
00211 int counter = 0;
00212 for ( ; it.current(); ++it )
00213 {
00214
00215 if ( it.current()->parent() != 0 || ( it.current()->childCount() ) == 0 )
00216 counter++;
00217 }
00218 if ( counter > 1 )
00219 transferbutton->show();
00220 else
00221 transferbutton->hide();
00222 }
00223
00224 void AccountDisplay::accountTransfer ( bool state )
00225 {
00226 if ( state == TRUE )
00227 {
00228 firstaccountid = -1;
00229 secondaccountid = -1;
00230 listview->clearSelection ();
00231 listview->setMultiSelection ( TRUE );
00232 disableParentsWithChildren ();
00233 connect ( listview, SIGNAL ( clicked(QListViewItem*) ), this, SLOT ( getTransferAccounts(QListViewItem*) ) );
00234 }
00235 else
00236 {
00237 firstaccountid = -1;
00238 secondaccountid = -1;
00239 listview->clearSelection ();
00240 listview->setMultiSelection ( FALSE );
00241 enableAccounts ();
00242 disconnect ( listview, SIGNAL ( clicked(QListViewItem*) ), this, SLOT ( getTransferAccounts(QListViewItem*) ) );
00243 }
00244 }
00245
00246 void AccountDisplay::getTransferAccounts ( QListViewItem * item )
00247 {
00248 if ( item->parent() != 0 || item->childCount() == 0 )
00249 {
00250 if ( firstaccountid == -1 )
00251 firstaccountid = item->text ( getIDColumn() ).toInt();
00252 else
00253 if ( item->text ( getIDColumn() ).toInt() != firstaccountid )
00254 secondaccountid = item->text ( getIDColumn() ).toInt();
00255 }
00256
00257
00258 if ( firstaccountid != -1 && secondaccountid != -1 )
00259 {
00260
00261 TransferDialog *td = new TransferDialog ( this, firstaccountid, secondaccountid );
00262
00263
00264 QDate today = QDate::currentDate ();
00265 int defaultday = today.day();
00266 int defaultmonth = today.month();
00267 int defaultyear = today.year();
00268 td->date->setText ( preferences->getDate ( defaultyear, defaultmonth, defaultday ) );
00269
00270 if ( td->exec() == QDialog::Accepted )
00271 {
00272
00273 if ( td->clearedcheckbox->isChecked() == TRUE )
00274 cleared = 1;
00275 odebug << "Year from transferdialog = " << td->getYear() << "" << oendl;
00276
00277 if ( td->getDateEdited () == TRUE )
00278 transfer->addTransfer ( firstaccountid, account->getParentAccountID ( firstaccountid ), secondaccountid, account->getParentAccountID ( secondaccountid ), td->getDay(), td->getMonth(), td->getYear(), td->amount->text().toFloat(), cleared );
00279 else
00280 transfer->addTransfer ( firstaccountid, account->getParentAccountID ( firstaccountid ), secondaccountid, account->getParentAccountID ( secondaccountid ), defaultday, defaultmonth, defaultyear, td->amount->text().toFloat(), cleared );
00281
00282
00283 account->updateAccountBalance ( firstaccountid );
00284 if ( account->getParentAccountID ( firstaccountid ) != -1 )
00285 account->changeParentAccountBalance ( account->getParentAccountID ( firstaccountid ) );
00286 account->updateAccountBalance ( secondaccountid );
00287 if ( account->getParentAccountID ( secondaccountid ) != -1 )
00288 account->changeParentAccountBalance ( account->getParentAccountID ( secondaccountid ) );
00289
00290
00291 account->displayAccounts ( listview );
00292 }
00293 else
00294 {
00295 firstaccountid = -1;
00296 secondaccountid = -1;
00297 listview->clearSelection ();
00298 listview->setMultiSelection ( FALSE );
00299 disconnect ( listview, SIGNAL ( clicked(QListViewItem*) ), this, SLOT ( getTransferAccounts(QListViewItem*) ) );
00300 }
00301
00302
00303 transferbutton->toggle();
00304
00305
00306 enableAccounts ();
00307 }
00308 }
00309
00310 void AccountDisplay::disableParentsWithChildren ()
00311 {
00312
00313 QListViewItemIterator it ( listview );
00314 for ( ; it.current(); ++it )
00315 {
00316 if ( it.current()->parent() == 0 && it.current()->childCount() != 0 )
00317 it.current()->setSelectable ( FALSE );
00318 }
00319 }
00320
00321 void AccountDisplay::enableAccounts ()
00322 {
00323
00324 QListViewItemIterator it ( listview );
00325 for ( ; it.current(); ++it )
00326 it.current()->setSelectable ( TRUE );
00327 }
00328
00329 void AccountDisplay::saveColumnSize ( int column, int oldsize, int newsize )
00330 {
00331 switch ( column )
00332 {
00333 case 0:
00334 if ( listview->columns() == 3 )
00335 preferences->changeColumnPreference ( 1, newsize );
00336 else
00337 preferences->changeColumnPreference ( 10, newsize );
00338 break;
00339 case 1:
00340 if ( listview->columns() == 3 )
00341 preferences->changeColumnPreference ( 2, newsize );
00342 else
00343 preferences->changeColumnPreference ( 11, newsize );
00344 break;
00345 case 2:
00346 preferences->changeColumnPreference ( 12, newsize );
00347 break;
00348 }
00349
00350 }
00351
00352 void AccountDisplay::saveSortingPreference ( int column )
00353 {
00354 preferences->changeSortingPreference ( 1, column );
00355 }
00356
00357 int AccountDisplay::getIDColumn ()
00358 {
00359 int counter;
00360 int columns = listview->columns();
00361 for ( counter = 0; counter <= columns; counter++ )
00362 if ( listview->header()->label ( counter ).length() == 0 )
00363 return counter;
00364 }
00365
00366 void AccountDisplay::editAccount ()
00367 {
00368 if ( listview->selectedItem() == 0 )
00369 QMessageBox::warning ( this, "QashMoney", "Please select an account\nto edit.");
00370 else
00371 {
00372
00373 int accountid = listview->selectedItem()->text ( getIDColumn() ).toInt();
00374
00375
00376 QDialog *editaccountwindow = new QDialog ( this, 0, TRUE );
00377 editaccountwindow->setCaption ( "Edit Account" );
00378
00379
00380 QLabel *namelabel = new QLabel ( "Account Name", editaccountwindow );
00381 QLineEdit *accountname = new QLineEdit ( editaccountwindow );
00382 QLabel *descriptionlabel = new QLabel ( "Account Description", editaccountwindow );
00383 QLineEdit *accountdescription = new QLineEdit ( editaccountwindow );
00384 Currency *currencybox = new Currency ( editaccountwindow );
00385
00386 QVBoxLayout *layout = new QVBoxLayout ( editaccountwindow, 5, 2 );
00387 layout->addWidget ( namelabel );
00388 layout->addWidget ( accountname );
00389 layout->addWidget ( descriptionlabel );
00390 layout->addWidget ( accountdescription );
00391 layout->addWidget ( currencybox );
00392
00393
00394 accountname->setText ( listview->selectedItem()->text ( 0 ) );
00395
00396
00397 accountdescription->setText ( account->getAccountDescription ( accountid ) );
00398
00399 if ( preferences->getPreference ( 4 ) == 1 )
00400 {
00401
00402
00403 int count = currencybox->currencybox->count();
00404 QString code = account->getCurrencyCode ( accountid );
00405 for ( int counter = 0; count - 1; counter++ )
00406 {
00407 if ( QString::compare ( currencybox->currencybox->text ( counter ), code ) == 0 )
00408 {
00409 currencybox->currencybox->setCurrentItem ( counter );
00410 break;
00411 }
00412 }
00413 }
00414 else
00415 currencybox->setEnabled ( FALSE );
00416
00417
00418 int response = editaccountwindow->exec();
00419 if ( response == 1 )
00420 {
00421 account->updateAccount ( accountname->text(), accountdescription->text(), currencybox->currencybox->currentText(), accountid );
00422 account->displayAccounts ( listview );
00423
00424
00425 QListViewItemIterator it ( listview );
00426 for ( ; it.current(); ++it )
00427 {
00428 if ( it.current()->text ( 0 ) == accountname->text() )
00429 {
00430 listview->setSelected ( it.current(), TRUE );
00431 return;
00432 }
00433 }
00434 maintabs->setTabEnabled ( tab2, FALSE );
00435 }
00436 }
00437 }
00438
00439 void AccountDisplay::setAccountExpanded ( QListViewItem *item )
00440 {
00441 int accountid = item->text ( getIDColumn() ).toInt();
00442 account->setAccountExpanded ( 1, accountid );
00443 }
00444
00445 void AccountDisplay::setAccountCollapsed ( QListViewItem *item )
00446 {
00447 int accountid = item->text ( getIDColumn() ).toInt();
00448 account->setAccountExpanded ( 0, accountid );
00449 }
00450
00451