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