wrote viewer resizing container

This commit is contained in:
itsmattkc
2019-08-30 00:55:28 +10:00
parent 033287e810
commit 7c7558d8e9
3 changed files with 58 additions and 20 deletions
+9 -2
View File
@@ -24,17 +24,24 @@
#include <QResizeEvent>
#include <QVBoxLayout>
#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);
+18 -14
View File
@@ -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<double>(width) / static_cast<double>(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<double>(width()) / static_cast<double>(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<double>(width) / static_cast<double>(height);
UpdateSize();
widget_->move(child_pos);
}
+31 -4
View File
@@ -23,6 +23,15 @@
#include <QWidget>
/**
* @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