Skip to content

Commit

Permalink
Almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
loathingKernel committed Sep 30, 2017
1 parent 7217b89 commit dc3d8c0
Show file tree
Hide file tree
Showing 16 changed files with 1,506 additions and 536 deletions.
14 changes: 9 additions & 5 deletions redshiftqt/RedShiftQt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
#-------------------------------------------------

QT += core gui
QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

Expand All @@ -15,16 +15,20 @@ TEMPLATE = app
SOURCES += \
main.cpp \
redshiftqttray.cpp \
redshiftqtprefs.cpp \
redshiftqtlog.cpp \
redshiftqtprefs.cpp
redshiftqt.cpp \
qdoublespinboxlist.cpp

HEADERS += \
redshiftqt.h \
redshiftqttray.h \
redshiftqtlog.h
redshiftqtprefs.h \
redshiftqtlog.h \
redshiftqt.h \
qdoublespinboxlist.h

FORMS += \
redshiftqt.ui \
redshiftqtprefs.ui \
redshiftqtlog.ui

RESOURCES += \
Expand Down
4 changes: 2 additions & 2 deletions redshiftqt/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "redshiftqt.h"
#include "redshiftqtlog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
a.setOrganizationName(a.applicationName());
RedShiftQt w;
Redshift

return a.exec();
}
23 changes: 23 additions & 0 deletions redshiftqt/qdoublespinboxlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "qdoublespinboxlist.h"

QStringList QDoubleSpinBoxList::value(void) {
QStringList gamma;
QDoubleSpinBoxList::iterator i;
for(i = this->begin(); i != this->end(); i++)
gamma.append(QString("%1").arg((*i)->value()));
return gamma;
}

void QDoubleSpinBoxList::setValue(QStringList v)
{
for(int i = 0; i < this->count(); i++) {
this->at(i)->setValue(v.value(i, v.at(0)).toDouble());
}
}

void QDoubleSpinBoxList::setEnabled(bool b)
{
QDoubleSpinBoxList::iterator i;
for(i = this->begin(); i != this->end(); i++)
(*i)->setEnabled(b);
}
13 changes: 13 additions & 0 deletions redshiftqt/qdoublespinboxlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef QDOUBLESPINBOXLIST_H
#define QDOUBLESPINBOXLIST_H

#include <QDoubleSpinBox>

class QDoubleSpinBoxList : public QList<QDoubleSpinBox*>
{
public:
QStringList value();
void setValue(QStringList);
void setEnabled(bool);
};
#endif // QDOUBLESPINBOXLIST_H
217 changes: 217 additions & 0 deletions redshiftqt/redshiftqt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#include "redshiftqt.h"

RedShiftQt::RedShiftQt(QWidget *parent) :
QMainWindow(parent)
{

setWindowIcon(QIcon(":/app/icon"));
setWindowTitle(qApp->applicationName());
setGeometry(QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
QSize(0,0),
qApp->desktop()->availableGeometry()));

conf = new QSettings(
QSettings::IniFormat,
QSettings::UserScope,
qApp->organizationName(),
qApp->applicationName(),
this);

tray = new RedShiftQtTray(this);
tray->show();

log = new RedShiftQtLog(this);

info_timer = new QTimer(this);
info_timer->setTimerType(Qt::VeryCoarseTimer);

susp_timer = new QTimer(this);
connect(susp_timer, SIGNAL(timeout()), this, SLOT(toggle()));
susp_timer->setTimerType(Qt::VeryCoarseTimer);

redshift = new QProcess(this);
connect(redshift, SIGNAL(stateChanged(QProcess::ProcessState)),
this, SLOT(onRedshiftStateChanged(QProcess::ProcessState)));
//connect(redshift, SIGNAL(readyRead()), this, SLOT(onProcReadyRead()));
//connect(redshift, SINGAL(readyReadStandardError(), this, SLOT(onProcReadyRead)));
onRedshiftStateChanged(QProcess::NotRunning);

redshift->setProgram(conf->value("redshift_path").toString());
redshift->setArguments(getArguments());

if(conf->value("start_enabled").toBool())
redshift->start(QProcess::ReadOnly);

helper = new QProcess(this);
helper->setProgram(conf->value("redshift_path").toString());

prefs = new RedShiftQtPrefs(this);
connect(prefs, SIGNAL(confChanged()), this, SLOT(onConfChanged()));

if(conf->value("show_prefs").toBool())
prefs->show();
}

RedShiftQt::~RedShiftQt()
{
redshift->kill();
redshift->waitForFinished();
helper->kill();
helper->waitForFinished();
}

void RedShiftQt::onRedshiftStateChanged(QProcess::ProcessState state)
{
switch(state) {
case QProcess::Running :
tray->setIcon(QIcon(":/tray/on"));
tray->setToolTip(windowTitle() +": Running");
tray->status->setChecked(true);
tray->suspend->setEnabled(true);
log->setStatus(QString("Enabled"));
log->appendToLog(redshift->program().toUtf8() + " " + redshift->arguments().join(" ").toUtf8());
break;

case QProcess::NotRunning :
tray->setIcon(QIcon(":/tray/off"));
tray->setToolTip(windowTitle() +": Suspended");
tray->status->setChecked(false);
tray->suspend->setEnabled(false);
log->setInfo(QStringList("Disabled"));
log->appendToLog("Redshift stopped");
break;

default:
break;
}
qDebug("Process state: %d", redshift->state());
}

