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