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
+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);
}