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