]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Make the handling of the "maximum text lines" setting more robust
authorFrank Reininghaus <frank78ac@googlemail.com>
Mon, 24 Feb 2014 20:05:09 +0000 (21:05 +0100)
committerFrank Reininghaus <frank78ac@googlemail.com>
Mon, 24 Feb 2014 20:05:09 +0000 (21:05 +0100)
If the user sets a maximum number of text lines in the settings, this
number was translated into a maximum height in pixels using
QFontMetrics::lineSpacing() before this commit.

In KStandardItemListWidgetInformant::itemSizeHint(), this maximum height
limited the size that is reserved for the item.

However, in KStandardItemListWidget::updateIconsLayoutTextCache(), the
maximum height was translated back into a maximum number of lines,
which limits the number of lines that are created using the QTextLayout.

This approach could lead to problems if the real height of the layouted
text is 1 pixel more or less than QFontMetrics::lineSpacing() times
"number of lines".

Now we do not store a "maximum height" inside the "maximum size"
explicitly, but store a maximum number of lines and a maximum with (for
Compact View) separately, and then use the number of lines also to
calculate the required size in
KStandardItemListWidgetInformant::itemSizeHint(). This should make sure
that the correct height is reserved for each item.

Thanks to Christoph Feck and Emmanuel Pescosta for helping to debug this
problem and testing the patch.

BUG: 323841
FIXED-IN: 4.13
REVIEW: 113871

src/kitemviews/kitemliststyleoption.cpp
src/kitemviews/kitemliststyleoption.h
src/kitemviews/kitemlistview.cpp
src/kitemviews/kstandarditemlistwidget.cpp
src/views/dolphinitemlistview.cpp

index ac2587962e847791788e55cdbdf78439022abf25..edd6363c852195c13f3e46514d1104907eb60cb6 100644 (file)
@@ -31,7 +31,8 @@ KItemListStyleOption::KItemListStyleOption() :
     verticalMargin(-1),
     iconSize(-1),
     extendedSelectionRegion(false),
-    maxTextSize()
+    maxTextLines(0),
+    maxTextWidth(0)
 {
 }
 
@@ -45,7 +46,8 @@ KItemListStyleOption::KItemListStyleOption(const KItemListStyleOption& other) :
     verticalMargin(other.verticalMargin),
     iconSize(other.iconSize),
     extendedSelectionRegion(other.extendedSelectionRegion),
-    maxTextSize(other.maxTextSize)
+    maxTextLines(other.maxTextLines),
+    maxTextWidth(other.maxTextWidth)
 {
 }
 
index 1a304fc28f52ab6f478a16dc4576f6f423c92a5f..782dd0ec2f93129f01568871601bca8f1df216c1 100644 (file)
@@ -43,7 +43,8 @@ public:
     int verticalMargin;
     int iconSize;
     bool extendedSelectionRegion;
-    QSize maxTextSize;
+    int maxTextLines;
+    int maxTextWidth;
 };
 #endif
 
index ab420cab15c05b6b1418e6663b14810924fc6095..f1b35fa53527a879d94a1cc81a5e2d546bbce497 100644 (file)
@@ -760,7 +760,8 @@ void KItemListView::setStyleOption(const KItemListStyleOption& option)
         updateGroupHeaderHeight();
     }
 
-    if (animate && previousOption.maxTextSize != option.maxTextSize) {
+    if (animate &&
+        (previousOption.maxTextLines != option.maxTextLines || previousOption.maxTextWidth != option.maxTextWidth)) {
         // Animating a change of the maximum text size just results in expensive
         // temporary eliding and clipping operations and does not look good visually.
         animate = false;
index 9a9a734ed058e5c158a6745d2a920b2402639083..9f7b26959f2b0eca553e32282221f9faad6834c6 100644 (file)
@@ -130,7 +130,6 @@ void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
 
     const qreal itemWidth = view->itemSize().width();
     const qreal maxWidth = itemWidth - 2 * option.padding;
-    const qreal maxTextHeight = option.maxTextSize.height();
     const qreal additionalRolesSpacing = additionalRolesCount * option.fontMetrics.lineSpacing();
     const qreal spacingAndIconHeight = option.iconSize + option.padding * 3;
 
@@ -150,20 +149,22 @@ void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
         layout.setTextOption(textOption);
         layout.beginLayout();
         QTextLine line;
+        int lineCount = 0;
         while ((line = layout.createLine()).isValid()) {
             line.setLineWidth(maxWidth);
             line.naturalTextWidth();
             textHeight += line.height();
+
+            ++lineCount;
+            if (lineCount == option.maxTextLines) {
+                break;
+            }
         }
         layout.endLayout();
 
         // Add one line for each additional information
         textHeight += additionalRolesSpacing;
 
-        if (maxTextHeight > 0 && textHeight > maxTextHeight) {
-            textHeight = maxTextHeight;
-        }
-
         sizeHints[index] = QSizeF(itemWidth, textHeight + spacingAndIconHeight);
     }
 }
