qBittorrent
utils.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017 Mike Tzou
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "utils.h"
30 
31 #ifdef Q_OS_WIN
32 #include <Objbase.h>
33 #include <Shlobj.h>
34 #endif
35 
36 #include <QApplication>
37 #include <QDesktopServices>
38 #include <QFileInfo>
39 #include <QIcon>
40 #include <QPixmap>
41 #include <QPixmapCache>
42 #include <QPoint>
43 #include <QProcess>
44 #include <QRegularExpression>
45 #include <QScreen>
46 #include <QStyle>
47 #include <QUrl>
48 #include <QWidget>
49 #include <QWindow>
50 
51 #include "base/utils/fs.h"
52 #include "base/utils/version.h"
53 
54 void Utils::Gui::resize(QWidget *widget, const QSize &newSize)
55 {
56  if (newSize.isValid())
57  widget->resize(newSize);
58  else // depends on screen DPI
59  widget->resize(widget->size() * screenScalingFactor(widget));
60 }
61 
62 qreal Utils::Gui::screenScalingFactor(const QWidget *widget)
63 {
64  Q_UNUSED(widget);
65  return 1;
66 }
67 
68 QPixmap Utils::Gui::scaledPixmap(const QIcon &icon, const QWidget *widget, const int height)
69 {
70  Q_ASSERT(height > 0);
71  const int scaledHeight = height * Utils::Gui::screenScalingFactor(widget);
72  return icon.pixmap(scaledHeight);
73 }
74 
75 QPixmap Utils::Gui::scaledPixmap(const QString &path, const QWidget *widget, const int height)
76 {
77  const QPixmap pixmap(path);
78  const int scaledHeight = ((height > 0) ? height : pixmap.height()) * Utils::Gui::screenScalingFactor(widget);
79  return pixmap.scaledToHeight(scaledHeight, Qt::SmoothTransformation);
80 }
81 
82 QPixmap Utils::Gui::scaledPixmapSvg(const QString &path, const QWidget *widget, const int baseHeight)
83 {
84  const int scaledHeight = baseHeight * Utils::Gui::screenScalingFactor(widget);
85  const QString normalizedKey = path + '@' + QString::number(scaledHeight);
86 
87  QPixmap pm;
88  QPixmapCache cache;
89  if (!cache.find(normalizedKey, &pm))
90  {
91  pm = QIcon(path).pixmap(scaledHeight);
92  cache.insert(normalizedKey, pm);
93  }
94  return pm;
95 }
96 
97 QSize Utils::Gui::smallIconSize(const QWidget *widget)
98 {
99  // Get DPI scaled icon size (device-dependent), see QT source
100  // under a 1080p screen is usually 16x16
101  const int s = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, widget);
102  return {s, s};
103 }
104 
105 QSize Utils::Gui::mediumIconSize(const QWidget *widget)
106 {
107  // under a 1080p screen is usually 24x24
108  return ((smallIconSize(widget) + largeIconSize(widget)) / 2);
109 }
110 
111 QSize Utils::Gui::largeIconSize(const QWidget *widget)
112 {
113  // Get DPI scaled icon size (device-dependent), see QT source
114  // under a 1080p screen is usually 32x32
115  const int s = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize, nullptr, widget);
116  return {s, s};
117 }
118 
119 QPoint Utils::Gui::screenCenter(const QWidget *w)
120 {
121  // Returns the QPoint which the widget will be placed center on screen (where parent resides)
122 
123  if (!w)
124  return {};
125 
126  QRect r = QGuiApplication::primaryScreen()->availableGeometry();
127  const QPoint primaryScreenCenter {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
128 
129  const QWidget *parent = w->parentWidget();
130  if (!parent)
131  return primaryScreenCenter;
132 
133  const QWindow *window = parent->window()->windowHandle();
134  if (!window)
135  return primaryScreenCenter;
136 
137  const QScreen *screen = window->screen();
138  if (!screen)
139  return primaryScreenCenter;
140 
141  r = screen->availableGeometry();
142  return {(r.x() + (r.width() - w->frameSize().width()) / 2), (r.y() + (r.height() - w->frameSize().height()) / 2)};
143 }
144 
145 // Open the given path with an appropriate application
146 void Utils::Gui::openPath(const QString &absolutePath)
147 {
148  const QString path = Utils::Fs::toUniformPath(absolutePath);
149  // Hack to access samba shares with QDesktopServices::openUrl
150  if (path.startsWith("//"))
151  QDesktopServices::openUrl(Utils::Fs::toNativePath("file:" + path));
152  else
153  QDesktopServices::openUrl(QUrl::fromLocalFile(path));
154 }
155 
156 // Open the parent directory of the given path with a file manager and select
157 // (if possible) the item at the given path
158 void Utils::Gui::openFolderSelect(const QString &absolutePath)
159 {
160  QString path {Utils::Fs::toUniformPath(absolutePath)};
161  const QFileInfo pathInfo {path};
162  // If the item to select doesn't exist, try to open its parent
163  if (!pathInfo.exists(path))
164  {
165  openPath(path.left(path.lastIndexOf('/')));
166  return;
167  }
168 
169 #ifdef Q_OS_WIN
170  HRESULT hresult = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
171  PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(reinterpret_cast<PCTSTR>(Utils::Fs::toNativePath(path).utf16()));
172  if (pidl)
173  {
174  ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
175  ::ILFree(pidl);
176  }
177  if ((hresult == S_OK) || (hresult == S_FALSE))
178  ::CoUninitialize();
179 #elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
180  QProcess proc;
181  proc.start("xdg-mime", {"query", "default", "inode/directory"});
182  proc.waitForFinished();
183  const QString output = proc.readLine().simplified();
184  if ((output == "dolphin.desktop") || (output == "org.kde.dolphin.desktop"))
185  {
186  proc.startDetached("dolphin", {"--select", Utils::Fs::toNativePath(path)});
187  }
188  else if ((output == "nautilus.desktop") || (output == "org.gnome.Nautilus.desktop")
189  || (output == "nautilus-folder-handler.desktop"))
190  {
191  if (pathInfo.isDir())
192  path = path.left(path.lastIndexOf('/'));
193  proc.start("nautilus", {"--version"});
194  proc.waitForFinished();
195  const QString nautilusVerStr = QString(proc.readLine()).remove(QRegularExpression("[^0-9.]"));
196  using NautilusVersion = Utils::Version<int, 3>;
197  if (NautilusVersion::tryParse(nautilusVerStr, {1, 0, 0}) > NautilusVersion {3, 28})
198  proc.startDetached("nautilus", {Utils::Fs::toNativePath(path)});
199  else
200  proc.startDetached("nautilus", {"--no-desktop", Utils::Fs::toNativePath(path)});
201  }
202  else if (output == "nemo.desktop")
203  {
204  if (pathInfo.isDir())
205  path = path.left(path.lastIndexOf('/'));
206  proc.startDetached("nemo", {"--no-desktop", Utils::Fs::toNativePath(path)});
207  }
208  else if ((output == "konqueror.desktop") || (output == "kfmclient_dir.desktop"))
209  {
210  proc.startDetached("konqueror", {"--select", Utils::Fs::toNativePath(path)});
211  }
212  else
213  {
214  // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003
215  openPath(path.left(path.lastIndexOf('/')));
216  }
217 #else
218  openPath(path.left(path.lastIndexOf('/')));
219 #endif
220 }
QString toUniformPath(const QString &path)
Definition: fs.cpp:69
QString toNativePath(const QString &path)
Definition: fs.cpp:64
QSize mediumIconSize(const QWidget *widget=nullptr)
Definition: utils.cpp:105
qreal screenScalingFactor(const QWidget *widget)
Definition: utils.cpp:62
QSize smallIconSize(const QWidget *widget=nullptr)
Definition: utils.cpp:97
QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height)
Definition: utils.cpp:68
void openFolderSelect(const QString &absolutePath)
Definition: utils.cpp:158
void resize(QWidget *widget, const QSize &newSize={})
Definition: utils.cpp:54
void openPath(const QString &absolutePath)
Definition: utils.cpp:146
QPoint screenCenter(const QWidget *w)
Definition: utils.cpp:119
QPixmap scaledPixmapSvg(const QString &path, const QWidget *widget, int baseHeight)
Definition: utils.cpp:82
QSize largeIconSize(const QWidget *widget=nullptr)
Definition: utils.cpp:111