ukui-clock/tinycountdown.cpp

354 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) 2020, Tianjin KYLIN Information Technology Co., Ltd.
*
* 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, or (at your option)
* 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/&gt;.
*
*/
#include "tinycountdown.h"
#include "ui_tinycountdown.h"
#include "theme.h"
#include <QBitmap>
#include "utils.h"
extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);
tinyCountdown::tinyCountdown(QWidget *parent) :
QWidget(parent),
ui(new Ui::tinyCountdown)
{
ui->setupUi(this);
//右上角关闭
closeStyle();
switchStyle();
suspendRunBtnStyle();
finishBtnStyle();
connect(ui->closeBtn, SIGNAL(clicked()), this, SLOT(set_dialog_close()) );
//左上闹钟图标
ui->clockIcon->setPixmap(QIcon::fromTheme("kylin-alarm-clock").pixmap(24,24));
//闹钟时间字体改为灰色
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::gray);
//主窗口connect
connect(ui->mainBtn, SIGNAL(clicked()), this, SLOT(showMainWindow()) );
//显示,但不可移动
settingsStyle();
updateWidgetRadius();
}
void tinyCountdown::updateWidgetRadius()
{
this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
this->setWindowFlags(Qt::FramelessWindowHint|Qt::ToolTip); //设置无边框窗口
this->setAttribute(Qt::WA_AlwaysShowToolTips);
// Qt::WindowFlags m_flags = windowFlags();
// this->setWindowFlags(m_flags | Qt::WindowStaysOnTopHint);
// 添加窗管协议
XAtomHelper::setStandardWindowHint(this->winId());
//圆角
QBitmap bmp(this->size());
bmp.fill();
QPainter p(&bmp);
p.setRenderHint(QPainter::Antialiasing); // 反锯齿;
p.setPen(Qt::NoPen);
p.setBrush(palette().color(QPalette::Base));
p.drawRoundedRect(bmp.rect(),WINDOWN_RADIUS,WINDOWN_RADIUS);
setMask(bmp);
}
tinyCountdown::~tinyCountdown()
{
delete ui;
}
/**
* @brief 显示倒计时时间
*/
void tinyCountdown::updateTimeInfo(QString str)
{
ui->timeLabel->setText(str);
ui->timeLabel->setAlignment(Qt::AlignVCenter);
}
/*
void tinyCountdown::focusOutEvent(QFocusEvent *event)
{
qDebug()<<"dbq-"<<"focusOutEvent";
}
void tinyCountdown::focusInEvent(QFocusEvent *event)
{
qDebug()<<"dbq-"<<"focusInEvent";
}
*/
// 在鼠标光标在小部件内部时按下鼠标按钮时调用或者当小部件使用grabMouse ()抓住鼠标时调用。按下鼠标而不释放它实际上与调用grabMouse ()相同。
void tinyCountdown::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
this->dragPosition = event->globalPos() - frameGeometry().topLeft();
this->mousePressed = true;
}
QWidget::mousePressEvent(event);
}
//在鼠标按钮被释放时被调用。当小部件接收到相应的鼠标按下事件时,它就会接收鼠标释放事件。这意味着如果用户在您的小部件内按下鼠标,然后在释放鼠标按钮之前将鼠标拖动到其他地方,您的小部件将收到释放事件。
void tinyCountdown::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
this->mousePressed = false;
}
QWidget::mouseReleaseEvent(event);
this->setCursor(Qt::ArrowCursor);
}
//每当鼠标移动而按住鼠标按钮时调用。这在拖放操作期间很有用。
void tinyCountdown::mouseMoveEvent(QMouseEvent *event)
{
if (this->mousePressed) {
move(event->globalPos() - this->dragPosition);
this->setCursor(Qt::ClosedHandCursor);
}
QWidget::mouseMoveEvent(event);
}
void tinyCountdown::closeEvent(QCloseEvent *event)
{
event->ignore();//忽视
this->hide();
}
void tinyCountdown::paintEvent(QPaintEvent * event)
{
Q_UNUSED(event);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QPainterPath rectPath;
rectPath.addRoundedRect(this->rect().adjusted(SHADOW_RANGE, SHADOW_RANGE, -SHADOW_RANGE, -SHADOW_RANGE), WINDOWN_RADIUS, WINDOWN_RADIUS);
// 画一个黑底
QPixmap pixmap(this->rect().size());
pixmap.fill(Qt::transparent);
QPainter pixmapPainter(&pixmap);
pixmapPainter.setRenderHint(QPainter::Antialiasing);
auto shadowColor = palette().text().color();
shadowColor.setAlphaF(SHADOWCOLOR_ALPHAF);
pixmapPainter.setBrush(shadowColor);
pixmapPainter.setPen(Qt::transparent);
pixmapPainter.drawPath(rectPath);
pixmapPainter.end();
// 模糊这个黑底
QImage img = pixmap.toImage();
qt_blurImage(img, VAGUE_RADIUS, false, false);
// 挖掉中心
pixmap = QPixmap::fromImage(img);
QPainter pixmapPainter2(&pixmap);
pixmapPainter2.setRenderHint(QPainter::Antialiasing);
pixmapPainter2.setCompositionMode(QPainter::CompositionMode_Clear);
pixmapPainter2.setPen(Qt::transparent);
pixmapPainter2.setBrush(Qt::transparent);
pixmapPainter2.drawPath(rectPath);
// 绘制阴影
p.drawPixmap(this->rect(), pixmap, pixmap.rect());
QStyleOption opt;
opt.init(this);
// 绘制一个背景
p.save();
//描边
QColor borderColor = palette().text().color();
borderColor.setAlphaF(BORDERCOLOR_ALPHAF);
p.setPen(borderColor);
p.translate(BORDER_RANGE, BORDER_RANGE);
p.setBrush(palette().color(QPalette::Base));
p.drawPath(rectPath);
p.restore();
}
/**
* @brief 清空数据
*/
void tinyCountdown::clearData()
{
ui->timeLabel->setText("00:00:00");
this->hide();
updateOnRunState(true);
}
void tinyCountdown::set_dialog_close()
{
this->hide();
}
void tinyCountdown::showMainWindow()
{
this->hide();
emit mainWindowClick();
}
void tinyCountdown::showTop()
{
this->hide();
this->update();
this->show();
}
void tinyCountdown::updateOnRunState(int onRun)
{
m_onRun = onRun;
suspendRunBtnIcon(theme::themetype);
}
void tinyCountdown::settingsStyle()
{
GsettingSubject * subject = GsettingSubject::getInstance();;
connect(subject,&GsettingSubject::blackStyle, this,[=](){
this->blackStyle();
});
connect(subject,&GsettingSubject::whiteStyle, this,[=](){
this->whiteStyle();
});
connect(subject,&GsettingSubject::iconChnaged, this,[=](){
ui->clockIcon->setPixmap(QIcon::fromTheme("kylin-alarm-clock").pixmap(24,24));
});
subject->iniWidgetStyle();
}
void tinyCountdown::whiteStyle()
{
ui->mainBtn->setIcon(QIcon(":image/miniIcon/restore_active-w.png"));
finishBtnIcon(0);
suspendRunBtnIcon(0);
}
void tinyCountdown::blackStyle()
{
ui->mainBtn->setIcon(QIcon(":image/miniIcon/restore-active-b.png"));
finishBtnIcon(1);
suspendRunBtnIcon(1);
}
void tinyCountdown::closeStyle()
{
ui->closeBtn->setToolTip(tr("close"));
ui->closeBtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
ui->closeBtn->setProperty("isWindowButton", 0x2);
ui->closeBtn->setProperty("useIconHighlightEffect", 0x8);
ui->closeBtn->setFlat(true);
}
void tinyCountdown::switchStyle()
{
ui->mainBtn->setToolTip(tr("main window"));
ui->mainBtn->setProperty("useButtonPalette", true);
ui->mainBtn->setIcon(QIcon(":image/miniIcon/restore_active-w.png"));
ui->mainBtn->setIconSize(QSize(24,24));
//手动改成透明色
Utils::setBtnBackgroundColorTransparent(ui->mainBtn);
}
/**
* @brief 暂停运行按钮样式
*/
void tinyCountdown::suspendRunBtnStyle()
{
ui->suspendRunBtn->setToolTip(tr("suspend"));
ui->suspendRunBtn->setRadius(14);
ui->suspendRunBtn->setFlag(RoundBtn::ICON_FLAG);
ui->suspendRunBtn->setIconSize(QSize(32,32));
QColor defaultColor = QColor(0, 0, 0, 0);
ui->suspendRunBtn->setBtnColor(defaultColor,defaultColor,defaultColor);
//初始化是运行中
m_onRun = true;
suspendRunBtnIcon(theme::themetype);
connect(ui->suspendRunBtn,&QPushButton::clicked,this,[=](){
m_onRun = !m_onRun;
suspendRunBtnIcon(theme::themetype);
emit suspendClick(m_onRun);
});
}
void tinyCountdown::finishBtnStyle()
{
ui->finishBtn->setToolTip(tr("finish"));
ui->finishBtn->setRadius(12);
ui->finishBtn->setFlag(RoundBtn::ICON_FLAG);
ui->finishBtn->setIconSize(QSize(28,28));
QColor defaultColor = QColor(0, 0, 0, 0);
ui->finishBtn->setBtnColor(defaultColor,defaultColor,defaultColor);
finishBtnIcon(theme::themetype);
connect(ui->finishBtn,&QPushButton::clicked,this,[=](){
emit finishClick();
});
}
void tinyCountdown::finishBtnIcon(int themeStatus)
{
if(themeStatus==0){
ui->finishBtn->setAllIconPatn(":image/miniIcon/finish-light-normal.png"
,":image/miniIcon/finish-light-hover.png"
,":image/miniIcon/finish-light-click.png");
}else{
ui->finishBtn->setAllIconPatn(":image/miniIcon/finish-dark-normal.png"
,":image/miniIcon/finish-dark-hover.png"
,":image/miniIcon/finish-dark-click.png");
}
}
void tinyCountdown::suspendRunBtnIcon(int themeStatus)
{
qDebug()<<"dbq-m_onRun"<<m_onRun;
if(m_onRun){
if(themeStatus==0){
ui->suspendRunBtn->setAllIconPatn(":image/miniIcon/suspend-normal-light.png"
,":image/miniIcon/suspend-hover-light.png"
,":image/miniIcon/suspend-click-light.png");
}else{
ui->suspendRunBtn->setAllIconPatn(":image/miniIcon/suspend-normal-dark.png"
,":image/miniIcon/suspend-hover-dark.png"
,":image/miniIcon/suspend-click-dark.png");
}
}else{
if(themeStatus==0){
ui->suspendRunBtn->setAllIconPatn(":image/miniIcon/start-normal-light.png"
,":image/miniIcon/start-hover-light.png"
,":image/miniIcon/start-hover-light.png");
}else{
ui->suspendRunBtn->setAllIconPatn(":image/miniIcon/start-normal-dark.png"
,":image/miniIcon/start-hover-dark.png"
,":image/miniIcon/start-hover-dark.png");
}
}
}