forked from qt-creator/qt-creator
utils: Add additional tooltip to ElidingLabel
In case you want to set a real Tooltip for an eliding label Added tests in tst_manual_widgets_infolabel Change-Id: I72bcbf672415ce439c14e260e364fcd2f85fc733 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -57,7 +57,7 @@ void ElidingLabel::setElideMode(const Qt::TextElideMode &elideMode)
|
|||||||
{
|
{
|
||||||
m_elideMode = elideMode;
|
m_elideMode = elideMode;
|
||||||
if (elideMode == Qt::ElideNone)
|
if (elideMode == Qt::ElideNone)
|
||||||
setToolTip({});
|
updateToolTip({});
|
||||||
|
|
||||||
setSizePolicy(QSizePolicy(
|
setSizePolicy(QSizePolicy(
|
||||||
m_elideMode == Qt::ElideNone ? QSizePolicy::Preferred : QSizePolicy::Ignored,
|
m_elideMode == Qt::ElideNone ? QSizePolicy::Preferred : QSizePolicy::Ignored,
|
||||||
@@ -66,10 +66,21 @@ void ElidingLabel::setElideMode(const Qt::TextElideMode &elideMode)
|
|||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString ElidingLabel::additionalToolTip()
|
||||||
|
{
|
||||||
|
return m_additionalToolTip;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ElidingLabel::setAdditionalToolTip(const QString &additionalToolTip)
|
||||||
|
{
|
||||||
|
m_additionalToolTip = additionalToolTip;
|
||||||
|
}
|
||||||
|
|
||||||
void ElidingLabel::paintEvent(QPaintEvent *)
|
void ElidingLabel::paintEvent(QPaintEvent *)
|
||||||
{
|
{
|
||||||
if (m_elideMode == Qt::ElideNone) {
|
if (m_elideMode == Qt::ElideNone) {
|
||||||
QLabel::paintEvent(nullptr);
|
QLabel::paintEvent(nullptr);
|
||||||
|
updateToolTip({});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +89,10 @@ void ElidingLabel::paintEvent(QPaintEvent *)
|
|||||||
QFontMetrics fm = fontMetrics();
|
QFontMetrics fm = fontMetrics();
|
||||||
QString txt = text();
|
QString txt = text();
|
||||||
if (txt.length() > 4 && fm.horizontalAdvance(txt) > contents.width()) {
|
if (txt.length() > 4 && fm.horizontalAdvance(txt) > contents.width()) {
|
||||||
setToolTip(txt);
|
updateToolTip(txt);
|
||||||
txt = fm.elidedText(txt, m_elideMode, contents.width());
|
txt = fm.elidedText(txt, m_elideMode, contents.width());
|
||||||
} else {
|
} else {
|
||||||
setToolTip(QString());
|
updateToolTip(QString());
|
||||||
}
|
}
|
||||||
int flags = QStyle::visualAlignment(layoutDirection(), alignment()) | Qt::TextSingleLine;
|
int flags = QStyle::visualAlignment(layoutDirection(), alignment()) | Qt::TextSingleLine;
|
||||||
|
|
||||||
@@ -90,4 +101,29 @@ void ElidingLabel::paintEvent(QPaintEvent *)
|
|||||||
painter.drawText(contents, flags, txt);
|
painter.drawText(contents, flags, txt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ElidingLabel::updateToolTip(const QString &elidedText)
|
||||||
|
{
|
||||||
|
if (m_additionalToolTip.isEmpty()) {
|
||||||
|
setToolTip(elidedText);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elidedText.isEmpty()) {
|
||||||
|
setToolTip(m_additionalToolTip);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setToolTip(elidedText + m_additionalToolTipSeparator + m_additionalToolTip);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ElidingLabel::additionalToolTipSeparator() const
|
||||||
|
{
|
||||||
|
return m_additionalToolTipSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ElidingLabel::setAdditionalToolTipSeparator(const QString &newAdditionalToolTipSeparator)
|
||||||
|
{
|
||||||
|
m_additionalToolTipSeparator = newAdditionalToolTipSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
@@ -34,6 +34,8 @@ class QTCREATOR_UTILS_EXPORT ElidingLabel : public QLabel
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(Qt::TextElideMode elideMode READ elideMode WRITE setElideMode DESIGNABLE true)
|
Q_PROPERTY(Qt::TextElideMode elideMode READ elideMode WRITE setElideMode DESIGNABLE true)
|
||||||
|
Q_PROPERTY(QString additionalToolTip READ additionalToolTip WRITE setAdditionalToolTip DESIGNABLE true)
|
||||||
|
Q_PROPERTY(QString additionalToolTipSeparator READ additionalToolTipSeparator WRITE setAdditionalToolTipSeparator DESIGNABLE true)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ElidingLabel(QWidget *parent = nullptr);
|
explicit ElidingLabel(QWidget *parent = nullptr);
|
||||||
@@ -42,11 +44,20 @@ public:
|
|||||||
Qt::TextElideMode elideMode() const;
|
Qt::TextElideMode elideMode() const;
|
||||||
void setElideMode(const Qt::TextElideMode &elideMode);
|
void setElideMode(const Qt::TextElideMode &elideMode);
|
||||||
|
|
||||||
|
QString additionalToolTip();
|
||||||
|
void setAdditionalToolTip(const QString& additionalToolTip);
|
||||||
|
|
||||||
|
QString additionalToolTipSeparator() const;
|
||||||
|
void setAdditionalToolTipSeparator(const QString &newAdditionalToolTipSeparator);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
void updateToolTip(const QString& elidedText);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Qt::TextElideMode m_elideMode;
|
Qt::TextElideMode m_elideMode;
|
||||||
|
QString m_additionalToolTip;
|
||||||
|
QString m_additionalToolTipSeparator{"\n\n"};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
@@ -54,13 +54,15 @@ int main(int argc, char *argv[])
|
|||||||
const static struct {
|
const static struct {
|
||||||
const InfoLabel::InfoType type;
|
const InfoLabel::InfoType type;
|
||||||
const char *text;
|
const char *text;
|
||||||
|
const char *tooltip;
|
||||||
|
const char *tooltipSeparator;
|
||||||
} labels[] = {
|
} labels[] = {
|
||||||
{InfoLabel::Information, "Information"},
|
{InfoLabel::Information, "Information", "This is an informative Tooltip for you", "\n\n"},
|
||||||
{InfoLabel::Warning, "Warning"},
|
{InfoLabel::Warning, "Warning", "This is a warning Tooltip for you", " - "},
|
||||||
{InfoLabel::Error, "Error"},
|
{InfoLabel::Error, "Error", "This is an erroneous Tooltip for you", " | "},
|
||||||
{InfoLabel::Ok, "Ok"},
|
{InfoLabel::Ok, "Ok", "This is an ok Tooltip for you", " :) "},
|
||||||
{InfoLabel::NotOk, "NotOk"},
|
{InfoLabel::NotOk, "NotOk", "This Tooltip is just not ok", ""},
|
||||||
{InfoLabel::None, "None"},
|
{InfoLabel::None, "None", "", "----"},
|
||||||
};
|
};
|
||||||
|
|
||||||
int gridRow = 0;
|
int gridRow = 0;
|
||||||
@@ -72,6 +74,8 @@ int main(int argc, char *argv[])
|
|||||||
+ QLatin1String(enabled ? "" : " (disabled)"), label.type);
|
+ QLatin1String(enabled ? "" : " (disabled)"), label.type);
|
||||||
infoLabel->setEnabled(enabled);
|
infoLabel->setEnabled(enabled);
|
||||||
infoLabel->setFilled(filled);
|
infoLabel->setFilled(filled);
|
||||||
|
infoLabel->setAdditionalToolTip(label.tooltip);
|
||||||
|
infoLabel->setAdditionalToolTipSeparator(label.tooltipSeparator);
|
||||||
variationsLayout->addWidget(infoLabel, gridRow, enabled ? 0 : 1);
|
variationsLayout->addWidget(infoLabel, gridRow, enabled ? 0 : 1);
|
||||||
}
|
}
|
||||||
gridRow++;
|
gridRow++;
|
||||||
@@ -102,11 +106,27 @@ int main(int argc, char *argv[])
|
|||||||
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
|
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
|
||||||
"ullamco laboris nisi ut aliquid ex ea commodi consequat.";
|
"ullamco laboris nisi ut aliquid ex ea commodi consequat.";
|
||||||
|
|
||||||
mainLayout->addWidget(new Utils::InfoLabel("Qt::ElideRight: " + lorem, InfoLabel::Information));
|
auto elideRight = new Utils::InfoLabel("Qt::ElideRight: " + lorem, InfoLabel::Information);
|
||||||
|
elideRight->setAdditionalToolTip("This control will elide the right side and show an Information Icon to its left. The Elided text will be separated from this text by two \\n");
|
||||||
|
mainLayout->addWidget(elideRight);
|
||||||
|
|
||||||
|
auto elideLeft = new Utils::InfoLabel("Qt::ElideLeft: " + lorem, InfoLabel::Warning);
|
||||||
|
elideLeft->setElideMode(Qt::ElideLeft);
|
||||||
|
elideLeft->setAdditionalToolTip("This control will elide the left side and show a Warning Icon to its left. The Elided text will be separated from this text by \" :) \"");
|
||||||
|
elideLeft->setAdditionalToolTipSeparator(" :) ");
|
||||||
|
mainLayout->addWidget(elideLeft);
|
||||||
|
|
||||||
|
auto elideMid= new Utils::InfoLabel("Qt::ElideMiddle: " + lorem, InfoLabel::Ok);
|
||||||
|
elideMid->setElideMode(Qt::ElideMiddle);
|
||||||
|
elideMid->setAdditionalToolTip("This control will elide the middle and show an Ok Icon to its left. The Elided text will be separated from this text by \" -> \"");
|
||||||
|
elideMid->setAdditionalToolTipSeparator(" -> ");
|
||||||
|
mainLayout->addWidget(elideMid);
|
||||||
|
|
||||||
|
|
||||||
auto elideNone = new Utils::InfoLabel("Qt::ElideNone: " + lorem, InfoLabel::Information);
|
auto elideNone = new Utils::InfoLabel("Qt::ElideNone: " + lorem, InfoLabel::Information);
|
||||||
elideNone->setElideMode(Qt::ElideNone);
|
elideNone->setElideMode(Qt::ElideNone);
|
||||||
elideNone->setWordWrap(true);
|
elideNone->setWordWrap(true);
|
||||||
|
elideNone->setAdditionalToolTip("This control is never elided due to setElideMode(Qt::ElideNone) being used");
|
||||||
mainLayout->addWidget(elideNone);
|
mainLayout->addWidget(elideNone);
|
||||||
|
|
||||||
widget->resize(350, 500);
|
widget->resize(350, 500);
|
||||||
|
Reference in New Issue
Block a user