created viewer sizer objects
This commit is contained in:
@@ -20,5 +20,7 @@ set(OLIVE_SOURCES
|
||||
widget/viewer/viewer.cpp
|
||||
widget/viewer/viewerglwidget.h
|
||||
widget/viewer/viewerglwidget.cpp
|
||||
widget/viewer/viewersizer.h
|
||||
widget/viewer/viewersizer.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "viewersizer.h"
|
||||
|
||||
ViewerSizer::ViewerSizer(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
widget_(nullptr),
|
||||
aspect_ratio_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void ViewerSizer::SetWidget(QWidget *widget)
|
||||
{
|
||||
if (widget_ != nullptr) {
|
||||
widget_->setParent(nullptr);
|
||||
|
||||
disconnect(widget_, SIGNAL(SizeChanged(int, int)), this, SLOT(SizeChangedSlot(int, int)));
|
||||
}
|
||||
|
||||
widget_ = widget;
|
||||
|
||||
if (widget_ != nullptr) {
|
||||
widget_->setParent(this);
|
||||
|
||||
connect(widget_, SIGNAL(SizeChanged(int, int)), this, SLOT(SizeChangedSlot(int, int)));
|
||||
|
||||
UpdateSize();
|
||||
}
|
||||
}
|
||||
|
||||
void ViewerSizer::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QWidget::resizeEvent(event);
|
||||
|
||||
UpdateSize();
|
||||
}
|
||||
|
||||
void ViewerSizer::UpdateSize()
|
||||
{
|
||||
if (widget_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (qIsNull(aspect_ratio_)) {
|
||||
widget_->setVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
widget_->setVisible(true);
|
||||
|
||||
double our_aspect_ratio = static_cast<double>(width()) / static_cast<double>(height());
|
||||
|
||||
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_));
|
||||
} else {
|
||||
// This container is taller than the image, scale by width
|
||||
child_size.setHeight(qRound(child_size.width() / aspect_ratio_));
|
||||
}
|
||||
|
||||
widget_->resize(child_size);
|
||||
}
|
||||
|
||||
void ViewerSizer::SizeChangedSlot(int width, int height)
|
||||
{
|
||||
aspect_ratio_ = static_cast<double>(width) / static_cast<double>(height);
|
||||
|
||||
UpdateSize();
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 Olive Team
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
***/
|
||||
|
||||
#ifndef VIEWERSIZER_H
|
||||
#define VIEWERSIZER_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class ViewerSizer : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ViewerSizer(QWidget* parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Set the widget to be adjusted by this widget
|
||||
*
|
||||
* ViewerSizer takes ownership of this widget
|
||||
*/
|
||||
void SetWidget(QWidget* widget);
|
||||
|
||||
protected:
|
||||
virtual void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private:
|
||||
void UpdateSize();
|
||||
|
||||
QWidget* widget_;
|
||||
|
||||
double aspect_ratio_;
|
||||
|
||||
private slots:
|
||||
void SizeChangedSlot(int width, int height);
|
||||
|
||||
};
|
||||
|
||||
#endif // VIEWERSIZER_H
|
||||
Reference in New Issue
Block a user