1 /* filter_expression_frame.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #include "filter_expression_frame.h"
11 #include <ui_filter_expression_frame.h>
13 #include <epan/filter_expressions.h>
14 #include <ui/preference_utils.h>
16 #include <ui/qt/models/uat_model.h>
17 #include <ui/qt/models/pref_models.h>
18 #include <ui/qt/main_application.h>
20 #include <QPushButton>
24 // - Add the ability to edit current expressions.
26 FilterExpressionFrame::FilterExpressionFrame(QWidget
*parent
) :
27 AccordionFrame(parent
),
28 ui(new Ui::FilterExpressionFrame
)
33 foreach (QWidget
*w
, findChildren
<QWidget
*>()) {
34 w
->setAttribute(Qt::WA_MacSmallSize
, true);
42 FilterExpressionFrame::~FilterExpressionFrame()
47 void FilterExpressionFrame::addExpression(const QString filter_text
)
50 on_buttonBox_rejected();
55 ui
->displayFilterLineEdit
->setText(filter_text
);
61 void FilterExpressionFrame::editExpression(int exprIdx
)
65 ui
->labelLineEdit
->clear();
66 ui
->displayFilterLineEdit
->clear();
67 ui
->commentLineEdit
->clear();
71 UatModel
* uatModel
= new UatModel(this, "Display expressions");
72 if (! uatModel
->index(exprIdx
, 1).isValid())
75 editExpression_
= exprIdx
;
77 ui
->labelLineEdit
->setText(uatModel
->data(uatModel
->index(exprIdx
, 1), Qt::DisplayRole
).toString());
78 ui
->displayFilterLineEdit
->setText(uatModel
->data(uatModel
->index(exprIdx
, 2), Qt::DisplayRole
).toString());
79 ui
->commentLineEdit
->setText(uatModel
->data(uatModel
->index(exprIdx
, 3), Qt::DisplayRole
).toString());
87 void FilterExpressionFrame::showEvent(QShowEvent
*event
)
89 ui
->labelLineEdit
->setFocus();
90 ui
->labelLineEdit
->selectAll();
92 AccordionFrame::showEvent(event
);
95 void FilterExpressionFrame::updateWidgets()
97 bool ok_enable
= true;
99 if (ui
->labelLineEdit
->text().isEmpty() ||
100 ((ui
->displayFilterLineEdit
->syntaxState() != SyntaxLineEdit::Valid
) &&
101 (ui
->displayFilterLineEdit
->syntaxState() != SyntaxLineEdit::Deprecated
)))
104 ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(ok_enable
);
107 void FilterExpressionFrame::on_filterExpressionPreferencesPushButton_clicked()
109 on_buttonBox_rejected();
110 emit
showPreferencesDialog(PrefsModel::typeToString(PrefsModel::FilterButtons
));
113 void FilterExpressionFrame::on_labelLineEdit_textChanged(const QString
)
118 void FilterExpressionFrame::on_displayFilterLineEdit_textChanged(const QString
)
123 void FilterExpressionFrame::on_buttonBox_accepted()
125 QByteArray label_ba
= ui
->labelLineEdit
->text().toUtf8();
126 QByteArray expr_ba
= ui
->displayFilterLineEdit
->text().toUtf8();
127 QByteArray comment_ba
= ui
->commentLineEdit
->text().toUtf8();
129 if (ui
->labelLineEdit
->text().length() == 0 || ui
->displayFilterLineEdit
->text().length() == 0)
132 if (! ui
->displayFilterLineEdit
->checkFilter())
135 if (editExpression_
>= 0)
137 UatModel
* uatModel
= new UatModel(this, "Display expressions");
138 if (! uatModel
->index(editExpression_
, 1).isValid())
141 uatModel
->setData(uatModel
->index(editExpression_
, 1), QVariant::fromValue(label_ba
));
142 uatModel
->setData(uatModel
->index(editExpression_
, 2), QVariant::fromValue(expr_ba
));
143 uatModel
->setData(uatModel
->index(editExpression_
, 3), QVariant::fromValue(comment_ba
));
147 filter_expression_new(label_ba
.constData(), expr_ba
.constData(), comment_ba
.constData(), true);
150 save_migrated_uat("Display expressions", &prefs
.filter_expressions_old
);
151 on_buttonBox_rejected();
152 emit
filterExpressionsChanged();
155 void FilterExpressionFrame::on_buttonBox_rejected()
157 ui
->labelLineEdit
->clear();
158 ui
->displayFilterLineEdit
->clear();
159 ui
->commentLineEdit
->clear();
160 editExpression_
= -1;
164 void FilterExpressionFrame::keyPressEvent(QKeyEvent
*event
)
166 if (event
->modifiers() == Qt::NoModifier
) {
167 if (event
->key() == Qt::Key_Escape
) {
168 on_buttonBox_rejected();
169 } else if (event
->key() == Qt::Key_Enter
|| event
->key() == Qt::Key_Return
) {
170 if (ui
->buttonBox
->button(QDialogButtonBox::Ok
)->isEnabled()) {
171 on_buttonBox_accepted();
172 } else if (ui
->labelLineEdit
->text().length() == 0) {
173 mainApp
->pushStatus(MainApplication::FilterSyntax
, tr("Missing label."));
174 } else if (ui
->displayFilterLineEdit
->syntaxState() == SyntaxLineEdit::Empty
) {
175 mainApp
->pushStatus(MainApplication::FilterSyntax
, tr("Missing filter expression."));
176 } else if (ui
->displayFilterLineEdit
->syntaxState() != SyntaxLineEdit::Valid
) {
177 mainApp
->pushStatus(MainApplication::FilterSyntax
, tr("Invalid filter expression."));
182 AccordionFrame::keyPressEvent(event
);