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 @@ | |||
1 | /* | ||
2 | This file is part of the OPIE Project | ||
3 | =. | ||
4 | .=l. Copyright (c) 2002 Dan Williams <williamsdr@acm.org> | ||
5 | .>+-= | ||
6 | _;:, .> :=|. This file is free software; you can | ||
7 | .> <`_, > . <= redistribute it and/or modify it under | ||
8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public | ||
9 | .="- .-=="i, .._ License as published by the Free Software | ||
10 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
11 | ._= =} : or (at your option) any later version. | ||
12 | .%`+i> _;_. | ||
13 | .i_,=:_. -<s. This file is distributed in the hope that | ||
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
15 | : .. .:, . . . without even the implied warranty of | ||
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | ||
18 | ..}^=.= = ; Public License for more details. | ||
19 | ++= -. .` .: | ||
20 | : = ...= . :.=- You should have received a copy of the GNU | ||
21 | -. .:....=;==+<; General Public License along with this file; | ||
22 | -_. . . )=. = see the file COPYING. If not, write to the | ||
23 | -- :-=` Free Software Foundation, Inc., | ||
24 | 59 Temple Place - Suite 330, | ||
25 | Boston, MA 02111-1307, USA. | ||
26 | |||
27 | */ | ||
28 | |||
29 | #include "checkbook.h" | ||
30 | #include "transaction.h" | ||
31 | |||
32 | #include <opie/otabwidget.h> | ||
33 | #include <qpe/config.h> | ||
34 | #include <qpe/qpeapplication.h> | ||
35 | #include <qpe/qpemessagebox.h> | ||
36 | #include <qpe/resource.h> | ||
37 | |||
38 | #include <qcombobox.h> | ||
39 | #include <qfile.h> | ||
40 | #include <qlabel.h> | ||
41 | #include <qlayout.h> | ||
42 | #include <qlineedit.h> | ||
43 | #include <qlistview.h> | ||
44 | #include <qmultilineedit.h> | ||
45 | #include <qpushbutton.h> | ||
46 | #include <qwidget.h> | ||
47 | |||
48 | Checkbook::Checkbook( QWidget *parent, const QString &n, const QString &fd, char symbol ) | ||
49 | : QDialog( parent, 0, TRUE, 0 ) | ||
50 | { | ||
51 | name = n; | ||
52 | filename = fd + name + ".qcb"; | ||
53 | filedir = fd; | ||
54 | currencySymbol = symbol; | ||
55 | currBalance = 0.0; | ||
56 | |||
57 | if ( name != "" ) | ||
58 | { | ||
59 | setCaption( name + " - " + tr( "Checkbook" ) ); | ||
60 | } | ||
61 | else | ||
62 | { | ||
63 | setCaption( tr( "New checkbook" ) ); | ||
64 | } | ||
65 | |||
66 | // Setup layout to make everything pretty | ||
67 | QVBoxLayout *layout = new QVBoxLayout( this ); | ||
68 | layout->setMargin( 2 ); | ||
69 | layout->setSpacing( 4 ); | ||
70 | |||
71 | // Setup tabs for all info | ||
72 | mainWidget = new OTabWidget( this ); | ||
73 | layout->addWidget( mainWidget ); | ||
74 | |||
75 | mainWidget->addTab( initInfo(), "help_icon", tr( "Info" ) ); | ||
76 | mainWidget->addTab( initTransactions(), "Spreadsheet", tr( "Transactions" ) ); | ||
77 | mainWidget->addTab( initCharts(), "DocumentTypePowerPoint", tr( "Charts" ) ); | ||
78 | mainWidget->setCurrentTab( tr( "Info" ) ); | ||
79 | |||
80 | // Load checkbook information | ||
81 | loadCheckbook(); | ||
82 | } | ||
83 | |||
84 | Checkbook::~Checkbook() | ||
85 | { | ||
86 | } | ||
87 | |||
88 | const QString &Checkbook::getName() | ||
89 | { | ||
90 | return( name ); | ||
91 | } | ||
92 | |||
93 | QWidget *Checkbook::initInfo() | ||
94 | { | ||
95 | QWidget *control = new QWidget( mainWidget ); | ||
96 | |||
97 | QVBoxLayout *vb = new QVBoxLayout( control ); | ||
98 | |||
99 | QScrollView *sv = new QScrollView( control ); | ||
100 | vb->addWidget( sv, 0, 0 ); | ||
101 | sv->setResizePolicy( QScrollView::AutoOneFit ); | ||
102 | sv->setFrameStyle( QFrame::NoFrame ); | ||
103 | |||
104 | QWidget *container = new QWidget( sv->viewport() ); | ||
105 | sv->addChild( container ); | ||
106 | |||
107 | QGridLayout *layout = new QGridLayout( container ); | ||
108 | layout->setSpacing( 2 ); | ||
109 | layout->setMargin( 4 ); | ||
110 | |||
111 | // Account name | ||
112 | QLabel *label = new QLabel( tr( "Name:" ), container ); | ||
113 | layout->addWidget( label, 0, 0 ); | ||
114 | nameEdit = new QLineEdit( container ); | ||
115 | connect( nameEdit, SIGNAL( textChanged( const QString & ) ), | ||
116 | this, SLOT( slotNameChanged( const QString & ) ) ); | ||
117 | layout->addWidget( nameEdit, 0, 1 ); | ||
118 | |||
119 | // Type of account | ||
120 | label = new QLabel( tr( "Type:" ), container ); | ||
121 | layout->addWidget( label, 1, 0 ); | ||
122 | typeList = new QComboBox( container ); | ||
123 | typeList->insertItem( tr( "Savings" ) ); // 0 | ||
124 | typeList->insertItem( tr( "Checking" ) ); // 1 | ||
125 | typeList->insertItem( tr( "CD" ) ); // 2 | ||
126 | typeList->insertItem( tr( "Money market" ) );// 3 | ||
127 | typeList->insertItem( tr( "Mutual fund" ) );// 4 | ||
128 | typeList->insertItem( tr( "Other" ) ); // 5 | ||
129 | layout->addWidget( typeList, 1, 1 ); | ||
130 | |||
131 | // Bank/institution name | ||
132 | label = new QLabel( tr( "Bank:" ), container ); | ||
133 | layout->addWidget( label, 2, 0 ); | ||
134 | bankEdit = new QLineEdit( container ); | ||
135 | layout->addWidget( bankEdit, 2, 1 ); | ||
136 | |||
137 | // Account number | ||
138 | label = new QLabel( tr( "Account number:" ), container ); | ||
139 | layout->addWidget( label, 3, 0 ); | ||
140 | acctNumEdit = new QLineEdit( container ); | ||
141 | layout->addWidget( acctNumEdit, 3, 1 ); | ||
142 | |||
143 | // PIN number | ||
144 | label = new QLabel( tr( "PIN number:" ), container ); | ||
145 | layout->addWidget( label, 4, 0 ); | ||
146 | pinNumEdit = new QLineEdit( container ); | ||
147 | layout->addWidget( pinNumEdit, 4, 1 ); | ||
148 | |||
149 | // Starting balance | ||
150 | label = new QLabel( tr( "Starting balance:" ), container ); | ||
151 | layout->addWidget( label, 5, 0 ); | ||
152 | balanceEdit = new QLineEdit( container ); | ||
153 | connect( balanceEdit, SIGNAL( textChanged( const QString & ) ), | ||
154 | this, SLOT( slotStartingBalanceChanged( const QString & ) ) ); | ||
155 | layout->addWidget( balanceEdit, 5, 1 ); | ||
156 | |||
157 | // Notes | ||
158 | label = new QLabel( tr( "Notes:" ), container ); | ||
159 | layout->addWidget( label, 6, 0 ); | ||
160 | notesEdit = new QMultiLineEdit( container ); | ||
161 | notesEdit->setMaximumHeight( 85 ); | ||
162 | layout->addMultiCellWidget( notesEdit, 7, 7, 0, 1 ); | ||
163 | |||
164 | return control; | ||
165 | } | ||
166 | |||
167 | QWidget *Checkbook::initTransactions() | ||
168 | { | ||
169 | QWidget *control = new QWidget( mainWidget ); | ||
170 | |||
171 | QGridLayout *layout = new QGridLayout( control ); | ||
172 | layout->setSpacing( 2 ); | ||
173 | layout->setMargin( 4 ); | ||
174 | |||
175 | balanceLabel = new QLabel( tr( "Current balance: %10.00" ).arg( currencySymbol ), | ||
176 | control ); | ||
177 | layout->addMultiCellWidget( balanceLabel, 0, 0, 0, 2 ); | ||
178 | |||
179 | tranTable = new QListView( control ); | ||
180 | tranTable->addColumn( tr( "ID" ) ); | ||
181 | tranTable->addColumn( tr( "Date" ) ); | ||
182 | tranTable->addColumn( tr( "Description" ) ); | ||
183 | int colnum = tranTable->addColumn( tr( "Amount" ) ); | ||
184 | tranTable->setColumnAlignment( colnum, Qt::AlignRight ); | ||
185 | tranTable->setAllColumnsShowFocus( TRUE ); | ||
186 | layout->addMultiCellWidget( tranTable, 1, 1, 0, 2 ); | ||
187 | QPEApplication::setStylusOperation( tranTable->viewport(), QPEApplication::RightOnHold ); | ||
188 | connect( tranTable, SIGNAL( rightButtonPressed( QListViewItem *, const QPoint &, int ) ), | ||
189 | this, SLOT( slotEditTran() ) ); | ||
190 | |||
191 | QPushButton *btn = new QPushButton( Resource::loadPixmap( "new" ), tr( "New" ), control ); | ||
192 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotNewTran() ) ); | ||
193 | layout->addWidget( btn, 2, 0 ); | ||
194 | |||
195 | btn = new QPushButton( Resource::loadPixmap( "edit" ), tr( "Edit" ), control ); | ||
196 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotEditTran() ) ); | ||
197 | layout->addWidget( btn, 2, 1 ); | ||
198 | |||
199 | btn = new QPushButton( Resource::loadPixmap( "editdelete" ), tr( "Delete" ), control ); | ||
200 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotDeleteTran() ) ); | ||
201 | layout->addWidget( btn, 2, 2 ); | ||
202 | |||
203 | return( control ); | ||
204 | } | ||
205 | |||
206 | QWidget *Checkbook::initCharts() | ||
207 | { | ||
208 | QWidget *control = new QWidget( mainWidget ); | ||
209 | |||
210 | QGridLayout *layout = new QGridLayout( control ); | ||
211 | layout->setSpacing( 2 ); | ||
212 | layout->setMargin( 4 ); | ||
213 | |||
214 | /* | ||
215 | QLabel *label = new QLabel( control ); | ||
216 | label->setText( tr( "Graph type:" ) ); | ||
217 | layout->addWidget( label, 0, 0 ); | ||
218 | graphList = new QComboBox( control ); | ||
219 | graphList->insertItem( tr( "By category" ) ); | ||
220 | graphList->insertItem( tr( "..." ) ); | ||
221 | graphList->insertItem( tr( "..." ) ); | ||
222 | layout->addWidget( graphList, 0, 1 ); | ||
223 | */ | ||
224 | |||
225 | QWidget *graphWidget = new QWidget( control ); | ||
226 | layout->addMultiCellWidget( graphWidget, 0, 0, 0, 1 ); | ||
227 | graphWidget->setBackgroundMode( QWidget::PaletteBase ); | ||
228 | |||
229 | QPushButton *btn = new QPushButton( Resource::loadPixmap( "edit" ), tr( "Draw" ), control ); | ||
230 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotDrawGraph() ) ); | ||
231 | layout->addWidget( btn, 1, 1 ); | ||
232 | |||
233 | return control; | ||
234 | } | ||
235 | |||
236 | void Checkbook::loadCheckbook() | ||
237 | { | ||
238 | transactions.clear(); | ||
239 | |||
240 | Config config(filename, Config::File); | ||
241 | |||
242 | // Load info | ||
243 | config.setGroup( "Account" ); | ||
244 | nameEdit->setText( name ); | ||
245 | QString temptext = config.readEntry( "Type" ); | ||
246 | int i = typeList->count(); | ||
247 | while ( i > 0 ) | ||
248 | { | ||
249 | i--; | ||
250 | typeList->setCurrentItem( i ); | ||
251 | if ( typeList->currentText() == temptext ) | ||
252 | { | ||
253 | break; | ||
254 | } | ||
255 | } | ||
256 | bankEdit->setText( config.readEntry( "Bank", "" ) ); | ||
257 | acctNumEdit->setText( config.readEntry( "Number", "" ) ); | ||
258 | pinNumEdit->setText( config.readEntry( "PINNumber", "" ) ); | ||
259 | balanceEdit->setText( config.readEntry( "Balance", "0.0" ) ); | ||
260 | notesEdit->setText( config.readEntry( "Notes", "" ) ); | ||
261 | |||
262 | bool ok; | ||
263 | currBalance = balanceEdit->text().toFloat( &ok ); | ||
264 | startBalance = currBalance; | ||
265 | |||
266 | // Load transactions | ||
267 | TranInfo *tran; | ||
268 | QString trandesc = ""; | ||
269 | float amount; | ||
270 | QString stramount; | ||
271 | for ( int i = 1; trandesc != QString::null; i++ ) | ||
272 | { | ||
273 | tran = new TranInfo( config, i ); | ||
274 | trandesc = tran->desc(); | ||
275 | if ( trandesc != QString::null ) | ||
276 | { | ||
277 | currBalance -= tran->fee(); | ||
278 | amount = tran->amount(); | ||
279 | if ( tran->withdrawal() ) | ||
280 | { | ||
281 | amount *= -1; | ||
282 | } | ||
283 | currBalance += amount; | ||
284 | stramount.sprintf( "%c%.2f", currencySymbol, amount ); | ||
285 | |||
286 | // Add to transaction list | ||
287 | transactions.append( tran ); | ||
288 | |||
289 | // Add to transaction table | ||
290 | QDate date = tran->date(); | ||
291 | QString datestr = QString::number( date.month() ) + "/" + | ||
292 | QString::number( date.day() ) + "/" + | ||
293 | QString::number( date.year() ); | ||
294 | ( void ) new QListViewItem( tranTable, QString::number( i ), datestr, | ||
295 | trandesc, stramount ); | ||
296 | } | ||
297 | else | ||
298 | { | ||
299 | delete tran; | ||
300 | } | ||
301 | } | ||
302 | balanceLabel->setText( tr( "Current balance: %1%2" ).arg( currencySymbol ).arg( currBalance, 0, 'f', 2 ) ); | ||
303 | |||
304 | highTranNum = transactions.count(); | ||
305 | } | ||
306 | |||
307 | void Checkbook::adjustBalance( float amount ) | ||
308 | { | ||
309 | currBalance += amount; | ||
310 | balanceLabel->setText( tr( "Current balance: %1%2" ).arg( currencySymbol ).arg( currBalance, 0, 'f', 2 ) ); | ||
311 | |||
312 | } | ||
313 | |||
314 | TranInfo *Checkbook::findTranByID( int id ) | ||
315 | { | ||
316 | TranInfo *traninfo = transactions.first(); | ||
317 | while ( traninfo && traninfo->id() != id ) | ||
318 | { | ||
319 | traninfo = transactions.next(); | ||
320 | } | ||
321 | return( traninfo ); | ||
322 | } | ||
323 | |||
324 | void Checkbook::accept() | ||
325 | { | ||
326 | QFile f( filename ); | ||
327 | if ( f.exists() ) | ||
328 | { | ||
329 | f.remove(); | ||
330 | } | ||
331 | |||
332 | Config *config = new Config(filename, Config::File); | ||
333 | |||
334 | // Save info | ||
335 | config->setGroup( "Account" ); | ||
336 | config->writeEntry( "Type", typeList->currentText() ); | ||
337 | config->writeEntry( "Bank", bankEdit->text() ); | ||
338 | config->writeEntry( "Number", acctNumEdit->text() ); | ||
339 | config->writeEntry( "PINNumber", pinNumEdit->text() ); | ||
340 | config->writeEntry( "Balance", balanceEdit->text() ); | ||
341 | config->writeEntry( "Notes", notesEdit->text() ); | ||
342 | |||
343 | // Save transactions | ||
344 | TranInfo *tran = transactions.first(); | ||
345 | int i = 1; | ||
346 | while ( tran ) | ||
347 | { | ||
348 | tran->write( config, i ); | ||
349 | tran = transactions.next(); | ||
350 | i++; | ||
351 | } | ||
352 | config->write(); | ||
353 | |||
354 | QDialog::accept(); | ||
355 | } | ||
356 | |||
357 | void Checkbook::slotNameChanged( const QString &newname ) | ||
358 | { | ||
359 | name = newname; | ||
360 | filename = filedir + newname + ".qcb"; | ||
361 | setCaption( name + " - " + tr( "Checkbook" ) ); | ||
362 | } | ||
363 | |||
364 | void Checkbook::slotStartingBalanceChanged( const QString &newbalance ) | ||
365 | { | ||
366 | currBalance -= startBalance; | ||
367 | bool ok; | ||
368 | startBalance = newbalance.toFloat( &ok ); | ||
369 | adjustBalance( startBalance ); | ||
370 | } | ||
371 | |||
372 | void Checkbook::slotNewTran() | ||
373 | { | ||
374 | highTranNum++; | ||
375 | TranInfo *traninfo = new TranInfo( highTranNum ); | ||
376 | |||
377 | Transaction *currtran = new Transaction( this, name, | ||
378 | traninfo, | ||
379 | currencySymbol ); | ||
380 | currtran->showMaximized(); | ||
381 | if ( currtran->exec() == QDialog::Accepted ) | ||
382 | { | ||
383 | float amount = traninfo->amount(); | ||
384 | if ( traninfo->withdrawal() ) | ||
385 | { | ||
386 | amount *= -1; | ||
387 | } | ||
388 | QString stramount; | ||
389 | stramount.sprintf( "%c%.2f", currencySymbol, amount ); | ||
390 | |||
391 | // Add to transaction list | ||
392 | transactions.append( traninfo ); | ||
393 | |||
394 | // Add to transaction table | ||
395 | |||
396 | QDate date = traninfo->date(); | ||
397 | QString datestr = QString::number( date.month() ) + "/" + | ||
398 | QString::number( date.day() ) + "/" + | ||
399 | QString::number( date.year() ); | ||
400 | ( void ) new QListViewItem( tranTable, QString::number( highTranNum ), datestr, | ||
401 | traninfo->desc(), stramount ); | ||
402 | |||
403 | adjustBalance( amount ); | ||
404 | } | ||
405 | else | ||
406 | { | ||
407 | highTranNum--; | ||
408 | delete traninfo; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | void Checkbook::slotEditTran() | ||
413 | { | ||
414 | bool ok; | ||
415 | QListViewItem *curritem = tranTable->currentItem(); | ||
416 | if ( !curritem ) | ||
417 | { | ||
418 | return; | ||
419 | } | ||
420 | |||
421 | int tranid = curritem->text( 0 ).toInt( &ok ); | ||
422 | TranInfo *traninfo = findTranByID( tranid ); | ||
423 | float origamt = traninfo->amount(); | ||
424 | if ( traninfo->withdrawal() ) | ||
425 | { | ||
426 | origamt *= -1; | ||
427 | } | ||
428 | |||
429 | Transaction *currtran = new Transaction( this, name, | ||
430 | traninfo, | ||
431 | currencySymbol ); | ||
432 | currtran->showMaximized(); | ||
433 | if ( currtran->exec() == QDialog::Accepted ) | ||
434 | { | ||
435 | QDate date = traninfo->date(); | ||
436 | QString datestr = QString::number( date.month() ) + "/" + | ||
437 | QString::number( date.day() ) + "/" + | ||
438 | QString::number( date.year() ); | ||
439 | curritem->setText( 1, datestr ); | ||
440 | |||
441 | curritem->setText( 2, traninfo->desc() ); | ||
442 | |||
443 | float amount = traninfo->amount(); | ||
444 | if ( traninfo->withdrawal() ) | ||
445 | { | ||
446 | amount *= -1; | ||
447 | } | ||
448 | adjustBalance( origamt * -1 ); | ||
449 | adjustBalance( amount ); | ||
450 | QString stramount; | ||
451 | stramount.sprintf( "%c%.2f", currencySymbol, amount ); | ||
452 | curritem->setText( 3, stramount ); | ||
453 | |||
454 | balanceLabel->setText( tr( "Current balance: %1%2" ).arg( currencySymbol ).arg( currBalance, 0, 'f', 2 ) ); | ||
455 | |||
456 | delete currtran; | ||
457 | } | ||
458 | } | ||
459 | |||
460 | void Checkbook::slotDeleteTran() | ||
461 | { | ||
462 | QListViewItem *curritem = tranTable->currentItem(); | ||
463 | if ( !curritem ) | ||
464 | { | ||
465 | return; | ||
466 | } | ||
467 | |||
468 | bool ok; | ||
469 | int tranid = curritem->text( 0 ).toInt( &ok ); | ||
470 | //TranInfo *traninfo = transactions.at( tranid - 1 ); | ||
471 | TranInfo *traninfo = findTranByID( tranid ); | ||
472 | |||
473 | if ( QPEMessageBox::confirmDelete ( this, tr( "Delete transaction" ), traninfo->desc() ) ) | ||
474 | { | ||
475 | float amount = traninfo->amount(); | ||
476 | if ( traninfo->withdrawal() ) | ||
477 | { | ||
478 | amount *= -1; | ||
479 | } | ||
480 | |||
481 | transactions.remove( traninfo ); | ||
482 | delete traninfo; | ||
483 | delete curritem; | ||
484 | |||
485 | adjustBalance( amount * -1 ); | ||
486 | } | ||
487 | } | ||
488 | |||
489 | void Checkbook::slotDrawGraph() | ||
490 | { | ||
491 | } | ||