From 7c7558d8e9452b3a94cc35ad93055e6d0d9e375a Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 30 Aug 2019 00:55:28 +1000 Subject: [PATCH] wrote viewer resizing container --- app/widget/viewer/viewer.cpp | 11 ++++++++-- app/widget/viewer/viewersizer.cpp | 32 +++++++++++++++------------- app/widget/viewer/viewersizer.h | 35 +++++++++++++++++++++++++++---- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/app/widget/viewer/viewer.cpp b/app/widget/viewer/viewer.cpp index 8b86da135..1e7af8d03 100644 --- a/app/widget/viewer/viewer.cpp +++ b/app/widget/viewer/viewer.cpp @@ -24,17 +24,24 @@ #include #include +#include "viewersizer.h" + ViewerWidget::ViewerWidget(QWidget *parent) : QWidget(parent) { // Set up main layout QVBoxLayout* layout = new QVBoxLayout(this); - layout->setSpacing(0); layout->setMargin(0); // Create main OpenGL-based view + ViewerSizer* sizer = new ViewerSizer(this); + layout->addWidget(sizer); + gl_widget_ = new ViewerGLWidget(this); - layout->addWidget(gl_widget_); + sizer->SetWidget(gl_widget_); + + // FIXME: Hardcoded values + sizer->SetSize(1920, 1080); // Create time ruler ruler_ = new TimeRuler(false, this); diff --git a/app/widget/viewer/viewersizer.cpp b/app/widget/viewer/viewersizer.cpp index cb2bd1e8c..a9c3c2fc1 100644 --- a/app/widget/viewer/viewersizer.cpp +++ b/app/widget/viewer/viewersizer.cpp @@ -9,23 +9,29 @@ ViewerSizer::ViewerSizer(QWidget *parent) : void ViewerSizer::SetWidget(QWidget *widget) { - if (widget_ != nullptr) { - widget_->setParent(nullptr); - - disconnect(widget_, SIGNAL(SizeChanged(int, int)), this, SLOT(SizeChangedSlot(int, int))); - } + // Delete any previous widgets occupying this space + delete widget_; widget_ = widget; if (widget_ != nullptr) { widget_->setParent(this); - connect(widget_, SIGNAL(SizeChanged(int, int)), this, SLOT(SizeChangedSlot(int, int))); - UpdateSize(); } } +void ViewerSizer::SetChildSize(int width, int height) +{ + if (height == 0) { + aspect_ratio_ = 0; + } else { + aspect_ratio_ = static_cast(width) / static_cast(height); + } + + UpdateSize(); +} + void ViewerSizer::resizeEvent(QResizeEvent *event) { QWidget::resizeEvent(event); @@ -39,6 +45,7 @@ void ViewerSizer::UpdateSize() return; } + // If the aspect ratio is 0, the widget is always hidden if (qIsNull(aspect_ratio_)) { widget_->setVisible(false); return; @@ -48,22 +55,19 @@ void ViewerSizer::UpdateSize() double our_aspect_ratio = static_cast(width()) / static_cast(height()); + QPoint child_pos; QSize child_size = size(); if (our_aspect_ratio > aspect_ratio_) { // This container is wider than the image, scale by height child_size.setWidth(qRound(child_size.height() * aspect_ratio_)); + child_pos.setX(width() / 2 - child_size.width() / 2); } else { // This container is taller than the image, scale by width child_size.setHeight(qRound(child_size.width() / aspect_ratio_)); + child_pos.setY(height() / 2 - child_size.height() / 2); } widget_->resize(child_size); -} - -void ViewerSizer::SizeChangedSlot(int width, int height) -{ - aspect_ratio_ = static_cast(width) / static_cast(height); - - UpdateSize(); + widget_->move(child_pos); } diff --git a/app/widget/viewer/viewersizer.h b/app/widget/viewer/viewersizer.h index a5aa6e3ad..182b61164 100644 --- a/app/widget/viewer/viewersizer.h +++ b/app/widget/viewer/viewersizer.h @@ -23,6 +23,15 @@ #include +/** + * @brief A container widget that enforces the aspect ratio of a child widget + * + * Using a provided width and height, this widget calculates the aspect ratio and forces the child widget to stay + * confined to that aspect ratio and centered within the widget. + * + * The aspect ratio is calculated width divided by height. If the aspect ratio is zero (either width or height == 0), + * the widget is hidden until a valid size is provided. + */ class ViewerSizer : public QWidget { Q_OBJECT @@ -32,23 +41,41 @@ public: /** * @brief Set the widget to be adjusted by this widget * - * ViewerSizer takes ownership of this widget + * ViewerSizer takes ownership of this widget. If a widget was previously set, it is destroyed. */ void SetWidget(QWidget* widget); + /** + * @brief Set resolution to use + * + * This is not the actual resolution of the viewer, it's used to calculate the aspect ratio + */ + void SetChildSize(int width, int height); + protected: + /** + * @brief Listen for resize events to ensure the child widget remains correctly sized + */ virtual void resizeEvent(QResizeEvent *event) override; private: + /** + * @brief Main sizing function, resizes widget_ to fit aspect_ratio_ (or hides if aspect ratio is 0) + */ void UpdateSize(); + /** + * @brief Reference to widget + * + * If this is nullptr, all sizing operations are no-ops + */ QWidget* widget_; + /** + * @brief Aspect ratio calculated from the size provided by SetChildSize() + */ double aspect_ratio_; -private slots: - void SizeChangedSlot(int width, int height); - }; #endif // VIEWERSIZER_H