Skip to content

Commit

Permalink
Qt: Add dfilter error location to tooltip
Browse files Browse the repository at this point in the history
If display filter compilation fails and the expression has a syntax
error and associated location, it will be displayed in the filter
tooltip, using a textual underline.

As far as I can tell using a graphical squiggly underline seems extremely
difficult for an object inheriting from QLineEdit, so the tooltip
textual method was used instead.
  • Loading branch information
randstr authored and A Wireshark GitLab Utility committed Aug 22, 2022
1 parent cbe4cd9 commit 6b35aa0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 1 addition & 2 deletions ui/qt/widgets/display_filter_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,8 @@ void DisplayFilterEdit::checkFilter(const QString& filter_text)
}
case Invalid:
{
QString invalidMsg = tr("Invalid filter: ").append(syntaxErrorMessage());
mainApp->pushStatus(MainApplication::FilterSyntax, syntaxErrorMessage());
setToolTip(invalidMsg);
setToolTip(syntaxErrorMessageFull());
break;
}
default:
Expand Down
29 changes: 27 additions & 2 deletions ui/qt/widgets/syntax_line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,33 @@ void SyntaxLineEdit::setSyntaxState(SyntaxState state) {
setStyleSheet(style_sheet_);
}

QString SyntaxLineEdit::syntaxErrorMessage() {
QString SyntaxLineEdit::syntaxErrorMessage()
{
return syntax_error_message_;
}

QString SyntaxLineEdit::syntaxErrorMessageFull()
{
return syntax_error_message_full_;
}

QString SyntaxLineEdit::createSyntaxErrorMessageFull(
const QString &filter, const QString &err_msg,
qsizetype loc_start, size_t loc_length)
{
QString msg = tr("Invalid filter: %1").arg(err_msg);

if (loc_start >= 0 && loc_length >= 1) {
// Add underlined location
msg = QString("<p>%1<pre> %2\n %3^%4</pre></p>")
.arg(msg)
.arg(filter)
.arg(QString(' ').repeated(loc_start))
.arg(QString('~').repeated(loc_length - 1));
}
return msg;
}

QString SyntaxLineEdit::styleSheet() const {
return style_sheet_;
}
Expand Down Expand Up @@ -179,7 +202,8 @@ bool SyntaxLineEdit::checkDisplayFilter(QString filter)

dfilter_t *dfp = NULL;
gchar *err_msg;
if (dfilter_compile(filter.toUtf8().constData(), &dfp, &err_msg)) {
dfilter_loc_t loc;
if (dfilter_compile2(filter.toUtf8().constData(), &dfp, &err_msg, &loc)) {
GPtrArray *depr = NULL;
if (dfp) {
depr = dfilter_deprecated_tokens(dfp);
Expand Down Expand Up @@ -208,6 +232,7 @@ bool SyntaxLineEdit::checkDisplayFilter(QString filter)
} else {
setSyntaxState(SyntaxLineEdit::Invalid);
syntax_error_message_ = QString::fromUtf8(err_msg);
syntax_error_message_full_ = createSyntaxErrorMessageFull(filter, syntax_error_message_, loc.col_start, loc.col_len);
g_free(err_msg);
}
dfilter_free(dfp);
Expand Down
7 changes: 7 additions & 0 deletions ui/qt/widgets/syntax_line_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ class SyntaxLineEdit : public QLineEdit
SyntaxState syntaxState() const { return syntax_state_; }
void setSyntaxState(SyntaxState state = Empty);
QString syntaxErrorMessage();
// Error message with filter expression and location error.
QString syntaxErrorMessageFull();
QString styleSheet() const;
QString deprecatedToken();

void setCompleter(QCompleter *c);
QCompleter *completer() const { return completer_; }
void allowCompletion(bool enabled);

static QString createSyntaxErrorMessageFull(const QString &filter,
const QString &err_msg,
qsizetype loc_start, size_t loc_length);

public slots:
void setStyleSheet(const QString &style_sheet);
// Insert filter text at the current position, adding spaces where needed.
Expand Down Expand Up @@ -69,6 +75,7 @@ public slots:
QString style_sheet_;
QString state_style_sheet_;
QString syntax_error_message_;
QString syntax_error_message_full_;
QString token_chars_;
bool completion_enabled_;

Expand Down

0 comments on commit 6b35aa0

Please sign in to comment.