From a9fc3681f67628fd112a76b0dc5ec145cc20ef4c Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Tue, 13 Aug 2019 11:28:48 +0200 Subject: Qt: Move CopyFrom from menu to button Move the CopyFromProfile implementation from a menu to a button to ease integration in existing code Change-Id: I4fb4e952e89665eda99d162e891ac6d3516a6f02 Reviewed-on: https://code.wireshark.org/review/34266 Reviewed-by: Roland Knall Petri-Dish: Roland Knall Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- ui/qt/CMakeLists.txt | 4 +- ui/qt/coloring_rules_dialog.cpp | 14 ++- ui/qt/coloring_rules_dialog.h | 2 +- ui/qt/io_graph_dialog.cpp | 14 ++- ui/qt/io_graph_dialog.h | 2 +- ui/qt/uat_dialog.cpp | 15 ++-- ui/qt/uat_dialog.h | 2 +- ui/qt/uat_frame.cpp | 12 +-- ui/qt/uat_frame.h | 2 +- ui/qt/uat_frame.ui | 7 +- ui/qt/widgets/copy_from_profile_button.cpp | 135 +++++++++++++++++++++++++++++ ui/qt/widgets/copy_from_profile_button.h | 43 +++++++++ ui/qt/widgets/copy_from_profile_menu.cpp | 102 ---------------------- ui/qt/widgets/copy_from_profile_menu.h | 35 -------- 14 files changed, 209 insertions(+), 180 deletions(-) create mode 100644 ui/qt/widgets/copy_from_profile_button.cpp create mode 100644 ui/qt/widgets/copy_from_profile_button.h delete mode 100644 ui/qt/widgets/copy_from_profile_menu.cpp delete mode 100644 ui/qt/widgets/copy_from_profile_menu.h diff --git a/ui/qt/CMakeLists.txt b/ui/qt/CMakeLists.txt index 8772dfd5ad..ea68c240b8 100644 --- a/ui/qt/CMakeLists.txt +++ b/ui/qt/CMakeLists.txt @@ -16,7 +16,7 @@ set(WIRESHARK_WIDGET_HEADERS widgets/capture_filter_combo.h widgets/capture_filter_edit.h widgets/clickable_label.h - widgets/copy_from_profile_menu.h + widgets/copy_from_profile_button.h widgets/display_filter_combo.h widgets/display_filter_edit.h widgets/dissector_tables_view.h @@ -245,7 +245,7 @@ set(WIRESHARK_WIDGET_SRCS widgets/capture_filter_combo.cpp widgets/capture_filter_edit.cpp widgets/clickable_label.cpp - widgets/copy_from_profile_menu.cpp + widgets/copy_from_profile_button.cpp widgets/display_filter_combo.cpp widgets/display_filter_edit.cpp widgets/dissector_tables_view.cpp diff --git a/ui/qt/coloring_rules_dialog.cpp b/ui/qt/coloring_rules_dialog.cpp index cfe997175c..87d886d6e6 100644 --- a/ui/qt/coloring_rules_dialog.cpp +++ b/ui/qt/coloring_rules_dialog.cpp @@ -22,7 +22,7 @@ #include "wireshark_application.h" #include "ui/qt/utils/qt_ui_utils.h" -#include "ui/qt/widgets/copy_from_profile_menu.h" +#include "ui/qt/widgets/copy_from_profile_button.h" #include "ui/qt/widgets/wireshark_file_dialog.h" #include @@ -90,12 +90,9 @@ ColoringRulesDialog::ColoringRulesDialog(QWidget *parent, QString add_filter) : export_button_ = ui->buttonBox->addButton(tr("Export" UTF8_HORIZONTAL_ELLIPSIS), QDialogButtonBox::ApplyRole); export_button_->setToolTip(tr("Save filters in a file.")); - QPushButton *copy_button = ui->buttonBox->addButton(tr("Copy from"), QDialogButtonBox::ActionRole); - CopyFromProfileMenu *copy_from_menu = new CopyFromProfileMenu(COLORFILTERS_FILE_NAME, copy_button); - copy_button->setMenu(copy_from_menu); - copy_button->setToolTip(tr("Copy coloring rules from another profile.")); - copy_button->setEnabled(copy_from_menu->haveProfiles()); - connect(copy_from_menu, SIGNAL(triggered(QAction *)), this, SLOT(copyFromProfile(QAction *))); + CopyFromProfileButton * copy_button = new CopyFromProfileButton(this, COLORFILTERS_FILE_NAME, tr("Copy coloring rules from another profile.")); + ui->buttonBox->addButton(copy_button, QDialogButtonBox::ActionRole); + connect(copy_button, &CopyFromProfileButton::copyProfile, this, &ColoringRulesDialog::copyFromProfile); QString abs_path = gchar_free_to_qstring(get_persconffile_path(COLORFILTERS_FILE_NAME, TRUE)); if (file_exists(abs_path.toUtf8().constData())) { @@ -141,9 +138,8 @@ void ColoringRulesDialog::checkUnknownColorfilters() } } -void ColoringRulesDialog::copyFromProfile(QAction *action) +void ColoringRulesDialog::copyFromProfile(QString filename) { - QString filename = action->data().toString(); QString err; if (!colorRuleModel_.importColors(filename, err)) { diff --git a/ui/qt/coloring_rules_dialog.h b/ui/qt/coloring_rules_dialog.h index 5678354f5b..06b09b4498 100644 --- a/ui/qt/coloring_rules_dialog.h +++ b/ui/qt/coloring_rules_dialog.h @@ -39,7 +39,7 @@ protected: void showEvent(QShowEvent *); private slots: - void copyFromProfile(QAction *action); + void copyFromProfile(QString fileName); void colorRuleSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected); void on_fGPushButton_clicked(); void on_bGPushButton_clicked(); diff --git a/ui/qt/io_graph_dialog.cpp b/ui/qt/io_graph_dialog.cpp index 28409b655e..a91c3b3246 100644 --- a/ui/qt/io_graph_dialog.cpp +++ b/ui/qt/io_graph_dialog.cpp @@ -29,7 +29,7 @@ #include #include //provides some default colors -#include +#include #include "ui/qt/widgets/wireshark_file_dialog.h" #include @@ -326,12 +326,9 @@ IOGraphDialog::IOGraphDialog(QWidget &parent, CaptureFile &cf) : QPushButton *copy_bt = ui->buttonBox->addButton(tr("Copy"), QDialogButtonBox::ActionRole); connect (copy_bt, SIGNAL(clicked()), this, SLOT(copyAsCsvClicked())); - QPushButton *copy_from_bt = ui->buttonBox->addButton(tr("Copy from"), QDialogButtonBox::ActionRole); - CopyFromProfileMenu *copy_from_menu = new CopyFromProfileMenu("io_graphs", copy_from_bt); - copy_from_bt->setMenu(copy_from_menu); - copy_from_bt->setToolTip(tr("Copy graphs from another profile.")); - copy_from_bt->setEnabled(copy_from_menu->haveProfiles()); - connect(copy_from_menu, SIGNAL(triggered(QAction *)), this, SLOT(copyFromProfile(QAction *))); + CopyFromProfileButton * copy_button = new CopyFromProfileButton(this, "io_graphs", tr("Copy graphs from another profile.")); + ui->buttonBox->addButton(copy_button, QDialogButtonBox::ActionRole); + connect(copy_button, &CopyFromProfileButton::copyProfile, this, &IOGraphDialog::copyFromProfile); QPushButton *close_bt = ui->buttonBox->button(QDialogButtonBox::Close); if (close_bt) { @@ -430,9 +427,8 @@ IOGraphDialog::~IOGraphDialog() ui = NULL; } -void IOGraphDialog::copyFromProfile(QAction *action) +void IOGraphDialog::copyFromProfile(QString filename) { - QString filename = action->data().toString(); guint orig_data_len = iog_uat_->raw_data->len; gchar *err = NULL; diff --git a/ui/qt/io_graph_dialog.h b/ui/qt/io_graph_dialog.h index 6dc18b37e0..08a03d9299 100644 --- a/ui/qt/io_graph_dialog.h +++ b/ui/qt/io_graph_dialog.h @@ -200,7 +200,7 @@ private: bool graphIsEnabled(int row) const; private slots: - void copyFromProfile(QAction *action); + void copyFromProfile(QString filename); void updateWidgets(); void graphClicked(QMouseEvent *event); void mouseMoved(QMouseEvent *event); diff --git a/ui/qt/uat_dialog.cpp b/ui/qt/uat_dialog.cpp index 63e56e195a..9cb64b8d69 100644 --- a/ui/qt/uat_dialog.cpp +++ b/ui/qt/uat_dialog.cpp @@ -16,7 +16,7 @@ #include "ui/help_url.h" #include -#include +#include #include #include @@ -101,12 +101,9 @@ void UatDialog::setUat(epan_uat *uat) } if (uat->from_profile) { - QPushButton *copy_button = ui->buttonBox->addButton(tr("Copy from"), QDialogButtonBox::ActionRole); - CopyFromProfileMenu *copy_from_menu = new CopyFromProfileMenu(uat->filename, copy_button); - copy_button->setMenu(copy_from_menu); - copy_button->setToolTip(tr("Copy entries from another profile.")); - copy_button->setEnabled(copy_from_menu->haveProfiles()); - connect(copy_from_menu, SIGNAL(triggered(QAction *)), this, SLOT(copyFromProfile(QAction *))); + CopyFromProfileButton * copy_button = new CopyFromProfileButton(this, uat->filename); + ui->buttonBox->addButton(copy_button, QDialogButtonBox::ActionRole); + connect(copy_button, &CopyFromProfileButton::copyProfile, this, &UatDialog::copyFromProfile); } QString abs_path = gchar_free_to_qstring(uat_get_actual_filename(uat_, FALSE)); @@ -140,10 +137,8 @@ void UatDialog::setUat(epan_uat *uat) setWindowTitle(title); } -void UatDialog::copyFromProfile(QAction *action) +void UatDialog::copyFromProfile(QString filename) { - QString filename = action->data().toString(); - gchar *err = NULL; if (uat_load(uat_, filename.toUtf8().constData(), &err)) { uat_->changed = TRUE; diff --git a/ui/qt/uat_dialog.h b/ui/qt/uat_dialog.h index 7bce5434dc..55be5af0b5 100644 --- a/ui/qt/uat_dialog.h +++ b/ui/qt/uat_dialog.h @@ -38,7 +38,7 @@ public: void setUat(struct epan_uat *uat = NULL); private slots: - void copyFromProfile(QAction *action); + void copyFromProfile(QString filename); void modelDataChanged(const QModelIndex &topLeft); void modelRowsRemoved(); void modelRowsReset(); diff --git a/ui/qt/uat_frame.cpp b/ui/qt/uat_frame.cpp index 0a8cb69e2c..aa8504fd30 100644 --- a/ui/qt/uat_frame.cpp +++ b/ui/qt/uat_frame.cpp @@ -18,7 +18,7 @@ #include #include "wireshark_application.h" -#include +#include #include #include @@ -91,10 +91,8 @@ void UatFrame::setUat(epan_uat *uat) } if (uat->from_profile) { - CopyFromProfileMenu *copy_from_menu = new CopyFromProfileMenu(uat_->filename, ui->copyFromProfileButton); - ui->copyFromProfileButton->setMenu(copy_from_menu); - ui->copyFromProfileButton->setEnabled(copy_from_menu->haveProfiles()); - connect(copy_from_menu, SIGNAL(triggered(QAction *)), this, SLOT(copyFromProfile(QAction *))); + ui->copyFromProfileButton->setFilename(uat->filename); + connect(ui->copyFromProfileButton, &CopyFromProfileButton::copyProfile, this, &UatFrame::copyFromProfile); } QString abs_path = gchar_free_to_qstring(uat_get_actual_filename(uat_, FALSE)); @@ -119,10 +117,8 @@ void UatFrame::setUat(epan_uat *uat) setWindowTitle(title); } -void UatFrame::copyFromProfile(QAction *action) +void UatFrame::copyFromProfile(QString filename) { - QString filename = action->data().toString(); - gchar *err = NULL; if (uat_load(uat_, filename.toUtf8().constData(), &err)) { uat_->changed = TRUE; diff --git a/ui/qt/uat_frame.h b/ui/qt/uat_frame.h index ccf46e1fbf..f420e16a28 100644 --- a/ui/qt/uat_frame.h +++ b/ui/qt/uat_frame.h @@ -49,7 +49,7 @@ private: void applyChanges(); private slots: - void copyFromProfile(QAction *action); + void copyFromProfile(QString filename); void modelDataChanged(const QModelIndex &topLeft); void modelRowsRemoved(); void modelRowsReset(); diff --git a/ui/qt/uat_frame.ui b/ui/qt/uat_frame.ui index 786aa2c07a..452ae65d09 100644 --- a/ui/qt/uat_frame.ui +++ b/ui/qt/uat_frame.ui @@ -102,7 +102,7 @@ - + Copy entries from another profile. @@ -150,6 +150,11 @@ QToolButton
widgets/stock_icon_tool_button.h
+ + CopyFromProfileButton + QPushButton +
widgets/copy_from_profile_button.h
+
diff --git a/ui/qt/widgets/copy_from_profile_button.cpp b/ui/qt/widgets/copy_from_profile_button.cpp new file mode 100644 index 0000000000..1093439252 --- /dev/null +++ b/ui/qt/widgets/copy_from_profile_button.cpp @@ -0,0 +1,135 @@ +/* copy_from_profile_button.cpp + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include +#include +#include + +#include +#include +#include +#include + +CopyFromProfileButton::CopyFromProfileButton(QWidget * parent, QString fileName, QString toolTip) : + QPushButton(parent), + buttonMenu_(Q_NULLPTR) +{ + setText(tr("Copy from")); + if ( toolTip.length() == 0 ) + setToolTip(tr("Copy entries from another profile.")); + else { + setToolTip(toolTip); + } + + if ( fileName.length() > 0 ) + setFilename(fileName); +} + +void CopyFromProfileButton::setFilename(QString filename) +{ + setEnabled(false); + + if ( filename.length() <= 0 ) + return; + + ProfileModel model(this); + + QList global; + QList user; + + QAction * pa = systemDefault(filename); + if ( pa ) + global << pa; + + if ( ! buttonMenu_ ) + buttonMenu_ = new QMenu(); + + if ( buttonMenu_->actions().count() > 0 ) + buttonMenu_->clear(); + + for(int cnt = 0; cnt < model.rowCount(); cnt++) + { + QModelIndex idx = model.index(cnt, ProfileModel::COL_NAME); + QString profilePath = idx.data(ProfileModel::DATA_PATH).toString(); + if ( ! idx.isValid() || profilePath.isEmpty() ) + continue; + + if ( ! idx.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() || idx.data(ProfileModel::DATA_IS_SELECTED).toBool() ) + continue; + + QDir profileDir(profilePath); + if ( ! profileDir.exists() ) + continue; + + QFileInfo fi = profileDir.filePath(filename); + if ( ! fi.exists() ) + continue; + + if ( ! config_file_exists_with_entries(fi.absoluteFilePath().toUtf8().constData(), '#') ) + continue; + + QString name = idx.data().toString(); + pa = new QAction(name, this); + if ( idx.data(ProfileModel::DATA_IS_DEFAULT).toBool() ) + buttonMenu_->addAction(pa); + else if ( idx.data(ProfileModel::DATA_IS_GLOBAL).toBool() ) + global << pa; + else + user << pa; + + pa->setFont(idx.data(Qt::FontRole).value()); + pa->setProperty("profile_name", name); + pa->setProperty("profile_is_global", idx.data(ProfileModel::DATA_IS_GLOBAL)); + pa->setProperty("profile_filename", fi.absoluteFilePath()); + } + + buttonMenu_->addActions(user); + if (global.count() > 0) + { + if ( actions().count() > 0 ) + buttonMenu_->addSeparator(); + buttonMenu_->addActions(global); + } + if ( buttonMenu_->actions().count() <= 0 ) + return; + + connect(buttonMenu_, &QMenu::triggered, this, &CopyFromProfileButton::menuActionTriggered); + setMenu(buttonMenu_); + setEnabled(true); +} + +// "System default" is not a profile. +// Add a special entry for this if the filename exists. +QAction * CopyFromProfileButton::systemDefault(QString filename) +{ + QAction * data = Q_NULLPTR; + + QDir dataDir(get_datafile_dir()); + QString path = dataDir.filePath(filename); + if ( QFile::exists(path) ) + { + data = new QAction(tr("System default"), this); + data->setData(path); + QFont font = data->font(); + font.setItalic(true); + data->setFont(font); + } + + return data; +} + +void CopyFromProfileButton::menuActionTriggered(QAction * action) +{ + if ( action->property("profile_filename").toString().length() > 0 ) + { + QString filename = action->property("profile_filename").toString(); + if ( QFileInfo::exists(filename) ) + emit copyProfile(filename); + } +} diff --git a/ui/qt/widgets/copy_from_profile_button.h b/ui/qt/widgets/copy_from_profile_button.h new file mode 100644 index 0000000000..b54ed35a6c --- /dev/null +++ b/ui/qt/widgets/copy_from_profile_button.h @@ -0,0 +1,43 @@ +/* copy_from_profile_button.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef COPY_FROM_PROFILE_BUTTON_H +#define COPY_FROM_PROFILE_BUTTON_H + +#include +#include + +#include +#include +#include +#include + +class CopyFromProfileButton : public QPushButton +{ + Q_OBJECT + +public: + CopyFromProfileButton(QWidget * parent = Q_NULLPTR, QString profileFile = QString(), QString toolTip = QString()); + + void setFilename(QString filename); + +Q_SIGNALS: + void copyProfile(QString filename); + +private: + QString filename_; + QMenu * buttonMenu_; + + QAction * systemDefault(QString filename); + +private slots: + void menuActionTriggered(QAction *); +}; + +#endif // COPY_FROM_PROFILE_BUTTON_H diff --git a/ui/qt/widgets/copy_from_profile_menu.cpp b/ui/qt/widgets/copy_from_profile_menu.cpp deleted file mode 100644 index c97002cdbb..0000000000 --- a/ui/qt/widgets/copy_from_profile_menu.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* copy_from_profile_menu.cpp - * - * Wireshark - Network traffic analyzer - * By Gerald Combs - * Copyright 1998 Gerald Combs - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#include -#include -#include - -#include -#include - -CopyFromProfileMenu::CopyFromProfileMenu(QString filename, QWidget *parent) : - QMenu(parent), - have_profiles_(false) -{ - ProfileModel model(this); - - QList global; - QList user; - - QAction * pa = systemDefault(filename); - if ( pa ) - global << pa; - - for(int cnt = 0; cnt < model.rowCount(); cnt++) - { - QModelIndex idx = model.index(cnt, ProfileModel::COL_NAME); - QString profilePath = idx.data(ProfileModel::DATA_PATH).toString(); - if ( ! idx.isValid() || profilePath.isEmpty() ) - continue; - - if ( ! idx.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() || idx.data(ProfileModel::DATA_IS_SELECTED).toBool() ) - continue; - - QDir profileDir(profilePath); - if ( ! profileDir.exists() ) - continue; - - QFileInfo fi = profileDir.filePath(filename); - if ( ! fi.exists() ) - continue; - - if ( ! config_file_exists_with_entries(fi.absoluteFilePath().toUtf8().constData(), '#') ) - continue; - - QString name = idx.data().toString(); - pa = new QAction(name, this); - if ( idx.data(ProfileModel::DATA_IS_DEFAULT).toBool() ) - addAction(pa); - else if ( idx.data(ProfileModel::DATA_IS_GLOBAL).toBool() ) - global << pa; - else - user << pa; - - pa->setFont(idx.data(Qt::FontRole).value()); - pa->setProperty("profile_name", name); - pa->setProperty("profile_is_global", idx.data(ProfileModel::DATA_IS_GLOBAL)); - - pa->setProperty("filename", fi.absoluteFilePath()); - pa->setData(fi.absoluteFilePath().toUtf8().constData()); - } - - addActions(user); - if (global.count() > 0) - { - if ( actions().count() > 0 ) - addSeparator(); - addActions(global); - } - - have_profiles_ = actions().count() > 0; -} - -bool CopyFromProfileMenu::haveProfiles() -{ - return have_profiles_; -} - -// "System default" is not a profile. -// Add a special entry for this if the filename exists. -QAction * CopyFromProfileMenu::systemDefault(QString filename) -{ - QAction * data = Q_NULLPTR; - - QDir dataDir(get_datafile_dir()); - QString path = dataDir.filePath(filename); - if ( QFile::exists(path) ) - { - data = new QAction(tr("System default"), this); - data->setData(path); - QFont font = data->font(); - font.setItalic(true); - data->setFont(font); - } - - return data; -} diff --git a/ui/qt/widgets/copy_from_profile_menu.h b/ui/qt/widgets/copy_from_profile_menu.h deleted file mode 100644 index f120d51195..0000000000 --- a/ui/qt/widgets/copy_from_profile_menu.h +++ /dev/null @@ -1,35 +0,0 @@ -/* copy_from_profile_menu.h - * - * Wireshark - Network traffic analyzer - * By Gerald Combs - * Copyright 1998 Gerald Combs - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef COPY_FROM_PROFILE_MENU_H -#define COPY_FROM_PROFILE_MENU_H - -#include -#include - -#include - -class CopyFromProfileMenu : public QMenu -{ - Q_OBJECT - -public: - explicit CopyFromProfileMenu(QString filename, QWidget *parent = Q_NULLPTR); - ~CopyFromProfileMenu() { } - - bool haveProfiles(); - -private: - - QAction * systemDefault(QString filename); - - bool have_profiles_; -}; - -#endif // COPY_FROM_PROFILE_MENU_H -- cgit v1.2.3