2017-03-25 21:53:28 +00:00
|
|
|
#include "ElidedLabel.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
|
|
|
|
ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f)
|
|
|
|
: QLabel(parent, f)
|
|
|
|
, elideMode_(Qt::ElideRight)
|
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
// setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
|
2017-03-25 21:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ElidedLabel::ElidedLabel(const QString& txt, QWidget* parent, Qt::WindowFlags f)
|
|
|
|
: QLabel(txt, parent, f)
|
|
|
|
, elideMode_(Qt::ElideRight)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
ElidedLabel::ElidedLabel(const QString& txt, Qt::TextElideMode elideMode, QWidget* parent, Qt::WindowFlags f)
|
2017-03-25 21:53:28 +00:00
|
|
|
: QLabel(txt, parent, f)
|
|
|
|
, elideMode_(elideMode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedLabel::setText(const QString& txt)
|
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
QLabel::setText(txt);
|
|
|
|
cacheElidedText(geometry().width());
|
2017-03-25 21:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedLabel::cacheElidedText(int w)
|
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
cachedElidedText = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
|
2017-03-25 21:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedLabel::resizeEvent(QResizeEvent* e)
|
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
QLabel::resizeEvent(e);
|
|
|
|
cacheElidedText(e->size().width());
|
2017-03-25 21:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedLabel::paintEvent(QPaintEvent* e)
|
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
if(elideMode_ == Qt::ElideNone)
|
|
|
|
{
|
|
|
|
QLabel::paintEvent(e);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QPainter p(this);
|
|
|
|
p.drawText(0, 0, geometry().width(), geometry().height(), alignment(), cachedElidedText);
|
|
|
|
}
|
2017-03-25 21:53:28 +00:00
|
|
|
}
|