This commit is contained in:
Yzzi
2020-08-14 20:27:22 +08:00
parent 2fed1bbb57
commit 3304a410c9
12 changed files with 1036 additions and 845 deletions

0
build.sh Executable file → Normal file
View File

43
countdown.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include "countdown.h"
countDown::countDown(QObject *parent) : QObject(parent)
{
timer=new QTimer(this);
connect(timer,&QTimer::timeout,this,&countDown::refresh);
}
void countDown::refresh()
{
if(ms)
{
ms--;
if(!ms) emit ended();//计时结束,触发结束信号,写在这里是为了让声音和画面同步
}
else
{
if(timer->isActive())timer->stop();
}
emit refreshed();
}
void countDown::setTime(int t)//弃坑,写不来
{
mem=ms=t;
emit setted();
}
void countDown::reset()
{
ms=mem;
refresh();
emit setted();
}
void countDown::start()
{
emit refreshed();
timer->start(1000);
}
void countDown::stop()
{
emit refreshed();
if(timer->isActive())timer->stop();
}

30
countdown.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef COUNTDOWN_H
#define COUNTDOWN_H
#include <QObject>
#include <QTimer>
class countDown : public QObject
{
Q_OBJECT
public:
explicit countDown(QObject *parent = nullptr);
int ms;//供Widget访问
bool end=true;//控制是否响应timeout信号
int mem=ms=1500;
QTimer *timer;
signals:
void refreshed();//刷新完成信号
void ended();//计时结束信号
void setted();//设置时间完成信号(会和refresh形成死循环废弃)
public slots:
void refresh();//刷新
void setTime(int t);//设置时间(菜是原罪,写不来,暂时废弃)
void reset();//重置时间
void start();//开始响应timeout信号
void stop();//停止响应timeout信号
};
#endif // COUNTDOWN_H

0
dabao/dabao.sh Executable file → Normal file
View File

View File

View File

@@ -26,10 +26,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\ SOURCES += main.cpp\
widget.cpp widget.cpp \
countdown.cpp \
toptime.cpp
HEADERS += \ HEADERS += \
widget.h widget.h \
countdown.h \
toptime.h
CONFIG += link_pkgconfig CONFIG += link_pkgconfig
PKGCONFIG += dtkwidget PKGCONFIG += dtkwidget

18
toptime.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "toptime.h"
#include <QDateTime>
topTime::topTime(QObject *parent) : QObject(parent)
{
current_time = QDateTime::currentDateTime();
str=current_time.toString("hh:mm");
timer=new QTimer(this);
connect(timer,&QTimer::timeout,this,&topTime::refresh);
timer->start(1000);
}
void topTime::refresh()
{
current_time = QDateTime::currentDateTime();
str=current_time.toString("hh:mm");
emit refreshed();
}

23
toptime.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef TOPTIME_H
#define TOPTIME_H
#include <QObject>
#include <QDateTime>
#include <QTimer>
class topTime : public QObject
{
Q_OBJECT
public:
explicit topTime(QObject *parent = nullptr);
QString str;//供Widget访问
QDateTime current_time;
QTimer *timer;
signals:
void refreshed();//刷新完成信号
public slots:
void refresh();//刷新槽函数
};
#endif // TOPTIME_H

View File

