Skip to content

Commit

Permalink
适配两个显示器;休息时长通过配置文件进行配置
Browse files Browse the repository at this point in the history
  • Loading branch information
djxc committed Mar 7, 2024
1 parent dcbfc95 commit 6065b35
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 45 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 休息休息
# 休息一下

- 1. 通过qt,采用定时任务创建休息提示软件,默认一个小时提示一次
- 2. 随机从bing壁纸获取图片作为背景图
Expand All @@ -11,4 +11,8 @@


## 打包
- 1、软件直接生成exe在其他电脑上不可直接运行,因此需要用qt自带的windeployqt.exe工具搜集软件的依赖库,具体执行`..\windeployqt.exe dj_rest.exe`.qt中可能会有msvc以及mingw编译程序,采用哪个编译软件就用哪个下的windeployqt.exe
- 1、软件直接生成exe在其他电脑上不可直接运行,因此需要用qt自带的windeployqt.exe工具搜集软件的依赖库,具体执行`..\windeployqt.exe dj_rest.exe`.qt中可能会有msvc以及mingw编译程序,采用哪个编译软件就用哪个下的windeployqt.exe,如D:\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin\windeployqt.exe.


## 细节
- 1、托盘图标一直不显示是因为icon路径不对,需要先创建qt资源文件qrc,然后添加前缀,最后添加文件即可。使用方法通过前缀加上文件名称即可。
3 changes: 3 additions & 0 deletions dj_rest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[config]
restTime=10
workTime=60
7 changes: 6 additions & 1 deletion dj_rest.pro
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
DISTFILES += \
.gitignore \
README.md \
favicon.ico
dj_rest.ini \
favicon.ico \
icon.png

RC_ICONS = favicon.ico

QT += network

RESOURCES += \
qt.qrc
10 changes: 5 additions & 5 deletions dj_rest.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2024-02-20T15:32:09. -->
<!-- Written by QtCreator 4.11.1, 2024-03-07T14:43:19. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -67,7 +67,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MSVC2017 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MSVC2017 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_msvc2017_64_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
Expand Down Expand Up @@ -119,14 +119,14 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/code/c/dj_rest</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/code/c/build-dj_rest-Desktop_Qt_5_14_2_MSVC2017_64bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">false</value>
<value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">true</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.SeparateDebugInfo">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.UseQtQuickCompiler">true</value>
</valuemap>
Expand Down Expand Up @@ -299,7 +299,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/code/c/build-dj_rest-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/code/c/build-dj_rest-Desktop_Qt_5_14_2_MSVC2017_64bit-Release</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 18 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
#include "mainwindow.h"

#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
QList<QScreen *> screenList = QGuiApplication::screens();
qDebug("screen size: %d;", screenList.size());
QMainWindow window;
if (screenList.size() > 1) {
MainWindow w;
w.init(0);
w.show();
MainWindow w2;
w2.init(1);
w2.show();
return a.exec();
} else {
MainWindow w;
w.init(0);
w.show();
return a.exec();
}
}
125 changes: 101 additions & 24 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
#include <cstdlib>
#include <QDebug>
#include <ctime>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
settings = new QSettings(":/dj_rest.ini", QSettings::IniFormat);
int time1 = settings->value("config/restTime").toInt();
if (time1 > 0 && time1 < 20 * 60) {
this->restTime = time1;
}
createTrayIcon();
ui->setupUi(this);
this->currentBackgroundColor = this->createRandomColor();
this->setWindowTitle("hava a rest");
setWindowFlags(Qt::WindowStaysOnTopHint|Qt::WindowMaximizeButtonHint|Qt::WindowMinimizeButtonHint);
setWindowState(Qt::WindowMinimized);
this->init();
setWindowFlags(Qt::WindowStaysOnTopHint|Qt::WindowMaximizeButtonHint|Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint);
}

