diff options
| author | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-12-07 21:57:21 -0500 |
|---|---|---|
| committer | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-12-07 21:57:21 -0500 |
| commit | 940a2361da8f51ab2547f1b7bfd42dc1c8645642 (patch) | |
| tree | 5916cfed50ae675ae10275c1134fc522ee59bae3 /src/utils/aspectratiowidget/aspectratiowidget.hpp | |
| parent | caa765bff49d54217b75aaf0e7acf4e5392a11e4 (diff) | |
added a GUI
Diffstat (limited to 'src/utils/aspectratiowidget/aspectratiowidget.hpp')
| -rw-r--r-- | src/utils/aspectratiowidget/aspectratiowidget.hpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/utils/aspectratiowidget/aspectratiowidget.hpp b/src/utils/aspectratiowidget/aspectratiowidget.hpp new file mode 100644 index 0000000..9a5f81c --- /dev/null +++ b/src/utils/aspectratiowidget/aspectratiowidget.hpp @@ -0,0 +1,66 @@ +#ifndef ASPECTRATIOWIDGET_HPP +#define ASPECTRATIOWIDGET_HPP + +#include <QWidget> +#include <QBoxLayout> + +class AspectRatioWidget : public QWidget +{ + Q_OBJECT +public: + AspectRatioWidget(QWidget *parent = 0) : QWidget(parent) + { + m_layout = new QHBoxLayout(); + m_layout->setSpacing(0); + m_layout->setContentsMargins(0, 0, 0, 0); + setLayout(m_layout); + } + + // the widget we want to keep the ratio + void setAspectWidget(QWidget* widget, const double ratio = 1.0) { + m_aspect_widget = widget; + m_layout->addWidget(widget); + m_ratio = ratio; + } + void setRatio(const double ratio) { + m_ratio = ratio; + applyAspectRatio(); + } + +protected: + void resizeEvent(QResizeEvent *event) { + (void)event; + applyAspectRatio(); + } + +public slots: + void applyAspectRatio() { + int w = this->width(); + int h = this->height(); + double aspect = static_cast<double>(h)/static_cast<double>(w); + + if(aspect < m_ratio) // parent is too wide + { + int target_width = static_cast<int>(static_cast<double>(h)/m_ratio); + m_aspect_widget->setMaximumWidth(target_width); + m_aspect_widget->setMaximumHeight(h); + + } + else // parent is too high + { + int target_heigth = static_cast<int>(static_cast<double>(w)*m_ratio); + m_aspect_widget->setMaximumHeight(target_heigth); + m_aspect_widget->setMaximumWidth(w); + } + } + +private: + QHBoxLayout* m_layout; + + QWidget* m_aspect_widget; + + double m_ratio; +}; + +#endif // ASPECTRATIOWINDOW_H + |
