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-12-21 17:28:21 +08:00
|
|
|
|
|
|
|
#include "onboarditem.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
2020-06-11 16:06:43 +08:00
|
|
|
#include <QPainterPath>
|
2018-12-21 17:28:21 +08:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QIcon>
|
|
|
|
|
2019-09-05 15:30:14 +08:00
|
|
|
#include <DStyle>
|
2019-09-17 13:15:53 +08:00
|
|
|
#include <DGuiApplicationHelper>
|
2019-09-05 15:30:14 +08:00
|
|
|
|
|
|
|
DWIDGET_USE_NAMESPACE;
|
|
|
|
|
2018-12-21 17:28:21 +08:00
|
|
|
OnboardItem::OnboardItem(QWidget *parent)
|
2019-01-07 09:14:37 +08:00
|
|
|
: QWidget(parent)
|
2019-09-05 15:30:14 +08:00
|
|
|
, m_hover(false)
|
|
|
|
, m_pressed(false)
|
2018-12-21 17:28:21 +08:00
|
|
|
{
|
2019-09-05 15:30:14 +08:00
|
|
|
setMouseTracking(true);
|
|
|
|
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
|
2019-10-11 10:08:24 +08:00
|
|
|
|
|
|
|
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [ = ] {
|
|
|
|
update();
|
|
|
|
});
|
2019-10-22 14:53:30 +08:00
|
|
|
m_icon = QIcon::fromTheme(":/icons/icon/deepin-virtualkeyboard.svg");
|
2018-12-21 17:28:21 +08:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:55:34 +08:00
|
|
|
QPixmap OnboardItem::iconPixmap(QSize size, DGuiApplicationHelper::ColorType themeType) const
|
2022-11-09 02:31:27 +00:00
|
|
|
{
|
|
|
|
QString iconName = "deepin-virtualkeyboard";
|
|
|
|
if (std::min(width(), height()) <= PLUGIN_BACKGROUND_MIN_SIZE
|
2023-01-12 13:55:34 +08:00
|
|
|
|| themeType == DGuiApplicationHelper::LightType)
|
2022-11-09 02:31:27 +00:00
|
|
|
iconName.append(PLUGIN_MIN_ICON_NAME);
|
|
|
|
|
2023-01-12 13:55:34 +08:00
|
|
|
return loadSvg(iconName, size);
|
2018-12-21 17:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnboardItem::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2019-09-05 15:30:14 +08:00
|
|
|
QPainter painter(this);
|
|
|
|
if (std::min(width(), height()) > PLUGIN_BACKGROUND_MIN_SIZE) {
|
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-05 15:30:14 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 14:44:07 +08:00
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
|
|
|
DStyleHelper dstyle(style());
|
|
|
|
const int radius = dstyle.pixelMetric(DStyle::PM_FrameRadius);
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
|
|
|
|
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-05 15:30:14 +08:00
|
|
|
painter.fillPath(path, color);
|
2018-12-21 17:28:21 +08:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:55:34 +08:00
|
|
|
QPixmap pixmap = iconPixmap(QSize(PLUGIN_ICON_MAX_SIZE, PLUGIN_ICON_MAX_SIZE), DGuiApplicationHelper::instance()->themeType());
|
2019-09-17 13:15:53 +08:00
|
|
|
painter.setOpacity(1);
|
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() / devicePixelRatioF(), pixmap);
|
2018-12-21 17:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QPixmap OnboardItem::loadSvg(const QString &fileName, const QSize &size) const
|
|
|
|
{
|
2019-04-23 14:28:09 +08:00
|
|
|
const auto ratio = devicePixelRatioF();
|
2018-12-21 17:28:21 +08:00
|
|
|
|
2022-11-30 16:46:54 +08:00
|
|
|
QSize pixmapSize = QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? size : (size * ratio);
|
2022-12-19 15:05:49 +08:00
|
|
|
QPixmap pixmap = QIcon::fromTheme(fileName, m_icon).pixmap(pixmapSize);
|
2018-12-21 17:28:21 +08:00
|
|
|
pixmap.setDevicePixelRatio(ratio);
|
2023-01-12 22:13:52 +08:00
|
|
|
pixmap = pixmap.scaled(size * ratio);
|
2018-12-21 17:28:21 +08:00
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
2019-09-05 15:30:14 +08:00
|
|
|
|
|
|
|
void OnboardItem::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
m_pressed = true;
|
|
|
|
update();
|
|
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnboardItem::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
m_pressed = false;
|
|
|
|
m_hover = false;
|
|
|
|
update();
|
|
|
|
|
|
|
|
QWidget::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnboardItem::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
m_hover = true;
|
|
|
|
|
|
|
|
QWidget::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnboardItem::leaveEvent(QEvent *event)
|
|
|
|
{
|
2019-10-24 15:13:16 +08:00
|
|
|
m_hover = false;
|
|
|
|
m_pressed = false;
|
|
|
|
update();
|
2019-09-05 15:30:14 +08:00
|
|
|
|
|
|
|
QWidget::leaveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnboardItem::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
}
|