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