@@ -9,10 +9,8 @@
#include <DApplication> #include <DApplication>
#include <QDialog> #include <QDialog>
#include <DInputDialog> #include <DInputDialog>
#include "countdown.h"
using namespace std; using namespace std;
QTimer *timer = new QTimer();
int a;
bool timerswitch;
Widget::Widget(DBlurEffectWidget *parent) : Widget::Widget(DBlurEffectWidget *parent) :
DBlurEffectWidget(parent), DBlurEffectWidget(parent),
m_menu(new QMenu), m_menu(new QMenu),
@@ -27,14 +25,13 @@ Widget::Widget(DBlurEffectWidget *parent) :
ui(new Ui::Widget) ui(new Ui::Widget)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->gridLayout->setContentsMargins(0, 0, 0, 0);
ui->titlebar->setFixedHeight(50);//初始化标题栏 ui->titlebar->setFixedHeight(50);//初始化标题栏
ui->titlebar->setBackgroundTransparent(true);//设置标题栏透明 ui->titlebar->setBackgroundTransparent(true);//设置标题栏透明
ui->titlebar->setIcon(QIcon::fromTheme(":/icon/icon/top.yzzi.tomato.svg")); ui->titlebar->setIcon(QIcon::fromTheme(":/icon/icon/top.yzzi.tomato.svg"));
ui->titlebar->setTitle(""); ui->titlebar->setTitle("");
setMaskAlpha(190); setMaskAlpha(190);
ui->titlebar->setMenu(m_menu); ui->titlebar->setMenu(m_menu);
mem=ms = 1500+1;//设置初始时间
m_menu->addMenu(menu_times);//设置菜单 m_menu->addMenu(menu_times);//设置菜单
menu_times->setTitle(tr("时间")); menu_times->setTitle(tr("时间"));
menu_times->addAction(m_5); menu_times->addAction(m_5);
@@ -44,7 +41,6 @@ Widget::Widget(DBlurEffectWidget *parent) :
menu_times->addAction(m_45); menu_times->addAction(m_45);
menu_times->addAction(m_set); menu_times->addAction(m_set);
timesGroup = new QActionGroup(this);//设置按钮互斥 timesGroup = new QActionGroup(this);//设置按钮互斥
timesGroup->addAction(m_5); timesGroup->addAction(m_5);
timesGroup->addAction(m_15); timesGroup->addAction(m_15);
@@ -62,52 +58,61 @@ Widget::Widget(DBlurEffectWidget *parent) :
m_25->setChecked(true); m_25->setChecked(true);
//in->setParent(this); //in->setParent(this);
//设置按钮响应 //设置按钮响应
connect(m_5,&QAction::triggered,[=](){mem=ms = 300+1;timer->start(1000);refresh();}); connect(m_5,&QAction::triggered,[=](){count->mem=count->ms = 300;refresh();});
connect(m_15,&QAction::triggered,[=](){mem=ms = 900+1;timer->start(1000);refresh();}); connect(m_15,&QAction::triggered,[=](){count->mem=count->ms = 900;refresh();});
connect(m_25,&QAction::triggered,[=](){mem=ms = 1500+1;timer->start(1000);refresh();}); connect(m_25,&QAction::triggered,[=](){count->mem=count->ms = 1500;refresh();});
connect(m_35,&QAction::triggered,[=](){mem=ms = 2100+1;timer->start(1000);refresh();}); connect(m_35,&QAction::triggered,[=](){count->mem=count->ms = 2100;refresh();});
connect(m_45,&QAction::triggered,[=](){mem=ms = 2700+1;timer->start(1000);refresh();}); connect(m_45,&QAction::triggered,[=](){count->mem=count->ms = 2700;refresh();});
connect(m_set,&QAction::triggered,[=](){input();}); connect(m_set,&QAction::triggered,[=](){input();refresh();});
//实例化对象
count=new countDown(this);
toptime=new topTime(this);
switchbutton = new DSwitchButton (this); switchbutton = new DSwitchButton (this);
ui->horizontalLayout_2->addWidget(switchbutton); ui->horizontalLayout_2->addWidget(switchbutton);
switchbutton->setChecked(false); switchbutton->setChecked(false);
timerswitch=false;
setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);// 禁止最大化按钮 setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);// 禁止最大化按钮
setFixedSize(this->width(),this->height());// 禁止拖动窗口大小 setFixedSize(this->width(),this->height());// 禁止拖动窗口大小
connect(switchbutton, SIGNAL(checkedChanged(bool)), this, SLOT(onSBtnSwitchButtonCheckedChanged(bool))); connect(switchbutton, SIGNAL(checkedChanged(bool)), this, SLOT(onSBtnSwitchButtonCheckedChanged(bool)));
//时间更新
timer->start(1000); //每隔1000ms发送timeout的信号
connect(timer, SIGNAL(timeout()), this,SLOT(refresh()));
refresh();
//结束时音频播放(待修改!)
//音频播放 player = new QMediaPlayer(this);
player = new QMediaPlayer;
player->setVolume(100); player->setVolume(100);
player->setMedia(QUrl("qrc:/audio/ding.wav")); player->setMedia(QUrl("qrc:/audio/ding.wav"));
//设置界面刷新和对象响应
connect(count,&countDown::refreshed,this,&Widget::refreshCount);
connect(toptime,&topTime::refreshed,this,&Widget::refreshTime);
connect(count,&countDown::ended,player,&QMediaPlayer::play);
//时间控制
timer->start(1000); //每隔1000ms发送timeout的信号
refreshCount();
refreshTime();
} }
void Widget::refresh()
void Widget::refresh()//刷新count的开始结束动作
{ {
ostringstream timeshow; if(btSwitch)
if(timerswitch==true)
{ {
if(ms==0) ms=mem; if(!count->ms) count->reset();
ms-=1; count->start();
QDateTime tomato_time = QDateTime::fromTime_t(ms); }
else
{
count->stop();
}
}
void Widget::refreshTime()//用toptime的信息刷新时间显示
{
ui->titlebar->setTitle(toptime->str);
}
void Widget::refreshCount()//用count的信息刷新倒计时显示
{
QDateTime tomato_time = QDateTime::fromTime_t(count->ms);
QString textout=tomato_time.toString("mm:ss"); QString textout=tomato_time.toString("mm:ss");
ui->time->setText(textout); ui->time->setText(textout);
if(ms==0)
{
player->play();
timer->stop();
}
}else {
QDateTime current_time = QDateTime::currentDateTime();
QString str=current_time.toString("hh:mm");
ui->time->setText(str);
}
} }
DTitlebar* Widget::getTitlebar() DTitlebar* Widget::getTitlebar()
{ {
@@ -151,22 +156,37 @@ void Widget::input()
timer->start(1000); timer->start(1000);
refresh(); refresh();
} }
void Widget::input()//输入时间
{
bool isOK;
int i = DInputDialog::getInt(this, "输入时间",
"请输入时间分钟小于60",
count->mem/60,0,100,1,&isOK);
if(isOK)
{
count->ms=count->mem=60*i;
}
switch (count->mem) {
case 300+1:
m_5->setChecked(true);
break;
case 900+1:
m_15->setChecked(true);
break;
case 1500+1:
m_25->setChecked(true);
break;
case 2100+1:
m_35->setChecked(true);
break;
case 2700+1:
m_45->setChecked(true);
break;
}
}
void Widget::onSBtnSwitchButtonCheckedChanged(bool ck) void Widget::onSBtnSwitchButtonCheckedChanged(bool ck)
{ {
if(ck==true)
{
timerswitch=true;
timer->start(1000);
/*Widget::player->play();*/
}
else
{
timerswitch=false;
timer->start(1000);
/*Widget::player->stop();*/
}
player->stop(); player->stop();
btSwitch=ck;
refresh(); refresh();
} }

