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.
This commit is contained in:
Michael Gregorius
2023-07-16 15:26:58 +02:00
parent 11f1bc747a
commit 0184efd630
3 changed files with 74 additions and 11 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -1,6 +1,7 @@
#include <BarModelEditor.h>
#include <QPainter>
#include <QStyle>
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);
}