summaryrefslogtreecommitdiffstats
path: root/src/common/gui/editlistbox.cpp
blob: 2e40b8a14db941e2ac5eca438222bf7c91862686 (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
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
/* Copyright (C) 2000 David Faure <faure@kde.org>, Alexander Neundorf <neundorf@kde.org>
                 2000, 2002 Carsten Pfeiffer <pfeiffer@kde.org>
   Copyright (C) 2006-2007 Nicolas Hadacek <hadacek@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include "editlistbox.h"

#include <tqstringlist.h>
#include <tqlabel.h>
#include <tqheader.h>

#include <kdialog.h>
#include <klocale.h>
#include <kapplication.h>
#include <knotifyclient.h>
#include <kiconloader.h>
#include <kstdguiitem.h>

EditListBox::EditListBox(uint nbColumns, TQWidget *tqparent, const char *name, Mode mode, Buttons buttons)
  : TQFrame(tqparent, name), _mode(mode), _buttons(buttons)
{
  m_lineEdit = new KLineEdit;
  init(nbColumns, m_lineEdit);
}

EditListBox::EditListBox(uint nbColumns, TQWidget *view, KLineEdit *lineEdit, TQWidget *tqparent, const char *name,
                         Mode mode, Buttons buttons)
  : TQFrame(tqparent, name), _mode(mode), _buttons(buttons)
{
  m_lineEdit = lineEdit;
  init(nbColumns, view);
}

void EditListBox::init(uint nbColumns, TQWidget *view)
{
    _addButton = 0;
    _removeButton = 0;
    _moveUpButton = 0;
    _moveDownButton = 0;
    _removeAllButton = 0;
    _resetButton = 0;
    tqsetSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding));

    TQGridLayout *grid = new TQGridLayout(this, 1, 1, 0, KDialog::spacingHint());
    uint row = 0;
    if (view) {
      TQHBoxLayout *hbox = new TQHBoxLayout(KDialog::spacingHint());
      grid->addMultiCellLayout(hbox, row,row, 0,1);
      if (m_lineEdit) {
	KIconLoader loader;
	TQIconSet iconset = loader.loadIcon("locationbar_erase", KIcon::Toolbar);
	KPushButton *button = new  KPushButton(iconset, TQString(), this);
	connect(button, TQT_SIGNAL(clicked()), TQT_SLOT(clearEdit()));
	hbox->addWidget(button);
      }
      view->reparent( this, TQPoint(0,0) );
      hbox->addWidget(view);
      row++;
    }
    _listView= new KListView(this);
    for (uint i=0; i<nbColumns; i++) _listView->addColumn(TQString());
    _listView->header()->hide();
    _listView->setSorting(-1);
    _listView->setResizeMode(KListView::LastColumn);
    _listView->setColumnWidthMode(nbColumns-1, KListView::Maximum);
    grid->addWidget(_listView, row, 0);
    TQVBoxLayout *vbox = new TQVBoxLayout(10);
    grid->addLayout(vbox, row, 1);
    _buttonsLayout = new TQVBoxLayout(10);
    vbox->addLayout(_buttonsLayout);
    vbox->addStretch(1);

    setButtons(_buttons);

    if (m_lineEdit) {
      connect(m_lineEdit,TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(typedSomething(const TQString&)));
      m_lineEdit->setTrapReturnKey(true);
      connect(m_lineEdit,TQT_SIGNAL(returnPressed()),this,TQT_SLOT(addItem()));
    }
    connect(_listView, TQT_SIGNAL(selectionChanged()), TQT_SLOT(selectionChanged()));

    // maybe supplied lineedit has some text already
    typedSomething(m_lineEdit ? m_lineEdit->text() : TQString());
}

void EditListBox::setButtons(Buttons buttons)
{
    _buttons = buttons;

    delete _addButton;
    _addButton = 0;
    if ( buttons & Add ) {
#if KDE_VERSION < KDE_MAKE_VERSION(3,4,0)
        _addButton = new KPushButton(KGuiItem(i18n("Add"), "edit_add"), this);
#else
        _addButton = new KPushButton(KStdGuiItem::add(), this);
#endif
        _addButton->setEnabled(false);
        _addButton->show();
        connect(_addButton, TQT_SIGNAL(clicked()), TQT_SLOT(addItem()));
        _buttonsLayout->addWidget(_addButton);
    }

    delete _removeButton;
    _removeButton = 0;
    if ( buttons & Remove ) {
        _removeButton = new KPushButton(KGuiItem(i18n("Remove"), "clear"), this);
        _removeButton->setEnabled(false);
        _removeButton->show();
        connect(_removeButton, TQT_SIGNAL(clicked()), TQT_SLOT(removeItem()));
        _buttonsLayout->addWidget(_removeButton);
    }

    delete _removeAllButton;
    _removeAllButton = 0;
    if ( buttons & RemoveAll ) {
        _removeAllButton = new KPushButton(KGuiItem(i18n("Remove All"), "delete"), this);
        _removeAllButton->show();
        connect(_removeAllButton, TQT_SIGNAL(clicked()), TQT_SLOT(clear()));
        _buttonsLayout->addWidget(_removeAllButton);
    }

    delete _moveUpButton;
    _moveUpButton = 0;
    delete _moveDownButton;
    _moveDownButton = 0;
    if ( buttons & UpDown ) {
        _moveUpButton = new KPushButton(KGuiItem(i18n("Move &Up"), "up"), this);
        _moveUpButton->setEnabled(false);
        _moveUpButton->show();
        connect(_moveUpButton, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemUp()));
        _buttonsLayout->addWidget(_moveUpButton);
        _moveDownButton = new KPushButton(KGuiItem(i18n("Move &Down"), "down"), this);
        _moveDownButton->setEnabled(false);
        _moveDownButton->show();
        connect(_moveDownButton, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemDown()));
        _buttonsLayout->addWidget(_moveDownButton);
    }

    delete _resetButton;
    _resetButton = 0;
    if ( buttons & Reset ) {
        _resetButton = new KPushButton(KStdGuiItem::reset(), this);
        _resetButton->show();
        connect(_resetButton, TQT_SIGNAL(clicked()), TQT_SIGNAL(reset()));
        _buttonsLayout->addWidget(_resetButton);
    }
}

void EditListBox::typedSomething(const TQString& text)
{
  TQListViewItem *item = _listView->selectedItem();
  if (item) {
    if( selectedText()!=text ) {
      item->setText(textColumn(), text);
      emit changed();
    }
  }
  updateButtons();
}

void EditListBox::moveItemUp()
{
  TQListViewItem *item = _listView->selectedItem();
  if ( item==0 || item->itemAbove()==0 ) return;
  item->itemAbove()->moveItem(item);
  updateButtons();
  emit changed();
}

void EditListBox::moveItemDown()
{
  TQListViewItem *item = _listView->selectedItem();
  if ( item==0 || item->itemBelow()==0 ) return;
  item->moveItem(item->itemBelow());
  updateButtons();
  emit changed();
}

void EditListBox::addItem()
{
    // when m_checkAtEntering is true, the add-button is disabled, but this
    // slot can still be called through Key_Return/Key_Enter. So we guard
    // against this.
    if ( !_addButton || !_addButton->isEnabled() || m_lineEdit==0 ) return;

    addItem(m_lineEdit->text());
}

void EditListBox::addItem(const TQString &text)
{
    bool alreadyInList(false);
    //if we didn't check for dupes at the inserting we have to do it now
    if ( _mode==DuplicatesDisallowed ) alreadyInList = _listView->tqfindItem(text, textColumn());

    if (m_lineEdit) {
      bool block = m_lineEdit->signalsBlocked();
      m_lineEdit->blockSignals(true);
      m_lineEdit->clear();
      m_lineEdit->blockSignals(block);
    }
    _listView->clearSelection();

    if (!alreadyInList) {
      TQListViewItem *item = createItem();
      item->setText(textColumn(), text);
      if ( _listView->lastItem() ) item->moveItem(_listView->lastItem());
      emit changed();
      emit added(text);
    }
    updateButtons();
}

void EditListBox::clearEdit()
{
  _listView->clearSelection();
  if (m_lineEdit) {
    m_lineEdit->clear();
    m_lineEdit->setFocus();
  }
  updateButtons();
}

void EditListBox::removeItem()
{
  TQListViewItem *item = _listView->selectedItem();
  if (item) {
    TQString text = item->text(textColumn());
    delete item;
    emit changed();
    emit removed(text);
    updateButtons();
  }
}

void EditListBox::selectionChanged()
{
  if (m_lineEdit) m_lineEdit->setText(selectedText());
  updateButtons();
}

void EditListBox::clear()
{
  _listView->clear();
  if (m_lineEdit) {
    m_lineEdit->clear();
    m_lineEdit->setFocus();
  }
  updateButtons();
  emit changed();
}

uint EditListBox::count() const
{
  uint nb = 0;
  TQListViewItemIterator it(_listView);
  for (; it.current(); ++it) nb++;
  return nb;
}

const TQListViewItem *EditListBox::item(uint i) const
{
  uint k = 0;
  TQListViewItemIterator it(_listView);
  for (; it.current(); ++it) {
    if ( k==i ) return it.current();
    k++;
  }
  return 0;
}

TQStringList EditListBox::texts() const
{
  TQStringList list;
  TQListViewItemIterator it(_listView);
  for (; it.current(); ++it) list.append(it.current()->text(textColumn()));
  return list;
}

void EditListBox::setTexts(const TQStringList& items)
{
  _listView->clear();
  for (int i=items.count()-1; i>=0; i--) {
    TQListViewItem *item = createItem();
    item->setText(textColumn(), items[i]);
  }
  if (m_lineEdit) m_lineEdit->clear();
  updateButtons();
}

void EditListBox::updateButtons()
{
  TQListViewItem *item = _listView->selectedItem();
  if (_addButton) {
    if ( m_lineEdit==0 ) _addButton->setEnabled(true);
    else {
      TQString text = m_lineEdit->text();
      if ( _mode!=DuplicatesCheckedAtEntering ) _addButton->setEnabled(!text.isEmpty());
      else if ( text.isEmpty() ) _addButton->setEnabled(false);
      else _addButton->setEnabled(!_listView->tqfindItem(text, textColumn()));
    }
  }
  if (_removeButton) _removeButton->setEnabled(item);
  if (_moveUpButton) _moveUpButton->setEnabled(item && item->itemAbove());
  if (_moveDownButton) _moveDownButton->setEnabled(item && item->itemBelow());
  if (_removeAllButton) _removeAllButton->setEnabled(_listView->firstChild());
}

void EditListBox::setEditText(const TQString &text)
{
  _listView->clearSelection();
  if (m_lineEdit) m_lineEdit->setText(text);
  updateButtons();
}

TQListViewItem *EditListBox::createItem()
{
  return new KListViewItem(_listView);
}

TQString EditListBox::selectedText() const
{
  TQListViewItem *item = _listView->selectedItem();
  if ( item==0 ) return TQString();
  return item->text(textColumn());
}