-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqresttimecounter.cpp
178 lines (128 loc) · 3.99 KB
/
qresttimecounter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <QtWidgets>
#include "qresttimecounter.h"
#include "ui_qresttimecounter.h"
#include "mainwindow.h"
#include "time2restdlg.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
bool isCfgExist();
void createAutoStartCfg(bool bAutoStart);
QRestTimeCounter::QRestTimeCounter(QWidget *parent) :
QDialog(parent),
ui(new Ui::QRestTimeCounter)
{
ui->setupUi(this);
exitAct = new QAction(tr("&Exit"), this);
exitAct->setStatusTip(tr("Exit eyeProtector"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(onExit()));
showMainUiAct = new QAction(tr("&Settings"), this);
showMainUiAct->setStatusTip(tr("Show main window"));
connect(showMainUiAct, SIGNAL(triggered()), this, SLOT(onSetting()));
setStyleSheet("background-color:#ccff99;");
m_timerPerSec = new QTimer(this);
connect(m_timerPerSec,SIGNAL(timeout()),this,SLOT(onTimerCounterEvent()));
m_iCounter = 0;
startMonitor();
ui->lcdNumber->resize(width(),height());
QSettings settings("eyeProtector.ini", QSettings::IniFormat);
int xPos = settings.value("xPos",QApplication::desktop()->width() - 200).toInt();
int yPos = settings.value("yPos",QApplication::desktop()->height() - 200).toInt();
move(xPos,yPos);
if (!isCfgExist())
{
createAutoStartCfg(settings.value("autostart",true).toBool());
const char *homedir;
if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
char cmd[1024];
sprintf(cmd,"sudo desktop-file-install %s/.config/autostart/eyeProtector.desktop",homedir);
system(cmd);
}
}
QRestTimeCounter::~QRestTimeCounter()
{
m_timerPerSec->stop();
delete ui;
}
void QRestTimeCounter::onExit()
{
close();
QApplication::quit();
}
void QRestTimeCounter::onSetting()
{
MainWindow *w = new MainWindow();
w->setAttribute(Qt::WA_DeleteOnClose);
w->show();
connect(w,SIGNAL(settingsChanged()),this,SLOT(onStartMonitor()));
}
#include <X11/Xlib.h>
extern void x11_window_set_on_top ( Window xid);
void QRestTimeCounter::onTimerCounterEvent()
{
--m_iCounter;
if (m_iCounter < 0)
{
m_timerPerSec->stop();
time2restDlg *dlg = new time2restDlg();
dlg->setWindowFlags(Qt::FramelessWindowHint);
dlg->setAttribute(Qt::WA_DeleteOnClose);
x11_window_set_on_top(dlg->winId());
dlg->showFullScreen();
connect(dlg,SIGNAL(restDlgClosed()),this,SLOT(onStartMonitor()));
return;
}
QString info = "";
if (m_iCounter > 3600)
{
info.sprintf("%02d:%02d:%02d",m_iCounter / 3600,(m_iCounter % 3600)/ 60,m_iCounter % 60);
}
else
{
info.sprintf("%02d:%02d",m_iCounter / 60, m_iCounter % 60);
}
ui->lcdNumber->display(info);
}
void QRestTimeCounter::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(showMainUiAct);
menu.addSeparator();
menu.addAction(exitAct);
menu.exec(event->globalPos());
}
void QRestTimeCounter::startMonitor()
{
m_timerPerSec->stop();
QSettings settings("eyeProtector.ini", QSettings::IniFormat);
int timeout = settings.value("locktime",60).toInt();
m_iCounter = timeout * 60;
m_timerPerSec->start(1000);
}
void QRestTimeCounter::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
QPoint pos = event->globalPos() - m_dragPosition;
move(pos.x(),pos.y());
QSettings settings("eyeProtector.ini", QSettings::IniFormat);
settings.setValue("xPos", pos.x());
settings.setValue("yPos", pos.y());
event->accept();
}
}
void QRestTimeCounter::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void QRestTimeCounter::keyPressEvent(QKeyEvent* /*event*/){
}
void QRestTimeCounter::onStartMonitor()
{
startMonitor();
}