mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-06-30 11:02:19 +08:00
Qt: new class ElidedLabel (#21277)
* shorten ssid * truncate with ellipsis * rename to trancate * truncate 22 characters * elidedLabel * trimmed * remove function elidedString * cache elidedTest * rebase master * minimizeSizeHint * add sizeHint * cleanup * revert * inherit from QLabel Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> old-commit-hash: aee2b893a7467a05b6d49c16c5b6e0b6cf2f9275
This commit is contained in:
@@ -174,7 +174,7 @@ void WifiUI::refresh() {
|
||||
for (Network &network : wifi->seen_networks) {
|
||||
QHBoxLayout *hlayout = new QHBoxLayout;
|
||||
|
||||
QLabel *ssid_label = new QLabel(QString::fromUtf8(network.ssid));
|
||||
ElidedLabel *ssid_label = new ElidedLabel(network.ssid);
|
||||
ssid_label->setStyleSheet("font-size: 55px;");
|
||||
hlayout->addWidget(ssid_label, 1, Qt::AlignLeft);
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "selfdrive/ui/qt/widgets/controls.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
|
||||
QFrame *horizontal_line(QWidget *parent) {
|
||||
QFrame *line = new QFrame(parent);
|
||||
line->setFrameShape(QFrame::StyledPanel);
|
||||
@@ -83,3 +86,31 @@ ButtonControl::ButtonControl(const QString &title, const QString &text, const QS
|
||||
QObject::connect(&btn, &QPushButton::released, this, &ButtonControl::released);
|
||||
hlayout->addWidget(&btn);
|
||||
}
|
||||
|
||||
// ElidedLabel
|
||||
|
||||
ElidedLabel::ElidedLabel(QWidget *parent) : ElidedLabel({}, parent) {}
|
||||
|
||||
ElidedLabel::ElidedLabel(const QString &text, QWidget *parent) : QLabel(text.trimmed(), parent) {
|
||||
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
||||
setMinimumWidth(1);
|
||||
}
|
||||
|
||||
void ElidedLabel::resizeEvent(QResizeEvent* event) {
|
||||
QLabel::resizeEvent(event);
|
||||
lastText_ = elidedText_ = "";
|
||||
}
|
||||
|
||||
void ElidedLabel::paintEvent(QPaintEvent *event) {
|
||||
const QString curText = text();
|
||||
if (curText != lastText_) {
|
||||
elidedText_ = fontMetrics().elidedText(curText, Qt::ElideRight, contentsRect().width());
|
||||
lastText_ = curText;
|
||||
}
|
||||
|
||||
QPainter painter(this);
|
||||
drawFrame(&painter);
|
||||
QStyleOption opt;
|
||||
opt.initFrom(this);
|
||||
style()->drawItemText(&painter, contentsRect(), alignment(), opt.palette, isEnabled(), elidedText_, foregroundRole());
|
||||
}
|
||||
|
||||
@@ -9,6 +9,20 @@
|
||||
#include "selfdrive/ui/qt/widgets/toggle.h"
|
||||
|
||||
QFrame *horizontal_line(QWidget *parent = nullptr);
|
||||
|
||||
class ElidedLabel : public QLabel {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ElidedLabel(QWidget *parent = 0);
|
||||
explicit ElidedLabel(const QString &text, QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void resizeEvent(QResizeEvent* event) override;
|
||||
QString lastText_, elidedText_;
|
||||
};
|
||||
|
||||
class AbstractControl : public QFrame {
|
||||
Q_OBJECT
|
||||
|
||||
@@ -48,7 +62,7 @@ public:
|
||||
void setText(const QString &text) { label.setText(text); }
|
||||
|
||||
private:
|
||||
QLabel label;
|
||||
ElidedLabel label;
|
||||
};
|
||||
|
||||
// widget for a button with a label
|
||||
|
||||
Reference in New Issue
Block a user