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 return m_customTextColor
.isValid() ? m_customTextColor
: styleOption().palette
.text().color();
285 void KFileItemListWidget::setOverlay(const QPixmap
& overlay
)
288 m_dirtyContent
= true;
292 QPixmap
KFileItemListWidget::overlay() const
297 void KFileItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
298 const QSet
<QByteArray
>& roles
)
300 KItemListWidget::dataChanged(current
, roles
);
301 m_dirtyContent
= true;
303 QSet
<QByteArray
> dirtyRoles
;
304 if (roles
.isEmpty()) {
305 dirtyRoles
= visibleRoles().toSet();
306 dirtyRoles
.insert("iconPixmap");
307 dirtyRoles
.insert("iconName");
312 QSetIterator
<QByteArray
> it(dirtyRoles
);
313 while (it
.hasNext()) {
314 const QByteArray
& role
= it
.next();
315 m_dirtyContentRoles
.insert(role
);
319 void KFileItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
320 const QList
<QByteArray
>& previous
)
322 KItemListWidget::visibleRolesChanged(current
, previous
);
323 m_sortedVisibleRoles
= current
;
324 m_dirtyLayout
= true;
327 void KFileItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
328 const QHash
<QByteArray
, QSizeF
>& previous
)
330 KItemListWidget::visibleRolesSizesChanged(current
, previous
);
331 m_dirtyLayout
= true;
334 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
335 const KItemListStyleOption
& previous
)
337 KItemListWidget::styleOptionChanged(current
, previous
);
338 updateAdditionalInfoTextColor();
339 m_dirtyLayout
= true;
342 void KFileItemListWidget::hoveredChanged(bool hovered
)
345 m_dirtyLayout
= true;
348 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
350 KItemListWidget::resizeEvent(event
);
351 m_dirtyLayout
= true;
354 void KFileItemListWidget::triggerCacheRefreshing()
356 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
362 m_isDir
= data()["isDir"].toBool();
364 updateExpansionArea();
368 m_dirtyLayout
= false;
369 m_dirtyContent
= false;
370 m_dirtyContentRoles
.clear();
373 void KFileItemListWidget::updateExpansionArea()
375 if (m_layout
== DetailsLayout
) {
376 const QHash
<QByteArray
, QVariant
> values
= data();
377 Q_ASSERT(values
.contains("expansionLevel"));
378 const KItemListStyleOption
& option
= styleOption();
379 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
381 const qreal widgetHeight
= size().height();
382 const qreal expansionLevelSize
= KIconLoader::SizeSmall
;
383 const qreal x
= option
.margin
+ expansionLevel
* widgetHeight
;
384 const qreal y
= (widgetHeight
- expansionLevelSize
) / 2;
385 m_expansionArea
= QRectF(x
, y
, expansionLevelSize
, expansionLevelSize
);
387 m_expansionArea
= QRectF();
391 void KFileItemListWidget::updatePixmapCache()
393 // Precondition: Requires already updated m_textPos values to calculate
394 // the remaining height when the alignment is vertical.
396 const bool iconOnTop
= (m_layout
== IconsLayout
);
397 const KItemListStyleOption
& option
= styleOption();
398 const int iconHeight
= option
.iconSize
;
400 const QHash
<QByteArray
, QVariant
> values
= data();
401 const QSizeF widgetSize
= size();
403 int scaledIconHeight
= 0;
405 scaledIconHeight
= static_cast<int>(m_textPos
[Name
].y() - 3 * option
.margin
);
407 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
408 const qreal requiredTextHeight
= textRowsCount
* option
.fontMetrics
.height();
409 scaledIconHeight
= (requiredTextHeight
< iconHeight
) ? widgetSize
.height() - 2 * option
.margin
: iconHeight
;
412 bool updatePixmap
= (iconHeight
!= m_pixmap
.height());
413 if (!updatePixmap
&& m_dirtyContent
) {
414 updatePixmap
= m_dirtyContentRoles
.isEmpty()
415 || m_dirtyContentRoles
.contains("iconPixmap")
416 || m_dirtyContentRoles
.contains("iconName");
420 // The selection toggle should always be applied to the top/left
422 m_selectionTogglePos
= QPointF(-option
.margin
, -option
.margin
);
424 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
425 if (m_pixmap
.isNull()) {
426 // Use the icon that fits to the MIME-type
427 QString iconName
= values
["iconName"].toString();
428 if (iconName
.isEmpty()) {
429 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
430 // use a generic icon as fallback
431 iconName
= QLatin1String("unknown");
433 m_pixmap
= pixmapForIcon(iconName
, iconHeight
);
434 m_hoverPixmapRect
.setSize(m_pixmap
.size());
435 } else if (m_pixmap
.size() != QSize(iconHeight
, iconHeight
)) {
436 // A custom pixmap has been applied. Assure that the pixmap
437 // is scaled to the available size.
438 const bool scale
= m_pixmap
.width() > iconHeight
|| m_pixmap
.height() > iconHeight
||
439 (m_pixmap
.width() < iconHeight
&& m_pixmap
.height() < iconHeight
);
441 KPixmapModifier::scale(m_pixmap
, QSize(iconHeight
, iconHeight
));
443 m_hoverPixmapRect
.setSize(m_pixmap
.size());
445 // To simplify the handling of scaling the original pixmap
446 // will be embedded into a square pixmap.
447 QPixmap
squarePixmap(iconHeight
, iconHeight
);
448 squarePixmap
.fill(Qt::transparent
);
450 QPainter
painter(&squarePixmap
);
453 x
= (iconHeight
- m_pixmap
.width()) / 2; // Center horizontally
454 y
= iconHeight
- m_pixmap
.height(); // Align on bottom
455 painter
.drawPixmap(x
, y
, m_pixmap
);
457 x
= iconHeight
- m_pixmap
.width(); // Align right
458 y
= (iconHeight
- m_pixmap
.height()) / 2; // Center vertically
459 painter
.drawPixmap(x
, y
, m_pixmap
);
461 m_selectionTogglePos
+= QPointF(x
, y
);
463 m_pixmap
= squarePixmap
;
465 m_hoverPixmapRect
.setSize(m_pixmap
.size());
468 Q_ASSERT(m_pixmap
.height() == iconHeight
);
470 if (!m_overlay
.isNull()) {
471 QPainter
painter(&m_pixmap
);
472 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
475 m_scaledPixmapSize
= QSize(scaledIconHeight
, scaledIconHeight
);
478 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
480 m_pixmapPos
.setX(m_textPos
[Name
].x() - 2 * option
.margin
- scaledIconHeight
);
482 m_pixmapPos
.setY(option
.margin
);
485 m_selectionTogglePos
+= m_pixmapPos
;
488 // Center the hover rectangle horizontally and align it on bottom
489 const qreal x
= m_pixmapPos
.x() + (m_scaledPixmapSize
.width() - m_hoverPixmapRect
.width()) / 2.0;
490 const qreal y
= m_pixmapPos
.y() + m_scaledPixmapSize
.height() - m_hoverPixmapRect
.height();
491 m_hoverPixmapRect
.moveTopLeft(QPointF(x
, y
));
493 // Prepare the pixmap that is used when the item gets hovered
495 m_hoverPixmap
= m_pixmap
;
496 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
497 // In the KIconLoader terminology, active = hover.
498 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
499 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
501 m_hoverPixmap
= m_pixmap
;
503 } else if (hoverOpacity() <= 0.0) {
504 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
505 m_hoverPixmap
= QPixmap();
509 void KFileItemListWidget::updateTextsCache()
511 QTextOption textOption
;
514 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
515 textOption
.setAlignment(Qt::AlignHCenter
);
519 textOption
.setAlignment(Qt::AlignLeft
);
520 textOption
.setWrapMode(QTextOption::NoWrap
);
527 for (int i
= 0; i
< TextIdCount
; ++i
) {
528 m_text
[i
].setText(QString());
529 m_text
[i
].setTextOption(textOption
);
533 case IconsLayout
: updateIconsLayoutTextCache(); break;
534 case CompactLayout
: updateCompactLayoutTextCache(); break;
535 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
536 default: Q_ASSERT(false); break;
540 void KFileItemListWidget::updateIconsLayoutTextCache()
547 // might get wrapped above
552 const QHash
<QByteArray
, QVariant
> values
= data();
554 const KItemListStyleOption
& option
= styleOption();
555 const qreal maxWidth
= size().width() - 2 * option
.margin
;
556 const qreal widgetHeight
= size().height();
557 const qreal fontHeight
= option
.fontMetrics
.height();
559 // Initialize properties for the "name" role. It will be used as anchor
560 // for initializing the position of the other roles.
561 m_text
[Name
].setText(KStringHandler::preProcessWrap(values
["name"].toString()));
563 // Calculate the number of lines required for the name and the required width
564 int textLinesCountForName
= 0;
565 qreal requiredWidthForName
= 0;
568 QTextLayout
layout(m_text
[Name
].text(), option
.font
);
569 layout
.setTextOption(m_text
[Name
].textOption());
570 layout
.beginLayout();
571 while ((line
= layout
.createLine()).isValid()) {
572 line
.setLineWidth(maxWidth
);
573 requiredWidthForName
= qMax(requiredWidthForName
, line
.naturalTextWidth());
574 ++textLinesCountForName
;
578 // Use one line for each additional information
579 int textLinesCount
= textLinesCountForName
;
580 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
581 textLinesCount
+= additionalRolesCount
;
583 m_text
[Name
].setTextWidth(maxWidth
);
584 m_textPos
[Name
] = QPointF(option
.margin
, widgetHeight
- textLinesCount
* fontHeight
- option
.margin
);
585 m_textRect
= QRectF(option
.margin
+ (maxWidth
- requiredWidthForName
) / 2,
587 requiredWidthForName
,
588 m_text
[Name
].size().height());
590 // Calculate the position for each additional information
591 qreal y
= m_textPos
[Name
].y() + textLinesCountForName
* fontHeight
;
592 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
593 const TextId textId
= roleTextId(role
);
594 if (textId
== Name
) {
598 const QString text
= roleText(role
, values
);
599 m_text
[textId
].setText(text
);
601 qreal requiredWidth
= 0;
603 QTextLayout
layout(text
, option
.font
);
604 layout
.setTextOption(m_text
[textId
].textOption());
605 layout
.beginLayout();
606 QTextLine textLine
= layout
.createLine();
607 if (textLine
.isValid()) {
608 textLine
.setLineWidth(maxWidth
);
609 requiredWidth
= textLine
.naturalTextWidth();
610 if (textLine
.textLength() < text
.length()) {
611 // TODO: QFontMetrics::elidedText() works different regarding the given width
612 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
613 // not get elided although it does not fit into the given width. As workaround
614 // the margin is substracted.
615 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
- option
.margin
);
616 m_text
[textId
].setText(elidedText
);
621 m_textPos
[textId
] = QPointF(option
.margin
, y
);
622 m_text
[textId
].setTextWidth(maxWidth
);
624 const QRectF
textRect(option
.margin
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, fontHeight
);
625 m_textRect
|= textRect
;
630 // Add a margin to the text rectangle
631 const qreal margin
= option
.margin
;
632 m_textRect
.adjust(-margin
, -margin
, margin
, margin
);
635 void KFileItemListWidget::updateCompactLayoutTextCache()
637 // +------+ Name role
638 // | Icon | Additional role 1
639 // +------+ Additional role 2
641 const QHash
<QByteArray
, QVariant
> values
= data();
643 const KItemListStyleOption
& option
= styleOption();
644 const qreal widgetHeight
= size().height();
645 const qreal fontHeight
= option
.fontMetrics
.height();
646 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * fontHeight
;
647 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.margin
: option
.iconSize
;
649 qreal maximumRequiredTextWidth
= 0;
650 const qreal x
= option
.margin
* 3 + scaledIconSize
;
651 qreal y
= (widgetHeight
- textLinesHeight
) / 2;
652 const qreal maxWidth
= size().width() - x
- option
.margin
;
653 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
654 const TextId textId
= roleTextId(role
);
656 const QString text
= roleText(role
, values
);
657 m_text
[textId
].setText(text
);
659 qreal requiredWidth
= option
.fontMetrics
.width(text
);
660 if (requiredWidth
> maxWidth
) {
661 requiredWidth
= maxWidth
;
662 const QString elidedText
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
663 m_text
[textId
].setText(elidedText
);
666 m_textPos
[textId
] = QPointF(x
, y
);
667 m_text
[textId
].setTextWidth(maxWidth
);
669 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
674 m_textRect
= QRectF(x
- option
.margin
, 0, maximumRequiredTextWidth
+ 2 * option
.margin
, widgetHeight
);
677 void KFileItemListWidget::updateDetailsLayoutTextCache()
679 // Precondition: Requires already updated m_expansionArea
680 // to determine the left position.
683 // | Icon | Name role Additional role 1 Additional role 2
685 m_textRect
= QRectF();
687 const KItemListStyleOption
& option
= styleOption();
688 const QHash
<QByteArray
, QVariant
> values
= data();
690 const qreal widgetHeight
= size().height();
691 const int scaledIconSize
= widgetHeight
- 2 * option
.margin
;
692 const int fontHeight
= option
.fontMetrics
.height();
694 const qreal columnMargin
= option
.margin
* 3;
695 const qreal firstColumnInc
= m_expansionArea
.right() + option
.margin
* 2 + scaledIconSize
;
696 qreal x
= firstColumnInc
;
697 const qreal y
= qMax(qreal(option
.margin
), (widgetHeight
- fontHeight
) / 2);
699 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
700 const TextId textId
= roleTextId(role
);
702 QString text
= roleText(role
, values
);
704 // Elide the text in case it does not fit into the available column-width
705 qreal requiredWidth
= option
.fontMetrics
.width(text
);
706 const qreal columnWidth
= visibleRolesSizes().value(role
, QSizeF(0, 0)).width();
707 qreal availableTextWidth
= columnWidth
- 2 * columnMargin
;
708 if (textId
== Name
) {
709 availableTextWidth
-= firstColumnInc
;
712 if (requiredWidth
> availableTextWidth
) {
713 text
= option
.fontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
714 requiredWidth
= option
.fontMetrics
.width(text
);
717 m_text
[textId
].setText(text
);
718 m_textPos
[textId
] = QPointF(x
+ columnMargin
, y
);
723 m_textRect
= QRectF(m_textPos
[textId
].x() - option
.margin
, 0,
724 requiredWidth
+ 2 * option
.margin
, size().height());
726 // The column after the name should always be aligned on the same x-position independent
727 // from the expansion-level shown in the name column
732 // The values for the size should be right aligned
733 m_textPos
[textId
].rx() += columnWidth
- requiredWidth
- 2 * columnMargin
;
742 void KFileItemListWidget::updateAdditionalInfoTextColor()
744 // For the color of the additional info the inactive text color
745 // is not used as this might lead to unreadable text for some color schemes. Instead
746 // the text color is slightly mixed with the background color.
747 const QColor c1
= textColor();
748 const QColor c2
= styleOption().palette
.base().color();
750 const int p2
= 100 - p1
;
751 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
752 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
753 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
756 void KFileItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
758 const bool isHiddenItem
= m_text
[Name
].text().startsWith(QLatin1Char('.'));
761 opacity
= painter
->opacity();
762 painter
->setOpacity(opacity
* 0.3);
765 if (m_scaledPixmapSize
!= pixmap
.size()) {
766 QPixmap scaledPixmap
= pixmap
;
767 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
768 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
770 #ifdef KFILEITEMLISTWIDGET_DEBUG
771 painter
->setPen(Qt::green
);
772 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(scaledPixmap
.size())));
775 painter
->drawPixmap(m_pixmapPos
, pixmap
);
779 painter
->setOpacity(opacity
);
783 QPixmap
KFileItemListWidget::pixmapForIcon(const QString
& name
, int size
)
785 const KIcon
icon(name
);
788 if (size
<= KIconLoader::SizeSmall
) {
789 requestedSize
= KIconLoader::SizeSmall
;
790 } else if (size
<= KIconLoader::SizeSmallMedium
) {
791 requestedSize
= KIconLoader::SizeSmallMedium
;
792 } else if (size
<= KIconLoader::SizeMedium
) {
793 requestedSize
= KIconLoader::SizeMedium
;
794 } else if (size
<= KIconLoader::SizeLarge
) {
795 requestedSize
= KIconLoader::SizeLarge
;
796 } else if (size
<= KIconLoader::SizeHuge
) {
797 requestedSize
= KIconLoader::SizeHuge
;
798 } else if (size
<= KIconLoader::SizeEnormous
) {
799 requestedSize
= KIconLoader::SizeEnormous
;
800 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
801 requestedSize
= KIconLoader::SizeEnormous
* 2;
803 requestedSize
= size
;
806 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
807 if (requestedSize
!= size
) {
808 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
814 KFileItemListWidget::TextId
KFileItemListWidget::roleTextId(const QByteArray
& role
)
816 static QHash
<QByteArray
, TextId
> rolesHash
;
817 if (rolesHash
.isEmpty()) {
818 rolesHash
.insert("name", Name
);
819 rolesHash
.insert("size", Size
);
820 rolesHash
.insert("date", Date
);
821 rolesHash
.insert("permissions", Permissions
);
822 rolesHash
.insert("owner", Owner
);
823 rolesHash
.insert("group", Group
);
824 rolesHash
.insert("type", Type
);
825 rolesHash
.insert("destination", Destination
);
826 rolesHash
.insert("path", Path
);
829 return rolesHash
.value(role
);
832 #include "kfileitemlistwidget.moc"