34 #include <QContextMenuEvent>
35 #include <QHBoxLayout>
36 #include <QPushButton>
40 using namespace LeechCraft::Util;
52 CategorySelector_->SetSeparator (Separator_);
53 CategorySelector_->hide ();
55 QAbstractItemModel *model = Completer_->model ();
57 if (model->metaObject ()->indexOfSignal (QMetaObject::normalizedSignature (
"tagsUpdated (QStringList)")) >= 0)
59 SIGNAL (tagsUpdated (QStringList)),
63 QStringList initialTags;
64 for (
int i = 0; i < model->rowCount (); ++i)
65 initialTags << model->data (model->index (i, 0)).toString ();
68 connect (CategorySelector_.get (),
69 SIGNAL (tagsSelectionChanged (
const QStringList&)),
71 SLOT (handleSelectionChanged (
const QStringList&)));
74 SIGNAL (textChanged (
const QString&)),
75 CategorySelector_.get (),
76 SLOT (lineTextChanged (
const QString&)));
87 if (CategorySelector_)
88 CategorySelector_->SetSeparator (sep);
93 if (Completer_->widget () !=
this)
96 QString wtext = text ();
97 if (completion.startsWith (wtext))
99 int pos = wtext.lastIndexOf (Separator_);
101 wtext = wtext.left (pos).append (Separator_);
104 wtext.append (completion);
105 wtext = wtext.simplified ();
113 CategorySelector_->setPossibleSelections (tags);
118 setText (tags.join (Separator_));
119 if (CategorySelector_.get ())
120 CategorySelector_->SetSelections (tags);
123 void TagsLineEdit::handleSelectionChanged (
const QStringList& tags)
125 setText (tags.join (Separator_));
132 if (Completer_ && Completer_->popup ()->isVisible ())
139 case Qt::Key_Backtab:
146 QLineEdit::keyPressEvent (e);
148 bool cos = e->modifiers () & (Qt::ControlModifier |
152 bool isShortcut = e->modifiers () & (Qt::ControlModifier |
156 (cos && e->text ().isEmpty ()) ||
160 QString completionPrefix = textUnderCursor ();
161 Completer_->setCompletionPrefix (completionPrefix);
162 Completer_->popup ()->
163 setCurrentIndex (Completer_->completionModel ()->index (0, 0));
164 Completer_->complete ();
170 Completer_->setWidget (
this);
171 QLineEdit::focusInEvent (e);
176 if (!CategorySelector_.get ())
178 QLineEdit::contextMenuEvent (e);
182 CategorySelector_->move (e->globalPos ());
183 CategorySelector_->show ();
189 disconnect (Completer_,
199 Completer_->setWidget (
this);
200 Completer_->setCompletionMode (QCompleter::PopupCompletion);
202 SIGNAL (activated (
const QString&)),
207 QString TagsLineEdit::textUnderCursor ()
const
209 auto rxStr = Separator_;
210 rxStr.replace (
' ',
"\\s*");
214 QString wtext = text ();
215 int pos = cursorPosition () - 1;
216 int last = wtext.indexOf (rx, pos);
217 int first = wtext.lastIndexOf (rx, pos);
221 last = wtext.size ();
222 return wtext.mid (first, last - first);
The CategorySelector widget provides a way to select amongst a group of items.