1 /***************************************************************************
2 * Copyright (C) 2012 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 "kstandarditemlistwidget.h"
22 #include "kfileitemlistview.h"
23 #include "kfileitemmodel.h"
26 #include <KIconEffect>
27 #include <KIconLoader>
29 #include <kratingpainter.h>
30 #include <KStringHandler>
33 #include "private/kfileitemclipboard.h"
34 #include "private/kitemlistroleeditor.h"
35 #include "private/kpixmapmodifier.h"
37 #include <QFontMetricsF>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneResizeEvent>
40 #include <QGraphicsView>
42 #include <QStyleOption>
43 #include <QTextLayout>
45 #include <QPixmapCache>
47 // #define KSTANDARDITEMLISTWIDGET_DEBUG
49 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
50 KItemListWidgetInformant()
54 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
58 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
60 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
61 case KStandardItemListWidget::IconsLayout
:
62 calculateIconsLayoutItemSizeHints(sizeHints
, view
);
65 case KStandardItemListWidget::CompactLayout
:
66 calculateCompactLayoutItemSizeHints(sizeHints
, view
);
69 case KStandardItemListWidget::DetailsLayout
:
70 calculateDetailsLayoutItemSizeHints(sizeHints
, view
);
79 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
81 const KItemListView
* view
) const
83 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
84 const KItemListStyleOption
& option
= view
->styleOption();
86 const QString text
= roleText(role
, values
);
87 qreal width
= KStandardItemListWidget::columnPadding(option
);
89 if (role
== "rating") {
90 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
92 width
+= option
.fontMetrics
.width(text
);
95 if (view
->supportsItemExpanding()) {
96 // Increase the width by the expansion-toggle and the current expansion level
97 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
98 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
99 width
+= (expandedParentsCount
+ 1) * height
;
102 // Increase the width by the required space for the icon
103 width
+= option
.padding
* 2 + option
.iconSize
;
110 QString
KStandardItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
112 return view
->model()->data(index
).value("text").toString();
115 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
116 const QHash
<QByteArray
, QVariant
>& values
) const
118 if (role
== "rating") {
119 // Always use an empty text, as the rating is shown by the image m_rating.
122 return values
.value(role
).toString();
125 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
127 const KItemListStyleOption
& option
= view
->styleOption();
128 const QFont
& font
= option
.font
;
129 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
131 const qreal itemWidth
= view
->itemSize().width();
132 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
133 const qreal maxTextHeight
= option
.maxTextSize
.height();
134 const qreal additionalRolesSpacing
= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
135 const qreal spacingAndIconHeight
= option
.iconSize
+ option
.padding
* 3;
137 QTextOption
textOption(Qt::AlignHCenter
);
138 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
140 for (int index
= 0; index
< sizeHints
.count(); ++index
) {
141 if (!sizeHints
.at(index
).isEmpty()) {
145 const QString
& text
= KStringHandler::preProcessWrap(itemText(index
, view
));
147 // Calculate the number of lines required for wrapping the name
148 qreal textHeight
= 0;
149 QTextLayout
layout(text
, font
);
150 layout
.setTextOption(textOption
);
151 layout
.beginLayout();
153 while ((line
= layout
.createLine()).isValid()) {
154 line
.setLineWidth(maxWidth
);
155 line
.naturalTextWidth();
156 textHeight
+= line
.height();
160 // Add one line for each additional information
161 textHeight
+= additionalRolesSpacing
;
163 if (maxTextHeight
> 0 && textHeight
> maxTextHeight
) {
164 textHeight
= maxTextHeight
;
167 sizeHints
[index
] = QSizeF(itemWidth
, textHeight
+ spacingAndIconHeight
);
171 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
173 const KItemListStyleOption
& option
= view
->styleOption();
174 const QFontMetrics
& fontMetrics
= option
.fontMetrics
;
175 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
177 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
178 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
179 const qreal maxWidth
= option
.maxTextSize
.width();
180 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
181 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * option
.fontMetrics
.lineSpacing());
183 for (int index
= 0; index
< sizeHints
.count(); ++index
) {
184 if (!sizeHints
.at(index
).isEmpty()) {
188 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
189 // to show all roles without horizontal clipping.
190 qreal maximumRequiredWidth
= 0.0;
192 if (showOnlyTextRole
) {
193 maximumRequiredWidth
= fontMetrics
.width(itemText(index
, view
));
195 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
196 foreach (const QByteArray
& role
, visibleRoles
) {
197 const QString
& text
= roleText(role
, values
);
198 const qreal requiredWidth
= fontMetrics
.width(text
);
199 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
203 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
204 if (maxWidth
> 0 && width
> maxWidth
) {
208 sizeHints
[index
] = QSizeF(width
, height
);
212 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
214 const KItemListStyleOption
& option
= view
->styleOption();
215 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
217 for (int index
= 0; index
< sizeHints
.count(); ++index
) {
218 if (!sizeHints
.at(index
).isEmpty()) {
222 sizeHints
[index
] = QSizeF(-1, height
);
226 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
227 KItemListWidget(informant
, parent
),
231 m_customizedFontMetrics(m_customizedFont
),
232 m_isExpandable(false),
233 m_supportsItemExpanding(false),
235 m_dirtyContent(true),
236 m_dirtyContentRoles(),
237 m_layout(IconsLayout
),
240 m_scaledPixmapSize(),
245 m_sortedVisibleRoles(),
248 m_additionalInfoTextColor(),
256 KStandardItemListWidget::~KStandardItemListWidget()
258 qDeleteAll(m_textInfo
);
262 m_roleEditor
->deleteLater();
265 if (m_oldRoleEditor
) {
266 m_oldRoleEditor
->deleteLater();
270 void KStandardItemListWidget::setLayout(Layout layout
)
272 if (m_layout
!= layout
) {
274 m_dirtyLayout
= true;
275 updateAdditionalInfoTextColor();
280 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
285 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
287 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
288 m_supportsItemExpanding
= supportsItemExpanding
;
289 m_dirtyLayout
= true;
294 bool KStandardItemListWidget::supportsItemExpanding() const
296 return m_supportsItemExpanding
;
299 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
301 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
303 KItemListWidget::paint(painter
, option
, widget
);
305 if (!m_expansionArea
.isEmpty()) {
306 drawSiblingsInformation(painter
);
309 const KItemListStyleOption
& itemListStyleOption
= styleOption();
311 if (hoverOpacity() < 1.0) {
313 * Linear interpolation between m_pixmap and m_hoverPixmap.
315 * Note that this cannot be achieved by painting m_hoverPixmap over
316 * m_pixmap, even if the opacities are adjusted. For details see
317 * https://git.reviewboard.kde.org/r/109614/
319 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
320 QPixmap
pixmap1(m_pixmap
.size());
321 pixmap1
.fill(Qt::transparent
);
323 QPainter
p(&pixmap1
);
324 p
.setOpacity(1.0 - hoverOpacity());
325 p
.drawPixmap(0, 0, m_pixmap
);
328 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
329 QPixmap
pixmap2(pixmap1
.size());
330 pixmap2
.fill(Qt::transparent
);
332 QPainter
p(&pixmap2
);
333 p
.setOpacity(hoverOpacity());
334 p
.drawPixmap(0, 0, m_hoverPixmap
);
337 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
338 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
339 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
341 QPainter
p(&pixmap1
);
342 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
343 p
.drawPixmap(0, 0, pixmap2
);
346 // Finally paint pixmap1 on the widget
347 drawPixmap(painter
, pixmap1
);
349 drawPixmap(painter
, m_hoverPixmap
);
352 drawPixmap(painter
, m_pixmap
);
355 painter
->setFont(m_customizedFont
);
356 painter
->setPen(textColor());
357 const TextInfo
* textInfo
= m_textInfo
.value("text");
360 // It seems that we can end up here even if m_textInfo does not contain
361 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
362 // this can only happen if the index is negative. This can happen when
363 // the item is about to be removed, see KItemListView::slotItemsRemoved().
364 // TODO: try to reproduce the crash and find a better fix.
368 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
370 bool clipAdditionalInfoBounds
= false;
371 if (m_supportsItemExpanding
) {
372 // Prevent a possible overlapping of the additional-information texts
373 // with the icon. This can happen if the user has minimized the width
374 // of the name-column to a very small value.
375 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
376 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
377 clipAdditionalInfoBounds
= true;
379 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
383 painter
->setPen(m_additionalInfoTextColor
);
384 painter
->setFont(m_customizedFont
);
386 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
387 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
388 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
391 if (!m_rating
.isNull()) {
392 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
393 QPointF pos
= ratingTextInfo
->pos
;
394 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
395 if (align
& Qt::AlignHCenter
) {
396 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
398 painter
->drawPixmap(pos
, m_rating
);
401 if (clipAdditionalInfoBounds
) {
405 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
406 painter
->setBrush(Qt::NoBrush
);
407 painter
->setPen(Qt::green
);
408 painter
->drawRect(m_iconRect
);
410 painter
->setPen(Qt::blue
);
411 painter
->drawRect(m_textRect
);
413 painter
->setPen(Qt::red
);
414 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
415 painter
->drawRect(rect());
419 QRectF
KStandardItemListWidget::iconRect() const
421 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
425 QRectF
KStandardItemListWidget::textRect() const
427 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
431 QRectF
KStandardItemListWidget::textFocusRect() const
433 // In the compact- and details-layout a larger textRect() is returned to be aligned
434 // with the iconRect(). This is useful to have a larger selection/hover-area
435 // when having a quite large icon size but only one line of text. Still the
436 // focus rectangle should be shown as narrow as possible around the text.
438 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
441 case CompactLayout
: {
442 QRectF rect
= m_textRect
;
443 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
444 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
445 rect
.setTop(topText
->pos
.y());
446 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
450 case DetailsLayout
: {
451 QRectF rect
= m_textRect
;
452 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
453 rect
.setTop(textInfo
->pos
.y());
454 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
456 const KItemListStyleOption
& option
= styleOption();
457 if (option
.extendedSelectionRegion
) {
458 const QString text
= textInfo
->staticText
.text();
459 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
472 QRectF
KStandardItemListWidget::expansionToggleRect() const
474 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
475 return m_isExpandable
? m_expansionArea
: QRectF();
478 QRectF
KStandardItemListWidget::selectionToggleRect() const
480 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
482 const int iconHeight
= styleOption().iconSize
;
484 int toggleSize
= KIconLoader::SizeSmall
;
485 if (iconHeight
>= KIconLoader::SizeEnormous
) {
486 toggleSize
= KIconLoader::SizeMedium
;
487 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
488 toggleSize
= KIconLoader::SizeSmallMedium
;
491 QPointF pos
= iconRect().topLeft();
493 // If the selection toggle has a very small distance to the
494 // widget borders, the size of the selection toggle will get
495 // increased to prevent an accidental clicking of the item
496 // when trying to hit the toggle.
497 const int widgetHeight
= size().height();
498 const int widgetWidth
= size().width();
499 const int minMargin
= 2;
501 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
502 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
503 toggleSize
= widgetHeight
;
506 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
507 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
508 toggleSize
= widgetWidth
;
512 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
515 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
518 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
519 if (m_layout
!= DetailsLayout
) {
523 // Only return the content of the text-column as pixmap
524 const int leftClip
= m_pixmapPos
.x();
526 const TextInfo
* textInfo
= m_textInfo
.value("text");
527 const int rightClip
= textInfo
->pos
.x() +
528 textInfo
->staticText
.size().width() +
529 2 * styleOption().padding
;
531 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
532 clippedPixmap
.fill(Qt::transparent
);
534 QPainter
painter(&clippedPixmap
);
535 painter
.drawPixmap(-leftClip
, 0, pixmap
);
537 return clippedPixmap
;
541 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
543 return new KStandardItemListWidgetInformant();
546 void KStandardItemListWidget::invalidateCache()
548 m_dirtyLayout
= true;
549 m_dirtyContent
= true;
552 void KStandardItemListWidget::refreshCache()
556 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
562 bool KStandardItemListWidget::isHidden() const
567 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
572 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
574 return QPalette::Text
;
577 void KStandardItemListWidget::setTextColor(const QColor
& color
)
579 if (color
!= m_customTextColor
) {
580 m_customTextColor
= color
;
581 updateAdditionalInfoTextColor();
586 QColor
KStandardItemListWidget::textColor() const
590 return m_additionalInfoTextColor
;
591 } else if (m_customTextColor
.isValid()) {
592 return m_customTextColor
;
596 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
597 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
598 return styleOption().palette
.color(group
, role
);
601 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
604 m_dirtyContent
= true;
608 QPixmap
KStandardItemListWidget::overlay() const
614 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
615 const QHash
<QByteArray
, QVariant
>& values
) const
617 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
620 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
621 const QSet
<QByteArray
>& roles
)
625 m_dirtyContent
= true;
627 QSet
<QByteArray
> dirtyRoles
;
628 if (roles
.isEmpty()) {
629 dirtyRoles
= visibleRoles().toSet();
634 // The icon-state might depend from other roles and hence is
635 // marked as dirty whenever a role has been changed
636 dirtyRoles
.insert("iconPixmap");
637 dirtyRoles
.insert("iconName");
639 QSetIterator
<QByteArray
> it(dirtyRoles
);
640 while (it
.hasNext()) {
641 const QByteArray
& role
= it
.next();
642 m_dirtyContentRoles
.insert(role
);
646 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
647 const QList
<QByteArray
>& previous
)
650 m_sortedVisibleRoles
= current
;
651 m_dirtyLayout
= true;
654 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
661 m_dirtyLayout
= true;
664 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
665 const KItemListStyleOption
& previous
)
669 updateAdditionalInfoTextColor();
670 m_dirtyLayout
= true;
673 void KStandardItemListWidget::hoveredChanged(bool hovered
)
676 m_dirtyLayout
= true;
679 void KStandardItemListWidget::selectedChanged(bool selected
)
682 updateAdditionalInfoTextColor();
683 m_dirtyContent
= true;
686 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
690 m_dirtyLayout
= true;
693 int KStandardItemListWidget::selectionLength(const QString
& text
) const
695 return text
.length();
698 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
702 QGraphicsView
* parent
= scene()->views()[0];
703 if (current
.isEmpty() || !parent
|| current
!= "text") {
705 emit
roleEditingCanceled(index(), current
, data().value(current
));
707 disconnect(m_roleEditor
, SIGNAL(roleEditingCanceled(QByteArray
,QVariant
)),
708 this, SLOT(slotRoleEditingCanceled(QByteArray
,QVariant
)));
709 disconnect(m_roleEditor
, SIGNAL(roleEditingFinished(QByteArray
,QVariant
)),
710 this, SLOT(slotRoleEditingFinished(QByteArray
,QVariant
)));
712 if (m_oldRoleEditor
) {
713 m_oldRoleEditor
->deleteLater();
715 m_oldRoleEditor
= m_roleEditor
;
716 m_roleEditor
->hide();
722 Q_ASSERT(!m_roleEditor
);
724 const TextInfo
* textInfo
= m_textInfo
.value("text");
726 m_roleEditor
= new KItemListRoleEditor(parent
);
727 m_roleEditor
->setRole(current
);
728 m_roleEditor
->setFont(styleOption().font
);
730 const QString text
= data().value(current
).toString();
731 m_roleEditor
->setPlainText(text
);
733 QTextOption textOption
= textInfo
->staticText
.textOption();
734 m_roleEditor
->document()->setDefaultTextOption(textOption
);
736 const int textSelectionLength
= selectionLength(text
);
738 if (textSelectionLength
> 0) {
739 QTextCursor cursor
= m_roleEditor
->textCursor();
740 cursor
.movePosition(QTextCursor::StartOfBlock
);
741 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
742 m_roleEditor
->setTextCursor(cursor
);
745 connect(m_roleEditor
, SIGNAL(roleEditingCanceled(QByteArray
,QVariant
)),
746 this, SLOT(slotRoleEditingCanceled(QByteArray
,QVariant
)));
747 connect(m_roleEditor
, SIGNAL(roleEditingFinished(QByteArray
,QVariant
)),
748 this, SLOT(slotRoleEditingFinished(QByteArray
,QVariant
)));
750 // Adjust the geometry of the editor
751 QRectF rect
= roleEditingRect(current
);
752 const int frameWidth
= m_roleEditor
->frameWidth();
753 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
754 rect
.translate(pos());
755 if (rect
.right() > parent
->width()) {
756 rect
.setWidth(parent
->width() - rect
.left());
758 m_roleEditor
->setGeometry(rect
.toRect());
759 m_roleEditor
->show();
760 m_roleEditor
->setFocus();
763 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
766 setEditedRole(QByteArray());
767 Q_ASSERT(!m_roleEditor
);
770 KItemListWidget::resizeEvent(event
);
772 m_dirtyLayout
= true;
775 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
777 KItemListWidget::showEvent(event
);
779 // Listen to changes of the clipboard to mark the item as cut/uncut
780 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
782 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
783 m_isCut
= clipboard
->isCut(itemUrl
);
785 connect(clipboard
, SIGNAL(cutItemsChanged()),
786 this, SLOT(slotCutItemsChanged()));
789 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
791 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
792 this, SLOT(slotCutItemsChanged()));
794 KItemListWidget::hideEvent(event
);
797 void KStandardItemListWidget::slotCutItemsChanged()
799 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
800 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
801 if (m_isCut
!= isCut
) {
803 m_pixmap
= QPixmap();
804 m_dirtyContent
= true;
809 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
810 const QVariant
& value
)
813 emit
roleEditingCanceled(index(), role
, value
);
814 setEditedRole(QByteArray());
817 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
818 const QVariant
& value
)
821 emit
roleEditingFinished(index(), role
, value
);
822 setEditedRole(QByteArray());
825 void KStandardItemListWidget::triggerCacheRefreshing()
827 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
833 const QHash
<QByteArray
, QVariant
> values
= data();
834 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
835 m_isHidden
= isHidden();
836 m_customizedFont
= customizedFont(styleOption().font
);
837 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
839 updateExpansionArea();
843 m_dirtyLayout
= false;
844 m_dirtyContent
= false;
845 m_dirtyContentRoles
.clear();
848 void KStandardItemListWidget::updateExpansionArea()
850 if (m_supportsItemExpanding
) {
851 const QHash
<QByteArray
, QVariant
> values
= data();
852 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
853 if (expandedParentsCount
>= 0) {
854 const KItemListStyleOption
& option
= styleOption();
855 const qreal widgetHeight
= size().height();
856 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
857 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
859 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
864 m_expansionArea
= QRectF();
867 void KStandardItemListWidget::updatePixmapCache()
869 // Precondition: Requires already updated m_textPos values to calculate
870 // the remaining height when the alignment is vertical.
872 const QSizeF widgetSize
= size();
873 const bool iconOnTop
= (m_layout
== IconsLayout
);
874 const KItemListStyleOption
& option
= styleOption();
875 const qreal padding
= option
.padding
;
877 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
878 const int maxIconHeight
= option
.iconSize
;
880 const QHash
<QByteArray
, QVariant
> values
= data();
882 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
883 if (!updatePixmap
&& m_dirtyContent
) {
884 updatePixmap
= m_dirtyContentRoles
.isEmpty()
885 || m_dirtyContentRoles
.contains("iconPixmap")
886 || m_dirtyContentRoles
.contains("iconName")
887 || m_dirtyContentRoles
.contains("iconOverlays");
891 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
892 if (m_pixmap
.isNull()) {
893 // Use the icon that fits to the MIME-type
894 QString iconName
= values
["iconName"].toString();
895 if (iconName
.isEmpty()) {
896 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
897 // use a generic icon as fallback
898 iconName
= QLatin1String("unknown");
900 const QStringList overlays
= values
["iconOverlays"].toStringList();
901 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
);
902 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
903 // A custom pixmap has been applied. Assure that the pixmap
904 // is scaled to the maximum available size.
905 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
909 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
910 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
914 KIconEffect::semiTransparent(m_pixmap
);
918 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
919 QImage image
= m_pixmap
.toImage();
920 KIconEffect::colorize(image
, color
, 0.8f
);
921 m_pixmap
= QPixmap::fromImage(image
);
925 if (!m_overlay
.isNull()) {
926 QPainter
painter(&m_pixmap
);
927 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
930 int scaledIconSize
= 0;
932 const TextInfo
* textInfo
= m_textInfo
.value("text");
933 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
935 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
936 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
937 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
938 widgetSize
.height() - 2 * padding
: maxIconHeight
;
941 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
942 const int maxScaledIconHeight
= scaledIconSize
;
944 m_scaledPixmapSize
= m_pixmap
.size();
945 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
948 // Center horizontally and align on bottom within the icon-area
949 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
950 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
952 // Center horizontally and vertically within the icon-area
953 const TextInfo
* textInfo
= m_textInfo
.value("text");
954 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
955 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
956 m_pixmapPos
.setY(padding
957 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
960 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
962 // Prepare the pixmap that is used when the item gets hovered
964 m_hoverPixmap
= m_pixmap
;
965 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
966 // In the KIconLoader terminology, active = hover.
967 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
968 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
970 m_hoverPixmap
= m_pixmap
;
972 } else if (hoverOpacity() <= 0.0) {
973 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
974 m_hoverPixmap
= QPixmap();
978 void KStandardItemListWidget::updateTextsCache()
980 QTextOption textOption
;
983 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
984 textOption
.setAlignment(Qt::AlignHCenter
);
988 textOption
.setAlignment(Qt::AlignLeft
);
989 textOption
.setWrapMode(QTextOption::NoWrap
);
996 qDeleteAll(m_textInfo
);
998 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
999 TextInfo
* textInfo
= new TextInfo();
1000 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1001 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1002 textInfo
->staticText
.setTextOption(textOption
);
1003 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1007 case IconsLayout
: updateIconsLayoutTextCache(); break;
1008 case CompactLayout
: updateCompactLayoutTextCache(); break;
1009 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1010 default: Q_ASSERT(false); break;
1013 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1014 if (ratingTextInfo
) {
1015 // The text of the rating-role has been set to empty to get
1016 // replaced by a rating-image showing the rating as stars.
1017 const KItemListStyleOption
& option
= styleOption();
1018 QSizeF ratingSize
= preferredRatingSize(option
);
1020 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1021 ? columnWidth("rating") - columnPadding(option
)
1023 if (ratingSize
.width() > availableWidth
) {
1024 ratingSize
.rwidth() = availableWidth
;
1026 m_rating
= QPixmap(ratingSize
.toSize());
1027 m_rating
.fill(Qt::transparent
);
1029 QPainter
painter(&m_rating
);
1030 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
1031 const int rating
= data().value("rating").toInt();
1032 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1033 } else if (!m_rating
.isNull()) {
1034 m_rating
= QPixmap();
1038 void KStandardItemListWidget::updateIconsLayoutTextCache()
1045 // might get wrapped above
1047 // Additional role 1
1048 // Additional role 2
1050 const QHash
<QByteArray
, QVariant
> values
= data();
1052 const KItemListStyleOption
& option
= styleOption();
1053 const qreal padding
= option
.padding
;
1054 const qreal maxWidth
= size().width() - 2 * padding
;
1055 const qreal widgetHeight
= size().height();
1056 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1058 // Initialize properties for the "text" role. It will be used as anchor
1059 // for initializing the position of the other roles.
1060 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1061 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1062 nameTextInfo
->staticText
.setText(nameText
);
1064 // Calculate the number of lines required for the name and the required width
1065 qreal nameWidth
= 0;
1066 qreal nameHeight
= 0;
1069 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1070 const int maxNameLines
= (option
.maxTextSize
.height() / int(lineSpacing
)) - additionalRolesCount
;
1072 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1073 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1074 layout
.beginLayout();
1075 int nameLineIndex
= 0;
1076 while ((line
= layout
.createLine()).isValid()) {
1077 line
.setLineWidth(maxWidth
);
1078 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1079 nameHeight
+= line
.height();
1082 if (nameLineIndex
== maxNameLines
) {
1083 // The maximum number of textlines has been reached. If this is
1084 // the case provide an elided text if necessary.
1085 const int textLength
= line
.textStart() + line
.textLength();
1086 if (textLength
< nameText
.length()) {
1087 // Elide the last line of the text
1088 QString lastTextLine
= nameText
.mid(line
.textStart());
1089 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
1092 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1093 nameTextInfo
->staticText
.setText(elidedText
);
1095 const qreal lastLineWidth
= m_customizedFontMetrics
.boundingRect(lastTextLine
).width();
1096 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1103 // Use one line for each additional information
1104 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1105 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1107 additionalRolesCount
* lineSpacing
-
1109 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1110 nameTextInfo
->pos
.y(),
1114 // Calculate the position for each additional information
1115 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1116 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1117 if (role
== "text") {
1121 const QString text
= roleText(role
, values
);
1122 TextInfo
* textInfo
= m_textInfo
.value(role
);
1123 textInfo
->staticText
.setText(text
);
1125 qreal requiredWidth
= 0;
1127 QTextLayout
layout(text
, m_customizedFont
);
1128 QTextOption textOption
;
1129 textOption
.setWrapMode(QTextOption::NoWrap
);
1130 layout
.setTextOption(textOption
);
1132 layout
.beginLayout();
1133 QTextLine textLine
= layout
.createLine();
1134 if (textLine
.isValid()) {
1135 textLine
.setLineWidth(maxWidth
);
1136 requiredWidth
= textLine
.naturalTextWidth();
1137 if (requiredWidth
> maxWidth
) {
1138 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1139 textInfo
->staticText
.setText(elidedText
);
1140 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1141 } else if (role
== "rating") {
1142 // Use the width of the rating pixmap, because the rating text is empty.
1143 requiredWidth
= m_rating
.width();
1148 textInfo
->pos
= QPointF(padding
, y
);
1149 textInfo
->staticText
.setTextWidth(maxWidth
);
1151 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1152 m_textRect
|= textRect
;
1157 // Add a padding to the text rectangle
1158 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1161 void KStandardItemListWidget::updateCompactLayoutTextCache()
1163 // +------+ Name role
1164 // | Icon | Additional role 1
1165 // +------+ Additional role 2
1167 const QHash
<QByteArray
, QVariant
> values
= data();
1169 const KItemListStyleOption
& option
= styleOption();
1170 const qreal widgetHeight
= size().height();
1171 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1172 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1173 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1175 qreal maximumRequiredTextWidth
= 0;
1176 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1177 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1178 const qreal maxWidth
= size().width() - x
- option
.padding
;
1179 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1180 const QString text
= roleText(role
, values
);
1181 TextInfo
* textInfo
= m_textInfo
.value(role
);
1182 textInfo
->staticText
.setText(text
);
1184 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1185 if (requiredWidth
> maxWidth
) {
1186 requiredWidth
= maxWidth
;
1187 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1188 textInfo
->staticText
.setText(elidedText
);
1191 textInfo
->pos
= QPointF(x
, y
);
1192 textInfo
->staticText
.setTextWidth(maxWidth
);
1194 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1199 m_textRect
= QRectF(x
- 2 * option
.padding
, 0, maximumRequiredTextWidth
+ 3 * option
.padding
, widgetHeight
);
1202 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1204 // Precondition: Requires already updated m_expansionArea
1205 // to determine the left position.
1208 // | Icon | Name role Additional role 1 Additional role 2
1210 m_textRect
= QRectF();
1212 const KItemListStyleOption
& option
= styleOption();
1213 const QHash
<QByteArray
, QVariant
> values
= data();
1215 const qreal widgetHeight
= size().height();
1216 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1217 const int fontHeight
= m_customizedFontMetrics
.height();
1219 const qreal columnWidthInc
= columnPadding(option
);
1220 qreal firstColumnInc
= scaledIconSize
;
1221 if (m_supportsItemExpanding
) {
1222 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1224 firstColumnInc
+= option
.padding
;
1227 qreal x
= firstColumnInc
;
1228 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1230 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1231 QString text
= roleText(role
, values
);
1233 // Elide the text in case it does not fit into the available column-width
1234 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1235 const qreal roleWidth
= columnWidth(role
);
1236 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1238 const bool isTextRole
= (role
== "text");
1240 availableTextWidth
-= firstColumnInc
;
1243 if (requiredWidth
> availableTextWidth
) {
1244 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1245 requiredWidth
= m_customizedFontMetrics
.width(text
);
1248 TextInfo
* textInfo
= m_textInfo
.value(role
);
1249 textInfo
->staticText
.setText(text
);
1250 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1254 const qreal textWidth
= option
.extendedSelectionRegion
1255 ? size().width() - textInfo
->pos
.x()
1256 : requiredWidth
+ 2 * option
.padding
;
1257 m_textRect
= QRectF(textInfo
->pos
.x() - 2 * option
.padding
, 0,
1258 textWidth
+ option
.padding
, size().height());
1260 // The column after the name should always be aligned on the same x-position independent
1261 // from the expansion-level shown in the name column
1262 x
-= firstColumnInc
;
1263 } else if (isRoleRightAligned(role
)) {
1264 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1269 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1272 if (m_customTextColor
.isValid()) {
1273 c1
= m_customTextColor
;
1274 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1275 c1
= styleOption().palette
.highlightedText().color();
1277 c1
= styleOption().palette
.text().color();
1280 // For the color of the additional info the inactive text color
1281 // is not used as this might lead to unreadable text for some color schemes. Instead
1282 // the text color c1 is slightly mixed with the background color.
1283 const QColor c2
= styleOption().palette
.base().color();
1285 const int p2
= 100 - p1
;
1286 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1287 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1288 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1291 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1293 if (m_scaledPixmapSize
!= pixmap
.size()) {
1294 QPixmap scaledPixmap
= pixmap
;
1295 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
1296 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1298 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1299 painter
->setPen(Qt::blue
);
1300 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1303 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1307 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1309 const int siblingSize
= size().height();
1310 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1311 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1313 QStyleOption option
;
1314 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1315 bool isItemSibling
= true;
1317 const QBitArray siblings
= siblingsInformation();
1318 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1319 option
.rect
= siblingRect
;
1320 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1322 if (isItemSibling
) {
1323 option
.state
|= QStyle::State_Item
;
1324 if (m_isExpandable
) {
1325 option
.state
|= QStyle::State_Children
;
1327 if (data()["isExpanded"].toBool()) {
1328 option
.state
|= QStyle::State_Open
;
1330 isItemSibling
= false;
1333 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1335 siblingRect
.translate(-siblingRect
.width(), 0);
1339 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1341 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1346 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1347 if (m_layout
== DetailsLayout
) {
1348 rect
.setWidth(columnWidth(role
) - rect
.x());
1354 void KStandardItemListWidget::closeRoleEditor()
1356 disconnect(m_roleEditor
, SIGNAL(roleEditingCanceled(QByteArray
,QVariant
)),
1357 this, SLOT(slotRoleEditingCanceled(QByteArray
,QVariant
)));
1358 disconnect(m_roleEditor
, SIGNAL(roleEditingFinished(QByteArray
,QVariant
)),
1359 this, SLOT(slotRoleEditingFinished(QByteArray
,QVariant
)));
1361 if (m_roleEditor
->hasFocus()) {
1362 // If the editing was not ended by a FocusOut event, we have
1363 // to transfer the keyboard focus back to the KItemListContainer.
1364 scene()->views()[0]->parentWidget()->setFocus();
1367 if (m_oldRoleEditor
) {
1368 m_oldRoleEditor
->deleteLater();
1370 m_oldRoleEditor
= m_roleEditor
;
1371 m_roleEditor
->hide();
1375 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
)
1377 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(":") % ":" % QString::number(size
);
1380 if (!QPixmapCache::find(key
, pixmap
)) {
1381 const KIcon
icon(name
);
1384 if (size
<= KIconLoader::SizeSmall
) {
1385 requestedSize
= KIconLoader::SizeSmall
;
1386 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1387 requestedSize
= KIconLoader::SizeSmallMedium
;
1388 } else if (size
<= KIconLoader::SizeMedium
) {
1389 requestedSize
= KIconLoader::SizeMedium
;
1390 } else if (size
<= KIconLoader::SizeLarge
) {
1391 requestedSize
= KIconLoader::SizeLarge
;
1392 } else if (size
<= KIconLoader::SizeHuge
) {
1393 requestedSize
= KIconLoader::SizeHuge
;
1394 } else if (size
<= KIconLoader::SizeEnormous
) {
1395 requestedSize
= KIconLoader::SizeEnormous
;
1396 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1397 requestedSize
= KIconLoader::SizeEnormous
* 2;
1399 requestedSize
= size
;
1402 pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
1403 if (requestedSize
!= size
) {
1404 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1407 // Strangely KFileItem::overlays() returns empty string-values, so
1408 // we need to check first whether an overlay must be drawn at all.
1409 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1410 // assumes that an overlay will be drawn and has some additional
1412 foreach (const QString
& overlay
, overlays
) {
1413 if (!overlay
.isEmpty()) {
1414 // There is at least one overlay, draw all overlays above m_pixmap
1415 // and cancel the check
1416 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
);
1421 QPixmapCache::insert(key
, pixmap
);
1427 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1429 const qreal height
= option
.fontMetrics
.ascent();
1430 return QSizeF(height
* 5, height
);
1433 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1435 return option
.padding
* 6;
1438 #include "kstandarditemlistwidget.moc"