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