New rotatable label widget
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "RotatableLabel.hpp"
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
#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<float>(angle) / 90;
|
||||
angleMultiplier = static_cast<float>(angleMultiplier) - std::floor(angleMultiplier);
|
||||
angle = static_cast<int>(90 * angleMultiplier);
|
||||
}
|
||||
|
||||
auto angleRadians = angle * (M_PI / 180);
|
||||
|
||||
size.setWidth(static_cast<int>(
|
||||
std::cos(angleRadians) * textSize.width()
|
||||
+ std::ceil(std::sin(angleRadians) * textSize.height())
|
||||
))
|
||||
;
|
||||
size.setHeight(static_cast<int>(
|
||||
std::sin(angleRadians) * textSize.width()
|
||||
+ std::ceil(std::cos(angleRadians) * textSize.height())
|
||||
));
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <QLabel>
|
||||
#include <QSize>
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user