1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/***************************************************************************
kmymoneyaccountselector.cpp - description
-------------------
begin : Thu Sep 18 2003
copyright : (C) 2003 by Thomas Baumgart
email : mte@users.sourceforge.net
Javier Campos Morales <javi_c@users.sourceforge.net>
Felix Rodriguez <frodriguez@users.sourceforge.net>
John C <thetacoturtle@users.sourceforge.net>
Thomas Baumgart <ipwizard@users.sourceforge.net>
Kevin Tambascio <ktambascio@users.sourceforge.net>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// QT Includes
#include <qlayout.h>
#include <qheader.h>
#include <qlabel.h>
#include <qtimer.h>
#include <qpainter.h>
#include <qstyle.h>
#include <qrect.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <klocale.h>
#include <kpushbutton.h>
// ----------------------------------------------------------------------------
// Project Includes
#include "kmymoneyaccountselector.h"
#include <kmymoney/mymoneyutils.h>
#include <kmymoney/mymoneyfile.h>
#include <kmymoney/kmymoneylistviewitem.h>
#include <kmymoney/kmymoneychecklistitem.h>
#include "../kmymoneyutils.h"
#include "../kmymoneyglobalsettings.h"
kMyMoneyAccountSelector::kMyMoneyAccountSelector(QWidget *parent, const char *name, QWidget::WFlags flags, const bool createButtons) :
KMyMoneySelector(parent, name, flags),
m_allAccountsButton(0),
m_noAccountButton(0),
m_incomeCategoriesButton(0),
m_expenseCategoriesButton(0)
{
if(createButtons) {
QVBoxLayout* buttonLayout = new QVBoxLayout( 0, 0, 6, "accountSelectorButtonLayout");
m_allAccountsButton = new KPushButton( this, "m_allAccountsButton" );
m_allAccountsButton->setText( i18n( "All" ) );
buttonLayout->addWidget( m_allAccountsButton );
m_incomeCategoriesButton = new KPushButton( this, "m_incomeCategoriesButton" );
m_incomeCategoriesButton->setText( i18n( "Income" ) );
buttonLayout->addWidget( m_incomeCategoriesButton );
m_expenseCategoriesButton = new KPushButton( this, "m_expenseCategoriesButton" );
m_expenseCategoriesButton->setText( i18n( "Expense" ) );
buttonLayout->addWidget( m_expenseCategoriesButton );
m_noAccountButton = new KPushButton( this, "m_noAccountButton" );
m_noAccountButton->setText( i18n( "None" ) );
buttonLayout->addWidget( m_noAccountButton );
QSpacerItem* spacer = new QSpacerItem( 0, 67, QSizePolicy::Minimum, QSizePolicy::Expanding );
buttonLayout->addItem( spacer );
m_layout->addLayout( buttonLayout );
connect(m_allAccountsButton, SIGNAL(clicked()), this, SLOT(slotSelectAllAccounts()));
connect(m_noAccountButton, SIGNAL(clicked()), this, SLOT(slotDeselectAllAccounts()));
connect(m_incomeCategoriesButton, SIGNAL(clicked()), this, SLOT(slotSelectIncomeCategories()));
connect(m_expenseCategoriesButton, SIGNAL(clicked()), this, SLOT(slotSelectExpenseCategories()));
}
// sort the list of accounts in ascending order
m_listView->setSorting(0);
}
kMyMoneyAccountSelector::~kMyMoneyAccountSelector()
{
}
void kMyMoneyAccountSelector::removeButtons(void)
{
delete m_allAccountsButton;
delete m_incomeCategoriesButton;
delete m_expenseCategoriesButton;
delete m_noAccountButton;
}
void kMyMoneyAccountSelector::selectCategories(const bool income, const bool expense)
{
QListViewItem* it_v;
for(it_v = m_listView->firstChild(); it_v != 0; it_v = it_v->nextSibling()) {
if(static_cast<QCheckListItem*>(it_v)->text() == i18n("Income categories"))
selectAllSubItems(it_v, income);
else
selectAllSubItems(it_v, expense);
}
emit stateChanged();
}
void kMyMoneyAccountSelector::setSelectionMode(QListView::SelectionMode mode)
{
m_incomeCategoriesButton->setHidden(mode == QListView::Multi);
m_expenseCategoriesButton->setHidden(mode == QListView::Multi);
KMyMoneySelector::setSelectionMode(mode);
}
QStringList kMyMoneyAccountSelector::accountList(const QValueList<MyMoneyAccount::accountTypeE>& filterList) const
{
QStringList list;
QListViewItemIterator it;
QListViewItem* it_v;
QValueList<MyMoneyAccount::accountTypeE>::ConstIterator it_f;
it = QListViewItemIterator(m_listView, QListViewItemIterator::Selectable);
while((it_v = it.current()) != 0) {
{
if(it_v->rtti() == 1) {
KMyMoneyCheckListItem* it_c = dynamic_cast<KMyMoneyCheckListItem*>(it_v);
if(it_c->type() == QCheckListItem::CheckBox) {
MyMoneyAccount acc = MyMoneyFile::instance()->account(it_c->id());
it_f = filterList.find(acc.accountType());
if(filterList.count() == 0 || it_f != filterList.end())
list << it_c->id();
}
} else if(it_v->rtti() == 0) {
KMyMoneyListViewItem* it_c = dynamic_cast<KMyMoneyListViewItem*>(it_v);
MyMoneyAccount acc = MyMoneyFile::instance()->account(it_c->id());
it_f = filterList.find(acc.accountType());
if(filterList.count() == 0 || it_f != filterList.end())
list << it_c->id();
}
}
it++;
}
return list;
}
bool kMyMoneyAccountSelector::match(const QRegExp& exp, QListViewItem* item) const
{
if(!item->isSelectable())
return false;
KMyMoneyListViewItem* it_l = dynamic_cast<KMyMoneyListViewItem*>(item);
if(!it_l) {
KMyMoneyCheckListItem* it_c = dynamic_cast<KMyMoneyCheckListItem*>(item);
if(!it_c) {
return KMyMoneySelector::match(exp, item);
}
return exp.search(it_c->key(1, true)) != -1;
}
return exp.search(it_l->key(1, true)) != -1;
}
bool kMyMoneyAccountSelector::contains(const QString& txt) const
{
QListViewItemIterator it(m_listView, QListViewItemIterator::Selectable);
QListViewItem* it_v;
QString baseName = i18n("Asset") + "|" +
i18n("Liability") + "|" +
i18n("Income")+ "|" +
i18n("Expense")+ "|" +
i18n("Equity") + "|" +
i18n("Security");
while((it_v = it.current()) != 0) {
QRegExp exp(QString("^(?:%1):%2$").arg(baseName).arg(QRegExp::escape(txt)));
if(it_v->rtti() == 1) {
KMyMoneyCheckListItem* it_c = dynamic_cast<KMyMoneyCheckListItem*>(it_v);
if(exp.search(it_c->key(1, true)) != -1) {
return true;
}
} else if(it_v->rtti() == 0) {
KMyMoneyListViewItem* it_c = dynamic_cast<KMyMoneyListViewItem*>(it_v);
if(exp.search(it_c->key(1, true)) != -1) {
return true;
}
}
it++;
}
return false;
}
# if 0
void kMyMoneyAccountSelector::update(const QString& /* id */)
{
QListViewItem* it_v = m_listView->currentItem();
QString previousHighlighted;
bool state = false;
if(m_selMode == QListView::Multi && it_v) {
if(it_v->rtti() == 1) {
KMyMoneyCheckListItem* it_c = static_cast<KMyMoneyCheckListItem*>(it_v);
if(it_c->type() == QCheckListItem::CheckBox) {
previousHighlighted = it_c->id();
state = it_c->isOn();
}
}
}
QStringList list = selectedAccounts();
QStringList::Iterator it;
if(!m_typeList.isEmpty())
loadList(m_typeList);
else if(!m_baseName.isEmpty()) {
loadList(m_baseName, m_accountList);
}
// because loadList() sets all accounts selected, we have to
// clear the selection and only turn on those, that were on
// before the update.
slotDeselectAllAccounts();
for(it = list.begin(); it != list.end(); ++it) {
setSelected(*it, true);
}
if(m_selMode == QListView::Multi) {
// make the previous highlighted item highlighted again
if(!previousHighlighted.isEmpty()) {
setSelected(previousHighlighted);
}
}
}
#endif
AccountSet::AccountSet() :
m_count(0),
m_file(MyMoneyFile::instance()),
m_favorites(0),
m_hideClosedAccounts(true)
{
}
void AccountSet::addAccountGroup(MyMoneyAccount::accountTypeE group)
{
if(group == MyMoneyAccount::Asset) {
m_typeList << MyMoneyAccount::Checkings;
m_typeList << MyMoneyAccount::Savings;
m_typeList << MyMoneyAccount::Cash;
m_typeList << MyMoneyAccount::AssetLoan;
m_typeList << MyMoneyAccount::CertificateDep;
m_typeList << MyMoneyAccount::Investment;
m_typeList << MyMoneyAccount::Stock;
m_typeList << MyMoneyAccount::MoneyMarket;
m_typeList << MyMoneyAccount::Asset;
m_typeList << MyMoneyAccount::Currency;
} else if(group == MyMoneyAccount::Liability) {
m_typeList << MyMoneyAccount::CreditCard;
m_typeList << MyMoneyAccount::Loan;
m_typeList << MyMoneyAccount::Liability;
} else if(group == MyMoneyAccount::Income) {
m_typeList << MyMoneyAccount::Income;
} else if(group == MyMoneyAccount::Expense) {
m_typeList << MyMoneyAccount::Expense;
} else if(group == MyMoneyAccount::Equity) {
m_typeList << MyMoneyAccount::Equity;
}
}
void AccountSet::addAccountType(MyMoneyAccount::accountTypeE type)
{
m_typeList << type;
}
void AccountSet::removeAccountType(MyMoneyAccount::accountTypeE type)
{
QValueList<MyMoneyAccount::accountTypeE>::iterator it;
it = m_typeList.find(type);
if(it != m_typeList.end()) {
m_typeList.remove(it);
}
}
void AccountSet::clear(void)
{
m_typeList.clear();
}
int AccountSet::load(kMyMoneyAccountSelector* selector)
{
QStringList list;
QStringList::ConstIterator it_l;
int count = 0;
int typeMask = 0;
QString currentId;
if(selector->selectionMode() == QListView::Single) {
QStringList list;
selector->selectedItems(list);
if(list.count() > 0)
currentId = list.first();
}
if((m_typeList.contains(MyMoneyAccount::Checkings)
+ m_typeList.contains(MyMoneyAccount::Savings)
+ m_typeList.contains(MyMoneyAccount::Cash)
+ m_typeList.contains(MyMoneyAccount::AssetLoan)
+ m_typeList.contains(MyMoneyAccount::CertificateDep)
+ m_typeList.contains(MyMoneyAccount::Investment)
+ m_typeList.contains(MyMoneyAccount::Stock)
+ m_typeList.contains(MyMoneyAccount::MoneyMarket)
+ m_typeList.contains(MyMoneyAccount::Asset)
+ m_typeList.contains(MyMoneyAccount::Currency)) > 0)
typeMask |= KMyMoneyUtils::asset;
if((m_typeList.contains(MyMoneyAccount::CreditCard)
+ m_typeList.contains(MyMoneyAccount::Loan)
+ m_typeList.contains(MyMoneyAccount::Liability)) > 0)
typeMask |= KMyMoneyUtils::liability;
if((m_typeList.contains(MyMoneyAccount::Income)) > 0)
typeMask |= KMyMoneyUtils::income;
if((m_typeList.contains(MyMoneyAccount::Expense)) > 0)
typeMask |= KMyMoneyUtils::expense;
if((m_typeList.contains(MyMoneyAccount::Equity)) > 0)
typeMask |= KMyMoneyUtils::equity;
selector->clear();
KListView* lv = selector->listView();
m_count = 0;
QString key;
QListViewItem* after = 0;
// create the favorite section first and sort it to the beginning
key = QString("A%1").arg(i18n("Favorites"));
m_favorites = selector->newItem(i18n("Favorites"), key);
for(int mask = 0x01; mask != KMyMoneyUtils::last; mask <<= 1) {
QListViewItem* item = 0;
if((typeMask & mask & KMyMoneyUtils::asset) != 0) {
++m_count;
key = QString("B%1").arg(i18n("Asset"));
item = selector->newItem(i18n("Asset accounts"), key);
list = m_file->asset().accountList();
}
if((typeMask & mask & KMyMoneyUtils::liability) != 0) {
++m_count;
key = QString("C%1").arg(i18n("Liability"));
item = selector->newItem(i18n("Liability accounts"), key);
list = m_file->liability().accountList();
}
if((typeMask & mask & KMyMoneyUtils::income) != 0) {
++m_count;
key = QString("D%1").arg(i18n("Income"));
item = selector->newItem(i18n("Income categories"), key);
list = m_file->income().accountList();
if(selector->selectionMode() == QListView::Multi) {
selector->m_incomeCategoriesButton->show();
}
}
if((typeMask & mask & KMyMoneyUtils::expense) != 0) {
++m_count;
key = QString("E%1").arg(i18n("Expense"));
item = selector->newItem(i18n("Expense categories"), key);
list = m_file->expense().accountList();
if(selector->selectionMode() == QListView::Multi) {
selector->m_expenseCategoriesButton->show();
}
}
if((typeMask & mask & KMyMoneyUtils::equity) != 0) {
++m_count;
key = QString("F%1").arg(i18n("Equity"));
item = selector->newItem(i18n("Equity accounts"), key);
list = m_file->equity().accountList();
}
if(!after)
after = item;
if(item != 0) {
// scan all matching accounts found in the engine
for(it_l = list.begin(); it_l != list.end(); ++it_l) {
const MyMoneyAccount& acc = m_file->account(*it_l);
++m_count;
++count;
//this will include an account if it matches the account type and
//if it is still open or it has been set to show closed accounts
if(includeAccount(acc)
&& (!isHidingClosedAccounts() || !acc.isClosed()) ) {
QString tmpKey;
tmpKey = key + MyMoneyFile::AccountSeperator + acc.name();
QListViewItem* subItem = selector->newItem(item, acc.name(), tmpKey, acc.id());
if(acc.value("PreferredAccount") == "Yes"
&& m_typeList.contains(acc.accountType())) {
selector->newItem(m_favorites, acc.name(), tmpKey, acc.id());
}
if(acc.accountList().count() > 0) {
subItem->setOpen(true);
count += loadSubAccounts(selector, subItem, tmpKey, acc.accountList());
}
//disable the item if it has been added only because a subaccount matches the type
if( !m_typeList.contains(acc.accountType()) ) {
subItem->setEnabled(false);
}
}
}
item->sortChildItems(0, true);
}
}
// if we don't have a favorite account or the selector is for multi-mode
// we get rid of the favorite entry and subentries.
if(m_favorites->childCount() == 0 || selector->selectionMode() == QListView::Multi) {
delete m_favorites;
m_favorites = 0;
}
// sort the list
selector->listView()->sort();
if(lv->firstChild()) {
if(currentId.isEmpty()) {
lv->setCurrentItem(lv->firstChild());
lv->clearSelection();
} else {
selector->setSelected(currentId);
}
}
selector->update();
return count;
}
int AccountSet::load(kMyMoneyAccountSelector* selector, const QString& baseName, const QValueList<QString>& accountIdList, const bool clear)
{
int count = 0;
QListViewItem* item = 0;
m_typeList.clear();
if(clear) {
m_count = 0;
selector->clear();
}
item = selector->newItem(baseName);
++m_count;
QValueList<QString>::ConstIterator it;
for(it = accountIdList.begin(); it != accountIdList.end(); ++it) {
const MyMoneyAccount& acc = m_file->account(*it);
if(acc.isClosed())
continue;
QString tmpKey;
// the first character must be preset. Since we don't know any sort order here, we just use A
tmpKey = QString("A%1%2%3").arg(baseName, MyMoneyFile::AccountSeperator, acc.name());
selector->newItem(item, acc.name(), tmpKey, acc.id());
++m_count;
++count;
}
KListView* lv = selector->listView();
if(lv->firstChild()) {
lv->setCurrentItem(lv->firstChild());
lv->clearSelection();
}
selector->update();
return count;
}
int AccountSet::loadSubAccounts(kMyMoneyAccountSelector* selector, QListViewItem* parent, const QString& key, const QStringList& list)
{
QStringList::ConstIterator it_l;
int count = 0;
for(it_l = list.begin(); it_l != list.end(); ++it_l) {
const MyMoneyAccount& acc = m_file->account(*it_l);
// don't include stock accounts if not in expert mode
if(acc.isInvest() && !KMyMoneyGlobalSettings::expertMode())
continue;
if(includeAccount(acc)
&& !acc.isClosed()) {
QString tmpKey;
tmpKey = key + MyMoneyFile::AccountSeperator + acc.name();
++count;
++m_count;
QListViewItem* item = selector->newItem(parent, acc.name(), tmpKey, acc.id());
if(acc.value("PreferredAccount") == "Yes"
&& m_typeList.contains(acc.accountType())) {
selector->newItem(m_favorites, acc.name(), tmpKey, acc.id());
}
if(acc.accountList().count() > 0) {
item->setOpen(true);
count += loadSubAccounts(selector, item, tmpKey, acc.accountList());
}
//disable the item if it has been added only because a subaccount matches the type
if( !m_typeList.contains(acc.accountType()) ) {
item->setEnabled(false);
}
}
}
return count;
}
bool AccountSet::includeAccount(const MyMoneyAccount& acc)
{
if( m_typeList.contains(acc.accountType()) )
return true;
QStringList accounts = acc.accountList();
if(accounts.size() > 0) {
QStringList::ConstIterator it_acc;
for(it_acc = accounts.begin(); it_acc != accounts.end(); ++it_acc) {
MyMoneyAccount account = m_file->account(*it_acc);
if( includeAccount(account) )
return true;
}
}
return false;
}
#include "kmymoneyaccountselector.moc"
|