-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller_window.cpp
213 lines (198 loc) · 4.73 KB
/
controller_window.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "controller_window.h"
#include "mainwindow.h"
#include "ui_controller_window.h"
#include "QMessageBox"
#include <QStandardPaths>
#include <QTimer>
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <QDesktopWidget>
#include <QDir>
#define TIMER_DURATION (min+(rand()%(max-min+1)))*1000
#define PHASETIMER_DURATION 7200000
#define MODE_CURSOR "Cursor Position"
#define MODE_RANDOM "Random Position"
#define MODE_CENTER "Screen Center"
controller_window::controller_window(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::controller_window)
{
srand(time(NULL));
ui->setupUi(this);
phase = 0;
timer = new QTimer(this);
stoptimer = new QTimer(this);
phasetimer = new QTimer(this);
phaseD = false;
this->phasetimer->setInterval(PHASETIMER_DURATION);
this->phasetimer->start();
//readConfig();
moveToCenter();
connect(timer,SIGNAL(timeout()),this,SLOT(ShowRC()));
connect(stoptimer,SIGNAL(timeout()),this,SLOT(Shutdown()));
connect(phasetimer,SIGNAL(timeout()),SLOT(AddPhase()));
connect(ui->goButton,SIGNAL(clicked()),this,SLOT(StartRC()));
connect(ui->stopCheck,SIGNAL(clicked()),this,SLOT(ToggleStops()));
connect(ui->exitButton,SIGNAL(clicked()),this,SLOT(Shutdown()));
}
controller_window::~controller_window()
{
delete ui;
}
//Work in progress function. No touchy -Mew
void controller_window::readConfig()
{
QString configDirPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)+PATH_SEPARATOR+APP_NAME;
if(!QDir(configDirPath).exists())
{
QDir().mkdir(configDirPath);
}
QString configPath = configDirPath+PATH_SEPARATOR+"config";
FILE *configFile = fopen(configPath.toStdString().c_str(),"r");
if(configFile!=NULL)
{
printf("Config file found, reading...\n");
char buffer[256];
while(fgets(buffer,sizeof(buffer),configFile))
{
QString curLine = buffer;
}
}
else
{
printf("Config file not found at location: %s\n"
"Creating file now...\n",configPath.toStdString().c_str());
configFile = fopen(configPath.toStdString().c_str(),"w");
if(configFile==NULL)
{
fprintf(stderr,"Cannot write to config file. Aborting and using program defaults.\n");
return;
}
else
{
fprintf(configFile,"#qtrc config file\n"
"MIN_DURATION=170\n"
"MAX_DURATION=270\n"
"LOCATION=CURSOR\n"
"STOP=no\n"
"STOP_HOURS=0\n"
"#do a RC!"
);
printf("Config file written!\n");
}
}
fclose(configFile);
}
void controller_window::StartRC()
{
min=this->ui->minDuration->text().toInt();
max=this->ui->maxDuration->text().toInt();
getPositionMode();
int stopCheck = (!this->ui->stopCheck->isChecked()||this->ui->stopHours->text().toInt()>0);
if(min>0 && max>=min && stopCheck)
{
this->hide();
timer->start(TIMER_DURATION);
if(this->ui->stopHours->text().toInt()>0 && this->ui->stopCheck->isChecked())
{
stoptimer->start(this->ui->stopHours->text().toInt()*60000);
}
}
else
{
QMessageBox msg;
msg.setText("Invalid Configuration");
if(min<=0)
{
msg.setInformativeText("Minimum duration must be greater than zero.");
}
else if(max<min)
{
msg.setInformativeText("Maximum duration must be greater or equal to minimum duration.");
}
else if(!stopCheck)
{
msg.setInformativeText("Hours must be greater than 0.");
}
msg.setStandardButtons(QMessageBox::Ok);
msg.setDefaultButton(QMessageBox::Ok);
msg.exec();
}
}
void controller_window::getPositionMode()
{
QString mode_str = this->ui->positionMode->currentText();
if(mode_str.compare(MODE_CURSOR)==0)
{
positionMode = RC_CURSOR;
}
else if(mode_str.compare(MODE_RANDOM)==0)
{
positionMode = RC_RANDOM;
}
else if(mode_str.compare(MODE_CENTER)==0)
{
positionMode = RC_CENTER;
}
else
{
positionMode = RC_CURSOR;
}
}
void controller_window::ShowRC()
{
this->timer->stop();
MainWindow *rc = new MainWindow();
rc->ChgGraphic(phase);
rc->setPositionMode(positionMode);
rc->show();
int duration = TIMER_DURATION;
#ifdef QTRC_DEBUG
qDebug("Next duration: %d\n",duration);
#endif
this->timer->setInterval(duration);
this->timer->start();
}
void controller_window::ToggleStops()
{
ui->stopHours->setEnabled(ui->stopCheck->isChecked());
}
void controller_window::Shutdown()
{
QApplication::exit(0);
}
void controller_window::AddPhase()
{
if(!phaseD)
{
phase = std::min(phase+1,MAX_PHASES);
if(phase == MAX_PHASES)
{
phaseD = true;
}
return;
}
else
{
phase = std::max(phase-1,0);
if(phase == 0)
{
phaseD = false;
}
return;
}
phasetimer->start();
}
void controller_window::moveToCenter()
{
int x,y;
QRect r = QApplication::desktop()->screenGeometry();
x = r.width()/2-this->geometry().width()/2;
y = r.height()/2-this->geometry().height()/2;
this->move(x,y);
}
bool controller_window::isActive()
{
return timer->isActive();
}