TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkdesktopscreen.cpp
Go to the documentation of this file.
1 #include "ttkdesktopscreen.h"
2 #include "ttklibrary.h"
3 
4 #include <QScreen>
5 #include <QApplication>
6 #ifdef Q_OS_WIN
7 # define WIN32_LEAN_AND_MEAN
8 # include <qt_windows.h>
9 #endif
10 #if !TTK_QT_VERSION_CHECK(5,0,0)
11 # include <QDesktopWidget>
12 #endif
13 #if defined Q_OS_LINUX
14 # include <X11/Xlib.h>
15 # include "ttkregularexpression.h"
16 #endif
17 
19 {
20  const QRect &sr = screenGeometry(index);
21  const QRect &dr = availableGeometry(index);
22 
23  TaskbarInfo info;
24  if(sr.left() != dr.left())
25  {
26  info.m_size = std::abs(sr.left() - dr.left());
28  }
29  else if(sr.right() != dr.right())
30  {
31  info.m_size = std::abs(sr.right() - dr.right());
33  }
34  else if(sr.top() != dr.top())
35  {
36  info.m_size = std::abs(sr.top() - dr.top());
38  }
39  else if(sr.bottom() != dr.bottom())
40  {
41  info.m_size = std::abs(sr.bottom() - dr.bottom());
43  }
44  else
45  {
46  info.m_size = 0;
48  }
49  return info;
50 }
51 
53 {
54 #if TTK_QT_VERSION_CHECK(5,0,0)
55  const QList<QScreen*> &screens = QApplication::screens();
56  QRegion virtualGeometry;
57  for(QScreen *screen : screens)
58  {
59  virtualGeometry += screen->geometry();
60  }
61  return virtualGeometry.boundingRect();
62 #else
63  QDesktopWidget *widget = QApplication::desktop();
64  return widget ? widget->geometry() : QRect();
65 #endif
66 }
67 
69 {
70 #if TTK_QT_VERSION_CHECK(5,0,0)
71  const QList<QScreen*> &screens = QApplication::screens();
72  return (index < 0 || index >= screens.count()) ? QRect() : screens[index]->geometry();
73 #else
74  QDesktopWidget *widget = QApplication::desktop();
75  return widget ? widget->screenGeometry(index) : QRect();
76 #endif
77 }
78 
80 {
81 #if TTK_QT_VERSION_CHECK(5,0,0)
82  const QList<QScreen*> &screens = QApplication::screens();
83  return (index < 0 || index >= screens.count()) ? QRect() : screens[index]->availableGeometry();
84 #else
85  QDesktopWidget *widget = QApplication::desktop();
86  return widget ? widget->availableGeometry(index) : QRect();
87 #endif
88 }
89 
91 {
92 #if TTK_QT_VERSION_CHECK(5,0,0)
93  return QApplication::screens().count();
94 #else
95  return QApplication::desktop()->screenCount();
96 #endif
97 }
98 
100 {
101  int index = 0;
102  const int count = TTKDesktopScreen::count();
103  if(count > 1)
104  {
105  const QPoint &pos = QCursor::pos();
106  for(int i = 0; i < count; ++i)
107  {
108 #if TTK_QT_VERSION_CHECK(5,0,0)
109  if(QApplication::screens().at(i)->geometry().contains(pos))
110 #else
111  if(QApplication::desktop()->screenGeometry(i).contains(pos))
112 #endif
113  {
114  index = i;
115  break;
116  }
117  }
118  }
119  return index;
120 }
121 
123 {
124  const int index = currentIndex();
125  return screenGeometry(index);
126 }
127 
128 void TTKDesktopScreen::centerWidget(QWidget *widget)
129 {
130  const QRect &rect = currentGeometry();
131  widget->move(rect.x() + (rect.width() - widget->width()) / 2, rect.y() + (rect.height() - widget->height()) / 2);
132 }
133 
134 QPixmap TTKDesktopScreen::grabWidget(QWidget *widget, const QRect &rect)
135 {
136 #if TTK_QT_VERSION_CHECK(5,0,0)
137  return widget->grab(rect);
138 #else
139  return QPixmap::grabWidget(widget, rect);
140 #endif
141 }
142 
143 QPixmap TTKDesktopScreen::grabWindow(int x, int y, int w, int h)
144 {
145  QWidget widget(nullptr, Qt::Desktop);
146  widget.setVisible(false);
147  widget.setGeometry(geometry());
148 #if TTK_QT_VERSION_CHECK(5,0,0)
149  QScreen *screen = QApplication::primaryScreen();
150  return screen ? screen->grabWindow(widget.winId(), x, y, w, h) : QPixmap();
151 #else
152  return QPixmap::grabWindow(widget.winId(), x, y, w, h);
153 #endif
154 }
155 
156 static constexpr int DEFAULT_DPI = 96;
157 
158 static QSize generateDPIValue()
159 {
160  QSize dpiSize(DEFAULT_DPI, DEFAULT_DPI);
161 #ifdef Q_OS_WIN
162  HDC screen = GetDC(GetDesktopWindow());
163  dpiSize.setWidth(GetDeviceCaps(screen, LOGPIXELSX));
164  dpiSize.setHeight(GetDeviceCaps(screen, LOGPIXELSY));
165  ReleaseDC(GetDesktopWindow(), screen);
166 #elif defined Q_OS_LINUX
167  Display *dp = XOpenDisplay(nullptr);
168  if(!dp)
169  {
170  return dpiSize;
171  }
172 
173  double dpi = DEFAULT_DPI;
174  const char *resource = XResourceManagerString(dp);
175  if(resource)
176  {
177  const QString data(resource);
178  static TTKRegularExpression regx("Xft.dpi:\t(\\d+)");
179  if(regx.match(data) != -1)
180  {
181  dpi = regx.captured(1).toDouble();
182  }
183  }
184 
185  const int screen = 0; /* Screen number */
186  const double x = (DisplayWidth(dp, screen) * 25.4) / DisplayWidthMM(dp, screen);
187  const double y = (DisplayHeight(dp, screen) * 25.4) / DisplayHeightMM(dp, screen);
188 
189  if(x < dpi && x != DEFAULT_DPI && TTKStaticCast(int, x) % DEFAULT_DPI != 0 &&
190  y < dpi && y != DEFAULT_DPI && TTKStaticCast(int, y) % DEFAULT_DPI != 0)
191  {
192  dpiSize.setWidth(x + 0.5);
193  dpiSize.setHeight(y + 0.5);
194  }
195  else
196  {
197  dpiSize.setWidth(dpi + 0.5);
198  dpiSize.setHeight(dpi + 0.5);
199  }
200 
201  XCloseDisplay(dp);
202 #elif defined Q_OS_MAC
203  if(!qApp)
204  {
205  int count = 0;
206  QApplication(count, nullptr);
207  }
208 
209 # if TTK_QT_VERSION_CHECK(5,0,0)
210  QScreen *screen = QApplication::primaryScreen();
211  if(screen)
212  {
213  dpiSize.setWidth(screen->logicalDotsPerInchX());
214  dpiSize.setHeight(screen->logicalDotsPerInchY());
215  }
216  // fallback below if no screen
217 # else
218  QDesktopWidget* desktop = QApplication::desktop();
219  if(desktop)
220  {
221  dpiSize.setWidth(desktop->logicalDpiX());
222  dpiSize.setHeight(desktop->logicalDpiY());
223  }
224 # endif
225 #endif
226  return dpiSize;
227 }
228 
230 {
231  return devicePixelRatio(index) * DEFAULT_DPI;
232 }
233 
235 {
236 #if TTK_QT_VERSION_CHECK(5,5,0)
237  const QList<QScreen*> &screens = QApplication::screens();
238  return (index < 0 || index >= screens.count()) ? 1.0 : screens[index]->devicePixelRatio();
239 #else
240  Q_UNUSED(index);
241  const QSize &size = generateDPIValue();
242  return 1.0 * (size.width() + size.height()) / 2 / DEFAULT_DPI;
243 #endif
244 }
245 
247 {
248  const int index = currentIndex();
249  return devicePixelRatio(index);
250 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:231
static qreal devicePixelRatio(int index=0)
static QRect geometry()
QString captured(int index) const
static qreal currentPixelRatio()
voidpf void uLong size
Definition: ioapi.h:136
static constexpr int DEFAULT_DPI
static QPixmap grabWindow(int x=0, int y=0, int w=-1, int h=-1)
static TaskbarInfo screenTaskbar(int index=0)
static void centerWidget(QWidget *widget)
static int currentIndex()
static QRect currentGeometry()
static int deviceDotsPerInch(int index=0)
static QSize generateDPIValue()
static QPixmap grabWidget(QWidget *widget, const QRect &rect)
The class of the regular expression.
static QRect screenGeometry(int index=0)
int match(const QString &str, int pos=0)
static QRect availableGeometry(int index=0)