34 #include <QContextMenuEvent> 35 #include <QHBoxLayout> 36 #include <QPushButton> 37 #include <QToolButton> 38 #include <QAbstractItemView> 57 CategorySelector_->SetSeparator (Separator_);
58 CategorySelector_->hide ();
60 QAbstractItemModel *model = Completer_->model ();
62 if (model->metaObject ()->indexOfSignal (QMetaObject::normalizedSignature (
"tagsUpdated (QStringList)")) >= 0)
64 SIGNAL (tagsUpdated (QStringList)),
68 QStringList initialTags;
69 for (
int i = 0; i < model->rowCount (); ++i)
70 initialTags << model->data (model->index (i, 0)).
toString ();
73 connect (CategorySelector_.get (),
74 SIGNAL (tagsSelectionChanged (
const QStringList&)),
76 SLOT (handleSelectionChanged (
const QStringList&)));
79 SIGNAL (textChanged (
const QString&)),
80 CategorySelector_.get (),
81 SLOT (lineTextChanged (
const QString&)));
86 auto button =
new QToolButton {
this };
87 button->setIconSize ({ 16, 16 });
88 button->setIcon (QIcon::fromTheme (
"mail-tagged"));
89 button->setCursor (Qt::ArrowCursor);
90 button->setStyleSheet (
"QToolButton { border: none; padding: 0px; }");
97 SLOT (showSelector ()));
108 if (CategorySelector_)
109 CategorySelector_->SetSeparator (sep);
114 if (Completer_->widget () !=
this)
117 QString wtext = text ();
118 if (completion.startsWith (wtext))
120 int pos = wtext.lastIndexOf (Separator_);
122 wtext = wtext.left (pos).append (Separator_);
125 wtext.append (completion);
126 wtext = wtext.simplified ();
134 CategorySelector_->setPossibleSelections (tags);
139 setText (tags.join (Separator_));
140 if (CategorySelector_.get ())
141 CategorySelector_->SetSelections (tags);
144 void TagsLineEdit::handleSelectionChanged (
const QStringList& tags)
146 setText (tags.join (Separator_));
151 void TagsLineEdit::showSelector ()
153 CategorySelector_->move (QCursor::pos ());
154 CategorySelector_->show ();
159 if (Completer_ && Completer_->popup ()->isVisible ())
166 case Qt::Key_Backtab:
173 QLineEdit::keyPressEvent (e);
175 bool cos = e->modifiers () & (Qt::ControlModifier |
179 bool isShortcut = e->modifiers () & (Qt::ControlModifier |
183 (cos && e->text ().isEmpty ()) ||
187 QString completionPrefix = textUnderCursor ();
188 Completer_->setCompletionPrefix (completionPrefix);
189 Completer_->popup ()->
190 setCurrentIndex (Completer_->completionModel ()->index (0, 0));
191 Completer_->complete ();
197 Completer_->setWidget (
this);
198 QLineEdit::focusInEvent (e);
203 if (!CategorySelector_.get ())
205 QLineEdit::contextMenuEvent (e);
209 CategorySelector_->move (e->globalPos ());
210 CategorySelector_->show ();
216 disconnect (Completer_,
226 Completer_->setWidget (
this);
227 Completer_->setCompletionMode (QCompleter::PopupCompletion);
229 SIGNAL (activated (
const QString&)),
234 QString TagsLineEdit::textUnderCursor ()
const 236 auto rxStr = Separator_;
237 rxStr.replace (
' ',
"\\s*");
241 QString wtext = text ();
242 int pos = cursorPosition () - 1;
243 int last = wtext.indexOf (rx, pos);
244 int first = wtext.lastIndexOf (rx, pos);
248 last = wtext.size ();
249 return wtext.mid (first, last - first);
char * toString(const char *name, const T &t)
The CategorySelector widget provides a way to select amongst a group of items.