┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kitemviews
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
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')
-rw-r--r--src/kitemviews/kfileitemlistwidget.cpp47
-rw-r--r--src/kitemviews/kitemliststyleoption.cpp6
-rw-r--r--src/kitemviews/kitemliststyleoption.h1
-rw-r--r--src/kitemviews/kitemlistview.cpp27
4 files changed, 60 insertions, 21 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 -
diff --git a/src/kitemviews/kitemliststyleoption.cpp b/src/kitemviews/kitemliststyleoption.cpp
index c512fa43e..36cfeb088 100644
--- a/src/kitemviews/kitemliststyleoption.cpp
+++ b/src/kitemviews/kitemliststyleoption.cpp
@@ -30,7 +30,8 @@ KItemListStyleOption::KItemListStyleOption() :
horizontalMargin(0),
verticalMargin(0),
iconSize(KIconLoader::SizeMedium),
- extendedSelectionRegion(false)
+ extendedSelectionRegion(false),
+ maxTextSize()
{
}
@@ -43,7 +44,8 @@ KItemListStyleOption::KItemListStyleOption(const KItemListStyleOption& other) :
horizontalMargin(other.horizontalMargin),
verticalMargin(other.verticalMargin),
iconSize(other.iconSize),
- extendedSelectionRegion(other.extendedSelectionRegion)
+ extendedSelectionRegion(other.extendedSelectionRegion),
+ maxTextSize(other.maxTextSize)
{
}
diff --git a/src/kitemviews/kitemliststyleoption.h b/src/kitemviews/kitemliststyleoption.h
index 62441ef4b..1a304fc28 100644
--- a/src/kitemviews/kitemliststyleoption.h
+++ b/src/kitemviews/kitemliststyleoption.h
@@ -43,6 +43,7 @@ public:
int verticalMargin;
int iconSize;
bool extendedSelectionRegion;
+ QSize maxTextSize;
};
#endif
diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp
index 732ed24e4..c62523410 100644
--- a/src/kitemviews/kitemlistview.cpp
+++ b/src/kitemviews/kitemlistview.cpp
@@ -148,10 +148,10 @@ Qt::Orientation KItemListView::scrollOrientation() const
return m_layouter->scrollOrientation();
}
-void KItemListView::setItemSize(const QSizeF& itemSize)
+void KItemListView::setItemSize(const QSizeF& size)
{
const QSizeF previousSize = m_itemSize;
- if (itemSize == previousSize) {
+ if (size == previousSize) {
return;
}
@@ -159,14 +159,14 @@ void KItemListView::setItemSize(const QSizeF& itemSize)
// are changed in the grid layout. Although the animation
// engine can handle this usecase, it looks obtrusive.
const bool animate = !changesItemGridLayout(m_layouter->size(),
- itemSize,
+ size,
m_layouter->itemMargin());
const bool alternateBackgroundsChanged = (m_visibleRoles.count() > 1) &&
- (( m_itemSize.isEmpty() && !itemSize.isEmpty()) ||
- (!m_itemSize.isEmpty() && itemSize.isEmpty()));
+ (( m_itemSize.isEmpty() && !size.isEmpty()) ||
+ (!m_itemSize.isEmpty() && size.isEmpty()));
- m_itemSize = itemSize;
+ m_itemSize = size;
if (alternateBackgroundsChanged) {
// For an empty item size alternate backgrounds are drawn if more than
@@ -175,23 +175,23 @@ void KItemListView::setItemSize(const QSizeF& itemSize)
updateAlternateBackgrounds();
}
- if (itemSize.isEmpty()) {
+ if (size.isEmpty()) {
if (m_headerWidget->automaticColumnResizing()) {
updatePreferredColumnWidths();
} else {
// Only apply the changed height and respect the header widths
// set by the user
const qreal currentWidth = m_layouter->itemSize().width();
- const QSizeF newSize(currentWidth, itemSize.height());
+ const QSizeF newSize(currentWidth, size.height());
m_layouter->setItemSize(newSize);
}
} else {
- m_layouter->setItemSize(itemSize);
+ m_layouter->setItemSize(size);
}
m_sizeHintResolver->clearCache();
doLayout(animate ? Animation : NoAnimation);
- onItemSizeChanged(itemSize, previousSize);
+ onItemSizeChanged(size, previousSize);
}
QSizeF KItemListView::itemSize() const
@@ -392,6 +392,12 @@ void KItemListView::setStyleOption(const KItemListStyleOption& option)
updateGroupHeaderHeight();
}
+ if (animate && previousOption.maxTextSize != option.maxTextSize) {
+ // 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;
+ }
+
QHashIterator<int, KItemListWidget*> it(m_visibleItems);
while (it.hasNext()) {
it.next();
@@ -399,6 +405,7 @@ void KItemListView::setStyleOption(const KItemListStyleOption& option)
}
m_sizeHintResolver->clearCache();
+ m_layouter->markAsDirty();
doLayout(animate ? Animation : NoAnimation);
onStyleOptionChanged(option, previousOption);