From: Frank Reininghaus Date: Sat, 26 May 2012 13:32:57 +0000 (+0200) Subject: Use an italic font for symbolic links X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/f061d2a6b3421d9b087c99161323d0df3a3b443e?ds=inline Use an italic font for symbolic links BUG: 298218 FIXED-IN: 4.9.0 --- diff --git a/src/kitemviews/kfileitemlistview.cpp b/src/kitemviews/kfileitemlistview.cpp index e52865aa6..b7f7455b4 100644 --- a/src/kitemviews/kfileitemlistview.cpp +++ b/src/kitemviews/kfileitemlistview.cpp @@ -401,6 +401,7 @@ void KFileItemListView::applyRolesToModel() roles.insert("iconName"); roles.insert("text"); roles.insert("isDir"); + roles.insert("isLink"); if (supportsItemExpanding()) { roles.insert("isExpanded"); roles.insert("isExpandable"); diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index 62a17710c..c99da383f 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -93,4 +93,12 @@ bool KFileItemListWidget::isHidden() const return data().value("text").toString().startsWith(QLatin1Char('.')); } +QFont KFileItemListWidget::customizedFont(const QFont& baseFont) const +{ + // The customized font should be italic if the file is a symbolic link. + QFont font(baseFont); + font.setItalic(data().value("isLink").toBool()); + return font; +} + #include "kfileitemlistwidget.moc" diff --git a/src/kitemviews/kfileitemlistwidget.h b/src/kitemviews/kfileitemlistwidget.h index bb9034bfa..b0d8e1cd7 100644 --- a/src/kitemviews/kfileitemlistwidget.h +++ b/src/kitemviews/kfileitemlistwidget.h @@ -47,6 +47,7 @@ public: protected: virtual bool isRoleRightAligned(const QByteArray& role) const; virtual bool isHidden() const; + virtual QFont customizedFont(const QFont& baseFont) const; }; #endif diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 62881e132..831493d80 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -77,8 +77,10 @@ KFileItemModel::KFileItemModel(QObject* parent) : resetRoles(); m_requestRole[NameRole] = true; m_requestRole[IsDirRole] = true; + m_requestRole[IsLinkRole] = true; m_roles.insert("text"); m_roles.insert("isDir"); + m_roles.insert("isLink"); // For slow KIO-slaves like used for searching it makes sense to show results periodically even // before the completed() or canceled() signal has been emitted. @@ -1116,6 +1118,7 @@ KFileItemModel::RoleType KFileItemModel::typeForRole(const QByteArray& role) con // Insert internal roles (take care to synchronize the implementation // with KFileItemModel::roleForType() in case if a change is done). roles.insert("isDir", IsDirRole); + roles.insert("isLink", IsLinkRole); roles.insert("isExpanded", IsExpandedRole); roles.insert("isExpandable", IsExpandableRole); roles.insert("expandedParentsCount", ExpandedParentsCountRole); @@ -1141,6 +1144,7 @@ QByteArray KFileItemModel::roleForType(RoleType roleType) const // Insert internal roles (take care to synchronize the implementation // with KFileItemModel::typeForRole() in case if a change is done). roles.insert(IsDirRole, "isDir"); + roles.insert(IsLinkRole, "isLink"); roles.insert(IsExpandedRole, "isExpanded"); roles.insert(IsExpandableRole, "isExpandable"); roles.insert(ExpandedParentsCountRole, "expandedParentsCount"); @@ -1164,6 +1168,11 @@ QHash KFileItemModel::retrieveData(const KFileItem& item) data.insert("isDir", isDir); } + if (m_requestRole[IsLinkRole]) { + const bool isLink = item.isLink(); + data.insert("isLink", isLink); + } + if (m_requestRole[NameRole]) { data.insert("text", item.text()); } diff --git a/src/kitemviews/kfileitemmodel.h b/src/kitemviews/kfileitemmodel.h index 783a0fecb..64359df2c 100644 --- a/src/kitemviews/kfileitemmodel.h +++ b/src/kitemviews/kfileitemmodel.h @@ -277,7 +277,7 @@ private: WordCountRole, LineCountRole, ArtistRole, AlbumRole, DurationRole, TrackRole, CopiedFromRole, // Non-visible roles: - IsDirRole, IsExpandedRole, IsExpandableRole, ExpandedParentsCountRole, + IsDirRole, IsLinkRole, IsExpandedRole, IsExpandableRole, ExpandedParentsCountRole, // Mandatory last entry: RolesCount }; diff --git a/src/kitemviews/kstandarditemlistwidget.cpp b/src/kitemviews/kstandarditemlistwidget.cpp index e85dfaabf..d72daeecc 100644 --- a/src/kitemviews/kstandarditemlistwidget.cpp +++ b/src/kitemviews/kstandarditemlistwidget.cpp @@ -172,6 +172,8 @@ KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant* infor KItemListWidget(informant, parent), m_isCut(false), m_isHidden(false), + m_customizedFont(), + m_customizedFontMetrics(m_customizedFont), m_isExpandable(false), m_supportsItemExpanding(false), m_dirtyLayout(true), @@ -258,7 +260,7 @@ void KStandardItemListWidget::paint(QPainter* painter, const QStyleOptionGraphic drawPixmap(painter, m_pixmap); } - painter->setFont(itemListStyleOption.font); + painter->setFont(m_customizedFont); painter->setPen(m_isHidden ? m_additionalInfoTextColor : textColor()); const TextInfo* textInfo = m_textInfo.value("text"); painter->drawStaticText(textInfo->pos, textInfo->staticText); @@ -277,7 +279,7 @@ void KStandardItemListWidget::paint(QPainter* painter, const QStyleOptionGraphic } painter->setPen(m_additionalInfoTextColor); - painter->setFont(itemListStyleOption.font); + painter->setFont(m_customizedFont); for (int i = 1; i < m_sortedVisibleRoles.count(); ++i) { const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles[i]); @@ -304,7 +306,7 @@ void KStandardItemListWidget::paint(QPainter* painter, const QStyleOptionGraphic painter->drawRect(m_iconRect); painter->setPen(Qt::red); - painter->drawText(QPointF(0, itemListStyleOption.fontMetrics.height()), QString::number(index())); + painter->drawText(QPointF(0, m_customizedFontMetrics.height()), QString::number(index())); painter->drawRect(rect()); #endif } @@ -349,7 +351,7 @@ QRectF KStandardItemListWidget::textFocusRect() const const KItemListStyleOption& option = styleOption(); if (option.extendedSelectionRegion) { const QString text = textInfo->staticText.text(); - rect.setWidth(option.fontMetrics.width(text) + 2 * option.padding); + rect.setWidth(m_customizedFontMetrics.width(text) + 2 * option.padding); } return rect; @@ -457,6 +459,11 @@ bool KStandardItemListWidget::isHidden() const return false; } +QFont KStandardItemListWidget::customizedFont(const QFont& baseFont) const +{ + return baseFont; +} + void KStandardItemListWidget::setTextColor(const QColor& color) { if (color != m_customTextColor) { @@ -707,6 +714,8 @@ void KStandardItemListWidget::triggerCacheRefreshing() const QHash values = data(); m_isExpandable = m_supportsItemExpanding && values["isExpandable"].toBool(); m_isHidden = isHidden(); + m_customizedFont = customizedFont(styleOption().font); + m_customizedFontMetrics = QFontMetrics(m_customizedFont); updateExpansionArea(); updateTextsCache(); @@ -820,7 +829,7 @@ void KStandardItemListWidget::updatePixmapCache() scaledIconSize = static_cast(textInfo->pos.y() - 2 * padding); } else { const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1; - const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height(); + const qreal requiredTextHeight = textRowsCount * m_customizedFontMetrics.height(); scaledIconSize = (requiredTextHeight < maxIconHeight) ? widgetSize.height() - 2 * padding : maxIconHeight; } @@ -940,7 +949,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache() const qreal padding = option.padding; const qreal maxWidth = size().width() - 2 * padding; const qreal widgetHeight = size().height(); - const qreal lineSpacing = option.fontMetrics.lineSpacing(); + const qreal lineSpacing = m_customizedFontMetrics.lineSpacing(); // Initialize properties for the "text" role. It will be used as anchor // for initializing the position of the other roles. @@ -956,7 +965,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache() const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0); const int maxNameLines = (option.maxTextSize.height() / int(lineSpacing)) - additionalRolesCount; - QTextLayout layout(nameTextInfo->staticText.text(), option.font); + QTextLayout layout(nameTextInfo->staticText.text(), m_customizedFont); layout.setTextOption(nameTextInfo->staticText.textOption()); layout.beginLayout(); int nameLineIndex = 0; @@ -973,9 +982,9 @@ void KStandardItemListWidget::updateIconsLayoutTextCache() 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); + lastTextLine = m_customizedFontMetrics.elidedText(lastTextLine, + Qt::ElideRight, + line.naturalTextWidth() - 1); const QString elidedText = nameText.left(line.textStart()) + lastTextLine; nameTextInfo->staticText.setText(elidedText); } @@ -1008,7 +1017,7 @@ void KStandardItemListWidget::updateIconsLayoutTextCache() qreal requiredWidth = 0; - QTextLayout layout(text, option.font); + QTextLayout layout(text, m_customizedFont); QTextOption textOption; textOption.setWrapMode(QTextOption::NoWrap); layout.setTextOption(textOption); @@ -1019,9 +1028,9 @@ void KStandardItemListWidget::updateIconsLayoutTextCache() textLine.setLineWidth(maxWidth); requiredWidth = textLine.naturalTextWidth(); if (requiredWidth > maxWidth) { - const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth); + const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, maxWidth); textInfo->staticText.setText(elidedText); - requiredWidth = option.fontMetrics.width(elidedText); + requiredWidth = m_customizedFontMetrics.width(elidedText); } } layout.endLayout(); @@ -1049,7 +1058,7 @@ void KStandardItemListWidget::updateCompactLayoutTextCache() const KItemListStyleOption& option = styleOption(); const qreal widgetHeight = size().height(); - const qreal lineSpacing = option.fontMetrics.lineSpacing(); + const qreal lineSpacing = m_customizedFontMetrics.lineSpacing(); const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * lineSpacing; const int scaledIconSize = (textLinesHeight < option.iconSize) ? widgetHeight - 2 * option.padding : option.iconSize; @@ -1062,10 +1071,10 @@ void KStandardItemListWidget::updateCompactLayoutTextCache() TextInfo* textInfo = m_textInfo.value(role); textInfo->staticText.setText(text); - qreal requiredWidth = option.fontMetrics.width(text); + qreal requiredWidth = m_customizedFontMetrics.width(text); if (requiredWidth > maxWidth) { requiredWidth = maxWidth; - const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth); + const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, maxWidth); textInfo->staticText.setText(elidedText); } @@ -1095,7 +1104,7 @@ void KStandardItemListWidget::updateDetailsLayoutTextCache() const qreal widgetHeight = size().height(); const int scaledIconSize = widgetHeight - 2 * option.padding; - const int fontHeight = option.fontMetrics.height(); + const int fontHeight = m_customizedFontMetrics.height(); const qreal columnWidthInc = columnPadding(option); qreal firstColumnInc = scaledIconSize; @@ -1112,7 +1121,7 @@ void KStandardItemListWidget::updateDetailsLayoutTextCache() QString text = roleText(role, values); // Elide the text in case it does not fit into the available column-width - qreal requiredWidth = option.fontMetrics.width(text); + qreal requiredWidth = m_customizedFontMetrics.width(text); const qreal roleWidth = columnWidth(role); qreal availableTextWidth = roleWidth - columnWidthInc; @@ -1122,8 +1131,8 @@ void KStandardItemListWidget::updateDetailsLayoutTextCache() } if (requiredWidth > availableTextWidth) { - text = option.fontMetrics.elidedText(text, Qt::ElideRight, availableTextWidth); - requiredWidth = option.fontMetrics.width(text); + text = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, availableTextWidth); + requiredWidth = m_customizedFontMetrics.width(text); } TextInfo* textInfo = m_textInfo.value(role); diff --git a/src/kitemviews/kstandarditemlistwidget.h b/src/kitemviews/kstandarditemlistwidget.h index 72c081b31..76b0d143c 100644 --- a/src/kitemviews/kstandarditemlistwidget.h +++ b/src/kitemviews/kstandarditemlistwidget.h @@ -115,6 +115,11 @@ protected: */ virtual bool isHidden() const; + /** + * @return A font based on baseFont which is customized according to the data shown in the widget. + */ + virtual QFont customizedFont(const QFont& baseFont) const; + void setTextColor(const QColor& color); QColor textColor() const; @@ -178,6 +183,8 @@ private: private: bool m_isCut; bool m_isHidden; + QFont m_customizedFont; + QFontMetrics m_customizedFontMetrics; bool m_isExpandable; bool m_supportsItemExpanding; diff --git a/src/tests/kfileitemmodeltest.cpp b/src/tests/kfileitemmodeltest.cpp index 7b56957d4..e6bcb906e 100644 --- a/src/tests/kfileitemmodeltest.cpp +++ b/src/tests/kfileitemmodeltest.cpp @@ -113,9 +113,10 @@ void KFileItemModelTest::cleanup() void KFileItemModelTest::testDefaultRoles() { const QSet roles = m_model->roles(); - QCOMPARE(roles.count(), 2); + QCOMPARE(roles.count(), 3); QVERIFY(roles.contains("text")); QVERIFY(roles.contains("isDir")); + QVERIFY(roles.contains("isLink")); } void KFileItemModelTest::testDefaultSortRole()