34 #include <QContextMenuEvent>
35 #include <QHBoxLayout>
36 #include <QPushButton>
37 #include <QToolButton>
56 CategorySelector_->SetSeparator (Separator_);
57 CategorySelector_->hide ();
59 QAbstractItemModel *model = Completer_->model ();
61 if (model->metaObject ()->indexOfSignal (QMetaObject::normalizedSignature (
"tagsUpdated (QStringList)")) >= 0)
63 SIGNAL (tagsUpdated (QStringList)),
67 QStringList initialTags;
68 for (
int i = 0; i < model->rowCount (); ++i)
69 initialTags << model->data (model->index (i, 0)).toString ();
72 connect (CategorySelector_.get (),
73 SIGNAL (tagsSelectionChanged (
const QStringList&)),
75 SLOT (handleSelectionChanged (
const QStringList&)));
78 SIGNAL (textChanged (
const QString&)),
79 CategorySelector_.get (),
80 SLOT (lineTextChanged (
const QString&)));
85 auto button =
new QToolButton {
this };
86 button->setIconSize ({ 16, 16 });
87 button->setIcon (QIcon::fromTheme (
"mail-tagged"));
88 button->setCursor (Qt::ArrowCursor);
89 button->setStyleSheet (
"QToolButton { border: none; padding: 0px; }");
96 SLOT (showSelector ()));
107 if (CategorySelector_)
108 CategorySelector_->SetSeparator (sep);
113 if (Completer_->widget () !=
this)
116 QString wtext = text ();
117 if (completion.startsWith (wtext))
119 int pos = wtext.lastIndexOf (Separator_);
121 wtext = wtext.left (pos).append (Separator_);
124 wtext.append (completion);
125 wtext = wtext.simplified ();
133 CategorySelector_->setPossibleSelections (tags);
138 setText (tags.join (Separator_));
139 if (CategorySelector_.get ())
140 CategorySelector_->SetSelections (tags);
143 void TagsLineEdit::handleSelectionChanged (
const QStringList& tags)
145 setText (tags.join (Separator_));
150 void TagsLineEdit::showSelector ()
152 CategorySelector_->move (QCursor::pos ());
153 CategorySelector_->show ();
158 if (Completer_ && Completer_->popup ()->isVisible ())
165 case Qt::Key_Backtab:
172 QLineEdit::keyPressEvent (e);
174 bool cos = e->modifiers () & (Qt::ControlModifier |
178 bool isShortcut = e->modifiers () & (Qt::ControlModifier |
182 (cos && e->text ().isEmpty ()) ||
186 QString completionPrefix = textUnderCursor ();
187 Completer_->setCompletionPrefix (completionPrefix);
188 Completer_->popup ()->
189 setCurrentIndex (Completer_->completionModel ()->index (0, 0));
190 Completer_->complete ();
196 Completer_->setWidget (
this);
197 QLineEdit::focusInEvent (e);
202 if (!CategorySelector_.get ())
204 QLineEdit::contextMenuEvent (e);
208 CategorySelector_->move (e->globalPos ());
209 CategorySelector_->show ();
215 disconnect (Completer_,
225 Completer_->setWidget (
this);
226 Completer_->setCompletionMode (QCompleter::PopupCompletion);
228 SIGNAL (activated (
const QString&)),
233 QString TagsLineEdit::textUnderCursor ()
const
235 auto rxStr = Separator_;
236 rxStr.replace (
' ',
"\\s*");
240 QString wtext = text ();
241 int pos = cursorPosition () - 1;
242 int last = wtext.indexOf (rx, pos);
243 int first = wtext.lastIndexOf (rx, pos);
247 last = wtext.size ();
248 return wtext.mid (first, last - first);
The CategorySelector widget provides a way to select amongst a group of items.