#include <KIconEffect>
#include <KIconLoader>
#include <KLocale>
+#include <kratingpainter.h>
#include <KStringHandler>
#include <KDebug>
m_expansionArea(),
m_customTextColor(),
m_additionalInfoTextColor(),
- m_overlay()
+ m_overlay(),
+ m_rating()
{
}
painter->drawStaticText(textInfo->pos, textInfo->staticText);
}
+ if (!m_rating.isNull()) {
+ const TextInfo* ratingTextInfo = m_textInfo.value("rating");
+ QPointF pos = ratingTextInfo->pos;
+ const Qt::Alignment align = ratingTextInfo->staticText.textOption().alignment();
+ if (align & Qt::AlignHCenter) {
+ pos.rx() += (size().width() - m_rating.width()) / 2;
+ }
+ painter->drawPixmap(pos, m_rating);
+ }
+
if (clipAdditionalInfoBounds) {
painter->restore();
}
int index,
const KItemListView* view)
{
- qreal width = 0;
const QHash<QByteArray, QVariant> values = view->model()->data(index);
const KItemListStyleOption& option = view->styleOption();
const QString text = KFileItemListWidget::roleText(role, values);
- if (!text.isEmpty()) {
- const qreal columnPadding = option.padding * 3;
- width = qMax(width, qreal(2 * columnPadding + option.fontMetrics.width(text)));
- }
+ qreal width = columnPadding(option);
- if (role == "name") {
- // Increase the width by the expansion-toggle and the current expansion level
- const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
- width += option.padding + (expandedParentsCount + 1) * view->itemSize().height() + KIconLoader::SizeSmall;
+ if (role == "rating") {
+ width += preferredRatingSize(option).width();
+ } else {
+ width += option.fontMetrics.width(text);
- // Increase the width by the required space for the icon
- width += option.padding * 2 + option.iconSize;
+ if (role == "name") {
+ // Increase the width by the expansion-toggle and the current expansion level
+ const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
+ width += option.padding + (expandedParentsCount + 1) * view->itemSize().height() + KIconLoader::SizeSmall;
+
+ // Increase the width by the required space for the icon
+ width += option.padding * 2 + option.iconSize;
+ }
}
return width;
case DetailsLayout: updateDetailsLayoutTextCache(); break;
default: Q_ASSERT(false); break;
}
+
+ const TextInfo* ratingTextInfo = m_textInfo.value("rating");
+ if (ratingTextInfo) {
+ // The text of the rating-role has been set to empty to get
+ // replaced by a rating-image showing the rating as stars.
+ const KItemListStyleOption& option = styleOption();
+ QSizeF ratingSize = preferredRatingSize(option);
+
+ const qreal availableWidth = (m_layout == DetailsLayout)
+ ? columnWidth("rating") - columnPadding(option)
+ : m_textRect.width();
+ if (ratingSize.width() > availableWidth) {
+ ratingSize.rwidth() = availableWidth;
+ }
+ m_rating = QPixmap(ratingSize.toSize());
+ m_rating.fill(Qt::transparent);
+
+ QPainter painter(&m_rating);
+ const QRect rect(0, 0, m_rating.width(), m_rating.height());
+ const int rating = data().value("rating").toInt();
+ KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating);
+ } else if (!m_rating.isNull()) {
+ m_rating = QPixmap();
+ }
}
void KFileItemListWidget::updateIconsLayoutTextCache()
const int scaledIconSize = widgetHeight - 2 * option.padding;
const int fontHeight = option.fontMetrics.height();
- const qreal columnPadding = option.padding * 3;
+ const qreal columnWidthInc = columnPadding(option);
qreal firstColumnInc = scaledIconSize;
if (m_supportsItemExpanding) {
firstColumnInc += (m_expansionArea.left() + m_expansionArea.right() + widgetHeight) / 2;
// Elide the text in case it does not fit into the available column-width
qreal requiredWidth = option.fontMetrics.width(text);
const qreal roleWidth = columnWidth(role);
- qreal availableTextWidth = roleWidth - 2 * columnPadding;
+ qreal availableTextWidth = roleWidth - columnWidthInc;
if (type == Name) {
availableTextWidth -= firstColumnInc;
}
TextInfo* textInfo = m_textInfo.value(role);
textInfo->staticText.setText(text);
- textInfo->pos = QPointF(x + columnPadding, y);
+ textInfo->pos = QPointF(x + columnWidthInc / 2, y);
x += roleWidth;
switch (type) {
}
case Size:
// The values for the size should be right aligned
- textInfo->pos.rx() += roleWidth - requiredWidth - 2 * columnPadding;
+ textInfo->pos.rx() += roleWidth - requiredWidth - columnWidthInc;
break;
default:
rolesHash.insert("name", Name);
rolesHash.insert("size", Size);
rolesHash.insert("date", Date);
+ rolesHash.insert("rating", Rating);
}
return rolesHash.value(role, Generic);
break;
}
+ case Rating:
+ // Always use an empty text, as the rating is shown by the image m_rating.
+ break;
+
case Name:
case Generic:
text = roleValue.toString();
return text;
}
+
+QSizeF KFileItemListWidget::preferredRatingSize(const KItemListStyleOption& option)
+{
+ const qreal height = option.fontMetrics.ascent();
+ return QSizeF(height * 5, height);
+}
+
+qreal KFileItemListWidget::columnPadding(const KItemListStyleOption& option)
+{
+ return option.padding * 6;
+}
+
#include "kfileitemlistwidget.moc"