summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/kmymoneycompletion.cpp
blob: 55e4d75f4179e39ba43d520d3def7fdede2ea37f (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
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
/***************************************************************************
                          kmymoneycompletion.cpp  -  description
                             -------------------
    begin                : Mon Apr 26 2004
    copyright            : (C) 2000-2004 by Michael Edwardes
    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 <tqapplication.h>

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

#include <klistview.h>

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

#include "kmymoneycompletion.h"
#include <kmymoney/kmymoneyselector.h>
#include <kmymoney/kmymoneylistviewitem.h>
#include "./kmymoneycombo.h"

const int kMyMoneyCompletion::MAX_ITEMS = 16;

kMyMoneyCompletion::kMyMoneyCompletion(TQWidget *tqparent, const char *name ) :
  TQVBox(tqparent, name, WType_Popup)
{
  m_selector = new KMyMoneySelector(this);
  m_selector->listView()->setFocusProxy(this);

  m_parent = tqparent;
  setFocusProxy((tqparent) ? tqparent : (TQWidget*) TQ_NoFocus);
  setFrameStyle(TQFrame::PopupPanel | TQFrame::Raised);
  connectSignals(m_selector, m_selector->listView());
}

void kMyMoneyCompletion::connectSignals(TQWidget* widget, KListView* lv)
{
  m_widget = widget;
  m_lv = lv;
  connect(lv, TQT_SIGNAL(executed(TQListViewItem*,const TQPoint&,int)), this, TQT_SLOT(slotItemSelected(TQListViewItem*,const TQPoint&,int)));
}

kMyMoneyCompletion::~kMyMoneyCompletion()
{
}

void kMyMoneyCompletion::adjustSize(void)
{
  TQListViewItemIterator it(m_lv, TQListViewItemIterator::Visible);
  int count = 0;
  while(it.current()) {
    ++count;
    ++it;
  }
  adjustSize(count);
}

void kMyMoneyCompletion::adjustSize(const int count)
{
  int w = m_widget->tqsizeHint().width();
  if(m_parent && w < m_parent->width())
    w = m_parent->width();

  TQFontMetrics fm(font());
  if(w < fm.maxWidth()*15)
    w = fm.maxWidth()*15;

  int h = 0;
  TQListViewItemIterator it(m_lv, TQListViewItemIterator::Visible);
  TQListViewItem* item = it.current();
  if(item)
    h = item->height() * (count > MAX_ITEMS ? MAX_ITEMS : count);

  // the offset of 4 in the next statement avoids the
  // display of a scroll bar if count < MAX_ITEMS.
  resize(w, h+4);

  if(m_parent) {
    // the code of this basic block is taken from KCompletionBox::show()
    // and modified to our local needs

    // this is probably better, once kde switches to requiring qt3.1
    // TQRect screenSize = TQApplication::desktop()->availableGeometry(d->m_parent);
    // for now use this since it's qt3.0.x-safe
    TQRect screenSize = TQApplication::desktop()->screenGeometry(TQApplication::desktop()->screenNumber(m_parent));

    TQPoint orig = m_parent->mapToGlobal( TQPoint(0, m_parent->height()) );
    int x = orig.x();
    int y = orig.y();

    if ( x + width() > screenSize.right() )
        x = screenSize.right() - width();

    // check for the maximum height here to avoid flipping
    // of the completion box from top to bottom of the
    // edit widget. The offset (y) is certainly based
    // on the actual height.
    if(item) {
      if ((y + item->height()*MAX_ITEMS) > screenSize.bottom() )
        y = y - height() - m_parent->height();
    }

    move( x, y);
  }
}

void kMyMoneyCompletion::show(bool presetSelected)
{
  if(!m_id.isEmpty() && presetSelected)
    m_selector->setSelected(m_id);

  adjustSize();

  if(m_parent) {
    m_parent->installEventFilter(this);
    // make sure to install the filter for the combobox lineedit as well
    // We have do this here because TQObject::installEventFilter() is not
    // declared virtual and we have no chance to override it in KMyMoneyCombo
    KMyMoneyCombo* c = dynamic_cast<KMyMoneyCombo*>(m_parent);
    if(c && c->lineEdit()) {
      c->lineEdit()->installEventFilter(this);
    }
  }

  TQVBox::show();
}

void kMyMoneyCompletion::hide(void)
{
  if(m_parent) {
    m_parent->removeEventFilter(this);
    // make sure to uninstall the filter for the combobox lineedit as well
    // We have do this here because TQObject::installEventFilter() is not
    // declared virtual and we have no chance to override it in KMyMoneyCombo
    KMyMoneyCombo* c = dynamic_cast<KMyMoneyCombo*>(m_parent);
    if(c && c->lineEdit()) {
      c->lineEdit()->removeEventFilter(this);
    }
  }
  TQVBox::hide();
}

bool kMyMoneyCompletion::eventFilter(TQObject* o, TQEvent* e)
{
  int type = e->type();

  KMyMoneyCombo *c = dynamic_cast<KMyMoneyCombo*>(m_parent);
  TQListViewItem* item;
  if(TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(m_parent) || (c && TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(c->lineEdit()))) {
    if(isVisible()) {
      if(type == TQEvent::KeyPress) {
        TQKeyEvent* ev = TQT_TQKEYEVENT (e);
        TQKeyEvent evt(TQEvent::KeyPress,
                      Key_Down, 0, ev->state(), TQString(),
                      ev->isAutoRepeat(), ev->count());
        TQKeyEvent evbt(TQEvent::KeyPress,
                      Key_Up, 0, ev->state(), TQString(),
                      ev->isAutoRepeat(), ev->count());

        switch(ev->key()) {
          case Key_Tab:
          case Key_BackTab:
            slotItemSelected(m_lv->currentItem(), TQPoint(0,0), 0);
            break;

          case Key_Down:
          case Key_Next:
            item = m_lv->currentItem();
            while(item) {
              item = item->itemBelow();
              if(item && selector()->match(m_lastCompletion, item))
                break;
            }
            if(item) {
              m_lv->setCurrentItem(item);
              selector()->ensureItemVisible(item);
            }
            ev->accept();
            return true;

          case Key_Up:
          case Key_Prior:
            item = m_lv->currentItem();
            while(item) {
              item = item->itemAbove();
              if(item && selector()->match(m_lastCompletion, item))
                break;
            }
            if(item) {
              m_lv->setCurrentItem(item);
              // make sure, we always see a possible (non-selectable) group item
              if(item->itemAbove())
                item = item->itemAbove();
              selector()->ensureItemVisible(item);
            }
            ev->accept();
            return true;

          case Key_Escape:
            hide();
            ev->accept();
            return true;

          case Key_Enter:
          case Key_Return:
            slotItemSelected(m_lv->currentItem(), TQPoint(0,0), 0);
            ev->accept();
            return true;

          case Key_Home:
          case Key_End:
            if(ev->state() & ControlButton) {
              item = m_lv->currentItem();
              if(ev->key() == Key_Home) {
                while(item && item->itemAbove()) {
                  item = item->itemAbove();
                }
                while(item && !selector()->match(m_lastCompletion, item)) {
                  item = item->itemBelow();
                }
              } else {
                while(item && item->itemBelow()) {
                  item = item->itemBelow();
                }
                while(item && !selector()->match(m_lastCompletion, item)) {
                  item = item->itemAbove();
                }
              }
              if(item) {
                m_lv->setCurrentItem(item);
                // make sure, we always see a possible (non-selectable) group item
                if(item->itemAbove())
                  item = item->itemAbove();
                selector()->ensureItemVisible(item);
              }
              ev->accept();
              return true;
            }
            break;

          default:
            break;

        }
      }
    }
  }
  return TQVBox::eventFilter(o, e);
}

void kMyMoneyCompletion::slotMakeCompletion(const TQString& txt)
{
  int cnt = selector()->slotMakeCompletion(txt.stripWhiteSpace());

  if(m_parent && m_parent->isVisible() && !isVisible() && cnt)
    show(false);
  else {
    if(cnt != 0) {
      adjustSize();
    } else {
      hide();
    }
  }
}

void kMyMoneyCompletion::slotItemSelected(TQListViewItem *item, const TQPoint&, int)
{
  KMyMoneyListViewItem* it_v = static_cast<KMyMoneyListViewItem*>(item);
  if(it_v && it_v->isSelectable()) {
    TQString id = it_v->id();
    // hide the widget, so we can debug the slots that are connect
    // to the signal we emit very soon
    hide();
    m_id = id;
    emit itemSelected(id);
  }
}

void kMyMoneyCompletion::setSelected(const TQString& id)
{
  m_id = id;
  m_selector->setSelected(id, true);
}

#include "kmymoneycompletion.moc"