Skip to content

Commit 902f6d1

Browse files
Warchamp7RytoEX
authored andcommitted
frontend: Display volume source names with QLabel
1 parent a42a6c4 commit 902f6d1

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

frontend/components/VolumeName.cpp

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,34 @@
1717

1818
#include "VolumeName.hpp"
1919

20+
#include <QHBoxLayout>
21+
#include <QLabel>
22+
#include <QResizeEvent>
2023
#include <QStyle>
2124
#include <QStyleOptionButton>
2225
#include <QStylePainter>
2326

2427
#include "moc_VolumeName.cpp"
2528

26-
VolumeName::VolumeName(obs_source_t *source, QWidget *parent) : QAbstractButton(parent)
29+
VolumeName::VolumeName(obs_source_t *source, QWidget *parent)
30+
: QAbstractButton(parent),
31+
indicatorWidth(style()->pixelMetric(QStyle::PM_MenuButtonIndicator, nullptr, this))
2732
{
2833
renamedSignal = OBSSignal(obs_source_get_signal_handler(source), "rename", &VolumeName::obsSourceRenamed, this);
2934
removedSignal = OBSSignal(obs_source_get_signal_handler(source), "remove", &VolumeName::obsSourceRemoved, this);
3035
destroyedSignal =
3136
OBSSignal(obs_source_get_signal_handler(source), "destroy", &VolumeName::obsSourceDestroyed, this);
3237

33-
setText(obs_source_get_name(source));
38+
QHBoxLayout *layout = new QHBoxLayout();
39+
setLayout(layout);
40+
41+
label = new QLabel(this);
42+
layout->addWidget(label);
43+
44+
layout->setContentsMargins(0, 0, indicatorWidth, 0);
45+
46+
QString name = obs_source_get_name(source);
47+
setText(name);
3448
}
3549

3650
VolumeName::~VolumeName() {}
@@ -54,15 +68,12 @@ QSize VolumeName::sizeHint() const
5468
int width = textSize.width();
5569
int height = textSize.height();
5670

57-
int iconWidth = style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
58-
int iconHeight = iconWidth;
59-
6071
if (!opt.icon.isNull()) {
61-
height = std::max(height, iconHeight);
72+
height = std::max(height, indicatorWidth);
6273
}
6374

6475
const int spacing = style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, this) / 2;
65-
width += iconWidth + spacing;
76+
width += indicatorWidth + spacing;
6677

6778
QSize contentsSize = style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(width, height), this);
6879
return contentsSize;
@@ -90,6 +101,12 @@ void VolumeName::obsSourceDestroyed(void *data, calldata_t *)
90101
QMetaObject::invokeMethod(widget, "onDestroyed", Qt::QueuedConnection);
91102
}
92103

