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 "kfileitemclipboard_p.h"
23 #include "kfileitemmodel.h"
24 #include "kitemlistview.h"
25 #include "kpixmapmodifier_p.h"
28 #include <KIconEffect>
29 #include <KIconLoader>
31 #include <KStringHandler>
34 #include <QFontMetricsF>
35 #include <QGraphicsSceneResizeEvent>
37 #include <QStyleOption>
38 #include <QTextLayout>
41 // #define KFILEITEMLISTWIDGET_DEBUG
43 KFileItemListWidget::KFileItemListWidget(QGraphicsItem
* parent
) :
44 KItemListWidget(parent
),
47 m_isExpandable(false),
50 m_dirtyContentRoles(),
51 m_layout(IconsLayout
),
60 m_sortedVisibleRoles(),
63 m_additionalInfoTextColor(),
66 for (int i
= 0; i
< TextIdCount
; ++i
) {
67 m_text
[i
].setTextFormat(Qt::PlainText
);
68 m_text
[i
].setPerformanceHint(QStaticText::AggressiveCaching
);
72 KFileItemListWidget::~KFileItemListWidget()
76 void KFileItemListWidget::setLayout(Layout layout
)
78 if (m_layout
!= layout
) {
81 updateAdditionalInfoTextColor();
86 KFileItemListWidget::Layout
KFileItemListWidget::layout() const
91 void KFileItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
93 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
95 KItemListWidget::paint(painter
, option
, widget
);
97 // Draw expansion toggle '>' or 'V'
98 if (m_isExpandable
&& !m_expansionArea
.isEmpty()) {
99 QStyleOption arrowOption
;
100 arrowOption
.rect
= m_expansionArea
.toRect();
101 const QStyle::PrimitiveElement arrow
= data()["isExpanded"].toBool()
102 ? QStyle::PE_IndicatorArrowDown
: QStyle::PE_IndicatorArrowRight
;
103 style()->drawPrimitive(arrow
, &arrowOption
, painter
);
106 const KItemListStyleOption
& itemListStyleOption
= styleOption();
108 // Blend the unhovered and hovered pixmap if the hovering
109 // animation is ongoing
110 if (hoverOpacity() < 1.0) {
111 drawPixmap(painter
, m_pixmap
);
114 const qreal opacity
= painter
->opacity();
115 painter
->setOpacity(hoverOpacity() * opacity
);
116 drawPixmap(painter
, m_hoverPixmap
);
117 painter
->setOpacity(opacity
);
119 drawPixmap(painter
, m_pixmap
);
122 painter
->setFont(itemListStyleOption
.font
);
123 painter
->setPen(textColor());
124 painter
->drawStaticText(m_textPos
[Name
], m_text
[Name
]);
126 bool clipAdditionalInfoBounds
= false;
127 if (m_layout
== DetailsLayout
) {
128 // Prevent a possible overlapping of the additional-information texts
129 // with the icon. This can happen if the user has minimized the width
130 // of the name-column to a very small value.
131 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
132 if (m_textPos
[Name
+ 1].x() < minX
) {
133 clipAdditionalInfoBounds
= true;
135 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
139 painter
->setPen(m_additionalInfoTextColor
);
140 painter
->setFont(itemListStyleOption
.font
);
141 for (int i
= Name
+ 1; i
< TextIdCount
; ++i
) {
142 painter
->drawStaticText(m_textPos
[i
], m_text
[i
]);
145 if (clipAdditionalInfoBounds
) {
149 #ifdef KFILEITEMLISTWIDGET_DEBUG
150 painter
->setBrush(Qt::NoBrush
);
151 painter
->setPen(Qt::green
);
152 painter
->drawRect(m_iconRect
);
154 painter
->setPen(Qt::red
);
155 painter
->drawText(QPointF(0, itemListStyleOption
.fontMetrics
.height()), QString::number(index()));
156 painter
->drawRect(rect());
160 QRectF
KFileItemListWidget::iconRect() const
162 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
166 QRectF
KFileItemListWidget::textRect() const
168 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
172 QRectF
KFileItemListWidget::expansionToggleRect() const
174 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
175 return m_isExpandable
? m_expansionArea
: QRectF();
178 QRectF
KFileItemListWidget::selectionToggleRect() const
180 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
182 const int iconHeight
= styleOption().iconSize
;
184 int toggleSize
= KIconLoader::SizeSmall
;
185 if (iconHeight
>= KIconLoader::SizeEnormous
) {
186 toggleSize
= KIconLoader::SizeMedium
;
187 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
188 toggleSize
= KIconLoader::SizeSmallMedium
;
191 QPointF pos
= iconRect().topLeft();
193 // If the selection toggle has a very small distance to the
194 // widget borders, the size of the selection toggle will get
195 // increased to prevent an accidental clicking of the item
196 // when trying to hit the toggle.
197 const int widgetHeight
= size().height();
198 const int widgetWidth
= size().width();
199 const int minMargin
= 2;
201 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
202 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
203 toggleSize
= widgetHeight
;
206 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
207 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
208 toggleSize
= widgetWidth
;
212 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
215 QString
KFileItemListWidget::roleText(const QByteArray
& role
, const QHash
<QByteArray
, QVariant
>& values
)
218 const QVariant roleValue
= values
.value(role
);
220 switch (roleTextId(role
)) {
228 text
= roleValue
.toString();
232 if (values
.value("isDir").toBool()) {
233 // The item represents a directory. Show the number of sub directories
234 // instead of the file size of the directory.
235 if (!roleValue
.isNull()) {
236 const int count
= roleValue
.toInt();
238 text
= i18nc("@item:intable", "Unknown");
240 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
244 // Show the size in kilobytes (always round up)
245 const KLocale
* locale
= KGlobal::locale();
246 const int roundInc
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
) ? 499 : 511;
247 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>() + roundInc
;
248 text
= locale
->formatByteSize(size
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
254 const QDateTime dateTime
= roleValue
.toDateTime();
255 text
= KGlobal::locale()->formatDateTime(dateTime
);
267 void KFileItemListWidget::invalidateCache()
269 m_dirtyLayout
= true;
270 m_dirtyContent
= true;
273 void KFileItemListWidget::refreshCache()
277 void KFileItemListWidget::setTextColor(const QColor
& color
)
279 if (color
!= m_customTextColor
) {
280 m_customTextColor
= color
;
281 updateAdditionalInfoTextColor();
286 QColor
KFileItemListWidget::textColor() const
288 if (m_customTextColor
.isValid() && !isSelected()) {
289 return m_customTextColor
;
292 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
293 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Text
;
294 return styleOption().palette
.brush(group
, role
).color();
297 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
300 m_dirtyContent
= true;
304 QPixmap
KFileItemListWidget::overlay() const
309 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
310 const QSet
<QByteArray
>& roles
)
312 KItemListWidget::dataChanged(current
, roles
);
313 m_dirtyContent
= true;
315 QSet
<QByteArray
> dirtyRoles
;
316 if (roles
.isEmpty()) {
317 dirtyRoles
= visibleRoles().toSet();
318 dirtyRoles
.insert("iconPixmap");
319 dirtyRoles
.insert("iconName");
324 QSetIterator
<QByteArray
> it(dirtyRoles
);
325 while (it
.hasNext()) {
326 const QByteArray
& role
= it
.next();
327 m_dirtyContentRoles
.insert(role
);
331 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
332 const QList
<QByteArray
>& previous
)
334 KItemListWidget::visibleRolesChanged(current
, previous
);
335 m_sortedVisibleRoles
= current
;
336 m_dirtyLayout
= true;
339 void KFileItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
340 const QHash
<QByteArray
, QSizeF
>& previous
)
342 KItemListWidget::visibleRolesSizesChanged(current
, previous
);
343 m_dirtyLayout
= true;
346 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
347 const KItemListStyleOption
& previous
)
349 KItemListWidget::styleOptionChanged(current
, previous
);
350 updateAdditionalInfoTextColor();
351 m_dirtyLayout
= true;
354 void KFileItemListWidget::hoveredChanged(bool hovered
)
357 m_dirtyLayout
= true;
360 void KFileItemListWidget::selectedChanged(bool selected
)
363 updateAdditionalInfoTextColor();
366 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
368 KItemListWidget::resizeEvent(event
);
369 m_dirtyLayout
= true;
372 void KFileItemListWidget::showEvent(QShowEvent
* event
)
374 KItemListWidget::showEvent(event
);
376 // Listen to changes of the clipboard to mark the item as cut/uncut
377 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
379 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
380 m_isCut
= clipboard
->isCut(itemUrl
);
382 connect(clipboard
, SIGNAL(cutItemsChanged()),
383 this, SLOT(slotCutItemsChanged()));
386 void KFileItemListWidget::hideEvent(QHideEvent
* event
)
388 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
389 this, SLOT(slotCutItemsChanged()));
391 KItemListWidget::hideEvent(event
);
394 void KFileItemListWidget::slotCutItemsChanged()
396 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
397 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
398 if (m_isCut
!= isCut
) {
400 m_pixmap
= QPixmap();
401 m_dirtyContent
= true;
406 void KFileItemListWidget::triggerCacheRefreshing()
408 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
414 const QHash
<QByteArray
, QVariant
> values
= data();
415 m_isExpandable
= values
["isExpandable"].toBool();
416 m_isHidden
= values
["name"].toString().startsWith(QLatin1Char('.'));
418 updateExpansionArea();
422 m_dirtyLayout
= false;
423 m_dirtyContent
= false;
424 m_dirtyContentRoles
.clear();
427 void KFileItemListWidget::updateExpansionArea()
429 if (m_layout
== DetailsLayout
) {
430 const QHash
<QByteArray
, QVariant
> values
= data();
431 Q_ASSERT(values
.contains("expansionLevel"));
432 const KItemListStyleOption
& option
= styleOption();
433 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
434 if (expansionLevel
>= 0) {
435 const qreal widgetHeight
= size().height();
436 const qreal expansionLevelSize
= KIconLoader::SizeSmall
;
437 const qreal x
= option
.padding
+ expansionLevel
* widgetHeight
;
438 const qreal y
= (widgetHeight
- expansionLevelSize
) / 2;
439 m_expansionArea
= QRectF(x
, y
, expansionLevelSize
, expansionLevelSize
);
444 m_expansionArea
= QRectF();
447 void KFileItemListWidget::updatePixmapCache()
449 // Precondition: Requires already updated m_textPos values to calculate
450 // the remaining height when the alignment is vertical.
452 const QSizeF widgetSize
= size();
453 const bool iconOnTop
= (m_layout
== IconsLayout
);
454 const KItemListStyleOption
& option
= styleOption();
455 const qreal padding
= option
.padding
;
457 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
458 const int maxIconHeight
= option
.iconSize
;
460 const QHash
<QByteArray
, QVariant
> values
= data();
462 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
463 if (!updatePixmap
&& m_dirtyContent
) {
464 updatePixmap
= m_dirtyContentRoles
.isEmpty()
465 || m_dirtyContentRoles
.contains("iconPixmap")
466 || m_dirtyContentRoles
.contains("iconName")
467 || m_dirtyContentRoles
.contains("iconOverlays");
471 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
472 if (m_pixmap
.isNull()) {
473 // Use the icon that fits to the MIME-type
474 QString iconName
= values
["iconName"].toString();
475 if (iconName
.isEmpty()) {
476 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
477 // use a generic icon as fallback
478 iconName
= QLatin1String("unknown");
480 m_pixmap
= pixmapForIcon(iconName
, maxIconHeight
);
481 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
482 // A custom pixmap has been applied. Assure that the pixmap
483 // is scaled to the maximum available size.
484 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
487 const QStringList overlays
= values
["iconOverlays"].toStringList();
489 // Strangely KFileItem::overlays() returns empty string-values, so
490 // we need to check first whether an overlay must be drawn at all.
491 // It is more efficient to do it here, as KIconLoader::drawOverlays()
492 // assumes that an overlay will be drawn and has some additional
494 foreach (const QString
& overlay
, overlays
) {
495 if (!overlay
.isEmpty()) {
496 // There is at least one overlay, draw all overlays above m_pixmap
497 // and cancel the check
498 KIconLoader::global()->drawOverlays(overlays
, m_pixmap
, KIconLoader::Desktop
);
504 applyCutEffect(m_pixmap
);
508 applyHiddenEffect(m_pixmap
);
512 if (!m_overlay
.isNull()) {
513 QPainter
painter(&m_pixmap
);
514 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
517 int scaledIconSize
= 0;
519 scaledIconSize
= static_cast<int>(m_textPos
[Name
].y() - 2 * padding
);
521 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
522 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
523 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
524 widgetSize
.height() - 2 * padding
: maxIconHeight
;
527 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
528 const int maxScaledIconHeight
= scaledIconSize
;
530 m_scaledPixmapSize
= m_pixmap
.size();
531 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
534 // Center horizontally and align on bottom within the icon-area
535 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
536 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
538 // Center horizontally and vertically within the icon-area
539 m_pixmapPos
.setX(m_textPos
[Name
].x() - 2 * padding
540 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
541 m_pixmapPos
.setY(padding
542 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
545 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
547 // Prepare the pixmap that is used when the item gets hovered
549 m_hoverPixmap
= m_pixmap
;
550 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
551 // In the KIconLoader terminology, active = hover.
552 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
553 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
555 m_hoverPixmap
= m_pixmap
;
557 } else if (hoverOpacity() <= 0.0) {
558 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
559 m_hoverPixmap
= QPixmap();
563 void KFileItemListWidget::updateTextsCache()
565 QTextOption textOption
;
568 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
569 textOption
.setAlignment(Qt::AlignHCenter
);
573 textOption
.setAlignment(Qt::AlignLeft
);
574 textOption
.setWrapMode(QTextOption::NoWrap
);
581 for (int i
= 0; i
< TextIdCount
; ++i
) {
582 m_text
[i
].setText(QString());
583 m_text
[i
].setTextOption(textOption
);
587 case IconsLayout
: updateIconsLayoutTextCache(); break;
588 case CompactLayout
: updateCompactLayoutTextCache(); break;
589 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
590 default: Q_ASSERT(false); break;
594 void KFileItemListWidget::updateIconsLayoutTextCache()
601 // might get wrapped above
606 const QHash
<QByteArray
, QVariant
> values
= data();
608 const KItemListStyleOption
& option
= styleOption();
609 const qreal padding
= option
.padding
;
610 const qreal maxWidth
= size().width() - 2 * padding
;
611 const qreal widgetHeight
= size().height();
612 const qreal fontHeight
= option
.fontMetrics
.height();
614 // Initialize properties for the "name" role. It will be used as anchor
615 // for initializing the position of the other roles.
616 m_text
[Name
].setText(KStringHandler::preProcessWrap(values
["name"].toString()));
618 // Calculate the number of lines required for the name and the required width
619 int textLinesCountForName
= 0;
620 qreal requiredWidthForName
= 0;
623 QTextLayout
layout(m_text
[Name
].text(), option
.font
);
624 layout
.setTextOption(m_text
[Name
].textOption());
625 layout
.beginLayout();
626 while ((line
= layout
.createLine()).isValid()) {
627 line
.setLineWidth(maxWidth
);
628 requiredWidthForName
= qMax(requiredWidthForName
, line
.naturalTextWidth());
629 ++textLinesCountForName
;
633 // Use one line for each additional information
634 int textLinesCount
= textLinesCountForName
;
635 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
636 textLinesCount
+= additionalRolesCount
;
638 m_text
[Name
].setTextWidth(maxWidth
);
639 m_textPos
[Name
] = QPointF(padding
, widgetHeight
- textLinesCount
* fontHeight
- padding
);
640 m_textRect
= QRectF(padding
+ (maxWidth
- requiredWidthForName
) / 2,
642 requiredWidthForName
,
643 textLinesCountForName
* fontHeight
);
645 // Calculate the position for each additional information
646 qreal y
= m_textPos
[Name
].y() + textLinesCountForName
* fontHeight
;
647 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
648 const TextId textId
= roleTextId(role
);
649 if (textId
== Name
) {
653 const QString text
= roleText(role
, values
);
654 m_text
[textId
].setText(text
);
656 qreal requiredWidth
= 0;
658 QTextLayout
layout(text
, option
.font
);
659 layout
.setTextOption(m_text
[textId
].textOption());
660 layout
.beginLayout();
661 QTextLine textLine
= layout
.createLine();
662 if (textLine
.isValid()) {
663 textLine
.setLineWidth(maxWidth
);
664 requiredWidth
= textLine
.naturalTextWidth();
665 if (textLine
.textLength() < text
.length()) {
666 // TODO: QFontMetrics::elidedText() works different regarding the given width
667 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
668 // not get elided although it does not fit into the given width. As workaround
669 // the padding is substracted.
670 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
- padding
);
671 m_text
[textId
].setText(elidedText
);
676 m_textPos
[textId
] = QPointF(padding
, y
);
677 m_text
[textId
].setTextWidth(maxWidth
);
679 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, fontHeight
);
680 m_textRect
|= textRect
;
685 // Add a padding to the text rectangle
686 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
689 void KFileItemListWidget::updateCompactLayoutTextCache()
691 // +------+ Name role
692 // | Icon | Additional role 1
693 // +------+ Additional role 2
695 const QHash
<QByteArray
, QVariant
> values
= data();
697 const KItemListStyleOption
& option
= styleOption();
698 const qreal widgetHeight
= size().height();
699 const qreal fontHeight
= option
.fontMetrics
.height();
700 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * fontHeight
;
701 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
703 qreal maximumRequiredTextWidth
= 0;
704 const qreal x
= option
.padding
* 3 + scaledIconSize
;
705 qreal y
= (widgetHeight
- textLinesHeight
) / 2;
706 const qreal maxWidth
= size().width() - x
- option
.padding
;
707 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
708 const TextId textId
= roleTextId(role
);
710 const QString text
= roleText(role
, values
);
711 m_text
[textId
].setText(text
);
713 qreal requiredWidth
= option
.fontMetrics
.width(text
);
714 if (requiredWidth
> maxWidth
) {
715 requiredWidth
= maxWidth
;
716 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
717 m_text
[textId
].setText(elidedText
);
720 m_textPos
[textId
] = QPointF(x
, y
);
721 m_text
[textId
].setTextWidth(maxWidth
);
723 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
728 m_textRect
= QRectF(x
- option
.padding
, 0, maximumRequiredTextWidth
+ 2 * option
.padding
, widgetHeight
);
731 void KFileItemListWidget::updateDetailsLayoutTextCache()
733 // Precondition: Requires already updated m_expansionArea
734 // to determine the left position.
737 // | Icon | Name role Additional role 1 Additional role 2
739 m_textRect
= QRectF();
741 const KItemListStyleOption
& option
= styleOption();
742 const QHash
<QByteArray
, QVariant
> values
= data();
744 const qreal widgetHeight
= size().height();
745 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
746 const int fontHeight
= option
.fontMetrics
.height();
748 const qreal columnPadding
= option
.padding
* 3;
749 const qreal firstColumnInc
= m_expansionArea
.right() + option
.padding
* 2 + scaledIconSize
;
750 qreal x
= firstColumnInc
;
751 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
753 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
754 const TextId textId
= roleTextId(role
);
756 QString text
= roleText(role
, values
);
758 // Elide the text in case it does not fit into the available column-width
759 qreal requiredWidth
= option
.fontMetrics
.width(text
);
760 const qreal columnWidth
= visibleRolesSizes().value(role
, QSizeF(0, 0)).width();
761 qreal availableTextWidth
= columnWidth
- 2 * columnPadding
;
762 if (textId
== Name
) {
763 availableTextWidth
-= firstColumnInc
;
766 if (requiredWidth
> availableTextWidth
) {
767 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
768 requiredWidth
= option
.fontMetrics
.width(text
);
771 m_text
[textId
].setText(text
);
772 m_textPos
[textId
] = QPointF(x
+ columnPadding
, y
);
777 m_textRect
= QRectF(m_textPos
[textId
].x() - option
.padding
, 0,
778 requiredWidth
+ 2 * option
.padding
, size().height());
780 // The column after the name should always be aligned on the same x-position independent
781 // from the expansion-level shown in the name column
786 // The values for the size should be right aligned
787 m_textPos
[textId
].rx() += columnWidth
- requiredWidth
- 2 * columnPadding
;
796 void KFileItemListWidget::updateAdditionalInfoTextColor()
799 if (m_customTextColor
.isValid()) {
800 c1
= m_customTextColor
;
801 } else if (isSelected() && m_layout
!= DetailsLayout
) {
802 c1
= styleOption().palette
.highlightedText().color();
804 c1
= styleOption().palette
.text().color();
807 // For the color of the additional info the inactive text color
808 // is not used as this might lead to unreadable text for some color schemes. Instead
809 // the text color c1 is slightly mixed with the background color.
810 const QColor c2
= styleOption().palette
.base().color();
812 const int p2
= 100 - p1
;
813 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
814 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
815 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
818 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
820 if (m_scaledPixmapSize
!= pixmap
.size()) {
821 QPixmap scaledPixmap
= pixmap
;
822 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
823 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
825 #ifdef KFILEITEMLISTWIDGET_DEBUG
826 painter
->setPen(Qt::blue
);
827 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
830 painter
->drawPixmap(m_pixmapPos
, pixmap
);
834 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
836 const KIcon
icon(name
);
839 if (size
<= KIconLoader::SizeSmall
) {
840 requestedSize
= KIconLoader::SizeSmall
;
841 } else if (size
<= KIconLoader::SizeSmallMedium
) {
842 requestedSize
= KIconLoader::SizeSmallMedium
;
843 } else if (size
<= KIconLoader::SizeMedium
) {
844 requestedSize
= KIconLoader::SizeMedium
;
845 } else if (size
<= KIconLoader::SizeLarge
) {
846 requestedSize
= KIconLoader::SizeLarge
;
847 } else if (size
<= KIconLoader::SizeHuge
) {
848 requestedSize
= KIconLoader::SizeHuge
;
849 } else if (size
<= KIconLoader::SizeEnormous
) {
850 requestedSize
= KIconLoader::SizeEnormous
;
851 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
852 requestedSize
= KIconLoader::SizeEnormous
* 2;
854 requestedSize
= size
;
857 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
858 if (requestedSize
!= size
) {
859 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
865 KFileItemListWidget::TextId
KFileItemListWidget::roleTextId(const QByteArray
& role
)
867 static QHash
<QByteArray
, TextId
> rolesHash
;
868 if (rolesHash
.isEmpty()) {
869 rolesHash
.insert("name", Name
);
870 rolesHash
.insert("size", Size
);
871 rolesHash
.insert("date", Date
);
872 rolesHash
.insert("permissions", Permissions
);
873 rolesHash
.insert("owner", Owner
);
874 rolesHash
.insert("group", Group
);
875 rolesHash
.insert("type", Type
);
876 rolesHash
.insert("destination", Destination
);
877 rolesHash
.insert("path", Path
);
880 return rolesHash
.value(role
);
883 void KFileItemListWidget::applyCutEffect(QPixmap
& pixmap
)
885 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
886 pixmap
= effect
->apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
889 void KFileItemListWidget::applyHiddenEffect(QPixmap
& pixmap
)
891 KIconEffect::semiTransparent(pixmap
);
894 #include "kfileitemlistwidget.moc"