1 Star 0 Fork 0

universe/timeDown

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TimeWidget.cpp 4.78 KB
一键复制 编辑 原始数据 按行查看 历史
universe 提交于 2021-04-04 14:21 . 优化铃声
#include "TimeWidget.h"
#include "ui_TimeWidget.h"
#include "Constant.h"
#include "mainapp.h"
#include "SqliteDao.h"
#include <QTimer>
#include <QDateTime>
#include <QPainter>
TimeWidget::TimeWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TimeWidget)
{
ui->setupUi(this);
player = new QMediaPlayer;
// player->setMedia(QUrl::fromLocalFile("/home/platinum/音乐/dingDong_daZhong.wav"));
player->setMedia(QUrl("qrc:/media/media/daZhong.wav"));
// player->set
player->setVolume(50);
/* player->play(); */
}
TimeWidget::~TimeWidget()
{
delete ui;
}
void TimeWidget::paintEvent(QPaintEvent *){
QPainter painter(this);
QColor colorPink(255,136,240);
QPen pen(colorPink,4);
painter.setPen(pen);
painter.drawArc(QRect(10,10,Constant::TW-20,Constant::TW-30),startAngle,timeDownLength);
}
void TimeWidget::init(TimeEntity timeData){
this->timeData = timeData;
ui->labelTitle->setText(timeData.title);
this->initTimes();
timer = new QTimer(this);
timer->setInterval(timeData.refreshTime);
timer->stop();
connect(timer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
}
void TimeWidget::initTimes(){
hour = timeData.hour;
minute = timeData.minute;
second = timeData.second;
if(second > 60){
second = second - 60;
minute ++;
}
if(minute > 60){
minute = minute - 60;
hour ++;
}
if(hour > 99){
hour = 99;
}
totalSecond = hour * 3600 + minute * 60 + second;
totalMillisecond = (long) (totalSecond * 1000);
timeSurplus = totalMillisecond;
totalSecond = hour * 3600 + minute * 60 + second;
repeatTime();
}
/**
* 刷新时间
*/
void TimeWidget::timeToStr(){
QString str;
if(hour < 10){str.append(Constant::STR_ZERO);}
str.append(QString::number(hour));
// str.append(hour);//错误写法
str.append(Constant::STR_MAO_HAO);
if(minute < 10){str.append(Constant::STR_ZERO);}
str.append(QString::number(minute));
str.append(Constant::STR_MAO_HAO);
if(second < 10){str.append(Constant::STR_ZERO);}
str.append(QString::number(second));
// str.toLatin1().data() str.toLocal8Bit().data()
// printf("a==1==%d:%d:%d ==2== %s--3--%d\n",
// hour,minute,second,str.toStdString().data(),timeData.refreshTime);
ui->labelTime->setText(QString::fromUtf8(str.toUtf8()));
}
void TimeWidget::repeatTime(){
//刷新时间
timeToStr();
//刷新进度
repaint();
}
/**
* 时间到了、重置
*/
void TimeWidget::timeOver(){
timer->stop();
isRunning = false;
changeBtnEditIcon(true);
changeBtnRSIcon();
}
void TimeWidget::changeBtnEditIcon(bool flag){
QIcon icon;
QString str = flag?Constant::IMG_EDIT_BLUE:Constant::IMG_EDIT_GREY;
icon.addFile(str, QSize(), QIcon::Normal, QIcon::Off);
ui->btnEdit->setEnabled(flag);
ui->btnEdit->setIcon(icon);
}
void TimeWidget::changeBtnRSIcon(){
QIcon icon;
QString str = timer->isActive()?Constant::IMG_STOP:Constant::IMG_START;
icon.addFile(str, QSize(), QIcon::Normal, QIcon::Off);
ui->btnRS->setIcon(icon);
}
void TimeWidget::on_btnDel_clicked()
{
Constant::objList.removeOne(this);
SqliteDao::instance()->sqlDelete(&timeData.title);
timer->stop();
player->stop();
delete this;
}
void TimeWidget::on_timer_timeout(){
//计算剩余时分秒
timeSurplus = totalMillisecond - (QDateTime::currentMSecsSinceEpoch() - timeStart);
hour = (int) (timeSurplus / ih);
tm = timeSurplus - hour * ih;
minute = (int) (tm /im);
second = (int) ((tm - minute * im) / is) + 1;
//计算圆弧
tmpArc = (double)timeSurplus / totalMillisecond;
timeDownLength = tmpArc * timeDown360;
startAngle = startAngleParam - timeDownLength;
// printf("%ld\n",QDateTime::currentMSecsSinceEpoch());
if (timeSurplus < 1){
timeDownLength = 0;
second = 0;
timeSurplus = 0;
timeOver();
player->play();
}
repeatTime();
}
void TimeWidget::on_btnRS_clicked()
{
if(isRunning){
if(timer->isActive()){//暂停
timer->stop();
timePauseStart = QDateTime::currentMSecsSinceEpoch();
}else{//开始
timer->start();
//再次开始时向后推移开始计时的时间点
timeStart += QDateTime::currentMSecsSinceEpoch() - timePauseStart;
}
}else{
timeStart = QDateTime::currentMSecsSinceEpoch();
isRunning = true;
changeBtnEditIcon(false);
step = timeDown360/totalSecond;
timer->start();
}
changeBtnRSIcon();
}
void TimeWidget::on_btnReset_clicked()
{
timeDownLength = timeDown360;
startAngle = 0;
initTimes();
timeOver();
player->stop();
}
void TimeWidget::on_btnEdit_clicked()
{
MainApp::mainApp->isEdit = true;
MainApp::mainApp->timeDataEdit(this);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ITKing9/timeDown.git
[email protected]:ITKing9/timeDown.git
ITKing9
timeDown
timeDown
master

搜索帮助