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