]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemlistwidget.cpp
Remove toolbar-spacer if the menu has been disabled
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
index 80a3a318370eb355b6637446380781948e058e66..af7ea1523d74c9727e6a42f5cc86c3618050a8eb 100644 (file)
@@ -28,6 +28,7 @@
 #include <KIconEffect>
 #include <KIconLoader>
 #include <KLocale>
+#include <kratingpainter.h>
 #include <KStringHandler>
 #include <KDebug>
 
@@ -61,7 +62,8 @@ KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) :
     m_expansionArea(),
     m_customTextColor(),
     m_additionalInfoTextColor(),
-    m_overlay()
+    m_overlay(),
+    m_rating()
 {
 }
 
@@ -152,6 +154,16 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte
         painter->drawStaticText(textInfo->pos, textInfo->staticText);
     }
 
+    if (!m_rating.isNull()) {
+        const TextInfo* ratingTextInfo = m_textInfo.value("rating");
+        QPointF pos = ratingTextInfo->pos;
+        const Qt::Alignment align = ratingTextInfo->staticText.textOption().alignment();
+        if (align & Qt::AlignHCenter) {
+            pos.rx() += (size().width() - m_rating.width()) / 2;
+        }
+        painter->drawPixmap(pos, m_rating);
+    }
+
     if (clipAdditionalInfoBounds) {
         painter->restore();
     }
@@ -309,24 +321,26 @@ qreal KFileItemListWidget::preferredRoleColumnWidth(const QByteArray& role,
                                                     int index,
                                                     const KItemListView* view)
 {
-    qreal width = 0;
 
     const QHash<QByteArray, QVariant> values = view->model()->data(index);
     const KItemListStyleOption& option = view->styleOption();
 
     const QString text = KFileItemListWidget::roleText(role, values);
-    if (!text.isEmpty()) {
-        const qreal columnPadding = option.padding * 3;
-        width = qMax(width, qreal(2 * columnPadding + option.fontMetrics.width(text)));
-    }
+    qreal width = columnPadding(option);
 
-    if (role == "name") {
-        // Increase the width by the expansion-toggle and the current expansion level
-        const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
-        width += option.padding + (expandedParentsCount + 1) * view->itemSize().height() + KIconLoader::SizeSmall;
+    if (role == "rating") {
+        width += preferredRatingSize(option).width();
+    } else {
+        width += option.fontMetrics.width(text);
 
-        // Increase the width by the required space for the icon
-        width += option.padding * 2 + option.iconSize;
+        if (role == "name") {
+            // Increase the width by the expansion-toggle and the current expansion level
+            const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
+            width += option.padding + (expandedParentsCount + 1) * view->itemSize().height() + KIconLoader::SizeSmall;
+
+            // Increase the width by the required space for the icon
+            width += option.padding * 2 + option.iconSize;
+        }
     }
 
     return width;
@@ -676,6 +690,30 @@ void KFileItemListWidget::updateTextsCache()
     case DetailsLayout: updateDetailsLayoutTextCache(); break;
     default: Q_ASSERT(false); break;
     }
+
+    const TextInfo* ratingTextInfo = m_textInfo.value("rating");
+    if (ratingTextInfo) {
+        // The text of the rating-role has been set to empty to get
+        // replaced by a rating-image showing the rating as stars.
+        const KItemListStyleOption& option = styleOption();
+        QSizeF ratingSize = preferredRatingSize(option);
+
+        const qreal availableWidth = (m_layout == DetailsLayout)
+                                     ? columnWidth("rating") - columnPadding(option)
+                                     : m_textRect.width();
+        if (ratingSize.width() > availableWidth) {
+            ratingSize.rwidth() = availableWidth;
+        }
+        m_rating = QPixmap(ratingSize.toSize());
+        m_rating.fill(Qt::transparent);
+
+        QPainter painter(&m_rating);
+        const QRect rect(0, 0, m_rating.width(), m_rating.height());
+        const int rating = data().value("rating").toInt();
+        KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating);
+    } else if (!m_rating.isNull()) {
+        m_rating = QPixmap();
+    }
 }
 
 void KFileItemListWidget::updateIconsLayoutTextCache()
