#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMouseEvent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
setMouseTracking(true);
ui->setupUi(this);
bg_player = new QMediaPlayer;
MuYu_player = new QMediaPlayer;
this->setWindowTitle("电子木鱼");
this->setFixedSize(WIDTH,HEIGH);
this->setWindowIcon(QIcon(ICON));
m_TopPropertyAnimation = new QPropertyAnimation(this);
pos=ui->label_2->pos();
timer = new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(Auto()));
bgMusice();
tray();
}
Widget::~Widget()
{
delete ui;
}
void Widget::tray()
{
m_systemTray = new QSystemTrayIcon(this);
m_systemTray->setIcon(QIcon(ICON));
m_systemTray->setToolTip("SystemTray Program");
m_systemTray->show();
}
// m=10 图签放大 pos点上移
// m=-10 图签缩小 pos点下移
void Widget::MuYu(int m)
{
//获取当前label图片宽
int currentWidth = ui->label->width();
//获取当前label图片高
int currentHeight = ui->label->height();
//改变图片大小
currentWidth += m;
currentHeight += m;
//在标签上重新设置图片大小和图片起始位置
ui->label->setGeometry(ui->label->pos().x()-m/2,ui->label->pos().y()-m/2,currentWidth, currentHeight);
}
void Widget::MuYuMusice()
{
//设置要播放的媒体
MuYu_player->setMedia(QUrl::fromLocalFile(MUYUMUSICE));
//设置音量
MuYu_player->setVolume(50);
//播放
MuYu_player->play();
}
void Widget::bgMusice()
{
qDebug()<<"dmz";
bg_player->setMedia(QUrl::fromLocalFile(BACKMUSICE));
bg_player->setVolume(10);
bg_player->play();
// 槽函数 监听QMediaPlayer::mediaStatusChanged信号 实现背景音乐循环播放
connect(bg_player, &QMediaPlayer::mediaStatusChanged,this,&Widget::initStatus);
}
void Widget::initStatus(QMediaPlayer::MediaStatus status)
{
if(status == QMediaPlayer::EndOfMedia)
{
bg_player->setPosition(0);
bg_player->play();
}
}
void Widget::gongde()
{
ui->label_2->setText("功德+1");
// bind
m_TopPropertyAnimation->setTargetObject(ui->label_2);
m_TopPropertyAnimation->setPropertyName("pos");
// set 动画的起点、终点、持续时间
m_TopPropertyAnimation->setDuration(600);
m_TopPropertyAnimation->setStartValue(pos);
m_TopPropertyAnimation->setEndValue(pos+QPoint(0, -120));
// 启动和结束
m_TopPropertyAnimation->start();
Delay(600);
ui->label_2->clear();
}
void Widget::mousePressEvent(QMouseEvent *event)
{
qDebug()<<"press";
MuYu(-10);
MuYuMusice();
gongde();
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
qDebug()<<"release";
MuYu(10);
}
void Widget::on_toolButton_clicked(bool checked)
{
if (checked) {
timer->start(500);
}
else {
timer->stop();
}
}
void Widget::on_toolButton_2_clicked(bool checked)
{
if(checked)
{
bg_player->stop();
}else {
bg_player->play();
}
}
void Widget::Auto()
{
qDebug()<<"Auto";
MuYu(-10);
MuYuMusice();
gongde();
MuYu(10);
}
//延时
void Widget::Delay(int delay_time)
{
QEventLoop loop;
QTimer::singleShot(delay_time,&loop,SLOT(quit()));
loop.exec();
}
|