author | drw <drw> | 2002-11-02 21:45:56 (UTC) |
---|---|---|
committer | drw <drw> | 2002-11-02 21:45:56 (UTC) |
commit | 2dc81c48428222533e5479947d9ad318e464bafa (patch) (unidiff) | |
tree | 2e3570d18f3574c49ffc8aaf287bac4822ff3a11 | |
parent | 5e1893923a9ddf1e2bd3e8f9c0dd264d86b8d98d (diff) | |
download | opie-2dc81c48428222533e5479947d9ad318e464bafa.zip opie-2dc81c48428222533e5479947d9ad318e464bafa.tar.gz opie-2dc81c48428222533e5479947d9ad318e464bafa.tar.bz2 |
Implementing charting...
-rw-r--r-- | noncore/apps/checkbook/checkbook.cpp | 122 | ||||
-rw-r--r-- | noncore/apps/checkbook/checkbook.h | 11 | ||||
-rw-r--r-- | noncore/apps/checkbook/graph.cpp | 85 | ||||
-rw-r--r-- | noncore/apps/checkbook/graph.h | 9 | ||||
-rw-r--r-- | noncore/apps/checkbook/graphinfo.cpp | 45 | ||||
-rw-r--r-- | noncore/apps/checkbook/graphinfo.h | 8 |
6 files changed, 241 insertions, 39 deletions
diff --git a/noncore/apps/checkbook/checkbook.cpp b/noncore/apps/checkbook/checkbook.cpp index 20b42b5..77c1f57 100644 --- a/noncore/apps/checkbook/checkbook.cpp +++ b/noncore/apps/checkbook/checkbook.cpp | |||
@@ -211,75 +211,72 @@ QWidget *Checkbook::initTransactions() | |||
211 | QPEApplication::setStylusOperation( tranTable->viewport(), QPEApplication::RightOnHold ); | 211 | QPEApplication::setStylusOperation( tranTable->viewport(), QPEApplication::RightOnHold ); |
212 | connect( tranTable, SIGNAL( rightButtonPressed( QListViewItem *, const QPoint &, int ) ), | 212 | connect( tranTable, SIGNAL( rightButtonPressed( QListViewItem *, const QPoint &, int ) ), |
213 | this, SLOT( slotEditTran() ) ); | 213 | this, SLOT( slotEditTran() ) ); |
214 | 214 | ||
215 | QPushButton *btn = new QPushButton( Resource::loadPixmap( "new" ), tr( "New" ), control ); | 215 | QPushButton *btn = new QPushButton( Resource::loadPixmap( "new" ), tr( "New" ), control ); |
216 | QWhatsThis::add( btn, tr( "Click here to add a new transaction." ) ); | 216 | QWhatsThis::add( btn, tr( "Click here to add a new transaction." ) ); |
217 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotNewTran() ) ); | 217 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotNewTran() ) ); |
218 | layout->addWidget( btn, 2, 0 ); | 218 | layout->addWidget( btn, 2, 0 ); |
219 | 219 | ||
220 | btn = new QPushButton( Resource::loadPixmap( "edit" ), tr( "Edit" ), control ); | 220 | btn = new QPushButton( Resource::loadPixmap( "edit" ), tr( "Edit" ), control ); |
221 | QWhatsThis::add( btn, tr( "Select a transaction and then click here to edit it." ) ); | 221 | QWhatsThis::add( btn, tr( "Select a transaction and then click here to edit it." ) ); |
222 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotEditTran() ) ); | 222 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotEditTran() ) ); |
223 | layout->addWidget( btn, 2, 1 ); | 223 | layout->addWidget( btn, 2, 1 ); |
224 | 224 | ||
225 | btn = new QPushButton( Resource::loadPixmap( "trash" ), tr( "Delete" ), control ); | 225 | btn = new QPushButton( Resource::loadPixmap( "trash" ), tr( "Delete" ), control ); |
226 | QWhatsThis::add( btn, tr( "Select a checkbook and then click here to delete it." ) ); | 226 | QWhatsThis::add( btn, tr( "Select a checkbook and then click here to delete it." ) ); |
227 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotDeleteTran() ) ); | 227 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotDeleteTran() ) ); |
228 | layout->addWidget( btn, 2, 2 ); | 228 | layout->addWidget( btn, 2, 2 ); |
229 | 229 | ||
230 | return( control ); | 230 | return( control ); |
231 | } | 231 | } |
232 | 232 | ||
233 | QWidget *Checkbook::initCharts() | 233 | QWidget *Checkbook::initCharts() |
234 | { | 234 | { |
235 | graphInfo = 0x0; | ||
236 | |||
235 | QWidget *control = new QWidget( mainWidget ); | 237 | QWidget *control = new QWidget( mainWidget ); |
236 | 238 | ||
237 | QGridLayout *layout = new QGridLayout( control ); | 239 | QGridLayout *layout = new QGridLayout( control ); |
238 | layout->setSpacing( 2 ); | 240 | layout->setSpacing( 2 ); |
239 | layout->setMargin( 4 ); | 241 | layout->setMargin( 4 ); |
240 | 242 | ||
241 | /* | 243 | graphWidget = new Graph( control ); |
242 | QLabel *label = new QLabel( control ); | 244 | QWhatsThis::add( graphWidget, tr( "Select the desired chart below and then click on the Draw button." ) ); |
243 | label->setText( tr( "Graph type:" ) ); | 245 | layout->addMultiCellWidget( graphWidget, 0, 0, 0, 2 ); |
244 | layout->addWidget( label, 0, 0 ); | 246 | |
245 | graphList = new QComboBox( control ); | 247 | graphList = new QComboBox( control ); |
246 | graphList->insertItem( tr( "By category" ) ); | 248 | QWhatsThis::add( graphList, tr( "Click here to select the desired chart type." ) ); |
247 | graphList->insertItem( tr( "..." ) ); | 249 | graphList->insertItem( tr( "Account balance" ) ); |
248 | graphList->insertItem( tr( "..." ) ); | 250 | graphList->insertItem( tr( "Withdrawals by category" ) ); |
249 | layout->addWidget( graphList, 0, 1 ); | 251 | graphList->insertItem( tr( "Deposits by category" ) ); |
250 | */ | ||
251 | 252 | ||
252 | GraphInfo* info = new GraphInfo( GraphInfo::BarChart, 0x0, tr( "Graph Title" ), | 253 | layout->addMultiCellWidget( graphList, 1, 1, 0, 1 ); |
253 | tr( "X-Axis" ), tr( "Y-Axis" ) ); | ||
254 | graphWidget = new Graph( control, info ); | ||
255 | QWhatsThis::add( graphWidget, tr( "Charting is not implemented yet." ) ); | ||
256 | layout->addMultiCellWidget( graphWidget, 0, 0, 0, 1 ); | ||
257 | 254 | ||
258 | QPushButton *btn = new QPushButton( Resource::loadPixmap( "checkbook/drawbtn" ), tr( "Draw" ), control ); | 255 | QPushButton *btn = new QPushButton( Resource::loadPixmap( "checkbook/drawbtn" ), tr( "Draw" ), control ); |
259 | QWhatsThis::add( btn, tr( "Click here to draw the chart." ) ); | 256 | QWhatsThis::add( btn, tr( "Click here to draw the selected chart." ) ); |
260 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotDrawGraph() ) ); | 257 | connect( btn, SIGNAL( clicked() ), this, SLOT( slotDrawGraph() ) ); |
261 | layout->addWidget( btn, 1, 1 ); | 258 | layout->addWidget( btn, 1, 2 ); |
262 | 259 | ||
263 | return control; | 260 | return control; |
264 | } | 261 | } |
265 | 262 | ||
266 | void Checkbook::loadCheckbook() | 263 | void Checkbook::loadCheckbook() |
267 | { | 264 | { |
268 | transactions.clear(); | 265 | transactions.clear(); |
269 | 266 | ||
270 | Config config(filename, Config::File); | 267 | Config config(filename, Config::File); |
271 | 268 | ||
272 | // Load info | 269 | // Load info |
273 | config.setGroup( "Account" ); | 270 | config.setGroup( "Account" ); |
274 | nameEdit->setText( name ); | 271 | nameEdit->setText( name ); |
275 | QString temptext = config.readEntry( "Type" ); | 272 | QString temptext = config.readEntry( "Type" ); |
276 | int i = typeList->count(); | 273 | int i = typeList->count(); |
277 | while ( i > 0 ) | 274 | while ( i > 0 ) |
278 | { | 275 | { |
279 | i--; | 276 | i--; |
280 | typeList->setCurrentItem( i ); | 277 | typeList->setCurrentItem( i ); |
281 | if ( typeList->currentText() == temptext ) | 278 | if ( typeList->currentText() == temptext ) |
282 | { | 279 | { |
283 | break; | 280 | break; |
284 | } | 281 | } |
285 | } | 282 | } |
@@ -346,54 +343,52 @@ TranInfo *Checkbook::findTranByID( int id ) | |||
346 | } | 343 | } |
347 | return( traninfo ); | 344 | return( traninfo ); |
348 | } | 345 | } |
349 | 346 | ||
350 | void Checkbook::accept() | 347 | void Checkbook::accept() |
351 | { | 348 | { |
352 | QFile f( filename ); | 349 | QFile f( filename ); |
353 | if ( f.exists() ) | 350 | if ( f.exists() ) |
354 | { | 351 | { |
355 | f.remove(); | 352 | f.remove(); |
356 | } | 353 | } |
357 | 354 | ||
358 | Config *config = new Config(filename, Config::File); | 355 | Config *config = new Config(filename, Config::File); |
359 | 356 | ||
360 | // Save info | 357 | // Save info |
361 | config->setGroup( "Account" ); | 358 | config->setGroup( "Account" ); |
362 | config->writeEntry( "Type", typeList->currentText() ); | 359 | config->writeEntry( "Type", typeList->currentText() ); |
363 | config->writeEntry( "Bank", bankEdit->text() ); | 360 | config->writeEntry( "Bank", bankEdit->text() ); |
364 | config->writeEntryCrypt( "Number", acctNumEdit->text() ); | 361 | config->writeEntryCrypt( "Number", acctNumEdit->text() ); |
365 | config->writeEntryCrypt( "PINNumber", pinNumEdit->text() ); | 362 | config->writeEntryCrypt( "PINNumber", pinNumEdit->text() ); |
366 | config->writeEntry( "Balance", balanceEdit->text() ); | 363 | config->writeEntry( "Balance", balanceEdit->text() ); |
367 | config->writeEntry( "Notes", notesEdit->text() ); | 364 | config->writeEntry( "Notes", notesEdit->text() ); |
368 | 365 | ||
369 | // Save transactions | 366 | // Save transactions |
370 | TranInfo *tran = transactions.first(); | ||
371 | int i = 1; | 367 | int i = 1; |
372 | while ( tran ) | 368 | for ( TranInfo *tran = transactions.first(); tran; tran = transactions.next() ) |
373 | { | 369 | { |
374 | tran->write( config, i ); | 370 | tran->write( config, i ); |
375 | tran = transactions.next(); | ||
376 | i++; | 371 | i++; |
377 | } | 372 | } |
378 | config->write(); | 373 | config->write(); |
379 | 374 | ||
380 | QDialog::accept(); | 375 | QDialog::accept(); |
381 | } | 376 | } |
382 | 377 | ||
383 | void Checkbook::slotNameChanged( const QString &newname ) | 378 | void Checkbook::slotNameChanged( const QString &newname ) |
384 | { | 379 | { |
385 | name = newname; | 380 | name = newname; |
386 | filename = filedir; | 381 | filename = filedir; |
387 | filename.append( newname ); | 382 | filename.append( newname ); |
388 | filename.append( ".qcb" ); | 383 | filename.append( ".qcb" ); |
389 | QString tempstr = name; | 384 | QString tempstr = name; |
390 | tempstr.append( " - " ); | 385 | tempstr.append( " - " ); |
391 | tempstr.append( tr( "Checkbook" ) ); | 386 | tempstr.append( tr( "Checkbook" ) ); |
392 | setCaption( tempstr ); | 387 | setCaption( tempstr ); |
393 | } | 388 | } |
394 | 389 | ||
395 | void Checkbook::slotStartingBalanceChanged( const QString &newbalance ) | 390 | void Checkbook::slotStartingBalanceChanged( const QString &newbalance ) |
396 | { | 391 | { |
397 | currBalance -= startBalance; | 392 | currBalance -= startBalance; |
398 | bool ok; | 393 | bool ok; |
399 | startBalance = newbalance.toFloat( &ok ); | 394 | startBalance = newbalance.toFloat( &ok ); |
@@ -488,25 +483,114 @@ void Checkbook::slotDeleteTran() | |||
488 | } | 483 | } |
489 | 484 | ||
490 | bool ok; | 485 | bool ok; |
491 | int tranid = curritem->text( 0 ).toInt( &ok ); | 486 | int tranid = curritem->text( 0 ).toInt( &ok ); |
492 | TranInfo *traninfo = findTranByID( tranid ); | 487 | TranInfo *traninfo = findTranByID( tranid ); |
493 | 488 | ||
494 | if ( QPEMessageBox::confirmDelete ( this, tr( "Delete transaction" ), traninfo->desc() ) ) | 489 | if ( QPEMessageBox::confirmDelete ( this, tr( "Delete transaction" ), traninfo->desc() ) ) |
495 | { | 490 | { |
496 | float amount = traninfo->amount(); | 491 | float amount = traninfo->amount(); |
497 | if ( traninfo->withdrawal() ) | 492 | if ( traninfo->withdrawal() ) |
498 | { | 493 | { |
499 | amount *= -1; | 494 | amount *= -1; |
500 | } | 495 | } |
501 | 496 | ||
502 | transactions.remove( traninfo ); | 497 | transactions.remove( traninfo ); |
503 | delete traninfo; | 498 | delete traninfo; |
504 | delete curritem; | 499 | delete curritem; |
505 | 500 | ||
506 | adjustBalance( amount * -1 ); | 501 | adjustBalance( amount * -1 ); |
507 | } | 502 | } |
508 | } | 503 | } |
509 | 504 | ||
510 | void Checkbook::slotDrawGraph() | 505 | void Checkbook::slotDrawGraph() |
511 | { | 506 | { |
507 | if ( graphInfo ) | ||
508 | { | ||
509 | delete graphInfo; | ||
510 | } | ||
511 | |||
512 | switch ( graphList->currentItem() ) | ||
513 | { | ||
514 | case 0 : drawBalanceChart(); | ||
515 | break; | ||
516 | case 1 : drawCategoryChart( TRUE ); | ||
517 | break; | ||
518 | case 2 : drawCategoryChart( FALSE ); | ||
519 | break; | ||
520 | }; | ||
521 | |||
522 | graphWidget->setGraphInfo( graphInfo ); | ||
523 | graphWidget->drawGraph( TRUE ); | ||
524 | } | ||
525 | |||
526 | void Checkbook::drawBalanceChart() | ||
527 | { | ||
528 | DataPointList *list = new DataPointList(); | ||
529 | |||
530 | float balance = startBalance; | ||
531 | float amount; | ||
532 | QString label; | ||
533 | int i = 0; | ||
534 | int count = transactions.count(); | ||
535 | |||
536 | for ( TranInfo *tran = transactions.first(); tran; tran = transactions.next() ) | ||
537 | { | ||
538 | i++; | ||
539 | balance -= tran->fee(); | ||
540 | amount = tran->amount(); | ||
541 | if ( tran->withdrawal() ) | ||
542 | { | ||
543 | amount *= -1; | ||
544 | } | ||
545 | balance += amount; | ||
546 | if ( i == 1 || i == count / 2 || i == count ) | ||
547 | { | ||
548 | label = tran->datestr(); | ||
549 | } | ||
550 | else | ||
551 | { | ||
552 | label = ""; | ||
553 | } | ||
554 | list->append( new DataPointInfo( label, balance ) ); | ||
555 | } | ||
556 | |||
557 | graphInfo = new GraphInfo( GraphInfo::BarChart, list ); | ||
558 | } | ||
559 | |||
560 | void Checkbook::drawCategoryChart( bool withdrawals ) | ||
561 | { | ||
562 | DataPointList *list = new DataPointList(); | ||
563 | |||
564 | TranInfo *tran = transactions.first(); | ||
565 | if ( tran->withdrawal() == withdrawals ) | ||
566 | { | ||
567 | list->append( new DataPointInfo( tran->category(), tran->amount() ) ); | ||
568 | } | ||
569 | tran = transactions.next(); | ||
570 | |||
571 | DataPointInfo *cat; | ||
572 | for ( ; tran; tran = transactions.next() ) | ||
573 | { | ||
574 | if ( tran->withdrawal() == withdrawals ) | ||
575 | { | ||
576 | // Find category in list | ||
577 | for ( cat = list->first(); cat; cat = list->next() ) | ||
578 | { | ||
579 | if ( cat->label() == tran->category() ) | ||
580 | { | ||
581 | break; | ||
582 | } | ||
583 | } | ||
584 | if ( cat && cat->label() == tran->category() ) | ||
585 | { // Found category, add to transaction to category total | ||
586 | cat->addToValue( tran->amount() ); | ||
587 | } | ||
588 | else | ||
589 | { // Didn't find category, add category to list | ||
590 | list->append( new DataPointInfo( tran->category(), tran->amount() ) ); | ||
591 | } | ||
592 | } | ||
593 | } | ||
594 | |||
595 | graphInfo = new GraphInfo( GraphInfo::PieChart, list ); | ||
512 | } | 596 | } |
diff --git a/noncore/apps/checkbook/checkbook.h b/noncore/apps/checkbook/checkbook.h index 01f1115..287788a 100644 --- a/noncore/apps/checkbook/checkbook.h +++ b/noncore/apps/checkbook/checkbook.h | |||
@@ -15,89 +15,94 @@ | |||
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General |
18 | ..}^=.= = ; Public License for more details. | 18 | ..}^=.= = ; Public License for more details. |
19 | ++= -. .` .: | 19 | ++= -. .` .: |
20 | : = ...= . :.=- You should have received a copy of the GNU | 20 | : = ...= . :.=- You should have received a copy of the GNU |
21 | -. .:....=;==+<; General Public License along with this file; | 21 | -. .:....=;==+<; General Public License along with this file; |
22 | -_. . . )=. = see the file COPYING. If not, write to the | 22 | -_. . . )=. = see the file COPYING. If not, write to the |
23 | -- :-=` Free Software Foundation, Inc., | 23 | -- :-=` Free Software Foundation, Inc., |
24 | 59 Temple Place - Suite 330, | 24 | 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifndef CHECKBOOK_H | 29 | #ifndef CHECKBOOK_H |
30 | #define CHECKBOOK_H | 30 | #define CHECKBOOK_H |
31 | 31 | ||
32 | #include "traninfo.h" | 32 | #include "traninfo.h" |
33 | 33 | ||
34 | #include <qdialog.h> | 34 | #include <qdialog.h> |
35 | 35 | ||
36 | class OTabWidget; | 36 | class OTabWidget; |
37 | 37 | ||
38 | class Graph; | 38 | class Graph; |
39 | class GraphInfo; | ||
39 | class QComboBox; | 40 | class QComboBox; |
40 | class QLabel; | 41 | class QLabel; |
41 | class QLineEdit; | 42 | class QLineEdit; |
42 | class QListView; | 43 | class QListView; |
43 | class QMultiLineEdit; | 44 | class QMultiLineEdit; |
44 | class QString; | 45 | class QString; |
45 | 46 | ||
46 | class Checkbook : public QDialog | 47 | class Checkbook : public QDialog |
47 | { | 48 | { |
48 | Q_OBJECT | 49 | Q_OBJECT |
49 | 50 | ||
50 | public: | 51 | public: |
51 | Checkbook( QWidget * = 0x0, const QString & = 0x0, const QString & = 0x0, char = '$' ); | 52 | Checkbook( QWidget * = 0x0, const QString & = 0x0, const QString & = 0x0, char = '$' ); |
52 | ~Checkbook(); | 53 | ~Checkbook(); |
53 | 54 | ||
54 | const QString &getName(); | 55 | const QString &getName(); |
55 | 56 | ||
56 | private: | 57 | private: |
57 | TranInfoList transactions; | 58 | TranInfoList transactions; |
58 | QString name; | 59 | QString name; |
59 | QString filename; | 60 | QString filename; |
60 | QString filedir; | 61 | QString filedir; |
61 | char currencySymbol; | 62 | char currencySymbol; |
62 | int highTranNum; | 63 | int highTranNum; |
63 | 64 | ||
64 | OTabWidget *mainWidget; | 65 | OTabWidget *mainWidget; |
65 | void loadCheckbook(); | 66 | void loadCheckbook(); |
66 | void adjustBalance( float ); | 67 | void adjustBalance( float ); |
67 | TranInfo *findTranByID( int ); | 68 | TranInfo *findTranByID( int ); |
68 | 69 | ||
69 | // Info tab | 70 | // Info tab |
70 | QWidget *initInfo(); | 71 | QWidget *initInfo(); |
71 | QLineEdit *nameEdit; | 72 | QLineEdit *nameEdit; |
72 | QComboBox *typeList; | 73 | QComboBox *typeList; |
73 | QLineEdit *bankEdit; | 74 | QLineEdit *bankEdit; |
74 | QLineEdit *acctNumEdit; | 75 | QLineEdit *acctNumEdit; |
75 | QLineEdit *pinNumEdit; | 76 | QLineEdit *pinNumEdit; |
76 | QLineEdit *balanceEdit; | 77 | QLineEdit *balanceEdit; |
77 | QMultiLineEdit *notesEdit; | 78 | QMultiLineEdit *notesEdit; |
78 | float startBalance; | 79 | float startBalance; |
79 | 80 | ||
80 | // Transactions tab | 81 | // Transactions tab |
81 | QWidget *initTransactions(); | 82 | QWidget *initTransactions(); |
82 | QListView *tranTable; | 83 | QListView *tranTable; |
83 | QLabel *balanceLabel; | 84 | QLabel *balanceLabel; |
84 | float currBalance; | 85 | float currBalance; |
85 | 86 | ||
86 | // Charts tab | 87 | // Charts tab |
87 | QWidget *initCharts(); | 88 | QWidget *initCharts(); |
88 | //QComboBox *graphList; | 89 | GraphInfo *graphInfo; |
89 | Graph *graphWidget; | 90 | QComboBox *graphList; |
91 | Graph *graphWidget; | ||
92 | |||
93 | void drawBalanceChart(); | ||
94 | void drawCategoryChart( bool = TRUE ); | ||
90 | 95 | ||
91 | protected slots: | 96 | protected slots: |
92 | void accept(); | 97 | void accept(); |
93 | 98 | ||
94 | private slots: | 99 | private slots: |
95 | void slotNameChanged( const QString & ); | 100 | void slotNameChanged( const QString & ); |
96 | void slotStartingBalanceChanged( const QString & ); | 101 | void slotStartingBalanceChanged( const QString & ); |
97 | void slotNewTran(); | 102 | void slotNewTran(); |
98 | void slotEditTran(); | 103 | void slotEditTran(); |
99 | void slotDeleteTran(); | 104 | void slotDeleteTran(); |
100 | void slotDrawGraph(); | 105 | void slotDrawGraph(); |
101 | }; | 106 | }; |
102 | 107 | ||
103 | #endif | 108 | #endif |
diff --git a/noncore/apps/checkbook/graph.cpp b/noncore/apps/checkbook/graph.cpp index bae92da..a0d8b78 100644 --- a/noncore/apps/checkbook/graph.cpp +++ b/noncore/apps/checkbook/graph.cpp | |||
@@ -8,105 +8,172 @@ | |||
8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public | 8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public |
9 | .="- .-=="i, .._ License as published by the Free Software | 9 | .="- .-=="i, .._ License as published by the Free Software |
10 | - . .-<_> .<> Foundation; either version 2 of the License, | 10 | - . .-<_> .<> Foundation; either version 2 of the License, |
11 | ._= =} : or (at your option) any later version. | 11 | ._= =} : or (at your option) any later version. |
12 | .%`+i> _;_. | 12 | .%`+i> _;_. |
13 | .i_,=:_. -<s. This file is distributed in the hope that | 13 | .i_,=:_. -<s. This file is distributed in the hope that |
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General |
18 | ..}^=.= = ; Public License for more details. | 18 | ..}^=.= = ; Public License for more details. |
19 | ++= -. .` .: | 19 | ++= -. .` .: |
20 | : = ...= . :.=- You should have received a copy of the GNU | 20 | : = ...= . :.=- You should have received a copy of the GNU |
21 | -. .:....=;==+<; General Public License along with this file; | 21 | -. .:....=;==+<; General Public License along with this file; |
22 | -_. . . )=. = see the file COPYING. If not, write to the | 22 | -_. . . )=. = see the file COPYING. If not, write to the |
23 | -- :-=` Free Software Foundation, Inc., | 23 | -- :-=` Free Software Foundation, Inc., |
24 | 59 Temple Place - Suite 330, | 24 | 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #include "graph.h" | 29 | #include "graph.h" |
30 | #include "graphinfo.h" | 30 | #include "graphinfo.h" |
31 | 31 | ||
32 | #include <qcolor.h> | ||
33 | #include <qfontmetrics.h> | ||
32 | #include <qpainter.h> | 34 | #include <qpainter.h> |
33 | 35 | ||
34 | Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags ) | 36 | Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags ) |
35 | : QWidget( parent, name, flags ) | 37 | : QWidget( parent, name, flags ) |
36 | { | 38 | { |
37 | setBackgroundMode( QWidget::PaletteBase ); | ||
38 | |||
39 | data = d; | 39 | data = d; |
40 | 40 | ||
41 | graph.setOptimization( QPixmap::BestOptim ); | 41 | graph.setOptimization( QPixmap::BestOptim ); |
42 | } | 42 | } |
43 | 43 | ||
44 | void Graph::setGraphInfo( GraphInfo *d ) | 44 | void Graph::setGraphInfo( GraphInfo *d ) |
45 | { | 45 | { |
46 | data = d; | 46 | data = d; |
47 | } | 47 | } |
48 | 48 | ||
49 | void Graph::drawGraph( bool regen ) | 49 | void Graph::drawGraph( bool regen ) |
50 | { | 50 | { |
51 | if ( regen ) | 51 | if ( regen ) |
52 | { | 52 | { |
53 | initGraph(); | 53 | initGraph(); |
54 | } | 54 | } |
55 | QPainter p( this ); | 55 | QPainter p( this ); |
56 | p.drawPixmap( 0, 0, graph ); | 56 | p.drawPixmap( 0, 0, graph ); |
57 | } | 57 | } |
58 | 58 | ||
59 | void Graph::paintEvent( QPaintEvent * ) | 59 | void Graph::paintEvent( QPaintEvent * ) |
60 | { | 60 | { |
61 | drawGraph( FALSE ); | 61 | drawGraph( FALSE ); |
62 | } | 62 | } |
63 | 63 | ||
64 | void Graph::resizeEvent( QResizeEvent * ) | 64 | void Graph::resizeEvent( QResizeEvent * ) |
65 | { | 65 | { |
66 | drawGraph( TRUE ); | 66 | drawGraph( TRUE ); |
67 | } | 67 | } |
68 | 68 | ||
69 | void Graph::initGraph() | 69 | void Graph::initGraph() |
70 | { | 70 | { |
71 | graph.resize( width(), height() ); | 71 | graph.resize( width(), height() ); |
72 | graph.fill( QColor( 255, 255, 255 ) ); | 72 | graph.fill( QColor( 255, 255, 255 ) ); |
73 | 73 | ||
74 | if ( !data ) | 74 | if ( !data ) |
75 | { | 75 | { |
76 | return; | 76 | return; |
77 | } | 77 | } |
78 | 78 | ||
79 | // Any common stuff here (titles, ???) | 79 | // Any common stuff here (titles, ???) |
80 | 80 | ||
81 | switch ( data->graphType() ) | 81 | switch ( data->graphType() ) |
82 | { | 82 | { |
83 | case GraphInfo::BarChart : | 83 | case GraphInfo::BarChart : |
84 | { | 84 | { |
85 | drawBarChart(); | 85 | drawBarChart( width(), height(), data->maxValue() ); |
86 | } | 86 | } |
87 | break; | 87 | break; |
88 | case GraphInfo::LineChart : | 88 | case GraphInfo::LineChart : |
89 | { | 89 | { |
90 | drawLineChart(); | 90 | //drawLineChart( p, s, min, max ); |
91 | } | 91 | } |
92 | break; | 92 | break; |
93 | case GraphInfo::PieChart : | 93 | case GraphInfo::PieChart : |
94 | { | 94 | { |
95 | drawPieChart(); | 95 | drawPieChart( width(), height(), data->totalValue() ); |
96 | } | 96 | } |
97 | }; | 97 | }; |
98 | } | 98 | } |
99 | 99 | ||
100 | void Graph::drawBarChart() | 100 | void Graph::drawBarChart( int width, int height, float max ) |
101 | { | 101 | { |
102 | //Find max value in GraphInfo->dataPoints() - make function in GraphInfo!!! | 102 | QPainter p( &graph ); |
103 | |||
104 | // Try to set the font size smaller for text | ||
105 | QFont f = font(); | ||
106 | f.setPointSize( 8 ); | ||
107 | p.setFont( f ); | ||
108 | |||
109 | int x = 0; | ||
110 | int i = 0; | ||
111 | int n = data->numberDataPoints(); | ||
112 | QFontMetrics fm=fontMetrics(); | ||
113 | int fh = fm.height(); | ||
114 | int fw; | ||
115 | |||
116 | QColor c( 0, 0, 255); | ||
117 | p.setBrush( c ); | ||
118 | |||
119 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) | ||
120 | { | ||
121 | int bw = ( width - width / 4 - x ) / ( n - i ); | ||
122 | int bh = int( ( height - height / 4 - 1 ) * dp->value() / max ); | ||
123 | p.drawRect( width / 8 + x, height - height / 8 - 1 - bh, bw, bh ); | ||
124 | fw = fm.width( dp->label() ); | ||
125 | p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8, fw, | ||
126 | fh + height / 8, AlignTop | AlignHCenter, dp->label() ); | ||
127 | // WordBreak | AlignTop | AlignHCenter, dp->label() ); | ||
128 | i++; | ||
129 | x += bw; | ||
130 | } | ||
103 | } | 131 | } |
104 | 132 | ||
105 | void Graph::drawLineChart() | 133 | void Graph::drawLineChart( int width, int height, float max ) |
106 | { | 134 | { |
107 | } | 135 | } |
108 | 136 | ||
109 | void Graph::drawPieChart() | 137 | void Graph::drawPieChart( int width, int height, float sum ) |
110 | { | 138 | { |
139 | QPainter p( &graph ); | ||
140 | |||
141 | // Try to set the font size smaller for text | ||
142 | QFont f = font(); | ||
143 | f.setPointSize( 8 ); | ||
144 | p.setFont( f ); | ||
145 | |||
146 | int n = data->numberDataPoints(); | ||
147 | |||
148 | int apos = -90 * 16; | ||
149 | |||
150 | int xd = width - width / 5; | ||
151 | int yd = height - height / 5; | ||
152 | |||
153 | int i = 0; | ||
154 | |||
155 | QColor c; | ||
156 | |||
157 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) | ||
158 | { | ||
159 | c.setHsv( ( i *255) / n, 255, 255 ); | ||
160 | p.setBrush( c ); | ||
161 | |||
162 | int a = int( ( dp->value() * 360.0 ) / sum * 16.0 + 0.5 ); | ||
163 | p.drawPie( width/10, height/10, xd, yd, -apos, -a ); | ||
164 | apos += a; | ||
165 | i++; | ||
166 | } | ||
167 | |||
168 | double apos2 = -90 * 3.14159 / 180; | ||
169 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) | ||
170 | { | ||
171 | double a = dp->value() *360 / sum * 3.14159 / 180; | ||
172 | int x = int( cos( apos2 + a/2 ) * width * 5/16 + width/2 + 0.5 ); | ||
173 | int y = int( sin( apos2 + a/2 ) * height * 5/16 + height/2 + 0.5 ); | ||
174 | p.drawText( x - width/8, y - height/8, width/4, height/4, WordBreak | AlignCenter, | ||
175 | dp->label() ); | ||
176 | apos2 += a; | ||
177 | } | ||
111 | } | 178 | } |
112 | 179 | ||
diff --git a/noncore/apps/checkbook/graph.h b/noncore/apps/checkbook/graph.h index 7379be7..40b23cd 100644 --- a/noncore/apps/checkbook/graph.h +++ b/noncore/apps/checkbook/graph.h | |||
@@ -12,52 +12,53 @@ | |||
12 | .%`+i> _;_. | 12 | .%`+i> _;_. |
13 | .i_,=:_. -<s. This file is distributed in the hope that | 13 | .i_,=:_. -<s. This file is distributed in the hope that |
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General |
18 | ..}^=.= = ; Public License for more details. | 18 | ..}^=.= = ; Public License for more details. |
19 | ++= -. .` .: | 19 | ++= -. .` .: |
20 | : = ...= . :.=- You should have received a copy of the GNU | 20 | : = ...= . :.=- You should have received a copy of the GNU |
21 | -. .:....=;==+<; General Public License along with this file; | 21 | -. .:....=;==+<; General Public License along with this file; |
22 | -_. . . )=. = see the file COPYING. If not, write to the | 22 | -_. . . )=. = see the file COPYING. If not, write to the |
23 | -- :-=` Free Software Foundation, Inc., | 23 | -- :-=` Free Software Foundation, Inc., |
24 | 59 Temple Place - Suite 330, | 24 | 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifndef GRAPH_H | 29 | #ifndef GRAPH_H |
30 | #define GRAPH_H | 30 | #define GRAPH_H |
31 | 31 | ||
32 | #include <qpixmap.h> | 32 | #include <qpixmap.h> |
33 | #include <qwidget.h> | 33 | #include <qwidget.h> |
34 | 34 | ||
35 | class GraphInfo; | 35 | class GraphInfo; |
36 | class QPainter; | ||
36 | 37 | ||
37 | class Graph : public QWidget | 38 | class Graph : public QWidget |
38 | { | 39 | { |
39 | Q_OBJECT | 40 | Q_OBJECT |
40 | 41 | ||
41 | public: | 42 | public: |
42 | Graph( QWidget * = 0x0, GraphInfo * = 0x0, const QString & = 0x0, int = 0 ); | 43 | Graph( QWidget * = 0x0, GraphInfo * = 0x0, const QString & = 0x0, int = 0 ); |
43 | 44 | ||
44 | void setGraphInfo( GraphInfo * ); | 45 | void setGraphInfo( GraphInfo * ); |
45 | 46 | ||
46 | void drawGraph( bool = FALSE ); | 47 | void drawGraph( bool = FALSE ); |
47 | 48 | ||
48 | protected: | 49 | protected: |
49 | void paintEvent( QPaintEvent * ); | 50 | void paintEvent( QPaintEvent * ); |
50 | void resizeEvent( QResizeEvent * ); | 51 | void resizeEvent( QResizeEvent * ); |
51 | 52 | ||
52 | private: | 53 | private: |
53 | GraphInfo *data; | 54 | GraphInfo *data; |
54 | 55 | ||
55 | QPixmap graph; | 56 | QPixmap graph; |
56 | 57 | ||
57 | void initGraph(); | 58 | void initGraph(); |
58 | void drawBarChart(); | 59 | void drawBarChart( int, int, float ); |
59 | void drawLineChart(); | 60 | void drawLineChart( int, int, float ); |
60 | void drawPieChart(); | 61 | void drawPieChart( int, int, float ); |
61 | }; | 62 | }; |
62 | 63 | ||
63 | #endif | 64 | #endif |
diff --git a/noncore/apps/checkbook/graphinfo.cpp b/noncore/apps/checkbook/graphinfo.cpp index 7b06bdb..ec6a465 100644 --- a/noncore/apps/checkbook/graphinfo.cpp +++ b/noncore/apps/checkbook/graphinfo.cpp | |||
@@ -17,68 +17,107 @@ | |||
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General |
18 | ..}^=.= = ; Public License for more details. | 18 | ..}^=.= = ; Public License for more details. |
19 | ++= -. .` .: | 19 | ++= -. .` .: |
20 | : = ...= . :.=- You should have received a copy of the GNU | 20 | : = ...= . :.=- You should have received a copy of the GNU |
21 | -. .:....=;==+<; General Public License along with this file; | 21 | -. .:....=;==+<; General Public License along with this file; |
22 | -_. . . )=. = see the file COPYING. If not, write to the | 22 | -_. . . )=. = see the file COPYING. If not, write to the |
23 | -- :-=` Free Software Foundation, Inc., | 23 | -- :-=` Free Software Foundation, Inc., |
24 | 59 Temple Place - Suite 330, | 24 | 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #include "graphinfo.h" | 29 | #include "graphinfo.h" |
30 | 30 | ||
31 | GraphInfo::GraphInfo( GraphType type, DataPointList *data, const QString &title, | 31 | GraphInfo::GraphInfo( GraphType type, DataPointList *data, const QString &title, |
32 | const QString &xtitle, const QString &ytitle ) | 32 | const QString &xtitle, const QString &ytitle ) |
33 | { | 33 | { |
34 | t = type; | 34 | t = type; |
35 | d = data; | 35 | d = data; |
36 | gt = title; | 36 | gt = title; |
37 | xt = xtitle; | 37 | xt = xtitle; |
38 | yt = ytitle; | 38 | yt = ytitle; |
39 | } | 39 | } |
40 | 40 | ||
41 | GraphInfo::~GraphInfo() | ||
42 | { | ||
43 | if ( d ) | ||
44 | { | ||
45 | for ( DataPointInfo *data = d->first(); data; data = d->next() ) | ||
46 | { | ||
47 | delete data; | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
41 | GraphInfo::GraphType GraphInfo::graphType() | 52 | GraphInfo::GraphType GraphInfo::graphType() |
42 | { | 53 | { |
43 | return t; | 54 | return t; |
44 | } | 55 | } |
45 | 56 | ||
46 | void GraphInfo::setGraphType( GraphType type ) | 57 | void GraphInfo::setGraphType( GraphType type ) |
47 | { | 58 | { |
48 | t = type; | 59 | t = type; |
49 | } | 60 | } |
50 | 61 | ||
51 | DataPointList *GraphInfo::dataPoints() | 62 | DataPointList *GraphInfo::dataPoints() |
52 | { | 63 | { |
53 | return d; | 64 | return d; |
54 | } | 65 | } |
55 | 66 | ||
56 | void GraphInfo::setDataPoints( DataPointList *data ) | 67 | void GraphInfo::setDataPoints( DataPointList *data ) |
57 | { | 68 | { |
58 | d = data; | 69 | d = data; |
59 | } | 70 | } |
60 | 71 | ||
61 | float GraphInfo::maxValue() | 72 | DataPointInfo *GraphInfo::firstDataPoint() |
62 | { | 73 | { |
63 | float max; | 74 | return( d->first() ); |
75 | } | ||
64 | 76 | ||
77 | DataPointInfo *GraphInfo::nextDataPoint() | ||
78 | { | ||
79 | return( d->next() ); | ||
80 | } | ||
81 | |||
82 | int GraphInfo::numberDataPoints() | ||
83 | { | ||
84 | return( d->count() ); | ||
85 | } | ||
86 | |||
87 | float GraphInfo::maxValue() | ||
88 | { | ||
89 | float max = 0.0; | ||
90 | for ( DataPointInfo *data = d->first(); data; data = d->next() ) | ||
91 | { | ||
92 | if ( data->value() > max ) | ||
93 | { | ||
94 | max = data->value(); | ||
95 | } | ||
96 | } | ||
97 | return max; | ||
65 | } | 98 | } |
66 | 99 | ||
67 | float GraphInfo::minValue() | 100 | float GraphInfo::totalValue() |
68 | { | 101 | { |
102 | float sum = 0.0; | ||
103 | for ( DataPointInfo *data = d->first(); data; data = d->next() ) | ||
104 | { | ||
105 | sum += data->value(); | ||
106 | } | ||
107 | return sum; | ||
69 | } | 108 | } |
70 | 109 | ||
71 | void GraphInfo::setGraphTitle( const QString &title ) | 110 | void GraphInfo::setGraphTitle( const QString &title ) |
72 | { | 111 | { |
73 | gt = title; | 112 | gt = title; |
74 | } | 113 | } |
75 | 114 | ||
76 | void GraphInfo::setXAxisTitle( const QString &xtitle ) | 115 | void GraphInfo::setXAxisTitle( const QString &xtitle ) |
77 | { | 116 | { |
78 | xt = xtitle; | 117 | xt = xtitle; |
79 | } | 118 | } |
80 | 119 | ||
81 | void GraphInfo::setYAxisTitle( const QString &ytitle ) | 120 | void GraphInfo::setYAxisTitle( const QString &ytitle ) |
82 | { | 121 | { |
83 | yt = ytitle; | 122 | yt = ytitle; |
84 | } | 123 | } |
diff --git a/noncore/apps/checkbook/graphinfo.h b/noncore/apps/checkbook/graphinfo.h index 4ad1dc9..620da74 100644 --- a/noncore/apps/checkbook/graphinfo.h +++ b/noncore/apps/checkbook/graphinfo.h | |||
@@ -21,62 +21,68 @@ | |||
21 | -. .:....=;==+<; General Public License along with this file; | 21 | -. .:....=;==+<; General Public License along with this file; |
22 | -_. . . )=. = see the file COPYING. If not, write to the | 22 | -_. . . )=. = see the file COPYING. If not, write to the |
23 | -- :-=` Free Software Foundation, Inc., | 23 | -- :-=` Free Software Foundation, Inc., |
24 | 59 Temple Place - Suite 330, | 24 | 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifndef GRAPHINFO_H | 29 | #ifndef GRAPHINFO_H |
30 | #define GRAPHINFO_H | 30 | #define GRAPHINFO_H |
31 | 31 | ||
32 | #include <qlist.h> | 32 | #include <qlist.h> |
33 | #include <qstringlist.h> | 33 | #include <qstringlist.h> |
34 | 34 | ||
35 | class DataPointInfo | 35 | class DataPointInfo |
36 | { | 36 | { |
37 | public: | 37 | public: |
38 | DataPointInfo() | 38 | DataPointInfo() |
39 | : l( 0x0 ), v( 0.0 ) {} | 39 | : l( 0x0 ), v( 0.0 ) {} |
40 | DataPointInfo( const QString &label, float value ) | 40 | DataPointInfo( const QString &label, float value ) |
41 | : l( label ), v( value ) {} | 41 | : l( label ), v( value ) {} |
42 | 42 | ||
43 | const QString &label() { return l; } | 43 | const QString &label() { return l; } |
44 | float value() { return v; } | 44 | float value() { return v; } |
45 | |||
46 | void addToValue( float value ) { v += value; } | ||
45 | 47 | ||
46 | private: | 48 | private: |
47 | QString l; | 49 | QString l; |
48 | float v; | 50 | float v; |
49 | }; | 51 | }; |
50 | 52 | ||
51 | typedef QList<DataPointInfo> DataPointList; | 53 | typedef QList<DataPointInfo> DataPointList; |
52 | 54 | ||
53 | class GraphInfo | 55 | class GraphInfo |
54 | { | 56 | { |
55 | public: | 57 | public: |
56 | enum GraphType { BarChart, LineChart, PieChart }; | 58 | enum GraphType { BarChart, LineChart, PieChart }; |
57 | 59 | ||
58 | GraphInfo( GraphType = BarChart, DataPointList * = 0x0, | 60 | GraphInfo( GraphType = BarChart, DataPointList * = 0x0, |
59 | const QString & = 0x0, const QString & = 0x0, const QString & = 0x0 ); | 61 | const QString & = 0x0, const QString & = 0x0, const QString & = 0x0 ); |
62 | ~GraphInfo(); | ||
60 | 63 | ||
61 | GraphInfo::GraphType graphType(); | 64 | GraphInfo::GraphType graphType(); |
62 | void setGraphType( GraphType ); | 65 | void setGraphType( GraphType ); |
63 | 66 | ||
64 | DataPointList *dataPoints(); | 67 | DataPointList *dataPoints(); |
65 | void setDataPoints( DataPointList * ); | 68 | void setDataPoints( DataPointList * ); |
69 | DataPointInfo *firstDataPoint(); | ||
70 | DataPointInfo *nextDataPoint(); | ||
71 | int numberDataPoints(); | ||
66 | 72 | ||
67 | float maxValue(); | 73 | float maxValue(); |
68 | float minValue(); | 74 | float totalValue(); |
69 | 75 | ||
70 | void setGraphTitle( const QString & ); | 76 | void setGraphTitle( const QString & ); |
71 | void setXAxisTitle( const QString & ); | 77 | void setXAxisTitle( const QString & ); |
72 | void setYAxisTitle( const QString & ); | 78 | void setYAxisTitle( const QString & ); |
73 | 79 | ||
74 | private: | 80 | private: |
75 | GraphType t; | 81 | GraphType t; |
76 | DataPointList *d; | 82 | DataPointList *d; |
77 | QString gt; | 83 | QString gt; |
78 | QString xt; | 84 | QString xt; |
79 | QString yt; | 85 | QString yt; |
80 | }; | 86 | }; |
81 | 87 | ||
82 | #endif | 88 | #endif |