Skip to content

Commit dc3d8c0

Browse files
Almost working
1 parent 7217b89 commit dc3d8c0

16 files changed

+1506
-536
lines changed

redshiftqt/RedShiftQt.pro

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
#-------------------------------------------------
66

7-
QT += core gui
7+
QT += core gui network
88

99
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
1010

@@ -15,16 +15,20 @@ TEMPLATE = app
1515
SOURCES += \
1616
main.cpp \
1717
redshiftqttray.cpp \
18+
redshiftqtprefs.cpp \
1819
redshiftqtlog.cpp \
19-
redshiftqtprefs.cpp
20+
redshiftqt.cpp \
21+
qdoublespinboxlist.cpp
2022

2123
HEADERS += \
22-
redshiftqt.h \
2324
redshiftqttray.h \
24-
redshiftqtlog.h
25+
redshiftqtprefs.h \
26+
redshiftqtlog.h \
27+
redshiftqt.h \
28+
qdoublespinboxlist.h
2529

2630
FORMS += \
27-
redshiftqt.ui \
31+
redshiftqtprefs.ui \
2832
redshiftqtlog.ui
2933

3034
RESOURCES += \

redshiftqt/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "redshiftqt.h"
2-
#include "redshiftqtlog.h"
32
#include <QApplication>
43

54
int main(int argc, char *argv[])
65
{
76
QApplication a(argc, argv);
7+
a.setQuitOnLastWindowClosed(false);
8+
a.setOrganizationName(a.applicationName());
89
RedShiftQt w;
9-
Redshift
1010

1111
return a.exec();
1212
}

redshiftqt/qdoublespinboxlist.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "qdoublespinboxlist.h"
2+
3+
QStringList QDoubleSpinBoxList::value(void) {
4+
QStringList gamma;
5+
QDoubleSpinBoxList::iterator i;
6+
for(i = this->begin(); i != this->end(); i++)
7+
gamma.append(QString("%1").arg((*i)->value()));
8+
return gamma;
9+
}
10+
11+
void QDoubleSpinBoxList::setValue(QStringList v)
12+
{
13+
for(int i = 0; i < this->count(); i++) {
14+
this->at(i)->setValue(v.value(i, v.at(0)).toDouble());
15+
}
16+
}
17+
18+
void QDoubleSpinBoxList::setEnabled(bool b)
19+
{
20+
QDoubleSpinBoxList::iterator i;
21+
for(i = this->begin(); i != this->end(); i++)
22+
(*i)->setEnabled(b);
23+
}

redshiftqt/qdoublespinboxlist.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef QDOUBLESPINBOXLIST_H
2+
#define QDOUBLESPINBOXLIST_H
3+
4+
#include <QDoubleSpinBox>
5+
6+
class QDoubleSpinBoxList : public QList<QDoubleSpinBox*>
7+
{
8+
public:
9+
QStringList value();
10+
void setValue(QStringList);
11+
void setEnabled(bool);
12+
};
13+
#endif // QDOUBLESPINBOXLIST_H

