aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/display_filter_edit.cpp
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2015-05-19 10:18:54 -0700
committerGerald Combs <gerald@wireshark.org>2015-05-20 03:03:58 +0000
commitaf054591c31fdccd8661c87bb3d28cdeb3077db3 (patch)
treea115ca9125de66b8764c6ce3e284cee4c40dd021 /ui/qt/display_filter_edit.cpp
parentfd985194f590225d4a41a8291cf9afae12f1b013 (diff)
Add capture filter autocompletion.
Autocomplete on recent and saved capture filters along with the primitives in gencode.l in the libpcap sources. Move common autocomplete code to SyntaxLineEdit. Change-Id: I0931a6775bacf9c917c294befbbdaade51d19b93 Reviewed-on: https://code.wireshark.org/review/8542 Petri-Dish: Gerald Combs <gerald@wireshark.org> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'ui/qt/display_filter_edit.cpp')
-rw-r--r--ui/qt/display_filter_edit.cpp143
1 files changed, 5 insertions, 138 deletions
diff --git a/ui/qt/display_filter_edit.cpp b/ui/qt/display_filter_edit.cpp
index b20b88963a..09fc1fb8d3 100644
--- a/ui/qt/display_filter_edit.cpp
+++ b/ui/qt/display_filter_edit.cpp
@@ -36,9 +36,7 @@
#include <QComboBox>
#include <QCompleter>
#include <QEvent>
-#include <QKeyEvent>
#include <QPainter>
-#include <QScrollBar>
#include <QStringListModel>
#include <QStyleOptionFrame>
#include <QToolButton>
@@ -95,22 +93,19 @@ UIMiniCancelButton::UIMiniCancelButton(QWidget *pParent /* = 0 */)
#define DEFAULT_MODIFIER "Ctrl-"
#endif
-const int max_completion_items_ = 20;
-
// proto.c:fld_abbrev_chars
static const QString fld_abbrev_chars_ = "-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
DisplayFilterEdit::DisplayFilterEdit(QWidget *parent, bool plain) :
SyntaxLineEdit(parent),
plain_(plain),
- apply_button_(NULL),
- completer_(NULL)
+ apply_button_(NULL)
{
setAccessibleName(tr("Display filter entry"));
completion_model_ = new QStringListModel(this);
- QCompleter *completer_ = new QCompleter(completion_model_, this);
- setCompleter(completer_);
+ setCompleter(new QCompleter(completion_model_, this));
+ setCompletionTokenChars(fld_abbrev_chars_);
if (plain_) {
placeholder_text_ = QString(tr("Enter a display filter %1")).arg(UTF8_HORIZONTAL_ELLIPSIS);
@@ -238,27 +233,6 @@ DisplayFilterEdit::DisplayFilterEdit(QWidget *parent, bool plain) :
);
}
-// Override setCompleter so that we don't clobber the filter text on activate.
-void DisplayFilterEdit::setCompleter(QCompleter *c)
-{
- if (completer_)
- QObject::disconnect(completer_, 0, this, 0);
-
- completer_ = c;
-
- if (!completer_)
- return;
-
- completer_->setWidget(this);
- completer_->setCompletionMode(QCompleter::PopupCompletion);
- completer_->setCaseSensitivity(Qt::CaseInsensitive);
- // Completion items are not guaranteed to be sorted (recent filters +
- // fields), so no setModelSorting.
- completer_->setMaxVisibleItems(max_completion_items_);
- QObject::connect(completer_, SIGNAL(activated(QString)),
- this, SLOT(insertFieldCompletion(QString)));
-}
-
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
void DisplayFilterEdit::paintEvent(QPaintEvent *evt) {
SyntaxLineEdit::paintEvent(evt);
@@ -308,64 +282,11 @@ void DisplayFilterEdit::resizeEvent(QResizeEvent *)
bookmark_button_->setMaximumHeight(contentsRect().height());
}
-void DisplayFilterEdit::keyPressEvent(QKeyEvent *event)
-{
- // Forward to the completer if needed...
- if (completer_ && completer_->popup()->isVisible()) {
- switch (event->key()) {
- case Qt::Key_Enter:
- case Qt::Key_Return:
- case Qt::Key_Escape:
- case Qt::Key_Tab:
- case Qt::Key_Backtab:
- event->ignore();
- return;
- default:
- break;
- }
- }
-
- // ...otherwise process the key ourselves.
- QLineEdit::keyPressEvent(event);
-
- if (!completer_) return;
-
- // Do nothing on bare shift.
- if ((event->modifiers() & Qt::ShiftModifier) && event->text().isEmpty()) return;
-
- if (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) {
- completer_->popup()->hide();
- return;
- }
-
- QPoint field_coords(getFieldUnderCursor());
-
- QString field_word = text().mid(field_coords.x(), field_coords.y());
- buildCompletionList(field_word);
-
- if (completion_model_->stringList().length() < 1) {
- completer_->popup()->hide();
- return;
- }
-
- QRect cr = cursorRect();
- cr.setWidth(completer_->popup()->sizeHintForColumn(0)
- + completer_->popup()->verticalScrollBar()->sizeHint().width());
- completer_->complete(cr);
-}
-
-void DisplayFilterEdit::focusInEvent(QFocusEvent *evt)
-{
- if (completer_)
- completer_->setWidget(this);
- SyntaxLineEdit::focusInEvent(evt);
-}
-
-void DisplayFilterEdit::focusOutEvent(QFocusEvent *evt)
+void DisplayFilterEdit::focusOutEvent(QFocusEvent *event)
{
if (syntaxState() == Valid)
emit popFilterSyntaxStatus();
- SyntaxLineEdit::focusOutEvent(evt);
+ SyntaxLineEdit::focusOutEvent(event);
}
void DisplayFilterEdit::checkFilter(const QString& text)
@@ -404,22 +325,6 @@ void DisplayFilterEdit::checkFilter(const QString& text)
}
}
-bool DisplayFilterEdit::isComplexFilter(const QString &dfilter)
-{
- bool is_complex = false;
- for (int i = 0; i < dfilter.length(); i++) {
- if (!fld_abbrev_chars_.contains(dfilter.at(i))) {
- is_complex = true;
- break;
- }
- }
- // Don't complete the current filter.
- if (is_complex && dfilter.startsWith(text()) && dfilter.compare(text())) {
- return true;
- }
- return false;
-}
-
// GTK+ behavior:
// - Operates on words (proto.c:fld_abbrev_chars).
// - Popup appears when you enter or remove text.
@@ -563,44 +468,6 @@ void DisplayFilterEdit::changeEvent(QEvent* event)
SyntaxLineEdit::changeEvent(event);
}
-void DisplayFilterEdit::insertFieldCompletion(const QString &completion_text)
-{
- QCompleter *completer_ = completer();
- if (!completer_) return;
-
- QPoint field_coords(getFieldUnderCursor());
-
- // Insert only if we have a matching field or if the entry is empty
- if (field_coords.y() < 1 && !text().isEmpty()) {
- completer_->popup()->hide();
- return;
- }
-
- QString new_text = text().replace(field_coords.x(), field_coords.y(), completion_text);
- setText(new_text);
- setCursorPosition(field_coords.x() + completion_text.length());
-}
-
-QPoint DisplayFilterEdit::getFieldUnderCursor()
-{
- if (selectionStart() >= 0) return (QPoint(0,0));
-
- int pos = cursorPosition();
- int start = pos;
- int len = 0;
-
- while (start > 0 && fld_abbrev_chars_.contains(text().at(start -1))) {
- start--;
- len++;
- }
- while (pos < text().length() && fld_abbrev_chars_.contains(text().at(pos))) {
- pos++;
- len++;
- }
-
- return QPoint(start, len);
-}
-
/*
* Editor modelines
*