From 0184efd63043eb1af521a74c73b3b558ece4013c Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sun, 16 Jul 2023 15:26:58 +0200 Subject: [PATCH] Style sheets for BarModelEditor Add style sheets options for BarModelEditor. See the changes in style.css for the available options and their usage. The members that can be manipulated by the style sheet options are initialized accoriding to the palette so that the BarModelEditor should also render something useful if no style is set. Adjust the paintEvent method to use the style sheet brushes and colors. --- data/themes/default/style.css | 6 +++ include/BarModelEditor.h | 19 ++++++++++ src/gui/widgets/BarModelEditor.cpp | 60 ++++++++++++++++++++++++------ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/data/themes/default/style.css b/data/themes/default/style.css index a9646cfe4..0721a6cea 100644 --- a/data/themes/default/style.css +++ b/data/themes/default/style.css @@ -1002,6 +1002,12 @@ lmms--gui--CompressorControlDialog lmms--gui--Knob { qproperty-lineWidth: 2; } +lmms--gui--BarModelEditor { + qproperty-backgroundBrush: rgba(30, 40, 51, 255); + qproperty-barBrush: rgba(3, 94, 97, 255); + qproperty-textColor: rgba(14, 192, 198, 255); +} + /* palette information */ lmms--gui--LmmsPalette { diff --git a/include/BarModelEditor.h b/include/BarModelEditor.h index 1897e801d..6779f7c76 100644 --- a/include/BarModelEditor.h +++ b/include/BarModelEditor.h @@ -35,7 +35,13 @@ namespace lmms::gui class LMMS_EXPORT BarModelEditor : public FloatModelEditorBase { + Q_OBJECT + public: + Q_PROPERTY(QBrush backgroundBrush READ getBackgroundBrush WRITE setBackgroundBrush) + Q_PROPERTY(QBrush barBrush READ getBarBrush WRITE setBarBrush) + Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) + BarModelEditor(QString text, FloatModel * floatModel, QWidget * parent = nullptr); // Define how the widget will behave in a layout @@ -45,11 +51,24 @@ public: virtual QSize sizeHint() const override; + QBrush const & getBackgroundBrush() const; + void setBackgroundBrush(QBrush const & backgroundBrush); + + QBrush const & getBarBrush() const; + void setBarBrush(QBrush const & barBrush); + + QColor const & getTextColor() const; + void setTextColor(QColor const & textColor); + protected: virtual void paintEvent(QPaintEvent *event) override; private: QString const m_text; + + QBrush m_backgroundBrush; + QBrush m_barBrush; + QColor m_textColor; }; } // namespace lmms::gui diff --git a/src/gui/widgets/BarModelEditor.cpp b/src/gui/widgets/BarModelEditor.cpp index 0464fb789..31a458208 100644 --- a/src/gui/widgets/BarModelEditor.cpp +++ b/src/gui/widgets/BarModelEditor.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace lmms::gui @@ -8,7 +9,10 @@ namespace lmms::gui BarModelEditor::BarModelEditor(QString text, FloatModel * floatModel, QWidget * parent) : FloatModelEditorBase(DirectionOfManipulation::Horizontal, parent), - m_text(text) + m_text(text), + m_backgroundBrush(palette().base()), + m_barBrush(palette().button()), + m_textColor(palette().text().color()) { setModel(floatModel); } @@ -29,14 +33,40 @@ QSize BarModelEditor::sizeHint() const return minimumSizeHint(); } +QBrush const & BarModelEditor::getBackgroundBrush() const +{ + return m_backgroundBrush; +} + +void BarModelEditor::setBackgroundBrush(QBrush const & backgroundBrush) +{ + m_backgroundBrush = backgroundBrush; +} + +QBrush const & BarModelEditor::getBarBrush() const +{ + return m_barBrush; +} + +void BarModelEditor::setBarBrush(QBrush const & barBrush) +{ + m_barBrush = barBrush; +} + +QColor const & BarModelEditor::getTextColor() const +{ + return m_textColor; +} + +void BarModelEditor::setTextColor(QColor const & textColor) +{ + m_textColor = textColor; +} + void BarModelEditor::paintEvent(QPaintEvent *event) { QWidget::paintEvent(event); - QColor const background(30, 40, 51); - QColor const foreground(3, 94, 97); - QColor const textColor(14, 192, 198); - auto const * mod = model(); auto const minValue = mod->minValue(); auto const maxValue = mod->maxValue(); @@ -45,11 +75,16 @@ void BarModelEditor::paintEvent(QPaintEvent *event) QRect const r = rect(); QPainter painter(this); - painter.setPen(background); - painter.setBrush(background); + + // Paint the base rectangle into which the bar and the text go + QBrush const & backgroundBrush = getBackgroundBrush(); + painter.setPen(backgroundBrush.color()); + painter.setBrush(backgroundBrush); painter.drawRect(r); - // Compute the percentage + + // Paint the bar + // Compute the percentage as: // min + x * (max - min) = v <=> x = (v - min) / (max - min) auto const percentage = range == 0 ? 1. : (mod->value() - minValue) / range; @@ -57,14 +92,16 @@ void BarModelEditor::paintEvent(QPaintEvent *event) QMargins const margins(margin, margin, margin, margin); QRect const valueRect = r.marginsRemoved(margins); - painter.setPen(foreground); - painter.setBrush(foreground); + QBrush const & barBrush = getBarBrush(); + painter.setPen(barBrush.color()); + painter.setBrush(barBrush); QPoint const startPoint = valueRect.topLeft(); QPoint endPoint = valueRect.bottomRight(); endPoint.setX(startPoint.x() + percentage * (endPoint.x() - startPoint.x())); painter.drawRect(QRect(startPoint, endPoint)); + // Draw the text into the value rectangle but move it slightly to the right QRect const textRect = valueRect.marginsRemoved(QMargins(3, 0, 0, 0)); @@ -72,7 +109,8 @@ void BarModelEditor::paintEvent(QPaintEvent *event) auto const fm = fontMetrics(); QString const elidedText = fm.elidedText(m_text, Qt::ElideRight, textRect.width()); - painter.setPen(textColor); + // Now draw the text + painter.setPen(getTextColor()); painter.drawText(textRect, elidedText); }