]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kstandarditemlistwidget.cpp
Try to avoid calling the model's data method if only "text" is needed
[dolphin.git] / src / kitemviews / kstandarditemlistwidget.cpp
index 621cd81cc8250ceb47bd1b112451f6a08b59ae80..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
 {
@@ -204,8 +209,13 @@ KStandardItemListWidget::~KStandardItemListWidget()
     qDeleteAll(m_textInfo);
     m_textInfo.clear();
 
-    delete m_roleEditor;
-    delete m_oldRoleEditor;
+    if (m_roleEditor) {
+        m_roleEditor->deleteLater();
+    }
+
+    if (m_oldRoleEditor) {
+        m_oldRoleEditor->deleteLater();
+    }
 }
 
 void KStandardItemListWidget::setLayout(Layout layout)
@@ -649,16 +659,15 @@ void KStandardItemListWidget::editedRoleChanged(const QByteArray& current, const
                        this, SLOT(slotRoleEditingCanceled(QByteArray,QVariant)));
             disconnect(m_roleEditor, SIGNAL(roleEditingFinished(QByteArray,QVariant)),
                        this, SLOT(slotRoleEditingFinished(QByteArray,QVariant)));
+
+            if (m_oldRoleEditor) {
+                m_oldRoleEditor->deleteLater();
+            }
             m_oldRoleEditor = m_roleEditor;
             m_roleEditor->hide();
             m_roleEditor = 0;
         }
         return;
-    } else if (m_oldRoleEditor) {
-        // Delete the old editor before constructing the new one to
-        // prevent a memory leak.
-        m_oldRoleEditor->deleteLater();
-        m_oldRoleEditor = 0;
     }
 
     Q_ASSERT(!m_roleEditor);
@@ -791,7 +800,6 @@ void KStandardItemListWidget::updateExpansionArea()
 {
     if (m_supportsItemExpanding) {
         const QHash<QByteArray, QVariant> values = data();
-        Q_ASSERT(values.contains("expandedParentsCount"));
         const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
         if (expandedParentsCount >= 0) {
             const qreal widgetHeight = size().height();
@@ -1027,12 +1035,15 @@ void KStandardItemListWidget::updateIconsLayoutTextCache()
             const int textLength = line.textStart() + line.textLength();
             if (textLength < nameText.length()) {
                 // Elide the last line of the text
-                QString lastTextLine = nameText.mid(line.textStart(), line.textLength());
+                QString lastTextLine = nameText.mid(line.textStart());
                 lastTextLine = m_customizedFontMetrics.elidedText(lastTextLine,
                                                                   Qt::ElideRight,
-                                                                  line.naturalTextWidth() - 1);
+                                                                  maxWidth);
                 const QString elidedText = nameText.left(line.textStart()) + lastTextLine;
                 nameTextInfo->staticText.setText(elidedText);
+
+                const qreal lastLineWidth = m_customizedFontMetrics.boundingRect(lastTextLine).width();
+                nameWidth = qMax(nameWidth, lastLineWidth);
             }
             break;
         }
@@ -1303,6 +1314,9 @@ void KStandardItemListWidget::closeRoleEditor()
         scene()->views()[0]->parentWidget()->setFocus();
     }
 
+    if (m_oldRoleEditor) {
+        m_oldRoleEditor->deleteLater();
+    }
     m_oldRoleEditor = m_roleEditor;
     m_roleEditor->hide();
     m_roleEditor = 0;