@@ -176,7 +177,7 @@ void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVect
 
     const QList<QByteArray>& visibleRoles = view->visibleRoles();
     const bool showOnlyTextRole = (visibleRoles.count() == 1) && (visibleRoles.first() == "text");
-    const qreal maxWidth = option.maxTextSize.width();
+    const qreal maxWidth = option.maxTextWidth;
     const qreal paddingAndIconWidth = option.padding * 4 + option.iconSize;
     const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.lineSpacing());
 
@@ -1066,9 +1067,6 @@ void KStandardItemListWidget::updateIconsLayoutTextCache()
     qreal nameHeight = 0;
     QTextLine line;
 
-    const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
-    const int maxNameLines = (option.maxTextSize.height() / int(lineSpacing)) - additionalRolesCount;
-
     QTextLayout layout(nameTextInfo->staticText.text(), m_customizedFont);
     layout.setTextOption(nameTextInfo->staticText.textOption());
     layout.beginLayout();
@@ -1079,7 +1077,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache()
         nameHeight += line.height();
 
         ++nameLineIndex;
-        if (nameLineIndex == maxNameLines) {
+        if (nameLineIndex == option.maxTextLines) {
             // The maximum number of textlines has been reached. If this is
             // the case provide an elided text if necessary.
             const int textLength = line.textStart() + line.textLength();
@@ -1101,6 +1099,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache()
     layout.endLayout();
 
     // Use one line for each additional information
+    const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
     nameTextInfo->staticText.setTextWidth(maxWidth);
     nameTextInfo->pos = QPointF(padding, widgetHeight -
                                          nameHeight -
index eb61ccb21fa4986eff79fe6645379b0d1f473ff5..db4dadf2fee04cd1affb07ed7e94396aea13b068 100644 (file)
@@ -180,7 +180,8 @@ void DolphinItemListView::updateGridSize()
     // Calculate the item-width and item-height
     int itemWidth;
     int itemHeight;
-    QSize maxTextSize;
+    int maxTextLines = 0;
+    int maxTextWidth = 0;
 
     switch (itemLayout()) {
     case KFileItemListView::IconsLayout: {
@@ -200,16 +201,10 @@ void DolphinItemListView::updateGridSize()
         }
 
         itemHeight = padding * 3 + iconSize + option.fontMetrics.lineSpacing();
-        if (IconsModeSettings::maximumTextLines() > 0) {
-            // A restriction is given for the maximum number of textlines (0 means
-            // having no restriction)
-            const int additionalInfoCount = visibleRoles().count() - 1;
-            const int maxAdditionalLines = additionalInfoCount + IconsModeSettings::maximumTextLines();
-            maxTextSize.rheight() = option.fontMetrics.lineSpacing() * maxAdditionalLines;
-        }
 
         horizontalMargin = 4;
         verticalMargin = 8;
+        maxTextLines = IconsModeSettings::maximumTextLines();
         break;
     }
     case KFileItemListView::CompactLayout: {
@@ -220,8 +215,7 @@ void DolphinItemListView::updateGridSize()
         if (CompactModeSettings::maximumTextWidthIndex() > 0) {
             // A restriction is given for the maximum width of the text (0 means
             // having no restriction)
-            maxTextSize.rwidth() = option.fontMetrics.height() * 10 *
-                                   CompactModeSettings::maximumTextWidthIndex();
+            maxTextWidth = option.fontMetrics.height() * 10 * CompactModeSettings::maximumTextWidthIndex();
         }
 
         horizontalMargin = 8;
@@ -244,7 +238,8 @@ void DolphinItemListView::updateGridSize()
     option.horizontalMargin = horizontalMargin;
     option.verticalMargin = verticalMargin;
     option.iconSize = iconSize;
-    option.maxTextSize = maxTextSize;
+    option.maxTextLines = maxTextLines;
+    option.maxTextWidth = maxTextWidth;
     beginTransaction();
     setStyleOption(option);
     setItemSize(QSizeF(itemWidth, itemHeight));