From 862e2acdd94ca38e9310f4ec9509a1108346f2bc Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Thu, 29 Aug 2019 13:26:18 +0200 Subject: Qt: Extend filterbutton context menu The filterbutton context menu allows for appending the filter button expression to an already existing display filter and copying the filterbutton filter expression to the clipboard Bug: 15980 Change-Id: I7b24007e597b9a9800729339926378d0ff264c73 Reviewed-on: https://code.wireshark.org/review/34394 Petri-Dish: Roland Knall Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall --- ui/qt/filter_action.cpp | 24 ++++++++++++ ui/qt/filter_action.h | 2 + ui/qt/widgets/display_filter_edit.cpp | 60 ++--------------------------- ui/qt/widgets/display_filter_edit.h | 4 -- ui/qt/widgets/filter_expression_toolbar.cpp | 18 ++++++--- 5 files changed, 43 insertions(+), 65 deletions(-) diff --git a/ui/qt/filter_action.cpp b/ui/qt/filter_action.cpp index 97602f1fcc..ca884a8e95 100644 --- a/ui/qt/filter_action.cpp +++ b/ui/qt/filter_action.cpp @@ -255,6 +255,30 @@ void FilterAction::groupTriggered(QAction * action) } } +QAction * FilterAction::copyFilterAction(QString filter, QWidget *par) +{ + FilterAction * filterAction = new FilterAction(par, ActionCopy); + QAction * action = new QAction(QObject::tr("Copy"), par); + action->setProperty("filter", qVariantFromValue(filter)); + connect(action, &QAction::triggered, filterAction, &FilterAction::copyActionTriggered); + + if ( filter.isEmpty() ) + action->setEnabled(false); + + return action; +} + +void FilterAction::copyActionTriggered() +{ + QAction * sendAction = qobject_cast(sender()); + if ( ! sendAction ) + return; + + QString filter = sendAction->property("filter").toString(); + if ( filter.length() > 0 ) + wsApp->clipboard()->setText(filter); +} + /* * Editor modelines * diff --git a/ui/qt/filter_action.h b/ui/qt/filter_action.h index fe640cbe82..a3a4c2d5ee 100644 --- a/ui/qt/filter_action.h +++ b/ui/qt/filter_action.h @@ -74,6 +74,7 @@ public: static QActionGroup * createFilterGroup(QString filter, bool prepare, bool enabled, QWidget * parent); static QMenu * createFilterMenu(FilterAction::Action act, QString filter, bool enabled, QWidget * parent); + static QAction * copyFilterAction(QString filter, QWidget *par); signals: @@ -86,6 +87,7 @@ private: private slots: void groupTriggered(QAction *); + void copyActionTriggered(); }; diff --git a/ui/qt/widgets/display_filter_edit.cpp b/ui/qt/widgets/display_filter_edit.cpp index 50fe74f5a0..6308305597 100644 --- a/ui/qt/widgets/display_filter_edit.cpp +++ b/ui/qt/widgets/display_filter_edit.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "wireshark_application.h" #include @@ -615,63 +616,10 @@ void DisplayFilterEdit::createFilterTextDropMenu(QDropEvent *event, bool prepare if ( filterText.isEmpty() ) return; - QMenu applyMenu(this); + FilterAction::Action filterAct = prepare ? FilterAction::ActionPrepare : FilterAction::ActionApply; + QMenu * applyMenu = FilterAction::createFilterMenu(filterAct, filterText, true, this); - QAction * selAction = applyMenu.addAction(tr("Selected")); - selAction->setData(QString("%1").arg(filterText)); - selAction->setProperty("clear", qVariantFromValue(true)); - connect(selAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - - QAction * notSelAction = applyMenu.addAction(tr("Not Selected")); - notSelAction->setData(QString("!(%1)").arg(filterText)); - notSelAction->setProperty("clear", qVariantFromValue(true)); - connect(notSelAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - - if ( this->text().length() > 0 ) - { - QAction * andAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "and Selected")); - andAction->setData(QString("&& %1").arg(filterText)); - connect(andAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - - QAction * orAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "or Selected")); - orAction->setData(QString("|| %1").arg(filterText)); - connect(orAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - - QAction * andNotAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "and not Selected")); - andNotAction->setData(QString("&& !(%1)").arg(filterText)); - connect(andNotAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - - QAction * orNotAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "or not Selected")); - orNotAction->setData(QString("|| !(%1)").arg(filterText)); - connect(orNotAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - } - - foreach ( QAction * action, applyMenu.actions() ) - action->setProperty("prepare", qVariantFromValue(prepare)); - - applyMenu.exec(this->mapToGlobal(event->pos())); - -} - -void DisplayFilterEdit::dropActionMenuEvent() -{ - QAction * sendAction = qobject_cast(sender()); - if ( ! sendAction ) - return; - - QString value = sendAction->data().toString(); - bool prepare = sendAction->property("prepare").toBool(); - - QString filterText; - if ( sendAction->property("clear").toBool() ) - filterText = value; - else - filterText = QString("((%1) %2)").arg(this->text()).arg(value); - setText(filterText); - - // Holding down the Shift key will only prepare filter. - if ( ! prepare ) - applyDisplayFilter(); + applyMenu->exec(this->mapToGlobal(event->pos())); } /* diff --git a/ui/qt/widgets/display_filter_edit.h b/ui/qt/widgets/display_filter_edit.h index 85ea801433..7e5bcd68bb 100644 --- a/ui/qt/widgets/display_filter_edit.h +++ b/ui/qt/widgets/display_filter_edit.h @@ -77,10 +77,6 @@ signals: void filterPackets(QString new_filter, bool force); void showPreferencesDialog(QString pane_name); -private slots: - - void dropActionMenuEvent(); - }; #endif // DISPLAYFILTEREDIT_H diff --git a/ui/qt/widgets/filter_expression_toolbar.cpp b/ui/qt/widgets/filter_expression_toolbar.cpp index 89cf3b648f..b12c508cd5 100644 --- a/ui/qt/widgets/filter_expression_toolbar.cpp +++ b/ui/qt/widgets/filter_expression_toolbar.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -70,11 +71,11 @@ void FilterExpressionToolBar::onCustomMenuHandler(const QPoint& pos) QMenu * filterMenu = new QMenu(this); - QAction *actFilter = filterMenu->addAction(tr("Filter Button Preferences...")); - connect(actFilter, &QAction::triggered, this, &FilterExpressionToolBar::toolBarShowPreferences); - actFilter->setProperty(dfe_property_label_, filterAction->property(dfe_property_label_)); - actFilter->setProperty(dfe_property_expression_, filterAction->property(dfe_property_expression_)); - actFilter->setData(filterAction->data()); + QString filterText = filterAction->property(dfe_property_expression_).toString(); + filterMenu->addMenu(FilterAction::createFilterMenu(FilterAction::ActionApply, filterText, true, this)); + filterMenu->addMenu(FilterAction::createFilterMenu(FilterAction::ActionPrepare, filterText, true, this)); + filterMenu->addSeparator(); + filterMenu->addAction(FilterAction::copyFilterAction(filterText, this)); filterMenu->addSeparator(); QAction * actEdit = filterMenu->addAction(tr("Edit")); connect(actEdit, &QAction::triggered, this, &FilterExpressionToolBar::editFilter); @@ -91,6 +92,13 @@ void FilterExpressionToolBar::onCustomMenuHandler(const QPoint& pos) actRemove->setProperty(dfe_property_label_, filterAction->property(dfe_property_label_)); actRemove->setProperty(dfe_property_expression_, filterAction->property(dfe_property_expression_)); actRemove->setData(filterAction->data()); + filterMenu->addSeparator(); + QAction *actFilter = filterMenu->addAction(tr("Filter Button Preferences...")); + connect(actFilter, &QAction::triggered, this, &FilterExpressionToolBar::toolBarShowPreferences); + actFilter->setProperty(dfe_property_label_, filterAction->property(dfe_property_label_)); + actFilter->setProperty(dfe_property_expression_, filterAction->property(dfe_property_expression_)); + actFilter->setData(filterAction->data()); + filterMenu->exec(mapToGlobal(pos)); } -- cgit v1.2.3