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
3650VolumeName::~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+
93110void 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 *)
130131void 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+
140165void VolumeName::onRemoved ()
141166{
142167 emit removed ();
0 commit comments