dde-dock/plugins/onboard/onboarditem.cpp

160 lines
4.1 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: listenerri <listenerri@gmail.com>
*
* Maintainer: listenerri <listenerri@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "onboarditem.h"
#include <QPainter>
2020-06-11 16:06:43 +08:00
#include <QPainterPath>
#include <QMouseEvent>
#include <QApplication>
#include <QIcon>
2019-09-05 15:30:14 +08:00
#include <DStyle>
#include <DGuiApplicationHelper>
2019-09-05 15:30:14 +08:00
DWIDGET_USE_NAMESPACE;
OnboardItem::OnboardItem(QWidget *parent)
: QWidget(parent)
2019-09-05 15:30:14 +08:00
, m_hover(false)
, m_pressed(false)
{
2019-09-05 15:30:14 +08:00
setMouseTracking(true);
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [ = ] {
update();
});
2019-10-22 14:53:30 +08:00
m_icon = QIcon::fromTheme(":/icons/icon/deepin-virtualkeyboard.svg");
}
QPixmap OnboardItem::iconPixmap(int iconSize) const
{
QString iconName = "deepin-virtualkeyboard";
if (std::min(width(), height()) <= PLUGIN_BACKGROUND_MIN_SIZE
|| DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
iconName.append(PLUGIN_MIN_ICON_NAME);
return loadSvg(iconName, QSize(iconSize, iconSize));
}
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) {
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);
}
QPixmap pixmap = iconPixmap(PLUGIN_ICON_MAX_SIZE);
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);
}
const QPixmap OnboardItem::loadSvg(const QString &fileName, const QSize &size) const
{
const auto ratio = devicePixelRatioF();
QPixmap pixmap;
2019-10-22 14:53:30 +08:00
pixmap = QIcon::fromTheme(fileName, m_icon).pixmap(size * ratio);
pixmap.setDevicePixelRatio(ratio);
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);
}