104+
void VolumeName::resizeEvent(QResizeEvent *event)
105+
{
106+
updateLabelText(text());
107+
QAbstractButton::resizeEvent(event);
108+
}
109+
93110
void VolumeName::paintEvent(QPaintEvent *)
94111
{
95112
QStylePainter painter(this);
@@ -102,22 +119,6 @@ void VolumeName::paintEvent(QPaintEvent *)
102119
int paddingRight = opt.rect.right() - contentRect.right();
103120

104121
int indicatorWidth = style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
105-
QRect textRect = contentRect.adjusted(0, 0, -indicatorWidth - paddingRight / 2, 0);
106-
107-
painter.setFont(font());
108-
painter.setPen(opt.palette.color(QPalette::ButtonText));
109-
Qt::Alignment align = (textAlignment & (Qt::AlignLeft | Qt::AlignRight | Qt::AlignHCenter)) ? textAlignment
110-
: Qt::AlignHCenter;
111-
align |= Qt::AlignVCenter;
112-
113-
QFontMetrics metrics(font());
114-
QString elidedText = metrics.elidedText(text(), Qt::ElideMiddle, textRect.width());
115-
QRect metricsRect = metrics.boundingRect(text());
116-
117-
int textWidth = metricsRect.width();
118-
int maxTextWidth = contentRect.width() - indicatorWidth - paddingRight / 2;
119-
120-
painter.drawText(textRect, align, textWidth > maxTextWidth ? elidedText : text());
121122

122123
QStyleOption arrowOpt = opt;
123124
QRect arrowRect(opt.rect.right() - indicatorWidth - paddingRight / 2,
@@ -130,13 +131,37 @@ void VolumeName::paintEvent(QPaintEvent *)
130131
void VolumeName::onRenamed(QString name)
131132
{
132133
setText(name);
133-
updateGeometry();
134134

135135
std::string nameStr = name.toStdString();
136-
137136
emit renamed(nameStr.c_str());
138137
}
139138

139+
void VolumeName::setText(const QString &text)
140+
{
141+
QAbstractButton::setText(text);
142+
updateLabelText(text);
143+
}
144+
145+
void VolumeName::updateLabelText(const QString &name)
146+
{
147+
QString plainText = name;
148+
// Handle source names that use rich text.
149+
if (name.contains("<") && name.contains(">")) {
150+
QTextDocument doc;
151+
doc.setHtml(name);
152+
153+
plainText = doc.toPlainText();
154+
}
155+
156+
QFontMetrics metrics(label->font());
157+
QString elidedText = metrics.elidedText(plainText, Qt::ElideMiddle, width() - indicatorWidth * 2);
158+
159+
bool useElidedText = metrics.boundingRect(plainText).width() > width() - indicatorWidth;
160+
161+
bool isRichText = name != plainText;
162+
label->setText(useElidedText && !isRichText ? elidedText : name);
163+
}
164+
140165
void VolumeName::onRemoved()
141166
{
142167
emit removed();

frontend/components/VolumeName.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
#include <obs.hpp>
2121

2222
#include <QAbstractButton>
23+
#include <QLabel>
24+
#include <QPointer>
2325

2426
class VolumeName : public QAbstractButton {
2527
Q_OBJECT;
2628
Q_PROPERTY(Qt::Alignment textAlignment READ alignment WRITE setAlignment)
2729

30+
QPointer<QLabel> label{};
31+
int indicatorWidth;
32+
2833
public:
2934
VolumeName(obs_source_t *source, QWidget *parent = nullptr);
3035
~VolumeName();
@@ -34,6 +39,9 @@ class VolumeName : public QAbstractButton {
3439

3540
QSize sizeHint() const override;
3641

42+
void updateLabelText(const QString &name);
43+
void setText(const QString &text);
44+
3745
protected:
3846
OBSSignal renamedSignal;
3947
OBSSignal removedSignal;
@@ -45,6 +53,7 @@ class VolumeName : public QAbstractButton {
4553
static void obsSourceRemoved(void *data, calldata_t *params);
4654
static void obsSourceDestroyed(void *data, calldata_t *params);
4755

56+
void resizeEvent(QResizeEvent *event) override;
4857
void paintEvent(QPaintEvent *event) override;
4958

5059
private slots:

frontend/data/themes/Yami.obt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,9 +1728,12 @@ VolumeMeter {
17281728
}
17291729

17301730
VolumeName {
1731+
background: transparent;
1732+
padding: var(--padding_base);
1733+
}
1734+
1735+
VolumeName QLabel {
17311736
font-size: var(--font_small);
1732-
padding: 1px var(--padding_base);
1733-
border: 1px solid transparent;
17341737
}
17351738

17361739
VolumeName:focus {
@@ -1762,8 +1765,7 @@ VolumeControl #volLabel {
17621765
border-radius: var(--border_radius);
17631766
}
17641767

1765-
#hVolumeWidgets VolumeName {
1766-
padding: var(--padding_xlarge) var(--padding_xlarge);
1768+
#hVolumeWidgets VolumeName QLabel {
17671769
margin: 0px var(--spacing_base) var(--spacing_base);
17681770
}
17691771

0 commit comments

Comments
 (0)