┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kitemviews/kfileitemlistwidget.cpp
diff options
context:
space:
mode:
authorPeter Penz <[email protected]>2012-04-14 00:22:08 +0200
committerPeter Penz <[email protected]>2012-04-14 00:28:24 +0200
commit60b868108151463a702e8c10933b0ceb99f11bbe (patch)
tree97b5a6aeca8df6b121674d38b11ea69b11b7878a /src/kitemviews/kfileitemlistwidget.cpp
parent31ee4085c2b2c374158fb956ac376399ff375b5a (diff)
Allow to optionally limit the maximum number of text lines
Showing the whole filename unclipped seems to be a good default, however for users with a lot of files that have extremely long names this might get a problem especially in the icons-view. - Allow to limit the maximum number of lines in the icons-view - Allow to specify a maximum width in the compact-view (No limit is required for the details-view, as the name is shortened automatically to show other columns) BUG: 288596 FIXED-IN: 4.9.0
Diffstat (limited to 'src/kitemviews/kfileitemlistwidget.cpp')
-rw-r--r--src/kitemviews/kfileitemlistwidget.cpp47
1 files changed, 38 insertions, 9 deletions
diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp
index f3b4da892..5c5690c40 100644
--- a/src/kitemviews/kfileitemlistwidget.cpp
+++ b/src/kitemviews/kfileitemlistwidget.cpp
@@ -279,7 +279,8 @@ QSizeF KFileItemListWidget::itemSizeHint(int index, const KItemListView* view)
case IconsLayout: {
const QString text = KStringHandler::preProcessWrap(values["name"].toString());
- const qreal maxWidth = view->itemSize().width() - 2 * option.padding;
+ const qreal itemWidth = view->itemSize().width();
+ const qreal maxWidth = itemWidth - 2 * option.padding;
QTextLine line;
// Calculate the number of lines required for wrapping the name
@@ -298,11 +299,14 @@ QSizeF KFileItemListWidget::itemSizeHint(int index, const KItemListView* view)
layout.endLayout();
// Add one line for each additional information
- const qreal height = textHeight +
- additionalRolesCount * option.fontMetrics.lineSpacing() +
- option.iconSize +
- option.padding * 3;
- return QSizeF(view->itemSize().width(), height);
+ textHeight += additionalRolesCount * option.fontMetrics.lineSpacing();
+
+ const qreal maxTextHeight = option.maxTextSize.height();
+ if (maxTextHeight > 0 && textHeight > maxTextHeight) {
+ textHeight = maxTextHeight;
+ }
+
+ return QSizeF(itemWidth, textHeight + option.iconSize + option.padding * 3);
}
case CompactLayout: {
@@ -316,7 +320,11 @@ QSizeF KFileItemListWidget::itemSizeHint(int index, const KItemListView* view)
maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
}
- const qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
+ qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
+ const qreal maxWidth = option.maxTextSize.width();
+ if (maxWidth > 0 && width > maxWidth) {
+ width = maxWidth;
+ }
const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.lineSpacing());
return QSizeF(width, height);
}
@@ -756,25 +764,46 @@ void KFileItemListWidget::updateIconsLayoutTextCache()
// Initialize properties for the "name" role. It will be used as anchor
// for initializing the position of the other roles.
TextInfo* nameTextInfo = m_textInfo.value("name");
- nameTextInfo->staticText.setText(KStringHandler::preProcessWrap(values["name"].toString()));
+ const QString nameText = KStringHandler::preProcessWrap(values["name"].toString());
+ nameTextInfo->staticText.setText(nameText);
// Calculate the number of lines required for the name and the required width
qreal nameWidth = 0;
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(), option.font);
layout.setTextOption(nameTextInfo->staticText.textOption());
layout.beginLayout();
+ int nameLineIndex = 0;
while ((line = layout.createLine()).isValid()) {
line.setLineWidth(maxWidth);
nameWidth = qMax(nameWidth, line.naturalTextWidth());
nameHeight += line.height();
+
+ ++nameLineIndex;
+ if (nameLineIndex == maxNameLines) {
+ // 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();
+ if (textLength < nameText.length()) {
+ // Elide the last line of the text
+ QString lastTextLine = nameText.mid(line.textStart(), line.textLength());
+ lastTextLine = option.fontMetrics.elidedText(lastTextLine,
+ Qt::ElideRight,
+ line.naturalTextWidth() - 1);
+ const QString elidedText = nameText.left(line.textStart()) + lastTextLine;
+ nameTextInfo->staticText.setText(elidedText);
+ }
+ break;
+ }
}
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 -