@@ -744,19 +782,19 @@ void KFileItemListWidget::updateIconsLayoutTextCache()
         qreal requiredWidth = 0;
 
         QTextLayout layout(text, option.font);
-        layout.setTextOption(textInfo->staticText.textOption());
+        QTextOption textOption;
+        textOption.setWrapMode(QTextOption::NoWrap);
+        layout.setTextOption(textOption);
+
         layout.beginLayout();
         QTextLine textLine = layout.createLine();
         if (textLine.isValid()) {
             textLine.setLineWidth(maxWidth);
             requiredWidth = textLine.naturalTextWidth();
-            if (textLine.textLength() < text.length()) {
-                // TODO: QFontMetrics::elidedText() works different regarding the given width
-                // in comparison to QTextLine::setLineWidth(). It might happen that the text does
-                // not get elided although it does not fit into the given width. As workaround
-                // the padding is substracted.
-                const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - padding);
+            if (requiredWidth > maxWidth) {
+                const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
                 textInfo->staticText.setText(elidedText);
+                requiredWidth = option.fontMetrics.width(elidedText);
             }
         }
         layout.endLayout();
@@ -832,7 +870,7 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
     const int scaledIconSize = widgetHeight - 2 * option.padding;
     const int fontHeight = option.fontMetrics.height();
 
-    const qreal columnPadding = option.padding * 3;
+    const qreal columnWidthInc = columnPadding(option);
     qreal firstColumnInc = scaledIconSize;
     if (m_supportsItemExpanding) {
         firstColumnInc += (m_expansionArea.left() + m_expansionArea.right() + widgetHeight) / 2;
@@ -851,7 +889,7 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
         // Elide the text in case it does not fit into the available column-width
         qreal requiredWidth = option.fontMetrics.width(text);
         const qreal roleWidth = columnWidth(role);
-        qreal availableTextWidth = roleWidth - 2 * columnPadding;
+        qreal availableTextWidth = roleWidth - columnWidthInc;
         if (type == Name) {
             availableTextWidth -= firstColumnInc;
         }
@@ -863,7 +901,7 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
 
         TextInfo* textInfo = m_textInfo.value(role);
         textInfo->staticText.setText(text);
-        textInfo->pos = QPointF(x + columnPadding, y);
+        textInfo->pos = QPointF(x + columnWidthInc / 2, y);
         x += roleWidth;
 
         switch (type) {
@@ -881,7 +919,7 @@ void KFileItemListWidget::updateDetailsLayoutTextCache()
         }
         case Size:
             // The values for the size should be right aligned
-            textInfo->pos.rx() += roleWidth - requiredWidth - 2 * columnPadding;
+            textInfo->pos.rx() += roleWidth - requiredWidth - columnWidthInc;
             break;
 
         default:
@@ -1008,6 +1046,7 @@ KFileItemListWidget::RoleType KFileItemListWidget::roleType(const QByteArray& ro
         rolesHash.insert("name", Name);
         rolesHash.insert("size", Size);
         rolesHash.insert("date", Date);
+        rolesHash.insert("rating", Rating);
     }
 
     return rolesHash.value(role, Generic);
@@ -1047,6 +1086,10 @@ QString KFileItemListWidget::roleText(const QByteArray& role, const QHash<QByteA
         break;
     }
 
+    case Rating:
+        // Always use an empty text, as the rating is shown by the image m_rating.
+        break;
+
     case Name:
     case Generic:
         text = roleValue.toString();
@@ -1059,4 +1102,16 @@ QString KFileItemListWidget::roleText(const QByteArray& role, const QHash<QByteA
 
     return text;
 }
+
+QSizeF KFileItemListWidget::preferredRatingSize(const KItemListStyleOption& option)
+{
+    const qreal height = option.fontMetrics.ascent();
+    return QSizeF(height * 5, height);
+}
+
+qreal KFileItemListWidget::columnPadding(const KItemListStyleOption& option)
+{
+    return option.padding * 6;
+}
+
 #include "kfileitemlistwidget.moc"