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'
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
->setPen(Qt::red
);
151 painter
->setBrush(Qt::NoBrush
);
152 painter
->drawText(QPointF(0, itemListStyleOption
.fontMetrics
.height()), QString::number(index()));
153 painter
->drawRect(rect());
157 QRectF
KFileItemListWidget::iconRect() const
159 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
163 QRectF
KFileItemListWidget::textRect() const
165 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
169 QRectF
KFileItemListWidget::expansionToggleRect() const
171 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
172 return m_isExpandable
? m_expansionArea
: QRectF();
175 QRectF
KFileItemListWidget::selectionToggleRect() const
177 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
179 const int iconHeight
= m_pixmap
.height();
181 int toggleSize
= KIconLoader::SizeSmall
;
182 if (iconHeight
>= KIconLoader::SizeEnormous
) {
183 toggleSize
= KIconLoader::SizeMedium
;
184 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
185 toggleSize
= KIconLoader::SizeSmallMedium
;
188 QPointF pos
= iconRect().topLeft();
190 // If the selection toggle has a very small distance to the
191 // widget borders, the size of the selection toggle will get
192 // increased to prevent an accidental clicking of the item
193 // when trying to hit the toggle.
194 const int widgetHeight
= size().height();
195 const int widgetWidth
= size().width();
196 const int minMargin
= 2;
198 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
199 toggleSize
= widgetHeight
;
202 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
203 toggleSize
= widgetWidth
;
207 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
210 QString
KFileItemListWidget::roleText(const QByteArray
& role
, const QHash
<QByteArray
, QVariant
>& values
)
213 const QVariant roleValue
= values
.value(role
);
215 switch (roleTextId(role
)) {
223 text
= roleValue
.toString();
227 if (values
.value("isDir").toBool()) {
228 // The item represents a directory. Show the number of sub directories
229 // instead of the file size of the directory.
230 if (roleValue
.isNull()) {
231 text
= i18nc("@item:intable", "Unknown");
233 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
234 text
= i18ncp("@item:intable", "%1 item", "%1 items", size
);
237 // Show the size in kilobytes (always round up)
238 const KLocale
* locale
= KGlobal::locale();
239 const int roundInc
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
) ? 499 : 511;
240 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>() + roundInc
;
241 text
= locale
->formatByteSize(size
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
247 const QDateTime dateTime
= roleValue
.toDateTime();
248 text
= KGlobal::locale()->formatDateTime(dateTime
);
260 void KFileItemListWidget::invalidateCache()
262 m_dirtyLayout
= true;
263 m_dirtyContent
= true;
266 void KFileItemListWidget::refreshCache()
270 void KFileItemListWidget::setTextColor(const QColor
& color
)
272 if (color
!= m_customTextColor
) {
273 m_customTextColor
= color
;
274 updateAdditionalInfoTextColor();
279 QColor
KFileItemListWidget::textColor() const
281 if (m_customTextColor
.isValid() && !isSelected()) {
282 return m_customTextColor
;
285 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
286 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Text
;
287 return styleOption().palette
.brush(group
, role
).color();
290 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
293 m_dirtyContent
= true;
297 QPixmap
KFileItemListWidget::overlay() const
302 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
303 const QSet
<QByteArray
>& roles
)
305 KItemListWidget::dataChanged(current
, roles
);
306 m_dirtyContent
= true;
308 QSet
<QByteArray
> dirtyRoles
;
309 if (roles
.isEmpty()) {
310 dirtyRoles
= visibleRoles().toSet();
311 dirtyRoles
.insert("iconPixmap");
312 dirtyRoles
.insert("iconName");
317 QSetIterator
<QByteArray
> it(dirtyRoles
);
318 while (it
.hasNext()) {
319 const QByteArray
& role
= it
.next();
320 m_dirtyContentRoles
.insert(role
);
324 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
325 const QList
<QByteArray
>& previous
)
327 KItemListWidget::visibleRolesChanged(current
, previous
);
328 m_sortedVisibleRoles
= current
;
329 m_dirtyLayout
= true;
332 void KFileItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
333 const QHash
<QByteArray
, QSizeF
>& previous
)
335 KItemListWidget::visibleRolesSizesChanged(current
, previous
);
336 m_dirtyLayout
= true;
339 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
340 const KItemListStyleOption
& previous
)
342 KItemListWidget::styleOptionChanged(current
, previous
);
343 updateAdditionalInfoTextColor();
344 m_dirtyLayout
= true;
347 void KFileItemListWidget::hoveredChanged(bool hovered
)
350 m_dirtyLayout
= true;
353 void KFileItemListWidget::selectedChanged(bool selected
)
356 updateAdditionalInfoTextColor();
359 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
361 KItemListWidget::resizeEvent(event
);
362 m_dirtyLayout
= true;
365 void KFileItemListWidget::showEvent(QShowEvent
* event
)
367 KItemListWidget::showEvent(event
);
369 // Listen to changes of the clipboard to mark the item as cut/uncut
370 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
372 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
373 m_isCut
= clipboard
->isCut(itemUrl
);
375 connect(clipboard
, SIGNAL(cutItemsChanged()),
376 this, SLOT(slotCutItemsChanged()));
379 void KFileItemListWidget::hideEvent(QHideEvent
* event
)
381 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
382 this, SLOT(slotCutItemsChanged()));
384 KItemListWidget::hideEvent(event
);
387 void KFileItemListWidget::slotCutItemsChanged()
389 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
390 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
391 if (m_isCut
!= isCut
) {
393 m_pixmap
= QPixmap();
394 m_dirtyContent
= true;
399 void KFileItemListWidget::triggerCacheRefreshing()
401 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
407 const QHash
<QByteArray
, QVariant
> values
= data();
408 m_isExpandable
= values
["isExpandable"].toBool();
409 m_isHidden
= values
["name"].toString().startsWith(QLatin1Char('.'));
411 updateExpansionArea();
415 m_dirtyLayout
= false;
416 m_dirtyContent
= false;
417 m_dirtyContentRoles
.clear();
420 void KFileItemListWidget::updateExpansionArea()
422 if (m_layout
== DetailsLayout
) {
423 const QHash
<QByteArray
, QVariant
> values
= data();
424 Q_ASSERT(values
.contains("expansionLevel"));
425 const KItemListStyleOption
& option
= styleOption();
426 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
427 if (expansionLevel
>= 0) {
428 const qreal widgetHeight
= size().height();
429 const qreal expansionLevelSize
= KIconLoader::SizeSmall
;
430 const qreal x
= option
.margin
+ expansionLevel
* widgetHeight
;
431 const qreal y
= (widgetHeight
- expansionLevelSize
) / 2;
432 m_expansionArea
= QRectF(x
, y
, expansionLevelSize
, expansionLevelSize
);
437 m_expansionArea
= QRectF();
440 void KFileItemListWidget::updatePixmapCache()
442 // Precondition: Requires already updated m_textPos values to calculate
443 // the remaining height when the alignment is vertical.
445 const bool iconOnTop
= (m_layout
== IconsLayout
);
446 const KItemListStyleOption
& option
= styleOption();
447 const int iconHeight
= option
.iconSize
;
449 const QHash
<QByteArray
, QVariant
> values
= data();
450 const QSizeF widgetSize
= size();
452 int scaledIconHeight
= 0;
454 scaledIconHeight
= static_cast<int>(m_textPos
[Name
].y() - 3 * option
.margin
);
456 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
457 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
458 scaledIconHeight
= (requiredTextHeight
< iconHeight
) ? widgetSize
.height() - 2 * option
.margin
: iconHeight
;
461 bool updatePixmap
= (iconHeight
!= m_pixmap
.height());
462 if (!updatePixmap
&& m_dirtyContent
) {
463 updatePixmap
= m_dirtyContentRoles
.isEmpty()
464 || m_dirtyContentRoles
.contains("iconPixmap")
465 || m_dirtyContentRoles
.contains("iconName")
466 || m_dirtyContentRoles
.contains("iconOverlays");
470 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
471 if (m_pixmap
.isNull()) {
472 // Use the icon that fits to the MIME-type
473 QString iconName
= values
["iconName"].toString();
474 if (iconName
.isEmpty()) {
475 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
476 // use a generic icon as fallback
477 iconName
= QLatin1String("unknown");
479 m_pixmap
= pixmapForIcon(iconName
, iconHeight
);
480 m_originalPixmapSize
= m_pixmap
.size();
481 } else if (m_pixmap
.size() != QSize(iconHeight
, iconHeight
)) {
482 // A custom pixmap has been applied. Assure that the pixmap
483 // is scaled to the available size.
484 const bool scale
= m_pixmap
.width() > iconHeight
|| m_pixmap
.height() > iconHeight
||
485 (m_pixmap
.width() < iconHeight
&& m_pixmap
.height() < iconHeight
);
487 KPixmapModifier::scale(m_pixmap
, QSize(iconHeight
, iconHeight
));
489 m_originalPixmapSize
= m_pixmap
.size();
491 // To simplify the handling of scaling the original pixmap
492 // will be embedded into a square pixmap.
493 QPixmap
squarePixmap(iconHeight
, iconHeight
);
494 squarePixmap
.fill(Qt::transparent
);
496 QPainter
painter(&squarePixmap
);
499 x
= (iconHeight
- m_pixmap
.width()) / 2; // Center horizontally
500 y
= iconHeight
- m_pixmap
.height(); // Align on bottom
501 painter
.drawPixmap(x
, y
, m_pixmap
);
503 x
= iconHeight
- m_pixmap
.width(); // Align right
504 y
= (iconHeight
- m_pixmap
.height()) / 2; // Center vertically
505 painter
.drawPixmap(x
, y
, m_pixmap
);
508 m_pixmap
= squarePixmap
;
510 m_originalPixmapSize
= m_pixmap
.size();
513 const QStringList overlays
= values
["iconOverlays"].toStringList();
515 // Strangely KFileItem::overlays() returns empty string-values, so
516 // we need to check first whether an overlay must be drawn at all.
517 // It is more efficient to do it here, as KIconLoader::drawOverlays()
518 // assumes that an overlay will be drawn and has some additional
520 foreach (const QString
& overlay
, overlays
) {
521 if (!overlay
.isEmpty()) {
522 // There is at least one overlay, draw all overlays above m_pixmap
523 // and cancel the check
524 KIconLoader::global()->drawOverlays(overlays
, m_pixmap
, KIconLoader::Desktop
);
530 applyCutEffect(m_pixmap
);
534 applyHiddenEffect(m_pixmap
);
537 Q_ASSERT(m_pixmap
.height() == iconHeight
);
539 if (!m_overlay
.isNull()) {
540 QPainter
painter(&m_pixmap
);
541 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
544 m_scaledPixmapSize
= QSize(scaledIconHeight
, scaledIconHeight
);
547 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
549 m_pixmapPos
.setX(m_textPos
[Name
].x() - 2 * option
.margin
- scaledIconHeight
);
551 m_pixmapPos
.setY(option
.margin
);
553 // Center the hover rectangle horizontally and align it on bottom
554 qreal hoverWidth
= m_originalPixmapSize
.width();
555 qreal hoverHeight
= m_originalPixmapSize
.height();
556 if (scaledIconHeight
!= m_pixmap
.height()) {
557 const qreal scaleFactor
= qreal(scaledIconHeight
) / qreal(m_pixmap
.height());
558 hoverWidth
*= scaleFactor
;
559 hoverHeight
*= scaleFactor
;
561 const qreal hoverX
= m_pixmapPos
.x() + (m_scaledPixmapSize
.width() - hoverWidth
) / 2.0;
562 const qreal hoverY
= m_pixmapPos
.y() + m_scaledPixmapSize
.height() - hoverHeight
;
563 m_iconRect
= QRectF(hoverX
, hoverY
, hoverWidth
, hoverHeight
);
564 const qreal margin
= option
.margin
;
565 m_iconRect
.adjust(-margin
, -margin
, margin
, margin
);
567 // Prepare the pixmap that is used when the item gets hovered
569 m_hoverPixmap
= m_pixmap
;
570 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
571 // In the KIconLoader terminology, active = hover.
572 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
573 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
575 m_hoverPixmap
= m_pixmap
;
577 } else if (hoverOpacity() <= 0.0) {
578 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
579 m_hoverPixmap
= QPixmap();
583 void KFileItemListWidget::updateTextsCache()
585 QTextOption textOption
;
588 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
589 textOption
.setAlignment(Qt::AlignHCenter
);
593 textOption
.setAlignment(Qt::AlignLeft
);
594 textOption
.setWrapMode(QTextOption::NoWrap
);
601 for (int i
= 0; i
< TextIdCount
; ++i
) {
602 m_text
[i
].setText(QString());
603 m_text
[i
].setTextOption(textOption
);
607 case IconsLayout
: updateIconsLayoutTextCache(); break;
608 case CompactLayout
: updateCompactLayoutTextCache(); break;
609 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
610 default: Q_ASSERT(false); break;
614 void KFileItemListWidget::updateIconsLayoutTextCache()
621 // might get wrapped above
626 const QHash
<QByteArray
, QVariant
> values
= data();
628 const KItemListStyleOption
& option
= styleOption();
629 const qreal maxWidth
= size().width() - 2 * option
.margin
;
630 const qreal widgetHeight
= size().height();
631 const qreal fontHeight
= option
.fontMetrics
.height();
633 // Initialize properties for the "name" role. It will be used as anchor
634 // for initializing the position of the other roles.
635 m_text
[Name
].setText(KStringHandler::preProcessWrap(values
["name"].toString()));
637 // Calculate the number of lines required for the name and the required width
638 int textLinesCountForName
= 0;
639 qreal requiredWidthForName
= 0;
642 QTextLayout
layout(m_text
[Name
].text(), option
.font
);
643 layout
.setTextOption(m_text
[Name
].textOption());
644 layout
.beginLayout();
645 while ((line
= layout
.createLine()).isValid()) {
646 line
.setLineWidth(maxWidth
);
647 requiredWidthForName
= qMax(requiredWidthForName
, line
.naturalTextWidth());
648 ++textLinesCountForName
;
652 // Use one line for each additional information
653 int textLinesCount
= textLinesCountForName
;
654 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
655 textLinesCount
+= additionalRolesCount
;
657 m_text
[Name
].setTextWidth(maxWidth
);
658 m_textPos
[Name
] = QPointF(option
.margin
, widgetHeight
- textLinesCount
* fontHeight
- option
.margin
);
659 m_textRect
= QRectF(option
.margin
+ (maxWidth
- requiredWidthForName
) / 2,
661 requiredWidthForName
,
662 textLinesCountForName
* fontHeight
);
664 // Calculate the position for each additional information
665 qreal y
= m_textPos
[Name
].y() + textLinesCountForName
* fontHeight
;
666 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
667 const TextId textId
= roleTextId(role
);
668 if (textId
== Name
) {
672 const QString text
= roleText(role
, values
);
673 m_text
[textId
].setText(text
);
675 qreal requiredWidth
= 0;
677 QTextLayout
layout(text
, option
.font
);
678 layout
.setTextOption(m_text
[textId
].textOption());
679 layout
.beginLayout();
680 QTextLine textLine
= layout
.createLine();
681 if (textLine
.isValid()) {
682 textLine
.setLineWidth(maxWidth
);
683 requiredWidth
= textLine
.naturalTextWidth();
684 if (textLine
.textLength() < text
.length()) {
685 // TODO: QFontMetrics::elidedText() works different regarding the given width
686 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
687 // not get elided although it does not fit into the given width. As workaround
688 // the margin is substracted.
689 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
- option
.margin
);
690 m_text
[textId
].setText(elidedText
);
695 m_textPos
[textId
] = QPointF(option
.margin
, y
);
696 m_text
[textId
].setTextWidth(maxWidth
);
698 const QRectF
textRect(option
.margin
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, fontHeight
);
699 m_textRect
|= textRect
;
704 // Add a margin to the text rectangle
705 const qreal margin
= option
.margin
;
706 m_textRect
.adjust(-margin
, -margin
, margin
, margin
);
709 void KFileItemListWidget::updateCompactLayoutTextCache()
711 // +------+ Name role
712 // | Icon | Additional role 1
713 // +------+ Additional role 2
715 const QHash
<QByteArray
, QVariant
> values
= data();
717 const KItemListStyleOption
& option
= styleOption();
718 const qreal widgetHeight
= size().height();
719 const qreal fontHeight
= option
.fontMetrics
.height();
720 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * fontHeight
;
721 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.margin
: option
.iconSize
;
723 qreal maximumRequiredTextWidth
= 0;
724 const qreal x
= option
.margin
* 3 + scaledIconSize
;
725 qreal y
= (widgetHeight
- textLinesHeight
) / 2;
726 const qreal maxWidth
= size().width() - x
- option
.margin
;
727 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
728 const TextId textId
= roleTextId(role
);
730 const QString text
= roleText(role
, values
);
731 m_text
[textId
].setText(text
);
733 qreal requiredWidth
= option
.fontMetrics
.width(text
);
734 if (requiredWidth
> maxWidth
) {
735 requiredWidth
= maxWidth
;
736 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
737 m_text
[textId
].setText(elidedText
);
740 m_textPos
[textId
] = QPointF(x
, y
);
741 m_text
[textId
].setTextWidth(maxWidth
);
743 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
748 m_textRect
= QRectF(x
- option
.margin
, 0, maximumRequiredTextWidth
+ 2 * option
.margin
, widgetHeight
);
751 void KFileItemListWidget::updateDetailsLayoutTextCache()
753 // Precondition: Requires already updated m_expansionArea
754 // to determine the left position.
757 // | Icon | Name role Additional role 1 Additional role 2
759 m_textRect
= QRectF();
761 const KItemListStyleOption
& option
= styleOption();
762 const QHash
<QByteArray
, QVariant
> values
= data();
764 const qreal widgetHeight
= size().height();
765 const int scaledIconSize
= widgetHeight
- 2 * option
.margin
;
766 const int fontHeight
= option
.fontMetrics
.height();
768 const qreal columnMargin
= option
.margin
* 3;
769 const qreal firstColumnInc
= m_expansionArea
.right() + option
.margin
* 2 + scaledIconSize
;
770 qreal x
= firstColumnInc
;
771 const qreal y
= qMax(qreal(option
.margin
), (widgetHeight
- fontHeight
) / 2);
773 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
774 const TextId textId
= roleTextId(role
);
776 QString text
= roleText(role
, values
);
778 // Elide the text in case it does not fit into the available column-width
779 qreal requiredWidth
= option
.fontMetrics
.width(text
);
780 const qreal columnWidth
= visibleRolesSizes().value(role
, QSizeF(0, 0)).width();
781 qreal availableTextWidth
= columnWidth
- 2 * columnMargin
;
782 if (textId
== Name
) {
783 availableTextWidth
-= firstColumnInc
;
786 if (requiredWidth
> availableTextWidth
) {
787 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
788 requiredWidth
= option
.fontMetrics
.width(text
);
791 m_text
[textId
].setText(text
);
792 m_textPos
[textId
] = QPointF(x
+ columnMargin
, y
);
797 m_textRect
= QRectF(m_textPos
[textId
].x() - option
.margin
, 0,
798 requiredWidth
+ 2 * option
.margin
, size().height());
800 // The column after the name should always be aligned on the same x-position independent
801 // from the expansion-level shown in the name column
806 // The values for the size should be right aligned
807 m_textPos
[textId
].rx() += columnWidth
- requiredWidth
- 2 * columnMargin
;
816 void KFileItemListWidget::updateAdditionalInfoTextColor()
819 if (m_customTextColor
.isValid()) {
820 c1
= m_customTextColor
;
821 } else if (isSelected() && m_layout
!= DetailsLayout
) {
822 c1
= styleOption().palette
.highlightedText().color();
824 c1
= styleOption().palette
.text().color();
827 // For the color of the additional info the inactive text color
828 // is not used as this might lead to unreadable text for some color schemes. Instead
829 // the text color c1 is slightly mixed with the background color.
830 const QColor c2
= styleOption().palette
.base().color();
832 const int p2
= 100 - p1
;
833 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
834 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
835 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
838 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
840 if (m_scaledPixmapSize
!= pixmap
.size()) {
841 QPixmap scaledPixmap
= pixmap
;
842 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
843 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
845 #ifdef KFILEITEMLISTWIDGET_DEBUG
846 painter
->setPen(Qt::green
);
847 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(scaledPixmap
.size())));
850 painter
->drawPixmap(m_pixmapPos
, pixmap
);
854 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
856 const KIcon
icon(name
);
859 if (size
<= KIconLoader::SizeSmall
) {
860 requestedSize
= KIconLoader::SizeSmall
;
861 } else if (size
<= KIconLoader::SizeSmallMedium
) {
862 requestedSize
= KIconLoader::SizeSmallMedium
;
863 } else if (size
<= KIconLoader::SizeMedium
) {
864 requestedSize
= KIconLoader::SizeMedium
;
865 } else if (size
<= KIconLoader::SizeLarge
) {
866 requestedSize
= KIconLoader::SizeLarge
;
867 } else if (size
<= KIconLoader::SizeHuge
) {
868 requestedSize
= KIconLoader::SizeHuge
;
869 } else if (size
<= KIconLoader::SizeEnormous
) {
870 requestedSize
= KIconLoader::SizeEnormous
;
871 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
872 requestedSize
= KIconLoader::SizeEnormous
* 2;
874 requestedSize
= size
;
877 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
878 if (requestedSize
!= size
) {
879 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
885 KFileItemListWidget::TextId
KFileItemListWidget::roleTextId(const QByteArray
& role
)
887 static QHash
<QByteArray
, TextId
> rolesHash
;
888 if (rolesHash
.isEmpty()) {
889 rolesHash
.insert("name", Name
);
890 rolesHash
.insert("size", Size
);
891 rolesHash
.insert("date", Date
);
892 rolesHash
.insert("permissions", Permissions
);
893 rolesHash
.insert("owner", Owner
);
894 rolesHash
.insert("group", Group
);
895 rolesHash
.insert("type", Type
);
896 rolesHash
.insert("destination", Destination
);
897 rolesHash
.insert("path", Path
);
900 return rolesHash
.value(role
);
903 void KFileItemListWidget::applyCutEffect(QPixmap
& pixmap
)
905 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
906 pixmap
= effect
->apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
909 void KFileItemListWidget::applyHiddenEffect(QPixmap
& pixmap
)
911 KIconEffect::semiTransparent(pixmap
);
914 #include "kfileitemlistwidget.moc"