Qt: Simplify window background drawing
Vicki Pfau vi@endrift.com
Sat, 10 Mar 2018 17:24:54 -0800
2 files changed,
30 insertions(+),
18 deletions(-)
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -963,6 +963,7 @@ });
} m_stateWindow->setAttribute(Qt::WA_DeleteOnClose); m_stateWindow->setMode(ls); + updateFrame(); attachWidget(m_stateWindow); }@@ -1759,6 +1760,16 @@ m_controller->setPaused(true);
} } +void Window::updateFrame() { + QSize size = m_controller->screenDimensions(); + QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), size.width(), size.height(), + size.width() * BYTES_PER_PIXEL, QImage::Format_RGBX8888); + QPixmap pixmap; + pixmap.convertFromImage(currentImage); + m_screenWidget->setPixmap(pixmap); + emit paused(true); +} + void Window::setController(CoreController* controller, const QString& fname) { if (!controller) { return;@@ -1795,15 +1806,8 @@ m_controller.reset();
}); } connect(m_controller.get(), &CoreController::stopping, &m_inputController, &InputController::resumeScreensaver); - connect(m_controller.get(), &CoreController::paused, [this]() { - QSize size = m_controller->screenDimensions(); - QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), size.width(), size.height(), - size.width() * BYTES_PER_PIXEL, QImage::Format_RGBX8888); - QPixmap pixmap; - pixmap.convertFromImage(currentImage); - m_screenWidget->setPixmap(pixmap); - emit paused(true); - }); + connect(m_controller.get(), &CoreController::paused, this, &Window::updateFrame); + #ifndef Q_OS_MAC connect(m_controller.get(), &CoreController::paused, menuBar(), &QWidget::show); connect(m_controller.get(), &CoreController::unpaused, [this]() {@@ -1874,11 +1878,15 @@ m_controller->start();
} WindowBackground::WindowBackground(QWidget* parent) - : QLabel(parent) + : QWidget(parent) { setLayout(new QStackedLayout()); layout()->setContentsMargins(0, 0, 0, 0); - setAlignment(Qt::AlignCenter); +} + +void WindowBackground::setPixmap(const QPixmap& pmap) { + m_pixmap = pmap; + update(); } void WindowBackground::setSizeHint(const QSize& hint) {@@ -1902,11 +1910,9 @@ void WindowBackground::setLockAspectRatio(bool lock) {
m_lockAspectRatio = lock; } -void WindowBackground::paintEvent(QPaintEvent*) { - const QPixmap* logo = pixmap(); - if (!logo) { - return; - } +void WindowBackground::paintEvent(QPaintEvent* event) { + QWidget::paintEvent(event); + const QPixmap& logo = pixmap(); QPainter painter(this); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.fillRect(QRect(QPoint(), size()), Qt::black);@@ -1925,5 +1931,5 @@ ds.setHeight(ds.height() - ds.height() % m_aspectHeight);
} QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2); QRect full(origin, ds); - painter.drawPixmap(full, *logo); + painter.drawPixmap(full, logo); }
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -134,6 +134,8 @@ void recordFrame();
void showFPS(); void focusCheck(); + void updateFrame(); + private: static const int FPS_TIMER_INTERVAL = 2000; static const int FRAME_LIST_SIZE = 120;@@ -222,22 +224,26 @@ LibraryController* m_libraryView;
#endif }; -class WindowBackground : public QLabel { +class WindowBackground : public QWidget { Q_OBJECT public: WindowBackground(QWidget* parent = 0); + void setPixmap(const QPixmap& pixmap); void setSizeHint(const QSize& size); virtual QSize sizeHint() const override; void setDimensions(int width, int height); void setLockIntegerScaling(bool lock); void setLockAspectRatio(bool lock); + const QPixmap& pixmap() const { return m_pixmap; } + protected: virtual void paintEvent(QPaintEvent*) override; private: + QPixmap m_pixmap; QSize m_sizeHint; int m_aspectWidth; int m_aspectHeight;