wireshark/ui/qt/filter_expression_frame.cpp

183 lines
4.9 KiB
C++
Raw Normal View History

/* filter_expression_frame.cpp
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "filter_expression_frame.h"
#include <ui_filter_expression_frame.h>
#include <epan/filter_expressions.h>
#include <epan/uat-int.h>
#include <ui/preference_utils.h>
#include <ui/qt/uat_model.h>
#include <QPushButton>
// To do:
// - Add the ability to edit current expressions.
FilterExpressionFrame::FilterExpressionFrame(QWidget *parent) :
AccordionFrame(parent),
ui(new Ui::FilterExpressionFrame)
{
ui->setupUi(this);
#ifdef Q_OS_MAC
foreach (QWidget *w, findChildren<QWidget *>()) {
w->setAttribute(Qt::WA_MacSmallSize, true);
}
#endif
editExpression_ = -1;
}
FilterExpressionFrame::~FilterExpressionFrame()
{
delete ui;
}
void FilterExpressionFrame::addExpression(const QString filter_text)
{
if (isVisible()) {
on_buttonBox_rejected();
return;
}
editExpression_ = -1;
ui->displayFilterLineEdit->setText(filter_text);
}
void FilterExpressionFrame::editExpression(int exprIdx)
{
if (isVisible())
{
ui->labelLineEdit->clear();
ui->displayFilterLineEdit->clear();
ui->commentLineEdit->clear();
editExpression_ = -1;
}
UatModel * uatModel = new UatModel(this, "Display expressions");
if ( ! uatModel->index(exprIdx, 1).isValid() )
return;
editExpression_ = exprIdx;
ui->labelLineEdit->setText(uatModel->data(uatModel->index(exprIdx, 1), Qt::DisplayRole).toString());
ui->displayFilterLineEdit->setText(uatModel->data(uatModel->index(exprIdx, 2), Qt::DisplayRole).toString());
ui->commentLineEdit->setText(uatModel->data(uatModel->index(exprIdx, 3), Qt::DisplayRole).toString());
delete(uatModel);
if (! isVisible())
animatedShow();
}
void FilterExpressionFrame::showEvent(QShowEvent *event)
{
ui->labelLineEdit->setFocus();
ui->labelLineEdit->selectAll();
AccordionFrame::showEvent(event);
}
void FilterExpressionFrame::updateWidgets()
{
bool ok_enable = true;
if (ui->labelLineEdit->text().isEmpty() || ui->displayFilterLineEdit->text().isEmpty())
ok_enable = false;
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok_enable);
}
void FilterExpressionFrame::on_filterExpressionPreferencesPushButton_clicked()
{
on_buttonBox_rejected();
emit showPreferencesDialog(PreferencesDialog::ppFilterExpressions);
}
void FilterExpressionFrame::on_labelLineEdit_textChanged(const QString)
{
updateWidgets();
}
void FilterExpressionFrame::on_displayFilterLineEdit_textChanged(const QString)
{
updateWidgets();
}
void FilterExpressionFrame::on_buttonBox_accepted()
{
gchar* err = NULL;
QByteArray label_ba = ui->labelLineEdit->text().toUtf8();
QByteArray expr_ba = ui->displayFilterLineEdit->text().toUtf8();
QByteArray comment_ba = ui->commentLineEdit->text().toUtf8();
if ( ui->labelLineEdit->text().length() == 0 || ui->displayFilterLineEdit->text().length() == 0 )
return;
if ( ! ui->displayFilterLineEdit->checkFilter() )
return;
if ( editExpression_ >= 0 )
{
UatModel * uatModel = new UatModel(this, "Display expressions");
if ( ! uatModel->index(editExpression_, 1).isValid() )
return;
uatModel->setData(uatModel->index(editExpression_, 1), QVariant::fromValue(label_ba));
uatModel->setData(uatModel->index(editExpression_, 2), QVariant::fromValue(expr_ba));
uatModel->setData(uatModel->index(editExpression_, 3), QVariant::fromValue(comment_ba));
}
else
{
filter_expression_new(label_ba.constData(), expr_ba.constData(), comment_ba.constData(), TRUE);
}
uat_save(uat_get_table_by_name("Display expressions"), &err);
on_buttonBox_rejected();
emit filterExpressionsChanged();
g_free(err);
}
void FilterExpressionFrame::on_buttonBox_rejected()
{
ui->labelLineEdit->clear();
ui->displayFilterLineEdit->clear();
ui->commentLineEdit->clear();
editExpression_ = -1;
animatedHide();
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/