summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/kmymoneyaccountcombo.cpp
blob: 6865c291358f675e1f322685c2ae613b0f346ab8 (plain)
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
/***************************************************************************
                         kmymoneyaccountbutton  -  description
                            -------------------
   begin                : Mon May 31 2004
   copyright            : (C) 2000-2004 by Michael Edwardes
   email                : [email protected]
                          Javier Campos Morales <[email protected]>
                          Felix Rodriguez <[email protected]>
                          John C <[email protected]>
                          Thomas Baumgart <[email protected]>
                          Kevin Tambascio <[email protected]>
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <tqdrawutil.h>
#include <tqpainter.h>
#include <tqstyle.h>
#include <tqapplication.h>

// ----------------------------------------------------------------------------
// KDE Includes

// ----------------------------------------------------------------------------
// Project Includes

#include <kmymoney/mymoneyfile.h>
#include <kmymoney/kmymoneyaccountcombo.h>
#include "kmymoneyaccountcompletion.h"

KMyMoneyAccountCombo::KMyMoneyAccountCombo( TQWidget* tqparent, const char* name ) :
  KComboBox( tqparent, name ),
  m_completion(0),
  m_mlbDown(false)
{
#ifndef KMM_DESIGNER
  m_completion = new kMyMoneyAccountCompletion(this);

  connect(this, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotButtonPressed()));
  connect(m_completion, TQT_SIGNAL(itemSelected(const TQString&)), this, TQT_SLOT(slotSelected(const TQString&)));
#endif

  // make sure that we can display a minimum of characters
  TQFontMetrics fm(font());
  setMinimumWidth(fm.maxWidth()*15);
  setMaximumHeight(height());

  // we only use this one item and replace the text as we have our own dropdown box
  insertItem(TQString(""));
}

KMyMoneyAccountCombo::~KMyMoneyAccountCombo()
{
}

void KMyMoneyAccountCombo::slotButtonPressed(void)
{
  m_completion->show();
}

void KMyMoneyAccountCombo::slotSelected(const TQString& id)
{
  try {
    MyMoneyAccount acc = MyMoneyFile::instance()->account(id);
    setText(acc.name());
    emit accountSelected(id);
  } catch(MyMoneyException *e) {
    delete e;
  }
}

void KMyMoneyAccountCombo::setSelected(const TQString& id)
{
  if(!id.isEmpty()) {
    try {
      MyMoneyAccount acc = MyMoneyFile::instance()->account(id);
      setSelected(acc);
    } catch(MyMoneyException *e) {
      qDebug("Account '%s' not found in %s(%d)", id.data(), __FILE__, __LINE__);
      delete e;
    }
  } else {
    setText(TQString());
    m_completion->setSelected(id);
  }
}

void KMyMoneyAccountCombo::setSelected(const MyMoneyAccount& acc)
{
  m_completion->setSelected(acc.id());
  setText(acc.name());
}

void KMyMoneyAccountCombo::setText(const TQString& txt)
{
  changeItem(txt, currentItem());
}

int KMyMoneyAccountCombo::loadList(const TQString& baseName, const TQValueList<TQString>& accountIdList, const bool clear)
{
  AccountSet set;

  return set.load(m_completion->selector(), baseName, accountIdList, clear);
}

int KMyMoneyAccountCombo::loadList(KMyMoneyUtils::categoryTypeE typeMask)
{
  AccountSet set;
  TQValueList<int> typeList;

  if(typeMask & KMyMoneyUtils::asset) {
    set.addAccountGroup(MyMoneyAccount::Asset);
  }
  if(typeMask & KMyMoneyUtils::liability) {
    set.addAccountGroup(MyMoneyAccount::Liability);
  }
  if(typeMask & KMyMoneyUtils::income) {
    set.addAccountGroup(MyMoneyAccount::Income);
  }
  if(typeMask & KMyMoneyUtils::expense) {
    set.addAccountGroup(MyMoneyAccount::Expense);
  }

  return set.load(m_completion->selector());
}

int KMyMoneyAccountCombo::loadList(MyMoneyAccount::accountTypeE type)
{
  AccountSet set;

  set.addAccountType(type);

  return set.load(m_completion->selector());
}

void KMyMoneyAccountCombo::keyPressEvent(TQKeyEvent* k)
{
  switch(k->key()) {
    case TQt::Key_Tab:
      break;

    case TQt::Key_Space:
      emit clicked();
      break;

    default:
      break;
  }
  return;
}

void KMyMoneyAccountCombo::mousePressEvent(TQMouseEvent *e)
{
  if ( e->button() != Qt::LeftButton ) {
    e->ignore();
    return;
  }
  bool hit = TQT_TQRECT_OBJECT(rect()).contains( e->pos() );
  if ( hit ) {                                // mouse press on button
    m_mlbDown = TRUE;                         // left mouse button down
    emit pressed();
  }
}

void KMyMoneyAccountCombo::mouseReleaseEvent(TQMouseEvent *e)
{
  if ( e->button() != Qt::LeftButton ) {
      e->ignore();
      return;
  }
  if ( !m_mlbDown )
      return;
  m_mlbDown = FALSE;                            // left mouse button up
  emit released();
  if ( TQT_TQRECT_OBJECT(rect()).contains( e->pos() ) ) {              // mouse release on button
    emit clicked();
  }
}

int KMyMoneyAccountCombo::count(void) const
{
  return m_completion->selector()->accountList().count();
}

TQStringList KMyMoneyAccountCombo::accountList(const TQValueList<MyMoneyAccount::accountTypeE>& list) const
{
  return m_completion->selector()->accountList(list);
};

int KMyMoneyAccountCombo::loadList(const TQValueList<int>& list)
{
  // FIXME make the caller construct the AccountSet directly
  AccountSet set;
  TQValueList<int>::const_iterator it;
  for(it = list.begin(); it != list.end(); ++it) {
    set.addAccountType(static_cast<MyMoneyAccount::accountTypeE>(*it));
  }
  return set.load(m_completion->selector());
};

TQStringList KMyMoneyAccountCombo::selectedAccounts(void) const
{
  TQStringList list;
  m_completion->selector()->selectedItems(list);
  return list;
};

#include "kmymoneyaccountcombo.moc"