加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gugu.cpp 3.49 KB
一键复制 编辑 原始数据 按行查看 历史
bacon 提交于 2019-02-19 16:57 . :tada::neutral_face:
#include "gugu.h"
#include "ui_gugu.h"
#include <QAction>
#include <QMenu>
#include <QTimer>
#include <QDateTime>
#if _DEBUG
#include <QtDebug>
#endif
gugu::gugu(QWidget *parent) :
QDialog(parent),
ui(new Ui::gugu)
{
ui->setupUi(this);
setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); // remove "?" on dialog window
setWindowIcon(QIcon(":/icon/misaki"));
setWindowTitle("lalala");
start_hr = 900;
end_hr = 1800;
noon_start_hr = 1200;
noon_end_hr = 1400;
skip_noon = true;
week_mode = WEEKDAY;
half_hour = false;
resetDayHr();
createActions();
createTrayIcon();
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(updateTimer()));
timer->start(1000);
menuLastEvents.push_back(QEvent::None);
menuLastEvents.push_back(QEvent::None);
}
gugu::~gugu()
{
delete ui;
}
void gugu::createActions()
{
check_half_hour_action = new QAction(tr("&half-hour gu"), this);
check_half_hour_action->setCheckable(true);
check_half_hour_action->setChecked(half_hour);
connect(check_half_hour_action, &QAction::changed, this, &gugu::half_hour_checked);
}
void gugu::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(check_half_hour_action);
trayIconMenu->installEventFilter(this);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/icon/misaki"));
trayIcon->show();
}
void gugu::updateTimer()
{
QDateTime now = QDateTime::currentDateTime();
int day = now.date().dayOfWeek();
if(!checkDay(day))
return;
int hhmm = now.toString("hhmm").toInt();
if(checkTime(hhmm))
evokeAlarm();
}
bool gugu::checkDay(int day_of_week)
{
if(today != day_of_week)
resetDayHr();
switch(week_mode) {
case WEEKDAY:
if(day_of_week >=1 && day_of_week <= 5)
return true;
break;
case EVERYDAY:
return true;
}
return false;
}
bool gugu::checkTime(int hhmm)
{
if(today_start_hr > end_hr)
resetDayHr();
if(hhmm > today_start_hr && hhmm <= end_hr) {
if(skip_noon && (hhmm > noon_start_hr && hhmm < noon_end_hr))
return false;
if(hhmm % 100 == 0) {
today_start_hr = hhmm + 1;
return true;
}
else if(half_hour && hhmm % 100 == 30) {
return true;
}
}
return false;
}
void gugu::evokeAlarm()
{
trayIcon->showMessage("gugu",
"gu gu",
QIcon(":/misaki"),
2000);
}
void gugu::half_hour_checked()
{
if(check_half_hour_action->isChecked())
half_hour = true;
else if(!check_half_hour_action->isChecked())
half_hour = false;
}
void gugu::resetDayHr()
{
today = QDate::currentDate().dayOfWeek();
today_start_hr = start_hr;
}
bool gugu::eventFilter(QObject *obj, QEvent *event)
{
if(obj == trayIconMenu) {
menuLastEvents.pop_front();
menuLastEvents.push_back(event->type());
if(menuLastEvents[0] == QEvent::MouseButtonRelease &&
menuLastEvents[1] == QEvent::Close)
return true;
if(event->type() == QEvent::MouseButtonRelease) {
qDebug() << "mouse release";
return true;
}
else if(event->type() == QEvent::Close) {
qDebug() << "close";
return true;
}
}
return false;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化