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
= m_pixmap
.height();
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 minPadding
= 2;
201 if (toggleSize
+ minPadding
* 2 >= widgetHeight
) {
202 toggleSize
= widgetHeight
;
205 if (toggleSize
+ minPadding
* 2 >= widgetWidth
) {
206 toggleSize
= widgetWidth
;
210 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
213 QString
KFileItemListWidget::roleText(const QByteArray
& role
, const QHash
<QByteArray
, QVariant
>& values
)
216 const QVariant roleValue
= values
.value(role
);
218 switch (roleTextId(role
)) {
226 text
= roleValue
.toString();
230 if (values
.value("isDir").toBool()) {
231 // The item represents a directory. Show the number of sub directories
232 // instead of the file size of the directory.
233 if (!roleValue
.isNull()) {
234 const int count
= roleValue
.toInt();
236 text
= i18nc("@item:intable", "Unknown");
238 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
242 // Show the size in kilobytes (always round up)
243 const KLocale
* locale
= KGlobal::locale();
244 const int roundInc
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
) ? 499 : 511;
245 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>() + roundInc
;
246 text
= locale
->formatByteSize(size
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
252 const QDateTime dateTime
= roleValue
.toDateTime();
253 text
= KGlobal::locale()->formatDateTime(dateTime
);
265 void KFileItemListWidget::invalidateCache()
267 m_dirtyLayout
= true;
268 m_dirtyContent
= true;
271 void KFileItemListWidget::refreshCache()
275 void KFileItemListWidget::setTextColor(const QColor
& color
)
277 if (color
!= m_customTextColor
) {
278 m_customTextColor
= color
;
279 updateAdditionalInfoTextColor();
284 QColor
KFileItemListWidget::textColor() const
286 if (m_customTextColor
.isValid() && !isSelected()) {
287 return m_customTextColor
;
290 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
291 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Text
;
292 return styleOption().palette
.brush(group
, role
).color();
295 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
298 m_dirtyContent
= true;
302 QPixmap
KFileItemListWidget::overlay() const
307 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
308 const QSet
<QByteArray
>& roles
)
310 KItemListWidget::dataChanged(current
, roles
);
311 m_dirtyContent
= true;
313 QSet
<QByteArray
> dirtyRoles
;
314 if (roles
.isEmpty()) {
315 dirtyRoles
= visibleRoles().toSet();
316 dirtyRoles
.insert("iconPixmap");
317 dirtyRoles
.insert("iconName");
322 QSetIterator
<QByteArray
> it(dirtyRoles
);
323 while (it
.hasNext()) {
324 const QByteArray
& role
= it
.next();
325 m_dirtyContentRoles
.insert(role
);
329 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
330 const QList
<QByteArray
>& previous
)
332 KItemListWidget::visibleRolesChanged(current
, previous
);
333 m_sortedVisibleRoles
= current
;
334 m_dirtyLayout
= true;
337 void KFileItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
338 const QHash
<QByteArray
, QSizeF
>& previous
)
340 KItemListWidget::visibleRolesSizesChanged(current
, previous
);
341 m_dirtyLayout
= true;
344 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
345 const KItemListStyleOption
& previous
)
347 KItemListWidget::styleOptionChanged(current
, previous
);
348 updateAdditionalInfoTextColor();
349 m_dirtyLayout
= true;
352 void KFileItemListWidget::hoveredChanged(bool hovered
)
355 m_dirtyLayout
= true;
358 void KFileItemListWidget::selectedChanged(bool selected
)
361 updateAdditionalInfoTextColor();
364 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
366 KItemListWidget::resizeEvent(event
);
367 m_dirtyLayout
= true;
370 void KFileItemListWidget::showEvent(QShowEvent
* event
)
372 KItemListWidget::showEvent(event
);
374 // Listen to changes of the clipboard to mark the item as cut/uncut
375 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
377 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
378 m_isCut
= clipboard
->isCut(itemUrl
);
380 connect(clipboard
, SIGNAL(cutItemsChanged()),
381 this, SLOT(slotCutItemsChanged()));
384 void KFileItemListWidget::hideEvent(QHideEvent
* event
)
386 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
387 this, SLOT(slotCutItemsChanged()));
389 KItemListWidget::hideEvent(event
);
392 void KFileItemListWidget::slotCutItemsChanged()
394 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
395 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
396 if (m_isCut
!= isCut
) {
398 m_pixmap
= QPixmap();
399 m_dirtyContent
= true;
404 void KFileItemListWidget::triggerCacheRefreshing()
406 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
412 const QHash
<QByteArray
, QVariant
> values
= data();
413 m_isExpandable
= values
["isExpandable"].toBool();
414 m_isHidden
= values
["name"].toString().startsWith(QLatin1Char('.'));
416 updateExpansionArea();
420 m_dirtyLayout
= false;
421 m_dirtyContent
= false;
422 m_dirtyContentRoles
.clear();
425 void KFileItemListWidget::updateExpansionArea()
427 if (m_layout
== DetailsLayout
) {
428 const QHash
<QByteArray
, QVariant
> values
= data();
429 Q_ASSERT(values
.contains("expansionLevel"));
430 const KItemListStyleOption
& option
= styleOption();
431 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
432 if (expansionLevel
>= 0) {
433 const qreal widgetHeight
= size().height();
434 const qreal expansionLevelSize
= KIconLoader::SizeSmall
;
435 const qreal x
= option
.padding
+ expansionLevel
* widgetHeight
;
436 const qreal y
= (widgetHeight
- expansionLevelSize
) / 2;
437 m_expansionArea
= QRectF(x
, y
, expansionLevelSize
, expansionLevelSize
);
442 m_expansionArea
= QRectF();
445 void KFileItemListWidget::updatePixmapCache()
447 // Precondition: Requires already updated m_textPos values to calculate
448 // the remaining height when the alignment is vertical.
450 const QSizeF widgetSize
= size();
451 const bool iconOnTop
= (m_layout
== IconsLayout
);
452 const KItemListStyleOption
& option
= styleOption();
453 const qreal padding
= option
.padding
;
455 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
456 const int maxIconHeight
= option
.iconSize
;
458 const QHash
<QByteArray
, QVariant
> values
= data();
460 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
461 if (!updatePixmap
&& m_dirtyContent
) {
462 updatePixmap
= m_dirtyContentRoles
.isEmpty()
463 || m_dirtyContentRoles
.contains("iconPixmap")
464 || m_dirtyContentRoles
.contains("iconName")
465 || m_dirtyContentRoles
.contains("iconOverlays");
469 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
470 if (m_pixmap
.isNull()) {
471 // Use the icon that fits to the MIME-type
472 QString iconName
= values
["iconName"].toString();
473 if (iconName
.isEmpty()) {
474 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
475 // use a generic icon as fallback
476 iconName
= QLatin1String("unknown");
478 m_pixmap
= pixmapForIcon(iconName
, maxIconHeight
);
479 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
480 // A custom pixmap has been applied. Assure that the pixmap
481 // is scaled to the maximum available size.
482 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
485 const QStringList overlays
= values
["iconOverlays"].toStringList();
487 // Strangely KFileItem::overlays() returns empty string-values, so
488 // we need to check first whether an overlay must be drawn at all.
489 // It is more efficient to do it here, as KIconLoader::drawOverlays()
490 // assumes that an overlay will be drawn and has some additional
492 foreach (const QString
& overlay
, overlays
) {
493 if (!overlay
.isEmpty()) {
494 // There is at least one overlay, draw all overlays above m_pixmap
495 // and cancel the check
496 KIconLoader::global()->drawOverlays(overlays
, m_pixmap
, KIconLoader::Desktop
);
502 applyCutEffect(m_pixmap
);
506 applyHiddenEffect(m_pixmap
);
510 if (!m_overlay
.isNull()) {
511 QPainter
painter(&m_pixmap
);
512 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
515 int scaledIconSize
= 0;
517 scaledIconSize
= static_cast<int>(m_textPos
[Name
].y() - 2 * padding
);
519 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
520 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
521 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
522 widgetSize
.height() - 2 * padding
: maxIconHeight
;
525 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
526 const int maxScaledIconHeight
= scaledIconSize
;
528 m_scaledPixmapSize
= m_pixmap
.size();
529 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
532 // Center horizontally and align on bottom within the icon-area
533 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
534 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
536 // Center horizontally and vertically within the icon-area
537 m_pixmapPos
.setX(m_textPos
[Name
].x() - 2 * padding
538 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
539 m_pixmapPos
.setY(padding
540 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
543 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
544 m_iconRect
.adjust(-padding
, -padding
, padding
, padding
);
546 // Prepare the pixmap that is used when the item gets hovered
548 m_hoverPixmap
= m_pixmap
;
549 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
550 // In the KIconLoader terminology, active = hover.
551 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
552 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
554 m_hoverPixmap
= m_pixmap
;
556 } else if (hoverOpacity() <= 0.0) {
557 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
558 m_hoverPixmap
= QPixmap();
562 void KFileItemListWidget::updateTextsCache()
564 QTextOption textOption
;
567 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
568 textOption
.setAlignment(Qt::AlignHCenter
);
572 textOption
.setAlignment(Qt::AlignLeft
);
573 textOption
.setWrapMode(QTextOption::NoWrap
);
580 for (int i
= 0; i
< TextIdCount
; ++i
) {
581 m_text
[i
].setText(QString());
582 m_text
[i
].setTextOption(textOption
);
586 case IconsLayout
: updateIconsLayoutTextCache(); break;
587 case CompactLayout
: updateCompactLayoutTextCache(); break;
588 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
589 default: Q_ASSERT(false); break;
593 void KFileItemListWidget::updateIconsLayoutTextCache()
600 // might get wrapped above
605 const QHash
<QByteArray
, QVariant
> values
= data();
607 const KItemListStyleOption
& option
= styleOption();
608 const qreal padding
= option
.padding
;
609 const qreal maxWidth
= size().width() - 2 * padding
;
610 const qreal widgetHeight
= size().height();
611 const qreal fontHeight
= option
.fontMetrics
.height();
613 // Initialize properties for the "name" role. It will be used as anchor
614 // for initializing the position of the other roles.
615 m_text
[Name
].setText(KStringHandler::preProcessWrap(values
["name"].toString()));
617 // Calculate the number of lines required for the name and the required width
618 int textLinesCountForName
= 0;
619 qreal requiredWidthForName
= 0;
622 QTextLayout
layout(m_text
[Name
].text(), option
.font
);
623 layout
.setTextOption(m_text
[Name
].textOption());
624 layout
.beginLayout();
625 while ((line
= layout
.createLine()).isValid()) {
626 line
.setLineWidth(maxWidth
);
627 requiredWidthForName
= qMax(requiredWidthForName
, line
.naturalTextWidth());
628 ++textLinesCountForName
;
632 // Use one line for each additional information
633 int textLinesCount
= textLinesCountForName
;
634 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
635 textLinesCount
+= additionalRolesCount
;
637 m_text
[Name
].setTextWidth(maxWidth
);
638 m_textPos
[Name
] = QPointF(padding
, widgetHeight
- textLinesCount
* fontHeight
- padding
);
639 m_textRect
= QRectF(padding
+ (maxWidth
- requiredWidthForName
) / 2,
641 requiredWidthForName
,
642 textLinesCountForName
* fontHeight
);
644 // Calculate the position for each additional information
645 qreal y
= m_textPos
[Name
].y() + textLinesCountForName
* fontHeight
;
646 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
647 const TextId textId
= roleTextId(role
);
648 if (textId
== Name
) {
652 const QString text
= roleText(role
, values
);
653 m_text
[textId
].setText(text
);
655 qreal requiredWidth
= 0;
657 QTextLayout
layout(text
, option
.font
);
658 layout
.setTextOption(m_text
[textId
].textOption());
659 layout
.beginLayout();
660 QTextLine textLine
= layout
.createLine();
661 if (textLine
.isValid()) {
662 textLine
.setLineWidth(maxWidth
);
663 requiredWidth
= textLine
.naturalTextWidth();
664 if (textLine
.textLength() < text
.length()) {
665 // TODO: QFontMetrics::elidedText() works different regarding the given width
666 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
667 // not get elided although it does not fit into the given width. As workaround
668 // the padding is substracted.
669 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
- padding
);
670 m_text
[textId
].setText(elidedText
);
675 m_textPos
[textId
] = QPointF(padding
, y
);
676 m_text
[textId
].setTextWidth(maxWidth
);
678 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, fontHeight
);
679 m_textRect
|= textRect
;
684 // Add a padding to the text rectangle
685 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
688 void KFileItemListWidget::updateCompactLayoutTextCache()
690 // +------+ Name role
691 // | Icon | Additional role 1
692 // +------+ Additional role 2
694 const QHash
<QByteArray
, QVariant
> values
= data();
696 const KItemListStyleOption
& option
= styleOption();
697 const qreal widgetHeight
= size().height();
698 const qreal fontHeight
= option
.fontMetrics
.height();
699 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * fontHeight
;
700 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
702 qreal maximumRequiredTextWidth
= 0;
703 const qreal x
= option
.padding
* 3 + scaledIconSize
;
704 qreal y
= (widgetHeight
- textLinesHeight
) / 2;
705 const qreal maxWidth
= size().width() - x
- option
.padding
;
706 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
707 const TextId textId
= roleTextId(role
);
709 const QString text
= roleText(role
, values
);
710 m_text
[textId
].setText(text
);
712 qreal requiredWidth
= option
.fontMetrics
.width(text
);
713 if (requiredWidth
> maxWidth
) {
714 requiredWidth
= maxWidth
;
715 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
716 m_text
[textId
].setText(elidedText
);
719 m_textPos
[textId
] = QPointF(x
, y
);
720 m_text
[textId
].setTextWidth(maxWidth
);
722 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
727 m_textRect
= QRectF(x
- option
.padding
, 0, maximumRequiredTextWidth
+ 2 * option
.padding
, widgetHeight
);
730 void KFileItemListWidget::updateDetailsLayoutTextCache()
732 // Precondition: Requires already updated m_expansionArea
733 // to determine the left position.
736 // | Icon | Name role Additional role 1 Additional role 2
738 m_textRect
= QRectF();
740 const KItemListStyleOption
& option
= styleOption();
741 const QHash
<QByteArray
, QVariant
> values
= data();
743 const qreal widgetHeight
= size().height();
744 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
745 const int fontHeight
= option
.fontMetrics
.height();
747 const qreal columnPadding
= option
.padding
* 3;
748 const qreal firstColumnInc
= m_expansionArea
.right() + option
.padding
* 2 + scaledIconSize
;
749 qreal x
= firstColumnInc
;
750 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
752 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
753 const TextId textId
= roleTextId(role
);
755 QString text
= roleText(role
, values
);
757 // Elide the text in case it does not fit into the available column-width
758 qreal requiredWidth
= option
.fontMetrics
.width(text
);
759 const qreal columnWidth
= visibleRolesSizes().value(role
, QSizeF(0, 0)).width();
760 qreal availableTextWidth
= columnWidth
- 2 * columnPadding
;
761 if (textId
== Name
) {
762 availableTextWidth
-= firstColumnInc
;
765 if (requiredWidth
> availableTextWidth
) {
766 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
767 requiredWidth
= option
.fontMetrics
.width(text
);
770 m_text
[textId
].setText(text
);
771 m_textPos
[textId
] = QPointF(x
+ columnPadding
, y
);
776 m_textRect
= QRectF(m_textPos
[textId
].x() - option
.padding
, 0,
777 requiredWidth
+ 2 * option
.padding
, size().height());
779 // The column after the name should always be aligned on the same x-position independent
780 // from the expansion-level shown in the name column
785 // The values for the size should be right aligned
786 m_textPos
[textId
].rx() += columnWidth
- requiredWidth
- 2 * columnPadding
;
795 void KFileItemListWidget::updateAdditionalInfoTextColor()
798 if (m_customTextColor
.isValid()) {
799 c1
= m_customTextColor
;
800 } else if (isSelected() && m_layout
!= DetailsLayout
) {
801 c1
= styleOption().palette
.highlightedText().color();
803 c1
= styleOption().palette
.text().color();
806 // For the color of the additional info the inactive text color
807 // is not used as this might lead to unreadable text for some color schemes. Instead
808 // the text color c1 is slightly mixed with the background color.
809 const QColor c2
= styleOption().palette
.base().color();
811 const int p2
= 100 - p1
;
812 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
813 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
814 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
817 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
819 if (m_scaledPixmapSize
!= pixmap
.size()) {
820 QPixmap scaledPixmap
= pixmap
;
821 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
822 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
824 #ifdef KFILEITEMLISTWIDGET_DEBUG
825 painter
->setPen(Qt::blue
);
826 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
829 painter
->drawPixmap(m_pixmapPos
, pixmap
);
833 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
835 const KIcon
icon(name
);
838 if (size
<= KIconLoader::SizeSmall
) {
839 requestedSize
= KIconLoader::SizeSmall
;
840 } else if (size
<= KIconLoader::SizeSmallMedium
) {
841 requestedSize
= KIconLoader::SizeSmallMedium
;
842 } else if (size
<= KIconLoader::SizeMedium
) {
843 requestedSize
= KIconLoader::SizeMedium
;
844 } else if (size
<= KIconLoader::SizeLarge
) {
845 requestedSize
= KIconLoader::SizeLarge
;
846 } else if (size
<= KIconLoader::SizeHuge
) {
847 requestedSize
= KIconLoader::SizeHuge
;
848 } else if (size
<= KIconLoader::SizeEnormous
) {
849 requestedSize
= KIconLoader::SizeEnormous
;
850 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
851 requestedSize
= KIconLoader::SizeEnormous
* 2;
853 requestedSize
= size
;
856 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
857 if (requestedSize
!= size
) {
858 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
864 KFileItemListWidget::TextId
KFileItemListWidget::roleTextId(const QByteArray
& role
)
866 static QHash
<QByteArray
, TextId
> rolesHash
;
867 if (rolesHash
.isEmpty()) {
868 rolesHash
.insert("name", Name
);
869 rolesHash
.insert("size", Size
);
870 rolesHash
.insert("date", Date
);
871 rolesHash
.insert("permissions", Permissions
);
872 rolesHash
.insert("owner", Owner
);
873 rolesHash
.insert("group", Group
);
874 rolesHash
.insert("type", Type
);
875 rolesHash
.insert("destination", Destination
);
876 rolesHash
.insert("path", Path
);
879 return rolesHash
.value(role
);
882 void KFileItemListWidget::applyCutEffect(QPixmap
& pixmap
)
884 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
885 pixmap
= effect
->apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
888 void KFileItemListWidget::applyHiddenEffect(QPixmap
& pixmap
)
890 KIconEffect::semiTransparent(pixmap
);
893 #include "kfileitemlistwidget.moc"