diff options
Diffstat (limited to 'src/mainwindow.cpp')
-rw-r--r-- | src/mainwindow.cpp | 186 |
1 files changed, 98 insertions, 88 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1e08f0f..7c75489 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -32,106 +32,138 @@ MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidget *parent) : QWidget(parent), m_ScreenRect(screenRect), - m_Primary(primary), m_LoginForm(nullptr), m_messages(nullptr), m_Clock(nullptr), - m_Snake(nullptr) + m_Snake(nullptr), + m_Banner(nullptr), + m_News(nullptr) { setObjectName(QString("MainWindow_%1").arg(screen)); - setGeometry(screenRect); - - setBackground(); - // TODO: Check if testMode == false and greeter == NULL, if so display big error message // instead of exiting/crashing // display login dialog only in the main screen + if (primary) { + m_LoginForm = new LoginForm(this); + // This hack ensures that the primary screen will have focus + // if there are more screens (move the mouse cursor in the center + // of primary screen - not in the center of all X area). It + // won't affect single-screen environments. + int centerX = screenRect.width()/2 + screenRect.x(); + int centerY = screenRect.height()/2 + screenRect.y(); + QCursor::setPos(centerX, centerY); + } + // Banner + if (!Settings::bannerImageFile().isEmpty()) { + qWarning() << "Have banner " << Settings::bannerImageFile(); + m_Banner = new QSvgWidget(Settings::bannerImageFile(), this); + } + + createLogWindow(); + createClock(); + createNewsWindow(); + + setGeometry(screenRect); +} + +void MainWindow::resizeEvent(QResizeEvent *event) +{ + if (m_Snake != nullptr) { + delete m_Snake; + m_Snake = nullptr; + } + + QWidget::resizeEvent(event); + m_ScreenRect = QRect(this->pos(), event->size()); + setBackground(); /* * Everything is layed out manually here, since I don't know how to represent the size constraints * and interactions of everything using layout classes. You're welcome to improve this, but I double * dare you to not break any combination of having/not having certain logos or elements displayed. */ - int newsY = 100; - int newsX = screenRect.width() / 2; - int newsBottom = screenRect.height(); + int newsX = m_ScreenRect.width() / 2; + int newsBottom = m_ScreenRect.height(); - int spaceY = screenRect.height() / 2; - - if (showLoginForm()) { - m_LoginForm = new LoginForm(this); + int spaceY = m_ScreenRect.height() / 2; + + if (m_LoginForm != nullptr) { spaceY -= m_LoginForm->height() / 2; - int maxX = screenRect.width() - m_LoginForm->width(); - int maxY = screenRect.height() - m_LoginForm->height(); + int maxX = m_ScreenRect.width() - m_LoginForm->width(); + int maxY = m_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); - + m_LoginForm->move(offsetX, offsetY); m_LoginForm->show(); - - // This hack ensures that the primary screen will have focus - // if there are more screens (move the mouse cursor in the center - // of primary screen - not in the center of all X area). It - // won't affect single-screen environments. - int centerX = screenRect.width()/2 + screenRect.x(); - int centerY = screenRect.height()/2 + screenRect.y(); - QCursor::setPos(centerX, centerY); newsX = m_LoginForm->geometry().right() + 5; } // Banner - if (!Settings::bannerImageFile().isEmpty()) { - qWarning() << "Have banner " << Settings::bannerImageFile(); - QSvgWidget *banner = new QSvgWidget(Settings::bannerImageFile(), this); - qWarning() << banner->sizeHint(); - if (banner->renderer()->isValid()) { + if (m_Banner != nullptr) { + qWarning() << m_Banner->sizeHint(); + if (m_Banner->renderer()->isValid()) { QSize sh; int offs = 0; do { offs += 20; - sh = banner->sizeHint().scaled(screenRect.width() - offs, spaceY - offs - 50, Qt::KeepAspectRatio); - } while (offs < 200 && sh.width() > screenRect.width() / 2 && sh.height() > spaceY / 2); + sh = m_Banner->sizeHint().scaled(m_ScreenRect.width() - offs, spaceY - offs - 50, Qt::KeepAspectRatio); + } while (offs < 200 && sh.width() > m_ScreenRect.width() / 2 && sh.height() > spaceY / 2); int yoff = (spaceY - sh.height()); if (yoff < 100) { yoff = 100; } - banner->setGeometry((screenRect.width() - sh.width()) / 2, yoff / 2, sh.width(), sh.height()); - newsY = banner->geometry().bottom() + 10; + m_Banner->setGeometry((m_ScreenRect.width() - sh.width()) / 2, yoff / 2, sh.width(), sh.height()); + newsY = m_Banner->geometry().bottom() + 10; } } + for (QWidget *w : m_DecoItems) { + w->setParent(nullptr); + w->deleteLater(); + } + // Distro and custom icons left/right bottom + m_DecoItems.clear(); int ls = (spaceY > 500 ? 500 : spaceY); - if (ls > screenRect.height() / 5) ls = screenRect.height() / 5; - if (ls > screenRect.width() / 5) ls = screenRect.width() / 5; - QRect logoRect(QPoint(0, screenRect.height() / 3), QSize(ls, screenRect.height() * 2 / 3)); + if (ls > m_ScreenRect.height() / 5) ls = m_ScreenRect.height() / 5; + if (ls > m_ScreenRect.width() / 5) ls = m_ScreenRect.width() / 5; + QRect logoRect(QPoint(0, m_ScreenRect.height() / 3), QSize(ls, m_ScreenRect.height() * 2 / 3)); QSize logoSize = createLogo(logoRect); - QRect distroRect(QPoint(screenRect.width() - ls, screenRect.height() - ls), QSize(ls, ls)); + QRect distroRect(QPoint(m_ScreenRect.width() - ls, m_ScreenRect.height() - ls), QSize(ls, ls)); QSize distroSize = createDistro(distroRect); if (distroSize.height() > 0) { newsBottom -= distroSize.height() - 10; } - if (showLoginForm()) { - // Log window - QRect lwSize(QPoint(logoSize.width(), screenRect.height() * 3/4), QPoint(screenRect.width() - distroSize.width(), screenRect.height())); + // Log window + if (m_messages != nullptr) { + QRect lwSize(QPoint(logoSize.width(), m_ScreenRect.height() * 3/4), QPoint(m_ScreenRect.width() - distroSize.width(), m_ScreenRect.height())); lwSize.adjust(10, 10, -10, -10); - if (createLogWindow(lwSize)) { - newsBottom = lwSize.top(); - } + m_messages->setGeometry(lwSize); + int ps = lwSize.height() / 20; + if (ps > 20) ps = 20; + m_messages->setFontPointSize(ps); + newsBottom = lwSize.top(); + } + if (m_Clock != nullptr) { + m_Clock->setFixedWidth(m_ScreenRect.width()); + m_Clock->parentWidget()->setFixedWidth(m_ScreenRect.width()); } - createClock(); // News widget - QRect newsSize(QPoint(newsX, newsY), QPoint(screenRect.width() - 10, newsBottom - 10)); - createNewsWindow(newsSize); -} - -MainWindow::~MainWindow() -{ + if (m_News != nullptr) { + QRect newsSize(QPoint(newsX, newsY), QPoint(m_ScreenRect.width() - 10, newsBottom - 10)); + if (newsSize.width() < 200 || newsSize.height() < 80) { + m_News->hide(); + } else { + m_News->setGeometry(newsSize); + m_News->show(); + } + } } void MainWindow::paintEvent(QPaintEvent *event) @@ -194,14 +226,19 @@ void MainWindow::createNextLogo(QRect &max, QSize &retval, const QString &path) if (max.height() <= 0) return; QSvgWidget *img = new QSvgWidget(path, this); - if (!img->renderer()->isValid()) + if (!img->renderer()->isValid()) { + img->deleteLater(); return; + } QSize virtualSize = img->sizeHint().scaled(max.width(), max.height(), Qt::KeepAspectRatio); QSize realSize = img->sizeHint().scaled(max.width() - 20, max.height() - 20, Qt::KeepAspectRatio); - if (virtualSize.width() == 0 || virtualSize.height() == 0) + if (virtualSize.width() == 0 || virtualSize.height() == 0) { + img->deleteLater(); return; + } QRect c(max.left() + 10, max.bottom() - realSize.height() - 10, realSize.width(), realSize.height()); img->setGeometry(c); + m_DecoItems.append(img); max.setHeight(max.height() - virtualSize.height()); retval.setWidth(qMax(retval.width(), virtualSize.width())); retval.setHeight(retval.height() + virtualSize.height()); @@ -222,13 +259,14 @@ QSize MainWindow::createDistro(const QRect &max) } QPixmap pixmap(QPixmap::fromImage(img)); QLabel *lbl = new QLabel(this); + m_DecoItems.append(lbl); lbl->setPixmap(pixmap); QRect c(max.right() - realSize.width() - 10, max.bottom() - realSize.height() - 10, realSize.width(), realSize.height()); lbl->setGeometry(c); return virtualSize; } -bool MainWindow::createLogWindow(const QRect& geom) +bool MainWindow::createLogWindow() { QString path = Settings::logMessageFile(); if (path.isEmpty()) @@ -237,10 +275,6 @@ bool MainWindow::createLogWindow(const QRect& geom) if (f.size() == 0 || !f.open(QFile::ReadOnly)) return false; m_messages = new QTextEdit(this); - m_messages->setGeometry(geom); - int ps = geom.height() / 20; - if (ps > 20) ps = 20; - m_messages->setFontPointSize(ps); QTextStream stream(&f); const QColor black(Qt::black); while (!stream.atEnd()) { @@ -265,21 +299,18 @@ bool MainWindow::createLogWindow(const QRect& geom) return true; } -void MainWindow::createNewsWindow(const QRect &size) +void MainWindow::createNewsWindow() { - if (size.width() < 100 || size.height() < 100) - return; QString path = Settings::newsHtmlFile(); if (path.isEmpty()) return; QFile f(path); if (f.size() == 0 || !f.open(QFile::ReadOnly)) return; - QTextEdit *news = new QTextEdit(this); - news->setReadOnly(true); - news->setStyleSheet("border:none; background:rgba(255,255,255,.33); border-radius:5px"); - news->setHtml(QString::fromUtf8(f.readAll())); - news->setGeometry(size); + m_News = new QTextEdit(this); + m_News->setReadOnly(true); + m_News->setStyleSheet("border:none; background:rgba(255,255,255,.33); border-radius:5px"); + m_News->setHtml(QString::fromUtf8(f.readAll())); } void MainWindow::createClock() @@ -329,7 +360,7 @@ void MainWindow::createClock() bool MainWindow::showLoginForm() { - return m_Primary; + return m_LoginForm != nullptr; } void MainWindow::setFocus(Qt::FocusReason reason) @@ -368,6 +399,7 @@ int MainWindow::getOffset(QString settingsOffset, int maxVal, int defaultVal) void MainWindow::setBackground() { + m_background = QImage(); Qt::AspectRatioMode arMode = Qt::KeepAspectRatioByExpanding; QString bgPath = Settings::backgrundImageFile(); if (bgPath.length() != 0) { @@ -407,28 +439,6 @@ void MainWindow::setBackground() this->setPalette(palette); } -void MainWindow::showStandby() -{ - if (m_LoginForm != nullptr) { - m_LoginForm->hide(); - } - QSvgWidget *img = new QSvgWidget(":/resources/gnome-face-tired.svg", this); - if (img->renderer()->isValid()) { - QSize sh = img->sizeHint().scaled(this->width() / 2, this->height() / 2, Qt::KeepAspectRatio); - img->setGeometry((this->width() - sh.width()) / 2, (this->height() - sh.height()) / 2, sh.width(), sh.height()); - img->show(); - } else { - qWarning() << "Shice!"; - } - QTimer::singleShot(4000, [this, img]() { - img->hide(); - img->deleteLater(); - if (this->m_LoginForm != nullptr) { - m_LoginForm->show(); - } - }); -} - void MainWindow::updateClock() { int next = drawClock(); |