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 "kfileitemmodel.h"
23 #include "kitemlistview.h"
24 #include "kpixmapmodifier_p.h"
27 #include <KIconEffect>
28 #include <KIconLoader>
30 #include <KStringHandler>
33 #include <QFontMetricsF>
34 #include <QGraphicsSceneResizeEvent>
36 #include <QStyleOption>
37 #include <QTextLayout>
40 //#define KFILEITEMLISTWIDGET_DEBUG
42 KFileItemListWidget::KFileItemListWidget(QGraphicsItem
* parent
) :
43 KItemListWidget(parent
),
47 m_dirtyContentRoles(),
48 m_layout(IconsLayout
),
57 m_sortedVisibleRoles(),
60 m_additionalInfoTextColor(),
62 m_selectionTogglePos()
64 for (int i
= 0; i
< TextIdCount
; ++i
) {
65 m_text
[i
].setTextFormat(Qt::PlainText
);
66 m_text
[i
].setPerformanceHint(QStaticText::AggressiveCaching
);
70 KFileItemListWidget::~KFileItemListWidget()
74 void KFileItemListWidget::setLayout(Layout layout
)
76 if (m_layout
!= layout
) {
83 KFileItemListWidget::Layout
KFileItemListWidget::layout() const
88 void KFileItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
90 KItemListWidget::paint(painter
, option
, widget
);
92 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
94 // Draw expansion toggle '>' or 'V'
95 if (m_isDir
&& !m_expansionArea
.isEmpty()) {
96 QStyleOption arrowOption
;
97 arrowOption
.rect
= m_expansionArea
.toRect();
98 const QStyle::PrimitiveElement arrow
= data()["isExpanded"].toBool()
99 ? QStyle::PE_IndicatorArrowDown
: QStyle::PE_IndicatorArrowRight
;
100 style()->drawPrimitive(arrow
, &arrowOption
, painter
);
103 const KItemListStyleOption
& itemListStyleOption
= styleOption();
105 // Blend the unhovered and hovered pixmap if the hovering
106 // animation is ongoing
107 if (hoverOpacity() < 1.0) {
108 drawPixmap(painter
, m_pixmap
);
111 const qreal opacity
= painter
->opacity();
112 painter
->setOpacity(hoverOpacity() * opacity
);
113 drawPixmap(painter
, m_hoverPixmap
);
114 painter
->setOpacity(opacity
);
116 drawPixmap(painter
, m_pixmap
);
119 painter
->setFont(itemListStyleOption
.font
);
120 painter
->setPen(textColor());
121 painter
->drawStaticText(m_textPos
[Name
], m_text
[Name
]);
123 bool clipAdditionalInfoBounds
= false;
124 if (m_layout
== DetailsLayout
) {
125 // Prevent a possible overlapping of the additional-information texts
126 // with the icon. This can happen if the user has minimized the width
127 // of the name-column to a very small value.
128 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.margin
;
129 if (m_textPos
[Name
+ 1].x() < minX
) {
130 clipAdditionalInfoBounds
= true;
132 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
136 painter
->setPen(m_additionalInfoTextColor
);
137 painter
->setFont(itemListStyleOption
.font
);
138 for (int i
= Name
+ 1; i
< TextIdCount
; ++i
) {
139 painter
->drawStaticText(m_textPos
[i
], m_text
[i
]);
142 if (clipAdditionalInfoBounds
) {
146 #ifdef KFILEITEMLISTWIDGET_DEBUG
147 painter
->setPen(Qt::red
);
148 painter
->setBrush(Qt::NoBrush
);
149 painter
->drawText(QPointF(0, itemListStyleOption
.fontMetrics
.height()), QString::number(index()));
150 painter
->drawRect(rect());
154 QRectF
KFileItemListWidget::iconRect() const
156 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
158 QRectF bounds
= m_hoverPixmapRect
;
159 const qreal margin
= styleOption().margin
;
160 bounds
.adjust(-margin
, -margin
, margin
, margin
);
164 QRectF
KFileItemListWidget::textRect() const
166 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
170 QRectF
KFileItemListWidget::expansionToggleRect() const
172 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
173 return m_isDir
? m_expansionArea
: QRectF();
176 QRectF
KFileItemListWidget::selectionToggleRect() const
178 const_cast<KFileItemListWidget
*>(this)->triggerCacheRefreshing();
180 const int iconHeight
= m_pixmap
.height();
182 int toggleSize
= KIconLoader::SizeSmall
;
183 if (iconHeight
>= KIconLoader::SizeEnormous
) {
184 toggleSize
= KIconLoader::SizeMedium
;
185 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
186 toggleSize
= KIconLoader::SizeSmallMedium
;
189 QPointF pos
= m_selectionTogglePos
;
191 // If the selection toggle has a very small distance to the
192 // widget borders, the size of the selection toggle will get
193 // increased to prevent an accidental clicking of the item
194 // when trying to hit the toggle.
195 const int widgetHeight
= size().height();
196 const int widgetWidth
= size().width();
197 const int minMargin
= 2;
199 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
200 toggleSize
= widgetHeight
;
203 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
204 toggleSize
= widgetWidth
;
208 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
211 QString
KFileItemListWidget::roleText(const QByteArray
& role
, const QHash
<QByteArray
, QVariant
>& values
)
214 const QVariant roleValue
= values
.value(role
);
216 switch (roleTextId(role
)) {
224 text
= roleValue
.toString();
228 if (values
.value("isDir").toBool()) {
229 // The item represents a directory. Show the number of sub directories
230 // instead of the file size of the directory.
231 if (roleValue
.isNull()) {
232 text
= i18nc("@item:intable", "Unknown");
234 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
235 text
= i18ncp("@item:intable", "%1 item", "%1 items", size
);
238 // Show the size in kilobytes (always round up)
239 const KLocale
* locale
= KGlobal::locale();
240 const int roundInc
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
) ? 499 : 511;
241 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>() + roundInc
;
242 text
= locale
->formatByteSize(size
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
248 const QDateTime dateTime
= roleValue
.toDateTime();
249 text
= KGlobal::locale()->formatDateTime(dateTime
);
261 void KFileItemListWidget::invalidateCache()
263 m_dirtyLayout
= true;
264 m_dirtyContent
= true;
267 void KFileItemListWidget::refreshCache()
271 void KFileItemListWidget::setTextColor(const QColor
& color
)
273 if (color
!= m_customTextColor
) {
274 m_customTextColor
= color
;
275 updateAdditionalInfoTextColor();
280 QColor
KFileItemListWidget::textColor() const
282 if (m_customTextColor
.isValid()) {
283 return m_customTextColor
;
284 } else if (isSelected()) {
285 return styleOption().palette
.highlightedText().color();
287 return styleOption().palette
.text().color();
291 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
294 m_dirtyContent
= true;
298 QPixmap
KFileItemListWidget::overlay() const
303 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
304 const QSet
<QByteArray
>& roles
)
306 KItemListWidget::dataChanged(current
, roles
);
307 m_dirtyContent
= true;
309 QSet
<QByteArray
> dirtyRoles
;
310 if (roles
.isEmpty()) {
311 dirtyRoles
= visibleRoles().toSet();
312 dirtyRoles
.insert("iconPixmap");
313 dirtyRoles
.insert("iconName");
318 QSetIterator
<QByteArray
> it(dirtyRoles
);
319 while (it
.hasNext()) {
320 const QByteArray
& role
= it
.next();
321 m_dirtyContentRoles
.insert(role
);
325 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
326 const QList
<QByteArray
>& previous
)
328 KItemListWidget::visibleRolesChanged(current
, previous
);
329 m_sortedVisibleRoles
= current
;
330 m_dirtyLayout
= true;
333 void KFileItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
334 const QHash
<QByteArray
, QSizeF
>& previous
)
336 KItemListWidget::visibleRolesSizesChanged(current
, previous
);
337 m_dirtyLayout
= true;
340 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
341 const KItemListStyleOption
& previous
)
343 KItemListWidget::styleOptionChanged(current
, previous
);
344 updateAdditionalInfoTextColor();
345 m_dirtyLayout
= true;
348 void KFileItemListWidget::hoveredChanged(bool hovered
)
351 m_dirtyLayout
= true;
354 void KFileItemListWidget::selectedChanged(bool selected
)
357 updateAdditionalInfoTextColor();
360 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
362 KItemListWidget::resizeEvent(event
);
363 m_dirtyLayout
= true;
366 void KFileItemListWidget::triggerCacheRefreshing()
368 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
374 m_isDir
= data()["isDir"].toBool();
376 updateExpansionArea();
380 m_dirtyLayout
= false;
381 m_dirtyContent
= false;
382 m_dirtyContentRoles
.clear();
385 void KFileItemListWidget::updateExpansionArea()
387 if (m_layout
== DetailsLayout
) {
388 const QHash
<QByteArray
, QVariant
> values
= data();
389 Q_ASSERT(values
.contains("expansionLevel"));
390 const KItemListStyleOption
& option
= styleOption();
391 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
393 const qreal widgetHeight
= size().height();
394 const qreal expansionLevelSize
= KIconLoader::SizeSmall
;
395 const qreal x
= option
.margin
+ expansionLevel
* widgetHeight
;
396 const qreal y
= (widgetHeight
- expansionLevelSize
) / 2;
397 m_expansionArea
= QRectF(x
, y
, expansionLevelSize
, expansionLevelSize
);
399 m_expansionArea
= QRectF();
403 void KFileItemListWidget::updatePixmapCache()
405 // Precondition: Requires already updated m_textPos values to calculate
406 // the remaining height when the alignment is vertical.
408 const bool iconOnTop
= (m_layout
== IconsLayout
);
409 const KItemListStyleOption
& option
= styleOption();
410 const int iconHeight
= option
.iconSize
;
412 const QHash
<QByteArray
, QVariant
> values
= data();
413 const QSizeF widgetSize
= size();
415 int scaledIconHeight
= 0;
417 scaledIconHeight
= static_cast<int>(m_textPos
[Name
].y() - 3 * option
.margin
);
419 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
420 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
421 scaledIconHeight
= (requiredTextHeight
< iconHeight
) ? widgetSize
.height() - 2 * option
.margin
: iconHeight
;
424 bool updatePixmap
= (iconHeight
!= m_pixmap
.height());
425 if (!updatePixmap
&& m_dirtyContent
) {
426 updatePixmap
= m_dirtyContentRoles
.isEmpty()
427 || m_dirtyContentRoles
.contains("iconPixmap")
428 || m_dirtyContentRoles
.contains("iconName");
432 // The selection toggle should always be applied to the top/left
434 m_selectionTogglePos
= QPointF(-option
.margin
, -option
.margin
);
436 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
437 if (m_pixmap
.isNull()) {
438 // Use the icon that fits to the MIME-type
439 QString iconName
= values
["iconName"].toString();
440 if (iconName
.isEmpty()) {
441 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
442 // use a generic icon as fallback
443 iconName
= QLatin1String("unknown");
445 m_pixmap
= pixmapForIcon(iconName
, iconHeight
);
446 m_hoverPixmapRect
.setSize(m_pixmap
.size());
447 } else if (m_pixmap
.size() != QSize(iconHeight
, iconHeight
)) {
448 // A custom pixmap has been applied. Assure that the pixmap
449 // is scaled to the available size.
450 const bool scale
= m_pixmap
.width() > iconHeight
|| m_pixmap
.height() > iconHeight
||
451 (m_pixmap
.width() < iconHeight
&& m_pixmap
.height() < iconHeight
);
453 KPixmapModifier::scale(m_pixmap
, QSize(iconHeight
, iconHeight
));
455 m_hoverPixmapRect
.setSize(m_pixmap
.size());
457 // To simplify the handling of scaling the original pixmap
458 // will be embedded into a square pixmap.
459 QPixmap
squarePixmap(iconHeight
, iconHeight
);
460 squarePixmap
.fill(Qt::transparent
);
462 QPainter
painter(&squarePixmap
);
465 x
= (iconHeight
- m_pixmap
.width()) / 2; // Center horizontally
466 y
= iconHeight
- m_pixmap
.height(); // Align on bottom
467 painter
.drawPixmap(x
, y
, m_pixmap
);
469 x
= iconHeight
- m_pixmap
.width(); // Align right
470 y
= (iconHeight
- m_pixmap
.height()) / 2; // Center vertically
471 painter
.drawPixmap(x
, y
, m_pixmap
);
473 m_selectionTogglePos
+= QPointF(x
, y
);
475 m_pixmap
= squarePixmap
;
477 m_hoverPixmapRect
.setSize(m_pixmap
.size());
480 Q_ASSERT(m_pixmap
.height() == iconHeight
);
482 if (!m_overlay
.isNull()) {
483 QPainter
painter(&m_pixmap
);
484 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
487 m_scaledPixmapSize
= QSize(scaledIconHeight
, scaledIconHeight
);
490 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
492 m_pixmapPos
.setX(m_textPos
[Name
].x() - 2 * option
.margin
- scaledIconHeight
);
494 m_pixmapPos
.setY(option
.margin
);
497 m_selectionTogglePos
+= m_pixmapPos
;
500 // Center the hover rectangle horizontally and align it on bottom
501 const qreal x
= m_pixmapPos
.x() + (m_scaledPixmapSize
.width() - m_hoverPixmapRect
.width()) / 2.0;
502 const qreal y
= m_pixmapPos
.y() + m_scaledPixmapSize
.height() - m_hoverPixmapRect
.height();
503 m_hoverPixmapRect
.moveTopLeft(QPointF(x
, y
));
505 // Prepare the pixmap that is used when the item gets hovered
507 m_hoverPixmap
= m_pixmap
;
508 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
509 // In the KIconLoader terminology, active = hover.
510 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
511 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
513 m_hoverPixmap
= m_pixmap
;
515 } else if (hoverOpacity() <= 0.0) {
516 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
517 m_hoverPixmap
= QPixmap();
521 void KFileItemListWidget::updateTextsCache()
523 QTextOption textOption
;
526 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
527 textOption
.setAlignment(Qt::AlignHCenter
);
531 textOption
.setAlignment(Qt::AlignLeft
);
532 textOption
.setWrapMode(QTextOption::NoWrap
);
539 for (int i
= 0; i
< TextIdCount
; ++i
) {
540 m_text
[i
].setText(QString());
541 m_text
[i
].setTextOption(textOption
);
545 case IconsLayout
: updateIconsLayoutTextCache(); break;
546 case CompactLayout
: updateCompactLayoutTextCache(); break;
547 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
548 default: Q_ASSERT(false); break;
552 void KFileItemListWidget::updateIconsLayoutTextCache()
559 // might get wrapped above
564 const QHash
<QByteArray
, QVariant
> values
= data();
566 const KItemListStyleOption
& option
= styleOption();
567 const qreal maxWidth
= size().width() - 2 * option
.margin
;
568 const qreal widgetHeight
= size().height();
569 const qreal fontHeight
= option
.fontMetrics
.height();
571 // Initialize properties for the "name" role. It will be used as anchor
572 // for initializing the position of the other roles.
573 m_text
[Name
].setText(KStringHandler::preProcessWrap(values
["name"].toString()));
575 // Calculate the number of lines required for the name and the required width
576 int textLinesCountForName
= 0;
577 qreal requiredWidthForName
= 0;
580 QTextLayout
layout(m_text
[Name
].text(), option
.font
);
581 layout
.setTextOption(m_text
[Name
].textOption());
582 layout
.beginLayout();
583 while ((line
= layout
.createLine()).isValid()) {
584 line
.setLineWidth(maxWidth
);
585 requiredWidthForName
= qMax(requiredWidthForName
, line
.naturalTextWidth());
586 ++textLinesCountForName
;
590 // Use one line for each additional information
591 int textLinesCount
= textLinesCountForName
;
592 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
593 textLinesCount
+= additionalRolesCount
;
595 m_text
[Name
].setTextWidth(maxWidth
);
596 m_textPos
[Name
] = QPointF(option
.margin
, widgetHeight
- textLinesCount
* fontHeight
- option
.margin
);
597 m_textRect
= QRectF(option
.margin
+ (maxWidth
- requiredWidthForName
) / 2,
599 requiredWidthForName
,
600 m_text
[Name
].size().height());
602 // Calculate the position for each additional information
603 qreal y
= m_textPos
[Name
].y() + textLinesCountForName
* fontHeight
;
604 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
605 const TextId textId
= roleTextId(role
);
606 if (textId
== Name
) {
610 const QString text
= roleText(role
, values
);
611 m_text
[textId
].setText(text
);
613 qreal requiredWidth
= 0;
615 QTextLayout
layout(text
, option
.font
);
616 layout
.setTextOption(m_text
[textId
].textOption());
617 layout
.beginLayout();
618 QTextLine textLine
= layout
.createLine();
619 if (textLine
.isValid()) {
620 textLine
.setLineWidth(maxWidth
);
621 requiredWidth
= textLine
.naturalTextWidth();
622 if (textLine
.textLength() < text
.length()) {
623 // TODO: QFontMetrics::elidedText() works different regarding the given width
624 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
625 // not get elided although it does not fit into the given width. As workaround
626 // the margin is substracted.
627 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
- option
.margin
);
628 m_text
[textId
].setText(elidedText
);
633 m_textPos
[textId
] = QPointF(option
.margin
, y
);
634 m_text
[textId
].setTextWidth(maxWidth
);
636 const QRectF
textRect(option
.margin
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, fontHeight
);
637 m_textRect
|= textRect
;
642 // Add a margin to the text rectangle
643 const qreal margin
= option
.margin
;
644 m_textRect
.adjust(-margin
, -margin
, margin
, margin
);
647 void KFileItemListWidget::updateCompactLayoutTextCache()
649 // +------+ Name role
650 // | Icon | Additional role 1
651 // +------+ Additional role 2
653 const QHash
<QByteArray
, QVariant
> values
= data();
655 const KItemListStyleOption
& option
= styleOption();
656 const qreal widgetHeight
= size().height();
657 const qreal fontHeight
= option
.fontMetrics
.height();
658 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * fontHeight
;
659 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.margin
: option
.iconSize
;
661 qreal maximumRequiredTextWidth
= 0;
662 const qreal x
= option
.margin
* 3 + scaledIconSize
;
663 qreal y
= (widgetHeight
- textLinesHeight
) / 2;
664 const qreal maxWidth
= size().width() - x
- option
.margin
;
665 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
666 const TextId textId
= roleTextId(role
);
668 const QString text
= roleText(role
, values
);
669 m_text
[textId
].setText(text
);
671 qreal requiredWidth
= option
.fontMetrics
.width(text
);
672 if (requiredWidth
> maxWidth
) {
673 requiredWidth
= maxWidth
;
674 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
675 m_text
[textId
].setText(elidedText
);
678 m_textPos
[textId
] = QPointF(x
, y
);
679 m_text
[textId
].setTextWidth(maxWidth
);
681 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
686 m_textRect
= QRectF(x
- option
.margin
, 0, maximumRequiredTextWidth
+ 2 * option
.margin
, widgetHeight
);
689 void KFileItemListWidget::updateDetailsLayoutTextCache()
691 // Precondition: Requires already updated m_expansionArea
692 // to determine the left position.
695 // | Icon | Name role Additional role 1 Additional role 2
697 m_textRect
= QRectF();
699 const KItemListStyleOption
& option
= styleOption();
700 const QHash
<QByteArray
, QVariant
> values
= data();
702 const qreal widgetHeight
= size().height();
703 const int scaledIconSize
= widgetHeight
- 2 * option
.margin
;
704 const int fontHeight
= option
.fontMetrics
.height();
706 const qreal columnMargin
= option
.margin
* 3;
707 const qreal firstColumnInc
= m_expansionArea
.right() + option
.margin
* 2 + scaledIconSize
;
708 qreal x
= firstColumnInc
;
709 const qreal y
= qMax(qreal(option
.margin
), (widgetHeight
- fontHeight
) / 2);
711 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
712 const TextId textId
= roleTextId(role
);
714 QString text
= roleText(role
, values
);
716 // Elide the text in case it does not fit into the available column-width
717 qreal requiredWidth
= option
.fontMetrics
.width(text
);
718 const qreal columnWidth
= visibleRolesSizes().value(role
, QSizeF(0, 0)).width();
719 qreal availableTextWidth
= columnWidth
- 2 * columnMargin
;
720 if (textId
== Name
) {
721 availableTextWidth
-= firstColumnInc
;
724 if (requiredWidth
> availableTextWidth
) {
725 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
726 requiredWidth
= option
.fontMetrics
.width(text
);
729 m_text
[textId
].setText(text
);
730 m_textPos
[textId
] = QPointF(x
+ columnMargin
, y
);
735 m_textRect
= QRectF(m_textPos
[textId
].x() - option
.margin
, 0,
736 requiredWidth
+ 2 * option
.margin
, size().height());
738 // The column after the name should always be aligned on the same x-position independent
739 // from the expansion-level shown in the name column
744 // The values for the size should be right aligned
745 m_textPos
[textId
].rx() += columnWidth
- requiredWidth
- 2 * columnMargin
;
754 void KFileItemListWidget::updateAdditionalInfoTextColor()
757 if (m_customTextColor
.isValid()) {
758 c1
= m_customTextColor
;
759 } else if (isSelected() && m_layout
!= DetailsLayout
) {
760 c1
= styleOption().palette
.highlightedText().color();
762 c1
= styleOption().palette
.text().color();
765 // For the color of the additional info the inactive text color
766 // is not used as this might lead to unreadable text for some color schemes. Instead
767 // the text color c1 is slightly mixed with the background color.
768 const QColor c2
= styleOption().palette
.base().color();
770 const int p2
= 100 - p1
;
771 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
772 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
773 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
776 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
778 const bool isHiddenItem
= m_text
[Name
].text().startsWith(QLatin1Char('.'));
781 opacity
= painter
->opacity();
782 painter
->setOpacity(opacity
* 0.3);
785 if (m_scaledPixmapSize
!= pixmap
.size()) {
786 QPixmap scaledPixmap
= pixmap
;
787 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
788 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
790 #ifdef KFILEITEMLISTWIDGET_DEBUG
791 painter
->setPen(Qt::green
);
792 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(scaledPixmap
.size())));
795 painter
->drawPixmap(m_pixmapPos
, pixmap
);
799 painter
->setOpacity(opacity
);
803 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
805 const KIcon
icon(name
);
808 if (size
<= KIconLoader::SizeSmall
) {
809 requestedSize
= KIconLoader::SizeSmall
;
810 } else if (size
<= KIconLoader::SizeSmallMedium
) {
811 requestedSize
= KIconLoader::SizeSmallMedium
;
812 } else if (size
<= KIconLoader::SizeMedium
) {
813 requestedSize
= KIconLoader::SizeMedium
;
814 } else if (size
<= KIconLoader::SizeLarge
) {
815 requestedSize
= KIconLoader::SizeLarge
;
816 } else if (size
<= KIconLoader::SizeHuge
) {
817 requestedSize
= KIconLoader::SizeHuge
;
818 } else if (size
<= KIconLoader::SizeEnormous
) {
819 requestedSize
= KIconLoader::SizeEnormous
;
820 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
821 requestedSize
= KIconLoader::SizeEnormous
* 2;
823 requestedSize
= size
;
826 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
827 if (requestedSize
!= size
) {
828 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
834 KFileItemListWidget::TextId
KFileItemListWidget::roleTextId(const QByteArray
& role
)
836 static QHash
<QByteArray
, TextId
> rolesHash
;
837 if (rolesHash
.isEmpty()) {
838 rolesHash
.insert("name", Name
);
839 rolesHash
.insert("size", Size
);
840 rolesHash
.insert("date", Date
);
841 rolesHash
.insert("permissions", Permissions
);
842 rolesHash
.insert("owner", Owner
);
843 rolesHash
.insert("group", Group
);
844 rolesHash
.insert("type", Type
);
845 rolesHash
.insert("destination", Destination
);
846 rolesHash
.insert("path", Path
);
849 return rolesHash
.value(role
);
852 #include "kfileitemlistwidget.moc"