void MainWindow::init(){
void MainWindow::init(uint screenId){
this->screen = QGuiApplication::screens().at(screenId);
_Timer = new QTimer(this);
_Timer->setInterval(workTime); //一个小时
// _Timer->setInterval(1000 * 30); //30秒
Expand All @@ -41,16 +48,20 @@ void MainWindow::resting() {
uint now = timeDate.toTime_t(); // 将当前时间转为时间戳
if ((now - this->restTimestamp) > restTime) {
this->isResting = false;
this->changeLabel("工作中.....");
setWindowState(Qt::WindowMinimized);
}
if (this->isResting) {
this->hide();
} else {
if (imageData.size() > 0) {
this->setBackgroundImage();
} else {
this->setBackgroundColor();
}
this->changeLabel();
QString newTimeStr = this->refreshTime();
this->changeLabel(newTimeStr);
this->maxWindow();
QRect rect = this->screen->availableGeometry();
this->setGeometry(rect);
}
}
}
Expand All @@ -65,22 +76,15 @@ void MainWindow::timerFunc()
{
QDateTime timeDate = QDateTime::currentDateTime(); // 获取当前时间
this->restTimestamp = timeDate.toTime_t(); // 将当前时间转为时间戳
// 随机选择是背景图片还是单一颜色,如果获取web图片失败则采用颜色背景
int r = rand()%100;
qDebug("run time event! background type: %d; now: %d", r, this->restTimestamp);
this->currentBackgroundColor = this->createRandomColor();
if (r > 5) {
try {
ImageDownloader* imageDownloader = new ImageDownloader();
this->imageData = imageDownloader->getRandomImageData();
try {
this->setBackgroundImage();
} catch(_exception) {
qDebug("load image error");
this->setBackgroundColor();
}
} else {
this->setBackgroundColor();
this->setBackgroundImage();
} catch(_exception) {
qDebug("load image error");
this->currentBackgroundColor = this->createRandomColor();
this->imageData = NULL;
this->setBackgroundColor();
}
this->isResting = true;
}
Expand Down Expand Up @@ -118,29 +122,51 @@ void MainWindow::setBackgroundImage() {
*/
void MainWindow::maxWindow() {
this->setFocus();
this->show();
setWindowState(Qt::WindowMaximized);//初始状态最大化
}

/**
* @brief MainWindow::changeLabel
* 修改label内容,休息时间内每秒更新一次时间
* @brief MainWindow::refreshTime
* 更新休息倒计时
* @return QString
*/
void MainWindow::changeLabel() {
QLabel *label = this->ui->label;
QString MainWindow::refreshTime() {
QDateTime timeDate = QDateTime::currentDateTime(); // 获取当前时间
uint nowTime = timeDate.toTime_t(); // 将当前时间转为时间戳
uint leftTime = restTime - (nowTime - restTimestamp);
// 将剩余时间转换为分钟和秒
uint seconds = leftTime % 60;
uint minutes = leftTime / 60;
QString labelTxt = QString("该休息了, 站起来活动一下吧.休息剩余时间:%1m%2s").arg(minutes).arg(seconds);
return labelTxt;
}

/**
* @brief MainWindow::changeLabel
* 修改label内容,休息时间内每秒更新一次时间
*/
void MainWindow::changeLabel(QString labelTxt) {
QLabel *label = this->ui->label;
label->setText(labelTxt);
label->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
label->setMargin(10);
label->move(20, 10);
label->adjustSize();
}



void MainWindow::changeEvent(QEvent *event) {
if (isMinimized()) {
if (this->tip_hide == false) {
this->hide();
systemIcon->showMessage("have a rest", "后台运行", QSystemTrayIcon::MessageIcon::Information, 300);
this->tip_hide = true;
}
}
}

void MainWindow::resizeEvent(QResizeEvent *event) {
if (this->isInit) {
if (imageData.size() > 0) {
Expand All @@ -153,6 +179,57 @@ void MainWindow::resizeEvent(QResizeEvent *event) {
}
}

/**
* @brief MainWindow::closeEvent
* 拦截close事件
* @param event
*/
void MainWindow::closeEvent(QCloseEvent *event) {
setWindowState(Qt::WindowMinimized);
event->ignore();
}

/**
* @brief MainWindow::createTrayIcon
* 创建托盘
*/
void MainWindow::createTrayIcon() {
systemIcon = new QSystemTrayIcon(this);

systemIcon->setToolTip("这是系统系统图标"); // 设置提示语
systemIcon->setIcon(QIcon(":/icon.png")); // 设置图标

// 增加托盘菜单
QMenu *menu = new QMenu();
QAction *closeAction = new QAction("关闭");
menu->addAction(closeAction);
systemIcon->setContextMenu(menu);
connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(close()));
systemIcon->show();
// 关联点击拖盘事件
connect(systemIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this, SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
// 要在show之后调用
// systemIcon->showMessage("have a rest", "程序正在后台运行",QSystemTrayIcon::MessageIcon::Information,500);
}

/**
* @brief MainWindow::close
* 关闭程序
*/
void MainWindow::close() {
QApplication::quit();
}

/**
* @brief MainWindow::on_activatedSysTrayIcon
* 点击托盘
* @param reason
*/
void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
this->show();
}
}

/**
* @brief MainWindow::createRandomColor
Expand Down
25 changes: 21 additions & 4 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <QMainWindow>
#include <QTimer>
#include <QBuffer>
#include<QScreen>
#include <QDesktopWidget>
#include <QSystemTrayIcon>
#include <QCloseEvent>
#include <QEvent>
#include <QSettings>

#include "imagedownloader.h"

Expand All @@ -18,30 +24,41 @@ class MainWindow : public QMainWindow
public:
MainWindow(QWidget *parent = nullptr);
void resizeEvent(QResizeEvent *re);
void init();
void changeEvent(QEvent *event);
void closeEvent(QCloseEvent *event);
void init(uint screenId);

~MainWindow();


private slots:
void timerFunc();
void resting();
void close();
void on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason);

private:
uint restTime = 60 * 5; // 休息时间s
uint restTime = 10;//60 * 5; // 休息时间s
int workTime = 1000 * 60 * 60; // 工作时间毫秒
bool isInit = false;
bool isResting = false;
uint restTimestamp;
QByteArray imageData;
QColor currentBackgroundColor;
Ui::MainWindow *ui;
QTimer* _Timer;
QTimer* _Timer;
QScreen* screen;
QSystemTrayIcon* systemIcon;
bool tip_hide = false;
QSettings *settings;

void maxWindow();
QColor createRandomColor();
void setBackgroundColor();
void setBackgroundImage();
void changeLabel();
void changeLabel(QString labelTxt);
QString refreshTime();
void createTrayIcon();

};
#endif // MAINWINDOW_H
4 changes: 2 additions & 2 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<rect>
<x>10</x>
<y>10</y>
<width>141</width>
<width>211</width>
<height>61</height>
</rect>
</property>
Expand All @@ -31,7 +31,7 @@
</font>
</property>
<property name="text">
<string>休息一下</string>
<string>工作中……</string>
</property>
</widget>
</widget>
Expand Down
6 changes: 6 additions & 0 deletions qt.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>icon.png</file>
<file>dj_rest.ini</file>
</qresource>
</RCC>
Loading

0 comments on commit 6065b35

Please sign in to comment.