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