From 05fbb7833951df9176bcadb3da809ff41f2e7d56 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 10 Apr 2018 23:52:11 +0200 Subject: Add clock, refactor settings class to be static --- src/global.cpp | 2 +- src/main.cpp | 10 ++++--- src/mainwindow.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++--------- src/mainwindow.h | 6 ++++ src/settings.h | 31 +++++++++++--------- 5 files changed, 102 insertions(+), 33 deletions(-) diff --git a/src/global.cpp b/src/global.cpp index 1a3cf80..fd6e321 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -55,7 +55,7 @@ bool Global::startSession() QImage Global::getConfigGradient() { QImage img; - const QStringList cols = Settings().gradientColors(); + const QStringList cols = Settings::gradientColors(); qWarning() << "Got gradient color list: " << cols; if (cols.length() == 4 || cols.length() == 2) { bool ok = true; diff --git a/src/main.cpp b/src/main.cpp index a5fa5db..7973a68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,8 @@ #include "x11util.h" #include "global.h" +Settings *s_settings = new Settings(); + static int sockets[2]; static void createSimpleBackground(); @@ -56,13 +58,13 @@ int main(int argc, char *argv[]) // to /var/log/lightdm/x-0-greeter.log, so we use std::cerr instead.. qInstallMessageHandler(messageHandler); - if (! Settings().iconThemeName().isEmpty()) { - QIcon::setThemeName(Settings().iconThemeName()); + if (!Settings::iconThemeName().isEmpty()) { + QIcon::setThemeName(Settings::iconThemeName()); } - if (!Global::testMode() && !Settings().autoLoginCheckCmd().isEmpty()) { + if (!Global::testMode() && !Settings::autoLoginCheckCmd().isEmpty()) { QProcess p; - int ret = QProcess::execute(Settings().autoLoginCheckCmd()); + int ret = QProcess::execute(Settings::autoLoginCheckCmd()); if (ret == 0) { if (Global::autoLoginGuest()) { qWarning() << "Guest login ok"; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b46a15b..9742852 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -26,14 +27,13 @@ #include "settings.h" #include "global.h" -static const Settings _settings; - MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidget *parent) : QWidget(parent), m_ScreenRect(screenRect), m_Primary(primary), m_LoginForm(NULL), - m_messages(nullptr) + m_messages(nullptr), + m_Clock(nullptr) { setObjectName(QString("MainWindow_%1").arg(screen)); @@ -56,8 +56,8 @@ MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidge int maxY = screenRect.height() - m_LoginForm->height(); int defaultX = 50*maxX/100; int defaultY = 50*maxY/100; - int offsetX = getOffset(_settings.offsetX(), maxX, defaultX); - int offsetY = getOffset(_settings.offsetY(), maxY, defaultY); + int offsetX = getOffset(Settings::offsetX(), maxX, defaultX); + int offsetY = getOffset(Settings::offsetY(), maxY, defaultY); m_LoginForm->move(offsetX, offsetY); m_LoginForm->show(); @@ -72,18 +72,22 @@ MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidge } // Banner - if (!_settings.bannerImagePath().isEmpty()) { - qWarning() << "Have banner " << _settings.bannerImagePath(); - QSvgWidget *banner = new QSvgWidget(_settings.bannerImagePath(), this); + if (!Settings::bannerImagePath().isEmpty()) { + qWarning() << "Have banner " << Settings::bannerImagePath(); + QSvgWidget *banner = new QSvgWidget(Settings::bannerImagePath(), this); qWarning() << banner->sizeHint(); if (banner->renderer()->isValid()) { QSize sh; int offs = 0; do { offs += 20; - sh = banner->sizeHint().scaled(screenRect.width() - offs, spaceY - offs, Qt::KeepAspectRatio); + sh = banner->sizeHint().scaled(screenRect.width() - offs, spaceY - offs - 50, Qt::KeepAspectRatio); } while (offs < 200 && sh.width() > screenRect.width() / 2 && sh.height() > spaceY / 2); - banner->setGeometry((screenRect.width() - sh.width()) / 2, (spaceY - sh.height()) / 2, sh.width(), sh.height()); + int yoff = (spaceY - sh.height()); + if (yoff < 100) { + yoff = 100; + } + banner->setGeometry((screenRect.width() - sh.width()) / 2, yoff / 2, sh.width(), sh.height()); } } int ls = (spaceY > 500 ? 500 : spaceY); @@ -98,6 +102,7 @@ MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidge lwSize.adjust(10, 10, -10, -10); createLogWindow(lwSize); } + createClock(); } MainWindow::~MainWindow() @@ -106,7 +111,7 @@ MainWindow::~MainWindow() QSize MainWindow::createLogo(const QRect &max) { - QString path = _settings.bottomLeftLogoPath(); + QString path = Settings::bottomLeftLogoPath(); if (path.isEmpty()) return QSize(0, 0); QSvgWidget *img = new QSvgWidget(path, this); @@ -144,7 +149,7 @@ QSize MainWindow::createDistro(const QRect &max) void MainWindow::createLogWindow(const QRect& geom) { - QString path = _settings.logMessageFile(); + QString path = Settings::logMessageFile(); if (path.isEmpty()) return; QFile f(path); @@ -178,6 +183,41 @@ void MainWindow::createLogWindow(const QRect& geom) m_messages->setStyleSheet("border:none; background:rgba(255,255,255,.33); border-radius:5px"); } +void MainWindow::createClock() +{ + QString style = Settings::clockStyle(); + if (style == "none") + return; + if (style.isEmpty()) { + style = "border:none; background:rgba(255,255,255,0); color:#888687; font-size:26pt"; + } + QStringList slist = Settings::clockShadow(); + int x = 1, y = 1, blur = 1; + QString color = "#ddd"; + if (slist.length() > 0) { + x = slist.at(0).toInt(); + } + if (slist.length() > 1) { + y = slist.at(1).toInt(); + } + if (slist.length() > 2) { + color = slist.at(2); + } + if (slist.length() > 3) { + blur = slist.at(3).toInt(); + } + m_Clock = new QLabel("Today 00:00", this); + m_Clock->setStyleSheet(style); + QGraphicsDropShadowEffect *pLabelTextShadowEffect = new QGraphicsDropShadowEffect(this); + pLabelTextShadowEffect->setColor(QColor(color)); + pLabelTextShadowEffect->setBlurRadius(blur); + pLabelTextShadowEffect->setOffset(x, y); + m_Clock->setGraphicsEffect(pLabelTextShadowEffect); + m_Clock->setAlignment(Qt::AlignRight); + m_Clock->setFixedWidth(this->width()); + updateClock(); +} + bool MainWindow::showLoginForm() { return m_Primary; @@ -220,7 +260,7 @@ int MainWindow::getOffset(QString settingsOffset, int maxVal, int defaultVal) void MainWindow::setBackground() { Qt::AspectRatioMode arMode = Qt::KeepAspectRatioByExpanding; - QString bgPath = _settings.backgrundImagePath(); + QString bgPath = Settings::backgrundImagePath(); if (bgPath.length() != 0) { m_background = QImage(bgPath); if (m_background.isNull()) { @@ -271,7 +311,7 @@ void MainWindow::showStandby() } else { qWarning() << "Shice!"; } - QTimer::singleShot(7000, [this, img]() { + QTimer::singleShot(4000, [this, img]() { img->hide(); img->deleteLater(); if (this->m_LoginForm != NULL) { @@ -279,3 +319,21 @@ void MainWindow::showStandby() } }); } + +void MainWindow::updateClock() +{ + int next = drawClock(); + if (next > 0) { + QTimer::singleShot(next, this, SLOT(updateClock())); + } +} + +int MainWindow::drawClock() +{ + if (m_Clock == nullptr) + return 0; + QDateTime time = QDateTime::currentDateTime(); + QLocale loc; + m_Clock->setText(time.toString(loc.dateFormat() + " HH:mm ")); + return (60 - time.time().second()) * 1000 + 100; +} diff --git a/src/mainwindow.h b/src/mainwindow.h index e136540..8a5bcf7 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -13,6 +13,7 @@ #include #include #include +#include #include "loginform.h" @@ -38,6 +39,8 @@ public: void createLogWindow(const QRect& geom); + void createClock(); + bool showLoginForm(); QImage& getBackground() { return m_background; } @@ -46,15 +49,18 @@ public: public slots: void showStandby(); + void updateClock(); private: int getOffset(QString offset, int maxVal, int defaultVal); void setBackground(); + int drawClock(); QRect m_ScreenRect; bool m_Primary; LoginForm* m_LoginForm; QImage m_background; QTextEdit *m_messages; + QLabel *m_Clock; }; #endif // MAINWINDOW_H diff --git a/src/settings.h b/src/settings.h index 0a03567..f55d6ce 100644 --- a/src/settings.h +++ b/src/settings.h @@ -5,23 +5,26 @@ #define CONFIG_FILE "/etc/lightdm/qt-lightdm-greeter.conf" +class Settings; +extern Settings *s_settings; + class Settings : public QSettings { public: - Settings() : QSettings(QString("/etc/lightdm/qt-lightdm-greeter.conf"), QSettings::NativeFormat) {} - QString iconThemeName() const { return value("greeter-icon-theme").toString(); } - QString backgrundImagePath() const { return value("greeter-background-image").toString(); } - QString offsetX() const { return value("loginform-offset-x").toString(); } - QString offsetY() const { return value("loginform-offset-y").toString(); } - QString miniIconPath() const { return value("loginform-mini-icon").toString(); } - QString bannerImagePath() const { return value("greeter-banner-image").toString(); } - QString bottomLeftLogoPath() const { return value("greeter-bottom-left-logo-path").toString(); } - QStringList gradientColors() const { return value("greeter-background-gradient").toString().split(QRegExp("\\s"), QString::SkipEmptyParts); } - QString logMessageFile() const { return value("greeter-message-file").toString(); } - QString autoLoginCheckCmd() const { return value("auto-login-check-cmd").toString(); } -}; - - + Settings() : QSettings(QString(CONFIG_FILE), NativeFormat) {} + static QString iconThemeName() { return s_settings->value("greeter-icon-theme").toString(); } + static QString backgrundImagePath() { return s_settings->value("greeter-background-image").toString(); } + static QString offsetX() { return s_settings->value("loginform-offset-x").toString(); } + static QString offsetY() { return s_settings->value("loginform-offset-y").toString(); } + static QString miniIconPath() { return s_settings->value("loginform-mini-icon").toString(); } + static QString bannerImagePath() { return s_settings->value("greeter-banner-image").toString(); } + static QString bottomLeftLogoPath() { return s_settings->value("greeter-bottom-left-logo-path").toString(); } + static QStringList gradientColors() { return s_settings->value("greeter-background-gradient").toString().split(QRegExp("\\s"), QString::SkipEmptyParts); } + static QString logMessageFile() { return s_settings->value("greeter-message-file").toString(); } + static QString autoLoginCheckCmd() { return s_settings->value("auto-login-check-cmd").toString(); } + static QString clockStyle() { return s_settings->value("clock-style").toString(); } + static QStringList clockShadow() { return s_settings->value("clock-shadow").toString().split(QRegExp("\\s"), QString::SkipEmptyParts); } +}; #endif // SETTINGS_H -- cgit v1.2.3-55-g7522