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