summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2016-08-01 08:51:18 +0200
committerLiang Qi <liang.qi@qt.io>2016-08-01 10:03:21 +0200
commit3cb7302480390e5149a9cf6f49ac1ca94ef63f97 (patch)
tree831134f1053214ab0d5ba4d406cf7b860ed7c578 /src/widgets/itemviews
parent2ff1557937c398a7fb5cc7ba120e7ca3b5eacd36 (diff)
parentf24cc53cc27d8ed4be4c1d0d2df059dd6a6909a9 (diff)
Merge remote-tracking branch 'origin/5.6' into 5.7
Conflicts: src/widgets/itemviews/qabstractitemview.cpp src/widgets/itemviews/qabstractitemview_p.h Change-Id: I54589b1365103cb1749186af92aab03a49c94b64
Diffstat (limited to 'src/widgets/itemviews')
-rw-r--r--src/widgets/itemviews/qabstractitemdelegate.cpp55
-rw-r--r--src/widgets/itemviews/qabstractitemdelegate_p.h3
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp6
-rw-r--r--src/widgets/itemviews/qabstractitemview_p.h5
-rw-r--r--src/widgets/itemviews/qitemdelegate.cpp42
-rw-r--r--src/widgets/itemviews/qstyleditemdelegate.cpp36
6 files changed, 65 insertions, 82 deletions
diff --git a/src/widgets/itemviews/qabstractitemdelegate.cpp b/src/widgets/itemviews/qabstractitemdelegate.cpp
index 6ed019f4a8..5e3b74f7b7 100644
--- a/src/widgets/itemviews/qabstractitemdelegate.cpp
+++ b/src/widgets/itemviews/qabstractitemdelegate.cpp
@@ -376,6 +376,7 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
+ Q_D(QAbstractItemDelegate);
Q_UNUSED(option);
if (!event || !view)
@@ -384,9 +385,10 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
#ifndef QT_NO_TOOLTIP
case QEvent::ToolTip: {
QHelpEvent *he = static_cast<QHelpEvent*>(event);
- QVariant tooltip = index.data(Qt::ToolTipRole);
- if (tooltip.canConvert<QString>()) {
- QToolTip::showText(he->globalPos(), tooltip.toString(), view);
+ const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp
+ const QString tooltip = d->textForRole(Qt::ToolTipRole, index.data(Qt::ToolTipRole), option.locale, precision);
+ if (!tooltip.isEmpty()) {
+ QToolTip::showText(he->globalPos(), tooltip, view);
return true;
}
break;}
@@ -398,9 +400,10 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
break; }
case QEvent::WhatsThis: {
QHelpEvent *he = static_cast<QHelpEvent*>(event);
- QVariant whatsthis = index.data(Qt::WhatsThisRole);
- if (whatsthis.canConvert<QString>()) {
- QWhatsThis::showText(he->globalPos(), whatsthis.toString(), view);
+ const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp
+ const QString whatsthis = d->textForRole(Qt::WhatsThisRole, index.data(Qt::WhatsThisRole), option.locale, precision);
+ if (!whatsthis.isEmpty()) {
+ QWhatsThis::showText(he->globalPos(), whatsthis, view);
return true;
}
break ; }
@@ -543,6 +546,46 @@ bool QAbstractItemDelegatePrivate::tryFixup(QWidget *editor)
return true;
}
+QString QAbstractItemDelegatePrivate::textForRole(Qt::ItemDataRole role, const QVariant &value, const QLocale &locale, int precision) const
+{
+ const QLocale::FormatType formatType = (role == Qt::DisplayRole) ? QLocale::ShortFormat : QLocale::LongFormat;
+ QString text;
+ switch (value.userType()) {
+ case QMetaType::Float:
+ text = locale.toString(value.toFloat());
+ break;
+ case QVariant::Double:
+ text = locale.toString(value.toDouble(), 'g', precision);
+ break;
+ case QVariant::Int:
+ case QVariant::LongLong:
+ text = locale.toString(value.toLongLong());
+ break;
+ case QVariant::UInt:
+ case QVariant::ULongLong:
+ text = locale.toString(value.toULongLong());
+ break;
+ case QVariant::Date:
+ text = locale.toString(value.toDate(), formatType);
+ break;
+ case QVariant::Time:
+ text = locale.toString(value.toTime(), formatType);
+ break;
+ case QVariant::DateTime: {
+ const QDateTime dateTime = value.toDateTime();
+ text = locale.toString(dateTime.date(), formatType)
+ + QLatin1Char(' ')
+ + locale.toString(dateTime.time(), formatType);
+ break; }
+ default:
+ text = value.toString();
+ if (role == Qt::DisplayRole)
+ text.replace(QLatin1Char('\n'), QChar::LineSeparator);
+ break;
+ }
+ return text;
+}
+
void QAbstractItemDelegatePrivate::_q_commitDataAndCloseEditor(QWidget *editor)
{
Q_Q(QAbstractItemDelegate);
diff --git a/src/widgets/itemviews/qabstractitemdelegate_p.h b/src/widgets/itemviews/qabstractitemdelegate_p.h
index 2fd38bdfd7..958737dd01 100644
--- a/src/widgets/itemviews/qabstractitemdelegate_p.h
+++ b/src/widgets/itemviews/qabstractitemdelegate_p.h
@@ -58,7 +58,7 @@
QT_BEGIN_NAMESPACE
-class QAbstractItemDelegatePrivate : public QObjectPrivate
+class Q_AUTOTEST_EXPORT QAbstractItemDelegatePrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QAbstractItemDelegate)
public:
@@ -66,6 +66,7 @@ public:
bool editorEventFilter(QObject *object, QEvent *event);
bool tryFixup(QWidget *editor);
+ QString textForRole(Qt::ItemDataRole role, const QVariant &value, const QLocale &locale, int precision = 6) const;
void _q_commitDataAndCloseEditor(QWidget *editor);
};
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index e67f8d83fb..83b9b7716d 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -4325,6 +4325,12 @@ const QEditorInfo & QAbstractItemViewPrivate::editorForIndex(const QModelIndex &
return it.value();
}
+bool QAbstractItemViewPrivate::hasEditor(const QModelIndex &index) const
+{
+ // Search's implicit cast (QModelIndex to QPersistentModelIndex) is slow; use cheap pre-test to avoid when we can.
+ return !indexEditorHash.isEmpty() && indexEditorHash.contains(index);
+}
+
QModelIndex QAbstractItemViewPrivate::indexForEditor(QWidget *editor) const
{
// do not try to search to avoid slow implicit cast from QModelIndex to QPersistentModelIndex
diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h
index 60ae1559b1..fcbe25d735 100644
--- a/src/widgets/itemviews/qabstractitemview_p.h
+++ b/src/widgets/itemviews/qabstractitemview_p.h
@@ -276,10 +276,7 @@ public:
}
const QEditorInfo &editorForIndex(const QModelIndex &index) const;
- inline bool hasEditor(const QModelIndex &index) const {
- // Search's implicit cast (QModelIndex to QPersistentModelIndex) is slow; use cheap pre-test to avoid when we can.
- return !indexEditorHash.isEmpty() && indexEditorHash.contains(index);
- }
+ bool hasEditor(const QModelIndex &index) const;
QModelIndex indexForEditor(QWidget *editor) const;
void addEditor(const QModelIndex &index, QWidget *editor, bool isStatic);
diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp
index 194bd22493..747d5db782 100644
--- a/src/widgets/itemviews/qitemdelegate.cpp
+++ b/src/widgets/itemviews/qitemdelegate.cpp
@@ -67,6 +67,7 @@
#include <limits.h>
+// keep in sync with QAbstractItemDelegate::helpEvent()
#ifndef DBL_DIG
# define DBL_DIG 10
#endif
@@ -102,7 +103,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;
@@ -332,40 +333,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);
}
/*!
@@ -434,7 +404,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);
}
@@ -1061,7 +1031,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<QFont>(value).resolve(option.font);
return textRectangle(0, d->textLayoutBounds(option), fnt, text); }
diff --git a/src/widgets/itemviews/qstyleditemdelegate.cpp b/src/widgets/itemviews/qstyleditemdelegate.cpp
index 162ef826da..bab8436dce 100644
--- a/src/widgets/itemviews/qstyleditemdelegate.cpp
+++ b/src/widgets/itemviews/qstyleditemdelegate.cpp
@@ -261,41 +261,7 @@ QStyledItemDelegate::~QStyledItemDelegate()
*/
QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale& locale) const
{
- QString text;
- switch (value.userType()) {
- case QMetaType::Float:
- case QVariant::Double:
- text = locale.toString(value.toReal());
- break;
- case QVariant::Int:
- case QVariant::LongLong:
- text = locale.toString(value.toLongLong());
- break;
- case QVariant::UInt:
- case QVariant::ULongLong:
- text = locale.toString(value.toULongLong());
- break;
- case QVariant::Date:
- text = locale.toString(value.toDate(), QLocale::ShortFormat);
- break;
- case QVariant::Time:
- text = locale.toString(value.toTime(), QLocale::ShortFormat);
- break;
- case QVariant::DateTime:
- text = locale.toString(value.toDateTime().date(), QLocale::ShortFormat);
- text += QLatin1Char(' ');
- text += locale.toString(value.toDateTime().time(), QLocale::ShortFormat);
- break;
- default:
- // convert new lines into line separators
- text = value.toString();
- for (int i = 0; i < text.count(); ++i) {
- if (text.at(i) == QLatin1Char('\n'))
- text[i] = QChar::LineSeparator;
- }
- break;
- }
- return text;
+ return d_func()->textForRole(Qt::DisplayRole, value, locale);
}
/*!