void RedShiftQt::onReadyRead()
{

}

void RedShiftQt::onConfChanged()
{
int state = redshift->state();
if(state) {
redshift->kill();
redshift->waitForFinished();
}
redshift->setProgram(conf->value("redshift_path").toString());
redshift->setArguments(getArguments());
helper->setProgram(conf->value("redshift_path").toString());
if(state) {
redshift->start(QProcess::ReadOnly);
}
}

void RedShiftQt::toggle(void)
{
if(redshift->state() == QProcess::Running) {
redshift->kill();
redshift->waitForFinished();
helper->setArguments(QStringList("-x"));
helper->start(QProcess::ReadOnly);
helper->waitForFinished();
} else {
susp_timer->stop();
redshift->start(QProcess::ReadOnly);
redshift->waitForStarted();
}
}

void RedShiftQt::suspend(QAction* action)
{
toggle();

susp_timer->setInterval(action->data().toInt() * 60 * 1000);
susp_timer->start();

tray->setToolTip(tray->toolTip() + QString(" for %1 minutes").arg(action->data().toInt()));
log->appendToLog(QString("Suspending reshift for %1 minutes").arg(susp_timer->interval()/60/1000));
}

void RedShiftQt::activated(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::Trigger)
toggle();
}

void RedShiftQt::showPrefs()
{
prefs->setVisible(true);
prefs->raise();
}

void RedShiftQt::showLog()
{
log->setVisible(true);
log->raise();
}

QStringList RedShiftQt::getArguments()
{
QStringList args;
if(conf->value("use_own_conf").toBool()) {

conf->beginGroup("color-temp");
QString method(conf->value("method").toString());
if(method != "default") {
args.append("-m");
args.append(method);
}
args.append("-t");
args.append(QString("%1:%2").arg(
conf->value("day").toString(),
conf->value("night").toString()));
if(!conf->value("transition").toBool()) args.append("-r");
conf->endGroup();

conf->beginGroup("location");
QString provider(conf->value("provider").toString());
if(provider != "default") {
args.append("-l");
if(provider == "manual") {
args.append(QString("%1:%2").arg(
conf->value("latitude").toString(),
conf->value("longitude").toString()));
} else {
args.append(provider);
}
}
conf->endGroup();

conf->beginGroup("brightness");
if(conf->value("adj-type").toInt() > 0) {
args.append("-b");
if(conf->value("adj-type").toInt() > 1) {
args.append(QString("%1:%2").arg(
conf->value("day").toString(),
conf->value("night").toString()));
} else {
args.append(conf->value("day").toString());
}
}
conf->endGroup();

conf->beginGroup("gamma");
if(conf->value("adj-type").toInt() > 0) {
args.append("-g");
if(conf->value("adj-type").toInt() > 1) {
args.append(conf->value("day").toString());
} else {
args.append(conf->value("day").toString());
}
}
conf->endGroup();

} else {
args.append("-c");
args.append(conf->value("config_path").toString());
}
return args;
}
41 changes: 29 additions & 12 deletions redshiftqt/redshiftqt.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#ifndef REDSHIFTQT_H
#define REDSHIFTQT_H

#include <QApplication>
#include <QMainWindow>
#include <QIcon>
#include <QWidget>
#include <QSystemTrayIcon>
#include <QStyle>
#include <QDesktopWidget>
#include <QProcess>
#include <QSettings>
#include <QFileDialog>
#include <QStandardPaths>

#include "redshiftqttray.h"
#include "redshiftqtprefs.h"
#include "redshiftqtlog.h"

namespace Ui {
class RedShiftQt;
Expand All @@ -18,21 +20,36 @@ class RedShiftQt;
class RedShiftQt : public QMainWindow
{
Q_OBJECT

public:
explicit RedShiftQt(QWidget *parent = 0);
~RedShiftQt();

protected slots:
void onToolButtonPathClicked(void);
void onCheckBoxLocalSettingsChanged(void);
private slots:
void onRedshiftStateChanged(QProcess::ProcessState);
void onReadyRead();

void onConfChanged();

void toggle();
void suspend(QAction*);
void activated(QSystemTrayIcon::ActivationReason);
void showPrefs();
void showLog();

private:
QSettings* prefs;
QProcess* redshift;
RedShiftQtTray *tray;
RedShiftQtPrefs *prefs;
RedShiftQtLog *log;

QProcess *redshift;
QProcess *helper;

QTimer *susp_timer;
QTimer *info_timer;

QSettings *conf;

RedShiftQtTray* tray;
Ui::RedShiftQt* ui;
QStringList getArguments(void);
};

#endif // REDSHIFTQT_H
5 changes: 4 additions & 1 deletion redshiftqt/redshiftqt.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
<qresource prefix="/app">
<file alias="icon">images/redshift.ico</file>
</qresource>

<qresource prefix="/tray">
<file alias="off">images/redshift-status-off.svg</file>
<file alias="on">images/redshift-status-on.svg</file>
</qresource>
</RCC>
Loading

0 comments on commit dc3d8c0

Please sign in to comment.