mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-22 08:27:24 -06:00
Merge pull request #10340 from Chocobo1/move
Move helper functions to Utils::Gui namespace
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include "addnewtorrentdialog.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QMenu>
|
||||
#include <QPushButton>
|
||||
|
||||
@@ -29,10 +29,14 @@
|
||||
#ifndef MACUTILITIES_H
|
||||
#define MACUTILITIES_H
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QSize>
|
||||
#include <objc/objc.h>
|
||||
|
||||
#include <QSet>
|
||||
|
||||
class QPixmap;
|
||||
class QSize;
|
||||
class QString;
|
||||
|
||||
namespace MacUtils
|
||||
{
|
||||
QPixmap pixmapForExtension(const QString &ext, const QSize &size);
|
||||
|
||||
@@ -28,10 +28,14 @@
|
||||
|
||||
#include "macutilities.h"
|
||||
|
||||
#include <QSet>
|
||||
#include <QtMac>
|
||||
#include <objc/message.h>
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#include <objc/message.h>
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QSet>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
#include <QtMac>
|
||||
|
||||
namespace MacUtils
|
||||
{
|
||||
|
||||
@@ -107,10 +107,6 @@
|
||||
#include "programupdater.h"
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
void qt_mac_set_dock_menu(QMenu *menu);
|
||||
#endif
|
||||
|
||||
#define TIME_TRAY_BALLOON 5000
|
||||
#define PREVENT_SUSPEND_INTERVAL 60000
|
||||
|
||||
@@ -469,7 +465,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
#endif
|
||||
#ifdef Q_OS_MAC
|
||||
setupDockClickHandler();
|
||||
qt_mac_set_dock_menu(trayIconMenu());
|
||||
trayIconMenu()->setAsDockMenu();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1130,7 +1126,7 @@ void MainWindow::showEvent(QShowEvent *e)
|
||||
|
||||
// Make sure the window is initially centered
|
||||
if (!m_posInitialized) {
|
||||
move(Utils::Misc::screenCenter(this));
|
||||
move(Utils::Gui::screenCenter(this));
|
||||
m_posInitialized = true;
|
||||
}
|
||||
}
|
||||
@@ -1815,7 +1811,6 @@ void MainWindow::on_actionDownloadFromURL_triggered()
|
||||
}
|
||||
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
||||
|
||||
void MainWindow::handleUpdateCheckFinished(bool updateAvailable, QString newVersion, bool invokedByUser)
|
||||
{
|
||||
QMessageBox::StandardButton answer = QMessageBox::Yes;
|
||||
|
||||
@@ -30,12 +30,14 @@
|
||||
|
||||
#include <QAction>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QHeaderView>
|
||||
#include <QListWidgetItem>
|
||||
#include <QMenu>
|
||||
#include <QSplitter>
|
||||
#include <QStackedWidget>
|
||||
#include <QThread>
|
||||
#include <QUrl>
|
||||
|
||||
#include "base/bittorrent/filepriority.h"
|
||||
#include "base/bittorrent/session.h"
|
||||
@@ -527,7 +529,7 @@ void PropertiesWidget::openFile(const QModelIndex &index)
|
||||
qDebug("Trying to open file at %s", qUtf8Printable(filePath));
|
||||
// Flush data
|
||||
m_torrent->flushCache();
|
||||
Utils::Misc::openPath(filePath);
|
||||
Utils::Gui::openPath(filePath);
|
||||
}
|
||||
|
||||
void PropertiesWidget::openFolder(const QModelIndex &index, bool containingFolder)
|
||||
@@ -563,9 +565,9 @@ void PropertiesWidget::openFolder(const QModelIndex &index, bool containingFolde
|
||||
MacUtils::openFiles(QSet<QString>{absolutePath});
|
||||
#else
|
||||
if (containingFolder)
|
||||
Utils::Misc::openFolderSelect(absolutePath);
|
||||
Utils::Gui::openFolderSelect(absolutePath);
|
||||
else
|
||||
Utils::Misc::openPath(absolutePath);
|
||||
Utils::Gui::openPath(absolutePath);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -35,10 +35,9 @@
|
||||
#include <QHeaderView>
|
||||
#include <QMenu>
|
||||
#include <QPalette>
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QStandardItemModel>
|
||||
#include <QTableView>
|
||||
#include <QTreeView>
|
||||
#include <QUrl>
|
||||
|
||||
#include "base/bittorrent/session.h"
|
||||
#include "base/preferences.h"
|
||||
|
||||
@@ -36,7 +36,6 @@
|
||||
|
||||
class QHeaderView;
|
||||
class QModelIndex;
|
||||
class QStandardItem;
|
||||
class QStandardItemModel;
|
||||
|
||||
template <typename T> class CachedSettingValue;
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
#include <QStyle>
|
||||
|
||||
#include "base/preferences.h"
|
||||
#include "base/utils/misc.h"
|
||||
#include "ui_shutdownconfirmdialog.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -63,7 +62,7 @@ ShutdownConfirmDialog::ShutdownConfirmDialog(QWidget *parent, const ShutdownDial
|
||||
|
||||
// Always on top
|
||||
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
|
||||
move(Utils::Misc::screenCenter(this));
|
||||
move(Utils::Gui::screenCenter(this));
|
||||
|
||||
m_timer.setInterval(1000); // 1sec
|
||||
connect(&m_timer, &QTimer::timeout, this, &ShutdownConfirmDialog::updateSeconds);
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "transferlistmodel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QIcon>
|
||||
#include <QPalette>
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
#include "transferlistsortmodel.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QStringList>
|
||||
|
||||
#include "base/bittorrent/torrenthandle.h"
|
||||
@@ -35,7 +36,6 @@
|
||||
#include "base/utils/string.h"
|
||||
#include "transferlistmodel.h"
|
||||
|
||||
|
||||
TransferListSortModel::TransferListSortModel(QObject *parent)
|
||||
: QSortFilterProxyModel(parent)
|
||||
{
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
#include "transferlistmodel.h"
|
||||
#include "transferlistsortmodel.h"
|
||||
#include "updownratiodialog.h"
|
||||
#include "utils.h"
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
#include "macutilities.h"
|
||||
@@ -341,7 +342,7 @@ TransferListModel *TransferListWidget::getSourceModel() const
|
||||
|
||||
void TransferListWidget::previewFile(const QString &filePath)
|
||||
{
|
||||
Utils::Misc::openPath(filePath);
|
||||
Utils::Gui::openPath(filePath);
|
||||
}
|
||||
|
||||
inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const
|
||||
@@ -386,9 +387,9 @@ void TransferListWidget::torrentDoubleClicked()
|
||||
MacUtils::openFiles(QSet<QString>{torrent->contentPath(true)});
|
||||
#else
|
||||
if (torrent->filesCount() == 1)
|
||||
Utils::Misc::openFolderSelect(torrent->contentPath(true));
|
||||
Utils::Gui::openFolderSelect(torrent->contentPath(true));
|
||||
else
|
||||
Utils::Misc::openPath(torrent->contentPath(true));
|
||||
Utils::Gui::openPath(torrent->contentPath(true));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
@@ -592,9 +593,9 @@ void TransferListWidget::openSelectedTorrentsFolder() const
|
||||
QString path = torrent->contentPath(true);
|
||||
if (!pathsList.contains(path)) {
|
||||
if (torrent->filesCount() == 1)
|
||||
Utils::Misc::openFolderSelect(path);
|
||||
Utils::Gui::openFolderSelect(path);
|
||||
else
|
||||
Utils::Misc::openPath(path);
|
||||
Utils::Gui::openPath(path);
|
||||
}
|
||||
pathsList.insert(path);
|
||||
}
|
||||
|
||||
@@ -28,14 +28,30 @@
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <Objbase.h>
|
||||
#include <Shlobj.h>
|
||||
#endif
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDesktopServices>
|
||||
#include <QDesktopWidget>
|
||||
#include <QFileInfo>
|
||||
#include <QIcon>
|
||||
#include <QPixmap>
|
||||
#include <QPixmapCache>
|
||||
#include <QPoint>
|
||||
#include <QProcess>
|
||||
#include <QRegularExpression>
|
||||
#include <QScreen>
|
||||
#include <QStyle>
|
||||
#include <QUrl>
|
||||
#include <QWidget>
|
||||
#include <QWindow>
|
||||
|
||||
#include "base/utils/fs.h"
|
||||
#include "base/utils/version.h"
|
||||
|
||||
void Utils::Gui::resize(QWidget *widget, const QSize &newSize)
|
||||
{
|
||||
if (newSize.isValid())
|
||||
@@ -112,3 +128,93 @@ QSize Utils::Gui::largeIconSize(const QWidget *widget)
|
||||
const int s = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize, nullptr, widget);
|
||||
return {s, s};
|
||||
}
|
||||
|
||||
QPoint Utils::Gui::screenCenter(const QWidget *w)
|
||||
{
|
||||
// Returns the QPoint which the widget will be placed center on screen (where parent resides)
|
||||
|
||||
if (!w)
|
||||
return {};
|
||||
|
||||
QRect r = QGuiApplication::primaryScreen()->availableGeometry();
|
||||
const QPoint primaryScreenCenter {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
|
||||
|
||||
const QWidget *parent = w->parentWidget();
|
||||
if (!parent)
|
||||
return primaryScreenCenter;
|
||||
|
||||
const QWindow *window = parent->window()->windowHandle();
|
||||
if (!window)
|
||||
return primaryScreenCenter;
|
||||
|
||||
const QScreen *screen = window->screen();
|
||||
if (!screen)
|
||||
return primaryScreenCenter;
|
||||
|
||||
r = screen->availableGeometry();
|
||||
return {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
|
||||
}
|
||||
|
||||
// Open the given path with an appropriate application
|
||||
void Utils::Gui::openPath(const QString &absolutePath)
|
||||
{
|
||||
const QString path = Utils::Fs::fromNativePath(absolutePath);
|
||||
// Hack to access samba shares with QDesktopServices::openUrl
|
||||
if (path.startsWith("//"))
|
||||
QDesktopServices::openUrl(Utils::Fs::toNativePath("file:" + path));
|
||||
else
|
||||
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
|
||||
}
|
||||
|
||||
// Open the parent directory of the given path with a file manager and select
|
||||
// (if possible) the item at the given path
|
||||
void Utils::Gui::openFolderSelect(const QString &absolutePath)
|
||||
{
|
||||
const QString path = Utils::Fs::fromNativePath(absolutePath);
|
||||
// If the item to select doesn't exist, try to open its parent
|
||||
if (!QFileInfo::exists(path)) {
|
||||
openPath(path.left(path.lastIndexOf('/')));
|
||||
return;
|
||||
}
|
||||
#ifdef Q_OS_WIN
|
||||
HRESULT hresult = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
||||
PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(reinterpret_cast<PCTSTR>(Utils::Fs::toNativePath(path).utf16()));
|
||||
if (pidl) {
|
||||
::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
|
||||
::ILFree(pidl);
|
||||
}
|
||||
if ((hresult == S_OK) || (hresult == S_FALSE))
|
||||
::CoUninitialize();
|
||||
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
|
||||
QProcess proc;
|
||||
proc.start("xdg-mime", {"query", "default", "inode/directory"});
|
||||
proc.waitForFinished();
|
||||
const QString output = proc.readLine().simplified();
|
||||
if ((output == "dolphin.desktop") || (output == "org.kde.dolphin.desktop")) {
|
||||
proc.startDetached("dolphin", {"--select", Utils::Fs::toNativePath(path)});
|
||||
}
|
||||
else if ((output == "nautilus.desktop") || (output == "org.gnome.Nautilus.desktop")
|
||||
|| (output == "nautilus-folder-handler.desktop")) {
|
||||
proc.start("nautilus", {"--version"});
|
||||
proc.waitForFinished();
|
||||
const QString nautilusVerStr = QString(proc.readLine()).remove(QRegularExpression("[^0-9.]"));
|
||||
using NautilusVersion = Utils::Version<int, 3>;
|
||||
if (NautilusVersion::tryParse(nautilusVerStr, {1, 0, 0}) > NautilusVersion {3, 28})
|
||||
proc.startDetached("nautilus", {Utils::Fs::toNativePath(path)});
|
||||
else
|
||||
proc.startDetached("nautilus", {"--no-desktop", Utils::Fs::toNativePath(path)});
|
||||
}
|
||||
else if (output == "nemo.desktop") {
|
||||
proc.startDetached("nemo", {"--no-desktop", Utils::Fs::toNativePath(path)});
|
||||
}
|
||||
else if ((output == "konqueror.desktop") || (output == "kfmclient_dir.desktop")) {
|
||||
proc.startDetached("konqueror", {"--select", Utils::Fs::toNativePath(path)});
|
||||
}
|
||||
else {
|
||||
// "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
|
||||
openPath(path.left(path.lastIndexOf('/')));
|
||||
}
|
||||
#else
|
||||
openPath(path.left(path.lastIndexOf('/')));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@
|
||||
#ifndef UTILS_GUI_H
|
||||
#define UTILS_GUI_H
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QPixmap>
|
||||
#include <QSize>
|
||||
|
||||
class QIcon;
|
||||
class QPixmap;
|
||||
class QPoint;
|
||||
class QWidget;
|
||||
|
||||
namespace Utils
|
||||
@@ -55,6 +55,11 @@ namespace Utils
|
||||
QSize smallIconSize(const QWidget *widget = nullptr);
|
||||
QSize mediumIconSize(const QWidget *widget = nullptr);
|
||||
QSize largeIconSize(const QWidget *widget = nullptr);
|
||||
|
||||
QPoint screenCenter(const QWidget *w);
|
||||
|
||||
void openPath(const QString &absolutePath);
|
||||
void openFolderSelect(const QString &absolutePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user