1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kfileitemlistwidget.h"
22 #include "kfileitemlistview.h"
23 #include "kfileitemmodel.h"
26 #include <KIconEffect>
27 #include <KIconLoader>
29 #include <kratingpainter.h>
30 #include <KStringHandler>
33 #include "private/kfileitemclipboard.h"
34 #include "private/kpixmapmodifier.h"
36 #include <QFontMetricsF>
37 #include <QGraphicsSceneResizeEvent>
39 #include <QStyleOption>
40 #include <QTextLayout>
43 // #define KFILEITEMLISTWIDGET_DEBUG
45 KFileItemListWidget::KFileItemListWidget(QGraphicsItem
* parent
) :
46 KItemListWidget(parent
),
49 m_isExpandable(false),
50 m_supportsItemExpanding(false),
53 m_dirtyContentRoles(),
54 m_layout(IconsLayout
),
62 m_sortedVisibleRoles(),
65 m_additionalInfoTextColor(),
71 KFileItemListWidget::~KFileItemListWidget()
73 qDeleteAll(m_textInfo
);
77 void KFileItemListWidget::setLayout(Layout layout
)
79 if (m_layout
!= layout
) {
82 updateAdditionalInfoTextColor();
87 KFileItemListWidget::Layout
KFileItemListWidget::layout() const
92 void KFileItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
94 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
95 m_supportsItemExpanding
= supportsItemExpanding
;
101 bool KFileItemListWidget::supportsItemExpanding() const
103 return m_supportsItemExpanding
;
106 void KFileItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
108 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
110 KItemListWidget::paint(painter
, option
, widget
);
112 if (!m_expansionArea
.isEmpty()) {
113 drawSiblingsInformation(painter
);
116 const KItemListStyleOption
& itemListStyleOption
= styleOption();
118 // Blend the unhovered and hovered pixmap if the hovering
119 // animation is ongoing
120 if (hoverOpacity() < 1.0) {
121 drawPixmap(painter
, m_pixmap
);
124 const qreal opacity
= painter
->opacity();
125 painter
->setOpacity(hoverOpacity() * opacity
);
126 drawPixmap(painter
, m_hoverPixmap
);
127 painter
->setOpacity(opacity
);
129 drawPixmap(painter
, m_pixmap
);
132 painter
->setFont(itemListStyleOption
.font
);
133 painter
->setPen(textColor());
134 const TextInfo
* textInfo
= m_textInfo
.value("name");
135 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
137 bool clipAdditionalInfoBounds
= false;
138 if (m_supportsItemExpanding
) {
139 // Prevent a possible overlapping of the additional-information texts
140 // with the icon. This can happen if the user has minimized the width
141 // of the name-column to a very small value.
142 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
143 if (textInfo
->pos
.x() + columnWidth("name") > minX
) {
144 clipAdditionalInfoBounds
= true;
146 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
150 painter
->setPen(m_additionalInfoTextColor
);
151 painter
->setFont(itemListStyleOption
.font
);
153 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
154 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
155 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
158 if (!m_rating
.isNull()) {
159 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
160 QPointF pos
= ratingTextInfo
->pos
;
161 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
162 if (align
& Qt::AlignHCenter
) {
163 pos
.rx() += (size().width() - m_rating
.width()) / 2;
165 painter
->drawPixmap(pos
, m_rating
);
168 if (clipAdditionalInfoBounds
) {
172 #ifdef KFILEITEMLISTWIDGET_DEBUG
173 painter
->setBrush(Qt::NoBrush
);
174 painter
->setPen(Qt::green
);
175 painter
->drawRect(m_iconRect
);
177 painter
->setPen(Qt::red
);
178 painter
->drawText(QPointF(0, itemListStyleOption
.fontMetrics
.height()), QString::number(index()));
179 painter
->drawRect(rect());
183 QRectF
KFileItemListWidget::iconRect() const
185 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
189 QRectF
KFileItemListWidget::textRect() const
191 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
195 QRectF
KFileItemListWidget::textFocusRect() const
197 // In the compact- and details-layout a larger textRect() is returned to be aligned
198 // with the iconRect(). This is useful to have a larger selection/hover-area
199 // when having a quite large icon size but only one line of text. Still the
200 // focus rectangle should be shown as narrow as possible around the text.
202 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
205 case CompactLayout
: {
206 QRectF rect
= m_textRect
;
207 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
208 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
209 rect
.setTop(topText
->pos
.y());
210 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
214 case DetailsLayout
: {
215 QRectF rect
= m_textRect
;
216 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
217 rect
.setTop(textInfo
->pos
.y());
218 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
229 QRectF
KFileItemListWidget::expansionToggleRect() const
231 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
232 return m_isExpandable
? m_expansionArea
: QRectF();
235 QRectF
KFileItemListWidget::selectionToggleRect() const
237 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
239 const int iconHeight
= styleOption().iconSize
;
241 int toggleSize
= KIconLoader::SizeSmall
;
242 if (iconHeight
>= KIconLoader::SizeEnormous
) {
243 toggleSize
= KIconLoader::SizeMedium
;
244 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
245 toggleSize
= KIconLoader::SizeSmallMedium
;
248 QPointF pos
= iconRect().topLeft();
250 // If the selection toggle has a very small distance to the
251 // widget borders, the size of the selection toggle will get
252 // increased to prevent an accidental clicking of the item
253 // when trying to hit the toggle.
254 const int widgetHeight
= size().height();
255 const int widgetWidth
= size().width();
256 const int minMargin
= 2;
258 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
259 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
260 toggleSize
= widgetHeight
;
263 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
264 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
265 toggleSize
= widgetWidth
;
269 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
272 QSizeF
KFileItemListWidget::itemSizeHint(int index
, const KItemListView
* view
)
274 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
275 const KItemListStyleOption
& option
= view
->styleOption();
276 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
278 switch (static_cast<const KFileItemListView
*>(view
)->itemLayout()) {
280 const QString text
= KStringHandler::preProcessWrap(values
["name"].toString());
282 const qreal itemWidth
= view
->itemSize().width();
283 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
286 // Calculate the number of lines required for wrapping the name
287 QTextOption
textOption(Qt::AlignHCenter
);
288 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
290 qreal textHeight
= 0;
291 QTextLayout
layout(text
, option
.font
);
292 layout
.setTextOption(textOption
);
293 layout
.beginLayout();
294 while ((line
= layout
.createLine()).isValid()) {
295 line
.setLineWidth(maxWidth
);
296 line
.naturalTextWidth();
297 textHeight
+= line
.height();
301 // Add one line for each additional information
302 textHeight
+= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
304 const qreal maxTextHeight
= option
.maxTextSize
.height();
305 if (maxTextHeight
> 0 && textHeight
> maxTextHeight
) {
306 textHeight
= maxTextHeight
;
309 return QSizeF(itemWidth
, textHeight
+ option
.iconSize
+ option
.padding
* 3);
312 case CompactLayout
: {
313 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
314 // to show all roles without horizontal clipping.
315 qreal maximumRequiredWidth
= 0.0;
317 foreach (const QByteArray
& role
, view
->visibleRoles()) {
318 const QString text
= KFileItemListWidget::roleText(role
, values
);
319 const qreal requiredWidth
= option
.fontMetrics
.width(text
);
320 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
323 qreal width
= option
.padding
* 4 + option
.iconSize
+ maximumRequiredWidth
;
324 const qreal maxWidth
= option
.maxTextSize
.width();
325 if (maxWidth
> 0 && width
> maxWidth
) {
328 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * option
.fontMetrics
.lineSpacing());
329 return QSizeF(width
, height
);
332 case DetailsLayout
: {
333 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
334 return QSizeF(-1, height
);
345 qreal
KFileItemListWidget::preferredRoleColumnWidth(const QByteArray
& role
,
347 const KItemListView
* view
)
350 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
351 const KItemListStyleOption
& option
= view
->styleOption();
353 const QString text
= KFileItemListWidget::roleText(role
, values
);
354 qreal width
= columnPadding(option
);
356 if (role
== "rating") {
357 width
+= preferredRatingSize(option
).width();
359 width
+= option
.fontMetrics
.width(text
);
361 if (role
== "name") {
362 // Increase the width by the expansion-toggle and the current expansion level
363 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
364 width
+= option
.padding
+ (expandedParentsCount
+ 1) * view
->itemSize().height() + KIconLoader::SizeSmall
;
366 // Increase the width by the required space for the icon
367 width
+= option
.padding
* 2 + option
.iconSize
;
374 void KFileItemListWidget::invalidateCache()
376 m_dirtyLayout
= true;
377 m_dirtyContent
= true;
380 void KFileItemListWidget::refreshCache()
384 void KFileItemListWidget::setTextColor(const QColor
& color
)
386 if (color
!= m_customTextColor
) {
387 m_customTextColor
= color
;
388 updateAdditionalInfoTextColor();
393 QColor
KFileItemListWidget::textColor() const
395 if (m_customTextColor
.isValid() && !isSelected()) {
396 return m_customTextColor
;
399 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
400 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Text
;
401 return styleOption().palette
.brush(group
, role
).color();
404 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
407 m_dirtyContent
= true;
411 QPixmap
KFileItemListWidget::overlay() const
416 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
417 const QSet
<QByteArray
>& roles
)
421 m_dirtyContent
= true;
423 QSet
<QByteArray
> dirtyRoles
;
424 if (roles
.isEmpty()) {
425 dirtyRoles
= visibleRoles().toSet();
426 dirtyRoles
.insert("iconPixmap");
427 dirtyRoles
.insert("iconName");
432 QSetIterator
<QByteArray
> it(dirtyRoles
);
433 while (it
.hasNext()) {
434 const QByteArray
& role
= it
.next();
435 m_dirtyContentRoles
.insert(role
);
439 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
440 const QList
<QByteArray
>& previous
)
443 m_sortedVisibleRoles
= current
;
444 m_dirtyLayout
= true;
447 void KFileItemListWidget::columnWidthChanged(const QByteArray
& role
,
454 m_dirtyLayout
= true;
457 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
458 const KItemListStyleOption
& previous
)
462 updateAdditionalInfoTextColor();
463 m_dirtyLayout
= true;
466 void KFileItemListWidget::hoveredChanged(bool hovered
)
469 m_dirtyLayout
= true;
472 void KFileItemListWidget::selectedChanged(bool selected
)
475 updateAdditionalInfoTextColor();
478 void KFileItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
482 m_dirtyLayout
= true;
486 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
488 KItemListWidget::resizeEvent(event
);
489 m_dirtyLayout
= true;
492 void KFileItemListWidget::showEvent(QShowEvent
* event
)
494 KItemListWidget::showEvent(event
);
496 // Listen to changes of the clipboard to mark the item as cut/uncut
497 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
499 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
500 m_isCut
= clipboard
->isCut(itemUrl
);
502 connect(clipboard
, SIGNAL(cutItemsChanged()),
503 this, SLOT(slotCutItemsChanged()));
506 void KFileItemListWidget::hideEvent(QHideEvent
* event
)
508 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
509 this, SLOT(slotCutItemsChanged()));
511 KItemListWidget::hideEvent(event
);
514 void KFileItemListWidget::slotCutItemsChanged()
516 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
517 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
518 if (m_isCut
!= isCut
) {
520 m_pixmap
= QPixmap();
521 m_dirtyContent
= true;
526 void KFileItemListWidget::triggerCacheRefreshing()
528 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
534 const QHash
<QByteArray
, QVariant
> values
= data();
535 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
536 m_isHidden
= values
["name"].toString().startsWith(QLatin1Char('.'));
538 updateExpansionArea();
542 m_dirtyLayout
= false;
543 m_dirtyContent
= false;
544 m_dirtyContentRoles
.clear();
547 void KFileItemListWidget::updateExpansionArea()
549 if (m_supportsItemExpanding
) {
550 const QHash
<QByteArray
, QVariant
> values
= data();
551 Q_ASSERT(values
.contains("expandedParentsCount"));
552 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
553 if (expandedParentsCount
>= 0) {
554 const qreal widgetHeight
= size().height();
555 const qreal inc
= (widgetHeight
- KIconLoader::SizeSmall
) / 2;
556 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
558 m_expansionArea
= QRectF(x
, y
, KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
563 m_expansionArea
= QRectF();
566 void KFileItemListWidget::updatePixmapCache()
568 // Precondition: Requires already updated m_textPos values to calculate
569 // the remaining height when the alignment is vertical.
571 const QSizeF widgetSize
= size();
572 const bool iconOnTop
= (m_layout
== IconsLayout
);
573 const KItemListStyleOption
& option
= styleOption();
574 const qreal padding
= option
.padding
;
576 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
577 const int maxIconHeight
= option
.iconSize
;
579 const QHash
<QByteArray
, QVariant
> values
= data();
581 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
582 if (!updatePixmap
&& m_dirtyContent
) {
583 updatePixmap
= m_dirtyContentRoles
.isEmpty()
584 || m_dirtyContentRoles
.contains("iconPixmap")
585 || m_dirtyContentRoles
.contains("iconName")
586 || m_dirtyContentRoles
.contains("iconOverlays");
590 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
591 if (m_pixmap
.isNull()) {
592 // Use the icon that fits to the MIME-type
593 QString iconName
= values
["iconName"].toString();
594 if (iconName
.isEmpty()) {
595 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
596 // use a generic icon as fallback
597 iconName
= QLatin1String("unknown");
599 m_pixmap
= pixmapForIcon(iconName
, maxIconHeight
);
600 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
601 // A custom pixmap has been applied. Assure that the pixmap
602 // is scaled to the maximum available size.
603 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
606 const QStringList overlays
= values
["iconOverlays"].toStringList();
608 // Strangely KFileItem::overlays() returns empty string-values, so
609 // we need to check first whether an overlay must be drawn at all.
610 // It is more efficient to do it here, as KIconLoader::drawOverlays()
611 // assumes that an overlay will be drawn and has some additional
613 foreach (const QString
& overlay
, overlays
) {
614 if (!overlay
.isEmpty()) {
615 // There is at least one overlay, draw all overlays above m_pixmap
616 // and cancel the check
617 KIconLoader::global()->drawOverlays(overlays
, m_pixmap
, KIconLoader::Desktop
);
623 applyCutEffect(m_pixmap
);
627 applyHiddenEffect(m_pixmap
);
631 if (!m_overlay
.isNull()) {
632 QPainter
painter(&m_pixmap
);
633 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
636 int scaledIconSize
= 0;
638 const TextInfo
* textInfo
= m_textInfo
.value("name");
639 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
641 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
642 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
643 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
644 widgetSize
.height() - 2 * padding
: maxIconHeight
;
647 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
648 const int maxScaledIconHeight
= scaledIconSize
;
650 m_scaledPixmapSize
= m_pixmap
.size();
651 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
654 // Center horizontally and align on bottom within the icon-area
655 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
656 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
658 // Center horizontally and vertically within the icon-area
659 const TextInfo
* textInfo
= m_textInfo
.value("name");
660 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
661 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
662 m_pixmapPos
.setY(padding
663 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
666 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
668 // Prepare the pixmap that is used when the item gets hovered
670 m_hoverPixmap
= m_pixmap
;
671 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
672 // In the KIconLoader terminology, active = hover.
673 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
674 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
676 m_hoverPixmap
= m_pixmap
;
678 } else if (hoverOpacity() <= 0.0) {
679 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
680 m_hoverPixmap
= QPixmap();
684 void KFileItemListWidget::updateTextsCache()
686 QTextOption textOption
;
689 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
690 textOption
.setAlignment(Qt::AlignHCenter
);
694 textOption
.setAlignment(Qt::AlignLeft
);
695 textOption
.setWrapMode(QTextOption::NoWrap
);
702 qDeleteAll(m_textInfo
);
704 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
705 TextInfo
* textInfo
= new TextInfo();
706 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
707 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
708 textInfo
->staticText
.setTextOption(textOption
);
709 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
713 case IconsLayout
: updateIconsLayoutTextCache(); break;
714 case CompactLayout
: updateCompactLayoutTextCache(); break;
715 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
716 default: Q_ASSERT(false); break;
719 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
720 if (ratingTextInfo
) {
721 // The text of the rating-role has been set to empty to get
722 // replaced by a rating-image showing the rating as stars.
723 const KItemListStyleOption
& option
= styleOption();
724 QSizeF ratingSize
= preferredRatingSize(option
);
726 const qreal availableWidth
= (m_layout
== DetailsLayout
)
727 ? columnWidth("rating") - columnPadding(option
)
728 : m_textRect
.width();
729 if (ratingSize
.width() > availableWidth
) {
730 ratingSize
.rwidth() = availableWidth
;
732 m_rating
= QPixmap(ratingSize
.toSize());
733 m_rating
.fill(Qt::transparent
);
735 QPainter
painter(&m_rating
);
736 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
737 const int rating
= data().value("rating").toInt();
738 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
739 } else if (!m_rating
.isNull()) {
740 m_rating
= QPixmap();
744 void KFileItemListWidget::updateIconsLayoutTextCache()
751 // might get wrapped above
756 const QHash
<QByteArray
, QVariant
> values
= data();
758 const KItemListStyleOption
& option
= styleOption();
759 const qreal padding
= option
.padding
;
760 const qreal maxWidth
= size().width() - 2 * padding
;
761 const qreal widgetHeight
= size().height();
762 const qreal lineSpacing
= option
.fontMetrics
.lineSpacing();
764 // Initialize properties for the "name" role. It will be used as anchor
765 // for initializing the position of the other roles.
766 TextInfo
* nameTextInfo
= m_textInfo
.value("name");
767 const QString nameText
= KStringHandler::preProcessWrap(values
["name"].toString());
768 nameTextInfo
->staticText
.setText(nameText
);
770 // Calculate the number of lines required for the name and the required width
772 qreal nameHeight
= 0;
775 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
776 const int maxNameLines
= (option
.maxTextSize
.height() / int(lineSpacing
)) - additionalRolesCount
;
778 QTextLayout
layout(nameTextInfo
->staticText
.text(), option
.font
);
779 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
780 layout
.beginLayout();
781 int nameLineIndex
= 0;
782 while ((line
= layout
.createLine()).isValid()) {
783 line
.setLineWidth(maxWidth
);
784 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
785 nameHeight
+= line
.height();
788 if (nameLineIndex
== maxNameLines
) {
789 // The maximum number of textlines has been reached. If this is
790 // the case provide an elided text if necessary.
791 const int textLength
= line
.textStart() + line
.textLength();
792 if (textLength
< nameText
.length()) {
793 // Elide the last line of the text
794 QString lastTextLine
= nameText
.mid(line
.textStart(), line
.textLength());
795 lastTextLine
= option
.fontMetrics
.elidedText(lastTextLine
,
797 line
.naturalTextWidth() - 1);
798 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
799 nameTextInfo
->staticText
.setText(elidedText
);
806 // Use one line for each additional information
807 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
808 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
810 additionalRolesCount
* lineSpacing
-
812 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
813 nameTextInfo
->pos
.y(),
817 // Calculate the position for each additional information
818 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
819 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
820 if (role
== "name") {
824 const QString text
= roleText(role
, values
);
825 TextInfo
* textInfo
= m_textInfo
.value(role
);
826 textInfo
->staticText
.setText(text
);
828 qreal requiredWidth
= 0;
830 QTextLayout
layout(text
, option
.font
);
831 QTextOption textOption
;
832 textOption
.setWrapMode(QTextOption::NoWrap
);
833 layout
.setTextOption(textOption
);
835 layout
.beginLayout();
836 QTextLine textLine
= layout
.createLine();
837 if (textLine
.isValid()) {
838 textLine
.setLineWidth(maxWidth
);
839 requiredWidth
= textLine
.naturalTextWidth();
840 if (requiredWidth
> maxWidth
) {
841 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
842 textInfo
->staticText
.setText(elidedText
);
843 requiredWidth
= option
.fontMetrics
.width(elidedText
);
848 textInfo
->pos
= QPointF(padding
, y
);
849 textInfo
->staticText
.setTextWidth(maxWidth
);
851 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
852 m_textRect
|= textRect
;
857 // Add a padding to the text rectangle
858 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
861 void KFileItemListWidget::updateCompactLayoutTextCache()
863 // +------+ Name role
864 // | Icon | Additional role 1
865 // +------+ Additional role 2
867 const QHash
<QByteArray
, QVariant
> values
= data();
869 const KItemListStyleOption
& option
= styleOption();
870 const qreal widgetHeight
= size().height();
871 const qreal lineSpacing
= option
.fontMetrics
.lineSpacing();
872 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
873 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
875 qreal maximumRequiredTextWidth
= 0;
876 const qreal x
= option
.padding
* 3 + scaledIconSize
;
877 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
878 const qreal maxWidth
= size().width() - x
- option
.padding
;
879 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
880 const QString text
= roleText(role
, values
);
881 TextInfo
* textInfo
= m_textInfo
.value(role
);
882 textInfo
->staticText
.setText(text
);
884 qreal requiredWidth
= option
.fontMetrics
.width(text
);
885 if (requiredWidth
> maxWidth
) {
886 requiredWidth
= maxWidth
;
887 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
888 textInfo
->staticText
.setText(elidedText
);
891 textInfo
->pos
= QPointF(x
, y
);
892 textInfo
->staticText
.setTextWidth(maxWidth
);
894 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
899 m_textRect
= QRectF(x
- option
.padding
, 0, maximumRequiredTextWidth
+ 2 * option
.padding
, widgetHeight
);
902 void KFileItemListWidget::updateDetailsLayoutTextCache()
904 // Precondition: Requires already updated m_expansionArea
905 // to determine the left position.
908 // | Icon | Name role Additional role 1 Additional role 2
910 m_textRect
= QRectF();
912 const KItemListStyleOption
& option
= styleOption();
913 const QHash
<QByteArray
, QVariant
> values
= data();
915 const qreal widgetHeight
= size().height();
916 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
917 const int fontHeight
= option
.fontMetrics
.height();
919 const qreal columnWidthInc
= columnPadding(option
);
920 qreal firstColumnInc
= scaledIconSize
;
921 if (m_supportsItemExpanding
) {
922 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
924 firstColumnInc
+= option
.padding
;
927 qreal x
= firstColumnInc
;
928 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
930 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
931 const RoleType type
= roleType(role
);
933 QString text
= roleText(role
, values
);
935 // Elide the text in case it does not fit into the available column-width
936 qreal requiredWidth
= option
.fontMetrics
.width(text
);
937 const qreal roleWidth
= columnWidth(role
);
938 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
940 availableTextWidth
-= firstColumnInc
;
943 if (requiredWidth
> availableTextWidth
) {
944 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
945 requiredWidth
= option
.fontMetrics
.width(text
);
948 TextInfo
* textInfo
= m_textInfo
.value(role
);
949 textInfo
->staticText
.setText(text
);
950 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
955 const qreal textWidth
= option
.extendedSelectionRegion
956 ? size().width() - textInfo
->pos
.x()
957 : requiredWidth
+ 2 * option
.padding
;
958 m_textRect
= QRectF(textInfo
->pos
.x() - option
.padding
, 0,
959 textWidth
, size().height());
961 // The column after the name should always be aligned on the same x-position independent
962 // from the expansion-level shown in the name column
967 // The values for the size should be right aligned
968 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
977 void KFileItemListWidget::updateAdditionalInfoTextColor()
980 if (m_customTextColor
.isValid()) {
981 c1
= m_customTextColor
;
982 } else if (isSelected() && m_layout
!= DetailsLayout
) {
983 c1
= styleOption().palette
.highlightedText().color();
985 c1
= styleOption().palette
.text().color();
988 // For the color of the additional info the inactive text color
989 // is not used as this might lead to unreadable text for some color schemes. Instead
990 // the text color c1 is slightly mixed with the background color.
991 const QColor c2
= styleOption().palette
.base().color();
993 const int p2
= 100 - p1
;
994 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
995 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
996 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
999 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1001 if (m_scaledPixmapSize
!= pixmap
.size()) {
1002 QPixmap scaledPixmap
= pixmap
;
1003 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
1004 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1006 #ifdef KFILEITEMLISTWIDGET_DEBUG
1007 painter
->setPen(Qt::blue
);
1008 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1011 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1015 void KFileItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1017 const int siblingSize
= size().height();
1018 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1019 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1021 QStyleOption option
;
1022 bool isItemSibling
= true;
1024 const QBitArray siblings
= siblingsInformation();
1025 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1026 option
.rect
= siblingRect
;
1027 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1029 if (isItemSibling
) {
1030 option
.state
|= QStyle::State_Item
;
1031 if (m_isExpandable
) {
1032 option
.state
|= QStyle::State_Children
;
1034 if (data()["isExpanded"].toBool()) {
1035 option
.state
|= QStyle::State_Open
;
1037 isItemSibling
= false;
1040 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1042 siblingRect
.translate(-siblingRect
.width(), 0);
1046 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
1048 const KIcon
icon(name
);
1051 if (size
<= KIconLoader::SizeSmall
) {
1052 requestedSize
= KIconLoader::SizeSmall
;
1053 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1054 requestedSize
= KIconLoader::SizeSmallMedium
;
1055 } else if (size
<= KIconLoader::SizeMedium
) {
1056 requestedSize
= KIconLoader::SizeMedium
;
1057 } else if (size
<= KIconLoader::SizeLarge
) {
1058 requestedSize
= KIconLoader::SizeLarge
;
1059 } else if (size
<= KIconLoader::SizeHuge
) {
1060 requestedSize
= KIconLoader::SizeHuge
;
1061 } else if (size
<= KIconLoader::SizeEnormous
) {
1062 requestedSize
= KIconLoader::SizeEnormous
;
1063 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1064 requestedSize
= KIconLoader::SizeEnormous
* 2;
1066 requestedSize
= size
;
1069 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
1070 if (requestedSize
!= size
) {
1071 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1077 void KFileItemListWidget::applyCutEffect(QPixmap
& pixmap
)
1079 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
1080 pixmap
= effect
->apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
1083 void KFileItemListWidget::applyHiddenEffect(QPixmap
& pixmap
)
1085 KIconEffect::semiTransparent(pixmap
);
1088 KFileItemListWidget::RoleType
KFileItemListWidget::roleType(const QByteArray
& role
)
1090 static QHash
<QByteArray
, RoleType
> rolesHash
;
1091 if (rolesHash
.isEmpty()) {
1092 rolesHash
.insert("name", Name
);
1093 rolesHash
.insert("size", Size
);
1094 rolesHash
.insert("date", Date
);
1095 rolesHash
.insert("rating", Rating
);
1098 return rolesHash
.value(role
, Generic
);
1101 QString
KFileItemListWidget::roleText(const QByteArray
& role
, const QHash
<QByteArray
, QVariant
>& values
)
1104 const QVariant roleValue
= values
.value(role
);
1106 switch (roleType(role
)) {
1108 if (values
.value("isDir").toBool()) {
1109 // The item represents a directory. Show the number of sub directories
1110 // instead of the file size of the directory.
1111 if (!roleValue
.isNull()) {
1112 const int count
= roleValue
.toInt();
1114 text
= i18nc("@item:intable", "Unknown");
1116 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
1120 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
1121 text
= KGlobal::locale()->formatByteSize(size
);
1127 const QDateTime dateTime
= roleValue
.toDateTime();
1128 text
= KGlobal::locale()->formatDateTime(dateTime
);
1133 // Always use an empty text, as the rating is shown by the image m_rating.
1138 text
= roleValue
.toString();
1149 QSizeF
KFileItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1151 const qreal height
= option
.fontMetrics
.ascent();
1152 return QSizeF(height
* 5, height
);
1155 qreal
KFileItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1157 return option
.padding
* 6;
1160 #include "kfileitemlistwidget.moc"