From a50d46dd52f125ab17c0719f71e93b7a03866351 Mon Sep 17 00:00:00 2001 From: Stelios Tsampas Date: Sun, 1 Oct 2017 11:06:39 +0300 Subject: [PATCH] In preparation for 0.1.0 release --- redshiftqt/redshiftqt.cpp | 136 ++++++++++++++++------------------ redshiftqt/redshiftqt.h | 16 ++-- redshiftqt/redshiftqtlog.cpp | 20 ++++- redshiftqt/redshiftqtlog.h | 2 +- redshiftqt/redshiftqtlog.ui | 8 +- redshiftqt/redshiftqtprefs.ui | 14 ++-- redshiftqt/redshiftqttray.cpp | 6 +- 7 files changed, 103 insertions(+), 99 deletions(-) diff --git a/redshiftqt/redshiftqt.cpp b/redshiftqt/redshiftqt.cpp index 0e2d895..ce97b11 100644 --- a/redshiftqt/redshiftqt.cpp +++ b/redshiftqt/redshiftqt.cpp @@ -24,121 +24,110 @@ RedShiftQt::RedShiftQt(QWidget *parent) : log = new RedShiftQtLog(this); - info_timer = new QTimer(this); - info_timer->setTimerType(Qt::VeryCoarseTimer); + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(startRedshift())); + timer->setTimerType(Qt::VeryCoarseTimer); - susp_timer = new QTimer(this); - connect(susp_timer, SIGNAL(timeout()), this, SLOT(toggle())); - susp_timer->setTimerType(Qt::VeryCoarseTimer); + helper = new QProcess(this); + helper->setProgram(conf->value("redshift_path").toString()); 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); - + connect(redshift, SIGNAL(started()), this, SLOT(onRedshiftStarted())); + connect(redshift, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onRedshiftFinished())); redshift->setProgram(conf->value("redshift_path").toString()); redshift->setArguments(getArguments()); - + onRedshiftFinished(); 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(); + showPrefs(); } RedShiftQt::~RedShiftQt() +{ + stopRedshift(); +} + +QString RedShiftQt::getRedshiftInfo() +{ + helper->setArguments(QStringList("-pv")); + helper->start(QProcess::ReadOnly); + helper->waitForFinished(); + return(helper->readAllStandardOutput()); +} + +void RedShiftQt::startRedshift() +{ + timer->stop(); + redshift->start(QProcess::ReadOnly); + redshift->waitForStarted(); +} + +void RedShiftQt::stopRedshift() { redshift->kill(); redshift->waitForFinished(); - helper->kill(); + helper->setArguments(QStringList("-x")); + helper->start(QProcess::ReadOnly); helper->waitForFinished(); } -void RedShiftQt::onRedshiftStateChanged(QProcess::ProcessState state) +void RedShiftQt::onRedshiftStarted() { - 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()); + tray->setIcon(QIcon(":/tray/on")); + tray->setToolTip(windowTitle() + ": Running"); + tray->status->setChecked(true); + tray->suspend->setEnabled(true); + log->setStatus(QString("Enabled")); + log->setInfo(getRedshiftInfo()); + log->appendToLog("Redshift was started with: " + redshift->arguments().join(" ").toUtf8()); } -void RedShiftQt::onReadyRead() +void RedShiftQt::onRedshiftFinished() { - + tray->setIcon(QIcon(":/tray/off")); + tray->setToolTip(windowTitle() + ": Suspended"); + tray->status->setChecked(false); + tray->suspend->setEnabled(false); + log->setStatus(QString("Disabled")); + log->setInfo(getRedshiftInfo()); + log->appendToLog("Redshift was suspended"); } void RedShiftQt::onConfChanged() { int state = redshift->state(); - if(state) { - redshift->kill(); - redshift->waitForFinished(); - } + if(state) stopRedshift(); redshift->setProgram(conf->value("redshift_path").toString()); redshift->setArguments(getArguments()); helper->setProgram(conf->value("redshift_path").toString()); - if(state) { - redshift->start(QProcess::ReadOnly); - } + if(state) startRedshift(); } -void RedShiftQt::toggle(void) +void RedShiftQt::toggleRedshift() { - 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(); - } + if(redshift->state()) stopRedshift(); + else startRedshift(); } -void RedShiftQt::suspend(QAction* action) +void RedShiftQt::toggleRedshift(QSystemTrayIcon::ActivationReason reason) { - 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)); + if(reason == QSystemTrayIcon::Trigger) toggleRedshift(); } -void RedShiftQt::activated(QSystemTrayIcon::ActivationReason reason) +void RedShiftQt::suspendRedshift(QAction* action) { - if(reason == QSystemTrayIcon::Trigger) - toggle(); + stopRedshift(); + + timer->setInterval(action->data().toInt() * 60 * 1000); + timer->start(); + + tray->setToolTip(tray->toolTip() + QString(" for %1 minutes").arg(action->data().toInt())); + log->appendToLog(QString("Suspending reshift for %1 minutes").arg(timer->interval()/60/1000)); } void RedShiftQt::showPrefs() @@ -149,6 +138,7 @@ void RedShiftQt::showPrefs() void RedShiftQt::showLog() { + log->setInfo(getRedshiftInfo()); log->setVisible(true); log->raise(); } diff --git a/redshiftqt/redshiftqt.h b/redshiftqt/redshiftqt.h index f553fad..0b1c460 100644 --- a/redshiftqt/redshiftqt.h +++ b/redshiftqt/redshiftqt.h @@ -25,14 +25,17 @@ class RedShiftQt : public QMainWindow ~RedShiftQt(); private slots: - void onRedshiftStateChanged(QProcess::ProcessState); - void onReadyRead(); + void startRedshift(); + void stopRedshift(); + void onRedshiftStarted(); + void onRedshiftFinished(); + QString getRedshiftInfo(); void onConfChanged(); - void toggle(); - void suspend(QAction*); - void activated(QSystemTrayIcon::ActivationReason); + void toggleRedshift(); + void toggleRedshift(QSystemTrayIcon::ActivationReason); + void suspendRedshift(QAction*); void showPrefs(); void showLog(); @@ -44,8 +47,7 @@ private slots: QProcess *redshift; QProcess *helper; - QTimer *susp_timer; - QTimer *info_timer; + QTimer *timer; QSettings *conf; diff --git a/redshiftqt/redshiftqtlog.cpp b/redshiftqt/redshiftqtlog.cpp index cf29fc5..4d76d60 100644 --- a/redshiftqt/redshiftqtlog.cpp +++ b/redshiftqt/redshiftqtlog.cpp @@ -21,12 +21,26 @@ RedShiftQtLog::~RedShiftQtLog() void RedShiftQtLog::setStatus(QString status) { - ui->labelStatusInfo->setText(status); + ui->labelStatusValue->setText(status); } -void RedShiftQtLog::setInfo(QStringList info) +void RedShiftQtLog::setInfo(QString info) { - + QStringList info_list = info.split('\n'); + QStringList::iterator i; + for(i = info_list.begin(); i != info_list.end(); ++i) { + if(i->startsWith("Location")) { + ui->labelLocValue->setText(i->split(": ").at(1)); + ++i; + } + if(i->startsWith("Period")) { + ui->labelPeriodValue->setText(i->split(": ").at(1)); + ++i; + } + if(i->startsWith("Color temp")) { + ui->labelTempValue->setText(i->split(": ").at(1)); + } + } } void RedShiftQtLog::appendToLog(QString input) diff --git a/redshiftqt/redshiftqtlog.h b/redshiftqt/redshiftqtlog.h index 15f4da5..3fcfbd2 100644 --- a/redshiftqt/redshiftqtlog.h +++ b/redshiftqt/redshiftqtlog.h @@ -22,7 +22,7 @@ class RedShiftQtLog : public QDialog ~RedShiftQtLog(); void setStatus(QString); - void setInfo(QStringList); + void setInfo(QString); void appendToLog(QString); protected: diff --git a/redshiftqt/redshiftqtlog.ui b/redshiftqt/redshiftqtlog.ui index 6155bcf..a7bdd51 100644 --- a/redshiftqt/redshiftqtlog.ui +++ b/redshiftqt/redshiftqtlog.ui @@ -40,28 +40,28 @@ - + Disabled - + 6500K - + N, W - + Day diff --git a/redshiftqt/redshiftqtprefs.ui b/redshiftqt/redshiftqtprefs.ui index ce676c0..c8e02d2 100644 --- a/redshiftqt/redshiftqtprefs.ui +++ b/redshiftqt/redshiftqtprefs.ui @@ -2,13 +2,11 @@ RedShiftQtPrefs - - - 0 - 0 - 382 - 447 - + + + 0 + 0 + @@ -131,7 +129,7 @@ 100 - 6500 + 5500 diff --git a/redshiftqt/redshiftqttray.cpp b/redshiftqt/redshiftqttray.cpp index f988010..887e4da 100644 --- a/redshiftqt/redshiftqttray.cpp +++ b/redshiftqt/redshiftqttray.cpp @@ -5,12 +5,12 @@ RedShiftQtTray::RedShiftQtTray(QMainWindow* parent) : QSystemTrayIcon(parent) menu = new QMenu(); status = new QAction(tr("&Enabled"), this); - connect(status, SIGNAL(triggered()), parent, SLOT(toggle())); + connect(status, SIGNAL(triggered()), parent, SLOT(toggleRedshift())); status->setCheckable(true); menu->addAction(status); suspend = new QMenu(tr("&Suspend for")); - connect(suspend, SIGNAL(triggered(QAction*)), parent, SLOT(suspend(QAction*))); + connect(suspend, SIGNAL(triggered(QAction*)), parent, SLOT(suspendRedshift(QAction*))); suspend30m = new QAction(tr("&30 minutes")); suspend30m->setData(30); @@ -47,7 +47,7 @@ RedShiftQtTray::RedShiftQtTray(QMainWindow* parent) : QSystemTrayIcon(parent) setToolTip(parent->windowTitle()); connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - parent, SLOT(activated(QSystemTrayIcon::ActivationReason))); + parent, SLOT(toggleRedshift(QSystemTrayIcon::ActivationReason))); } RedShiftQtTray::~RedShiftQtTray()