Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

accountdisplay.cpp

Go to the documentation of this file.
00001 #include "accountdisplay.h"
00002 #include "newaccount.h"
00003 #include "transaction.h"
00004 #include "transferdialog.h"
00005 #include "transfer.h"
00006 
00007 /* OPIE */
00008 #include <opie2/odebug.h>
00009 using namespace Opie::Core;
00010 
00011 /* QT */
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     // initialize local variables
00070     int parentid = 0;
00071     type = 0;
00072     QString parentlist [ listview->childCount() + 1 ] [ 3 ] ;
00073 
00074     // create new account window for entering data
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     // if there are no accounts, disable the child check box
00084     if ( account->getNumberOfAccounts () == 0 )
00085       newaccount->childcheckbox->setEnabled ( FALSE );
00086 
00087     // if there are accounts, fill up the pulldown menu for
00088     // selecting a parent account.  We should only add those parents without transactions
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             // iterate through accountdisplay listview and add parents with no transactions
00097             // add this item to the list box only if it is a parent and has no transactions
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     // enter today's date in the date box as default
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     //add account information if user pushes OK button
00120     if ( newaccount->exec() == QDialog::Accepted )
00121       {
00122         if ( newaccount->childcheckbox->isChecked () == TRUE ) // set a parent id and type for a child account
00123           {
00124             // go through the parentlist we created and determine the parent accountid
00125             // we can't use the name of the account because there may be two accounts
00126             // with the same name.  This function does it all by accountid
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;  // sets account ids for child accounts.  See accountdisplay.h for types
00135           }
00136         else
00137           {
00138             parentid = -1;
00139             type = newaccount->accounttype->currentItem(); // sets account ids for parent accounts
00140           }
00141 
00142         // add the new account
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         // redisplay accounts
00156         // this function clears the account display first
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           // delete all the transactions and transfers for the account
00178           transaction->deleteAllTransactions ( accountid );
00179           transfer->deleteAllTransfers ( accountid );
00180 
00181           // delete the account
00182           account->deleteAccount ( accountid );
00183 
00184           // update account balances
00185           if ( parentid != -1 )
00186             account->changeParentAccountBalance ( parentid );
00187 
00188           //redisplay accounts
00189           account->displayAccounts ( listview );
00190 
00191           //remove all the columns from the accountdisplay if there are not any accounts
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     // iterate through account display and determine how many "transferable" accounts we have
00209     // if there are less than two, disable the transfer button
00210     QListViewItemIterator it ( listview );
00211     int counter = 0;
00212     for ( ; it.current(); ++it )
00213       {
00214         // add one to counter if we find a transferable account
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 ) // only set an account for transfer if its a child or parent with no children
00249       {
00250         if ( firstaccountid == -1 )
00251           firstaccountid = item->text ( getIDColumn() ).toInt(); // set first account if we've selected a valid account
00252         else
00253           if ( item->text ( getIDColumn() ).toInt() != firstaccountid ) // set the second account if its not equal to the first
00254             secondaccountid = item->text ( getIDColumn() ).toInt();
00255       }
00256 
00257     // open transfer window if both accounts are set
00258     if ( firstaccountid != -1 && secondaccountid != -1 )
00259       {
00260         // construct the transferdialog window
00261         TransferDialog *td = new TransferDialog ( this, firstaccountid, secondaccountid );
00262 
00263         // enter today's date in the date box as default
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             // set the cleared integer if the checkbox is checked
00273             if ( td->clearedcheckbox->isChecked() == TRUE )
00274               cleared = 1;
00275             odebug << "Year from transferdialog = " << td->getYear() << "" << oendl; 
00276             // add the transfer with a new date if its been edited or use the default date
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             // update account balances of both accounts and parents if necessary
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             // redisplay accounts
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        // reset the accounts display window
00303        transferbutton->toggle(); // toggling this button with clear the window as well
00304 
00305        // reenable all the accounts so the transaction tab will be properly set
00306        enableAccounts ();
00307       }
00308   }
00309 
00310 void AccountDisplay::disableParentsWithChildren ()
00311   {
00312      // iterate through accountdisplay listview and disable all the parents that have children
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      // iterate through accountdisplay listview and enable all accounts
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         // set the accountid
00373         int accountid = listview->selectedItem()->text ( getIDColumn() ).toInt();
00374 
00375         //construct new dialog box
00376         QDialog *editaccountwindow = new QDialog ( this, 0, TRUE );
00377         editaccountwindow->setCaption ( "Edit Account" );
00378 
00379         // construct the items which will go in the dialog bix
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         //set the account name
00394         accountname->setText ( listview->selectedItem()->text ( 0 ) );
00395 
00396         //set the account description
00397         accountdescription->setText ( account->getAccountDescription ( accountid ) );
00398 
00399         if ( preferences->getPreference ( 4 ) == 1 )
00400           {
00401             // get currency code for this account then iterate through the currency box
00402             // to find the one we want
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         //execute the dialog box
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            // Try and select the same account that was just edited
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 

Generated on Sat Nov 5 16:18:10 2005 for OPIE by  doxygen 1.4.2