View File

@@ -7,7 +7,8 @@
#include <QMediaPlayer> #include <QMediaPlayer>
#include <DBlurEffectWidget> #include <DBlurEffectWidget>
#include <DTitlebar> #include <DTitlebar>
#include "countdown.h"
#include "toptime.h"
DWIDGET_USE_NAMESPACE DWIDGET_USE_NAMESPACE
namespace Ui { namespace Ui {
@@ -24,35 +25,35 @@ public:
QMediaPlayer *player; QMediaPlayer *player;
QMediaPlaylist *playlist; QMediaPlaylist *playlist;
DTitlebar* getTitlebar(); DTitlebar* getTitlebar();
bool btSwitch;
private slots: private slots:
void onSBtnSwitchButtonCheckedChanged(bool); void onSBtnSwitchButtonCheckedChanged(bool);
void refresh(); void refresh();
void refreshTime();
void refreshCount();
void input(); void input();
private: private:
Ui::Widget *ui; Ui::Widget *ui;
DSwitchButton * switchbutton; DSwitchButton * switchbutton;
QTimer *timer = new QTimer(); QTimer *timer = new QTimer();
int a;
bool timerswitch;
int ms;
int mem;
DTitlebar *titlebar; DTitlebar *titlebar;
QMenu *m_menu; QMenu *m_menu;
QActionGroup *timesGroup; QActionGroup *timesGroup;
QAction *m_full;
QAction *m_5; QAction *m_5;
QAction *m_15; QAction *m_15;
QAction *m_25; QAction *m_25;
QAction *m_35; QAction *m_35;
QAction *m_45; QAction *m_45;
QAction *m_set; QAction *m_set;
countDown *count;
topTime *toptime;
QMenu *menu_times=new QMenu; QMenu *menu_times=new QMenu;
}; };
#endif // WIDGET_H #endif // WIDGET_H

122
widget.ui
View File

@@ -7,35 +7,21 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>470</width> <width>470</width>
<height>254</height> <height>265</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>470</width>
<height>265</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<widget class="QWidget" name="layoutWidget"> <layout class="QGridLayout" name="gridLayout">
<property name="geometry"> <item row="0" column="0">
<rect>
<x>0</x>
<y>0</y>
<width>471</width>
<height>182</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<widget class="DTitlebar" name="titlebar" native="true"> <widget class="DTitlebar" name="titlebar" native="true">
<property name="minimumSize"> <property name="minimumSize">
@@ -46,6 +32,19 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
@@ -63,6 +62,18 @@
</item> </item>
<item> <item>
<widget class="QLabel" name="time"> <widget class="QLabel" name="time">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>431</width> <width>431</width>
@@ -75,7 +86,7 @@
</font> </font>
</property> </property>
<property name="text"> <property name="text">
<string>24:33</string> <string>25:00</string>
</property> </property>
</widget> </widget>
</item> </item>
@@ -108,18 +119,54 @@
</spacer> </spacer>
</item> </item>
</layout> </layout>
</widget> </item>
<widget class="QWidget" name="layoutWidget"> <item row="1" column="0">
<property name="geometry"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<rect> <item>
<x>0</x> <spacer name="horizontalSpacer_3">
<y>180</y> <property name="orientation">
<width>471</width> <enum>Qt::Horizontal</enum>
<height>54</height>
</rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_2"/> <property name="sizeHint" stdset="0">
</widget> <size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="DSwitchButton" name="switchbutton" native="true"/>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>50</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>
@@ -128,6 +175,11 @@
<header location="global">dtitlebar.h</header> <header location="global">dtitlebar.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>DSwitchButton</class>
<extends>QWidget</extends>
<header>dswitchbutton.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>