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
),
55 m_originalPixmapSize(),
61 m_sortedVisibleRoles(),
64 m_additionalInfoTextColor(),
67 for (int i
= 0; i
< TextIdCount
; ++i
) {
68 m_text
[i
].setTextFormat(Qt::PlainText
);
69 m_text
[i
].setPerformanceHint(QStaticText::AggressiveCaching
);
73 KFileItemListWidget::~KFileItemListWidget()
77 void KFileItemListWidget::setLayout(Layout layout
)
79 if (m_layout
!= layout
) {
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
.margin
;
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 minMargin
= 2;
201 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
202 toggleSize
= widgetHeight
;
205 if (toggleSize
+ minMargin
* 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 text
= i18nc("@item:intable", "Unknown");
236 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
237 text
= i18ncp("@item:intable", "%1 item", "%1 items", size
);
240 // Show the size in kilobytes (always round up)
241 const KLocale
* locale
= KGlobal::locale();
242 const int roundInc
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
) ? 499 : 511;
243 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>() + roundInc
;
244 text
= locale
->formatByteSize(size
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
250 const QDateTime dateTime
= roleValue
.toDateTime();
251 text
= KGlobal::locale()->formatDateTime(dateTime
);
263 void KFileItemListWidget::invalidateCache()
265 m_dirtyLayout
= true;
266 m_dirtyContent
= true;
269 void KFileItemListWidget::refreshCache()
273 void KFileItemListWidget::setTextColor(const QColor
& color
)
275 if (color
!= m_customTextColor
) {
276 m_customTextColor
= color
;
277 updateAdditionalInfoTextColor();
282 QColor
KFileItemListWidget::textColor() const
284 if (m_customTextColor
.isValid() && !isSelected()) {
285 return m_customTextColor
;
288 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
289 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Text
;
290 return styleOption().palette
.brush(group
, role
).color();
293 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
296 m_dirtyContent
= true;
300 QPixmap
KFileItemListWidget::overlay() const
305 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
306 const QSet
<QByteArray
>& roles
)
308 KItemListWidget::dataChanged(current
, roles
);
309 m_dirtyContent
= true;
311 QSet
<QByteArray
> dirtyRoles
;
312 if (roles
.isEmpty()) {
313 dirtyRoles
= visibleRoles().toSet();
314 dirtyRoles
.insert("iconPixmap");
315 dirtyRoles
.insert("iconName");
320 QSetIterator
<QByteArray
> it(dirtyRoles
);
321 while (it
.hasNext()) {
322 const QByteArray
& role
= it
.next();
323 m_dirtyContentRoles
.insert(role
);
327 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
328 const QList
<QByteArray
>& previous
)
330 KItemListWidget::visibleRolesChanged(current
, previous
);
331 m_sortedVisibleRoles
= current
;
332 m_dirtyLayout
= true;
335 void KFileItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
336 const QHash
<QByteArray
, QSizeF
>& previous
)
338 KItemListWidget::visibleRolesSizesChanged(current
, previous
);
339 m_dirtyLayout
= true;
342 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
343 const KItemListStyleOption
& previous
)
345 KItemListWidget::styleOptionChanged(current
, previous
);
346 updateAdditionalInfoTextColor();
347 m_dirtyLayout
= true;
350 void KFileItemListWidget::hoveredChanged(bool hovered
)
353 m_dirtyLayout
= true;
356 void KFileItemListWidget::selectedChanged(bool selected
)
359 updateAdditionalInfoTextColor();
362 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
364 KItemListWidget::resizeEvent(event
);
365 m_dirtyLayout
= true;
368 void KFileItemListWidget::showEvent(QShowEvent
* event
)
370 KItemListWidget::showEvent(event
);
372 // Listen to changes of the clipboard to mark the item as cut/uncut
373 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
375 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
376 m_isCut
= clipboard
->isCut(itemUrl
);
378 connect(clipboard
, SIGNAL(cutItemsChanged()),
379 this, SLOT(slotCutItemsChanged()));
382 void KFileItemListWidget::hideEvent(QHideEvent
* event
)
384 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
385 this, SLOT(slotCutItemsChanged()));
387 KItemListWidget::hideEvent(event
);
390 void KFileItemListWidget::slotCutItemsChanged()
392 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
393 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
394 if (m_isCut
!= isCut
) {
396 m_pixmap
= QPixmap();
397 m_dirtyContent
= true;
402 void KFileItemListWidget::triggerCacheRefreshing()
404 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
410 const QHash
<QByteArray
, QVariant
> values
= data();
411 m_isExpandable
= values
["isExpandable"].toBool();
412 m_isHidden
= values
["name"].toString().startsWith(QLatin1Char('.'));
414 updateExpansionArea();
418 m_dirtyLayout
= false;
419 m_dirtyContent
= false;
420 m_dirtyContentRoles
.clear();
423 void KFileItemListWidget::updateExpansionArea()
425 if (m_layout
== DetailsLayout
) {
426 const QHash
<QByteArray
, QVariant
> values
= data();
427 Q_ASSERT(values
.contains("expansionLevel"));
428 const KItemListStyleOption
& option
= styleOption();
429 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
430 if (expansionLevel
>= 0) {
431 const qreal widgetHeight
= size().height();
432 const qreal expansionLevelSize
= KIconLoader::SizeSmall
;
433 const qreal x
= option
.margin
+ expansionLevel
* widgetHeight
;
434 const qreal y
= (widgetHeight
- expansionLevelSize
) / 2;
435 m_expansionArea
= QRectF(x
, y
, expansionLevelSize
, expansionLevelSize
);
440 m_expansionArea
= QRectF();
443 void KFileItemListWidget::updatePixmapCache()
445 // Precondition: Requires already updated m_textPos values to calculate
446 // the remaining height when the alignment is vertical.
448 const bool iconOnTop
= (m_layout
== IconsLayout
);
449 const KItemListStyleOption
& option
= styleOption();
450 const int iconHeight
= option
.iconSize
;
452 const QHash
<QByteArray
, QVariant
> values
= data();
453 const QSizeF widgetSize
= size();
455 int scaledIconHeight
= 0;
457 scaledIconHeight
= static_cast<int>(m_textPos
[Name
].y() - 3 * option
.margin
);
459 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
460 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
461 scaledIconHeight
= (requiredTextHeight
< iconHeight
) ? widgetSize
.height() - 2 * option
.margin
: iconHeight
;
464 bool updatePixmap
= (iconHeight
!= m_pixmap
.height());
465 if (!updatePixmap
&& m_dirtyContent
) {
466 updatePixmap
= m_dirtyContentRoles
.isEmpty()
467 || m_dirtyContentRoles
.contains("iconPixmap")
468 || m_dirtyContentRoles
.contains("iconName")
469 || m_dirtyContentRoles
.contains("iconOverlays");
473 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
474 if (m_pixmap
.isNull()) {
475 // Use the icon that fits to the MIME-type
476 QString iconName
= values
["iconName"].toString();
477 if (iconName
.isEmpty()) {
478 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
479 // use a generic icon as fallback
480 iconName
= QLatin1String("unknown");
482 m_pixmap
= pixmapForIcon(iconName
, iconHeight
);
483 m_originalPixmapSize
= m_pixmap
.size();
484 } else if (m_pixmap
.size() != QSize(iconHeight
, iconHeight
)) {
485 // A custom pixmap has been applied. Assure that the pixmap
486 // is scaled to the available size.
487 const bool scale
= m_pixmap
.width() > iconHeight
|| m_pixmap
.height() > iconHeight
||
488 (m_pixmap
.width() < iconHeight
&& m_pixmap
.height() < iconHeight
);
490 KPixmapModifier::scale(m_pixmap
, QSize(iconHeight
, iconHeight
));
492 m_originalPixmapSize
= m_pixmap
.size();
494 // To simplify the handling of scaling the original pixmap
495 // will be embedded into a square pixmap.
496 QPixmap
squarePixmap(iconHeight
, iconHeight
);
497 squarePixmap
.fill(Qt::transparent
);
499 QPainter
painter(&squarePixmap
);
500 const int x
= (iconHeight
- m_pixmap
.width()) / 2; // Center horizontally
501 int y
= iconHeight
- m_pixmap
.height(); // Move to bottom
503 y
/= 2.0; // Center vertically
505 painter
.drawPixmap(x
, y
, m_pixmap
);
507 m_pixmap
= squarePixmap
;
509 m_originalPixmapSize
= m_pixmap
.size();
512 const QStringList overlays
= values
["iconOverlays"].toStringList();
514 // Strangely KFileItem::overlays() returns empty string-values, so
515 // we need to check first whether an overlay must be drawn at all.
516 // It is more efficient to do it here, as KIconLoader::drawOverlays()
517 // assumes that an overlay will be drawn and has some additional
519 foreach (const QString
& overlay
, overlays
) {
520 if (!overlay
.isEmpty()) {
521 // There is at least one overlay, draw all overlays above m_pixmap
522 // and cancel the check
523 KIconLoader::global()->drawOverlays(overlays
, m_pixmap
, KIconLoader::Desktop
);
529 applyCutEffect(m_pixmap
);
533 applyHiddenEffect(m_pixmap
);
536 Q_ASSERT(m_pixmap
.height() == iconHeight
);
538 if (!m_overlay
.isNull()) {
539 QPainter
painter(&m_pixmap
);
540 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
543 m_scaledPixmapSize
= QSize(scaledIconHeight
, scaledIconHeight
);
546 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
548 m_pixmapPos
.setX(m_textPos
[Name
].x() - 2 * option
.margin
- scaledIconHeight
);
550 m_pixmapPos
.setY(option
.margin
);
552 // Center the hover rectangle horizontally and align it on bottom
553 qreal hoverWidth
= m_originalPixmapSize
.width();
554 qreal hoverHeight
= m_originalPixmapSize
.height();
555 if (scaledIconHeight
!= m_pixmap
.height()) {
556 const qreal scaleFactor
= qreal(scaledIconHeight
) / qreal(m_pixmap
.height());
557 hoverWidth
*= scaleFactor
;
558 hoverHeight
*= scaleFactor
;
560 const qreal hoverX
= m_pixmapPos
.x() + (m_scaledPixmapSize
.width() - hoverWidth
) / 2.0;
561 qreal hoverY
= m_scaledPixmapSize
.height() - hoverHeight
;
565 hoverY
+= m_pixmapPos
.y();
567 m_iconRect
= QRectF(hoverX
, hoverY
, hoverWidth
, hoverHeight
);
568 const qreal margin
= option
.margin
;
569 m_iconRect
.adjust(-margin
, -margin
, margin
, margin
);
571 // Prepare the pixmap that is used when the item gets hovered
573 m_hoverPixmap
= m_pixmap
;
574 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
575 // In the KIconLoader terminology, active = hover.
576 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
577 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
579 m_hoverPixmap
= m_pixmap
;
581 } else if (hoverOpacity() <= 0.0) {
582 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
583 m_hoverPixmap
= QPixmap();
587 void KFileItemListWidget::updateTextsCache()
589 QTextOption textOption
;
592 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
593 textOption
.setAlignment(Qt::AlignHCenter
);
597 textOption
.setAlignment(Qt::AlignLeft
);
598 textOption
.setWrapMode(QTextOption::NoWrap
);
605 for (int i
= 0; i
< TextIdCount
; ++i
) {
606 m_text
[i
].setText(QString());
607 m_text
[i
].setTextOption(textOption
);
611 case IconsLayout
: updateIconsLayoutTextCache(); break;
612 case CompactLayout
: updateCompactLayoutTextCache(); break;
613 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
614 default: Q_ASSERT(false); break;
618 void KFileItemListWidget::updateIconsLayoutTextCache()
625 // might get wrapped above
630 const QHash
<QByteArray
, QVariant
> values
= data();
632 const KItemListStyleOption
& option
= styleOption();
633 const qreal maxWidth
= size().width() - 2 * option
.margin
;
634 const qreal widgetHeight
= size().height();
635 const qreal fontHeight
= option
.fontMetrics
.height();
637 // Initialize properties for the "name" role. It will be used as anchor
638 // for initializing the position of the other roles.
639 m_text
[Name
].setText(KStringHandler::preProcessWrap(values
["name"].toString()));
641 // Calculate the number of lines required for the name and the required width
642 int textLinesCountForName
= 0;
643 qreal requiredWidthForName
= 0;
646 QTextLayout
layout(m_text
[Name
].text(), option
.font
);
647 layout
.setTextOption(m_text
[Name
].textOption());
648 layout
.beginLayout();
649 while ((line
= layout
.createLine()).isValid()) {
650 line
.setLineWidth(maxWidth
);
651 requiredWidthForName
= qMax(requiredWidthForName
, line
.naturalTextWidth());
652 ++textLinesCountForName
;
656 // Use one line for each additional information
657 int textLinesCount
= textLinesCountForName
;
658 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
659 textLinesCount
+= additionalRolesCount
;
661 m_text
[Name
].setTextWidth(maxWidth
);
662 m_textPos
[Name
] = QPointF(option
.margin
, widgetHeight
- textLinesCount
* fontHeight
- option
.margin
);
663 m_textRect
= QRectF(option
.margin
+ (maxWidth
- requiredWidthForName
) / 2,
665 requiredWidthForName
,
666 textLinesCountForName
* fontHeight
);
668 // Calculate the position for each additional information
669 qreal y
= m_textPos
[Name
].y() + textLinesCountForName
* fontHeight
;
670 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
671 const TextId textId
= roleTextId(role
);
672 if (textId
== Name
) {
676 const QString text
= roleText(role
, values
);
677 m_text
[textId
].setText(text
);
679 qreal requiredWidth
= 0;
681 QTextLayout
layout(text
, option
.font
);
682 layout
.setTextOption(m_text
[textId
].textOption());
683 layout
.beginLayout();
684 QTextLine textLine
= layout
.createLine();
685 if (textLine
.isValid()) {
686 textLine
.setLineWidth(maxWidth
);
687 requiredWidth
= textLine
.naturalTextWidth();
688 if (textLine
.textLength() < text
.length()) {
689 // TODO: QFontMetrics::elidedText() works different regarding the given width
690 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
691 // not get elided although it does not fit into the given width. As workaround
692 // the margin is substracted.
693 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
- option
.margin
);
694 m_text
[textId
].setText(elidedText
);
699 m_textPos
[textId
] = QPointF(option
.margin
, y
);
700 m_text
[textId
].setTextWidth(maxWidth
);
702 const QRectF
textRect(option
.margin
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, fontHeight
);
703 m_textRect
|= textRect
;
708 // Add a margin to the text rectangle
709 const qreal margin
= option
.margin
;
710 m_textRect
.adjust(-margin
, -margin
, margin
, margin
);
713 void KFileItemListWidget::updateCompactLayoutTextCache()
715 // +------+ Name role
716 // | Icon | Additional role 1
717 // +------+ Additional role 2
719 const QHash
<QByteArray
, QVariant
> values
= data();
721 const KItemListStyleOption
& option
= styleOption();
722 const qreal widgetHeight
= size().height();
723 const qreal fontHeight
= option
.fontMetrics
.height();
724 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * fontHeight
;
725 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.margin
: option
.iconSize
;
727 qreal maximumRequiredTextWidth
= 0;
728 const qreal x
= option
.margin
* 3 + scaledIconSize
;
729 qreal y
= (widgetHeight
- textLinesHeight
) / 2;
730 const qreal maxWidth
= size().width() - x
- option
.margin
;
731 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
732 const TextId textId
= roleTextId(role
);
734 const QString text
= roleText(role
, values
);
735 m_text
[textId
].setText(text
);
737 qreal requiredWidth
= option
.fontMetrics
.width(text
);
738 if (requiredWidth
> maxWidth
) {
739 requiredWidth
= maxWidth
;
740 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
741 m_text
[textId
].setText(elidedText
);
744 m_textPos
[textId
] = QPointF(x
, y
);
745 m_text
[textId
].setTextWidth(maxWidth
);
747 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
752 m_textRect
= QRectF(x
- option
.margin
, 0, maximumRequiredTextWidth
+ 2 * option
.margin
, widgetHeight
);
755 void KFileItemListWidget::updateDetailsLayoutTextCache()
757 // Precondition: Requires already updated m_expansionArea
758 // to determine the left position.
761 // | Icon | Name role Additional role 1 Additional role 2
763 m_textRect
= QRectF();
765 const KItemListStyleOption
& option
= styleOption();
766 const QHash
<QByteArray
, QVariant
> values
= data();
768 const qreal widgetHeight
= size().height();
769 const int scaledIconSize
= widgetHeight
- 2 * option
.margin
;
770 const int fontHeight
= option
.fontMetrics
.height();
772 const qreal columnMargin
= option
.margin
* 3;
773 const qreal firstColumnInc
= m_expansionArea
.right() + option
.margin
* 2 + scaledIconSize
;
774 qreal x
= firstColumnInc
;
775 const qreal y
= qMax(qreal(option
.margin
), (widgetHeight
- fontHeight
) / 2);
777 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
778 const TextId textId
= roleTextId(role
);
780 QString text
= roleText(role
, values
);
782 // Elide the text in case it does not fit into the available column-width
783 qreal requiredWidth
= option
.fontMetrics
.width(text
);
784 const qreal columnWidth
= visibleRolesSizes().value(role
, QSizeF(0, 0)).width();
785 qreal availableTextWidth
= columnWidth
- 2 * columnMargin
;
786 if (textId
== Name
) {
787 availableTextWidth
-= firstColumnInc
;
790 if (requiredWidth
> availableTextWidth
) {
791 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
792 requiredWidth
= option
.fontMetrics
.width(text
);
795 m_text
[textId
].setText(text
);
796 m_textPos
[textId
] = QPointF(x
+ columnMargin
, y
);
801 m_textRect
= QRectF(m_textPos
[textId
].x() - option
.margin
, 0,
802 requiredWidth
+ 2 * option
.margin
, size().height());
804 // The column after the name should always be aligned on the same x-position independent
805 // from the expansion-level shown in the name column
810 // The values for the size should be right aligned
811 m_textPos
[textId
].rx() += columnWidth
- requiredWidth
- 2 * columnMargin
;
820 void KFileItemListWidget::updateAdditionalInfoTextColor()
823 if (m_customTextColor
.isValid()) {
824 c1
= m_customTextColor
;
825 } else if (isSelected() && m_layout
!= DetailsLayout
) {
826 c1
= styleOption().palette
.highlightedText().color();
828 c1
= styleOption().palette
.text().color();
831 // For the color of the additional info the inactive text color
832 // is not used as this might lead to unreadable text for some color schemes. Instead
833 // the text color c1 is slightly mixed with the background color.
834 const QColor c2
= styleOption().palette
.base().color();
836 const int p2
= 100 - p1
;
837 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
838 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
839 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
842 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
844 if (m_scaledPixmapSize
!= pixmap
.size()) {
845 QPixmap scaledPixmap
= pixmap
;
846 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
847 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
849 #ifdef KFILEITEMLISTWIDGET_DEBUG
850 painter
->setPen(Qt::blue
);
851 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(scaledPixmap
.size())));
854 painter
->drawPixmap(m_pixmapPos
, pixmap
);
858 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
860 const KIcon
icon(name
);
863 if (size
<= KIconLoader::SizeSmall
) {
864 requestedSize
= KIconLoader::SizeSmall
;
865 } else if (size
<= KIconLoader::SizeSmallMedium
) {
866 requestedSize
= KIconLoader::SizeSmallMedium
;
867 } else if (size
<= KIconLoader::SizeMedium
) {
868 requestedSize
= KIconLoader::SizeMedium
;
869 } else if (size
<= KIconLoader::SizeLarge
) {
870 requestedSize
= KIconLoader::SizeLarge
;
871 } else if (size
<= KIconLoader::SizeHuge
) {
872 requestedSize
= KIconLoader::SizeHuge
;
873 } else if (size
<= KIconLoader::SizeEnormous
) {
874 requestedSize
= KIconLoader::SizeEnormous
;
875 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
876 requestedSize
= KIconLoader::SizeEnormous
* 2;
878 requestedSize
= size
;
881 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
882 if (requestedSize
!= size
) {
883 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
889 KFileItemListWidget::TextId
KFileItemListWidget::roleTextId(const QByteArray
& role
)
891 static QHash
<QByteArray
, TextId
> rolesHash
;
892 if (rolesHash
.isEmpty()) {
893 rolesHash
.insert("name", Name
);
894 rolesHash
.insert("size", Size
);
895 rolesHash
.insert("date", Date
);
896 rolesHash
.insert("permissions", Permissions
);
897 rolesHash
.insert("owner", Owner
);
898 rolesHash
.insert("group", Group
);
899 rolesHash
.insert("type", Type
);
900 rolesHash
.insert("destination", Destination
);
901 rolesHash
.insert("path", Path
);
904 return rolesHash
.value(role
);
907 void KFileItemListWidget::applyCutEffect(QPixmap
& pixmap
)
909 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
910 pixmap
= effect
->apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
913 void KFileItemListWidget::applyHiddenEffect(QPixmap
& pixmap
)
915 KIconEffect::semiTransparent(pixmap
);
918 #include "kfileitemlistwidget.moc"