summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/registersearchline.cpp
blob: 6acace34f96725246e79608f569ac7d711fc9d26 (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
/***************************************************************************
                          registersearchline.cpp
                             -------------------
    copyright            : (C) 2006 by Thomas Baumgart
    email                : ipwizard@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>
#include <tqlabel.h>
#include <tqtoolbutton.h>
#include <tqtimer.h>

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

#include <ktoolbar.h>
#include <ktoolbarbutton.h>
#include <kiconloader.h>
#include <klocale.h>

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

#include <registersearchline.h>

using namespace KMyMoneyRegister;

class RegisterSearchLine::RegisterSearchLinePrivate
{
public:
  RegisterSearchLinePrivate() :
    reg(0),
    combo(0),
    queuedSearches(0),
    status(0) {}

  Register* reg;
  TQComboBox* combo;
  TQString search;
  int queuedSearches;
  int status;
};

RegisterSearchLine::RegisterSearchLine(TQWidget* tqparent, Register* reg, const char* name) :
  KLineEdit(tqparent, name),
  d(new RegisterSearchLinePrivate)
{
  init(reg);
}

RegisterSearchLine::RegisterSearchLine(TQWidget* tqparent, const char* name) :
  KLineEdit(tqparent, name),
  d(new RegisterSearchLinePrivate)
{
  init(0);
}

void RegisterSearchLine::init(Register *reg)
{
  d->reg = reg;
  connect(this, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(queueSearch(const TQString&)));

  TQLabel* label = new TQLabel(i18n("label for status combo", "Stat&us"), tqparentWidget());
  d->combo = new TQComboBox(tqparentWidget());
  // don't change the order of the following lines unless updating
  // the case labels in RegisterSearchLine::itemMatches() at the same time
  d->combo->insertItem(SmallIcon("run"), i18n("Any status"));
  d->combo->insertItem(SmallIcon("fileimport"), i18n("Imported"));
  d->combo->insertItem(SmallIcon("connect_creating"), i18n("Matched"));
  d->combo->insertItem(SmallIcon("attention"), i18n("Erroneous"));
  d->combo->insertItem(i18n("Not marked"));
  d->combo->insertItem(i18n("Not reconciled"));
  d->combo->insertItem(i18n("Cleared"));
  d->combo->setCurrentItem(0);
  connect(d->combo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotStatusChanged(int)));

  label->setBuddy(d->combo);

  if(reg) {
    connect(reg, TQT_SIGNAL(destroyed()), this, TQT_SLOT(registerDestroyed()));
    connect(reg, TQT_SIGNAL(itemAdded(RegisterItem*)), this, TQT_SLOT(itemAdded(RegisterItem*)));
  } else {
    setEnabled(false);
  }
}

RegisterSearchLine::~RegisterSearchLine()
{
  delete d;
}

void RegisterSearchLine::setRegister(Register* reg)
{
  if(d->reg) {
    disconnect(d->reg, TQT_SIGNAL(destroyed()), this, TQT_SLOT(registerDestroyed()));
    disconnect(d->reg, TQT_SIGNAL(itemAdded(RegisterItem*)), this, TQT_SLOT(itemAdded(RegisterItem*)));
  }

  d->reg = reg;

  if(reg) {
    connect(reg, TQT_SIGNAL(destroyed()), this, TQT_SLOT(registerDestroyed()));
    connect(reg, TQT_SIGNAL(itemAdded(RegisterItem*)), this, TQT_SLOT(itemAdded(RegisterItem*)));
  }

  setEnabled(reg != 0);
}

void RegisterSearchLine::slotStatusChanged(int status)
{
  d->status = status;
  updateSearch();
}

void RegisterSearchLine::queueSearch(const TQString& search)
{
  d->queuedSearches++;
  d->search = search;
  TQTimer::singleShot(200, this, TQT_SLOT(activateSearch()));
}

void RegisterSearchLine::activateSearch(void)
{
  --(d->queuedSearches);
  if(d->queuedSearches == 0)
    updateSearch(d->search);
}

void RegisterSearchLine::updateSearch(const TQString& s)
{
  if(!d->reg)
    return;

  d->search = s.isNull() ? text() : s;

  // keep track of the current focus item
  RegisterItem* focusItem = d->reg->focusItem();

  bool enabled = d->reg->isUpdatesEnabled();
  d->reg->setUpdatesEnabled(false);

  bool scrollBarVisible = d->reg->verticalScrollBar()->isVisible();

  RegisterItem* p = d->reg->firstItem();
  for(; p; p = p->nextItem()) {
    p->setVisible(itemMatches(p, d->search));
  }
  d->reg->suppressAdjacentMarkers();
  d->reg->updateAlternate();
  d->reg->setUpdatesEnabled(enabled);

  // if focus item is still visible, then make sure we have
  // it on screen
  if(focusItem && focusItem->isVisible()) {
    d->reg->updateContents();
    d->reg->ensureItemVisible(focusItem);
  } else
    d->reg->tqrepaintContents();

  d->reg->updateScrollBars();

  // if the scrollbar's visibility changed, we need to resize the contents
  if(scrollBarVisible != d->reg->verticalScrollBar()->isVisible()) {
    d->reg->resize(DetailColumn);
  }
}

bool RegisterSearchLine::itemMatches(const RegisterItem* item, const TQString& s) const
{
  const Transaction* t = dynamic_cast<const Transaction*>(item);
  if(t && !t->transaction().id().isEmpty()) {
    // Keep the case list of the following switch statement
    // in sync with the logic to fill the combo box in
    // RegisterSearchLine::init()
    switch(d->status) {
      default:
        break;
      case 1:    // Imported
        if(!t->transaction().isImported())
          return false;
        break;
      case 2:    // Matched
        if(!t->split().isMatched())
          return false;
        break;
      case 3:    // Erroneous
        if(t->transaction().splitSum().isZero())
          return false;
        break;
      case 4:    // Not marked
        if(t->split().reconcileFlag() != MyMoneySplit::NotReconciled)
          return false;
        break;
      case 5:    // Not reconciled
        if(t->split().reconcileFlag() != MyMoneySplit::NotReconciled
        && t->split().reconcileFlag() != MyMoneySplit::Cleared)
          return false;
        break;
      case 6:    // Cleared
        if(t->split().reconcileFlag() != MyMoneySplit::Cleared)
          return false;
        break;
    }
  }

  return item->matches(s);
}

void RegisterSearchLine::reset(void)
{
  clear();
  d->combo->setCurrentItem(0);
  slotStatusChanged(0);
}

void RegisterSearchLine::itemAdded(RegisterItem* item) const
{
  item->setVisible(itemMatches(item, text()));
}

void RegisterSearchLine::registerDestroyed(void)
{
  d->reg = 0;
  setEnabled(false);
}


class RegisterSearchLineWidget::RegisterSearchLineWidgetPrivate
{
  public:
  RegisterSearchLineWidgetPrivate() :
    reg(0),
    searchLine(0),
    clearButton(0) {}

  Register* reg;
  RegisterSearchLine* searchLine;
  TQToolButton* clearButton;
};


RegisterSearchLineWidget::RegisterSearchLineWidget(Register* reg, TQWidget* tqparent, const char* name) :
  TQHBox(tqparent, name),
  d(new RegisterSearchLineWidgetPrivate)
{
  d->reg = reg;
  setSpacing(5);
  TQTimer::singleShot(0, this, TQT_SLOT(createWidgets()));
}

RegisterSearchLineWidget::~RegisterSearchLineWidget()
{
  delete d;
}

RegisterSearchLine* RegisterSearchLineWidget::createSearchLine(Register* reg)
{
  if(!d->searchLine)
    d->searchLine = new RegisterSearchLine(this, reg);
  return d->searchLine;
}

void RegisterSearchLineWidget::createWidgets(void)
{
  if(!d->clearButton) {
    d->clearButton = new TQToolButton(this);
    TQIconSet icon = SmallIconSet(TQApplication::reverseLayout() ? "clear_left" : "locationbar_erase");
    d->clearButton->setIconSet(icon);
  }

  d->clearButton->show();

  TQLabel *label = new TQLabel(i18n("S&earch:"), this, "kde toolbar widget");

  d->searchLine = createSearchLine(d->reg);
  d->searchLine->show();

  label->setBuddy(d->searchLine);
  label->show();

  connect(d->clearButton, TQT_SIGNAL(clicked()), d->searchLine, TQT_SLOT(reset()));
}

RegisterSearchLine* RegisterSearchLineWidget::searchLine(void) const
{
  return d->searchLine;
}

#include "registersearchline.moc"