const qreal height = textLinesCount * option.fontMetrics.height() +
option.iconSize +
- option.margin * 4;
+ option.margin * 3;
return QSizeF(itemSize().width(), height);
}
delete m_modelRolesUpdater;
m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
- const int size = styleOption().iconSize;
- m_modelRolesUpdater->setIconSize(QSize(size, size));
+ m_modelRolesUpdater->setIconSize(availableIconSize());
applyRolesToModel();
}
// of the icon-size before unpausing m_modelRolesUpdater. This prevents
// an unnecessary expensive recreation of all previews afterwards.
m_updateIconSizeTimer->stop();
- const KItemListStyleOption& option = styleOption();
- m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
+ m_modelRolesUpdater->setIconSize(availableIconSize());
}
m_modelRolesUpdater->setPaused(isTransactionActive());
return;
}
- const KItemListStyleOption& option = styleOption();
- m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
+ m_modelRolesUpdater->setIconSize(availableIconSize());
if (m_updateVisibleIndexRangeTimer->isActive()) {
// If the visibility-index-range update is pending do an immediate update
m_modelRolesUpdater->setRoles(roles);
}
+QSize KFileItemListView::availableIconSize() const
+{
+ const KItemListStyleOption& option = styleOption();
+ const int iconSize = option.iconSize;
+ if (m_itemLayout == IconsLayout) {
+ const int maxIconWidth = itemSize().width() - 2 * option.margin;
+ return QSize(maxIconWidth, iconSize);
+ }
+
+ return QSize(iconSize, iconSize);
+}
+
#include "kfileitemlistview.moc"
m_pixmapPos(),
m_pixmap(),
m_scaledPixmapSize(),
- m_originalPixmapSize(),
m_iconRect(),
m_hoverPixmap(),
m_textPos(),
// Precondition: Requires already updated m_textPos values to calculate
// the remaining height when the alignment is vertical.
+ const QSizeF widgetSize = size();
const bool iconOnTop = (m_layout == IconsLayout);
const KItemListStyleOption& option = styleOption();
- const int iconHeight = option.iconSize;
+ const qreal margin = option.margin;
- const QHash<QByteArray, QVariant> values = data();
- const QSizeF widgetSize = size();
+ const int maxIconWidth = iconOnTop ? widgetSize.width() - 2 * margin : option.iconSize;
+ const int maxIconHeight = option.iconSize;
- int scaledIconHeight = 0;
- if (iconOnTop) {
- scaledIconHeight = static_cast<int>(m_textPos[Name].y() - 3 * option.margin);
- } else {
- const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
- const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
- scaledIconHeight = (requiredTextHeight < iconHeight) ? widgetSize.height() - 2 * option.margin : iconHeight;
- }
+ const QHash<QByteArray, QVariant> values = data();
- bool updatePixmap = (iconHeight != m_pixmap.height());
+ bool updatePixmap = (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight);
if (!updatePixmap && m_dirtyContent) {
updatePixmap = m_dirtyContentRoles.isEmpty()
|| m_dirtyContentRoles.contains("iconPixmap")
// use a generic icon as fallback
iconName = QLatin1String("unknown");
}
- m_pixmap = pixmapForIcon(iconName, iconHeight);
- m_originalPixmapSize = m_pixmap.size();
- } else if (m_pixmap.size() != QSize(iconHeight, iconHeight)) {
+ m_pixmap = pixmapForIcon(iconName, maxIconHeight);
+ } else if (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight) {
// A custom pixmap has been applied. Assure that the pixmap
- // is scaled to the available size.
- const bool scale = m_pixmap.width() > iconHeight || m_pixmap.height() > iconHeight ||
- (m_pixmap.width() < iconHeight && m_pixmap.height() < iconHeight);
- if (scale) {
- KPixmapModifier::scale(m_pixmap, QSize(iconHeight, iconHeight));
- }
- m_originalPixmapSize = m_pixmap.size();
-
- // To simplify the handling of scaling the original pixmap
- // will be embedded into a square pixmap.
- QPixmap squarePixmap(iconHeight, iconHeight);
- squarePixmap.fill(Qt::transparent);
-
- QPainter painter(&squarePixmap);
- const int x = (iconHeight - m_pixmap.width()) / 2; // Center horizontally
- int y = iconHeight - m_pixmap.height(); // Move to bottom
- if (!iconOnTop) {
- y /= 2.0; // Center vertically
- }
- painter.drawPixmap(x, y, m_pixmap);
-
- m_pixmap = squarePixmap;
- } else {
- m_originalPixmapSize = m_pixmap.size();
+ // is scaled to the maximum available size.
+ KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight));
}
const QStringList overlays = values["iconOverlays"].toStringList();
if (m_isHidden) {
applyHiddenEffect(m_pixmap);
}
-
- Q_ASSERT(m_pixmap.height() == iconHeight);
}
+
if (!m_overlay.isNull()) {
QPainter painter(&m_pixmap);
painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
}
- m_scaledPixmapSize = QSize(scaledIconHeight, scaledIconHeight);
-
+ int scaledIconSize = 0;
if (iconOnTop) {
- m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
+ scaledIconSize = static_cast<int>(m_textPos[Name].y() - 2 * margin);
} else {
- m_pixmapPos.setX(m_textPos[Name].x() - 2 * option.margin - scaledIconHeight);
+ const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
+ const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
+ scaledIconSize = (requiredTextHeight < maxIconHeight) ?
+ widgetSize.height() - 2 * margin : maxIconHeight;
}
- m_pixmapPos.setY(option.margin);
- // Center the hover rectangle horizontally and align it on bottom
- qreal hoverWidth = m_originalPixmapSize.width();
- qreal hoverHeight = m_originalPixmapSize.height();
- if (scaledIconHeight != m_pixmap.height()) {
- const qreal scaleFactor = qreal(scaledIconHeight) / qreal(m_pixmap.height());
- hoverWidth *= scaleFactor;
- hoverHeight *= scaleFactor;
- }
- const qreal hoverX = m_pixmapPos.x() + (m_scaledPixmapSize.width() - hoverWidth) / 2.0;
- qreal hoverY = m_scaledPixmapSize.height() - hoverHeight;
- if (!iconOnTop) {
- hoverY /= 2.0;
+ const int maxScaledIconWidth = iconOnTop ? widgetSize.width() - 2 * margin : scaledIconSize;
+ const int maxScaledIconHeight = scaledIconSize;
+
+ m_scaledPixmapSize = m_pixmap.size();
+ m_scaledPixmapSize.scale(maxScaledIconWidth, maxScaledIconHeight, Qt::KeepAspectRatio);
+
+ if (iconOnTop) {
+ // Center horizontally and align on bottom within the icon-area
+ m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
+ m_pixmapPos.setY(margin + scaledIconSize - m_scaledPixmapSize.height());
+ } else {
+ // Center horizontally and vertically within the icon-area
+ m_pixmapPos.setX(m_textPos[Name].x() - 2 * margin
+ - (scaledIconSize + m_scaledPixmapSize.width()) / 2);
+ m_pixmapPos.setY(margin
+ + (scaledIconSize - m_scaledPixmapSize.height()) / 2);
}
- hoverY += m_pixmapPos.y();
- m_iconRect = QRectF(hoverX, hoverY, hoverWidth, hoverHeight);
- const qreal margin = option.margin;
+ m_iconRect = QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize));
m_iconRect.adjust(-margin, -margin, margin, margin);
// Prepare the pixmap that is used when the item gets hovered
const QHash<QByteArray, QVariant> values = data();
const KItemListStyleOption& option = styleOption();
- const qreal maxWidth = size().width() - 2 * option.margin;
+ const qreal margin = option.margin;
+ const qreal maxWidth = size().width() - 2 * margin;
const qreal widgetHeight = size().height();
const qreal fontHeight = option.fontMetrics.height();
textLinesCount += additionalRolesCount;
m_text[Name].setTextWidth(maxWidth);
- m_textPos[Name] = QPointF(option.margin, widgetHeight - textLinesCount * fontHeight - option.margin);
- m_textRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
+ m_textPos[Name] = QPointF(margin, widgetHeight - textLinesCount * fontHeight - margin);
+ m_textRect = QRectF(margin + (maxWidth - requiredWidthForName) / 2,
m_textPos[Name].y(),
requiredWidthForName,
textLinesCountForName * fontHeight);
// in comparison to QTextLine::setLineWidth(). It might happen that the text does
// not get elided although it does not fit into the given width. As workaround
// the margin is substracted.
- const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - option.margin);
+ const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - margin);
m_text[textId].setText(elidedText);
}
}
layout.endLayout();
- m_textPos[textId] = QPointF(option.margin, y);
+ m_textPos[textId] = QPointF(margin, y);
m_text[textId].setTextWidth(maxWidth);
- const QRectF textRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
+ const QRectF textRect(margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
m_textRect |= textRect;
y += fontHeight;
}
// Add a margin to the text rectangle
- const qreal margin = option.margin;
m_textRect.adjust(-margin, -margin, margin, margin);
}
#ifdef KFILEITEMLISTWIDGET_DEBUG
painter->setPen(Qt::blue);
- painter->drawRect(QRectF(m_pixmapPos, QSizeF(scaledPixmap.size())));
+ painter->drawRect(QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize)));
#endif
} else {
painter->drawPixmap(m_pixmapPos, pixmap);