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

transaction.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 "transaction.h"
00030 #include "traninfo.h"
00031 #include "cfg.h"
00032 #include "checkbook.h"
00033 
00034 /* OPIE */
00035 #include <opie2/odebug.h>
00036 #include <qpe/datebookmonth.h>
00037 using namespace Opie::Core;
00038 
00039 /* QT */
00040 #include <qbuttongroup.h>
00041 #include <qcombobox.h>
00042 #include <qlabel.h>
00043 #include <qlayout.h>
00044 #include <qlineedit.h>
00045 #include <qmultilineedit.h>
00046 #include <qradiobutton.h>
00047 #include <qwhatsthis.h>
00048 
00049 Transaction::Transaction( QWidget *parent, bool bNew, const QString &acctname,
00050                                                          TranInfo *info, Cfg *pCfg )
00051         : QDialog( parent, 0, TRUE, WStyle_ContextHelp )
00052 {
00053         QString tempstr = tr( "Transaction for " );
00054         tempstr.append( acctname );
00055         setCaption( tempstr );
00056 
00057     _bNew=bNew;
00058         tran = info;
00059         _pCfg=pCfg;
00060 
00061         QVBoxLayout *vb = new QVBoxLayout( this );
00062 
00063         QScrollView *sv = new QScrollView( this );
00064         vb->addWidget( sv, 0, 0 );
00065         sv->setResizePolicy( QScrollView::AutoOneFit );
00066         sv->setFrameStyle( QFrame::NoFrame );
00067 
00068         QWidget *container = new QWidget( sv->viewport() );
00069         sv->addChild( container );
00070 
00071         QGridLayout *layout = new QGridLayout( container );
00072         layout->setSpacing( 2 );
00073         layout->setMargin( 4 );
00074 
00075         // Withdrawal/Deposit
00076         QButtonGroup *btngrp = new QButtonGroup( container );
00077         btngrp->setColumnLayout(0, Qt::Vertical );
00078         btngrp->layout()->setSpacing( 0 );
00079         btngrp->layout()->setMargin( 0 );
00080         btngrp->setMaximumWidth( 220 );
00081         QGridLayout *layout2 = new QGridLayout( btngrp->layout() );
00082         layout2->setSpacing( 2 );
00083         layout2->setMargin( 2 );
00084         withBtn = new QRadioButton( tr( "Withdrawal" ), btngrp );
00085         QWhatsThis::add( withBtn, tr( "Select whether the transaction is a withdrawal or deposit here." ) );
00086         layout2->addWidget( withBtn, 0, 0 );
00087         connect( withBtn, SIGNAL( clicked() ), this, SLOT( slotWithdrawalClicked() ) );
00088         depBtn = new QRadioButton( tr( "Deposit" ), btngrp );
00089         QWhatsThis::add( depBtn, tr( "Select whether the transaction is a withdrawal or deposit here." ) );
00090         layout2->addWidget( depBtn, 0, 1 );
00091         btngrp->setMaximumSize( 320, withBtn->height() );
00092         connect( depBtn, SIGNAL( clicked() ), this, SLOT( slotDepositClicked() ) );
00093         layout->addMultiCellWidget( btngrp, 0, 0, 0, 3 );
00094 
00095         // Date
00096         QLabel *label = new QLabel( tr( "Date:" ), container );
00097         QWhatsThis::add( label, tr( "Select date of transaction here." ) );
00098         layout->addWidget( label, 1, 0 );
00099         dateBtn = new QPushButton( TimeString::shortDate( QDate::currentDate() ),
00100                                                                 container );
00101         QWhatsThis::add( dateBtn, tr( "Select date of transaction here." ) );
00102         QPopupMenu *m1 = new QPopupMenu( container );
00103         datePicker = new DateBookMonth( m1, 0, TRUE );
00104         m1->insertItem( datePicker );
00105         dateBtn->setPopup( m1 );
00106         connect( datePicker, SIGNAL( dateClicked(int,int,int) ),
00107                          this, SLOT( slotDateChanged(int,int,int) ) );
00108         layout->addWidget( dateBtn, 1, 1 );
00109 
00110         // Check number
00111         label = new QLabel( tr( "Number:" ), container );
00112         QWhatsThis::add( label, tr( "Enter check number here." ) );
00113         layout->addWidget( label, 1, 2 );
00114         numEdit = new QLineEdit( container );
00115         QWhatsThis::add( numEdit, tr( "Enter check number here." ) );
00116         numEdit->setMaximumWidth( 40 );
00117         layout->addWidget( numEdit, 1, 3 );
00118 
00119         // Description
00120         label = new QLabel( tr( "Description:" ), container );
00121         QWhatsThis::add( label, tr( "Enter description of transaction here." ) );
00122         layout->addWidget( label, 2, 0 );
00123     _cbDesc=new QComboBox( true, container );
00124     _cbDesc->insertStringList( _pCfg->getPayees() );
00125         QWhatsThis::add( _cbDesc, tr( "Enter description of transaction here." ) );
00126         layout->addMultiCellWidget( _cbDesc, 2, 2, 1, 3 );
00127     connect( _cbDesc, SIGNAL( activated(const QString&) ), this, SLOT( slotActivated(const QString&) ) );
00128 
00129 
00130         // Category
00131         label = new QLabel( tr( "Category:" ), container );
00132         QWhatsThis::add( label, tr( "Select transaction category here." ) );
00133         layout->addWidget( label, 3, 0 );
00134         catList = new QComboBox( container );
00135         QWhatsThis::add( catList, tr( "Select transaction category here." ) );
00136         layout->addMultiCellWidget( catList, 3, 3, 1, 3 );
00137 
00138         // Type
00139         label = new QLabel( tr( "Type:" ), container );
00140         QWhatsThis::add( label, tr( "Select transaction type here.\n\nThe options available vary based on whether the transaction is a deposit or withdrawal." ) );
00141         layout->addWidget( label, 4, 0 );
00142         typeList = new QComboBox( container );
00143         QWhatsThis::add( typeList, tr( "Select transaction type here.\n\nThe options available vary based on whether the transaction is a deposit or withdrawal." ) );
00144         layout->addMultiCellWidget( typeList, 4, 4, 1, 3 );
00145 
00146 
00147         // Amount
00148         label = new QLabel( tr( "Amount:" ), container );
00149         QWhatsThis::add( label, tr( "Enter the amount of transaction here.\n\nThe value entered should always be positive." ) );
00150         layout->addWidget( label, 5, 0 );
00151         amtEdit = new QLineEdit( container );
00152         QWhatsThis::add( amtEdit, tr( "Enter the amount of transaction here.\n\nThe value entered should always be positive." ) );
00153         layout->addMultiCellWidget( amtEdit, 5, 5, 1, 3 );
00154 
00155         // Fee
00156         label = new QLabel( tr( "Fee:" ), container );
00157         QWhatsThis::add( label, tr( "Enter any fee amount assoiciated with this transaction.\n\nThe value entered should always be positive." ) );
00158         layout->addWidget( label, 6, 0 );
00159         feeEdit = new QLineEdit( container );
00160         QWhatsThis::add( feeEdit, tr( "Enter any fee amount assoiciated with this transaction.\n\nThe value entered should always be positive." ) );
00161         layout->addMultiCellWidget( feeEdit, 6, 6, 1, 3 );
00162 
00163         // Notes
00164         label = new QLabel( tr( "Notes:" ), container );
00165         QWhatsThis::add( label, tr( "Enter any additional information for this transaction here." ) );
00166         layout->addWidget( label, 7, 0 );
00167         noteEdit = new QMultiLineEdit( container );
00168         QWhatsThis::add( noteEdit, tr( "Enter any additional information for this transaction here." ) );
00169         layout->addMultiCellWidget( noteEdit, 8, 8, 0, 3 );
00170 
00171     // init date
00172     initFromInfo( info );
00173 
00174     // not new handlers
00175     connect( withBtn, SIGNAL( toggled(bool) ), this, SLOT( slotNotNew() ) );
00176     connect( depBtn, SIGNAL( toggled(bool) ), this, SLOT( slotNotNew() ) );
00177     connect( catList, SIGNAL(activated(const QString&)), this, SLOT( slotNotNew() ) );
00178     connect( typeList, SIGNAL(activated(const QString&)), this, SLOT( slotNotNew() ) );
00179     connect( amtEdit, SIGNAL(textChanged(const QString&)), this, SLOT( slotNotNew() ) );
00180     connect( feeEdit, SIGNAL(textChanged(const QString&)), this, SLOT( slotNotNew() ) );
00181     connect( noteEdit, SIGNAL(textChanged()), this, SLOT( slotNotNew() ) );
00182 }
00183 
00184 // --- initFromInfo -----------------------------------------------------------
00185 void Transaction::initFromInfo(TranInfo *info, bool bPopulateOld)
00186 {
00187         // Populate current values if provided
00188         if ( info )
00189         {
00190                 if ( info->withdrawal() )
00191                 {
00192                         withBtn->setChecked( TRUE );
00193                         slotWithdrawalClicked();
00194                 }
00195                 else
00196                 {
00197                         depBtn->setChecked( TRUE );
00198                         slotDepositClicked();
00199                 }
00200 
00201         if( !bPopulateOld ) {
00202                     QDate dt = info->date();
00203                     slotDateChanged( dt.year(), dt.month(), dt.day() );
00204                     datePicker->setDate( dt );
00205                     numEdit->setText( info->number() );
00206         }
00207                 QString temptext = info->category();
00208 
00209         // set description field
00210         int i;
00211         for(i=_cbDesc->count()-1; i>=0; i--) {
00212             if( _cbDesc->text(i)==info->desc() ) {
00213                 _cbDesc->setCurrentItem(i);
00214                 break;
00215             }
00216         }
00217         if( i<=0 )
00218             _cbDesc->setEditText( info->desc() );
00219 
00220                 i = catList->count();
00221                 while ( i > 0 )
00222                 {
00223                         i--;
00224                         catList->setCurrentItem( i );
00225                         if ( catList->currentText() == temptext )
00226                         {
00227                                 break;
00228                         }
00229                 }
00230                 temptext = info->type();
00231                 i = typeList->count();
00232                 while ( i > 0  )
00233                 {
00234                         i--;
00235                         typeList->setCurrentItem( i );
00236                         if ( typeList->currentText() == temptext )
00237                         {
00238                                 break;
00239                         }
00240                 }
00241                 amtEdit->setText( QString( "%1" ).arg( info->amount(), 0, 'f', 2 ) );
00242                 feeEdit->setText( QString( "%1" ).arg( info->fee(), 0, 'f', 2 ) );
00243                 noteEdit->setText( info->notes() );
00244         }
00245         else
00246         {
00247                 withBtn->setChecked( TRUE );
00248         }
00249 }
00250 
00251 
00252 // --- ~Transaction -----------------------------------------------------------
00253 Transaction::~Transaction()
00254 {
00255 }
00256 
00257 // --- accept -----------------------------------------------------------------
00258 void Transaction::accept()
00259 {
00260         tran->setDesc( _cbDesc->currentText() );
00261         tran->setDate( datePicker->selectedDate() );
00262         tran->setWithdrawal( withBtn->isChecked() );
00263         tran->setType( typeList->currentText() );
00264         tran->setCategory( catList->currentText() );
00265         bool ok;
00266         tran->setAmount( amtEdit->text().toFloat( &ok ) );
00267         tran->setFee( feeEdit->text().toFloat( &ok ) );
00268         tran->setNumber( numEdit->text() );
00269         tran->setNotes( noteEdit->text() );
00270 
00271         QDialog::accept();
00272 }
00273 
00274 void Transaction::slotWithdrawalClicked()
00275 {
00276         catList->clear();
00277     CategoryList *pCatList=_pCfg->getCategoryList();
00278     for(Category *pCat=pCatList->first(); pCat; pCat=pCatList->next()) {
00279         if( !pCat->isIncome() )
00280             catList->insertItem( pCat->getName() );
00281     }
00282     catList->setCurrentItem(0);
00283 
00284         typeList->clear();
00285         typeList->insertItem( tr( "Debit Charge" ) );
00286         typeList->insertItem( tr( "Written Check" ) );
00287         typeList->insertItem( tr( "Transfer" ) );
00288         typeList->insertItem( tr( "Credit Card" ) );
00289 }
00290 
00291 void Transaction::slotDepositClicked()
00292 {
00293         catList->clear();
00294         CategoryList *pCatList=_pCfg->getCategoryList();
00295     for(Category *pCat=pCatList->first(); pCat; pCat=pCatList->next()) {
00296         if( pCat->isIncome() )
00297             catList->insertItem( pCat->getName() );
00298     }
00299     catList->setCurrentItem( 0 );
00300 
00301         typeList->clear();
00302         typeList->insertItem( tr( "Written Check" ) );
00303         typeList->insertItem( tr( "Automatic Payment" ) );
00304         typeList->insertItem( tr( "Transfer" ) );
00305         typeList->insertItem( tr( "Cash" ) );
00306 }
00307 
00308 // --- slotDateChanged --------------------------------------------------------
00309 void Transaction::slotDateChanged( int y, int m, int d )
00310 {
00311         QDate date;
00312         date.setYMD( y, m, d );
00313         dateBtn->setText( TimeString::shortDate( date ) );
00314 }
00315 
00316 
00317 
00318 // --- slotActivated ----------------------------------------------------------
00319 // Search for the most recent transaction with this description/payee and
00320 // fill amount etc here, as long the new flag is set
00321 void Transaction::slotActivated(const QString &arg )
00322 {
00323     if( !_bNew ) return;
00324     TranInfoList *pTl=((Checkbook *)parentWidget())->getTranList();
00325     if( pTl ) {
00326         TranInfo *pTi=pTl->findMostRecentByDesc( arg );
00327         if( pTi ) {
00328             initFromInfo( pTi, true );
00329             amtEdit->setFocus();
00330             amtEdit->setSelection(0, amtEdit->text().length() );
00331             amtEdit->setCursorPosition(0);
00332         }
00333     }
00334 }
00335 
00336 // slotNotNew -----------------------------------------------------------------
00337 void Transaction::slotNotNew()
00338 {
00339     odebug << "Not new" << oendl; 
00340     _bNew=false;
00341 }

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