redshiftqt/redshiftqt.cpp

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#include "redshiftqt.h"
2+
3+
RedShiftQt::RedShiftQt(QWidget *parent) :
4+
QMainWindow(parent)
5+
{
6+
7+
setWindowIcon(QIcon(":/app/icon"));
8+
setWindowTitle(qApp->applicationName());
9+
setGeometry(QStyle::alignedRect(
10+
Qt::LeftToRight,
11+
Qt::AlignCenter,
12+
QSize(0,0),
13+
qApp->desktop()->availableGeometry()));
14+
15+
conf = new QSettings(
16+
QSettings::IniFormat,
17+
QSettings::UserScope,
18+
qApp->organizationName(),
19+
qApp->applicationName(),
20+
this);
21+
22+
tray = new RedShiftQtTray(this);
23+
tray->show();
24+
25+
log = new RedShiftQtLog(this);
26+
27+
info_timer = new QTimer(this);
28+
info_timer->setTimerType(Qt::VeryCoarseTimer);
29+
30+
susp_timer = new QTimer(this);
31+
connect(susp_timer, SIGNAL(timeout()), this, SLOT(toggle()));
32+
susp_timer->setTimerType(Qt::VeryCoarseTimer);
33+
34+
redshift = new QProcess(this);
35+
connect(redshift, SIGNAL(stateChanged(QProcess::ProcessState)),
36+
this, SLOT(onRedshiftStateChanged(QProcess::ProcessState)));
37+
//connect(redshift, SIGNAL(readyRead()), this, SLOT(onProcReadyRead()));
38+
//connect(redshift, SINGAL(readyReadStandardError(), this, SLOT(onProcReadyRead)));
39+
onRedshiftStateChanged(QProcess::NotRunning);
40+
41+
redshift->setProgram(conf->value("redshift_path").toString());
42+
redshift->setArguments(getArguments());
43+
44+
if(conf->value("start_enabled").toBool())
45+
redshift->start(QProcess::ReadOnly);
46+
47+
helper = new QProcess(this);
48+
helper->setProgram(conf->value("redshift_path").toString());
49+
50+
prefs = new RedShiftQtPrefs(this);
51+
connect(prefs, SIGNAL(confChanged()), this, SLOT(onConfChanged()));
52+
53+
if(conf->value("show_prefs").toBool())
54+
prefs->show();
55+
}
56+
57+
RedShiftQt::~RedShiftQt()
58+
{
59+
redshift->kill();
60+
redshift->waitForFinished();
61+
helper->kill();
62+
helper->waitForFinished();
63+
}
64+
65+
void RedShiftQt::onRedshiftStateChanged(QProcess::ProcessState state)
66+
{
67+
switch(state) {
68+
case QProcess::Running :
69+
tray->setIcon(QIcon(":/tray/on"));
70+
tray->setToolTip(windowTitle() +": Running");
71+
tray->status->setChecked(true);
72+
tray->suspend->setEnabled(true);
73+
log->setStatus(QString("Enabled"));
74+
log->appendToLog(redshift->program().toUtf8() + " " + redshift->arguments().join(" ").toUtf8());
75+
break;
76+
77+
case QProcess::NotRunning :
78+
tray->setIcon(QIcon(":/tray/off"));
79+
tray->setToolTip(windowTitle() +": Suspended");
80+
tray->status->setChecked(false);
81+
tray->suspend->setEnabled(false);
82+
log->setInfo(QStringList("Disabled"));
83+
log->appendToLog("Redshift stopped");
84+
break;
85+
86+
default:
87+
break;
88+
}
89+
qDebug("Process state: %d", redshift->state());
90+
}
91+
92+
void RedShiftQt::onReadyRead()
93+
{
94+
95+
}
96+
97+
void RedShiftQt::onConfChanged()
98+
{
99+
int state = redshift->state();
100+
if(state) {
101+
redshift->kill();
102+
redshift->waitForFinished();
103+
}
104+
redshift->setProgram(conf->value("redshift_path").toString());
105+
redshift->setArguments(getArguments());
106+
helper->setProgram(conf->value("redshift_path").toString());
107+
if(state) {
108+
redshift->start(QProcess::ReadOnly);
109+
}
110+
}
111+
112+
void RedShiftQt::toggle(void)
113+
{
114+
if(redshift->state() == QProcess::Running) {
115+
redshift->kill();
116+
redshift->waitForFinished();
117+
helper->setArguments(QStringList("-x"));
118+
helper->start(QProcess::ReadOnly);
119+
helper->waitForFinished();
120+
} else {
121+
susp_timer->stop();
122+
redshift->start(QProcess::ReadOnly);
123+
redshift->waitForStarted();
124+
}
125+
}
126+
127+
void RedShiftQt::suspend(QAction* action)
128+
{
129+
toggle();
130+
131+
susp_timer->setInterval(action->data().toInt() * 60 * 1000);
132+
susp_timer->start();
133+
134+
tray->setToolTip(tray->toolTip() + QString(" for %1 minutes").arg(action->data().toInt()));
135+
log->appendToLog(QString("Suspending reshift for %1 minutes").arg(susp_timer->interval()/60/1000));
136+
}
137+
138+
void RedShiftQt::activated(QSystemTrayIcon::ActivationReason reason)
139+
{
140+
if(reason == QSystemTrayIcon::Trigger)
141+
toggle();
142+
}
143+
144+
void RedShiftQt::showPrefs()
145+
{
146+
prefs->setVisible(true);
147+
prefs->raise();
148+
}
149+
150+
void RedShiftQt::showLog()
151+
{
152+
log->setVisible(true);
153+
log->raise();
154+
}
155+
156+
QStringList RedShiftQt::getArguments()
157+
{
158+
QStringList args;
159+
if(conf->value("use_own_conf").toBool()) {
160+
161+
conf->beginGroup("color-temp");
162+
QString method(conf->value("method").toString());
163+
if(method != "default") {
164+
args.append("-m");
165+
args.append(method);
166+
}
167+
args.append("-t");
168+
args.append(QString("%1:%2").arg(
169+
conf->value("day").toString(),
170+
conf->value("night").toString()));
171+
if(!conf->value("transition").toBool()) args.append("-r");
172+
conf->endGroup();
173+
174+
conf->beginGroup("location");
175+
QString provider(conf->value("provider").toString());
176+
if(provider != "default") {
177+
args.append("-l");
178+
if(provider == "manual") {
179+
args.append(QString("%1:%2").arg(
180+
conf->value("latitude").toString(),
181+
conf->value("longitude").toString()));
182+
} else {
183+
args.append(provider);
184+
}
185+
}
186+
conf->endGroup();
187+
188+
conf->beginGroup("brightness");
189+
if(conf->value("adj-type").toInt() > 0) {
190+
args.append("-b");
191+
if(conf->value("adj-type").toInt() > 1) {
192+
args.append(QString("%1:%2").arg(
193+
conf->value("day").toString(),
194+
conf->value("night").toString()));
195+
} else {
196+
args.append(conf->value("day").toString());
197+
}
198+
}
199+
conf->endGroup();
200+
201+
conf->beginGroup("gamma");
202+
if(conf->value("adj-type").toInt() > 0) {
203+
args.append("-g");
204+
if(conf->value("adj-type").toInt() > 1) {
205+
args.append(conf->value("day").toString());
206+
} else {
207+
args.append(conf->value("day").toString());
208+
}
209+
}
210+
conf->endGroup();
211+
212+
} else {
213+
args.append("-c");
214+
args.append(conf->value("config_path").toString());
215+
}
216+
return args;
217+
}

