diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bcc09e5..88d02be2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,6 +139,7 @@ add_executable(Bloom src/Insight/UserInterfaces/InsightWindow/AboutWindow.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp + src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/ClickableWidget.cpp diff --git a/src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp b/src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp index 26b1b0a4..49933746 100644 --- a/src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/UiLoader.cpp @@ -5,6 +5,7 @@ // Custom widgets #include "Widgets/PanelWidget.hpp" #include "Widgets/RotatableLabel.hpp" +#include "Widgets/LabeledSeparator.hpp" #include "Widgets/SvgWidget.hpp" #include "Widgets/SvgToolButton.hpp" #include "Widgets/ExpandingHeightScrollAreaWidget.hpp" @@ -33,6 +34,15 @@ UiLoader::UiLoader(QObject* parent): QUiLoader(parent) { return widget; } }, + { + "LabeledSeparator", + [this] (QWidget* parent, const QString& name) { + auto* widget = new LabeledSeparator(parent); + widget->setObjectName(name); + widget->setStyleSheet(parent->styleSheet()); + return widget; + } + }, { "ExpandingHeightScrollAreaWidget", [this] (QWidget* parent, const QString& name) { diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.cpp new file mode 100644 index 00000000..e296ef29 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.cpp @@ -0,0 +1,35 @@ +#include "LabeledSeparator.hpp" + +using namespace Bloom::Widgets; + +LabeledSeparator::LabeledSeparator(QString title, QWidget* parent): title(std::move(title)), QWidget(parent) { + this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); + this->setFixedHeight(LabeledSeparator::DEFAULT_HEIGHT); +} + +void LabeledSeparator::paintEvent(QPaintEvent* event) { + auto painter = QPainter(this); + this->drawWidget(painter); +} + +void LabeledSeparator::drawWidget(QPainter& painter) { + const auto fontMetrics = painter.fontMetrics(); + const auto titleSize = fontMetrics.size(Qt::TextFlag::TextSingleLine, this->title); + const auto titleRect = QRect( + QPoint(this->marginLeft, (this->height() - titleSize.height()) / 2), + titleSize + ); + + const auto lineYPosition = titleRect.y() + (titleRect.height() / 2); + const auto line = QLine( + titleRect.right() + 8, + lineYPosition, + this->width() - this->marginRight, + lineYPosition + ); + + painter.drawText(titleRect, Qt::AlignCenter, this->title); + + painter.setPen(this->lineColor); + painter.drawLine(line); +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.hpp new file mode 100644 index 00000000..086540ff --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/LabeledSeparator.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace Bloom::Widgets +{ + class LabeledSeparator: public QWidget + { + Q_OBJECT + Q_PROPERTY(QString title READ getTitle WRITE setTitle DESIGNABLE true) + Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true) + Q_PROPERTY(int marginLeft READ getMarginLeft WRITE setMarginLeft DESIGNABLE true) + Q_PROPERTY(int marginRight READ getMarginRight WRITE setMarginRight DESIGNABLE true) + + public: + explicit LabeledSeparator(QString title = "", QWidget* parent = nullptr); + explicit LabeledSeparator(QWidget* parent = nullptr): LabeledSeparator("", parent) {}; + + [[nodiscard]] QString getTitle() const { + return this->title; + } + + void setTitle(const QString& title) { + this->title = title; + } + + [[nodiscard]] QColor getLineColor() const { + return this->lineColor; + } + + void setLineColor(QColor lineColor) { + this->lineColor = lineColor; + } + + [[nodiscard]] int getMarginLeft() const { + return this->marginLeft; + } + + void setMarginLeft(int marginLeft) { + this->marginLeft = marginLeft; + } + + [[nodiscard]] int getMarginRight() const { + return this->marginRight; + } + + void setMarginRight(int marginRight) { + this->marginRight = marginRight; + } + + protected: + void paintEvent(QPaintEvent* event) override; + void drawWidget(QPainter& painter); + + private: + static constexpr int DEFAULT_HEIGHT = 20; + + QString title; + QColor lineColor = QColor(0x4A, 0x4A, 0x4A); + int marginLeft = 0; + int marginRight = 10; + }; +}