Merge branch 'Plasma/5.17'
This commit is contained in:
commit
01982a7892
4 changed files with 53 additions and 50 deletions
|
@ -371,13 +371,7 @@ void Manager::resetSlowUpdateStartTimer()
|
||||||
connect(m_slowUpdateStartTimer, &QTimer::timeout, this, &Manager::resetSlowUpdateStartTimer);
|
connect(m_slowUpdateStartTimer, &QTimer::timeout, this, &Manager::resetSlowUpdateStartTimer);
|
||||||
|
|
||||||
updateSunTimings(false);
|
updateSunTimings(false);
|
||||||
int diff;
|
const int diff = QDateTime::currentDateTime().msecsTo(m_next.first);
|
||||||
if (m_mode == NightColorMode::Timings) {
|
|
||||||
// Timings mode is in local time
|
|
||||||
diff = QDateTime::currentDateTime().msecsTo(m_next.first);
|
|
||||||
} else {
|
|
||||||
diff = QDateTime::currentDateTimeUtc().msecsTo(m_next.first);
|
|
||||||
}
|
|
||||||
if (diff <= 0) {
|
if (diff <= 0) {
|
||||||
qCCritical(KWIN_COLORCORRECTION) << "Error in time calculation. Deactivating Night Color.";
|
qCCritical(KWIN_COLORCORRECTION) << "Error in time calculation. Deactivating Night Color.";
|
||||||
return;
|
return;
|
||||||
|
@ -393,7 +387,7 @@ void Manager::resetSlowUpdateTimer()
|
||||||
delete m_slowUpdateTimer;
|
delete m_slowUpdateTimer;
|
||||||
m_slowUpdateTimer = nullptr;
|
m_slowUpdateTimer = nullptr;
|
||||||
|
|
||||||
QDateTime now = QDateTime::currentDateTimeUtc();
|
QDateTime now = QDateTime::currentDateTime();
|
||||||
bool isDay = daylight();
|
bool isDay = daylight();
|
||||||
int targetTemp = isDay ? m_dayTargetTemp : m_nightTargetTemp;
|
int targetTemp = isDay ? m_dayTargetTemp : m_nightTargetTemp;
|
||||||
|
|
||||||
|
@ -443,21 +437,18 @@ void Manager::slowUpdate(int targetTemp)
|
||||||
|
|
||||||
void Manager::updateSunTimings(bool force)
|
void Manager::updateSunTimings(bool force)
|
||||||
{
|
{
|
||||||
QDateTime todayNow = QDateTime::currentDateTimeUtc();
|
QDateTime todayNow = QDateTime::currentDateTime();
|
||||||
|
|
||||||
if (m_mode == NightColorMode::Timings) {
|
if (m_mode == NightColorMode::Timings) {
|
||||||
|
QDateTime morB = QDateTime(todayNow.date(), m_morning);
|
||||||
QDateTime todayNowLocal = QDateTime::currentDateTime();
|
|
||||||
|
|
||||||
QDateTime morB = QDateTime(todayNowLocal.date(), m_morning);
|
|
||||||
QDateTime morE = morB.addSecs(m_trTime * 60);
|
QDateTime morE = morB.addSecs(m_trTime * 60);
|
||||||
QDateTime eveB = QDateTime(todayNowLocal.date(), m_evening);
|
QDateTime eveB = QDateTime(todayNow.date(), m_evening);
|
||||||
QDateTime eveE = eveB.addSecs(m_trTime * 60);
|
QDateTime eveE = eveB.addSecs(m_trTime * 60);
|
||||||
|
|
||||||
if (morB <= todayNowLocal && todayNowLocal < eveB) {
|
if (morB <= todayNow && todayNow < eveB) {
|
||||||
m_next = DateTimes(eveB, eveE);
|
m_next = DateTimes(eveB, eveE);
|
||||||
m_prev = DateTimes(morB, morE);
|
m_prev = DateTimes(morB, morE);
|
||||||
} else if (todayNowLocal < morB) {
|
} else if (todayNow < morB) {
|
||||||
m_next = DateTimes(morB, morE);
|
m_next = DateTimes(morB, morE);
|
||||||
m_prev = DateTimes(eveB.addDays(-1), eveE.addDays(-1));
|
m_prev = DateTimes(eveB.addDays(-1), eveE.addDays(-1));
|
||||||
} else {
|
} else {
|
||||||
|
@ -481,62 +472,63 @@ void Manager::updateSunTimings(bool force)
|
||||||
if (daylight()) {
|
if (daylight()) {
|
||||||
// next is morning
|
// next is morning
|
||||||
m_prev = m_next;
|
m_prev = m_next;
|
||||||
m_next = getSunTimings(todayNow.date().addDays(1), lat, lng, true);
|
m_next = getSunTimings(todayNow.addDays(1), lat, lng, true);
|
||||||
} else {
|
} else {
|
||||||
// next is evening
|
// next is evening
|
||||||
m_prev = m_next;
|
m_prev = m_next;
|
||||||
m_next = getSunTimings(todayNow.date(), lat, lng, false);
|
m_next = getSunTimings(todayNow, lat, lng, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (force || !checkAutomaticSunTimings()) {
|
if (force || !checkAutomaticSunTimings()) {
|
||||||
// in case this fails, reset them
|
// in case this fails, reset them
|
||||||
DateTimes morning = getSunTimings(todayNow.date(), lat, lng, true);
|
DateTimes morning = getSunTimings(todayNow, lat, lng, true);
|
||||||
if (todayNow < morning.first) {
|
if (todayNow < morning.first) {
|
||||||
m_prev = getSunTimings(todayNow.date().addDays(-1), lat, lng, false);
|
m_prev = getSunTimings(todayNow.addDays(-1), lat, lng, false);
|
||||||
m_next = morning;
|
m_next = morning;
|
||||||
} else {
|
} else {
|
||||||
DateTimes evening = getSunTimings(todayNow.date(), lat, lng, false);
|
DateTimes evening = getSunTimings(todayNow, lat, lng, false);
|
||||||
if (todayNow < evening.first) {
|
if (todayNow < evening.first) {
|
||||||
m_prev = morning;
|
m_prev = morning;
|
||||||
m_next = evening;
|
m_next = evening;
|
||||||
} else {
|
} else {
|
||||||
m_prev = evening;
|
m_prev = evening;
|
||||||
m_next = getSunTimings(todayNow.date().addDays(1), lat, lng, true);
|
m_next = getSunTimings(todayNow.addDays(1), lat, lng, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DateTimes Manager::getSunTimings(QDate date, double latitude, double longitude, bool morning) const
|
DateTimes Manager::getSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning) const
|
||||||
{
|
{
|
||||||
Times times = calculateSunTimings(date, latitude, longitude, morning);
|
DateTimes dateTimes = calculateSunTimings(dateTime, latitude, longitude, morning);
|
||||||
// At locations near the poles it is possible, that we can't
|
// At locations near the poles it is possible, that we can't
|
||||||
// calculate some or all sun timings (midnight sun).
|
// calculate some or all sun timings (midnight sun).
|
||||||
// In this case try to fallback to sensible default values.
|
// In this case try to fallback to sensible default values.
|
||||||
bool beginDefined = !times.first.isNull();
|
bool beginDefined = !dateTimes.first.isNull();
|
||||||
bool endDefined = !times.second.isNull();
|
bool endDefined = !dateTimes.second.isNull();
|
||||||
if (!beginDefined || !endDefined) {
|
if (!beginDefined || !endDefined) {
|
||||||
if (beginDefined) {
|
if (beginDefined) {
|
||||||
times.second = times.first.addMSecs( FALLBACK_SLOW_UPDATE_TIME );
|
dateTimes.second = dateTimes.first.addMSecs( FALLBACK_SLOW_UPDATE_TIME );
|
||||||
} else if (endDefined) {
|
} else if (endDefined) {
|
||||||
times.first = times.second.addMSecs( - FALLBACK_SLOW_UPDATE_TIME);
|
dateTimes.first = dateTimes.second.addMSecs( - FALLBACK_SLOW_UPDATE_TIME );
|
||||||
} else {
|
} else {
|
||||||
// Just use default values for morning and evening, but the user
|
// Just use default values for morning and evening, but the user
|
||||||
// will probably deactivate Night Color anyway if he is living
|
// will probably deactivate Night Color anyway if he is living
|
||||||
// in a region without clear sun rise and set.
|
// in a region without clear sun rise and set.
|
||||||
times.first = morning ? QTime(6,0,0) : QTime(18,0,0);
|
const QTime referenceTime = morning ? QTime(6, 0) : QTime(18, 0);
|
||||||
times.second = times.first.addMSecs( FALLBACK_SLOW_UPDATE_TIME );
|
dateTimes.first = QDateTime(dateTime.date(), referenceTime);
|
||||||
|
dateTimes.second = dateTimes.first.addMSecs( FALLBACK_SLOW_UPDATE_TIME );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return DateTimes(QDateTime(date, times.first, Qt::UTC), QDateTime(date, times.second, Qt::UTC));
|
return dateTimes;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Manager::checkAutomaticSunTimings() const
|
bool Manager::checkAutomaticSunTimings() const
|
||||||
{
|
{
|
||||||
if (m_prev.first.isValid() && m_prev.second.isValid() &&
|
if (m_prev.first.isValid() && m_prev.second.isValid() &&
|
||||||
m_next.first.isValid() && m_next.second.isValid()) {
|
m_next.first.isValid() && m_next.second.isValid()) {
|
||||||
QDateTime todayNow = QDateTime::currentDateTimeUtc();
|
QDateTime todayNow = QDateTime::currentDateTime();
|
||||||
return m_prev.first <= todayNow && todayNow < m_next.first &&
|
return m_prev.first <= todayNow && todayNow < m_next.first &&
|
||||||
m_prev.first.msecsTo(m_next.first) < MSC_DAY * 23./24;
|
m_prev.first.msecsTo(m_next.first) < MSC_DAY * 23./24;
|
||||||
}
|
}
|
||||||
|
@ -558,7 +550,7 @@ int Manager::currentTargetTemp() const
|
||||||
return m_nightTargetTemp;
|
return m_nightTargetTemp;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDateTime todayNow = QDateTime::currentDateTimeUtc();
|
QDateTime todayNow = QDateTime::currentDateTime();
|
||||||
|
|
||||||
auto f = [this, todayNow](int target1, int target2) {
|
auto f = [this, todayNow](int target1, int target2) {
|
||||||
if (todayNow <= m_prev.second) {
|
if (todayNow <= m_prev.second) {
|
||||||
|
|
|
@ -154,7 +154,7 @@ private:
|
||||||
void resetSlowUpdateTimer();
|
void resetSlowUpdateTimer();
|
||||||
|
|
||||||
void updateSunTimings(bool force);
|
void updateSunTimings(bool force);
|
||||||
DateTimes getSunTimings(QDate date, double latitude, double longitude, bool morning) const;
|
DateTimes getSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning) const;
|
||||||
bool checkAutomaticSunTimings() const;
|
bool checkAutomaticSunTimings() const;
|
||||||
bool daylight() const;
|
bool daylight() const;
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QTimeZone>
|
||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
|
|
||||||
namespace KWin {
|
namespace KWin {
|
||||||
|
@ -31,7 +32,14 @@ namespace ColorCorrect {
|
||||||
#define SUN_RISE_SET -0.833
|
#define SUN_RISE_SET -0.833
|
||||||
#define SUN_HIGH 2.0
|
#define SUN_HIGH 2.0
|
||||||
|
|
||||||
QPair<QTime, QTime> calculateSunTimings(QDate prompt, double latitude, double longitude, bool morning)
|
static QTime convertToLocalTime(const QDateTime &when, const QTime &utcTime)
|
||||||
|
{
|
||||||
|
const QTimeZone timeZone = QTimeZone::systemTimeZone();
|
||||||
|
const int utcOffset = timeZone.offsetFromUtc(when);
|
||||||
|
return utcTime.addSecs(utcOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
QPair<QDateTime, QDateTime> calculateSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning)
|
||||||
{
|
{
|
||||||
// calculations based on https://aa.quae.nl/en/reken/zonpositie.html
|
// calculations based on https://aa.quae.nl/en/reken/zonpositie.html
|
||||||
// accuracy: +/- 5min
|
// accuracy: +/- 5min
|
||||||
|
@ -44,7 +52,8 @@ QPair<QTime, QTime> calculateSunTimings(QDate prompt, double latitude, double lo
|
||||||
const double lng = -longitude; // lw
|
const double lng = -longitude; // lw
|
||||||
|
|
||||||
// times
|
// times
|
||||||
const double juPrompt = prompt.toJulianDay(); // J
|
const QDateTime utcDateTime = dateTime.toUTC();
|
||||||
|
const double juPrompt = utcDateTime.date().toJulianDay(); // J
|
||||||
const double ju2000 = 2451545.; // J2000
|
const double ju2000 = 2451545.; // J2000
|
||||||
|
|
||||||
// geometry
|
// geometry
|
||||||
|
@ -141,22 +150,24 @@ QPair<QTime, QTime> calculateSunTimings(QDate prompt, double latitude, double lo
|
||||||
begin += 0.5;
|
begin += 0.5;
|
||||||
end += 0.5;
|
end += 0.5;
|
||||||
|
|
||||||
QTime timeBegin, timeEnd;
|
QDateTime dateTimeBegin;
|
||||||
|
QDateTime dateTimeEnd;
|
||||||
|
|
||||||
if (std::isnan(begin)) {
|
if (!std::isnan(begin)) {
|
||||||
timeBegin = QTime();
|
const double dayFraction = begin - int(begin);
|
||||||
} else {
|
const QTime utcTime = QTime::fromMSecsSinceStartOfDay(dayFraction * MSC_DAY);
|
||||||
double timePart = begin - (int)begin;
|
const QTime localTime = convertToLocalTime(dateTime, utcTime);
|
||||||
timeBegin = QTime::fromMSecsSinceStartOfDay((int)( timePart * MSC_DAY ));
|
dateTimeBegin = QDateTime(dateTime.date(), localTime);
|
||||||
}
|
|
||||||
if (std::isnan(end)) {
|
|
||||||
timeEnd = QTime();
|
|
||||||
} else {
|
|
||||||
double timePart = end - (int)end;
|
|
||||||
timeEnd = QTime::fromMSecsSinceStartOfDay((int)( timePart * MSC_DAY ));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return QPair<QTime,QTime> (timeBegin, timeEnd);
|
if (!std::isnan(end)) {
|
||||||
|
const double dayFraction = end - int(end);
|
||||||
|
const QTime utcTime = QTime::fromMSecsSinceStartOfDay(dayFraction * MSC_DAY);
|
||||||
|
const QTime localTime = convertToLocalTime(dateTime, utcTime);
|
||||||
|
dateTimeEnd = QDateTime(dateTime.date(), localTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { dateTimeBegin, dateTimeEnd };
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ namespace ColorCorrect
|
||||||
* @since 5.12
|
* @since 5.12
|
||||||
*/
|
*/
|
||||||
|
|
||||||
QPair<QTime, QTime> calculateSunTimings(QDate prompt, double latitude, double longitude, bool morning);
|
QPair<QDateTime, QDateTime> calculateSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue