Skip to content

Commit

Permalink
Merge pull request #163 from lxqt/eliding_name_label
Browse files Browse the repository at this point in the history
GUI fixes: Elide playback name label if needed and…
  • Loading branch information
tsujan committed Aug 19, 2020
2 parents c0f31f8 + 0ae1420 commit c900346
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(pavucontrol-qt_HDRS
sourceoutputwidget.h
sourcewidget.h
streamwidget.h
elidinglabel.h
)

set(pavucontrol-qt_SRCS
Expand All @@ -31,6 +32,7 @@ set(pavucontrol-qt_SRCS
sourceoutputwidget.cc
sourcewidget.cc
streamwidget.cc
elidinglabel.cc
)

set(pavucontrol-qt_UI
Expand Down
46 changes: 46 additions & 0 deletions src/elidinglabel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/***
This file is part of pavucontrol-qt.
pavucontrol-qt is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
pavucontrol-qt is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with pavucontrol-qt. If not, see <https://www.gnu.org/licenses/>.
***/

#include "elidinglabel.h"
#include <QPainter>
#include <QStyleOption>

ElidingLabel::ElidingLabel(QWidget *parent, Qt::WindowFlags f):
QLabel(parent, f),
lastWidth_(0) {
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
// set a min width to prevent the window from widening with long texts
setMinimumWidth(fontMetrics().averageCharWidth() * 10);
}

// A simplified version of QLabel::paintEvent() without pixmap or shortcut but with eliding
void ElidingLabel::paintEvent(QPaintEvent */*event*/) {
QRect cr = contentsRect().adjusted(margin(), margin(), -margin(), -margin());
QString txt = text();
// if the text is changed or its rect is resized (due to window resizing),
// find whether it needs to be elided...
if (txt != lastText_ || cr.width() != lastWidth_) {
lastText_ = txt;
lastWidth_ = cr.width();
elidedText_ = fontMetrics().elidedText(txt, Qt::ElideMiddle, cr.width());
}
// ... then, draw the (elided) text */
QPainter painter(this);
QStyleOption opt;
opt.initFrom(this);
style()->drawItemText(&painter, cr, alignment(), opt.palette, isEnabled(), elidedText_, foregroundRole());
}
38 changes: 38 additions & 0 deletions src/elidinglabel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/***
This file is part of pavucontrol-qt.
pavucontrol-qt is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
pavucontrol-qt is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with pavucontrol-qt. If not, see <https://www.gnu.org/licenses/>.
***/

#ifndef elidinglabel_h
#define elidinglabel_h

#include <QLabel>

class ElidingLabel : public QLabel {
Q_OBJECT

public:
explicit ElidingLabel(QWidget *parent = 0, Qt::WindowFlags f = Qt::WindowFlags());

protected:
void paintEvent(QPaintEvent *event) override;

private:
QString elidedText_;
QString lastText_;
int lastWidth_;
};

#endif // elidinglabel_h
1 change: 0 additions & 1 deletion src/minimalstreamwidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ MinimalStreamWidget::MinimalStreamWidget(QWidget *parent) :
volumeMeterVisible(true) {

peakProgressBar->setTextVisible(false);
peakProgressBar->setMaximumHeight(4 /* FIXME: hardcoded */);
peakProgressBar->hide();
}

Expand Down
9 changes: 8 additions & 1 deletion src/streamwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="nameLabel">
<widget class="ElidingLabel" name="nameLabel">
<property name="text">
<string>Device Title</string>
</property>
Expand Down Expand Up @@ -103,6 +103,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ElidingLabel</class>
<extends>QLabel</extends>
<header>elidinglabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit c900346

Please sign in to comment.