13 #include <QContextMenuEvent> 14 #include <QHBoxLayout> 15 #include <QPushButton> 16 #include <QToolButton> 17 #include <QAbstractItemView> 42 auto button =
new QToolButton {
this };
43 button->setIconSize ({ 16, 16 });
44 button->setIcon (QIcon::fromTheme (QStringLiteral (
"mail-tagged")));
45 button->setCursor (Qt::ArrowCursor);
46 button->setStyleSheet (QStringLiteral (
"QToolButton { border: none; padding: 0px; }"));
51 &QToolButton::clicked,
55 selector->move (QCursor::pos ());
62 CategorySelector_ = selector;
65 QAbstractItemModel *model = Completer_->model ();
67 QStringList initialTags;
68 for (
int i = 0; i < model->rowCount (); ++i)
69 initialTags << model->data (model->index (i, 0)).
toString ();
72 connect (CategorySelector_,
75 [
this] (
const QStringList& tags)
77 setText (tags.join (Separator_));
82 &QLineEdit::textChanged,
95 if (CategorySelector_)
99 void TagsLineEdit::InsertTag (
const QString& completion)
101 if (Completer_->widget () !=
this)
104 QString wtext = text ();
105 if (completion.startsWith (wtext))
107 int pos = wtext.lastIndexOf (Separator_);
109 wtext = wtext.left (pos).append (Separator_);
112 wtext.append (completion);
113 wtext = wtext.simplified ();
121 setText (tags.join (Separator_));
122 if (CategorySelector_)
128 if (Completer_ && Completer_->popup ()->isVisible ())
135 case Qt::Key_Backtab:
142 QLineEdit::keyPressEvent (e);
144 bool cos = e->modifiers () & (Qt::ControlModifier |
148 bool isShortcut = e->modifiers () & (Qt::ControlModifier |
152 (cos && e->text ().isEmpty ()) ||
156 QString completionPrefix = textUnderCursor ();
157 Completer_->setCompletionPrefix (completionPrefix);
158 Completer_->popup ()->
159 setCurrentIndex (Completer_->completionModel ()->index (0, 0));
160 Completer_->complete ();
166 Completer_->setWidget (
this);
167 QLineEdit::focusInEvent (e);
172 if (!CategorySelector_ || CategorySelector_->parentWidget () !=
this)
174 QLineEdit::contextMenuEvent (e);
178 CategorySelector_->move (e->globalPos ());
179 CategorySelector_->show ();
185 disconnect (Completer_,
195 Completer_->setWidget (
this);
196 Completer_->setCompletionMode (QCompleter::PopupCompletion);
198 qOverload<const QString&> (&QCompleter::activated),
200 &TagsLineEdit::InsertTag);
203 QString TagsLineEdit::textUnderCursor ()
const 205 auto rxStr = Separator_;
206 rxStr.replace (
' ', R
"(\s*)"_ql); 210 QString wtext = text (); 211 int pos = cursorPosition () - 1;
212 int last = wtext.indexOf (rx, pos);
213 int first = wtext.lastIndexOf (rx, pos);
217 last = wtext.size ();
218 return wtext.mid (first, last - first);
QString GetDefaultTagsSeparator()
void tagsSelectionChanged(const QStringList &newSelections)
Indicates that selections have changed.
void SetSelectionsFromString(const QString &newText)
Notifies CategorySelector about logical selection changes.
void SetSeparator(const QString &)
Sets the separator for the tags.
void SetSelections(const QStringList &subset)
Selects some of the items.
char * toString(const PKey< T, Args... > &pkey)
The CategorySelector widget provides a way to select amongst a group of items.
virtual void SetPossibleSelections(QStringList selections, bool sort=true)
Sets possible selections.