diff --git a/parts/CMakeLists.txt b/parts/CMakeLists.txt index 602c2c0..300120f 100644 --- a/parts/CMakeLists.txt +++ b/parts/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory(exd) add_subdirectory(exl) add_subdirectory(hex) add_subdirectory(mdl) +add_subdirectory(tex) \ No newline at end of file diff --git a/parts/tex/imagelabel.cpp b/parts/tex/imagelabel.cpp new file mode 100644 index 0000000..67c8317 --- /dev/null +++ b/parts/tex/imagelabel.cpp @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: 2023 Joshua Goins +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "imagelabel.h" + +ImageLabel::ImageLabel(QWidget *parent) + : QLabel(parent) +{ + this->setMinimumSize(1, 1); + setScaledContents(false); +} + +void ImageLabel::setQPixmap(const QPixmap &p) +{ + pix = p; + QLabel::setPixmap(scaledPixmap()); +} + +int ImageLabel::heightForWidth(int width) const +{ + return pix.isNull() ? height() : (pix.height() * width) / pix.width(); +} + +QSize ImageLabel::sizeHint() const +{ + const int w = this->width(); + return {w, heightForWidth(w)}; +} + +QPixmap ImageLabel::scaledPixmap() const +{ + return pix.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); +} + +void ImageLabel::resizeEvent(QResizeEvent *e) +{ + Q_UNUSED(e); + if (!pix.isNull()) { + QLabel::setPixmap(scaledPixmap()); + } +} \ No newline at end of file diff --git a/parts/tex/imagelabel.h b/parts/tex/imagelabel.h new file mode 100644 index 0000000..88280ad --- /dev/null +++ b/parts/tex/imagelabel.h @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2023 Joshua Goins +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include + +class ImageLabel : public QLabel +{ + Q_OBJECT +public: + explicit ImageLabel(QWidget *parent = nullptr); + + [[nodiscard]] int heightForWidth(int width) const override; + [[nodiscard]] QSize sizeHint() const override; + [[nodiscard]] QPixmap scaledPixmap() const; + +public Q_SLOTS: + void setQPixmap(const QPixmap &); + void resizeEvent(QResizeEvent *) override; + +private: + QPixmap pix; +};