update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / ui / qt / filter_expression_frame.cpp
blobdd40b3faaf8a2418e7e2b46ef4fd6c8aa157b1da
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
8 */
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>
21 #include <QKeyEvent>
23 // To do:
24 // - Add the ability to edit current expressions.
26 FilterExpressionFrame::FilterExpressionFrame(QWidget *parent) :
27 AccordionFrame(parent),
28 ui(new Ui::FilterExpressionFrame)
30 ui->setupUi(this);
32 #ifdef Q_OS_MAC
33 foreach (QWidget *w, findChildren<QWidget *>()) {
34 w->setAttribute(Qt::WA_MacSmallSize, true);
36 #endif
38 editExpression_ = -1;
39 updateWidgets();
42 FilterExpressionFrame::~FilterExpressionFrame()
44 delete ui;
47 void FilterExpressionFrame::addExpression(const QString filter_text)
49 if (isVisible()) {
50 on_buttonBox_rejected();
51 return;
54 editExpression_ = -1;
55 ui->displayFilterLineEdit->setText(filter_text);
57 if (! isVisible())
58 animatedShow();
61 void FilterExpressionFrame::editExpression(int exprIdx)
63 if (isVisible())
65 ui->labelLineEdit->clear();
66 ui->displayFilterLineEdit->clear();
67 ui->commentLineEdit->clear();
68 editExpression_ = -1;
71 UatModel * uatModel = new UatModel(this, "Display expressions");
72 if (! uatModel->index(exprIdx, 1).isValid())
73 return;
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());
81 delete(uatModel);
83 if (! isVisible())
84 animatedShow();
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)))
102 ok_enable = false;
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)
115 updateWidgets();
118 void FilterExpressionFrame::on_displayFilterLineEdit_textChanged(const QString)
120 updateWidgets();
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)
130 return;
132 if (! ui->displayFilterLineEdit->checkFilter())
133 return;
135 if (editExpression_ >= 0)
137 UatModel * uatModel = new UatModel(this, "Display expressions");
138 if (! uatModel->index(editExpression_, 1).isValid())
139 return;
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));
145 else
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;
161 animatedHide();
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);