aboutsummaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2015-10-09 13:56:27 -0700
committerGerald Combs <gerald@wireshark.org>2015-10-13 02:44:53 +0000
commit7c3800228881f45049b29f491b9d7648c0a5ae5d (patch)
tree9066cdc94579a8d7ff11b5b775e324361b6fe48c /ui
parent1859ae8aca39d4c8678e8d0f25be3caf6c349393 (diff)
Qt: Recent list context menu.
Add a context menu to the main window recent list. Add items that let the user open each file's containing folder and copy the file path to the clipboard. When opening the folder on Windows and OS X try to highlight the file in Explorer or the Finder. Change-Id: I991e8df8ba9f1f8c6385d1a861eb40223cfdd047 Reviewed-on: https://code.wireshark.org/review/10915 Reviewed-by: Gerald Combs <gerald@wireshark.org> Petri-Dish: Gerald Combs <gerald@wireshark.org> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Diffstat (limited to 'ui')
-rw-r--r--ui/qt/main_welcome.cpp54
-rw-r--r--ui/qt/main_welcome.h4
-rw-r--r--ui/qt/qt_ui_utils.cpp37
-rw-r--r--ui/qt/qt_ui_utils.h8
4 files changed, 103 insertions, 0 deletions
diff --git a/ui/qt/main_welcome.cpp b/ui/qt/main_welcome.cpp
index b79758d0f1..621003dcc4 100644
--- a/ui/qt/main_welcome.cpp
+++ b/ui/qt/main_welcome.cpp
@@ -33,10 +33,14 @@
#include <ui_main_welcome.h>
#include "tango_colors.h"
+#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include "interface_tree.h"
+#include <QClipboard>
+#include <QDir>
#include <QListWidget>
+#include <QMenu>
#include <QResizeEvent>
#include <QTreeWidgetItem>
#include <QWidget>
@@ -133,6 +137,11 @@ MainWelcome::MainWelcome(QWidget *parent) :
);
recent_files_->setTextElideMode(Qt::ElideLeft);
+ recent_ctx_menu_ = new QMenu(this);
+ welcome_ui_->recentList->setContextMenuPolicy(Qt::CustomContextMenu);
+ connect(recent_files_, SIGNAL(customContextMenuRequested(QPoint)),
+ this, SLOT(showRecentContextMenu(QPoint)));
+
connect(wsApp, SIGNAL(updateRecentItemStatus(const QString &, qint64, bool)), this, SLOT(updateRecentFiles()));
connect(wsApp, SIGNAL(appInitialized()), this, SLOT(appInitialized()));
connect(welcome_ui_->interfaceTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
@@ -302,6 +311,51 @@ void MainWelcome::changeEvent(QEvent* event)
QFrame::changeEvent(event);
}
+#ifdef Q_OS_MAC
+static const QString show_in_str_ = QObject::tr("Show in Finder");
+#else
+static const QString show_in_str_ = QObject::tr("Show in Folder");
+#endif
+void MainWelcome::showRecentContextMenu(QPoint pos)
+{
+ QListWidgetItem *li = recent_files_->itemAt(pos);
+ if (!li) return;
+
+ recent_ctx_menu_->clear();
+
+ QString cf_path = li->data(Qt::UserRole).toString();
+ QAction *show_action = recent_ctx_menu_->addAction(show_in_str_);
+
+ show_action->setData(cf_path);
+ connect(show_action, SIGNAL(triggered(bool)), this, SLOT(showRecentFolder()));
+
+ QAction *copy_action = recent_ctx_menu_->addAction(tr("Copy file path"));
+ copy_action->setData(cf_path);
+ connect(copy_action, SIGNAL(triggered(bool)), this, SLOT(copyRecentPath()));
+
+ recent_ctx_menu_->exec(recent_files_->mapToGlobal(pos));
+}
+
+void MainWelcome::showRecentFolder()
+{
+ QAction *ria = qobject_cast<QAction*>(sender());
+ if (!ria) return;
+
+ QString cf_path = ria->data().toString();
+ desktop_show_in_folder(cf_path);
+}
+
+void MainWelcome::copyRecentPath()
+{
+ QAction *ria = qobject_cast<QAction*>(sender());
+ if (!ria) return;
+
+ QString cf_path = ria->data().toString();
+ if (cf_path.isEmpty()) return;
+
+ wsApp->clipboard()->setText(cf_path);
+}
+
/*
* Editor modelines
*
diff --git a/ui/qt/main_welcome.h b/ui/qt/main_welcome.h
index 97d66c0f53..18574cbabf 100644
--- a/ui/qt/main_welcome.h
+++ b/ui/qt/main_welcome.h
@@ -54,6 +54,7 @@ private:
// We may want to subclass it at some point.
QListWidget *recent_files_;
// MWOverlay *overlay;
+ QMenu *recent_ctx_menu_;
signals:
@@ -75,6 +76,9 @@ private slots:
void updateRecentFiles();
void openRecentItem(QListWidgetItem *item);
void changeEvent(QEvent* event);
+ void showRecentContextMenu(QPoint pos);
+ void showRecentFolder();
+ void copyRecentPath();
};
#endif // MAIN_WELCOME_H
diff --git a/ui/qt/qt_ui_utils.cpp b/ui/qt/qt_ui_utils.cpp
index 18be1b58ef..d18efa97b4 100644
--- a/ui/qt/qt_ui_utils.cpp
+++ b/ui/qt/qt_ui_utils.cpp
@@ -37,7 +37,12 @@
#include <QAction>
#include <QDateTime>
+#include <QDesktopServices>
+#include <QDir>
+#include <QFileInfo>
#include <QFontDatabase>
+#include <QProcess>
+#include <QUrl>
#include <QUuid>
/* Make the format_size_flags_e enum usable in C++ */
@@ -193,6 +198,38 @@ bool qStringCaseLessThan(const QString &s1, const QString &s2)
return s1.compare(s2, Qt::CaseInsensitive) < 0;
}
+// http://stackoverflow.com/questions/3490336/how-to-reveal-in-finder-or-show-in-explorer-with-qt
+void desktop_show_in_folder(const QString file_path)
+{
+ bool success = false;
+
+#if defined(Q_OS_WIN)
+ QString path = QDir::toNativeSeparators(file_path);
+ QString command = "explorer.exe /select," + path;
+ success = QProcess::startDetached(command);
+#elif defined(Q_OS_MAC)
+ QStringList script_args;
+
+ // XXX Find a way to pass special characters here.
+ script_args << "-e"
+ << QString("tell application \"Finder\" to reveal POSIX file \"%1\"")
+ .arg(file_path);
+ if (QProcess::execute("/usr/bin/osascript", script_args) == 0) {
+ success = true;
+ script_args.clear();
+ script_args << "-e"
+ << "tell application \"Finder\" to activate";
+ QProcess::execute("/usr/bin/osascript", script_args);
+ }
+#else
+ // Is there a way to highlight the file using xdg-open?
+#endif
+ if (!success) { // Last resort
+ QFileInfo file_info = file_path;
+ QDesktopServices::openUrl(QUrl::fromLocalFile(file_info.dir().absolutePath()));
+ }
+}
+
/*
* Editor modelines
*
diff --git a/ui/qt/qt_ui_utils.h b/ui/qt/qt_ui_utils.h
index c94e1f1900..401ddbe00f 100644
--- a/ui/qt/qt_ui_utils.h
+++ b/ui/qt/qt_ui_utils.h
@@ -185,6 +185,14 @@ bool qActionLessThan(const QAction *a1, const QAction *a2);
*/
bool qStringCaseLessThan(const QString &s1, const QString &s2);
+/**
+ * Given the path to a file, open its containing folder in the desktop
+ * shell. Highlight the file if possible.
+ *
+ * @param file_path Path to the file.
+ */
+void desktop_show_in_folder(const QString file_path);
+
#endif /* __QT_UI_UTILS__H__ */
// XXX Add a routine to fetch the HWND corresponding to a widget using QPlatformIntegration