-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathfullscreenbackground.cpp
More file actions
742 lines (630 loc) · 24.4 KB
/
Copy pathfullscreenbackground.cpp
File metadata and controls
742 lines (630 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "fullscreenbackground.h"
#include "black_widget.h"
#include "lockcontent.h"
#include "public_func.h"
#include "sessionbasemodel.h"
#include "dconfig_helper.h"
#include <DGuiApplicationHelper>
#include <DPlatformWindowHandle>
#include <QDebug>
#include <QImageReader>
#include <QKeyEvent>
#include <QPainter>
#include <QScreen>
#include <QTimer>
#include <QWindow>
#include "dbusconstant.h"
Q_LOGGING_CATEGORY(DDE_SS, "dss.active")
DGUI_USE_NAMESPACE
const int PIXMAP_TYPE_BACKGROUND = 0;
const int PIXMAP_TYPE_BLUR_BACKGROUND = 1;
QString FullScreenBackground::originBackgroundPath;
QString FullScreenBackground::blurBackgroundPath;
QMap<QString, QPixmap> FullScreenBackground::blurBackgroundCacheMap;
QList<FullScreenBackground *> FullScreenBackground::frameList;
QPointer<FullScreenBackground> FullScreenBackground::currentFrame = nullptr;
QPointer<QWidget> FullScreenBackground::currentContent = nullptr;
FullScreenBackground::FullScreenBackground(SessionBaseModel *model, QWidget *parent)
: QWidget(parent)
, m_model(model)
, m_useSolidBackground(false)
, m_blackWidget(new BlackWidget(this))
, m_resetGeometryTimer(new QTimer(this))
, m_shutdownBlackWidget(nullptr)
{
#ifndef QT_DEBUG
if (!m_model->isUseWayland()) {
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
} else {
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Window);
setAttribute(Qt::WA_NativeWindow); // 创建窗口 handle
// onScreenDisplay 低于override,高于tooltip,希望显示在锁屏上方的界面,均需要调整层级为onScreenDisplay或者override
windowHandle()->setProperty("_d_dwayland_window-type", "onScreenDisplay");
DPlatformWindowHandle handle(this, this);
handle.setWindowRadius(-1);
}
#endif
frameList.append(this);
m_useSolidBackground = DConfigHelper::instance()->getConfig("useSolidBackground", false).toBool();
m_blackWidget->setBlackMode(m_model->isBlackMode());
connect(m_model, &SessionBaseModel::blackModeChanged, this, [this](bool is_black) {
m_blackWidget->setBlackMode(is_black);
if (currentContent) {
// 黑屏模式的时候需要隐藏中间的窗口
currentContent->setVisible(!is_black);
currentContent->raise();
}
});
connect(m_resetGeometryTimer, &QTimer::timeout, this, [this] {
const auto ¤tGeometry = geometry();
if (currentGeometry != m_geometryRect) {
qCDebug(DDE_SHELL) << "Current geometry:" << currentGeometry <<"setGeometry:" << m_geometryRect;
setGeometry(m_geometryRect);
}
});
connect(m_model, &SessionBaseModel::shutdownkModeChanged, this, [this] (bool value){
if (!m_shutdownBlackWidget) {
m_shutdownBlackWidget = new ShutdownBlackWidget(this);
}
qCInfo(DDE_SHELL) << "FullScreenBackground size : " << size();
m_shutdownBlackWidget->setFixedSize(this->size());
m_shutdownBlackWidget->setBlackMode(value);
});
}
FullScreenBackground::~FullScreenBackground()
{
frameList.removeAll(this);
}
void FullScreenBackground::updateBackground(const QString &path)
{
if (m_useSolidBackground || !isPicture(path))
return;
if (path == originBackgroundPath && !blurBackgroundPath.isEmpty()) {
updateScreenBluBackground(blurBackgroundPath);
return;
}
originBackgroundPath = path;
updateBlurBackground(path);
}
void FullScreenBackground::updateScreenBluBackground(const QString &blurPath)
{
if (!blurPath.isEmpty() && blurBackgroundPath != blurPath) {
blurBackgroundPath = blurPath;
}
QString scaledPath;
if (getScaledBlurImage(blurPath, scaledPath)) {
QPixmap pixmap;
loadPixmap(scaledPath, pixmap);
addPixmap(pixmap, PIXMAP_TYPE_BLUR_BACKGROUND);
} else {
handleBackground(blurBackgroundPath, PIXMAP_TYPE_BLUR_BACKGROUND);
}
if (isVisible()) {
update();
}
}
void FullScreenBackground::updateBlurBackground(const QString &path)
{
QDBusMessage message = QDBusMessage::createMethodCall(DSS_DBUS::imageEffectService, DSS_DBUS::imageEffectPath,
DSS_DBUS::imageEffectService, "Get");
message << "" << path;
QDBus::CallMode callMode = isVisible() ? QDBus::BlockWithGui : QDBus::Block;
QDBusPendingReply<QString> reply = QDBusConnection::systemBus().call(message, callMode, 2 * 1000);
QString blurPath;
if (!reply.isError()) {
blurPath = reply.value();
bool isPicture = QFile::exists(blurPath) && QFile(blurPath).size() && checkPictureCanRead(blurPath);
if (!isPicture) {
blurPath = "/usr/share/backgrounds/default_background.jpg";
}
} else {
blurPath = "/usr/share/backgrounds/default_background.jpg";
qCWarning(DDE_SHELL) << "Get blur background path error:" << reply.error().message();
}
// 处理模糊背景图和执行动画或update;
updateScreenBluBackground(blurPath);
}
bool FullScreenBackground::contentVisible() const
{
return currentContent && currentContent->isVisible() && currentContent->parent() == this;
}
void FullScreenBackground::setEnterEnable(bool enable)
{
m_enableEnterEvent = enable;
}
void FullScreenBackground::setScreen(QPointer<QScreen> screen, bool isVisible)
{
if (screen.isNull()) {
qCWarning(DDE_SHELL) << "Screen is nullptr";
return;
}
qCInfo(DDE_SHELL) << "Set screen:" << screen
<< ", screen geometry:" << screen->geometry()
<< ", full screen background object:" << this
<< ", visible:" << isVisible;
if (isVisible) {
updateCurrentFrame(this);
} else {
// 如果有多个屏幕则根据鼠标所在的屏幕来显示
QTimer::singleShot(1000, this, [this] {
setMouseTracking(true);
});
}
updateScreen(screen);
// 更新屏幕后设置壁纸,这样避免处理多个尺寸的壁纸
updateBackground(m_model->currentUser()->greeterBackground());
}
void FullScreenBackground::setContent(QWidget *const w)
{
if (!w) {
qCWarning(DDE_SHELL) << "Content is null";
return;
}
qCInfo(DDE_SHELL) << "Incoming content:" << w << ", current content:" << currentContent;
// 不重复设置content
if (currentContent && currentContent->isVisible() && currentContent == w && currentFrame && w->parent() == currentFrame) {
qCDebug(DDE_SHELL) << "Parent is current frame";
return;
}
if (!currentContent.isNull()) {
currentContent->setParent(nullptr);
currentContent->hide();
}
currentContent = w;
if (!currentFrame) {
qCWarning(DDE_SHELL) << "Current frame is null";
return;
}
currentContent->setParent(currentFrame);
currentContent->move(0, 0);
currentContent->resize(currentFrame->size());
currentFrame->setFocusProxy(currentContent);
currentFrame->setFocus();
currentFrame->activateWindow();
// 如果是黑屏状态则不置顶
if (!currentFrame->m_blackWidget->isVisible()) {
currentContent->raise();
currentContent->show();
};
auto lockContent = dynamic_cast<LockContent *>(w);
if (lockContent)
Q_EMIT lockContent->parentChanged();
}
void FullScreenBackground::setIsHibernateMode()
{
updateGeometry();
updateCurrentFrame(this);
}
bool FullScreenBackground::isPicture(const QString &file)
{
return QFile::exists(file) && QFile(file).size() && checkPictureCanRead(file);
}
QString FullScreenBackground::getLocalFile(const QString &file)
{
const QUrl url(file);
return url.isLocalFile() ? url.toLocalFile() : url.url();
}
void FullScreenBackground::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
const QRect trueRect(QPoint(0, 0), QSize(size() * devicePixelRatioF()));
if (m_useSolidBackground) {
painter.fillRect(trueRect, QColor(DDESESSIONCC::SOLID_BACKGROUND_COLOR));
} else {
const QPixmap &blurBackground = getPixmap(PIXMAP_TYPE_BLUR_BACKGROUND);
if (blurBackground.isNull()) {
painter.fillRect(trueRect, QColor(DDESESSIONCC::SOLID_BACKGROUND_COLOR));
} else {
painter.drawPixmap(trueRect,
blurBackground,
QRect(trueRect.topLeft(), trueRect.size() * devicePixelRatioF()));
}
}
}
void FullScreenBackground::tryActiveWindow(int count /* = 9*/)
{
if (count < 0 || m_model->isUseWayland())
return;
qCDebug(DDE_SS) << "try active window..." << count;
if (isActiveWindow()) {
qCDebug(DDE_SS) << "...finally activewindow is me ...";
return;
}
activateWindow();
if (currentContent && !currentContent->isVisible()) {
qCDebug(DDE_SS) << "hide..." << count;
return;
}
QTimer::singleShot(50, this, std::bind(&FullScreenBackground::tryActiveWindow, this, count - 1));
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void FullScreenBackground::enterEvent(QEnterEvent *event)
#else
void FullScreenBackground::enterEvent(QEvent *event)
#endif
{
qCInfo(DDE_SS) << "Enter event, enable enter event:" << m_enableEnterEvent << ", visible:" << m_model->visible();
if (m_enableEnterEvent && m_model->visible()) {
updateCurrentFrame(this);
// 多屏情况下,此Frame晚于其它Frame显示出来时,可能处于未激活状态(特别是在wayland环境下比较明显)
activateWindow();
}
// 锁屏截图之后 active window 不是锁屏了,此时发现不是 active window 主动尝试激活
if (!isActiveWindow() && contentVisible())
tryActiveWindow();
return QWidget::enterEvent(event);
}
void FullScreenBackground::leaveEvent(QEvent *event)
{
return QWidget::leaveEvent(event);
}
void FullScreenBackground::resizeEvent(QResizeEvent *event)
{
if (!blurBackgroundCacheMap.isEmpty() && isPicture(blurBackgroundPath) && !contains(PIXMAP_TYPE_BLUR_BACKGROUND)) {
QString scaledPath;
if (getScaledBlurImage(blurBackgroundPath, scaledPath)) {
QPixmap pixmap;
loadPixmap(scaledPath, pixmap);
addPixmap(pixmap, PIXMAP_TYPE_BLUR_BACKGROUND);
} else {
handleBackground(blurBackgroundPath, PIXMAP_TYPE_BLUR_BACKGROUND);
}
}
updatePixmap();
m_blackWidget->resize(size());
if (currentFrame == this && currentContent) {
currentContent->resize(size());
}
QWidget::resizeEvent(event);
}
/**
* @brief 鼠标移动触发这个事件,可能需要配合setMouseTracking(true)使用
*
* @param event
*/
void FullScreenBackground::mouseMoveEvent(QMouseEvent *event)
{
if (m_model->visible() && m_enableEnterEvent) {
updateCurrentFrame(this);
}
QWidget::mouseMoveEvent(event);
}
void FullScreenBackground::keyPressEvent(QKeyEvent *e)
{
QWidget::keyPressEvent(e);
switch (e->key()) {
#ifdef QT_DEBUG
case Qt::Key_Escape:
qApp->quit();
break;
#endif
default:
break;
}
}
void FullScreenBackground::showEvent(QShowEvent *event)
{
qCDebug(DDE_SHELL) << "Frame is already displayed:" << this;
if (m_model->isUseWayland()) {
Q_EMIT requestDisableGlobalShortcutsForWayland(true);
}
// fix bug 155019 此处是针对wayland下屏保退出,因qt没有发送enterEvent事件,进而导致密码框没有显示的规避方案
// x下面先激活其它窗口右键菜单,锁屏后,再激活dock右键菜单自动锁屏后,同样存在没有发送enterEvent事件的问题
// 如果鼠标的位置在当前屏幕内且锁屏状态为显示,将密码框显示出来
if (m_model->visible() && geometry().contains(QCursor::pos())) {
currentContent->show();
// 多屏情况下,此Frame晚于其它Frame显示出来时,可能处于未激活状态(特别是在wayland环境下比较明显)
activateWindow();
}
#ifndef ENABLE_DSS_SNIPE
// setScreen中有设置updateGeometry,单屏不需要再次设置
if (qApp->screens().size() > 1) {
updateGeometry();
}
#else
updateGeometry();
#endif
// 显示的时候需要置顶,截图在上方的话无法显示锁屏 见Bug-140545
raise();
m_blackWidget->setBlackMode(m_model->isBlackMode());
return QWidget::showEvent(event);
}
void FullScreenBackground::hideEvent(QHideEvent *event)
{
qCDebug(DDE_SHELL) << "Frame is hidden:" << this;
if (m_model->isUseWayland()) {
Q_EMIT requestDisableGlobalShortcutsForWayland(false);
}
QWidget::hideEvent(event);
}
void FullScreenBackground::updateScreen(QPointer<QScreen> screen)
{
if (screen == m_screen)
return;
if (!m_screen.isNull())
disconnect(m_screen, &QScreen::geometryChanged, this, &FullScreenBackground::updateGeometry);
if (!screen.isNull()) {
connect(screen, &QScreen::geometryChanged, this, &FullScreenBackground::updateGeometry);
}
m_screen = screen;
updateGeometry();
}
void FullScreenBackground::updateGeometry()
{
if (m_screen.isNull()) {
return;
}
qCInfo(DDE_SHELL) << "Set background screen:" << m_screen << ", geometry:" << m_screen->geometry();
// for bug:184943.系统修改分辨率后,登录界面获取的屏幕分辨率不正确,通过xrandr获取屏幕分辨率
if (m_model->appType() == AuthCommon::Login && !m_model->isUseWayland()) {
const auto screensGeometry = getScreenGeometryByXrandr();
if (screensGeometry.contains(m_screen->name())) {
// 如果qt获取的屏幕分辨率和xrandr获取的屏幕分辨率一致,使用qt获取的屏幕geometry
QSize qtScreenSize = m_screen->size();
QSize xrandrScreenSize = screensGeometry[m_screen->name()].size();
if (qtScreenSize == xrandrScreenSize) {
// fix205519。获取到的屏幕尺寸一致,但qt的位置可能是错的,但不影响窗口位置。
setddeGeometry(m_screen->geometry());
} else {
setddeGeometry(screensGeometry[m_screen->name()]);
qCDebug(DDE_SHELL) << "Set geometry by xrandr, this:" << this << screensGeometry[m_screen->name()]
<< " screen:" << m_screen->name() << "screen geometry:" << m_screen->geometry();
}
} else {
setddeGeometry(m_screen->geometry());
}
return;
}
if (!m_screen.isNull()) {
if(m_model->isUseWayland())
windowHandle()->setScreen(m_screen);
setddeGeometry(m_screen->geometry());
qCInfo(DDE_SHELL) << "Update geometry, screen:" << m_screen
<< ", screen geometry:" << m_screen->geometry()
<< ", lockFrame:" << this
<< ", frame geometry:" << this->geometry();
} else {
qCWarning(DDE_SHELL) << "Screen is nullptr";
}
}
/********************************************************
* 监听主窗体属性。
* 用户登录界面,主窗体在某时刻会被设置为WindowDeactivate,
* 此时登录界面获取不到焦点,需要调用requestActivate激活窗体。
********************************************************/
bool FullScreenBackground::event(QEvent *e)
{
#ifndef QT_DEBUG
if (e->type() == QEvent::WindowDeactivate) {
if (contentVisible()) {
tryActiveWindow();
}
}
#endif
if (e->type() == QEvent::MouseButtonPress) {
setFocus();
}
return QWidget::event(e);
}
const QPixmap &FullScreenBackground::getPixmap(int type)
{
static QPixmap nullPixmap;
QString strSize = sizeToString(trueSize());
if (PIXMAP_TYPE_BLUR_BACKGROUND == type && blurBackgroundCacheMap.contains(strSize)) {
return blurBackgroundCacheMap[strSize];
}
return nullPixmap;
}
QSize FullScreenBackground::trueSize() const
{
return size() * devicePixelRatioF();
}
/**
* @brief FullScreenBackground::addPixmap
* 新增pixmap,存在相同size的则替换,没有则新增
* @param pixmap pixmap对象
* @param type 清晰壁纸还是模糊壁纸
*/
void FullScreenBackground::addPixmap(const QPixmap &pixmap, const int type)
{
QString strSize = sizeToString(trueSize());
if (type == PIXMAP_TYPE_BLUR_BACKGROUND) {
blurBackgroundCacheMap[strSize] = pixmap;
}
}
/**
* @brief FullScreenBackground::updatePixmap
* 更新壁纸列表,移除当前所有屏幕都不会使用的壁纸数据
*/
void FullScreenBackground::updatePixmap()
{
auto removeNotUsedPixmap = [](QMap<QString, QPixmap> &cacheMap, const QStringList &frameSizeList) {
QStringList cacheMapKeys = cacheMap.keys();
for (auto &key : cacheMapKeys) {
if (!frameSizeList.contains(key)) {
qCInfo(DDE_SHELL) << "Remove not used pixmap:" << key;
cacheMap.remove(key);
}
}
};
QStringList frameSizeList;
for (auto &frame : frameList) {
QString strSize = sizeToString(frame->trueSize());
frameSizeList.append(strSize);
}
removeNotUsedPixmap(blurBackgroundCacheMap, frameSizeList);
}
bool FullScreenBackground::contains(int type)
{
auto containPixmap = [](const QMap<QString, QPixmap> &cacheMap, const QString &strSize) {
if (cacheMap.contains(strSize)) {
const QPixmap &pixmap = cacheMap[strSize];
return !pixmap.isNull(); // pixmap 为空也返回false
} else {
return false;
}
};
QString strSize = sizeToString(trueSize());
if (PIXMAP_TYPE_BLUR_BACKGROUND == type) {
return containPixmap(blurBackgroundCacheMap, strSize);
}
return false;
}
void FullScreenBackground::moveEvent(QMoveEvent *event)
{
// 规避 bug189309,登录时后端屏幕变化通知太晚
if (currentContent && !currentContent->isVisible()) {
if (m_model->isUseWayland()) {
if (m_model->visible() && geometry().contains(QCursor::pos())) {
currentContent->show();
}
}
}
QWidget::moveEvent(event);
}
QMap<QString, QRect> FullScreenBackground::getScreenGeometryByXrandr()
{
QMap<QString, QRect> screensGeometry;
// 获取缩放比例
double scale = getScaleFactorFromDisplay();
double scaleFromDisplayQt = qApp->devicePixelRatio();
qCInfo(DDE_SHELL) << "Get scale factor from display scale:" << scale << " scale from display qt:" << scaleFromDisplayQt;
// 如果获取失败或者与从Qt获取的不一致,则不需要处理,使用Qt的屏幕信息
if (scale <= 0 || scaleFromDisplayQt != scale) {
return screensGeometry;
}
// 启动 xrandr | grep connected 进程
QProcess process;
QStringList arguments;
arguments << "-c"
<< "xrandr | grep connected";
process.start("/bin/sh", arguments);
process.waitForStarted();
process.waitForFinished();
// 读取进程输出并解析屏幕信息
QString output = QString::fromLocal8Bit(process.readAll());
QRegularExpression regex("(\\S+) connected (?:primary )?(\\d+)x(\\d+)\\+(\\d+)\\+(\\d+)");
QRegularExpressionMatchIterator iter = regex.globalMatch(output);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
if (match.hasMatch()) {
QString name = match.captured(1);
int width = match.captured(2).toInt();
int height = match.captured(3).toInt();
int x = match.captured(4).toInt();
int y = match.captured(5).toInt();
QRect rect(x, y, width / scale, height / scale);
screensGeometry.insert(name, rect);
qCInfo(DDE_SHELL) << "Screen name:" << name << ", screen rect:" << rect;
}
}
return screensGeometry;
}
double FullScreenBackground::getScaleFactorFromDisplay()
{
QDBusInterface display(DSS_DBUS::systemDisplayService,
DSS_DBUS::systemDisplayPath,
DSS_DBUS::systemDisplayService,
QDBusConnection::systemBus(), this);
QDBusReply<QString> reply = display.call("GetConfig");
QString jsonObjStr = reply.value();
if (jsonObjStr.isNull()) {
qCWarning(DDE_SHELL) << "Greeter get system display config failed (`GetConfig` null)";
return -1.0;
}
// 获取 ScaleFactors 对象
QJsonObject json = QJsonDocument::fromJson(jsonObjStr.toUtf8()).object();
QJsonObject scaleFactors = json.value("Config").toObject().value("ScaleFactors").toObject();
// 遍历 ScaleFactors 对象
for (const auto &key : scaleFactors.keys()) {
return scaleFactors.value(key).toDouble();
}
qCWarning(DDE_SHELL) << "greeter get system display config failed(`scaleFactors` null)";
return -1.0;
}
void FullScreenBackground::updateCurrentFrame(FullScreenBackground *frame)
{
if (!frame) {
qCWarning(DDE_SHELL) << "Update current frame failed, frame is null";
return;
}
if (frame->m_screen)
qCInfo(DDE_SHELL) << "Update current frame:" << frame << ", screen:" << frame->m_screen->name();
else
qWarning() << "Frame's screen is null, frame:" << frame;
currentFrame = frame;
setContent(currentContent);
}
void FullScreenBackground::handleBackground(const QString &path, int type)
{
QSize trueSize = this->trueSize();
QPixmap pixmap;
loadPixmap(path, pixmap);
pixmap = pixmap.scaled(trueSize, Qt::KeepAspectRatioByExpanding, Qt::FastTransformation);
pixmap = pixmap.copy(QRect((pixmap.width() - trueSize.width()) / 2,
(pixmap.height() - trueSize.height()) / 2,
trueSize.width(),
trueSize.height()));
// draw pix to widget, so pix need set pixel ratio from qwidget devicepixelratioF
pixmap.setDevicePixelRatio(devicePixelRatioF());
addPixmap(pixmap, type);
}
QString FullScreenBackground::sizeToString(const QSize &size)
{
return QString("%1x%2").arg(size.width()).arg(size.height());
}
bool FullScreenBackground::getScaledBlurImage(const QString &originPath, QString &scaledPath)
{
// 为了兼容没有安装壁纸服务环境;Qt5.15高版本可以使用activatableServiceNames()遍历然后可判断系统有没有安装服务
const QString wallpaperServicePath = "/lib/systemd/system/dde-wallpaper-cache.service";
if (!QFile::exists(wallpaperServicePath)) {
qWarning() << "dde-wallpaper-cache service not existed";
return false;
}
// 壁纸服务dde-wallpaper-cache
QDBusInterface wallpaperCacheInterface("org.deepin.dde.WallpaperCache", "/org/deepin/dde/WallpaperCache",
"org.deepin.dde.WallpaperCache", QDBusConnection::systemBus());
QFile file(originPath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open file.";
return false;
}
const int fd = file.handle();
if (fd <= 0) {
qWarning() << "originPath file fd error";
return false;
}
QDBusUnixFileDescriptor dbusFd(fd);
const QString &pathmd5 = QCryptographicHash::hash(originPath.toUtf8(), QCryptographicHash::Md5).toHex();
QVariantList sizeArray;
sizeArray << QVariant::fromValue(this->trueSize());
QDBusReply<QStringList> pathList = wallpaperCacheInterface.call("GetProcessedImagePathByFd", QVariant::fromValue(dbusFd), pathmd5, sizeArray);
if (pathList.value().isEmpty()) {
qWarning() << "GetProcessedImagePathByFd interface return null";
return false;
}
QString path = pathList.value().at(0);
if (!path.isEmpty() && path != originPath) {
scaledPath = path;
// 图片处理完之后会添加_xxxx尺寸后缀,升级场景第一次登录只能获取到正在处理的原生图片,此时不能进行设置
if (!path.contains("_")) {
return false;
}
qDebug() << "get scaled path:" << path;
return true;
}
return false;
}
//增加一个定时器,每隔50ms再设置一次Geometry,避免出现xorg初始化未完成的情况,导致界面显示不全
void FullScreenBackground::setddeGeometry(const QRect &rect)
{
setGeometry(rect);
m_geometryRect = rect;
m_resetGeometryTimer->start(200);
QTimer::singleShot(400 * 5, m_resetGeometryTimer, &QTimer::stop);
}