2023-02-16 13:51:55 +08:00
|
|
|
// Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2018-10-28 15:50:20 +08:00
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
#include "shutdownwidget.h"
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
|
|
#include <QSvgRenderer>
|
|
|
|
#include <QPainter>
|
2020-06-11 16:06:43 +08:00
|
|
|
#include <QPainterPath>
|
2018-10-28 15:50:20 +08:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QApplication>
|
2018-12-07 16:40:58 +08:00
|
|
|
#include <QIcon>
|
2018-10-28 15:50:20 +08:00
|
|
|
|
2019-09-03 09:35:11 +08:00
|
|
|
#include <DStyle>
|
2019-09-17 13:15:53 +08:00
|
|
|
#include <DGuiApplicationHelper>
|
2019-09-03 09:35:11 +08:00
|
|
|
|
|
|
|
DWIDGET_USE_NAMESPACE;
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
ShutdownWidget::ShutdownWidget(QWidget *parent)
|
2019-01-07 09:14:37 +08:00
|
|
|
: QWidget(parent)
|
2019-09-03 09:35:11 +08:00
|
|
|
, m_hover(false)
|
|
|
|
, m_pressed(false)
|
2018-10-28 15:50:20 +08:00
|
|
|
{
|
2019-09-03 09:35:11 +08:00
|
|
|
setMouseTracking(true);
|
2019-09-06 14:44:07 +08:00
|
|
|
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
|
2019-10-11 10:08:24 +08:00
|
|
|
|
|
|
|
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [ = ] {
|
|
|
|
update();
|
|
|
|
});
|
2018-10-28 15:50:20 +08:00
|
|
|
}
|
|
|
|
|
2022-05-19 22:09:08 +08:00
|
|
|
QPixmap ShutdownWidget::loadPixmap() const
|
|
|
|
{
|
2023-06-07 14:11:50 +08:00
|
|
|
const QString iconName = "system-shutdown";
|
|
|
|
const auto ratio = devicePixelRatioF();
|
|
|
|
return QIcon::fromTheme(iconName).pixmap(PLUGIN_ICON_MAX_SIZE * ratio);
|
2022-05-19 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
void ShutdownWidget::paintEvent(QPaintEvent *e)
|
2018-10-28 15:50:20 +08:00
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2019-09-03 09:35:11 +08:00
|
|
|
QPainter painter(this);
|
|
|
|
|
2019-09-06 14:44:07 +08:00
|
|
|
if (rect().height() > PLUGIN_BACKGROUND_MIN_SIZE) {
|
2019-09-03 09:35:11 +08:00
|
|
|
|
2019-09-17 13:15:53 +08:00
|
|
|
QColor color;
|
|
|
|
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
|
|
|
|
color = Qt::black;
|
|
|
|
painter.setOpacity(0.5);
|
|
|
|
|
|
|
|
if (m_hover) {
|
|
|
|
painter.setOpacity(0.6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pressed) {
|
|
|
|
painter.setOpacity(0.3);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
color = Qt::white;
|
|
|
|
painter.setOpacity(0.1);
|
|
|
|
|
|
|
|
if (m_hover) {
|
|
|
|
painter.setOpacity(0.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pressed) {
|
|
|
|
painter.setOpacity(0.05);
|
|
|
|
}
|
2019-09-06 14:44:07 +08:00
|
|
|
}
|
2019-09-03 09:35:11 +08:00
|
|
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
|
|
|
DStyleHelper dstyle(style());
|
|
|
|
const int radius = dstyle.pixelMetric(DStyle::PM_FrameRadius);
|
|
|
|
|
|
|
|
QPainterPath path;
|
2019-09-06 14:44:07 +08:00
|
|
|
|
|
|
|
int minSize = std::min(width(), height());
|
|
|
|
QRect rc(0, 0, minSize, minSize);
|
|
|
|
rc.moveTo(rect().center() - rc.center());
|
|
|
|
|
|
|
|
path.addRoundedRect(rc, radius, radius);
|
2019-09-03 09:35:11 +08:00
|
|
|
painter.fillPath(path, color);
|
2018-12-07 16:40:58 +08:00
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
2019-09-03 09:35:11 +08:00
|
|
|
painter.setOpacity(1);
|
2018-10-28 15:50:20 +08:00
|
|
|
|
2023-06-07 14:11:50 +08:00
|
|
|
QPixmap pixmap = loadPixmap();
|
2019-10-22 14:53:30 +08:00
|
|
|
|
2019-09-28 17:23:10 +08:00
|
|
|
const QRectF &rf = QRectF(rect());
|
|
|
|
const QRectF &rfp = QRectF(pixmap.rect());
|
|
|
|
painter.drawPixmap(rf.center() - rfp.center() / pixmap.devicePixelRatioF(), pixmap);
|
2018-10-28 15:50:20 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
void ShutdownWidget::mousePressEvent(QMouseEvent *event)
|
2019-09-03 09:35:11 +08:00
|
|
|
{
|
|
|
|
m_pressed = true;
|
|
|
|
update();
|
|
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
void ShutdownWidget::mouseReleaseEvent(QMouseEvent *event)
|
2019-09-03 09:35:11 +08:00
|
|
|
{
|
|
|
|
m_pressed = false;
|
|
|
|
m_hover = false;
|
|
|
|
update();
|
|
|
|
|
|
|
|
QWidget::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
void ShutdownWidget::mouseMoveEvent(QMouseEvent *event)
|
2019-09-03 09:35:11 +08:00
|
|
|
{
|
|
|
|
m_hover = true;
|
|
|
|
QWidget::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
void ShutdownWidget::leaveEvent(QEvent *event)
|
2019-09-03 09:35:11 +08:00
|
|
|
{
|
2019-10-24 15:13:16 +08:00
|
|
|
m_hover = false;
|
|
|
|
m_pressed = false;
|
|
|
|
update();
|
2019-09-03 09:35:11 +08:00
|
|
|
|
|
|
|
QWidget::leaveEvent(event);
|
|
|
|
}
|