mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 10:45:37 +08:00
Ref T573, elide text messages for labels
This commit is contained in:
committed by
Mat Sutcliffe
parent
57ae224c41
commit
53d5ed96af
@@ -31,6 +31,7 @@
|
|||||||
#include <QTabWidget>
|
#include <QTabWidget>
|
||||||
#include <QThreadStorage>
|
#include <QThreadStorage>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QLabel>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <Qt>
|
#include <Qt>
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
@@ -645,4 +646,42 @@ namespace BlackGui
|
|||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CGuiUtility::setElidedText(QLabel *label, const QString &text, Qt::TextElideMode mode)
|
||||||
|
{
|
||||||
|
if (!label) { return; }
|
||||||
|
|
||||||
|
label->setToolTip(text);
|
||||||
|
if (mode == Qt::ElideNone)
|
||||||
|
{
|
||||||
|
label->setText(text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QFontMetrics metrics(label->font());
|
||||||
|
const int width = qMax(label->width() - 2, 0);
|
||||||
|
const QString clippedText = metrics.elidedText(text, mode, width);
|
||||||
|
label->setText(clippedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CGuiUtility::setElidedText(QLabel *label, const QString &shortText, const QString &longText, Qt::TextElideMode mode)
|
||||||
|
{
|
||||||
|
if (!label) { return; }
|
||||||
|
if (shortText.isEmpty()) { CGuiUtility::setElidedText(label, longText, mode); return; }
|
||||||
|
if (longText.isEmpty()) { CGuiUtility::setElidedText(label, shortText, mode); return; }
|
||||||
|
|
||||||
|
label->setToolTip(longText);
|
||||||
|
const QFontMetrics metrics(label->font());
|
||||||
|
const int width = qMax(label->width() - 2, 0);
|
||||||
|
const int wl = metrics.width(longText);
|
||||||
|
if (wl >= width) { label->setText(longText); return; }
|
||||||
|
if (qRound(wl * 0.85) > wl)
|
||||||
|
{
|
||||||
|
const QString clippedText = metrics.elidedText(longText, mode, width);
|
||||||
|
label->setText(clippedText);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const QString clippedText = metrics.elidedText(shortText, mode, width);
|
||||||
|
label->setText(clippedText);
|
||||||
|
}
|
||||||
} // ns
|
} // ns
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class QMimeData;
|
|||||||
class QLineEdit;
|
class QLineEdit;
|
||||||
class QTabWidget;
|
class QTabWidget;
|
||||||
class QDialog;
|
class QDialog;
|
||||||
|
class QLabel;
|
||||||
class QGraphicsOpacityEffect;
|
class QGraphicsOpacityEffect;
|
||||||
class QAbstractItemModel;
|
class QAbstractItemModel;
|
||||||
|
|
||||||
@@ -237,6 +238,12 @@ namespace BlackGui
|
|||||||
//! Find parent dialog if there is any, otherwise null
|
//! Find parent dialog if there is any, otherwise null
|
||||||
static QDialog *findParentDialog(QWidget *widget);
|
static QDialog *findParentDialog(QWidget *widget);
|
||||||
|
|
||||||
|
//! Set elided text
|
||||||
|
static void setElidedText(QLabel *label, const QString &text, Qt::TextElideMode mode = Qt::ElideMiddle);
|
||||||
|
|
||||||
|
//! Set elided text
|
||||||
|
static void setElidedText(QLabel *label, const QString &shortText, const QString &longText, Qt::TextElideMode mode = Qt::ElideMiddle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! No constructor, use static functions only
|
//! No constructor, use static functions only
|
||||||
CGuiUtility() {}
|
CGuiUtility() {}
|
||||||
|
|||||||
Reference in New Issue
Block a user