diff options
Diffstat (limited to 'ui/qt')
-rw-r--r-- | ui/qt/main_welcome.cpp | 54 | ||||
-rw-r--r-- | ui/qt/main_welcome.h | 4 | ||||
-rw-r--r-- | ui/qt/qt_ui_utils.cpp | 37 | ||||
-rw-r--r-- | ui/qt/qt_ui_utils.h | 8 |
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 |