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

checkbook.cpp

Go to the documentation of this file.
00001 /*
00002                      This file is part of the OPIE Project
00003                =.
00004       .=l.            Copyright (c)  2002 Dan Williams <drw@handhelds.org>
00005      .>+-=
00006 _;:,   .>  :=|.         This file is free software; you can
00007 .> <`_,  > .  <=          redistribute it and/or modify it under
00008 :`=1 )Y*s>-.--  :           the terms of the GNU General Public
00009 .="- .-=="i,   .._         License as published by the Free Software
00010 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00011   ._= =}    :          or (at your option) any later version.
00012   .%`+i>    _;_.
00013   .i_,=:_.   -<s.       This file is distributed in the hope that
00014   + . -:.    =       it will be useful, but WITHOUT ANY WARRANTY;
00015   : ..  .:,   . . .    without even the implied warranty of
00016   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00017  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU General
00018 ..}^=.=    =    ;      Public License for more details.
00019 ++=  -.   .`   .:
00020 :   = ...= . :.=-        You should have received a copy of the GNU
00021 -.  .:....=;==+<;          General Public License along with this file;
00022  -_. . .  )=. =           see the file COPYING. If not, write to the
00023   --    :-=`           Free Software Foundation, Inc.,
00024                              59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include "checkbook.h"
00030 #include "cbinfo.h"
00031 #include "transaction.h"
00032 #include "traninfo.h"
00033 #include "graph.h"
00034 #include "graphinfo.h"
00035 #include "password.h"
00036 #include "cfg.h"
00037 
00038 #include <opie2/oresource.h>
00039 
00040 #include <qpe/applnk.h>
00041 #include <qpe/qpeapplication.h>
00042 #include <qpe/qpemessagebox.h>
00043 
00044 #include <qcheckbox.h>
00045 #include <qcombobox.h>
00046 #include <qlabel.h>
00047 #include <qlayout.h>
00048 #include <qlineedit.h>
00049 #include <qmultilineedit.h>
00050 #include <qpushbutton.h>
00051 #include <qwhatsthis.h>
00052 #include <qpopupmenu.h>
00053 
00054 #define COL_ID          0
00055 #define COL_SORTDATE    1
00056 #define COL_NUM         2
00057 #define COL_DATE        3
00058 #define COL_DESC        4
00059 #define COL_AMOUNT      5
00060 #define COL_BAL         6
00061 
00062 // --- Checkbook --------------------------------------------------------------
00063 using namespace Opie::Ui;
00064 Checkbook::Checkbook( QWidget *parent, CBInfo *i, Cfg *cfg )
00065     : QDialog( parent, 0, TRUE, WStyle_ContextHelp )
00066 {
00067     info = i;
00068     _pCfg=cfg;
00069 
00070     // Title bar
00071     if ( info->name() != "" )
00072     {
00073         QString tempstr = info->name();
00074         tempstr.append( " - " );
00075         tempstr.append( tr( "Checkbook" ) );
00076         setCaption( tempstr );
00077     }
00078     else
00079     {
00080         setCaption( tr( "New checkbook" ) );
00081     }
00082 
00083 
00084     // Setup layout to make everything pretty
00085     QVBoxLayout *layout = new QVBoxLayout( this );
00086     layout->setMargin( 2 );
00087     layout->setSpacing( 4 );
00088 
00089     // Setup tabs for all info
00090     mainWidget = new OTabWidget( this );
00091     layout->addWidget( mainWidget );
00092     mainWidget->addTab( initInfo(), "checkbook/infotab", tr( "Info" ) );
00093     mainWidget->addTab( initTransactions(), "checkbook/trantab", tr( "Transactions" ) );
00094     mainWidget->addTab( initCharts(), "checkbook/charttab", tr( "Charts" ) );
00095     if( _pCfg->isShowLastTab() )
00096         mainWidget->setCurrentTab( info->getLastTab() );
00097     else
00098         mainWidget->setCurrentTab( tr( "Info" ) );
00099     connect( mainWidget, SIGNAL( currentChanged(QWidget*) ), this, SLOT( slotTab(QWidget*) ) );
00100 
00101     // Load checkbook information
00102     loadCheckbook();
00103 }
00104 
00105 Checkbook::~Checkbook()
00106 {
00107 }
00108 
00109 // --- initInfo ---------------------------------------------------------------
00110 QWidget *Checkbook::initInfo()
00111 {
00112     QWidget *control = new QWidget( mainWidget, tr("Info") );
00113 
00114     QVBoxLayout *vb = new QVBoxLayout( control );
00115 
00116     QScrollView *sv = new QScrollView( control );
00117     vb->addWidget( sv, 0, 0 );
00118     sv->setResizePolicy( QScrollView::AutoOneFit );
00119     sv->setFrameStyle( QFrame::NoFrame );
00120 
00121     QWidget *container = new QWidget( sv->viewport() );
00122     sv->addChild( container );
00123 
00124     QGridLayout *layout = new QGridLayout( container );
00125     layout->setSpacing( 2 );
00126     layout->setMargin( 4 );
00127 
00128     // Password protection
00129     passwordCB = new QCheckBox( tr( "Password protect" ), container );
00130     QWhatsThis::add( passwordCB, tr( "Click here to enable/disable password protection of this checkbook." ) );
00131     connect( passwordCB, SIGNAL( clicked() ), this, SLOT( slotPasswordClicked() ) );
00132     layout->addMultiCellWidget( passwordCB, 0, 0, 0, 1 );
00133 
00134     // Account name
00135     QLabel *label = new QLabel( tr( "Name:" ), container );
00136     QWhatsThis::add( label, tr( "Enter name of checkbook here." ) );
00137     layout->addWidget( label, 1, 0 );
00138     nameEdit = new QLineEdit( container );
00139     QWhatsThis::add( nameEdit, tr( "Enter name of checkbook here." ) );
00140     connect( nameEdit, SIGNAL( textChanged(const QString&) ),
00141              this, SLOT( slotNameChanged(const QString&) ) );
00142     layout->addWidget( nameEdit, 1, 1 );
00143 
00144     // Type of account
00145     label = new QLabel( tr( "Type:" ), container );
00146     QWhatsThis::add( label, tr( "Select type of checkbook here." ) );
00147     layout->addWidget( label, 2, 0 );
00148     typeList = new QComboBox( container );
00149     QWhatsThis::add( typeList, tr( "Select type of checkbook here." ) );
00150     typeList->insertStringList( _pCfg->getAccountTypes() );
00151     layout->addWidget( typeList, 2, 1 );
00152 
00153     // Bank/institution name
00154     label = new QLabel( tr( "Bank:" ), container );
00155     QWhatsThis::add( label, tr( "Enter name of the bank for this checkbook here." ) );
00156     layout->addWidget( label, 3, 0 );
00157     bankEdit = new QLineEdit( container );
00158     QWhatsThis::add( bankEdit, tr( "Enter name of the bank for this checkbook here." ) );
00159     layout->addWidget( bankEdit, 3, 1 );
00160 
00161     // Account number
00162     label = new QLabel( tr( "Account number:" ), container );
00163     QWhatsThis::add( label, tr( "Enter account number for this checkbook here." ) );
00164     layout->addWidget( label, 4, 0 );
00165     acctNumEdit = new QLineEdit( container );
00166     QWhatsThis::add( acctNumEdit, tr( "Enter account number for this checkbook here." ) );
00167     layout->addWidget( acctNumEdit, 4, 1 );
00168 
00169     // PIN number
00170     label = new QLabel( tr( "PIN number:" ), container );
00171     QWhatsThis::add( label, tr( "Enter PIN number for this checkbook here." ) );
00172     layout->addWidget( label, 5, 0 );
00173     pinNumEdit = new QLineEdit( container );
00174     QWhatsThis::add( pinNumEdit, tr( "Enter PIN number for this checkbook here." ) );
00175     layout->addWidget( pinNumEdit, 5, 1 );
00176 
00177     // Starting balance
00178     label = new QLabel( tr( "Starting balance:" ), container );
00179     QWhatsThis::add( label, tr( "Enter the initial balance for this checkbook here." ) );
00180     layout->addWidget( label, 6, 0 );
00181     balanceEdit = new QLineEdit( container );
00182     QWhatsThis::add( balanceEdit, tr( "Enter the initial balance for this checkbook here." ) );
00183     connect( balanceEdit, SIGNAL( textChanged(const QString&) ),
00184              this, SLOT( slotStartingBalanceChanged(const QString&) ) );
00185     layout->addWidget( balanceEdit, 6, 1 );
00186 
00187     // Notes
00188     label = new QLabel( tr( "Notes:" ), container );
00189     QWhatsThis::add( label, tr( "Enter any additional information for this checkbook here." ) );
00190     layout->addWidget( label, 7, 0 );
00191     notesEdit = new QMultiLineEdit( container );
00192     QWhatsThis::add( notesEdit, tr( "Enter any additional information for this checkbook here." ) );
00193     notesEdit->setMinimumHeight( 25 );
00194     notesEdit->setMaximumHeight( 65 );
00195     layout->addMultiCellWidget( notesEdit, 8, 8, 0, 1 );
00196 
00197     return control;
00198 }
00199 
00200 
00201 // --- initTransactions -------------------------------------------------------
00202 QWidget *Checkbook::initTransactions()
00203 {
00204     QWidget *control = new QWidget( mainWidget, tr("Transactions") );
00205 
00206     QGridLayout *layout = new QGridLayout( control );
00207     layout->setSpacing( 2 );
00208     layout->setMargin( 4 );
00209 
00210     // Sort selector
00211     QLabel *label = new QLabel( tr( "Sort by:" ), control );
00212     QWhatsThis::add( label, tr( "Select checkbook sorting here." ) );
00213     layout->addMultiCellWidget( label, 0, 0, 0, 1 );
00214     _cbSortType=new QComboBox( control );
00215     _cbSortType->insertItem( tr("Entry Order") );
00216     _cbSortType->insertItem( tr("Date") );
00217     _cbSortType->insertItem( tr("Number") );
00218     layout->addMultiCellWidget( _cbSortType, 0, 0, 1, 2 );
00219     connect( _cbSortType, SIGNAL( activated(const QString&) ), this, SLOT( slotSortChanged(const QString&) ) );
00220 
00221     // Table
00222     tranTable = new QListView( control );
00223     QFont fnt(QPEApplication::font());
00224     if( _pCfg->getUseSmallFont() )
00225         {
00226                 fnt.setPointSize( fnt.pointSize()-1 );
00227         }
00228     tranTable->setFont( fnt );
00229     QWhatsThis::add( tranTable, tr( "This is a listing of all transactions entered for this checkbook.\n\nTo sort entries by a specific field, click on the column name." ) );
00230     tranTable->addColumn( tr( "Id" ) );
00231     tranTable->setColumnWidthMode( COL_ID, QListView::Manual );
00232     tranTable->setColumnWidth( COL_ID, 0);
00233     tranTable->addColumn( tr( "SortDate" ) );
00234     tranTable->setColumnWidthMode( COL_SORTDATE, QListView::Manual );
00235     tranTable->setColumnWidth( COL_SORTDATE, 0);
00236     tranTable->addColumn( tr( "Num" ) );
00237     tranTable->addColumn( tr( "Date" ) );
00238     //tranTable->addColumn( tr( "Cleared" ) );
00239     tranTable->addColumn( tr( "Description" ) );
00240     int column = tranTable->addColumn( tr( "Amount" ) );
00241     tranTable->setColumnAlignment( column, Qt::AlignRight );
00242     column=tranTable->addColumn( tr("Balance") );
00243     tranTable->setColumnAlignment( column, Qt::AlignRight );
00244     tranTable->setAllColumnsShowFocus( TRUE );
00245     tranTable->setSorting( -1 );
00246     layout->addMultiCellWidget( tranTable, 1, 1, 0, 2 );
00247     QPEApplication::setStylusOperation( tranTable->viewport(), QPEApplication::RightOnHold );
00248     connect( tranTable, SIGNAL( rightButtonPressed(QListViewItem*,const QPoint&,int) ),
00249           this, SLOT( slotMenuTran(QListViewItem*,const QPoint&) ) );
00250     connect( tranTable, SIGNAL( doubleClicked(QListViewItem*) ),
00251           this, SLOT( slotEditTran() ) );
00252     _sortCol=COL_ID;
00253 
00254     // Buttons
00255     QPushButton *btn = new QPushButton( Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
00256                                         tr( "New" ), control );
00257     btn->setFixedHeight( AppLnk::smallIconSize()+4 );
00258     QWhatsThis::add( btn, tr( "Click here to add a new transaction." ) );
00259     connect( btn, SIGNAL( clicked() ), this, SLOT( slotNewTran() ) );
00260     layout->addWidget( btn, 2, 0 );
00261 
00262     btn = new QPushButton( Opie::Core::OResource::loadPixmap( "edit", Opie::Core::OResource::SmallIcon ),
00263                            tr( "Edit" ), control );
00264     btn->setFixedHeight( AppLnk::smallIconSize()+4 );
00265     QWhatsThis::add( btn, tr( "Select a transaction and then click here to edit it." ) );
00266     connect( btn, SIGNAL( clicked() ), this, SLOT( slotEditTran() ) );
00267     layout->addWidget( btn, 2, 1 );
00268 
00269     btn = new QPushButton( Opie::Core::OResource::loadPixmap( "trash", Opie::Core::OResource::SmallIcon ),
00270                            tr( "Delete" ), control );
00271     btn->setFixedHeight( AppLnk::smallIconSize()+4 );
00272     QWhatsThis::add( btn, tr( "Select a checkbook and then click here to delete it." ) );
00273     connect( btn, SIGNAL( clicked() ), this, SLOT( slotDeleteTran() ) );
00274     layout->addWidget( btn, 2, 2 );
00275 
00276     return( control );
00277 }
00278 
00279 
00280 // --- initCharts -------------------------------------------------------------
00281 QWidget *Checkbook::initCharts()
00282 {
00283     graphInfo = 0x0;
00284 
00285     QWidget *control = new QWidget( mainWidget, tr("Charts") );
00286 
00287     QGridLayout *layout = new QGridLayout( control );
00288     layout->setSpacing( 2 );
00289     layout->setMargin( 4 );
00290 
00291     graphWidget = new Graph( control );
00292     QWhatsThis::add( graphWidget, tr( "Select the desired chart below and then click on the Draw button." ) );
00293     layout->addMultiCellWidget( graphWidget, 0, 0, 0, 2 );
00294 
00295     graphList = new QComboBox( control );
00296     QWhatsThis::add( graphList, tr( "Click here to select the desired chart type." ) );
00297     graphList->insertItem( tr( "Account balance" ) );
00298     graphList->insertItem( tr( "Withdrawals by category" ) );
00299     graphList->insertItem( tr( "Deposits by category" ) );
00300 
00301     layout->addMultiCellWidget( graphList, 1, 1, 0, 1 );
00302 
00303     QPushButton *btn = new QPushButton( Opie::Core::OResource::loadPixmap( "checkbook/drawbtn", Opie::Core::OResource::SmallIcon ),
00304                                         tr( "Draw" ), control );
00305     btn->setFixedHeight( AppLnk::smallIconSize()+4 );
00306     QWhatsThis::add( btn, tr( "Click here to draw the selected chart." ) );
00307     connect( btn, SIGNAL( clicked() ), this, SLOT( slotDrawGraph() ) );
00308     layout->addWidget( btn, 1, 2 );
00309 
00310     return control;
00311 }
00312 
00313 // --- loadCheckbook ----------------------------------------------------------
00314 void Checkbook::loadCheckbook()
00315 {
00316     if ( !info )
00317     {
00318         return;
00319     }
00320 
00321     tranList = info->transactions();
00322 
00323     passwordCB->setChecked( !info->password().isNull() );
00324     nameEdit->setText( info->name() );
00325     QString temptext = info->type();
00326     int i = typeList->count();
00327     while ( i > 0 )
00328     {
00329         i--;
00330         typeList->setCurrentItem( i );
00331         if ( typeList->currentText() == temptext )
00332         {
00333             break;
00334         }
00335     }
00336     if( i<=0 ) {
00337         typeList->insertItem( temptext, 0 );
00338         typeList->setCurrentItem(0);
00339     }
00340     bankEdit->setText( info->bank() );
00341     acctNumEdit->setText( info->account() );
00342     pinNumEdit->setText( info->pin() );
00343     temptext.setNum( info->startingBalance(), 'f', 2 );
00344     balanceEdit->setText( temptext );
00345     notesEdit->setText( info->notes() );
00346 
00347     // Load transactions
00348     float amount;
00349     QString stramount;
00350     QString symbol = _pCfg->getCurrencySymbol();
00351     for ( TranInfo *tran = tranList->first(); tran; tran = tranList->next() )
00352     {
00353         amount = tran->amount();
00354         if ( tran->withdrawal() )
00355         {
00356             amount *= -1;
00357         }
00358         stramount.sprintf( "%.2f", amount );
00359         stramount.prepend( symbol );
00360         ( void ) new CBListItem( tran, tranTable, tran->getIdStr(), tran->datestr(false), tran->number(), tran->datestr(true), tran->desc(), stramount );
00361     }
00362 
00363     // set sort order
00364     bool bOk=false;
00365     for(int i=0; i<_cbSortType->count(); i++) {
00366         if( _cbSortType->text(i)==info->getSortOrder() ) {
00367             _cbSortType->setCurrentItem(i);
00368             slotSortChanged( info->getSortOrder() );
00369             bOk=true;
00370             break;
00371         }
00372     }
00373     if( !bOk ) {
00374         _cbSortType->setCurrentItem(0);
00375         slotSortChanged( _cbSortType->currentText() );
00376     }
00377 
00378     // calc running balance
00379     adjustBalance();
00380 }
00381 
00382 
00383 // --- adjustBalance ----------------------------------------------------------
00384 void Checkbook::adjustBalance()
00385 {
00386     // update running balance in register
00387     QString sRunning;
00388     QString symbol = _pCfg->getCurrencySymbol();
00389     float bal=info->startingBalance();
00390     
00391     for(CBListItem *item=(CBListItem *)tranTable->firstChild(); item; item=(CBListItem *)item->nextSibling() ) {
00392         TranInfo *tran=item->getTranInfo();
00393         bal=bal + (tran->withdrawal() ? -1 : 1)*tran->amount() - tran->fee();
00394         sRunning.sprintf( "%.2f", bal );
00395         sRunning.prepend(symbol);
00396         item->setText( COL_BAL, sRunning);
00397     }
00398 }
00399 
00400 // --- resort -----------------------------------------------------------------
00401 void Checkbook::resort()
00402 {
00403     tranTable->setSorting(_sortCol);
00404     tranTable->sort();
00405     tranTable->setSorting(-1);
00406 }
00407 
00408 
00409 // --- accept -----------------------------------------------------------------
00410 void Checkbook::accept()
00411 {
00412     info->setName( nameEdit->text() );
00413     info->setType( typeList->currentText() );
00414     info->setBank( bankEdit->text() );
00415     info->setAccount( acctNumEdit->text() );
00416     info->setPin( pinNumEdit->text() );
00417     bool ok;
00418     info->setStartingBalance( balanceEdit->text().toFloat( &ok ) );
00419     info->setNotes( notesEdit->text() );
00420 
00421     QDialog::accept();
00422 }
00423 
00424 // --- slotPasswordClicked ----------------------------------------------------
00425 void Checkbook::slotPasswordClicked()
00426 {
00427     if ( info->password().isNull() && passwordCB->isChecked() )
00428     {
00429         Password *pw = new Password( this, tr( "Enter password" ), tr( "Please enter your password:" ) );
00430         if ( pw->exec() != QDialog::Accepted  )
00431         {
00432             passwordCB->setChecked( FALSE );
00433             delete pw;
00434             return;
00435         }
00436         info->setPassword( pw->password );
00437         delete pw;
00438 
00439         pw = new Password( this, tr( "Confirm password" ), tr( "Please confirm your password:" ) );
00440         if ( pw->exec() != QDialog::Accepted || pw->password != info->password() )
00441         {
00442             passwordCB->setChecked( FALSE );
00443             info->setPassword( QString::null );
00444         }
00445 
00446         delete pw;
00447     }
00448     else if ( !info->password().isNull() && !passwordCB->isChecked() )
00449     {
00450         Password *pw = new Password( this, tr( "Enter password" ),
00451                     tr( "Please enter your password to confirm removal of password protection:" ) );
00452         if ( pw->exec() == QDialog::Accepted && pw->password == info->password() )
00453         {
00454             info->setPassword( QString::null );
00455             delete pw;
00456             return;
00457         }
00458         else
00459         {
00460             passwordCB->setChecked( TRUE );
00461         }
00462 
00463         delete pw;
00464     }
00465 }
00466 
00467 void Checkbook::slotNameChanged( const QString &newname )
00468 {
00469     info->setName( newname );
00470 
00471     // TODO - need filedir
00472 //    QString namestr = filedir;
00473 //    namestr.append( newname );
00474 //    namestr.append( ".qcb" );
00475 //    info->setFilename( namestr );
00476 
00477     QString namestr = newname;
00478     namestr.append( " - " );
00479     namestr.append( tr( "Checkbook" ) );
00480     setCaption( namestr );
00481 }
00482 
00483 
00484 // ---slotStartingBalanceChanged ----------------------------------------------
00485 void Checkbook::slotStartingBalanceChanged( const QString &newbalance )
00486 {
00487     bool ok;
00488     info->setStartingBalance( newbalance.toFloat( &ok ) );
00489     adjustBalance();
00490 }
00491 
00492 
00493 // --- slotNewTran ------------------------------------------------------------
00494 void Checkbook::slotNewTran()
00495 {
00496     TranInfo *traninfo = new TranInfo( info->getNextNumber() );
00497     if( !_dLastNew.isNull() )
00498         traninfo->setDate(_dLastNew);
00499 
00500     Transaction *currtran = new Transaction( this, true, info->name(),
00501                                              traninfo,
00502                                              _pCfg );
00503     QString symbol = _pCfg->getCurrencySymbol();
00504     if ( QPEApplication::execDialog( currtran ) == QDialog::Accepted )
00505     {
00506         // Add to transaction list
00507         info->addTransaction( traninfo );
00508 
00509         // Add to transaction table
00510         float amount;
00511         QString stramount;
00512         amount = (traninfo->withdrawal() ? -1 : 1)*traninfo->amount();  
00513         stramount.sprintf( "%.2f", amount );
00514         stramount.prepend(symbol);
00515         ( void ) new CBListItem( traninfo, tranTable, traninfo->getIdStr(), traninfo->datestr(false),
00516                                  traninfo->number(), traninfo->datestr(true), traninfo->desc(),
00517                                  stramount );
00518         resort();
00519         adjustBalance();
00520 
00521         // save last date
00522         _dLastNew = traninfo->date();
00523 
00524         // save description in list of payees, if not in there
00525         QStringList *pLst=&_pCfg->getPayees();
00526         if( _pCfg->getSavePayees() && pLst->contains(traninfo->desc())==0 ) {
00527             pLst->append( traninfo->desc() );
00528             pLst->sort();
00529             _pCfg->setDirty(true);
00530         }
00531     }
00532     else
00533     {
00534         delete traninfo;
00535     }
00536 }
00537 
00538 
00539 // --- slotEditTran -----------------------------------------------------------
00540 void Checkbook::slotEditTran()
00541 {
00542     QListViewItem *curritem = tranTable->currentItem();
00543     if ( !curritem )
00544         return;
00545 
00546     TranInfo *traninfo=info->findTransaction( curritem->text(COL_ID) );
00547 
00548     Transaction *currtran = new Transaction( this, false, info->name(),
00549                                              traninfo,
00550                                              _pCfg );
00551     if ( QPEApplication::execDialog( currtran ) == QDialog::Accepted )
00552     {
00553         curritem->setText( COL_NUM, traninfo->number() );
00554         curritem->setText( COL_SORTDATE, traninfo->datestr(false) );
00555         curritem->setText( COL_DATE, traninfo->datestr(true) );
00556         curritem->setText( COL_DESC, traninfo->desc() );
00557 
00558         float amount = traninfo->amount();
00559         if ( traninfo->withdrawal() )
00560         {
00561             amount *= -1;
00562         }
00563         QString stramount;
00564         stramount.sprintf( "%.2f", amount );
00565         stramount.prepend( _pCfg->getCurrencySymbol() );
00566         curritem->setText( COL_AMOUNT, stramount );
00567         resort();
00568         adjustBalance();
00569 
00570         // save description in list of payees, if not in there
00571         QStringList *pLst=&_pCfg->getPayees();
00572         if( _pCfg->getSavePayees() && pLst->contains(traninfo->desc())==0 ) {
00573             pLst->append( traninfo->desc() );
00574             pLst->sort();
00575             _pCfg->setDirty(true);
00576         }
00577     }
00578 
00579     delete currtran;
00580 }
00581 
00582 // --- slotMenuTran -----------------------------------------------------------
00583 void Checkbook::slotMenuTran(QListViewItem *item, const QPoint &pnt)
00584 {
00585     // active item?
00586     if( !item )
00587         return;
00588 
00589     // Display menu
00590     QPopupMenu m;
00591     m.insertItem( QWidget::tr( "Edit" ), 1 );
00592     m.insertItem( QWidget::tr( "New" ), 2 );
00593     m.insertItem( QWidget::tr( "Delete" ), 3 );
00594     int r = m.exec( pnt );
00595     switch(r) {
00596     case 1:
00597         slotEditTran();
00598         break;
00599     case 2:
00600         slotNewTran();
00601         break;
00602     case 3:
00603         slotDeleteTran();
00604         break;
00605     }
00606 }
00607 
00608 
00609 // --- slotDeleteTran ---------------------------------------------------------
00610 void Checkbook::slotDeleteTran()
00611 {
00612     QListViewItem *curritem = tranTable->currentItem();
00613     if ( !curritem )
00614         return;
00615 
00616     TranInfo *traninfo = info->findTransaction( curritem->text(COL_ID) );
00617 
00618     if ( QPEMessageBox::confirmDelete ( this, tr( "Delete transaction" ), traninfo->desc() ) )
00619     {
00620         info->removeTransaction( traninfo );
00621         delete curritem;
00622         adjustBalance();
00623     }
00624 }
00625 
00626 void Checkbook::slotDrawGraph()
00627 {
00628     if ( graphInfo )
00629     {
00630         delete graphInfo;
00631     }
00632 
00633     switch ( graphList->currentItem() )
00634     {
00635         case 0 : drawBalanceChart();
00636             break;
00637         case 1 : drawCategoryChart( TRUE );
00638             break;
00639         case 2 : drawCategoryChart( FALSE );
00640             break;
00641     };
00642 
00643     graphWidget->setGraphInfo( graphInfo );
00644     graphWidget->drawGraph( TRUE );
00645 }
00646 
00647 void Checkbook::drawBalanceChart()
00648 {
00649     DataPointList *list = new DataPointList();
00650 
00651     float balance = info->startingBalance();
00652     float amount;
00653     QString label;
00654     int i = 0;
00655     int count = tranList->count();
00656 
00657     for ( TranInfo *tran = tranList->first(); tran; tran = tranList->next() )
00658     {
00659         i++;
00660         balance -= tran->fee();
00661         amount = tran->amount();
00662         if ( tran->withdrawal() )
00663         {
00664             amount *= -1;
00665         }
00666         balance += amount;
00667         if ( i == 1 || i == count / 2 || i == count )
00668         {
00669             label = tran->datestr(true);
00670         }
00671         else
00672         {
00673             label = "";
00674         }
00675         list->append( new DataPointInfo( label, balance ) );
00676     }
00677 
00678     graphInfo = new GraphInfo( GraphInfo::BarChart, list );
00679 }
00680 
00681 void Checkbook::drawCategoryChart( bool withdrawals )
00682 {
00683     DataPointList *list = new DataPointList();
00684 
00685     TranInfo *tran = tranList->first();
00686     if ( tran && tran->withdrawal() == withdrawals )
00687     {
00688         list->append( new DataPointInfo( tran->category(), tran->amount() ) );
00689     }
00690     tran = tranList->next();
00691 
00692     DataPointInfo *cat;
00693     for ( ; tran; tran = tranList->next() )
00694     {
00695         if ( tran->withdrawal() == withdrawals )
00696         {
00697             // Find category in list
00698             for ( cat = list->first(); cat; cat = list->next() )
00699             {
00700                 if ( cat->label() == tran->category() )
00701                 {
00702                     break;
00703                 }
00704             }
00705             if ( cat && cat->label() == tran->category() )
00706             { // Found category, add to transaction to category total
00707                 cat->addToValue( tran->amount() );
00708             }
00709             else
00710             { // Didn't find category, add category to list
00711                 list->append( new DataPointInfo( tran->category(), tran->amount() ) );
00712             }
00713         }
00714     }
00715 
00716     graphInfo = new GraphInfo( GraphInfo::PieChart, list );
00717 }
00718 
00719 CBListItem::CBListItem( TranInfo *pTran, QListView *parent, QString label1, QString label2,
00720                      QString label3, QString label4, QString label5, QString label6, QString label7,
00721                      QString label8 )
00722     : QListViewItem( parent, label1, label2, label3, label4, label5, label6, label7, label8 )
00723 {
00724     _pTran=pTran;
00725     m_known = FALSE;
00726     owner = parent;
00727 }
00728 
00729 void CBListItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align )
00730 {
00731     QColorGroup _cg = cg;
00732     const QPixmap *pm = listView()->viewport()->backgroundPixmap();
00733     if ( pm && !pm->isNull() )
00734     {
00735         _cg.setBrush( QColorGroup::Base, QBrush( cg.base(), *pm ) );
00736         p->setBrushOrigin( -listView()->contentsX(), -listView()->contentsY() );
00737     }
00738     else if ( isAltBackground() )
00739         _cg.setColor(QColorGroup::Base, cg.background() );
00740 
00741     QListViewItem::paintCell(p, _cg, column, width, align);
00742 }
00743 
00744 // --- CBListItem::isAltBackground --------------------------------------------
00745 bool CBListItem::isAltBackground()
00746 {
00747     QListView *lv = static_cast<QListView *>( listView() );
00748     if ( lv )
00749     {
00750         CBListItem *above = 0;
00751         above = (CBListItem *)( itemAbove() );
00752         m_known = above ? above->m_known : true;
00753         if ( m_known )
00754         {
00755             m_odd = above ? !above->m_odd : false;
00756         }
00757         else
00758         {
00759             CBListItem *item;
00760             bool previous = true;
00761             if ( parent() )
00762             {
00763                 item = (CBListItem *)( parent() );
00764                 if ( item )
00765                     previous = item->m_odd;
00766                 item = (CBListItem *)( parent()->firstChild() );
00767             }
00768             else
00769             {
00770                 item = (CBListItem *)( lv->firstChild() );
00771             }
00772 
00773             while(item)
00774             {
00775                 item->m_odd = previous = !previous;
00776                 item->m_known = true;
00777                 item = (CBListItem *)( item->nextSibling() );
00778             }
00779         }
00780         return m_odd;
00781     }
00782     return false;
00783 }
00784 
00785 
00786 // --- slotTab ----------------------------------------------------------------
00787 void Checkbook::slotTab(QWidget *tab)
00788 {
00789     if( !tab || !info ) return;
00790     info->setLastTab( tab->name() );
00791 }
00792 
00793 
00794 // --- slotSortChanged ---------------------------------------------------------
00795 void Checkbook::slotSortChanged( const QString &selc )
00796 {
00797     if( selc==tr("Entry Order") ) {
00798         _sortCol=COL_ID;
00799     } else if( selc==tr("Number") ) {
00800         _sortCol=COL_NUM;
00801     } else if( selc==tr("Date") ) {
00802         _sortCol=COL_SORTDATE;
00803     }
00804     info->setSortOrder( selc );
00805     resort();
00806 }
00807 

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