diff --git a/CMakeLists.txt b/CMakeLists.txt index cfc29b2b..0b6b6abf 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ add_executable(Bloom src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp src/Insight/UserInterfaces/InsightWindow/AboutWindow.cpp + src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp # Target package widgets diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp new file mode 100644 index 00000000..4bf81060 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp @@ -0,0 +1,63 @@ +#include "RotatableLabel.hpp" + +#include + +#include "src/Logger/Logger.hpp" + +using namespace Bloom::Widgets; + +void RotatableLabel::paintEvent(QPaintEvent* event) { + auto painter = QPainter(this); + static auto containerSize = this->getContainerSize(); + static auto textSize = QLabel::minimumSizeHint(); + static auto margins = this->contentsMargins(); + painter.setClipRect(0, 0, containerSize.width(), containerSize.height()); + painter.save(); + painter.setPen(Qt::PenStyle::SolidLine); + painter.setPen(QColor("#afb1b3")); + painter.translate(std::ceil(containerSize.width() / 2), std::ceil(containerSize.height() / 2)); + painter.rotate(this->angle); + painter.drawText( + -(textSize.width() / 2) + margins.left(), + (textSize.height() / 2) + margins.top(), + this->text() + ); + + painter.restore(); +} + +QSize RotatableLabel::getContainerSize() const { + auto size = QSize(); + auto textSize = QLabel::sizeHint(); + + if (this->angle % 360 == 0 || this->angle % 180 == 0) { + size = textSize; + + } else if (this->angle % 90 == 0) { + size.setHeight(textSize.width()); + size.setWidth(textSize.height()); + + } else { + auto angle = this->angle; + + if (angle > 90) { + float angleMultiplier = static_cast(angle) / 90; + angleMultiplier = static_cast(angleMultiplier) - std::floor(angleMultiplier); + angle = static_cast(90 * angleMultiplier); + } + + auto angleRadians = angle * (M_PI / 180); + + size.setWidth(static_cast( + std::cos(angleRadians) * textSize.width() + + std::ceil(std::sin(angleRadians) * textSize.height()) + )) + ; + size.setHeight(static_cast( + std::sin(angleRadians) * textSize.width() + + std::ceil(std::cos(angleRadians) * textSize.height()) + )); + } + + return size; +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.hpp new file mode 100644 index 00000000..47db9f6f --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include + +namespace Bloom::Widgets +{ + class RotatableLabel: public QLabel + { + Q_OBJECT + private: + int angle = 90; + + [[nodiscard]] QSize getContainerSize() const; + + protected: + void paintEvent(QPaintEvent* event) override; + + [[nodiscard]] QSize sizeHint() const override { + return this->getContainerSize(); + }; + + [[nodiscard]] QSize minimumSizeHint() const override { + return this->getContainerSize(); + }; + + public: + RotatableLabel(const QString& text, QWidget* parent): QLabel(text, parent) {}; + RotatableLabel(int angleDegrees, const QString& text, QWidget* parent): QLabel(text, parent) { + this->setAngle(angleDegrees); + }; + + void setAngle(int angleDegrees) { + this->angle = angleDegrees; + } + }; +}