Diffstat (limited to 'noncore/apps/checkbook/checkbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/checkbook/checkbook.cpp | 491 |
1 files changed, 491 insertions, 0 deletions
diff --git a/noncore/apps/checkbook/checkbook.cpp b/noncore/apps/checkbook/checkbook.cpp new file mode 100644 index 0000000..2919927 --- a/dev/null +++ b/noncore/apps/checkbook/checkbook.cpp @@ -0,0 +1,491 @@ +/* + This file is part of the OPIE Project + =. + .=l. Copyright (c) 2002 Dan Williams <williamsdr@acm.org> + .>+-= + _;:, .> :=|. This file is free software; you can +.> <`_, > . <= redistribute it and/or modify it under +:`=1 )Y*s>-.-- : the terms of the GNU General Public +.="- .-=="i, .._ License as published by the Free Software + - . .-<_> .<> Foundation; either version 2 of the License, + ._= =} : or (at your option) any later version. + .%`+i> _;_. + .i_,=:_. -<s. This file is distributed in the hope that + + . -:. = it will be useful, but WITHOUT ANY WARRANTY; + : .. .:, . . . without even the implied warranty of + =_ + =;=|` MERCHANTABILITY or FITNESS FOR A + _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General +..}^=.= = ; Public License for more details. +++= -. .` .: + : = ...= . :.=- You should have received a copy of the GNU + -. .:....=;==+<; General Public License along with this file; + -_. . . )=. = see the file COPYING. If not, write to the + -- :-=` Free Software Foundation, Inc., + 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + +*/ + +#include "checkbook.h" +#include "transaction.h" + +#include <opie/otabwidget.h> +#include <qpe/config.h> +#include <qpe/qpeapplication.h> +#include <qpe/qpemessagebox.h> +#include <qpe/resource.h> + +#include <qcombobox.h> +#include <qfile.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qlistview.h> +#include <qmultilineedit.h> +#include <qpushbutton.h> +#include <qwidget.h> + +Checkbook::Checkbook( QWidget *parent, const QString &n, const QString &fd, char symbol ) + : QDialog( parent, 0, TRUE, 0 ) +{ + name = n; + filename = fd + name + ".qcb"; + filedir = fd; + currencySymbol = symbol; + currBalance = 0.0; + + if ( name != "" ) + { + setCaption( name + " - " + tr( "Checkbook" ) ); + } + else + { + setCaption( tr( "New checkbook" ) ); + } + + // Setup layout to make everything pretty + QVBoxLayout *layout = new QVBoxLayout( this ); + layout->setMargin( 2 ); + layout->setSpacing( 4 ); + + // Setup tabs for all info + mainWidget = new OTabWidget( this ); + layout->addWidget( mainWidget ); + + mainWidget->addTab( initInfo(), "help_icon", tr( "Info" ) ); + mainWidget->addTab( initTransactions(), "Spreadsheet", tr( "Transactions" ) ); + mainWidget->addTab( initCharts(), "DocumentTypePowerPoint", tr( "Charts" ) ); + mainWidget->setCurrentTab( tr( "Info" ) ); + + // Load checkbook information + loadCheckbook(); +} + +Checkbook::~Checkbook() +{ +} + +const QString &Checkbook::getName() +{ + return( name ); +} + +QWidget *Checkbook::initInfo() +{ + QWidget *control = new QWidget( mainWidget ); + + QVBoxLayout *vb = new QVBoxLayout( control ); + + QScrollView *sv = new QScrollView( control ); + vb->addWidget( sv, 0, 0 ); + sv->setResizePolicy( QScrollView::AutoOneFit ); + sv->setFrameStyle( QFrame::NoFrame ); + + QWidget *container = new QWidget( sv->viewport() ); + sv->addChild( container ); + + QGridLayout *layout = new QGridLayout( container ); + layout->setSpacing( 2 ); + layout->setMargin( 4 ); + + // Account name + QLabel *label = new QLabel( tr( "Name:" ), container ); + layout->addWidget( label, 0, 0 ); + nameEdit = new QLineEdit( container ); + connect( nameEdit, SIGNAL( textChanged( const QString & ) ), + this, SLOT( slotNameChanged( const QString & ) ) ); + layout->addWidget( nameEdit, 0, 1 ); + + // Type of account + label = new QLabel( tr( "Type:" ), container ); + layout->addWidget( label, 1, 0 ); + typeList = new QComboBox( container ); + typeList->insertItem( tr( "Savings" ) ); // 0 + typeList->insertItem( tr( "Checking" ) ); // 1 + typeList->insertItem( tr( "CD" ) ); // 2 + typeList->insertItem( tr( "Money market" ) ); // 3 + typeList->insertItem( tr( "Mutual fund" ) ); // 4 + typeList->insertItem( tr( "Other" ) ); // 5 + layout->addWidget( typeList, 1, 1 ); + + // Bank/institution name + label = new QLabel( tr( "Bank:" ), container ); + layout->addWidget( label, 2, 0 ); + bankEdit = new QLineEdit( container ); + layout->addWidget( bankEdit, 2, 1 ); + + // Account number + label = new QLabel( tr( "Account number:" ), container ); + layout->addWidget( label, 3, 0 ); + acctNumEdit = new QLineEdit( container ); + layout->addWidget( acctNumEdit, 3, 1 ); + + // PIN number + label = new QLabel( tr( "PIN number:" ), container ); + layout->addWidget( label, 4, 0 ); + pinNumEdit = new QLineEdit( container ); + layout->addWidget( pinNumEdit, 4, 1 ); + + // Starting balance + label = new QLabel( tr( "Starting balance:" ), container ); + layout->addWidget( label, 5, 0 ); + balanceEdit = new QLineEdit( container ); + connect( balanceEdit, SIGNAL( textChanged( const QString & ) ), + this, SLOT( slotStartingBalanceChanged( const QString & ) ) ); + layout->addWidget( balanceEdit, 5, 1 ); + + // Notes + label = new QLabel( tr( "Notes:" ), container ); + layout->addWidget( label, 6, 0 ); + notesEdit = new QMultiLineEdit( container ); + notesEdit->setMaximumHeight( 85 ); + layout->addMultiCellWidget( notesEdit, 7, 7, 0, 1 ); + + return control; +} + +QWidget *Checkbook::initTransactions() +{ + QWidget *control = new QWidget( mainWidget ); + + QGridLayout *layout = new QGridLayout( control ); + layout->setSpacing( 2 ); + layout->setMargin( 4 ); + + balanceLabel = new QLabel( tr( "Current balance: %10.00" ).arg( currencySymbol ), + control ); + layout->addMultiCellWidget( balanceLabel, 0, 0, 0, 2 ); + + tranTable = new QListView( control ); + tranTable->addColumn( tr( "ID" ) ); + tranTable->addColumn( tr( "Date" ) ); + tranTable->addColumn( tr( "Description" ) ); + int colnum = tranTable->addColumn( tr( "Amount" ) ); + tranTable->setColumnAlignment( colnum, Qt::AlignRight ); + tranTable->setAllColumnsShowFocus( TRUE ); + layout->addMultiCellWidget( tranTable, 1, 1, 0, 2 ); + QPEApplication::setStylusOperation( tranTable->viewport(), QPEApplication::RightOnHold ); + connect( tranTable, SIGNAL( rightButtonPressed( QListViewItem *, const QPoint &, int ) ), + this, SLOT( slotEditTran() ) ); + + QPushButton *btn = new QPushButton( Resource::loadPixmap( "new" ), tr( "New" ), control ); + connect( btn, SIGNAL( clicked() ), this, SLOT( slotNewTran() ) ); + layout->addWidget( btn, 2, 0 ); + + btn = new QPushButton( Resource::loadPixmap( "edit" ), tr( "Edit" ), control ); + connect( btn, SIGNAL( clicked() ), this, SLOT( slotEditTran() ) ); + layout->addWidget( btn, 2, 1 ); + + btn = new QPushButton( Resource::loadPixmap( "editdelete" ), tr( "Delete" ), control ); + connect( btn, SIGNAL( clicked() ), this, SLOT( slotDeleteTran() ) ); + layout->addWidget( btn, 2, 2 ); + + return( control ); +} + +QWidget *Checkbook::initCharts() +{ + QWidget *control = new QWidget( mainWidget ); + + QGridLayout *layout = new QGridLayout( control ); + layout->setSpacing( 2 ); + layout->setMargin( 4 ); + +/* + QLabel *label = new QLabel( control ); + label->setText( tr( "Graph type:" ) ); + layout->addWidget( label, 0, 0 ); + graphList = new QComboBox( control ); + graphList->insertItem( tr( "By category" ) ); + graphList->insertItem( tr( "..." ) ); + graphList->insertItem( tr( "..." ) ); + layout->addWidget( graphList, 0, 1 ); +*/ + + QWidget *graphWidget = new QWidget( control ); + layout->addMultiCellWidget( graphWidget, 0, 0, 0, 1 ); + graphWidget->setBackgroundMode( QWidget::PaletteBase ); + + QPushButton *btn = new QPushButton( Resource::loadPixmap( "edit" ), tr( "Draw" ), control ); + connect( btn, SIGNAL( clicked() ), this, SLOT( slotDrawGraph() ) ); + layout->addWidget( btn, 1, 1 ); + + return control; +} + +void Checkbook::loadCheckbook() +{ + transactions.clear(); + + Config config(filename, Config::File); + + // Load info + config.setGroup( "Account" ); + nameEdit->setText( name ); + QString temptext = config.readEntry( "Type" ); + int i = typeList->count(); + while ( i > 0 ) + { + i--; + typeList->setCurrentItem( i ); + if ( typeList->currentText() == temptext ) + { + break; + } + } + bankEdit->setText( config.readEntry( "Bank", "" ) ); + acctNumEdit->setText( config.readEntry( "Number", "" ) ); + pinNumEdit->setText( config.readEntry( "PINNumber", "" ) ); + balanceEdit->setText( config.readEntry( "Balance", "0.0" ) ); + notesEdit->setText( config.readEntry( "Notes", "" ) ); + + bool ok; + currBalance = balanceEdit->text().toFloat( &ok ); + startBalance = currBalance; + + // Load transactions + TranInfo *tran; + QString trandesc = ""; + float amount; + QString stramount; + for ( int i = 1; trandesc != QString::null; i++ ) + { + tran = new TranInfo( config, i ); + trandesc = tran->desc(); + if ( trandesc != QString::null ) + { + currBalance -= tran->fee(); + amount = tran->amount(); + if ( tran->withdrawal() ) + { + amount *= -1; + } + currBalance += amount; + stramount.sprintf( "%c%.2f", currencySymbol, amount ); + + // Add to transaction list + transactions.append( tran ); + + // Add to transaction table + QDate date = tran->date(); + QString datestr = QString::number( date.month() ) + "/" + + QString::number( date.day() ) + "/" + + QString::number( date.year() ); + ( void ) new QListViewItem( tranTable, QString::number( i ), datestr, + trandesc, stramount ); + } + else + { + delete tran; + } + } + balanceLabel->setText( tr( "Current balance: %1%2" ).arg( currencySymbol ).arg( currBalance, 0, 'f', 2 ) ); + + highTranNum = transactions.count(); +} + +void Checkbook::adjustBalance( float amount ) +{ + currBalance += amount; + balanceLabel->setText( tr( "Current balance: %1%2" ).arg( currencySymbol ).arg( currBalance, 0, 'f', 2 ) ); + +} + +TranInfo *Checkbook::findTranByID( int id ) +{ + TranInfo *traninfo = transactions.first(); + while ( traninfo && traninfo->id() != id ) + { + traninfo = transactions.next(); + } + return( traninfo ); +} + +void Checkbook::accept() +{ + QFile f( filename ); + if ( f.exists() ) + { + f.remove(); + } + + Config *config = new Config(filename, Config::File); + + // Save info + config->setGroup( "Account" ); + config->writeEntry( "Type", typeList->currentText() ); + config->writeEntry( "Bank", bankEdit->text() ); + config->writeEntry( "Number", acctNumEdit->text() ); + config->writeEntry( "PINNumber", pinNumEdit->text() ); + config->writeEntry( "Balance", balanceEdit->text() ); + config->writeEntry( "Notes", notesEdit->text() ); + + // Save transactions + TranInfo *tran = transactions.first(); + int i = 1; + while ( tran ) + { + tran->write( config, i ); + tran = transactions.next(); + i++; + } + config->write(); + + QDialog::accept(); +} + +void Checkbook::slotNameChanged( const QString &newname ) +{ + name = newname; + filename = filedir + newname + ".qcb"; + setCaption( name + " - " + tr( "Checkbook" ) ); +} + +void Checkbook::slotStartingBalanceChanged( const QString &newbalance ) +{ + currBalance -= startBalance; + bool ok; + startBalance = newbalance.toFloat( &ok ); + adjustBalance( startBalance ); +} + +void Checkbook::slotNewTran() +{ + highTranNum++; + TranInfo *traninfo = new TranInfo( highTranNum ); + + Transaction *currtran = new Transaction( this, name, + traninfo, + currencySymbol ); + currtran->showMaximized(); + if ( currtran->exec() == QDialog::Accepted ) + { + float amount = traninfo->amount(); + if ( traninfo->withdrawal() ) + { + amount *= -1; + } + QString stramount; + stramount.sprintf( "%c%.2f", currencySymbol, amount ); + + // Add to transaction list + transactions.append( traninfo ); + + // Add to transaction table + + QDate date = traninfo->date(); + QString datestr = QString::number( date.month() ) + "/" + + QString::number( date.day() ) + "/" + + QString::number( date.year() ); + ( void ) new QListViewItem( tranTable, QString::number( highTranNum ), datestr, + traninfo->desc(), stramount ); + + adjustBalance( amount ); + } + else + { + highTranNum--; + delete traninfo; + } +} + +void Checkbook::slotEditTran() +{ + bool ok; + QListViewItem *curritem = tranTable->currentItem(); + if ( !curritem ) + { + return; + } + + int tranid = curritem->text( 0 ).toInt( &ok ); + TranInfo *traninfo = findTranByID( tranid ); + float origamt = traninfo->amount(); + if ( traninfo->withdrawal() ) + { + origamt *= -1; + } + + Transaction *currtran = new Transaction( this, name, + traninfo, + currencySymbol ); + currtran->showMaximized(); + if ( currtran->exec() == QDialog::Accepted ) + { + QDate date = traninfo->date(); + QString datestr = QString::number( date.month() ) + "/" + + QString::number( date.day() ) + "/" + + QString::number( date.year() ); + curritem->setText( 1, datestr ); + + curritem->setText( 2, traninfo->desc() ); + + float amount = traninfo->amount(); + if ( traninfo->withdrawal() ) + { + amount *= -1; + } + adjustBalance( origamt * -1 ); + adjustBalance( amount ); + QString stramount; + stramount.sprintf( "%c%.2f", currencySymbol, amount ); + curritem->setText( 3, stramount ); + + balanceLabel->setText( tr( "Current balance: %1%2" ).arg( currencySymbol ).arg( currBalance, 0, 'f', 2 ) ); + + delete currtran; + } +} + +void Checkbook::slotDeleteTran() +{ + QListViewItem *curritem = tranTable->currentItem(); + if ( !curritem ) + { + return; + } + + bool ok; + int tranid = curritem->text( 0 ).toInt( &ok ); + //TranInfo *traninfo = transactions.at( tranid - 1 ); + TranInfo *traninfo = findTranByID( tranid ); + + if ( QPEMessageBox::confirmDelete ( this, tr( "Delete transaction" ), traninfo->desc() ) ) + { + float amount = traninfo->amount(); + if ( traninfo->withdrawal() ) + { + amount *= -1; + } + + transactions.remove( traninfo ); + delete traninfo; + delete curritem; + + adjustBalance( amount * -1 ); + } +} + +void Checkbook::slotDrawGraph() +{ +} |