]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.cpp
Use QPixmapCache for KStandardItemListWidget::pixmapForIcon(const QString& name,...
[dolphin.git] / src / kitemviews / kstandarditemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "kstandarditemlistwidget.h"
21
22 #include "kfileitemlistview.h"
23 #include "kfileitemmodel.h"
24
25 #include <KIcon>
26 #include <KIconEffect>
27 #include <KIconLoader>
28 #include <KLocale>
29 #include <kratingpainter.h>
30 #include <KStringHandler>
31 #include <KDebug>
32
33 #include "private/kfileitemclipboard.h"
34 #include "private/kitemlistroleeditor.h"
35 #include "private/kpixmapmodifier.h"
36
37 #include <QFontMetricsF>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneResizeEvent>
40 #include <QGraphicsView>
41 #include <QPainter>
42 #include <QStyleOption>
43 #include <QTextLayout>
44 #include <QTextLine>
45 #include <QPixmapCache>
46
47 // #define KSTANDARDITEMLISTWIDGET_DEBUG
48
49 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
50 KItemListWidgetInformant()
51 {
52 }
53
54 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
55 {
56 }
57
58 QSizeF KStandardItemListWidgetInformant::itemSizeHint(int index, const KItemListView* view) const
59 {
60 const QHash<QByteArray, QVariant> values = view->model()->data(index);
61 const KItemListStyleOption& option = view->styleOption();
62 const int additionalRolesCount = qMax(view->visibleRoles().count() - 1, 0);
63
64 switch (static_cast<const KStandardItemListView*>(view)->itemLayout()) {
65 case KStandardItemListWidget::IconsLayout: {
66 const QString text = KStringHandler::preProcessWrap(values["text"].toString());
67
68 const qreal itemWidth = view->itemSize().width();
69 const qreal maxWidth = itemWidth - 2 * option.padding;
70 QTextLine line;
71
72 // Calculate the number of lines required for wrapping the name
73 QTextOption textOption(Qt::AlignHCenter);
74 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
75
76 qreal textHeight = 0;
77 QTextLayout layout(text, option.font);
78 layout.setTextOption(textOption);
79 layout.beginLayout();
80 while ((line = layout.createLine()).isValid()) {
81 line.setLineWidth(maxWidth);
82 line.naturalTextWidth();
83 textHeight += line.height();
84 }
85 layout.endLayout();
86
87 // Add one line for each additional information
88 textHeight += additionalRolesCount * option.fontMetrics.lineSpacing();
89
90 const qreal maxTextHeight = option.maxTextSize.height();
91 if (maxTextHeight > 0 && textHeight > maxTextHeight) {
92 textHeight = maxTextHeight;
93 }
94
95 return QSizeF(itemWidth, textHeight + option.iconSize + option.padding * 3);
96 }
97
98 case KStandardItemListWidget::CompactLayout: {
99 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
100 // to show all roles without horizontal clipping.
101 qreal maximumRequiredWidth = 0.0;
102
103 foreach (const QByteArray& role, view->visibleRoles()) {
104 const QString text = roleText(role, values);
105 const qreal requiredWidth = option.fontMetrics.width(text);
106 maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
107 }
108
109 qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
110 const qreal maxWidth = option.maxTextSize.width();
111 if (maxWidth > 0 && width > maxWidth) {
112 width = maxWidth;
113 }
114 const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.lineSpacing());
115 return QSizeF(width, height);
116 }
117
118 case KStandardItemListWidget::DetailsLayout: {
119 const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height());
120 return QSizeF(-1, height);
121 }
122
123 default:
124 Q_ASSERT(false);
125 break;
126 }
127
128 return QSize();
129 }
130
131 qreal KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray& role,
132 int index,
133 const KItemListView* view) const
134 {
135 const QHash<QByteArray, QVariant> values = view->model()->data(index);
136 const KItemListStyleOption& option = view->styleOption();
137
138 const QString text = roleText(role, values);
139 qreal width = KStandardItemListWidget::columnPadding(option);
140
141 if (role == "rating") {
142 width += KStandardItemListWidget::preferredRatingSize(option).width();
143 } else {
144 width += option.fontMetrics.width(text);
145
146 if (role == "text") {
147 if (view->supportsItemExpanding()) {
148 // Increase the width by the expansion-toggle and the current expansion level
149 const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
150 const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height());
151 width += (expandedParentsCount + 1) * height;
152 }
153
154 // Increase the width by the required space for the icon
155 width += option.padding * 2 + option.iconSize;
156 }
157 }
158
159 return width;
160 }
161
162 QString KStandardItemListWidgetInformant::roleText(const QByteArray& role,
163 const QHash<QByteArray, QVariant>& values) const
164 {
165 if (role == "rating") {
166 // Always use an empty text, as the rating is shown by the image m_rating.
167 return QString();
168 }
169 return values.value(role).toString();
170 }
171
172 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
173 KItemListWidget(informant, parent),
174 m_isCut(false),
175 m_isHidden(false),
176 m_customizedFont(),
177 m_customizedFontMetrics(m_customizedFont),
178 m_isExpandable(false),
179 m_supportsItemExpanding(false),
180 m_dirtyLayout(true),
181 m_dirtyContent(true),
182 m_dirtyContentRoles(),
183 m_layout(IconsLayout),
184 m_pixmapPos(),
185 m_pixmap(),
186 m_scaledPixmapSize(),
187 m_iconRect(),
188 m_hoverPixmap(),
189 m_textInfo(),
190 m_textRect(),
191 m_sortedVisibleRoles(),
192 m_expansionArea(),
193 m_customTextColor(),
194 m_additionalInfoTextColor(),
195 m_overlay(),
196 m_rating(),
197 m_roleEditor(0)
198 {
199 }
200
201 KStandardItemListWidget::~KStandardItemListWidget()
202 {
203 qDeleteAll(m_textInfo);
204 m_textInfo.clear();
205
206 delete m_roleEditor;
207 }
208
209 void KStandardItemListWidget::setLayout(Layout layout)
210 {
211 if (m_layout != layout) {
212 m_layout = layout;
213 m_dirtyLayout = true;
214 updateAdditionalInfoTextColor();
215 update();
216 }
217 }
218
219 KStandardItemListWidget::Layout KStandardItemListWidget::layout() const
220 {
221 return m_layout;
222 }
223
224 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding)
225 {
226 if (m_supportsItemExpanding != supportsItemExpanding) {
227 m_supportsItemExpanding = supportsItemExpanding;
228 m_dirtyLayout = true;
229 update();
230 }
231 }
232
233 bool KStandardItemListWidget::supportsItemExpanding() const
234 {
235 return m_supportsItemExpanding;
236 }
237
238 void KStandardItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
239 {
240 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
241
242 KItemListWidget::paint(painter, option, widget);
243
244 if (!m_expansionArea.isEmpty()) {
245 drawSiblingsInformation(painter);
246 }
247
248 const KItemListStyleOption& itemListStyleOption = styleOption();
249 if (isHovered()) {
250 // Blend the unhovered and hovered pixmap if the hovering
251 // animation is ongoing
252 if (hoverOpacity() < 1.0) {
253 drawPixmap(painter, m_pixmap);
254 }
255
256 const qreal opacity = painter->opacity();
257 painter->setOpacity(hoverOpacity() * opacity);
258 drawPixmap(painter, m_hoverPixmap);
259 painter->setOpacity(opacity);
260 } else {
261 drawPixmap(painter, m_pixmap);
262 }
263
264 painter->setFont(m_customizedFont);
265 painter->setPen(m_isHidden ? m_additionalInfoTextColor : textColor());
266 const TextInfo* textInfo = m_textInfo.value("text");
267
268 if (!textInfo) {
269 // It seems that we can end up here even if m_textInfo does not contain
270 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
271 // this can only happen if the index is negative. This can happen when
272 // the item is about to be removed, see KItemListView::slotItemsRemoved().
273 // TODO: try to reproduce the crash and find a better fix.
274 return;
275 }
276
277 painter->drawStaticText(textInfo->pos, textInfo->staticText);
278
279 bool clipAdditionalInfoBounds = false;
280 if (m_supportsItemExpanding) {
281 // Prevent a possible overlapping of the additional-information texts
282 // with the icon. This can happen if the user has minimized the width
283 // of the name-column to a very small value.
284 const qreal minX = m_pixmapPos.x() + m_pixmap.width() + 4 * itemListStyleOption.padding;
285 if (textInfo->pos.x() + columnWidth("text") > minX) {
286 clipAdditionalInfoBounds = true;
287 painter->save();
288 painter->setClipRect(minX, 0, size().width() - minX, size().height(), Qt::IntersectClip);
289 }
290 }
291
292 painter->setPen(m_additionalInfoTextColor);
293 painter->setFont(m_customizedFont);
294
295 for (int i = 1; i < m_sortedVisibleRoles.count(); ++i) {
296 const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles[i]);
297 painter->drawStaticText(textInfo->pos, textInfo->staticText);
298 }
299
300 if (!m_rating.isNull()) {
301 const TextInfo* ratingTextInfo = m_textInfo.value("rating");
302 QPointF pos = ratingTextInfo->pos;
303 const Qt::Alignment align = ratingTextInfo->staticText.textOption().alignment();
304 if (align & Qt::AlignHCenter) {
305 pos.rx() += (size().width() - m_rating.width()) / 2 - 2;
306 }
307 painter->drawPixmap(pos, m_rating);
308 }
309
310 if (clipAdditionalInfoBounds) {
311 painter->restore();
312 }
313
314 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
315 painter->setBrush(Qt::NoBrush);
316 painter->setPen(Qt::green);
317 painter->drawRect(m_iconRect);
318
319 painter->setPen(Qt::red);
320 painter->drawText(QPointF(0, m_customizedFontMetrics.height()), QString::number(index()));
321 painter->drawRect(rect());
322 #endif
323 }
324
325 QRectF KStandardItemListWidget::iconRect() const
326 {
327 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
328 return m_iconRect;
329 }
330
331 QRectF KStandardItemListWidget::textRect() const
332 {
333 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
334 return m_textRect;
335 }
336
337 QRectF KStandardItemListWidget::textFocusRect() const
338 {
339 // In the compact- and details-layout a larger textRect() is returned to be aligned
340 // with the iconRect(). This is useful to have a larger selection/hover-area
341 // when having a quite large icon size but only one line of text. Still the
342 // focus rectangle should be shown as narrow as possible around the text.
343
344 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
345
346 switch (m_layout) {
347 case CompactLayout: {
348 QRectF rect = m_textRect;
349 const TextInfo* topText = m_textInfo.value(m_sortedVisibleRoles.first());
350 const TextInfo* bottomText = m_textInfo.value(m_sortedVisibleRoles.last());
351 rect.setTop(topText->pos.y());
352 rect.setBottom(bottomText->pos.y() + bottomText->staticText.size().height());
353 return rect;
354 }
355
356 case DetailsLayout: {
357 QRectF rect = m_textRect;
358 const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles.first());
359 rect.setTop(textInfo->pos.y());
360 rect.setBottom(textInfo->pos.y() + textInfo->staticText.size().height());
361
362 const KItemListStyleOption& option = styleOption();
363 if (option.extendedSelectionRegion) {
364 const QString text = textInfo->staticText.text();
365 rect.setWidth(m_customizedFontMetrics.width(text) + 2 * option.padding);
366 }
367
368 return rect;
369 }
370
371 default:
372 break;
373 }
374
375 return m_textRect;
376 }
377
378 QRectF KStandardItemListWidget::expansionToggleRect() const
379 {
380 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
381 return m_isExpandable ? m_expansionArea : QRectF();
382 }
383
384 QRectF KStandardItemListWidget::selectionToggleRect() const
385 {
386 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
387
388 const int iconHeight = styleOption().iconSize;
389
390 int toggleSize = KIconLoader::SizeSmall;
391 if (iconHeight >= KIconLoader::SizeEnormous) {
392 toggleSize = KIconLoader::SizeMedium;
393 } else if (iconHeight >= KIconLoader::SizeLarge) {
394 toggleSize = KIconLoader::SizeSmallMedium;
395 }
396
397 QPointF pos = iconRect().topLeft();
398
399 // If the selection toggle has a very small distance to the
400 // widget borders, the size of the selection toggle will get
401 // increased to prevent an accidental clicking of the item
402 // when trying to hit the toggle.
403 const int widgetHeight = size().height();
404 const int widgetWidth = size().width();
405 const int minMargin = 2;
406
407 if (toggleSize + minMargin * 2 >= widgetHeight) {
408 pos.rx() -= (widgetHeight - toggleSize) / 2;
409 toggleSize = widgetHeight;
410 pos.setY(0);
411 }
412 if (toggleSize + minMargin * 2 >= widgetWidth) {
413 pos.ry() -= (widgetWidth - toggleSize) / 2;
414 toggleSize = widgetWidth;
415 pos.setX(0);
416 }
417
418 return QRectF(pos, QSizeF(toggleSize, toggleSize));
419 }
420
421 QPixmap KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
422 QWidget* widget)
423 {
424 QPixmap pixmap = KItemListWidget::createDragPixmap(option, widget);
425 if (m_layout != DetailsLayout) {
426 return pixmap;
427 }
428
429 // Only return the content of the text-column as pixmap
430 const int leftClip = m_pixmapPos.x();
431
432 const TextInfo* textInfo = m_textInfo.value("text");
433 const int rightClip = textInfo->pos.x() +
434 textInfo->staticText.size().width() +
435 2 * styleOption().padding;
436
437 QPixmap clippedPixmap(rightClip - leftClip + 1, pixmap.height());
438 clippedPixmap.fill(Qt::transparent);
439
440 QPainter painter(&clippedPixmap);
441 painter.drawPixmap(-leftClip, 0, pixmap);
442
443 return clippedPixmap;
444 }
445
446
447 KItemListWidgetInformant* KStandardItemListWidget::createInformant()
448 {
449 return new KStandardItemListWidgetInformant();
450 }
451
452 void KStandardItemListWidget::invalidateCache()
453 {
454 m_dirtyLayout = true;
455 m_dirtyContent = true;
456 }
457
458 void KStandardItemListWidget::refreshCache()
459 {
460 }
461
462 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray& role) const
463 {
464 Q_UNUSED(role);
465 return false;
466 }
467
468 bool KStandardItemListWidget::isHidden() const
469 {
470 return false;
471 }
472
473 QFont KStandardItemListWidget::customizedFont(const QFont& baseFont) const
474 {
475 return baseFont;
476 }
477
478 QPalette::ColorRole KStandardItemListWidget::normalTextColorRole() const
479 {
480 return QPalette::Text;
481 }
482
483 void KStandardItemListWidget::setTextColor(const QColor& color)
484 {
485 if (color != m_customTextColor) {
486 m_customTextColor = color;
487 updateAdditionalInfoTextColor();
488 update();
489 }
490 }
491
492 QColor KStandardItemListWidget::textColor() const
493 {
494 if (m_customTextColor.isValid() && !isSelected()) {
495 return m_customTextColor;
496 }
497
498 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
499 const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : normalTextColorRole();
500 return styleOption().palette.color(group, role);
501 }
502
503 void KStandardItemListWidget::setOverlay(const QPixmap& overlay)
504 {
505 m_overlay = overlay;
506 m_dirtyContent = true;
507 update();
508 }
509
510 QPixmap KStandardItemListWidget::overlay() const
511 {
512 return m_overlay;
513 }
514
515
516 QString KStandardItemListWidget::roleText(const QByteArray& role,
517 const QHash<QByteArray, QVariant>& values) const
518 {
519 return static_cast<const KStandardItemListWidgetInformant*>(informant())->roleText(role, values);
520 }
521
522 void KStandardItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
523 const QSet<QByteArray>& roles)
524 {
525 Q_UNUSED(current);
526
527 m_dirtyContent = true;
528
529 QSet<QByteArray> dirtyRoles;
530 if (roles.isEmpty()) {
531 dirtyRoles = visibleRoles().toSet();
532 } else {
533 dirtyRoles = roles;
534 }
535
536 // The icon-state might depend from other roles and hence is
537 // marked as dirty whenever a role has been changed
538 dirtyRoles.insert("iconPixmap");
539 dirtyRoles.insert("iconName");
540
541 QSetIterator<QByteArray> it(dirtyRoles);
542 while (it.hasNext()) {
543 const QByteArray& role = it.next();
544 m_dirtyContentRoles.insert(role);
545 }
546 }
547
548 void KStandardItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
549 const QList<QByteArray>& previous)
550 {
551 Q_UNUSED(previous);
552 m_sortedVisibleRoles = current;
553 m_dirtyLayout = true;
554 }
555
556 void KStandardItemListWidget::columnWidthChanged(const QByteArray& role,
557 qreal current,
558 qreal previous)
559 {
560 Q_UNUSED(role);
561 Q_UNUSED(current);
562 Q_UNUSED(previous);
563 m_dirtyLayout = true;
564 }
565
566 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
567 const KItemListStyleOption& previous)
568 {
569 Q_UNUSED(current);
570 Q_UNUSED(previous);
571 updateAdditionalInfoTextColor();
572 m_dirtyLayout = true;
573 }
574
575 void KStandardItemListWidget::hoveredChanged(bool hovered)
576 {
577 Q_UNUSED(hovered);
578 m_dirtyLayout = true;
579 }
580
581 void KStandardItemListWidget::selectedChanged(bool selected)
582 {
583 Q_UNUSED(selected);
584 updateAdditionalInfoTextColor();
585 m_dirtyContent = true;
586 }
587
588 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
589 {
590 Q_UNUSED(current);
591 Q_UNUSED(previous);
592 m_dirtyLayout = true;
593 }
594
595 int KStandardItemListWidget::selectionLength(const QString& text) const
596 {
597 return text.length();
598 }
599
600 void KStandardItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
601 {
602 Q_UNUSED(previous);
603
604 QGraphicsView* parent = scene()->views()[0];
605 if (current.isEmpty() || !parent || current != "text") {
606 if (m_roleEditor) {
607 emit roleEditingCanceled(index(), current, data().value(current));
608
609 disconnect(m_roleEditor, SIGNAL(roleEditingCanceled(int,QByteArray,QVariant)),
610 this, SLOT(slotRoleEditingCanceled(int,QByteArray,QVariant)));
611 disconnect(m_roleEditor, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
612 this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
613 m_roleEditor->deleteLater();
614 m_roleEditor = 0;
615 }
616 return;
617 }
618
619 Q_ASSERT(!m_roleEditor);
620
621 const TextInfo* textInfo = m_textInfo.value("text");
622
623 m_roleEditor = new KItemListRoleEditor(parent);
624 m_roleEditor->setIndex(index());
625 m_roleEditor->setRole(current);
626 m_roleEditor->setFont(styleOption().font);
627
628 const QString text = data().value(current).toString();
629 m_roleEditor->setPlainText(text);
630
631 QTextOption textOption = textInfo->staticText.textOption();
632 m_roleEditor->document()->setDefaultTextOption(textOption);
633
634 const int textSelectionLength = selectionLength(text);
635
636 if (textSelectionLength > 0) {
637 QTextCursor cursor = m_roleEditor->textCursor();
638 cursor.movePosition(QTextCursor::StartOfBlock);
639 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, textSelectionLength);
640 m_roleEditor->setTextCursor(cursor);
641 }
642
643 connect(m_roleEditor, SIGNAL(roleEditingCanceled(int,QByteArray,QVariant)),
644 this, SLOT(slotRoleEditingCanceled(int,QByteArray,QVariant)));
645 connect(m_roleEditor, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
646 this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
647
648 // Adjust the geometry of the editor
649 QRectF rect = roleEditingRect(current);
650 const int frameWidth = m_roleEditor->frameWidth();
651 rect.adjust(-frameWidth, -frameWidth, frameWidth, frameWidth);
652 rect.translate(pos());
653 if (rect.right() > parent->width()) {
654 rect.setWidth(parent->width() - rect.left());
655 }
656 m_roleEditor->setGeometry(rect.toRect());
657 m_roleEditor->show();
658 m_roleEditor->setFocus();
659 }
660
661 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
662 {
663 if (m_roleEditor) {
664 setEditedRole(QByteArray());
665 Q_ASSERT(!m_roleEditor);
666 }
667
668 KItemListWidget::resizeEvent(event);
669
670 m_dirtyLayout = true;
671 }
672
673 void KStandardItemListWidget::showEvent(QShowEvent* event)
674 {
675 KItemListWidget::showEvent(event);
676
677 // Listen to changes of the clipboard to mark the item as cut/uncut
678 KFileItemClipboard* clipboard = KFileItemClipboard::instance();
679
680 const KUrl itemUrl = data().value("url").value<KUrl>();
681 m_isCut = clipboard->isCut(itemUrl);
682
683 connect(clipboard, SIGNAL(cutItemsChanged()),
684 this, SLOT(slotCutItemsChanged()));
685 }
686
687 void KStandardItemListWidget::hideEvent(QHideEvent* event)
688 {
689 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
690 this, SLOT(slotCutItemsChanged()));
691
692 KItemListWidget::hideEvent(event);
693 }
694
695 void KStandardItemListWidget::slotCutItemsChanged()
696 {
697 const KUrl itemUrl = data().value("url").value<KUrl>();
698 const bool isCut = KFileItemClipboard::instance()->isCut(itemUrl);
699 if (m_isCut != isCut) {
700 m_isCut = isCut;
701 m_pixmap = QPixmap();
702 m_dirtyContent = true;
703 update();
704 }
705 }
706
707 void KStandardItemListWidget::slotRoleEditingCanceled(int index,
708 const QByteArray& role,
709 const QVariant& value)
710 {
711 closeRoleEditor();
712 emit roleEditingCanceled(index, role, value);
713 setEditedRole(QByteArray());
714 }
715
716 void KStandardItemListWidget::slotRoleEditingFinished(int index,
717 const QByteArray& role,
718 const QVariant& value)
719 {
720 closeRoleEditor();
721 emit roleEditingFinished(index, role, value);
722 setEditedRole(QByteArray());
723 }
724
725 void KStandardItemListWidget::triggerCacheRefreshing()
726 {
727 if ((!m_dirtyContent && !m_dirtyLayout) || index() < 0) {
728 return;
729 }
730
731 refreshCache();
732
733 const QHash<QByteArray, QVariant> values = data();
734 m_isExpandable = m_supportsItemExpanding && values["isExpandable"].toBool();
735 m_isHidden = isHidden();
736 m_customizedFont = customizedFont(styleOption().font);
737 m_customizedFontMetrics = QFontMetrics(m_customizedFont);
738
739 updateExpansionArea();
740 updateTextsCache();
741 updatePixmapCache();
742
743 m_dirtyLayout = false;
744 m_dirtyContent = false;
745 m_dirtyContentRoles.clear();
746 }
747
748 void KStandardItemListWidget::updateExpansionArea()
749 {
750 if (m_supportsItemExpanding) {
751 const QHash<QByteArray, QVariant> values = data();
752 Q_ASSERT(values.contains("expandedParentsCount"));
753 const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
754 if (expandedParentsCount >= 0) {
755 const qreal widgetHeight = size().height();
756 const qreal inc = (widgetHeight - KIconLoader::SizeSmall) / 2;
757 const qreal x = expandedParentsCount * widgetHeight + inc;
758 const qreal y = inc;
759 m_expansionArea = QRectF(x, y, KIconLoader::SizeSmall, KIconLoader::SizeSmall);
760 return;
761 }
762 }
763
764 m_expansionArea = QRectF();
765 }
766
767 void KStandardItemListWidget::updatePixmapCache()
768 {
769 // Precondition: Requires already updated m_textPos values to calculate
770 // the remaining height when the alignment is vertical.
771
772 const QSizeF widgetSize = size();
773 const bool iconOnTop = (m_layout == IconsLayout);
774 const KItemListStyleOption& option = styleOption();
775 const qreal padding = option.padding;
776
777 const int maxIconWidth = iconOnTop ? widgetSize.width() - 2 * padding : option.iconSize;
778 const int maxIconHeight = option.iconSize;
779
780 const QHash<QByteArray, QVariant> values = data();
781
782 bool updatePixmap = (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight);
783 if (!updatePixmap && m_dirtyContent) {
784 updatePixmap = m_dirtyContentRoles.isEmpty()
785 || m_dirtyContentRoles.contains("iconPixmap")
786 || m_dirtyContentRoles.contains("iconName")
787 || m_dirtyContentRoles.contains("iconOverlays");
788 }
789
790 if (updatePixmap) {
791 m_pixmap = values["iconPixmap"].value<QPixmap>();
792 if (m_pixmap.isNull()) {
793 // Use the icon that fits to the MIME-type
794 QString iconName = values["iconName"].toString();
795 if (iconName.isEmpty()) {
796 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
797 // use a generic icon as fallback
798 iconName = QLatin1String("unknown");
799 }
800 m_pixmap = pixmapForIcon(iconName, maxIconHeight);
801 } else if (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight) {
802 // A custom pixmap has been applied. Assure that the pixmap
803 // is scaled to the maximum available size.
804 KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight));
805 }
806
807 const QStringList overlays = values["iconOverlays"].toStringList();
808
809 // Strangely KFileItem::overlays() returns empty string-values, so
810 // we need to check first whether an overlay must be drawn at all.
811 // It is more efficient to do it here, as KIconLoader::drawOverlays()
812 // assumes that an overlay will be drawn and has some additional
813 // setup time.
814 foreach (const QString& overlay, overlays) {
815 if (!overlay.isEmpty()) {
816 // There is at least one overlay, draw all overlays above m_pixmap
817 // and cancel the check
818 KIconLoader::global()->drawOverlays(overlays, m_pixmap, KIconLoader::Desktop);
819 break;
820 }
821 }
822
823 if (m_isCut) {
824 KIconEffect* effect = KIconLoader::global()->iconEffect();
825 m_pixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
826 }
827
828 if (m_isHidden) {
829 KIconEffect::semiTransparent(m_pixmap);
830 }
831
832 if (isSelected()) {
833 const QColor color = palette().brush(QPalette::Normal, QPalette::Highlight).color();
834 QImage image = m_pixmap.toImage();
835 KIconEffect::colorize(image, color, 1.0f);
836 m_pixmap = QPixmap::fromImage(image);
837 }
838 }
839
840 if (!m_overlay.isNull()) {
841 QPainter painter(&m_pixmap);
842 painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
843 }
844
845 int scaledIconSize = 0;
846 if (iconOnTop) {
847 const TextInfo* textInfo = m_textInfo.value("text");
848 scaledIconSize = static_cast<int>(textInfo->pos.y() - 2 * padding);
849 } else {
850 const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
851 const qreal requiredTextHeight = textRowsCount * m_customizedFontMetrics.height();
852 scaledIconSize = (requiredTextHeight < maxIconHeight) ?
853 widgetSize.height() - 2 * padding : maxIconHeight;
854 }
855
856 const int maxScaledIconWidth = iconOnTop ? widgetSize.width() - 2 * padding : scaledIconSize;
857 const int maxScaledIconHeight = scaledIconSize;
858
859 m_scaledPixmapSize = m_pixmap.size();
860 m_scaledPixmapSize.scale(maxScaledIconWidth, maxScaledIconHeight, Qt::KeepAspectRatio);
861
862 if (iconOnTop) {
863 // Center horizontally and align on bottom within the icon-area
864 m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
865 m_pixmapPos.setY(padding + scaledIconSize - m_scaledPixmapSize.height());
866 } else {
867 // Center horizontally and vertically within the icon-area
868 const TextInfo* textInfo = m_textInfo.value("text");
869 m_pixmapPos.setX(textInfo->pos.x() - 2 * padding
870 - (scaledIconSize + m_scaledPixmapSize.width()) / 2);
871 m_pixmapPos.setY(padding
872 + (scaledIconSize - m_scaledPixmapSize.height()) / 2);
873 }
874
875 m_iconRect = QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize));
876
877 // Prepare the pixmap that is used when the item gets hovered
878 if (isHovered()) {
879 m_hoverPixmap = m_pixmap;
880 KIconEffect* effect = KIconLoader::global()->iconEffect();
881 // In the KIconLoader terminology, active = hover.
882 if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
883 m_hoverPixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
884 } else {
885 m_hoverPixmap = m_pixmap;
886 }
887 } else if (hoverOpacity() <= 0.0) {
888 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
889 m_hoverPixmap = QPixmap();
890 }
891 }
892
893 void KStandardItemListWidget::updateTextsCache()
894 {
895 QTextOption textOption;
896 switch (m_layout) {
897 case IconsLayout:
898 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
899 textOption.setAlignment(Qt::AlignHCenter);
900 break;
901 case CompactLayout:
902 case DetailsLayout:
903 textOption.setAlignment(Qt::AlignLeft);
904 textOption.setWrapMode(QTextOption::NoWrap);
905 break;
906 default:
907 Q_ASSERT(false);
908 break;
909 }
910
911 qDeleteAll(m_textInfo);
912 m_textInfo.clear();
913 for (int i = 0; i < m_sortedVisibleRoles.count(); ++i) {
914 TextInfo* textInfo = new TextInfo();
915 textInfo->staticText.setTextFormat(Qt::PlainText);
916 textInfo->staticText.setPerformanceHint(QStaticText::AggressiveCaching);
917 textInfo->staticText.setTextOption(textOption);
918 m_textInfo.insert(m_sortedVisibleRoles[i], textInfo);
919 }
920
921 switch (m_layout) {
922 case IconsLayout: updateIconsLayoutTextCache(); break;
923 case CompactLayout: updateCompactLayoutTextCache(); break;
924 case DetailsLayout: updateDetailsLayoutTextCache(); break;
925 default: Q_ASSERT(false); break;
926 }
927
928 const TextInfo* ratingTextInfo = m_textInfo.value("rating");
929 if (ratingTextInfo) {
930 // The text of the rating-role has been set to empty to get
931 // replaced by a rating-image showing the rating as stars.
932 const KItemListStyleOption& option = styleOption();
933 QSizeF ratingSize = preferredRatingSize(option);
934
935 const qreal availableWidth = (m_layout == DetailsLayout)
936 ? columnWidth("rating") - columnPadding(option)
937 : size().width();
938 if (ratingSize.width() > availableWidth) {
939 ratingSize.rwidth() = availableWidth;
940 }
941 m_rating = QPixmap(ratingSize.toSize());
942 m_rating.fill(Qt::transparent);
943
944 QPainter painter(&m_rating);
945 const QRect rect(0, 0, m_rating.width(), m_rating.height());
946 const int rating = data().value("rating").toInt();
947 KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating);
948 } else if (!m_rating.isNull()) {
949 m_rating = QPixmap();
950 }
951 }
952
953 void KStandardItemListWidget::updateIconsLayoutTextCache()
954 {
955 // +------+
956 // | Icon |
957 // +------+
958 //
959 // Name role that
960 // might get wrapped above
961 // several lines.
962 // Additional role 1
963 // Additional role 2
964
965 const QHash<QByteArray, QVariant> values = data();
966
967 const KItemListStyleOption& option = styleOption();
968 const qreal padding = option.padding;
969 const qreal maxWidth = size().width() - 2 * padding;
970 const qreal widgetHeight = size().height();
971 const qreal lineSpacing = m_customizedFontMetrics.lineSpacing();
972
973 // Initialize properties for the "text" role. It will be used as anchor
974 // for initializing the position of the other roles.
975 TextInfo* nameTextInfo = m_textInfo.value("text");
976 const QString nameText = KStringHandler::preProcessWrap(values["text"].toString());
977 nameTextInfo->staticText.setText(nameText);
978
979 // Calculate the number of lines required for the name and the required width
980 qreal nameWidth = 0;
981 qreal nameHeight = 0;
982 QTextLine line;
983
984 const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
985 const int maxNameLines = (option.maxTextSize.height() / int(lineSpacing)) - additionalRolesCount;
986
987 QTextLayout layout(nameTextInfo->staticText.text(), m_customizedFont);
988 layout.setTextOption(nameTextInfo->staticText.textOption());
989 layout.beginLayout();
990 int nameLineIndex = 0;
991 while ((line = layout.createLine()).isValid()) {
992 line.setLineWidth(maxWidth);
993 nameWidth = qMax(nameWidth, line.naturalTextWidth());
994 nameHeight += line.height();
995
996 ++nameLineIndex;
997 if (nameLineIndex == maxNameLines) {
998 // The maximum number of textlines has been reached. If this is
999 // the case provide an elided text if necessary.
1000 const int textLength = line.textStart() + line.textLength();
1001 if (textLength < nameText.length()) {
1002 // Elide the last line of the text
1003 QString lastTextLine = nameText.mid(line.textStart(), line.textLength());
1004 lastTextLine = m_customizedFontMetrics.elidedText(lastTextLine,
1005 Qt::ElideRight,
1006 line.naturalTextWidth() - 1);
1007 const QString elidedText = nameText.left(line.textStart()) + lastTextLine;
1008 nameTextInfo->staticText.setText(elidedText);
1009 }
1010 break;
1011 }
1012 }
1013 layout.endLayout();
1014
1015 // Use one line for each additional information
1016 nameTextInfo->staticText.setTextWidth(maxWidth);
1017 nameTextInfo->pos = QPointF(padding, widgetHeight -
1018 nameHeight -
1019 additionalRolesCount * lineSpacing -
1020 padding);
1021 m_textRect = QRectF(padding + (maxWidth - nameWidth) / 2,
1022 nameTextInfo->pos.y(),
1023 nameWidth,
1024 nameHeight);
1025
1026 // Calculate the position for each additional information
1027 qreal y = nameTextInfo->pos.y() + nameHeight;
1028 foreach (const QByteArray& role, m_sortedVisibleRoles) {
1029 if (role == "text") {
1030 continue;
1031 }
1032
1033 const QString text = roleText(role, values);
1034 TextInfo* textInfo = m_textInfo.value(role);
1035 textInfo->staticText.setText(text);
1036
1037 qreal requiredWidth = 0;
1038
1039 QTextLayout layout(text, m_customizedFont);
1040 QTextOption textOption;
1041 textOption.setWrapMode(QTextOption::NoWrap);
1042 layout.setTextOption(textOption);
1043
1044 layout.beginLayout();
1045 QTextLine textLine = layout.createLine();
1046 if (textLine.isValid()) {
1047 textLine.setLineWidth(maxWidth);
1048 requiredWidth = textLine.naturalTextWidth();
1049 if (requiredWidth > maxWidth) {
1050 const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
1051 textInfo->staticText.setText(elidedText);
1052 requiredWidth = m_customizedFontMetrics.width(elidedText);
1053 } else if (role == "rating") {
1054 // Use the width of the rating pixmap, because the rating text is empty.
1055 requiredWidth = m_rating.width();
1056 }
1057 }
1058 layout.endLayout();
1059
1060 textInfo->pos = QPointF(padding, y);
1061 textInfo->staticText.setTextWidth(maxWidth);
1062
1063 const QRectF textRect(padding + (maxWidth - requiredWidth) / 2, y, requiredWidth, lineSpacing);
1064 m_textRect |= textRect;
1065
1066 y += lineSpacing;
1067 }
1068
1069 // Add a padding to the text rectangle
1070 m_textRect.adjust(-padding, -padding, padding, padding);
1071 }
1072
1073 void KStandardItemListWidget::updateCompactLayoutTextCache()
1074 {
1075 // +------+ Name role
1076 // | Icon | Additional role 1
1077 // +------+ Additional role 2
1078
1079 const QHash<QByteArray, QVariant> values = data();
1080
1081 const KItemListStyleOption& option = styleOption();
1082 const qreal widgetHeight = size().height();
1083 const qreal lineSpacing = m_customizedFontMetrics.lineSpacing();
1084 const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * lineSpacing;
1085 const int scaledIconSize = (textLinesHeight < option.iconSize) ? widgetHeight - 2 * option.padding : option.iconSize;
1086
1087 qreal maximumRequiredTextWidth = 0;
1088 const qreal x = option.padding * 3 + scaledIconSize;
1089 qreal y = qRound((widgetHeight - textLinesHeight) / 2);
1090 const qreal maxWidth = size().width() - x - option.padding;
1091 foreach (const QByteArray& role, m_sortedVisibleRoles) {
1092 const QString text = roleText(role, values);
1093 TextInfo* textInfo = m_textInfo.value(role);
1094 textInfo->staticText.setText(text);
1095
1096 qreal requiredWidth = m_customizedFontMetrics.width(text);
1097 if (requiredWidth > maxWidth) {
1098 requiredWidth = maxWidth;
1099 const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
1100 textInfo->staticText.setText(elidedText);
1101 }
1102
1103 textInfo->pos = QPointF(x, y);
1104 textInfo->staticText.setTextWidth(maxWidth);
1105
1106 maximumRequiredTextWidth = qMax(maximumRequiredTextWidth, requiredWidth);
1107
1108 y += lineSpacing;
1109 }
1110
1111 m_textRect = QRectF(x - option.padding, 0, maximumRequiredTextWidth + 2 * option.padding, widgetHeight);
1112 }
1113
1114 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1115 {
1116 // Precondition: Requires already updated m_expansionArea
1117 // to determine the left position.
1118
1119 // +------+
1120 // | Icon | Name role Additional role 1 Additional role 2
1121 // +------+
1122 m_textRect = QRectF();
1123
1124 const KItemListStyleOption& option = styleOption();
1125 const QHash<QByteArray, QVariant> values = data();
1126
1127 const qreal widgetHeight = size().height();
1128 const int scaledIconSize = widgetHeight - 2 * option.padding;
1129 const int fontHeight = m_customizedFontMetrics.height();
1130
1131 const qreal columnWidthInc = columnPadding(option);
1132 qreal firstColumnInc = scaledIconSize;
1133 if (m_supportsItemExpanding) {
1134 firstColumnInc += (m_expansionArea.left() + m_expansionArea.right() + widgetHeight) / 2;
1135 } else {
1136 firstColumnInc += option.padding;
1137 }
1138
1139 qreal x = firstColumnInc;
1140 const qreal y = qMax(qreal(option.padding), (widgetHeight - fontHeight) / 2);
1141
1142 foreach (const QByteArray& role, m_sortedVisibleRoles) {
1143 QString text = roleText(role, values);
1144
1145 // Elide the text in case it does not fit into the available column-width
1146 qreal requiredWidth = m_customizedFontMetrics.width(text);
1147 const qreal roleWidth = columnWidth(role);
1148 qreal availableTextWidth = roleWidth - columnWidthInc;
1149
1150 const bool isTextRole = (role == "text");
1151 if (isTextRole) {
1152 availableTextWidth -= firstColumnInc;
1153 }
1154
1155 if (requiredWidth > availableTextWidth) {
1156 text = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, availableTextWidth);
1157 requiredWidth = m_customizedFontMetrics.width(text);
1158 }
1159
1160 TextInfo* textInfo = m_textInfo.value(role);
1161 textInfo->staticText.setText(text);
1162 textInfo->pos = QPointF(x + columnWidthInc / 2, y);
1163 x += roleWidth;
1164
1165 if (isTextRole) {
1166 const qreal textWidth = option.extendedSelectionRegion
1167 ? size().width() - textInfo->pos.x()
1168 : requiredWidth + 2 * option.padding;
1169 m_textRect = QRectF(textInfo->pos.x() - option.padding, 0,
1170 textWidth, size().height());
1171
1172 // The column after the name should always be aligned on the same x-position independent
1173 // from the expansion-level shown in the name column
1174 x -= firstColumnInc;
1175 } else if (isRoleRightAligned(role)) {
1176 textInfo->pos.rx() += roleWidth - requiredWidth - columnWidthInc;
1177 }
1178 }
1179 }
1180
1181 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1182 {
1183 QColor c1;
1184 if (m_customTextColor.isValid()) {
1185 c1 = m_customTextColor;
1186 } else if (isSelected() && m_layout != DetailsLayout) {
1187 c1 = styleOption().palette.highlightedText().color();
1188 } else {
1189 c1 = styleOption().palette.text().color();
1190 }
1191
1192 // For the color of the additional info the inactive text color
1193 // is not used as this might lead to unreadable text for some color schemes. Instead
1194 // the text color c1 is slightly mixed with the background color.
1195 const QColor c2 = styleOption().palette.base().color();
1196 const int p1 = 70;
1197 const int p2 = 100 - p1;
1198 m_additionalInfoTextColor = QColor((c1.red() * p1 + c2.red() * p2) / 100,
1199 (c1.green() * p1 + c2.green() * p2) / 100,
1200 (c1.blue() * p1 + c2.blue() * p2) / 100);
1201 }
1202
1203 void KStandardItemListWidget::drawPixmap(QPainter* painter, const QPixmap& pixmap)
1204 {
1205 if (m_scaledPixmapSize != pixmap.size()) {
1206 QPixmap scaledPixmap = pixmap;
1207 KPixmapModifier::scale(scaledPixmap, m_scaledPixmapSize);
1208 painter->drawPixmap(m_pixmapPos, scaledPixmap);
1209
1210 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1211 painter->setPen(Qt::blue);
1212 painter->drawRect(QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize)));
1213 #endif
1214 } else {
1215 painter->drawPixmap(m_pixmapPos, pixmap);
1216 }
1217 }
1218
1219 void KStandardItemListWidget::drawSiblingsInformation(QPainter* painter)
1220 {
1221 const int siblingSize = size().height();
1222 const int x = (m_expansionArea.left() + m_expansionArea.right() - siblingSize) / 2;
1223 QRect siblingRect(x, 0, siblingSize, siblingSize);
1224
1225 QStyleOption option;
1226 bool isItemSibling = true;
1227
1228 const QBitArray siblings = siblingsInformation();
1229 for (int i = siblings.count() - 1; i >= 0; --i) {
1230 option.rect = siblingRect;
1231 option.state = siblings.at(i) ? QStyle::State_Sibling : QStyle::State_None;
1232
1233 if (isItemSibling) {
1234 option.state |= QStyle::State_Item;
1235 if (m_isExpandable) {
1236 option.state |= QStyle::State_Children;
1237 }
1238 if (data()["isExpanded"].toBool()) {
1239 option.state |= QStyle::State_Open;
1240 }
1241 isItemSibling = false;
1242 }
1243
1244 style()->drawPrimitive(QStyle::PE_IndicatorBranch, &option, painter);
1245
1246 siblingRect.translate(-siblingRect.width(), 0);
1247 }
1248 }
1249
1250 QRectF KStandardItemListWidget::roleEditingRect(const QByteArray& role) const
1251 {
1252 const TextInfo* textInfo = m_textInfo.value(role);
1253 if (!textInfo) {
1254 return QRectF();
1255 }
1256
1257 QRectF rect(textInfo->pos, textInfo->staticText.size());
1258 if (m_layout == DetailsLayout) {
1259 rect.setWidth(columnWidth(role) - rect.x());
1260 }
1261
1262 return rect;
1263 }
1264
1265 void KStandardItemListWidget::closeRoleEditor()
1266 {
1267 if (m_roleEditor->hasFocus()) {
1268 // If the editing was not ended by a FocusOut event, we have
1269 // to transfer the keyboard focus back to the KItemListContainer.
1270 scene()->views()[0]->parentWidget()->setFocus();
1271 }
1272
1273 disconnect(m_roleEditor, SIGNAL(roleEditingCanceled(int,QByteArray,QVariant)),
1274 this, SLOT(slotRoleEditingCanceled(int,QByteArray,QVariant)));
1275 disconnect(m_roleEditor, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
1276 this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
1277 m_roleEditor->deleteLater();
1278 m_roleEditor = 0;
1279 }
1280
1281 QPixmap KStandardItemListWidget::pixmapForIcon(const QString& name, int size)
1282 {
1283 const QString key = "KStandardItemListWidget:" % name % ":" % QString::number(size);
1284 QPixmap pixmap;
1285
1286 if (!QPixmapCache::find(key, pixmap)) {
1287 const KIcon icon(name);
1288
1289 int requestedSize;
1290 if (size <= KIconLoader::SizeSmall) {
1291 requestedSize = KIconLoader::SizeSmall;
1292 } else if (size <= KIconLoader::SizeSmallMedium) {
1293 requestedSize = KIconLoader::SizeSmallMedium;
1294 } else if (size <= KIconLoader::SizeMedium) {
1295 requestedSize = KIconLoader::SizeMedium;
1296 } else if (size <= KIconLoader::SizeLarge) {
1297 requestedSize = KIconLoader::SizeLarge;
1298 } else if (size <= KIconLoader::SizeHuge) {
1299 requestedSize = KIconLoader::SizeHuge;
1300 } else if (size <= KIconLoader::SizeEnormous) {
1301 requestedSize = KIconLoader::SizeEnormous;
1302 } else if (size <= KIconLoader::SizeEnormous * 2) {
1303 requestedSize = KIconLoader::SizeEnormous * 2;
1304 } else {
1305 requestedSize = size;
1306 }
1307
1308 pixmap = icon.pixmap(requestedSize, requestedSize);
1309 if (requestedSize != size) {
1310 KPixmapModifier::scale(pixmap, QSize(size, size));
1311 }
1312
1313 QPixmapCache::insert(key, pixmap);
1314 }
1315
1316 return pixmap;
1317 }
1318
1319 QSizeF KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption& option)
1320 {
1321 const qreal height = option.fontMetrics.ascent();
1322 return QSizeF(height * 5, height);
1323 }
1324
1325 qreal KStandardItemListWidget::columnPadding(const KItemListStyleOption& option)
1326 {
1327 return option.padding * 6;
1328 }
1329
1330 #include "kstandarditemlistwidget.moc"