kwave  18.07.70
Kwave::KeywordWidget Class Reference

#include <KeywordWidget.h>

Inheritance diagram for Kwave::KeywordWidget:
Inheritance graph
Collaboration diagram for Kwave::KeywordWidget:
Collaboration graph

Signals

void autoGenerate ()
 

Public Member Functions

 KeywordWidget (QWidget *parent)
 
virtual ~KeywordWidget () Q_DECL_OVERRIDE
 
QStringList keywords ()
 
void setKeywords (const QStringList &keywords)
 

Protected Member Functions

bool contained (const QString &item)
 
void update ()
 
virtual bool eventFilter (QObject *sender, QEvent *event) Q_DECL_OVERRIDE
 

Private Slots

void editChanged (const QString &)
 
void returnPressed ()
 
void add ()
 
void remove ()
 
void listClicked (QListWidgetItem *item)
 
void autoClicked ()
 

Detailed Description

Re-implemented and slightly modified version of a KEditListBox. All items in the list are unique and sorted alphabetically, leading and trailing whitespaces are removed. An additional "Auto" button is provided for populating the list with automatically generated entries.

Definition at line 40 of file KeywordWidget.h.

Constructor & Destructor Documentation

◆ KeywordWidget()

Kwave::KeywordWidget::KeywordWidget ( QWidget *  parent)
explicit

Constructor

Definition at line 29 of file KeywordWidget.cpp.

References add(), autoClicked(), Kwave::connect(), editChanged(), listClicked(), and update().

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 }
void listClicked(QListWidgetItem *item)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
void editChanged(const QString &)
Here is the call graph for this function:

◆ ~KeywordWidget()

Kwave::KeywordWidget::~KeywordWidget ( )
virtual

Destructor

Definition at line 63 of file KeywordWidget.cpp.

64 {
65 }

Member Function Documentation

◆ add

void Kwave::KeywordWidget::add ( )
privateslot

add an entry to the list

Definition at line 149 of file KeywordWidget.cpp.

References contained(), and update().

Referenced by eventFilter(), KeywordWidget(), and returnPressed().

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 }
bool contained(const QString &item)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ autoClicked

void Kwave::KeywordWidget::autoClicked ( )
privateslot

forwards the click of the "Auto" button

Definition at line 198 of file KeywordWidget.cpp.

References autoGenerate().

Referenced by KeywordWidget().

199 {
200  emit autoGenerate();
201 }
Here is the caller graph for this function:

◆ autoGenerate

void Kwave::KeywordWidget::autoGenerate ( )
signal

emitted if the user pressed the "Auto" button

Referenced by autoClicked().

Here is the caller graph for this function:

◆ contained()

bool Kwave::KeywordWidget::contained ( const QString &  item)
protected

returns true if the given string is contained in the list

Definition at line 68 of file KeywordWidget.cpp.

Referenced by add(), setKeywords(), and update().

69 {
70  if (!item.length()) return false;
71  return (!lstKeywords->findItems(item, Qt::MatchExactly).isEmpty());
72 }
Here is the caller graph for this function:

◆ editChanged

void Kwave::KeywordWidget::editChanged ( const QString &  edit)
privateslot

updates the controls if the text edit changed

Definition at line 128 of file KeywordWidget.cpp.

References update().

Referenced by KeywordWidget().

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 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ eventFilter()

bool Kwave::KeywordWidget::eventFilter ( QObject *  sender,
QEvent *  event 
)
protectedvirtual

event filter for blocking the effect of pressing "return"

Definition at line 204 of file KeywordWidget.cpp.

References add().

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 }
Here is the call graph for this function:

◆ keywords()

QStringList Kwave::KeywordWidget::keywords ( )

Returns the list of keywords (sorted)

Definition at line 75 of file KeywordWidget.cpp.

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 }

◆ listClicked

void Kwave::KeywordWidget::listClicked ( QListWidgetItem *  item)
privateslot

called when a new list entry has been selected

Definition at line 190 of file KeywordWidget.cpp.

References update().

Referenced by KeywordWidget().

191 {
192  if (!item) return;
193  edKeyword->setText(item->text());
194  update();
195 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove

void Kwave::KeywordWidget::remove ( )
privateslot

remove the currently selected item from the list

Definition at line 172 of file KeywordWidget.cpp.

References update().

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 }
Here is the call graph for this function:

◆ returnPressed

void Kwave::KeywordWidget::returnPressed ( )
privateslot

called if the user pressed return in the edit line

Definition at line 143 of file KeywordWidget.cpp.

References add().

144 {
145  add(); // means the same as pressing "Add"
146 }
Here is the call graph for this function:

◆ setKeywords()

void Kwave::KeywordWidget::setKeywords ( const QStringList &  keywords)

Sets/initializes the list of keywords

Definition at line 88 of file KeywordWidget.cpp.

References contained(), and update().

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 }
bool contained(const QString &item)
QStringList keywords()
Here is the call graph for this function:

◆ update()

void Kwave::KeywordWidget::update ( )
protected

update the enable state of the buttons

Definition at line 107 of file KeywordWidget.cpp.

References contained().

Referenced by add(), editChanged(), KeywordWidget(), listClicked(), remove(), and setKeywords().

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 }
bool contained(const QString &item)
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: