]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Try to avoid calling the model's data method if only "text" is needed
authorFrank Reininghaus <frank78ac@googlemail.com>
Wed, 4 Sep 2013 19:34:03 +0000 (21:34 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Wed, 4 Sep 2013 19:37:01 +0000 (21:37 +0200)
Currently, KStandardItemListWidgetInformant::itemSizeHint() calls the
model's data(int) method for every single item, but the full data is
actually only needed for the size calculation in Compact View. In
Details View, no data is needed at all to determine the size required
for the item, and in Icons View, only the name is needed.

This patch makes it possible for subclasses of
KStandardItemListWidgetInformant to provide an alternative way to
obtain the "text", and implements this in the subclass
KFileItemListWidgetInformant.

The final goal is to achieve that the QHash which contains all data
for a file item is only created if it is really needed, e.g., because
the view needs access to the data for displaying the item on the
screen.

REVIEW: 112253

src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemlistwidget.h
src/kitemviews/kstandarditemlistwidget.cpp
src/kitemviews/kstandarditemlistwidget.h

index 3a7724134373a9e21c935d996515cd6b3aea8a33..688a4da083d0c5b15022f2c246d4e9cac5a6a542 100644 (file)
@@ -18,6 +18,8 @@
  ***************************************************************************/
 
 #include "kfileitemlistwidget.h"
+#include "kfileitemmodel.h"
+#include "kitemlistview.h"
 
 #include <kmimetype.h>
 #include <KDebug>
@@ -35,6 +37,15 @@ KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
 {
 }
 
+QString KFileItemListWidgetInformant::itemText(int index, const KItemListView* view) const
+{
+    Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
+    KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
+
+    const KFileItem item = fileItemModel->fileItem(index);
+    return item.text();
+}
+
 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
                                                const QHash<QByteArray, QVariant>& values) const
 {
index 24c6778281773823122836a16830a1153bc2f7af..1d7bc7f012d7be878a9b2ff8e3e98845c9e9b272 100644 (file)
@@ -31,6 +31,7 @@ public:
     virtual ~KFileItemListWidgetInformant();
 
 protected:
+    virtual QString itemText(int index, const KItemListView* view) const;
     virtual QString roleText(const QByteArray& role, const QHash<QByteArray, QVariant>& values) const;
 };
 
index bc0503663bceb1dfdb1065731bbaf6110838c084..4b9f33b6f07161f84d811040626f08ff8be935f4 100644 (file)
@@ -57,13 +57,12 @@ KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
 
 QSizeF KStandardItemListWidgetInformant::itemSizeHint(int index, const KItemListView* view) const
 {
-    const QHash<QByteArray, QVariant> values = view->model()->data(index);
     const KItemListStyleOption& option = view->styleOption();
     const int additionalRolesCount = qMax(view->visibleRoles().count() - 1, 0);
 
     switch (static_cast<const KStandardItemListView*>(view)->itemLayout()) {
     case KStandardItemListWidget::IconsLayout: {
-        const QString text = KStringHandler::preProcessWrap(values["text"].toString());
+        const QString text = KStringHandler::preProcessWrap(itemText(index, view));
 
         const qreal itemWidth = view->itemSize().width();
         const qreal maxWidth = itemWidth - 2 * option.padding;
@@ -100,6 +99,7 @@ QSizeF KStandardItemListWidgetInformant::itemSizeHint(int index, const KItemList
         // to show all roles without horizontal clipping.
         qreal maximumRequiredWidth = 0.0;
 
+        const QHash<QByteArray, QVariant> values = view->model()->data(index);
         foreach (const QByteArray& role, view->visibleRoles()) {
             const QString text = roleText(role, values);
             const qreal requiredWidth = option.fontMetrics.width(text);
@@ -159,6 +159,11 @@ qreal KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArra
     return width;
 }
 
+QString KStandardItemListWidgetInformant::itemText(int index, const KItemListView* view) const
+{
+    return view->model()->data(index).value("text").toString();
+}
+
 QString KStandardItemListWidgetInformant::roleText(const QByteArray& role,
                                                    const QHash<QByteArray, QVariant>& values) const
 {
index 4bf6116fd1121cd263c1cff74aed55ea0a75456b..7dd93b2b819babd5e48271ba47a1d89339b166bb 100644 (file)
@@ -44,6 +44,15 @@ public:
                                            int index,
                                            const KItemListView* view) const;
 protected:
+    /**
+     * @return The value of the "text" role. The default implementation returns
+     *         view->model()->data(index)["text"]. If a derived class can
+     *         prevent the (possibly expensive) construction of the
+     *         QHash<QByteArray, QVariant> returned by KItemModelBase::data(int),
+     *         it can reimplement this function.
+     */
+    virtual QString itemText(int index, const KItemListView* view) const;
+
     /**
      * @return String representation of the role \a role. The representation of
      *         a role might depend on other roles, so the values of all roles