From 011db4495d98932cbffa5fc2268ee1a3e2984aa6 Mon Sep 17 00:00:00 2001 From: Keshav Bhatt Date: Fri, 27 Jan 2023 02:57:48 +0530 Subject: chore: cleanup - move methods to page --- src/SunClock.cpp | 276 -------------------------------- src/SunClock.hpp | 126 --------------- src/WhatsApp.pro | 10 +- src/automatictheme.cpp | 2 +- src/downloadwidget.h | 2 +- src/downloadwidget.ui | 3 +- src/elidedlabel.cpp | 52 ------ src/elidedlabel.h | 49 ------ src/mainwindow.cpp | 147 +++-------------- src/mainwindow.h | 5 - src/sunclock.cpp | 276 ++++++++++++++++++++++++++++++++ src/sunclock.hpp | 126 +++++++++++++++ src/webenginepage.cpp | 113 ++++++++++++- src/webenginepage.h | 5 + src/widgets/elidedlabel/elidedlabel.cpp | 52 ++++++ src/widgets/elidedlabel/elidedlabel.h | 49 ++++++ 16 files changed, 648 insertions(+), 645 deletions(-) delete mode 100644 src/SunClock.cpp delete mode 100644 src/SunClock.hpp delete mode 100644 src/elidedlabel.cpp delete mode 100644 src/elidedlabel.h create mode 100644 src/sunclock.cpp create mode 100644 src/sunclock.hpp create mode 100644 src/widgets/elidedlabel/elidedlabel.cpp create mode 100644 src/widgets/elidedlabel/elidedlabel.h (limited to 'src') diff --git a/src/SunClock.cpp b/src/SunClock.cpp deleted file mode 100644 index 132c0a6..0000000 --- a/src/SunClock.cpp +++ /dev/null @@ -1,276 +0,0 @@ -#include -#include -#include - -inline double rad(double degrees) { - static const double degToRad = 4.0 * atan(1.0) / 180.0; - return degrees * degToRad; -} - -inline double deg(double radians) { - static const double radToDeg = 180.0 / (4.0 * atan(1.0)); - return radians * radToDeg; -} - -Sunclock::Sunclock(double const &latitude_, double const &longitude_, - double const &tz_offset_) - : latitude(latitude_), longitude(longitude_), tz_offset(tz_offset_) {} - -double Sunclock::irradiance() { return irradiance(time(0)); } - -double Sunclock::irradiance(time_t when) { - when = when + (time_t)(tz_offset * 60 * 60); - struct tm *t = gmtime(&when); - double _time_of_day = time_of_day(when); - double _julian_day = julian_day(t, _time_of_day, tz_offset); - double _julian_century = julian_century(_julian_day); - double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); - double _mean_long_sun = mean_long_sun(_julian_century); - double _mean_anom_sun = mean_anom_sun(_julian_century); - double _sun_eq_of_centre = sun_eq_of_centre(_mean_anom_sun, _julian_century); - double _sun_true_long = sun_true_long(_mean_long_sun, _sun_eq_of_centre); - double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); - double _sun_app_long = sun_app_long(_sun_true_long, _julian_century); - double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); - double _var_y = var_y(_obliq_corr); - double _eq_of_time = - eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); - double _declination = declination(_obliq_corr, _sun_app_long); - double _true_solar_time = true_solar_time(_time_of_day, _eq_of_time); - double _hour_angle = hour_angle(_true_solar_time); - double _solar_zenith = solar_zenith(_declination, _hour_angle); - return std::cos(rad(_solar_zenith)); -} - -time_t Sunclock::sunrise() { return sunrise(time(0)); } - -time_t Sunclock::sunrise(time_t date) { - date = date + (time_t)(tz_offset * 60 * 60); - struct tm *t = gmtime(&date); - double _time_of_day = time_of_day(date); - double _julian_day = julian_day(t, _time_of_day, tz_offset); - double _julian_century = julian_century(_julian_day); - double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); - double _mean_long_sun = mean_long_sun(_julian_century); - double _mean_anom_sun = mean_anom_sun(_julian_century); - double _sun_eq_of_centre = sun_eq_of_centre(_mean_anom_sun, _julian_century); - double _sun_true_long = sun_true_long(_mean_long_sun, _sun_eq_of_centre); - double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); - double _sun_app_long = sun_app_long(_sun_true_long, _julian_century); - double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); - double _var_y = var_y(_obliq_corr); - double _eq_of_time = - eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); - double _declination = declination(_obliq_corr, _sun_app_long); - double _hour_angle_sunrise = hour_angle_sunrise(_declination); - - double noon_decimal_day = - (720 - 4 * longitude - _eq_of_time + tz_offset * 60) / 1440; - double decimal_day = noon_decimal_day - _hour_angle_sunrise * 4 / 1440; - return time_from_decimal_day(date, decimal_day) - - (time_t)(tz_offset * 60 * 60); -} - -time_t Sunclock::solar_noon() { return solar_noon(time(0)); } - -time_t Sunclock::solar_noon(time_t date) { - date = date + (time_t)(tz_offset * 60 * 60); - struct tm *t = gmtime(&date); - double _time_of_day = time_of_day(date); - double _julian_day = julian_day(t, _time_of_day, tz_offset); - double _julian_century = julian_century(_julian_day); - double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); - double _mean_long_sun = mean_long_sun(_julian_century); - double _mean_anom_sun = mean_anom_sun(_julian_century); - double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); - double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); - double _var_y = var_y(_obliq_corr); - double _eq_of_time = - eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); - - double decimal_day = - (720 - 4 * longitude - _eq_of_time + tz_offset * 60) / 1440; - return time_from_decimal_day(date, decimal_day) - - (time_t)(tz_offset * 60 * 60); -} - -time_t Sunclock::sunset() { return sunset(time(0)); } - -time_t Sunclock::sunset(time_t date) { - date = date + (time_t)(tz_offset * 60 * 60); - struct tm *t = gmtime(&date); - double _time_of_day = time_of_day(date); - double _julian_day = julian_day(t, _time_of_day, tz_offset); - double _julian_century = julian_century(_julian_day); - double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); - double _mean_long_sun = mean_long_sun(_julian_century); - double _mean_anom_sun = mean_anom_sun(_julian_century); - double _sun_eq_of_centre = sun_eq_of_centre(_mean_anom_sun, _julian_century); - double _sun_true_long = sun_true_long(_mean_long_sun, _sun_eq_of_centre); - double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); - double _sun_app_long = sun_app_long(_sun_true_long, _julian_century); - double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); - double _var_y = var_y(_obliq_corr); - double _eq_of_time = - eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); - double _declination = declination(_obliq_corr, _sun_app_long); - double _hour_angle_sunrise = hour_angle_sunrise(_declination); - - double noon_decimal_day = - (720 - 4 * longitude - _eq_of_time + tz_offset * 60) / 1440; - double decimal_day = noon_decimal_day + _hour_angle_sunrise * 4 / 1440; - return time_from_decimal_day(date, decimal_day) - - (time_t)(tz_offset * 60 * 60); -} - -double Sunclock::time_of_day(time_t date) { - struct tm *t = gmtime(&date); - return (t->tm_hour + t->tm_min / 60.0 + t->tm_sec / 3600.0) / 24.0; -} - -time_t Sunclock::time_from_decimal_day(time_t date, double decimal_day) { - struct std::tm epoch; - epoch.tm_isdst = 0; - epoch.tm_sec = epoch.tm_min = epoch.tm_hour = epoch.tm_mon = 0; - epoch.tm_mday = 1; - epoch.tm_year = 70; - time_t local_tz_offset = mktime(&epoch); - - struct tm *dt = gmtime(&date); - struct tm t = {}; - t.tm_year = dt->tm_year; - t.tm_mon = dt->tm_mon; - t.tm_mday = dt->tm_mday; - double hours = 24.0 * decimal_day; - t.tm_hour = int(hours); - double minutes = (hours - t.tm_hour) * 60; - t.tm_min = int(minutes); - double seconds = (minutes - t.tm_sec) * 60; - t.tm_sec = int(seconds) % 60; - return mktime(&t) - local_tz_offset; -} - -int Sunclock::days_since_1900(struct tm *t) { - int year = t->tm_year; - if (year < 0 || year > 199) { - throw std::invalid_argument( - "days_since_1900 - Date must be between 1900 and 2099"); - } - int month = t->tm_mon + 1; - int days = t->tm_mday; - - if (month < 3) { - month += 12; - year--; - } - int yearDays = (int)(year * 365.25); - int monthDays = (int)((month + 1) * 30.61); - - return (yearDays + monthDays + days - 63); -} - -double Sunclock::julian_day(struct tm *t, double const &time_of_day, - double const &tz_offset) { - return days_since_1900(t) + 2415018.5 + time_of_day - tz_offset / 24; -} - -double Sunclock::julian_century(double _julian_day) { - return (_julian_day - 2451545.0) / 36525.0; -} - -double Sunclock::mean_long_sun(double _julian_century) { - return ( - 280.46646 + - fmod(_julian_century * (36000.76983 + _julian_century * 0.0003032), 360)); -} - -double Sunclock::mean_anom_sun(double _julian_century) { - return 357.52911 + - _julian_century * (35999.05029 - 0.0001537 * _julian_century); -} - -double Sunclock::sun_eq_of_centre(double _mean_anom_sun, - double _julian_century) { - return sin(rad(_mean_anom_sun)) * - (1.914602 - - _julian_century * (0.004817 + 0.000014 * _julian_century)) + - sin(rad(2 * _mean_anom_sun)) * - (0.019993 - 0.000101 * _julian_century) + - sin(rad(3 * _mean_anom_sun)) * 0.000289; -} - -double Sunclock::sun_true_long(double _mean_long_sun, - double _sun_eq_of_centre) { - return _mean_long_sun + _sun_eq_of_centre; -} - -double Sunclock::eccent_earth_orbit(double _julian_century) { - return 0.016708634 - - _julian_century * (0.000042037 + 0.0001537 * _julian_century); -} - -double Sunclock::var_y(double _obliq_corr) { - return tan(rad(_obliq_corr / 2)) * tan(rad(_obliq_corr / 2)); -} - -double Sunclock::mean_obliq_ecliptic(double _julian_century) { - return (23 + (26 + ((21.448 - _julian_century * - (46.815 + _julian_century * - (0.00059 - _julian_century * - 0.001813)))) / - 60) / - 60); -} - -double Sunclock::obliq_corr(double _mean_obliq_ecliptic, - double _julian_century) { - return _mean_obliq_ecliptic + - 0.00256 * cos(deg(125.04 - 1934.136 * _julian_century)); -} - -double Sunclock::sun_app_long(double _sun_true_long, double _julian_century) { - return (_sun_true_long - 0.00569 - - 0.00478 * sin(deg(125.04 - 1934.136 * _julian_century))); -} - -double Sunclock::declination(double _obliq_corr, double _sun_app_long) { - return deg(asin(sin(rad(_obliq_corr)) * sin(rad(_sun_app_long)))); -} - -double Sunclock::eq_of_time(double _var_y, double _mean_long_sun, - double _eccent_earth_orbit, double _mean_anom_sun) { - return 4 * deg(_var_y * sin(2 * rad(_mean_long_sun)) - - 2 * _eccent_earth_orbit * sin(rad(_mean_anom_sun)) + - 4 * _eccent_earth_orbit * _var_y * sin(rad(_mean_anom_sun)) * - cos(2 * rad(_mean_long_sun)) - - 0.5 * _var_y * _var_y * sin(4 * rad(_mean_long_sun)) - - 1.25 * _eccent_earth_orbit * _eccent_earth_orbit * - sin(2 * rad(_mean_anom_sun))); -} - -double Sunclock::true_solar_time(double _time_of_day, double _eq_of_time) { - return fmod( - (_time_of_day * 1440 + _eq_of_time + 4 * longitude - 60 * tz_offset), - 1440); -} - -double Sunclock::hour_angle(double _true_solar_time) { - return (_true_solar_time / 4 < 0 ? _true_solar_time / 4 + 180 - : _true_solar_time / 4 - 180); -} - -double Sunclock::hour_angle_sunrise(double _declination) { - return deg( - acos(cos(rad(90.833)) / (cos(rad(latitude)) * cos(rad(_declination))) - - tan(rad(latitude)) * tan(rad(_declination)))); -} - -double Sunclock::solar_zenith(double _declination, double _hour_angle) { - return deg(acos(sin(rad(latitude)) * sin(rad(_declination)) + - cos(rad(latitude)) * cos(rad(_declination)) * - cos(rad(_hour_angle)))); -} - -double Sunclock::solar_elevation(double _solar_zenith) { - return 90 - _solar_zenith; -} diff --git a/src/SunClock.hpp b/src/SunClock.hpp deleted file mode 100644 index 4cfad57..0000000 --- a/src/SunClock.hpp +++ /dev/null @@ -1,126 +0,0 @@ -#ifndef DAYLIGHT_SUNCLOCK_HPP -#define DAYLIGHT_SUNCLOCK_HPP - -#include - -class Sunclock { -public: - Sunclock(double const &latitude_, double const &longitude_, double const &tz_offset_ = 0); - - /** - * \overload double Sunclock::irradiance(time_t when) - */ - double irradiance(); - - /** - * Specifically, we calculate Direct Normal Irradiance - * That is we do not consider atmospheric components like cloud cover, - * moisture content etc. - * - * @param when - * @return a value representing irradiance (power per unit area) - */ - double irradiance(time_t when); - - /** - * \overload time_t Sunclock::sunrise(time_t date) - */ - time_t sunrise(); - - /** - * Returns sunrise time for given date - * - * @param date only date is considered - * @return sunrise time - */ - time_t sunrise(time_t date); - - /** - * \overload time_t Sunclock::solar_noon(time_t date) - */ - time_t solar_noon(); - - /** - * Returns solar_noon time for given date - * - * @param date only date is considered - * @return solar_noon time - */ - time_t solar_noon(time_t date); - - /** - * \overload time_t Sunclock::sunset(time_t date) - */ - time_t sunset(); - - /** - * Returns sunset time for given date - * - * @param date only date is considered - * @return sunset time - */ - time_t sunset(time_t date); - -private: - // in decimal degrees, east is positive - double const latitude; - - // in decimal degrees, north is positive - double const longitude; - - // in hours, east is positive, i.e. IST (+05:30) is 5.5 - double const tz_offset; - - /** - * @param date - * @return percentage past midnight, i.e. noon is 0.5 - */ - double time_of_day(time_t date); - - static int days_since_1900(struct tm *t); - - /** - * @param date - * @param decimal_day decimal between 0.0 and 1.0, e.g. noon = 0.5 - * @return time_t with date and time set accordingly - */ - static time_t time_from_decimal_day(time_t date, double decimal_day); - - /** - * Get day count since Monday, January 1, 4713 BC - * https://en.wikipedia.org/wiki/Julian_day - * - * @param t - * @param time_of_day percentage past midnight, i.e. noon is 0.5 - * @param tz_offset in hours, east is positive, i.e. IST (+05:30) is 5.5 - * @return julian day - */ - static double julian_day(struct tm *t, double const &time_of_day, double const &tz_offset); - - /** - * Extract information in a suitable format from a time_t object. - */ - void prepare_time(time_t const &when, struct tm *ptm); - - double julian_century(double _julian_day); - double mean_long_sun(double _julian_century); - double mean_anom_sun(double _julian_century); - double sun_eq_of_centre(double _mean_anom_sun, double _julian_century); - double eccent_earth_orbit(double _julian_century); - double var_y(double _obliq_corr); - double mean_obliq_ecliptic(double _julian_century); - double sun_true_long(double _mean_long_sun, double _sun_eq_of_centre); - double obliq_corr(double _mean_obliq_ecliptic, double _julian_century); - double sun_app_long(double _sun_true_long, double _julian_century); - double declination(double _obliq_corr, double _sun_app_long); - double eq_of_time(double _var_y, double _mean_long_sun, double _eccent_earth_orbit, - double _mean_anom_sun); - double true_solar_time(double _time_of_day, double _eq_of_time); - double hour_angle(double _true_solar_time); - double hour_angle_sunrise(double _declination); - double solar_zenith(double _declination, double _hour_angle); - double solar_elevation(double _solar_zenith); -}; - -#endif - diff --git a/src/WhatsApp.pro b/src/WhatsApp.pro index 36b1ca2..844c000 100644 --- a/src/WhatsApp.pro +++ b/src/WhatsApp.pro @@ -59,29 +59,27 @@ DEFINES += VERSIONSTR=\\\"$${VERSION}\\\" # DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ - SunClock.cpp \ about.cpp \ automatictheme.cpp \ dictionaries.cpp \ downloadmanagerwidget.cpp \ downloadwidget.cpp \ - elidedlabel.cpp \ lock.cpp \ main.cpp \ mainwindow.cpp \ permissiondialog.cpp \ rateapp.cpp \ settingswidget.cpp \ + sunclock.cpp \ utils.cpp \ webenginepage.cpp \ webview.cpp \ + widgets/elidedlabel/elidedlabel.cpp \ widgets/scrolltext/scrolltext.cpp - RESOURCES += \ icons.qrc HEADERS += \ - SunClock.hpp \ about.h \ autolockeventfilter.h \ automatictheme.h \ @@ -90,7 +88,6 @@ HEADERS += \ dictionaries.h \ downloadmanagerwidget.h \ downloadwidget.h \ - elidedlabel.h \ lock.h \ mainwindow.h \ notificationpopup.h \ @@ -98,11 +95,12 @@ HEADERS += \ rateapp.h \ requestinterceptor.h \ settingswidget.h \ + sunclock.hpp \ utils.h \ webenginepage.h \ webview.h \ + widgets/elidedlabel/elidedlabel.h \ widgets/scrolltext/scrolltext.h - FORMS += \ about.ui \ automatictheme.ui \ diff --git a/src/automatictheme.cpp b/src/automatictheme.cpp index 6e14623..3cf33c8 100644 --- a/src/automatictheme.cpp +++ b/src/automatictheme.cpp @@ -1,7 +1,7 @@ #include "automatictheme.h" #include "ui_automatictheme.h" -#include "SunClock.hpp" +#include "sunclock.hpp" #include AutomaticTheme::AutomaticTheme(QWidget *parent) diff --git a/src/downloadwidget.h b/src/downloadwidget.h index ff9e488..074a979 100644 --- a/src/downloadwidget.h +++ b/src/downloadwidget.h @@ -52,7 +52,7 @@ #define DOWNLOADWIDGET_H #include "ui_downloadwidget.h" -#include "elidedlabel.h" +#include "widgets/elidedlabel/elidedlabel.h" #include diff --git a/src/downloadwidget.ui b/src/downloadwidget.ui index d405b8f..45d16e9 100644 --- a/src/downloadwidget.ui +++ b/src/downloadwidget.ui @@ -47,7 +47,6 @@ - 75 true @@ -72,7 +71,7 @@ ElidedLabel QLabel -
elidedlabel.h
+
widgets/elidedlabel/elidedlabel.h
diff --git a/src/elidedlabel.cpp b/src/elidedlabel.cpp deleted file mode 100644 index e870959..0000000 --- a/src/elidedlabel.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "elidedlabel.h" - -#include -#include -#include -#include - -ElidedLabel::ElidedLabel(QWidget *parent) - : QLabel(parent), m_elide_mode(Qt::ElideRight) { - setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); -} - -ElidedLabel::ElidedLabel(const QString &txt, QWidget *parent) - : QLabel(txt, parent), m_elide_mode(Qt::ElideRight) { - setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); -} - -ElidedLabel::ElidedLabel(const QString &txt, Qt::TextElideMode elideMode, - QWidget *parent) - : QLabel(txt, parent), m_elide_mode(elideMode) { - setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); -} - -void ElidedLabel::setText(const QString &txt) { - QLabel::setText(txt); - cacheElidedText(geometry().width()); - setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); -} - -void ElidedLabel::cacheElidedText(int w) { - m_cached_elided_text = fontMetrics().elidedText( - text(), m_elide_mode, w, (buddy() == nullptr) ? 0 : Qt::TextShowMnemonic); -} - -void ElidedLabel::resizeEvent(QResizeEvent *e) { - QLabel::resizeEvent(e); - cacheElidedText(e->size().width()); -} - -void ElidedLabel::paintEvent(QPaintEvent *e) { - if (m_elide_mode == Qt::ElideNone) { - QLabel::paintEvent(e); - } else { - QPainter p(this); - p.drawText(0, 0, geometry().width(), geometry().height(), - QStyle::visualAlignment(text().isRightToLeft() ? Qt::RightToLeft - : Qt::LeftToRight, - alignment()) | - ((buddy() == nullptr) ? 0 : Qt::TextShowMnemonic), - m_cached_elided_text); - } -} diff --git a/src/elidedlabel.h b/src/elidedlabel.h deleted file mode 100644 index 7dc610a..0000000 --- a/src/elidedlabel.h +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -// A label that elides its text when not enough geometry is available to show all of the text. -// Currently only capable of one-line. -class ElidedLabel : public QLabel { - Q_OBJECT - -private: - Qt::TextElideMode m_elide_mode; - QString m_cached_elided_text; - - -public: - ElidedLabel(QWidget* parent = NULL); - ElidedLabel(const QString& txt, QWidget* parent = NULL); - ElidedLabel(const QString& txt, Qt::TextElideMode elideMode = Qt::ElideRight, QWidget* parent = NULL); - -public: - // Set the elide mode used for displaying text. - inline void setElideMode(Qt::TextElideMode elideMode) { - m_elide_mode = elideMode; - updateGeometry(); - } - - // Get the elide mode currently used to display text. - inline Qt::TextElideMode elideMode() const { - return m_elide_mode; - } - - - -public: // QLabel overrides - void setText(const QString&); // note: not virtual so no polymorphism ... - - -protected: // QLabel overrides - virtual void paintEvent(QPaintEvent*) override; - virtual void resizeEvent(QResizeEvent*) override; - -protected: - // Cache the elided text so as to not recompute it every paint event - void cacheElidedText(int w); - -}; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3d01ba0..ae8fdbe 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -205,11 +205,11 @@ void MainWindow::updateWindowTheme() { settings.value("widgetStyle", "Fusion").toString())); if (settings.value("windowTheme", "light").toString() == "dark") { qApp->setPalette(darkPalette); - this->webEngine->setStyleSheet( + webEngine->setStyleSheet( "QWebEngineView{background:rgb(17, 27, 33);}"); // whatsapp dark color } else { qApp->setPalette(lightPalette); - this->webEngine->setStyleSheet( + webEngine->setStyleSheet( "QWebEngineView{background:#F0F0F0;}"); // whatsapp light color } @@ -316,7 +316,7 @@ void MainWindow::initSettingWidget() { webSettings->setAttribute( QWebEngineSettings::PlaybackRequiresUserGesture, checked); - this->webEngine->page()->profile()->settings()->setAttribute( + webEngine->page()->profile()->settings()->setAttribute( QWebEngineSettings::PlaybackRequiresUserGesture, checked); }); @@ -357,7 +357,7 @@ void MainWindow::initSettingWidget() { connect(settingsWidget, &SettingsWidget::notificationPopupTimeOutChanged, settingsWidget, [=]() { - setNotificationPresenter(this->webEngine->page()->profile()); + setNotificationPresenter(webEngine->page()->profile()); }); connect(settingsWidget, &SettingsWidget::notify, settingsWidget, @@ -479,7 +479,7 @@ void MainWindow::showSettings(bool isAskedByCLI) { void MainWindow::updateSettingsUserAgentWidget() { settingsWidget->updateDefaultUAButton( - this->webEngine->page()->profile()->httpUserAgent()); + webEngine->page()->profile()->httpUserAgent()); } void MainWindow::askToReloadPage() { @@ -806,16 +806,16 @@ void MainWindow::createWebEngine() { m_dictionaries = Dictionaries::GetDictionaries(); - WebView *webEngine = new WebView(this, m_dictionaries); - setCentralWidget(webEngine); - webEngine->setSizePolicy(widgetSize); - webEngine->show(); + WebView *webEngineView = new WebView(this, m_dictionaries); + setCentralWidget(webEngineView); + webEngineView->setSizePolicy(widgetSize); + webEngineView->show(); - this->webEngine = webEngine; + this->webEngine = webEngineView; - webEngine->addAction(minimizeAction); - webEngine->addAction(lockAction); - webEngine->addAction(quitAction); + webEngineView->addAction(minimizeAction); + webEngineView->addAction(lockAction); + webEngineView->addAction(quitAction); createWebPage(false); } @@ -964,119 +964,14 @@ void MainWindow::handleLoadFinished(bool loaded) { checkLoadedCorrectly(); updatePageTheme(); handleZoom(); - injectMutationObserver(); - injectPreventScrollWheelZoomHelper(); - injectFullWidthJavaScript(); - injectClassChangeObserver(); - injectNewChatJavaScript(); + if (settingsWidget != nullptr) { settingsWidget->refresh(); } } } -void MainWindow::injectPreventScrollWheelZoomHelper() { - QString js = R"( - (function () { - const SSWZ = function () { - this.keyScrollHandler = function (e) { - if (e.ctrlKey) { - e.preventDefault(); - return false; - } - } - }; - if (window === top) { - const sswz = new SSWZ(); - window.addEventListener('wheel', sswz.keyScrollHandler, { - passive: false - }); - } - })(); - )"; - webEngine->page()->runJavaScript(js); -} - -void MainWindow::injectClassChangeObserver() { - QString js = R"( - const observer = new MutationObserver(() => { - var haveFullView = document.body.classList.contains('whatsie-full-view'); - var container = document.querySelector('#app > .app-wrapper-web > div'); - if(container){ - if(haveFullView){ - container.style.width = '100%'; - container.style.height = '100%'; - container.style.top = '0'; - container.style.maxWidth = 'unset'; - }else{ - container.style.width = null; - container.style.height = null; - container.style.top = null; - container.style.maxWidth = null; - } - } - }); - observer.observe(document.body, { - attributes: true, - attributeFilter: ['class'], - childList: false, - characterData: false - });)"; - webEngine->page()->runJavaScript(js); -} -void MainWindow::injectMutationObserver() { - QString js = - R"(function waitForElement(selector) { - return new Promise(resolve => { - if (document.querySelector(selector)) { - return resolve(document.querySelector(selector)); - } - const observer = new MutationObserver(mutations => { - if (document.querySelector(selector)) { - resolve(document.querySelector(selector)); - observer.disconnect(); - } - }); - observer.observe(document.body, { - childList: true, - subtree: true - }); - }); - };)"; - webEngine->page()->runJavaScript(js); -} - -void MainWindow::injectFullWidthJavaScript() { - if (!settings.value("fullWidthView", true).toBool()) - return; - QString js = - R"(waitForElement('#pane-side').then( () => { - var container = document.querySelector('#app > .app-wrapper-web > div'); - container.style.width = '100%'; - container.style.height = '100%'; - container.style.top = '0'; - container.style.maxWidth = 'unset'; - }); - )"; - webEngine->page()->runJavaScript(js); -} - -void MainWindow::injectNewChatJavaScript() { - QString js = R"(const openNewChatWhatsie = (phone,text) => { - const link = document.createElement('a'); - link.setAttribute('href', - `whatsapp://send/?phone=${phone}&text=${text}`); - document.body.append(link); - link.click(); - document.body.removeChild(link); - }; - function openNewChatWhatsieDefined() - { - return (openNewChatWhatsie != 'undefined'); - })"; - webEngine->page()->runJavaScript(js); -} void MainWindow::checkLoadedCorrectly() { if (webEngine && webEngine->page()) { @@ -1172,8 +1067,8 @@ void MainWindow::messageClicked() { void MainWindow::doAppReload() { - if (this->webEngine->page()) { - this->webEngine->page()->disconnect(); + if (webEngine->page()) { + webEngine->page()->disconnect(); } createWebPage(false); } @@ -1195,7 +1090,7 @@ void MainWindow::triggerNewChat(QString phone, QString text) { "openNewChatWhatsieDefined()", [this, phone, text](const QVariant &result) { if (result.toString().contains("true")) { - this->webEngine->page()->runJavaScript( + webEngine->page()->runJavaScript( QString("openNewChatWhatsie(\"%1\",\"%2\")").arg(phone, text)); } else { // create send url equivalent @@ -1203,7 +1098,7 @@ void MainWindow::triggerNewChat(QString phone, QString text) { textStr = text.isEmpty() ? "" : "text=" + text; QString urlStr = "https://web.whatsapp.com/send?" + phoneStr + "&" + textStr; - this->webEngine->page()->load(QUrl(urlStr)); + webEngine->page()->load(QUrl(urlStr)); } this->alreadyRunning(); }); @@ -1212,7 +1107,7 @@ void MainWindow::triggerNewChat(QString phone, QString text) { void MainWindow::doReload(bool byPassCache, bool isAskedByCLI, bool byLoadingQuirk) { if (byLoadingQuirk) { - this->webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache, + webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache, byPassCache); } else { if (lockWidget && !lockWidget->getIsLocked()) { @@ -1229,13 +1124,13 @@ void MainWindow::doReload(bool byPassCache, bool isAskedByCLI, this->show(); return; } - this->webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache, + webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache, byPassCache); } } void MainWindow::toggleMute(const bool &checked) { - this->webEngine->page()->setAudioMuted(checked); + webEngine->page()->setAudioMuted(checked); } // get value of page theme when page is loaded diff --git a/src/mainwindow.h b/src/mainwindow.h index 98dbf8b..f6346ad 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -129,13 +129,8 @@ private slots: bool isLoggedIn(); void initAutoLock(); void appAutoLockChanged(); - void injectNewChatJavaScript(); void triggerNewChat(QString phone, QString text); void restoreMainWindow(); - void injectFullWidthJavaScript(); - void injectMutationObserver(); - void injectClassChangeObserver(); - void injectPreventScrollWheelZoomHelper(); }; #endif // MAINWINDOW_H diff --git a/src/sunclock.cpp b/src/sunclock.cpp new file mode 100644 index 0000000..29400f1 --- /dev/null +++ b/src/sunclock.cpp @@ -0,0 +1,276 @@ +#include +#include +#include + +inline double rad(double degrees) { + static const double degToRad = 4.0 * atan(1.0) / 180.0; + return degrees * degToRad; +} + +inline double deg(double radians) { + static const double radToDeg = 180.0 / (4.0 * atan(1.0)); + return radians * radToDeg; +} + +Sunclock::Sunclock(double const &latitude_, double const &longitude_, + double const &tz_offset_) + : latitude(latitude_), longitude(longitude_), tz_offset(tz_offset_) {} + +double Sunclock::irradiance() { return irradiance(time(0)); } + +double Sunclock::irradiance(time_t when) { + when = when + (time_t)(tz_offset * 60 * 60); + struct tm *t = gmtime(&when); + double _time_of_day = time_of_day(when); + double _julian_day = julian_day(t, _time_of_day, tz_offset); + double _julian_century = julian_century(_julian_day); + double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); + double _mean_long_sun = mean_long_sun(_julian_century); + double _mean_anom_sun = mean_anom_sun(_julian_century); + double _sun_eq_of_centre = sun_eq_of_centre(_mean_anom_sun, _julian_century); + double _sun_true_long = sun_true_long(_mean_long_sun, _sun_eq_of_centre); + double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); + double _sun_app_long = sun_app_long(_sun_true_long, _julian_century); + double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); + double _var_y = var_y(_obliq_corr); + double _eq_of_time = + eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); + double _declination = declination(_obliq_corr, _sun_app_long); + double _true_solar_time = true_solar_time(_time_of_day, _eq_of_time); + double _hour_angle = hour_angle(_true_solar_time); + double _solar_zenith = solar_zenith(_declination, _hour_angle); + return std::cos(rad(_solar_zenith)); +} + +time_t Sunclock::sunrise() { return sunrise(time(0)); } + +time_t Sunclock::sunrise(time_t date) { + date = date + (time_t)(tz_offset * 60 * 60); + struct tm *t = gmtime(&date); + double _time_of_day = time_of_day(date); + double _julian_day = julian_day(t, _time_of_day, tz_offset); + double _julian_century = julian_century(_julian_day); + double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); + double _mean_long_sun = mean_long_sun(_julian_century); + double _mean_anom_sun = mean_anom_sun(_julian_century); + double _sun_eq_of_centre = sun_eq_of_centre(_mean_anom_sun, _julian_century); + double _sun_true_long = sun_true_long(_mean_long_sun, _sun_eq_of_centre); + double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); + double _sun_app_long = sun_app_long(_sun_true_long, _julian_century); + double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); + double _var_y = var_y(_obliq_corr); + double _eq_of_time = + eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); + double _declination = declination(_obliq_corr, _sun_app_long); + double _hour_angle_sunrise = hour_angle_sunrise(_declination); + + double noon_decimal_day = + (720 - 4 * longitude - _eq_of_time + tz_offset * 60) / 1440; + double decimal_day = noon_decimal_day - _hour_angle_sunrise * 4 / 1440; + return time_from_decimal_day(date, decimal_day) - + (time_t)(tz_offset * 60 * 60); +} + +time_t Sunclock::solar_noon() { return solar_noon(time(0)); } + +time_t Sunclock::solar_noon(time_t date) { + date = date + (time_t)(tz_offset * 60 * 60); + struct tm *t = gmtime(&date); + double _time_of_day = time_of_day(date); + double _julian_day = julian_day(t, _time_of_day, tz_offset); + double _julian_century = julian_century(_julian_day); + double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); + double _mean_long_sun = mean_long_sun(_julian_century); + double _mean_anom_sun = mean_anom_sun(_julian_century); + double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); + double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); + double _var_y = var_y(_obliq_corr); + double _eq_of_time = + eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); + + double decimal_day = + (720 - 4 * longitude - _eq_of_time + tz_offset * 60) / 1440; + return time_from_decimal_day(date, decimal_day) - + (time_t)(tz_offset * 60 * 60); +} + +time_t Sunclock::sunset() { return sunset(time(0)); } + +time_t Sunclock::sunset(time_t date) { + date = date + (time_t)(tz_offset * 60 * 60); + struct tm *t = gmtime(&date); + double _time_of_day = time_of_day(date); + double _julian_day = julian_day(t, _time_of_day, tz_offset); + double _julian_century = julian_century(_julian_day); + double _mean_obliq_ecliptic = mean_obliq_ecliptic(_julian_century); + double _mean_long_sun = mean_long_sun(_julian_century); + double _mean_anom_sun = mean_anom_sun(_julian_century); + double _sun_eq_of_centre = sun_eq_of_centre(_mean_anom_sun, _julian_century); + double _sun_true_long = sun_true_long(_mean_long_sun, _sun_eq_of_centre); + double _obliq_corr = obliq_corr(_mean_obliq_ecliptic, _julian_century); + double _sun_app_long = sun_app_long(_sun_true_long, _julian_century); + double _eccent_earth_orbit = eccent_earth_orbit(_julian_century); + double _var_y = var_y(_obliq_corr); + double _eq_of_time = + eq_of_time(_var_y, _mean_long_sun, _eccent_earth_orbit, _mean_anom_sun); + double _declination = declination(_obliq_corr, _sun_app_long); + double _hour_angle_sunrise = hour_angle_sunrise(_declination); + + double noon_decimal_day = + (720 - 4 * longitude - _eq_of_time + tz_offset * 60) / 1440; + double decimal_day = noon_decimal_day + _hour_angle_sunrise * 4 / 1440; + return time_from_decimal_day(date, decimal_day) - + (time_t)(tz_offset * 60 * 60); +} + +double Sunclock::time_of_day(time_t date) { + struct tm *t = gmtime(&date); + return (t->tm_hour + t->tm_min / 60.0 + t->tm_sec / 3600.0) / 24.0; +} + +time_t Sunclock::time_from_decimal_day(time_t date, double decimal_day) { + struct std::tm epoch; + epoch.tm_isdst = 0; + epoch.tm_sec = epoch.tm_min = epoch.tm_hour = epoch.tm_mon = 0; + epoch.tm_mday = 1; + epoch.tm_year = 70; + time_t local_tz_offset = mktime(&epoch); + + struct tm *dt = gmtime(&date); + struct tm t = {}; + t.tm_year = dt->tm_year; + t.tm_mon = dt->tm_mon; + t.tm_mday = dt->tm_mday; + double hours = 24.0 * decimal_day; + t.tm_hour = int(hours); + double minutes = (hours - t.tm_hour) * 60; + t.tm_min = int(minutes); + double seconds = (minutes - t.tm_sec) * 60; + t.tm_sec = int(seconds) % 60; + return mktime(&t) - local_tz_offset; +} + +int Sunclock::days_since_1900(struct tm *t) { + int year = t->tm_year; + if (year < 0 || year > 199) { + throw std::invalid_argument( + "days_since_1900 - Date must be between 1900 and 2099"); + } + int month = t->tm_mon + 1; + int days = t->tm_mday; + + if (month < 3) { + month += 12; + year--; + } + int yearDays = (int)(year * 365.25); + int monthDays = (int)((month + 1) * 30.61); + + return (yearDays + monthDays + days - 63); +} + +double Sunclock::julian_day(struct tm *t, double const &time_of_day, + double const &tz_offset) { + return days_since_1900(t) + 2415018.5 + time_of_day - tz_offset / 24; +} + +double Sunclock::julian_century(double _julian_day) { + return (_julian_day - 2451545.0) / 36525.0; +} + +double Sunclock::mean_long_sun(double _julian_century) { + return ( + 280.46646 + + fmod(_julian_century * (36000.76983 + _julian_century * 0.0003032), 360)); +} + +double Sunclock::mean_anom_sun(double _julian_century) { + return 357.52911 + + _julian_century * (35999.05029 - 0.0001537 * _julian_century); +} + +double Sunclock::sun_eq_of_centre(double _mean_anom_sun, + double _julian_century) { + return sin(rad(_mean_anom_sun)) * + (1.914602 - + _julian_century * (0.004817 + 0.000014 * _julian_century)) + + sin(rad(2 * _mean_anom_sun)) * + (0.019993 - 0.000101 * _julian_century) + + sin(rad(3 * _mean_anom_sun)) * 0.000289; +} + +double Sunclock::sun_true_long(double _mean_long_sun, + double _sun_eq_of_centre) { + return _mean_long_sun + _sun_eq_of_centre; +} + +double Sunclock::eccent_earth_orbit(double _julian_century) { + return 0.016708634 - + _julian_century * (0.000042037 + 0.0001537 * _julian_century); +} + +double Sunclock::var_y(double _obliq_corr) { + return tan(rad(_obliq_corr / 2)) * tan(rad(_obliq_corr / 2)); +} + +double Sunclock::mean_obliq_ecliptic(double _julian_century) { + return (23 + (26 + ((21.448 - _julian_century * + (46.815 + _julian_century * + (0.00059 - _julian_century * + 0.001813)))) / + 60) / + 60); +} + +double Sunclock::obliq_corr(double _mean_obliq_ecliptic, + double _julian_century) { + return _mean_obliq_ecliptic + + 0.00256 * cos(deg(125.04 - 1934.136 * _julian_century)); +} + +double Sunclock::sun_app_long(double _sun_true_long, double _julian_century) { + return (_sun_true_long - 0.00569 - + 0.00478 * sin(deg(125.04 - 1934.136 * _julian_century))); +} + +double Sunclock::declination(double _obliq_corr, double _sun_app_long) { + return deg(asin(sin(rad(_obliq_corr)) * sin(rad(_sun_app_long)))); +} + +double Sunclock::eq_of_time(double _var_y, double _mean_long_sun, + double _eccent_earth_orbit, double _mean_anom_sun) { + return 4 * deg(_var_y * sin(2 * rad(_mean_long_sun)) - + 2 * _eccent_earth_orbit * sin(rad(_mean_anom_sun)) + + 4 * _eccent_earth_orbit * _var_y * sin(rad(_mean_anom_sun)) * + cos(2 * rad(_mean_long_sun)) - + 0.5 * _var_y * _var_y * sin(4 * rad(_mean_long_sun)) - + 1.25 * _eccent_earth_orbit * _eccent_earth_orbit * + sin(2 * rad(_mean_anom_sun))); +} + +double Sunclock::true_solar_time(double _time_of_day, double _eq_of_time) { + return fmod( + (_time_of_day * 1440 + _eq_of_time + 4 * longitude - 60 * tz_offset), + 1440); +} + +double Sunclock::hour_angle(double _true_solar_time) { + return (_true_solar_time / 4 < 0 ? _true_solar_time / 4 + 180 + : _true_solar_time / 4 - 180); +} + +double Sunclock::hour_angle_sunrise(double _declination) { + return deg( + acos(cos(rad(90.833)) / (cos(rad(latitude)) * cos(rad(_declination))) - + tan(rad(latitude)) * tan(rad(_declination)))); +} + +double Sunclock::solar_zenith(double _declination, double _hour_angle) { + return deg(acos(sin(rad(latitude)) * sin(rad(_declination)) + + cos(rad(latitude)) * cos(rad(_declination)) * + cos(rad(_hour_angle)))); +} + +double Sunclock::solar_elevation(double _solar_zenith) { + return 90 - _solar_zenith; +} diff --git a/src/sunclock.hpp b/src/sunclock.hpp new file mode 100644 index 0000000..4cfad57 --- /dev/null +++ b/src/sunclock.hpp @@ -0,0 +1,126 @@ +#ifndef DAYLIGHT_SUNCLOCK_HPP +#define DAYLIGHT_SUNCLOCK_HPP + +#include + +class Sunclock { +public: + Sunclock(double const &latitude_, double const &longitude_, double const &tz_offset_ = 0); + + /** + * \overload double Sunclock::irradiance(time_t when) + */ + double irradiance(); + + /** + * Specifically, we calculate Direct Normal Irradiance + * That is we do not consider atmospheric components like cloud cover, + * moisture content etc. + * + * @param when + * @return a value representing irradiance (power per unit area) + */ + double irradiance(time_t when); + + /** + * \overload time_t Sunclock::sunrise(time_t date) + */ + time_t sunrise(); + + /** + * Returns sunrise time for given date + * + * @param date only date is considered + * @return sunrise time + */ + time_t sunrise(time_t date); + + /** + * \overload time_t Sunclock::solar_noon(time_t date) + */ + time_t solar_noon(); + + /** + * Returns solar_noon time for given date + * + * @param date only date is considered + * @return solar_noon time + */ + time_t solar_noon(time_t date); + + /** + * \overload time_t Sunclock::sunset(time_t date) + */ + time_t sunset(); + + /** + * Returns sunset time for given date + * + * @param date only date is considered + * @return sunset time + */ + time_t sunset(time_t date); + +private: + // in decimal degrees, east is positive + double const latitude; + + // in decimal degrees, north is positive + double const longitude; + + // in hours, east is positive, i.e. IST (+05:30) is 5.5 + double const tz_offset; + + /** + * @param date + * @return percentage past midnight, i.e. noon is 0.5 + */ + double time_of_day(time_t date); + + static int days_since_1900(struct tm *t); + + /** + * @param date + * @param decimal_day decimal between 0.0 and 1.0, e.g. noon = 0.5 + * @return time_t with date and time set accordingly + */ + static time_t time_from_decimal_day(time_t date, double decimal_day); + + /** + * Get day count since Monday, January 1, 4713 BC + * https://en.wikipedia.org/wiki/Julian_day + * + * @param t + * @param time_of_day percentage past midnight, i.e. noon is 0.5 + * @param tz_offset in hours, east is positive, i.e. IST (+05:30) is 5.5 + * @return julian day + */ + static double julian_day(struct tm *t, double const &time_of_day, double const &tz_offset); + + /** + * Extract information in a suitable format from a time_t object. + */ + void prepare_time(time_t const &when, struct tm *ptm); + + double julian_century(double _julian_day); + double mean_long_sun(double _julian_century); + double mean_anom_sun(double _julian_century); + double sun_eq_of_centre(double _mean_anom_sun, double _julian_century); + double eccent_earth_orbit(double _julian_century); + double var_y(double _obliq_corr); + double mean_obliq_ecliptic(double _julian_century); + double sun_true_long(double _mean_long_sun, double _sun_eq_of_centre); + double obliq_corr(double _mean_obliq_ecliptic, double _julian_century); + double sun_app_long(double _sun_true_long, double _julian_century); + double declination(double _obliq_corr, double _sun_app_long); + double eq_of_time(double _var_y, double _mean_long_sun, double _eccent_earth_orbit, + double _mean_anom_sun); + double true_solar_time(double _time_of_day, double _eq_of_time); + double hour_angle(double _true_solar_time); + double hour_angle_sunrise(double _declination); + double solar_zenith(double _declination, double _hour_angle); + double solar_elevation(double _solar_zenith); +}; + +#endif + diff --git a/src/webenginepage.cpp b/src/webenginepage.cpp index 6cd5f44..e27d928 100644 --- a/src/webenginepage.cpp +++ b/src/webenginepage.cpp @@ -116,7 +116,7 @@ void WebEnginePage::handleFeaturePermissionRequested(const QUrl &securityOrigin, } void WebEnginePage::handleLoadFinished(bool ok) { - Q_UNUSED(ok); + // turn on Notification settings by default if (settings.value("permissions/Notifications").isValid() == false) { settings.beginGroup("permissions"); @@ -132,6 +132,14 @@ void WebEnginePage::handleLoadFinished(bool ok) { QWebEnginePage::Feature::Notifications, QWebEnginePage::PermissionPolicy::PermissionGrantedByUser); } + + if(ok){ + injectMutationObserver(); + injectPreventScrollWheelZoomHelper(); + injectFullWidthJavaScript(); + injectClassChangeObserver(); + injectNewChatJavaScript(); + } } void WebEnginePage::fullScreenRequestedByPage( @@ -301,3 +309,106 @@ void WebEnginePage::javaScriptConsoleMessage( Q_UNUSED(lineId); Q_UNUSED(sourceId); } + +void WebEnginePage::injectPreventScrollWheelZoomHelper() { + QString js = R"( + (function () { + const SSWZ = function () { + this.keyScrollHandler = function (e) { + if (e.ctrlKey) { + e.preventDefault(); + return false; + } + } + }; + if (window === top) { + const sswz = new SSWZ(); + window.addEventListener('wheel', sswz.keyScrollHandler, { + passive: false + }); + } + })(); + )"; + this->runJavaScript(js); +} + +void WebEnginePage::injectClassChangeObserver() { + QString js = R"( + const observer = new MutationObserver(() => { + var haveFullView = document.body.classList.contains('whatsie-full-view'); + var container = document.querySelector('#app > .app-wrapper-web > div'); + if(container){ + if(haveFullView){ + container.style.width = '100%'; + container.style.height = '100%'; + container.style.top = '0'; + container.style.maxWidth = 'unset'; + }else{ + container.style.width = null; + container.style.height = null; + container.style.top = null; + container.style.maxWidth = null; + } + } + }); + observer.observe(document.body, { + attributes: true, + attributeFilter: ['class'], + childList: false, + characterData: false + });)"; + this->runJavaScript(js); +} + +void WebEnginePage::injectMutationObserver() { + QString js = + R"(function waitForElement(selector) { + return new Promise(resolve => { + if (document.querySelector(selector)) { + return resolve(document.querySelector(selector)); + } + const observer = new MutationObserver(mutations => { + if (document.querySelector(selector)) { + resolve(document.querySelector(selector)); + observer.disconnect(); + } + }); + observer.observe(document.body, { + childList: true, + subtree: true + }); + }); + };)"; + this->runJavaScript(js); +} + +void WebEnginePage::injectFullWidthJavaScript() { + if (!settings.value("fullWidthView", true).toBool()) + return; + QString js = + R"(waitForElement('#pane-side').then( () => { + var container = document.querySelector('#app > .app-wrapper-web > div'); + container.style.width = '100%'; + container.style.height = '100%'; + container.style.top = '0'; + container.style.maxWidth = 'unset'; + }); + )"; + this->runJavaScript(js); +} + +void WebEnginePage::injectNewChatJavaScript() { + QString js = R"(const openNewChatWhatsie = (phone,text) => { + const link = document.createElement('a'); + link.setAttribute('href', + `whatsapp://send/?phone=${phone}&text=${text}`); + document.body.append(link); + link.click(); + document.body.removeChild(link); + }; + function openNewChatWhatsieDefined() + { + return (openNewChatWhatsie != 'undefined'); + })"; + this->runJavaScript(js); +} diff --git a/src/webenginepage.h b/src/webenginepage.h index e2684f3..d039288 100644 --- a/src/webenginepage.h +++ b/src/webenginepage.h @@ -61,6 +61,11 @@ private slots: QWebEngineClientCertificateSelection clientCertSelection); #endif void fullScreenRequestedByPage(QWebEngineFullScreenRequest request); + void injectPreventScrollWheelZoomHelper(); + void injectClassChangeObserver(); + void injectMutationObserver(); + void injectFullWidthJavaScript(); + void injectNewChatJavaScript(); }; #endif // WEBENGINEPAGE_H diff --git a/src/widgets/elidedlabel/elidedlabel.cpp b/src/widgets/elidedlabel/elidedlabel.cpp new file mode 100644 index 0000000..e870959 --- /dev/null +++ b/src/widgets/elidedlabel/elidedlabel.cpp @@ -0,0 +1,52 @@ +#include "elidedlabel.h" + +#include +#include +#include +#include + +ElidedLabel::ElidedLabel(QWidget *parent) + : QLabel(parent), m_elide_mode(Qt::ElideRight) { + setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); +} + +ElidedLabel::ElidedLabel(const QString &txt, QWidget *parent) + : QLabel(txt, parent), m_elide_mode(Qt::ElideRight) { + setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); +} + +ElidedLabel::ElidedLabel(const QString &txt, Qt::TextElideMode elideMode, + QWidget *parent) + : QLabel(txt, parent), m_elide_mode(elideMode) { + setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); +} + +void ElidedLabel::setText(const QString &txt) { + QLabel::setText(txt); + cacheElidedText(geometry().width()); + setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); +} + +void ElidedLabel::cacheElidedText(int w) { + m_cached_elided_text = fontMetrics().elidedText( + text(), m_elide_mode, w, (buddy() == nullptr) ? 0 : Qt::TextShowMnemonic); +} + +void ElidedLabel::resizeEvent(QResizeEvent *e) { + QLabel::resizeEvent(e); + cacheElidedText(e->size().width()); +} + +void ElidedLabel::paintEvent(QPaintEvent *e) { + if (m_elide_mode == Qt::ElideNone) { + QLabel::paintEvent(e); + } else { + QPainter p(this); + p.drawText(0, 0, geometry().width(), geometry().height(), + QStyle::visualAlignment(text().isRightToLeft() ? Qt::RightToLeft + : Qt::LeftToRight, + alignment()) | + ((buddy() == nullptr) ? 0 : Qt::TextShowMnemonic), + m_cached_elided_text); + } +} diff --git a/src/widgets/elidedlabel/elidedlabel.h b/src/widgets/elidedlabel/elidedlabel.h new file mode 100644 index 0000000..7dc610a --- /dev/null +++ b/src/widgets/elidedlabel/elidedlabel.h @@ -0,0 +1,49 @@ +#pragma once +#include +#include +#include +#include +#include + +// A label that elides its text when not enough geometry is available to show all of the text. +// Currently only capable of one-line. +class ElidedLabel : public QLabel { + Q_OBJECT + +private: + Qt::TextElideMode m_elide_mode; + QString m_cached_elided_text; + + +public: + ElidedLabel(QWidget* parent = NULL); + ElidedLabel(const QString& txt, QWidget* parent = NULL); + ElidedLabel(const QString& txt, Qt::TextElideMode elideMode = Qt::ElideRight, QWidget* parent = NULL); + +public: + // Set the elide mode used for displaying text. + inline void setElideMode(Qt::TextElideMode elideMode) { + m_elide_mode = elideMode; + updateGeometry(); + } + + // Get the elide mode currently used to display text. + inline Qt::TextElideMode elideMode() const { + return m_elide_mode; + } + + + +public: // QLabel overrides + void setText(const QString&); // note: not virtual so no polymorphism ... + + +protected: // QLabel overrides + virtual void paintEvent(QPaintEvent*) override; + virtual void resizeEvent(QResizeEvent*) override; + +protected: + // Cache the elided text so as to not recompute it every paint event + void cacheElidedText(int w); + +}; -- cgit v1.2.3