From 695d85363e537b51b5d0ae6ab08a9a1e7d9e8354 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Mon, 6 Apr 2015 13:47:16 +0300 Subject: Item delegates: show localized detailed tooltips and "What's this?" texts Extract the common part from QItemDelegate and QStyledItemDelegate which uses QLocale to convert a value for Qt::DisplayRole to a string. Use this code to get the text for tooltips and "What's this?". [ChangeLog][QtWidgets][QAbstractItemDelegate] Show localized detailed tooltips and "What's this?" texts. Task-number: QTBUG-16469 Change-Id: I8618763d45b8cfddafc2f263d658ba256be60a15 Reviewed-by: Giuseppe D'Angelo --- src/widgets/itemviews/qitemdelegate.cpp | 42 +++++---------------------------- 1 file changed, 6 insertions(+), 36 deletions(-) (limited to 'src/widgets/itemviews/qitemdelegate.cpp') diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp index cd952737dd..2b6e1210ca 100644 --- a/src/widgets/itemviews/qitemdelegate.cpp +++ b/src/widgets/itemviews/qitemdelegate.cpp @@ -61,6 +61,7 @@ #include +// keep in sync with QAbstractItemDelegate::helpEvent() #ifndef DBL_DIG # define DBL_DIG 10 #endif @@ -96,7 +97,7 @@ public: return text; } - static QString valueToText(const QVariant &value, const QStyleOptionViewItem &option); + QString valueToText(const QVariant &value, const QStyleOptionViewItem &option) const; QItemEditorFactory *f; bool clipPainting; @@ -326,40 +327,9 @@ void QItemDelegate::setClipping(bool clip) d->clipPainting = clip; } -QString QItemDelegatePrivate::valueToText(const QVariant &value, const QStyleOptionViewItem &option) +QString QItemDelegatePrivate::valueToText(const QVariant &value, const QStyleOptionViewItem &option) const { - QString text; - switch (value.userType()) { - case QMetaType::Float: - text = option.locale.toString(value.toFloat(), 'g'); - break; - case QVariant::Double: - text = option.locale.toString(value.toDouble(), 'g', DBL_DIG); - break; - case QVariant::Int: - case QVariant::LongLong: - text = option.locale.toString(value.toLongLong()); - break; - case QVariant::UInt: - case QVariant::ULongLong: - text = option.locale.toString(value.toULongLong()); - break; - case QVariant::Date: - text = option.locale.toString(value.toDate(), QLocale::ShortFormat); - break; - case QVariant::Time: - text = option.locale.toString(value.toTime(), QLocale::ShortFormat); - break; - case QVariant::DateTime: - text = option.locale.toString(value.toDateTime().date(), QLocale::ShortFormat); - text += QLatin1Char(' '); - text += option.locale.toString(value.toDateTime().time(), QLocale::ShortFormat); - break; - default: - text = replaceNewLine(value.toString()); - break; - } - return text; + return textForRole(Qt::DisplayRole, value, option.locale, DBL_DIG); } /*! @@ -428,7 +398,7 @@ void QItemDelegate::paint(QPainter *painter, QRect displayRect; value = index.data(Qt::DisplayRole); if (value.isValid() && !value.isNull()) { - text = QItemDelegatePrivate::valueToText(value, opt); + text = d->valueToText(value, opt); displayRect = textRectangle(painter, d->textLayoutBounds(opt), opt.font, text); } @@ -1055,7 +1025,7 @@ QRect QItemDelegate::rect(const QStyleOptionViewItem &option, return QRect(QPoint(0, 0), option.decorationSize); case QVariant::String: default: { - QString text = QItemDelegatePrivate::valueToText(value, option); + const QString text = d->valueToText(value, option); value = index.data(Qt::FontRole); QFont fnt = qvariant_cast(value).resolve(option.font); return textRectangle(0, d->textLayoutBounds(option), fnt, text); } -- cgit v1.2.3