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