┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMéven Car <[email protected]>2020-05-16 09:59:54 +0200
committerMéven Car <[email protected]>2020-05-18 08:14:47 +0200
commit99cf24c03def1c0722ba8dbd86a27b9dbc521f43 (patch)
treef034e45599047de861db1891292ef4665acb238b
parent57d9eee90035f5d408f324c89c184efaddb9414b (diff)
Left-elide file/folders while keeping their extension visible
Summary: Tweak behavior introduced in D19471. BUG: 404955 Test Plan: Before: {F8325282} After: {F8325283} {F8325284} Reviewers: ngraham, #dolphin, elvisangelaccio, #vdg Reviewed By: ngraham, #dolphin, elvisangelaccio, #vdg Subscribers: kfm-devel Tags: #dolphin Differential Revision: https://phabricator.kde.org/D29794
-rw-r--r--src/kitemviews/kstandarditemlistwidget.cpp30
-rw-r--r--src/kitemviews/kstandarditemlistwidget.h2
2 files changed, 26 insertions, 6 deletions
diff --git a/src/kitemviews/kstandarditemlistwidget.cpp b/src/kitemviews/kstandarditemlistwidget.cpp
index 4bbbf17a8..13f80996d 100644
--- a/src/kitemviews/kstandarditemlistwidget.cpp
+++ b/src/kitemviews/kstandarditemlistwidget.cpp
@@ -1113,6 +1113,26 @@ void KStandardItemListWidget::updateTextsCache()
}
}
+QString KStandardItemListWidget::elideRightKeepExtension(const QString &text, int elidingWidth) const
+{
+ auto extensionIndex = text.lastIndexOf('.');
+ if (extensionIndex != -1) {
+ // has file extension
+ auto extensionLength = text.length() - extensionIndex;
+ auto extensionWidth = m_customizedFontMetrics.width(text.right(extensionLength));
+ if (elidingWidth > extensionWidth && extensionLength < 6 && (float(extensionWidth) / float(elidingWidth)) < 0.3) {
+ // if we have room to display the file extension and the extension is not too long
+ QString ret = m_customizedFontMetrics.elidedText(text.chopped(extensionLength),
+ Qt::ElideRight,
+ elidingWidth - extensionWidth);
+ ret.append(text.right(extensionLength));
+ return ret;
+ }
+ }
+ return m_customizedFontMetrics.elidedText(text,Qt::ElideRight,
+ elidingWidth);
+}
+
void KStandardItemListWidget::updateIconsLayoutTextCache()
{
// +------+
@@ -1164,9 +1184,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache()
qreal lastLineWidth;
do {
QString lastTextLine = nameText.mid(line.textStart());
- lastTextLine = m_customizedFontMetrics.elidedText(lastTextLine,
- Qt::ElideMiddle,
- elidingWidth);
+ lastTextLine = elideRightKeepExtension(lastTextLine, elidingWidth);
const QString elidedText = nameText.left(line.textStart()) + lastTextLine;
nameTextInfo->staticText.setText(elidedText);
@@ -1221,7 +1239,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache()
textLine.setLineWidth(maxWidth);
requiredWidth = textLine.naturalTextWidth();
if (requiredWidth > maxWidth) {
- const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideMiddle, maxWidth);
+ const QString elidedText = elideRightKeepExtension(text, maxWidth);
textInfo->staticText.setText(elidedText);
requiredWidth = m_customizedFontMetrics.width(elidedText);
} else if (role == "rating") {
@@ -1270,7 +1288,7 @@ void KStandardItemListWidget::updateCompactLayoutTextCache()
qreal requiredWidth = m_customizedFontMetrics.width(text);
if (requiredWidth > maxWidth) {
requiredWidth = maxWidth;
- const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideMiddle, maxWidth);
+ const QString elidedText = elideRightKeepExtension(text, maxWidth);
textInfo->staticText.setText(elidedText);
}
@@ -1327,7 +1345,7 @@ void KStandardItemListWidget::updateDetailsLayoutTextCache()
}
if (requiredWidth > availableTextWidth) {
- text = m_customizedFontMetrics.elidedText(text, Qt::ElideMiddle, availableTextWidth);
+ text = elideRightKeepExtension(text, availableTextWidth);
requiredWidth = m_customizedFontMetrics.width(text);
}
diff --git a/src/kitemviews/kstandarditemlistwidget.h b/src/kitemviews/kstandarditemlistwidget.h
index c8102e421..e7f2de2e0 100644
--- a/src/kitemviews/kstandarditemlistwidget.h
+++ b/src/kitemviews/kstandarditemlistwidget.h
@@ -209,6 +209,8 @@ private:
QRectF roleEditingRect(const QByteArray &role) const;
+ QString elideRightKeepExtension(const QString &text, int elidingWidth) const;
+
/**
* Closes the role editor and returns the focus back
* to the KItemListContainer.