redshiftqt/redshiftqt.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#ifndef REDSHIFTQT_H
22
#define REDSHIFTQT_H
33

4+
#include <QApplication>
45
#include <QMainWindow>
5-
#include <QIcon>
6-
#include <QWidget>
7-
#include <QSystemTrayIcon>
6+
#include <QStyle>
7+
#include <QDesktopWidget>
88
#include <QProcess>
99
#include <QSettings>
10-
#include <QFileDialog>
10+
#include <QStandardPaths>
1111

1212
#include "redshiftqttray.h"
13+
#include "redshiftqtprefs.h"
14+
#include "redshiftqtlog.h"
1315

1416
namespace Ui {
1517
class RedShiftQt;
@@ -18,21 +20,36 @@ class RedShiftQt;
1820
class RedShiftQt : public QMainWindow
1921
{
2022
Q_OBJECT
21-
2223
public:
2324
explicit RedShiftQt(QWidget *parent = 0);
2425
~RedShiftQt();
2526

26-
protected slots:
27-
void onToolButtonPathClicked(void);
28-
void onCheckBoxLocalSettingsChanged(void);
27+
private slots:
28+
void onRedshiftStateChanged(QProcess::ProcessState);
29+
void onReadyRead();
30+
31+
void onConfChanged();
32+
33+
void toggle();
34+
void suspend(QAction*);
35+
void activated(QSystemTrayIcon::ActivationReason);
36+
void showPrefs();
37+
void showLog();
2938

3039
private:
31-
QSettings* prefs;
32-
QProcess* redshift;
40+
RedShiftQtTray *tray;
41+
RedShiftQtPrefs *prefs;
42+
RedShiftQtLog *log;
43+
44+
QProcess *redshift;
45+
QProcess *helper;
46+
47+
QTimer *susp_timer;
48+
QTimer *info_timer;
49+
50+
QSettings *conf;
3351

34-
RedShiftQtTray* tray;
35-
Ui::RedShiftQt* ui;
52+
QStringList getArguments(void);
3653
};
3754

3855
#endif // REDSHIFTQT_H

redshiftqt/redshiftqt.qrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
<qresource prefix="/app">
33
<file alias="icon">images/redshift.ico</file>
44
</qresource>
5-
5+
<qresource prefix="/tray">
6+
<file alias="off">images/redshift-status-off.svg</file>
7+
<file alias="on">images/redshift-status-on.svg</file>
8+
</qresource>
69
</RCC>

0 commit comments

Comments
 (0)