kwave  18.07.70
KeywordWidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  KeywordWidget.cpp - widget for editing a list of keywords
3  -------------------
4  begin : Fri 02 2002
5  copyright : (C) 2002 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QEvent>
21 #include <QKeyEvent>
22 #include <QPushButton>
23 #include <QLineEdit>
24 #include <QListWidget>
25 
26 #include "KeywordWidget.h"
27 
28 //***************************************************************************
30  :QWidget(parent), Ui::KeywordWidgetBase()
31 {
32  setupUi(this);
33 
34  Q_ASSERT(edKeyword);
35  Q_ASSERT(btAdd);
36  Q_ASSERT(btAuto);
37  Q_ASSERT(btRemove);
38  Q_ASSERT(lstKeywords);
39 
40  connect(edKeyword, SIGNAL(textChanged(QString)),
41  this, SLOT(editChanged(QString)));
42  connect(btAdd, SIGNAL(clicked()),
43  this, SLOT(add()));
44  connect(btAuto, SIGNAL(clicked()),
45  this, SLOT(autoClicked()));
46  connect(btRemove, SIGNAL(clicked()),
47  this, SLOT(remove()));
48  connect(lstKeywords, SIGNAL(itemActivated(QListWidgetItem*)),
49  this, SLOT(listClicked(QListWidgetItem*)));
50  connect(lstKeywords, SIGNAL(itemClicked(QListWidgetItem*)),
51  this, SLOT(listClicked(QListWidgetItem*)));
52  connect(lstKeywords, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
53  this, SLOT(listClicked(QListWidgetItem*)));
54 
55  // if the user presses return in the edit control, this means
56  // the same as clicking on the "Add" button
57  edKeyword->installEventFilter(this);
58 
59  update();
60 }
61 
62 //***************************************************************************
64 {
65 }
66 
67 //***************************************************************************
68 bool Kwave::KeywordWidget::contained(const QString &item)
69 {
70  if (!item.length()) return false;
71  return (!lstKeywords->findItems(item, Qt::MatchExactly).isEmpty());
72 }
73 
74 //***************************************************************************
76 {
77  QStringList list;
78  unsigned int count = lstKeywords->count();
79  for (unsigned int index=0; index < count; ++index) {
80  QListWidgetItem *item = lstKeywords->item(index);
81  if (item && item->text().length())
82  list.append(item->text());
83  }
84  return list;
85 }
86 
87 //***************************************************************************
89 {
90  lstKeywords->clear();
91  edKeyword->clear();
92 
93  foreach (const QString &it, keywords) {
94  QString item = it.simplified();
95  if (contained(item)) continue; // skip duplicate
96  lstKeywords->addItem(item);
97  }
98  lstKeywords->setSortingEnabled(true);
99  lstKeywords->sortItems();
100 
101  edKeyword->clear();
102  update();
103  edKeyword->clear();
104 }
105 
106 //***************************************************************************
108 {
109  QString edit = edKeyword->text().simplified();
110 
111  // "Add" is only allowed if the current edit space is not empty and
112  // the entered text is not already in the list
113  btAdd->setEnabled(edit.length() && !contained(edit));
114 
115  // "Remove" is only enabled if something out of the list has been selected
116  btRemove->setEnabled((lstKeywords->currentItem() != Q_NULLPTR) &&
117  (contained(edit) || !edit.length()));
118 
119  // the list is only enabled if it is not empty
120  lstKeywords->setEnabled(lstKeywords->count() != 0);
121 
122  // the current item should always be visible
123  lstKeywords->scrollToItem(lstKeywords->currentItem(),
124  QAbstractItemView::EnsureVisible);
125 }
126 
127 //***************************************************************************
128 void Kwave::KeywordWidget::editChanged(const QString &edit)
129 {
130  QString text = edit.simplified();
131  QList<QListWidgetItem *> matches =
132  lstKeywords->findItems(text, Qt::MatchStartsWith);
133  if (edit.length() && !matches.isEmpty()) {
134  lstKeywords->setCurrentItem(matches.takeFirst());
135  update();
136  edKeyword->setText(edit);
137  } else {
138  update();
139  }
140 }
141 
142 //***************************************************************************
144 {
145  add(); // means the same as pressing "Add"
146 }
147 
148 //***************************************************************************
150 {
151  QString text = edKeyword->text().simplified();
152  if (!text.length()) return;
153  if (contained(text)) return;
154 
155  // insert the current edit text and sort the list
156  lstKeywords->addItem(text);
157  lstKeywords->sortItems();
158 
159  // find the new item again and make it the current selection
160  QList<QListWidgetItem *> matches =
161  lstKeywords->findItems(text, Qt::MatchStartsWith);
162  if (!matches.isEmpty())
163  lstKeywords->setCurrentItem(matches.takeFirst());
164  edKeyword->clear();
165 
166  // now we do no longer need the edit
167  update();
168  edKeyword->clear();
169 }
170 
171 //***************************************************************************
173 {
174  // remove the item from the list
175  int index = lstKeywords->currentRow();
176  QListWidgetItem *item = lstKeywords->takeItem(index);
177  if (item) delete item;
178  edKeyword->clear();
179 
180  // set the previous item as current
181  if (index) --index;
182  if (lstKeywords->item(index))
183  lstKeywords->item(index)->setSelected(true);
184 
185  edKeyword->clear();
186  update();
187 }
188 
189 //***************************************************************************
190 void Kwave::KeywordWidget::listClicked(QListWidgetItem *item)
191 {
192  if (!item) return;
193  edKeyword->setText(item->text());
194  update();
195 }
196 
197 //***************************************************************************
199 {
200  emit autoGenerate();
201 }
202 
203 //***************************************************************************
204 bool Kwave::KeywordWidget::eventFilter(QObject *sender, QEvent *event)
205 {
206  if (!event) return false;
207 
208  if ((sender == edKeyword) && (event->type() == QEvent::KeyPress)) {
209  QKeyEvent *k = static_cast<QKeyEvent *>(event);
210  if ((k->key() == Qt::Key_Return) || (k->key() == Qt::Key_Enter)) {
211  add();
212  return true;
213  }
214  }
215  return QObject::eventFilter(sender, event);
216 }
217 
218 
219 //***************************************************************************
220 //***************************************************************************
virtual ~KeywordWidget() Q_DECL_OVERRIDE
void setKeywords(const QStringList &keywords)
KeywordWidget(QWidget *parent)
void listClicked(QListWidgetItem *item)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
bool contained(const QString &item)
virtual bool eventFilter(QObject *sender, QEvent *event) Q_DECL_OVERRIDE
void editChanged(const QString &)
QStringList keywords()