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