m_scaledPixmapSize(),
m_iconRect(),
m_hoverPixmap(),
- m_textPos(),
- m_text(),
+ m_textInfo(),
m_textRect(),
m_sortedVisibleRoles(),
m_expansionArea(),
m_additionalInfoTextColor(),
m_overlay()
{
- for (int i = 0; i < TextIdCount; ++i) {
- m_text[i].setTextFormat(Qt::PlainText);
- m_text[i].setPerformanceHint(QStaticText::AggressiveCaching);
- }
}
KFileItemListWidget::~KFileItemListWidget()
{
+ qDeleteAll(m_textInfo);
+ m_textInfo.clear();
}
void KFileItemListWidget::setLayout(Layout layout)
painter->setFont(itemListStyleOption.font);
painter->setPen(textColor());
- painter->drawStaticText(m_textPos[Name], m_text[Name]);
+ const TextInfo* textInfo = m_textInfo.value("name");
+ painter->drawStaticText(textInfo->pos, textInfo->staticText);
bool clipAdditionalInfoBounds = false;
if (m_supportsItemExpanding) {
// with the icon. This can happen if the user has minimized the width
// of the name-column to a very small value.
const qreal minX = m_pixmapPos.x() + m_pixmap.width() + 4 * itemListStyleOption.padding;
- if (m_textPos[Name].x() + columnWidth("name") > minX) {
+ if (textInfo->pos.x() + columnWidth("name") > minX) {
clipAdditionalInfoBounds = true;
painter->save();
painter->setClipRect(minX, 0, size().width() - minX, size().height(), Qt::IntersectClip);
painter->setPen(m_additionalInfoTextColor);
painter->setFont(itemListStyleOption.font);
- for (int i = Name + 1; i < TextIdCount; ++i) {
- painter->drawStaticText(m_textPos[i], m_text[i]);
+
+ for (int i = 1; i < m_sortedVisibleRoles.count(); ++i) {
+ const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles[i]);
+ painter->drawStaticText(textInfo->pos, textInfo->staticText);
}
if (clipAdditionalInfoBounds) {
return m_textRect;
}
+QRectF KFileItemListWidget::textFocusRect() const
+{
+ const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
+ if (m_layout == CompactLayout) {
+ // In the compact layout a larger textRect() is returned to be aligned
+ // with the iconRect(). This is useful to have a larger selection/hover-area
+ // when having a quite large icon size but only one line of text. Still the
+ // focus rectangle should be shown as narrow as possible around the text.
+ QRectF rect = m_textRect;
+ const TextInfo* topText = m_textInfo.value(m_sortedVisibleRoles.first());
+ const TextInfo* bottomText = m_textInfo.value(m_sortedVisibleRoles.last());
+ rect.setTop(topText->pos.y());
+ rect.setBottom(bottomText->pos.y() + bottomText->staticText.size().height());
+ return rect;
+ }
+ return m_textRect;
+}
+
QRectF KFileItemListWidget::expansionToggleRect() const
{
const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
const QString text = KStringHandler::preProcessWrap(values["name"].toString());
const qreal maxWidth = view->itemSize().width() - 2 * option.padding;
- int textLinesCount = 0;
QTextLine line;
// Calculate the number of lines required for wrapping the name
QTextOption textOption(Qt::AlignHCenter);
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
+ qreal textHeight = 0;
QTextLayout layout(text, option.font);
layout.setTextOption(textOption);
layout.beginLayout();
while ((line = layout.createLine()).isValid()) {
line.setLineWidth(maxWidth);
line.naturalTextWidth();
- ++textLinesCount;
+ textHeight += line.height();
}
layout.endLayout();
// Add one line for each additional information
- textLinesCount += additionalRolesCount;
-
- const qreal height = textLinesCount * option.fontMetrics.height() +
+ const qreal height = textHeight +
+ additionalRolesCount * option.fontMetrics.lineSpacing() +
option.iconSize +
option.padding * 3;
return QSizeF(view->itemSize().width(), height);
}
const qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
- const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.height());
+ const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.lineSpacing());
return QSizeF(width, height);
}
int scaledIconSize = 0;
if (iconOnTop) {
- scaledIconSize = static_cast<int>(m_textPos[Name].y() - 2 * padding);
+ const TextInfo* textInfo = m_textInfo.value("name");
+ scaledIconSize = static_cast<int>(textInfo->pos.y() - 2 * padding);
} else {
const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
m_pixmapPos.setY(padding + scaledIconSize - m_scaledPixmapSize.height());
} else {
// Center horizontally and vertically within the icon-area
- m_pixmapPos.setX(m_textPos[Name].x() - 2 * padding
+ const TextInfo* textInfo = m_textInfo.value("name");
+ m_pixmapPos.setX(textInfo->pos.x() - 2 * padding
- (scaledIconSize + m_scaledPixmapSize.width()) / 2);
m_pixmapPos.setY(padding
+ (scaledIconSize - m_scaledPixmapSize.height()) / 2);
break;
}
- for (int i = 0; i < TextIdCount; ++i) {
- m_text[i].setText(QString());
- m_text[i].setTextOption(textOption);
+ qDeleteAll(m_textInfo);
+ m_textInfo.clear();
+ for (int i = 0; i < m_sortedVisibleRoles.count(); ++i) {
+ TextInfo* textInfo = new TextInfo();
+ textInfo->staticText.setTextFormat(Qt::PlainText);
+ textInfo->staticText.setPerformanceHint(QStaticText::AggressiveCaching);
+ textInfo->staticText.setTextOption(textOption);
+ m_textInfo.insert(m_sortedVisibleRoles[i], textInfo);
}
switch (m_layout) {
const qreal padding = option.padding;
const qreal maxWidth = size().width() - 2 * padding;
const qreal widgetHeight = size().height();
- const qreal fontHeight = option.fontMetrics.height();
+ const qreal lineSpacing = option.fontMetrics.lineSpacing();
// Initialize properties for the "name" role. It will be used as anchor
// for initializing the position of the other roles.
- m_text[Name].setText(KStringHandler::preProcessWrap(values["name"].toString()));
+ TextInfo* nameTextInfo = m_textInfo.value("name");
+ nameTextInfo->staticText.setText(KStringHandler::preProcessWrap(values["name"].toString()));
// Calculate the number of lines required for the name and the required width
- int textLinesCountForName = 0;
- qreal requiredWidthForName = 0;
+ qreal nameWidth = 0;
+ qreal nameHeight = 0;
QTextLine line;
- QTextLayout layout(m_text[Name].text(), option.font);
- layout.setTextOption(m_text[Name].textOption());
+ QTextLayout layout(nameTextInfo->staticText.text(), option.font);
+ layout.setTextOption(nameTextInfo->staticText.textOption());
layout.beginLayout();
while ((line = layout.createLine()).isValid()) {
line.setLineWidth(maxWidth);
- requiredWidthForName = qMax(requiredWidthForName, line.naturalTextWidth());
- ++textLinesCountForName;
+ nameWidth = qMax(nameWidth, line.naturalTextWidth());
+ nameHeight += line.height();
}
layout.endLayout();
// Use one line for each additional information
- int textLinesCount = textLinesCountForName;
const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
- textLinesCount += additionalRolesCount;
-
- m_text[Name].setTextWidth(maxWidth);
- m_textPos[Name] = QPointF(padding, widgetHeight - textLinesCount * fontHeight - padding);
- m_textRect = QRectF(padding + (maxWidth - requiredWidthForName) / 2,
- m_textPos[Name].y(),
- requiredWidthForName,
- textLinesCountForName * fontHeight);
+ nameTextInfo->staticText.setTextWidth(maxWidth);
+ nameTextInfo->pos = QPointF(padding, widgetHeight -
+ nameHeight -
+ additionalRolesCount * lineSpacing -
+ padding);
+ m_textRect = QRectF(padding + (maxWidth - nameWidth) / 2,
+ nameTextInfo->pos.y(),
+ nameWidth,
+ nameHeight);
// Calculate the position for each additional information
- qreal y = m_textPos[Name].y() + textLinesCountForName * fontHeight;
+ qreal y = nameTextInfo->pos.y() + nameHeight;
foreach (const QByteArray& role, m_sortedVisibleRoles) {
- const TextId textId = roleTextId(role);
- if (textId == Name) {
+ if (role == "name") {
continue;
}
const QString text = roleText(role, values);
- m_text[textId].setText(text);
+ TextInfo* textInfo = m_textInfo.value(role);
+ textInfo->staticText.setText(text);
qreal requiredWidth = 0;
QTextLayout layout(text, option.font);
- layout.setTextOption(m_text[textId].textOption());
+ layout.setTextOption(textInfo->staticText.textOption());
layout.beginLayout();
QTextLine textLine = layout.createLine();
if (textLine.isValid()) {
// not get elided although it does not fit into the given width. As workaround
// the padding is substracted.
const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - padding);
- m_text[textId].setText(elidedText);
+ textInfo->staticText.setText(elidedText);
}
}
layout.endLayout();
- m_textPos[textId] = QPointF(padding, y);
- m_text[textId].setTextWidth(maxWidth);
+ textInfo->pos = QPointF(padding, y);
+ textInfo->staticText.setTextWidth(maxWidth);
- const QRectF textRect(padding + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
+ const QRectF textRect(padding + (maxWidth - requiredWidth) / 2, y, requiredWidth, lineSpacing);
m_textRect |= textRect;
- y += fontHeight;
+ y += lineSpacing;
}
// Add a padding to the text rectangle
const KItemListStyleOption& option = styleOption();
const qreal widgetHeight = size().height();
- const qreal fontHeight = option.fontMetrics.height();
- const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * fontHeight;
+ const qreal lineSpacing = option.fontMetrics.lineSpacing();
+ const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * lineSpacing;
const int scaledIconSize = (textLinesHeight < option.iconSize) ? widgetHeight - 2 * option.padding : option.iconSize;
qreal maximumRequiredTextWidth = 0;
const qreal x = option.padding * 3 + scaledIconSize;
- qreal y = (widgetHeight - textLinesHeight) / 2;
+ qreal y = qRound((widgetHeight - textLinesHeight) / 2);
const qreal maxWidth = size().width() - x - option.padding;
foreach (const QByteArray& role, m_sortedVisibleRoles) {
- const TextId textId = roleTextId(role);
-
const QString text = roleText(role, values);
- m_text[textId].setText(text);
+ TextInfo* textInfo = m_textInfo.value(role);
+ textInfo->staticText.setText(text);
qreal requiredWidth = option.fontMetrics.width(text);
if (requiredWidth > maxWidth) {
requiredWidth = maxWidth;
const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
- m_text[textId].setText(elidedText);
+ textInfo->staticText.setText(elidedText);
}
- m_textPos[textId] = QPointF(x, y);
- m_text[textId].setTextWidth(maxWidth);
+ textInfo->pos = QPointF(x, y);
+ textInfo->staticText.setTextWidth(maxWidth);
maximumRequiredTextWidth = qMax(maximumRequiredTextWidth, requiredWidth);
- y += fontHeight;
+ y += lineSpacing;
}
m_textRect = QRectF(x - option.padding, 0, maximumRequiredTextWidth + 2 * option.padding, widgetHeight);
const qreal y = qMax(qreal(option.padding), (widgetHeight - fontHeight) / 2);
foreach (const QByteArray& role, m_sortedVisibleRoles) {
- const TextId textId = roleTextId(role);
+ const RoleType type = roleType(role);
QString text = roleText(role, values);
qreal requiredWidth = option.fontMetrics.width(text);
const qreal roleWidth = columnWidth(role);
qreal availableTextWidth = roleWidth - 2 * columnPadding;
- if (textId == Name) {
+ if (type == Name) {
availableTextWidth -= firstColumnInc;
}
requiredWidth = option.fontMetrics.width(text);
}
- m_text[textId].setText(text);
- m_textPos[textId] = QPointF(x + columnPadding, y);
+ TextInfo* textInfo = m_textInfo.value(role);
+ textInfo->staticText.setText(text);
+ textInfo->pos = QPointF(x + columnPadding, y);
x += roleWidth;
- switch (textId) {
+ switch (type) {
case Name: {
const qreal textWidth = option.extendedSelectionRegion
- ? size().width() - m_textPos[textId].x()
+ ? size().width() - textInfo->pos.x()
: requiredWidth + 2 * option.padding;
- m_textRect = QRectF(m_textPos[textId].x() - option.padding, 0,
+ m_textRect = QRectF(textInfo->pos.x() - option.padding, 0,
textWidth, size().height());
// The column after the name should always be aligned on the same x-position independent
}
case Size:
// The values for the size should be right aligned
- m_textPos[textId].rx() += roleWidth - requiredWidth - 2 * columnPadding;
+ textInfo->pos.rx() += roleWidth - requiredWidth - 2 * columnPadding;
break;
default:
KIconEffect::semiTransparent(pixmap);
}
-KFileItemListWidget::TextId KFileItemListWidget::roleTextId(const QByteArray& role)
+KFileItemListWidget::RoleType KFileItemListWidget::roleType(const QByteArray& role)
{
- static QHash<QByteArray, TextId> rolesHash;
+ static QHash<QByteArray, RoleType> rolesHash;
if (rolesHash.isEmpty()) {
rolesHash.insert("name", Name);
rolesHash.insert("size", Size);
rolesHash.insert("date", Date);
- rolesHash.insert("permissions", Permissions);
- rolesHash.insert("owner", Owner);
- rolesHash.insert("group", Group);
- rolesHash.insert("type", Type);
- rolesHash.insert("destination", Destination);
- rolesHash.insert("path", Path);
}
- return rolesHash.value(role);
+ return rolesHash.value(role, Generic);
}
QString KFileItemListWidget::roleText(const QByteArray& role, const QHash<QByteArray, QVariant>& values)
QString text;
const QVariant roleValue = values.value(role);
- switch (roleTextId(role)) {
- case Name:
- case Permissions:
- case Owner:
- case Group:
- case Type:
- case Destination:
- case Path:
- text = roleValue.toString();
- break;
-
+ switch (roleType(role)) {
case Size: {
if (values.value("isDir").toBool()) {
// The item represents a directory. Show the number of sub directories
break;
}
+ case Name:
+ case Generic:
+ text = roleValue.toString();
+ break;
+
default:
Q_ASSERT(false);
break;