]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
Fix crash when expanding/closing a sub-tree
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 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 "kfileitemlistwidget.h"
21
22 #include "kfileitemmodel.h"
23 #include "kitemlistview.h"
24 #include "kpixmapmodifier_p.h"
25
26 #include <KIcon>
27 #include <KIconEffect>
28 #include <KIconLoader>
29 #include <KLocale>
30 #include <KStringHandler>
31 #include <KDebug>
32
33 #include <QFontMetricsF>
34 #include <QGraphicsSceneResizeEvent>
35 #include <QPainter>
36 #include <QStyleOption>
37 #include <QTextLayout>
38 #include <QTextLine>
39
40 //#define KFILEITEMLISTWIDGET_DEBUG
41
42 KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) :
43 KItemListWidget(parent),
44 m_isDir(false),
45 m_dirtyLayout(true),
46 m_dirtyContent(true),
47 m_dirtyContentRoles(),
48 m_layout(IconsLayout),
49 m_pixmapPos(),
50 m_pixmap(),
51 m_scaledPixmapSize(),
52 m_hoverPixmapRect(),
53 m_hoverPixmap(),
54 m_textPos(),
55 m_text(),
56 m_textBoundingRect(),
57 m_sortedVisibleRoles(),
58 m_expansionArea(),
59 m_customTextColor(),
60 m_additionalInfoTextColor(),
61 m_overlay()
62 {
63 for (int i = 0; i < TextIdCount; ++i) {
64 m_text[i].setTextFormat(Qt::PlainText);
65 m_text[i].setPerformanceHint(QStaticText::AggressiveCaching);
66 }
67 }
68
69 KFileItemListWidget::~KFileItemListWidget()
70 {
71 }
72
73 void KFileItemListWidget::setLayout(Layout layout)
74 {
75 if (m_layout != layout) {
76 m_layout = layout;
77 m_dirtyLayout = true;
78 update();
79 }
80 }
81
82 KFileItemListWidget::Layout KFileItemListWidget::layout() const
83 {
84 return m_layout;
85 }
86
87 void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
88 {
89 KItemListWidget::paint(painter, option, widget);
90
91 const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
92
93 // Draw expansion toggle '>' or 'V'
94 if (m_isDir && !m_expansionArea.isEmpty()) {
95 QStyleOption arrowOption;
96 arrowOption.rect = m_expansionArea.toRect();
97 const QStyle::PrimitiveElement arrow = data()["isExpanded"].toBool()
98 ? QStyle::PE_IndicatorArrowDown : QStyle::PE_IndicatorArrowRight;
99 style()->drawPrimitive(arrow, &arrowOption, painter);
100 }
101
102 const KItemListStyleOption& itemListStyleOption = styleOption();
103 if (isHovered()) {
104 // Blend the unhovered and hovered pixmap if the hovering
105 // animation is ongoing
106 if (hoverOpacity() < 1.0) {
107 drawPixmap(painter, m_pixmap);
108 }
109
110 const qreal opacity = painter->opacity();
111 painter->setOpacity(hoverOpacity() * opacity);
112 drawPixmap(painter, m_hoverPixmap);
113 painter->setOpacity(opacity);
114 } else {
115 drawPixmap(painter, m_pixmap);
116 }
117
118 painter->setFont(itemListStyleOption.font);
119 painter->setPen(textColor());
120 painter->drawStaticText(m_textPos[Name], m_text[Name]);
121
122 painter->setPen(m_additionalInfoTextColor);
123 painter->setFont(itemListStyleOption.font);
124 for (int i = Name + 1; i < TextIdCount; ++i) {
125 painter->drawStaticText(m_textPos[i], m_text[i]);
126 }
127
128 #ifdef KFILEITEMLISTWIDGET_DEBUG
129 painter->setPen(Qt::red);
130 painter->setBrush(Qt::NoBrush);
131 painter->drawText(QPointF(0, itemListStyleOption.fontMetrics.height()), QString::number(index()));
132 painter->drawRect(rect());
133 #endif
134 }
135
136 QRectF KFileItemListWidget::iconBoundingRect() const
137 {
138 const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
139
140 QRectF bounds = m_hoverPixmapRect;
141 const qreal margin = styleOption().margin;
142 bounds.adjust(-margin, -margin, margin, margin);
143 return bounds;
144 }
145
146 QRectF KFileItemListWidget::textBoundingRect() const
147 {
148 const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
149 return m_textBoundingRect;
150 }
151
152 QRectF KFileItemListWidget::expansionToggleRect() const
153 {
154 const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
155 return m_isDir ? m_expansionArea : QRectF();
156 }
157
158 void KFileItemListWidget::invalidateCache()
159 {
160 m_dirtyLayout = true;
161 m_dirtyContent = true;
162 }
163
164 void KFileItemListWidget::refreshCache()
165 {
166 }
167
168 void KFileItemListWidget::setTextColor(const QColor& color)
169 {
170 if (color != m_customTextColor) {
171 m_customTextColor = color;
172 updateAdditionalInfoTextColor();
173 update();
174 }
175 }
176
177 QColor KFileItemListWidget::textColor() const
178 {
179 return m_customTextColor.isValid() ? m_customTextColor : styleOption().palette.text().color();
180 }
181
182 void KFileItemListWidget::setOverlay(const QPixmap& overlay)
183 {
184 m_overlay = overlay;
185 m_dirtyContent = true;
186 update();
187 }
188
189 QPixmap KFileItemListWidget::overlay() const
190 {
191 return m_overlay;
192 }
193
194 void KFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
195 const QSet<QByteArray>& roles)
196 {
197 KItemListWidget::dataChanged(current, roles);
198 m_dirtyContent = true;
199
200 QSet<QByteArray> dirtyRoles;
201 if (roles.isEmpty()) {
202 dirtyRoles = visibleRoles().keys().toSet();
203 dirtyRoles.insert("iconPixmap");
204 dirtyRoles.insert("iconName");
205 } else {
206 dirtyRoles = roles;
207 }
208
209 QSetIterator<QByteArray> it(dirtyRoles);
210 while (it.hasNext()) {
211 const QByteArray& role = it.next();
212 m_dirtyContentRoles.insert(role);
213 }
214 }
215
216 void KFileItemListWidget::visibleRolesChanged(const QHash<QByteArray, int>& current,
217 const QHash<QByteArray, int>& previous)
218 {
219 KItemListWidget::visibleRolesChanged(current, previous);
220 m_dirtyLayout = true;
221
222 // Cache the roles sorted into m_sortedVisibleRoles:
223 const int visibleRolesCount = current.count();
224 m_sortedVisibleRoles.clear();
225 m_sortedVisibleRoles.reserve(visibleRolesCount);
226 for (int i = 0; i < visibleRolesCount; ++i) {
227 m_sortedVisibleRoles.append(QByteArray());
228 }
229
230 QHashIterator<QByteArray, int> it(current);
231 while (it.hasNext()) {
232 it.next();
233
234 const int index = it.value();
235 if (index < 0 || index >= visibleRolesCount || !m_sortedVisibleRoles.at(index).isEmpty()) {
236 kWarning() << "The visible roles have an invalid sort order.";
237 break;
238 }
239
240 const QByteArray& role = it.key();
241 m_sortedVisibleRoles[index] = role;
242 }
243 }
244
245 void KFileItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
246 const QHash<QByteArray, QSizeF>& previous)
247 {
248 KItemListWidget::visibleRolesSizesChanged(current, previous);
249 m_dirtyLayout = true;
250 }
251
252 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
253 const KItemListStyleOption& previous)
254 {
255 KItemListWidget::styleOptionChanged(current, previous);
256 updateAdditionalInfoTextColor();
257 m_dirtyLayout = true;
258 }
259
260 void KFileItemListWidget::hoveredChanged(bool hovered)
261 {
262 Q_UNUSED(hovered);
263 m_dirtyLayout = true;
264 }
265
266 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
267 {
268 KItemListWidget::resizeEvent(event);
269 m_dirtyLayout = true;
270 }
271
272 void KFileItemListWidget::triggerCacheRefreshing()
273 {
274 if ((!m_dirtyContent && !m_dirtyLayout) || index() < 0) {
275 return;
276 }
277
278 refreshCache();
279
280 m_isDir = data()["isDir"].toBool();
281
282 updateExpansionArea();
283 updateTextsCache();
284 updatePixmapCache();
285
286 m_dirtyLayout = false;
287 m_dirtyContent = false;
288 m_dirtyContentRoles.clear();
289 }
290
291 void KFileItemListWidget::updateExpansionArea()
292 {
293 if (m_layout == DetailsLayout) {
294 const QHash<QByteArray, QVariant> values = data();
295 Q_ASSERT(values.contains("expansionLevel"));
296 const KItemListStyleOption& option = styleOption();
297 const int expansionLevel = values.value("expansionLevel", 0).toInt();
298
299 const qreal widgetHeight = size().height();
300 const qreal expansionLevelSize = KIconLoader::SizeSmall;
301 const qreal x = option.margin + expansionLevel * widgetHeight;
302 const qreal y = (widgetHeight - expansionLevelSize) / 2;
303 m_expansionArea = QRectF(x, y, expansionLevelSize, expansionLevelSize);
304 } else {
305 m_expansionArea = QRectF();
306 }
307 }
308
309 void KFileItemListWidget::updatePixmapCache()
310 {
311 // Precondition: Requires already updated m_textPos values to calculate
312 // the remaining height when the alignment is vertical.
313
314 const bool iconOnTop = (m_layout == IconsLayout);
315 const KItemListStyleOption& option = styleOption();
316 const int iconHeight = option.iconSize;
317
318 const QHash<QByteArray, QVariant> values = data();
319 const QSizeF widgetSize = size();
320
321 int scaledIconHeight = 0;
322 if (iconOnTop) {
323 scaledIconHeight = static_cast<int>(m_textPos[Name].y() - 3 * option.margin);
324 } else {
325 const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
326 const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
327 scaledIconHeight = (requiredTextHeight < iconHeight) ? widgetSize.height() - 2 * option.margin : iconHeight;
328 }
329
330 bool updatePixmap = (iconHeight != m_pixmap.height());
331 if (!updatePixmap && m_dirtyContent) {
332 updatePixmap = m_dirtyContentRoles.isEmpty()
333 || m_dirtyContentRoles.contains("iconPixmap")
334 || m_dirtyContentRoles.contains("iconName");
335 }
336
337 if (updatePixmap) {
338 m_pixmap = values["iconPixmap"].value<QPixmap>();
339 if (m_pixmap.isNull()) {
340 // Use the icon that fits to the MIME-type
341 QString iconName = values["iconName"].toString();
342 if (iconName.isEmpty()) {
343 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
344 // use a generic icon as fallback
345 iconName = QLatin1String("unknown");
346 }
347 m_pixmap = pixmapForIcon(iconName, iconHeight);
348 m_hoverPixmapRect.setSize(m_pixmap.size());
349 } else if (m_pixmap.size() != QSize(iconHeight, iconHeight)) {
350 // A custom pixmap has been applied. Assure that the pixmap
351 // is scaled to the available size.
352 const bool scale = m_pixmap.width() > iconHeight || m_pixmap.height() > iconHeight ||
353 (m_pixmap.width() < iconHeight && m_pixmap.height() < iconHeight);
354 if (scale) {
355 KPixmapModifier::scale(m_pixmap, QSize(iconHeight, iconHeight));
356 }
357 m_hoverPixmapRect.setSize(m_pixmap.size());
358
359 // To simplify the handling of scaling the original pixmap
360 // will be embedded into a square pixmap.
361 QPixmap squarePixmap(iconHeight, iconHeight);
362 squarePixmap.fill(Qt::transparent);
363
364 QPainter painter(&squarePixmap);
365 if (iconOnTop) {
366 const int x = (iconHeight - m_pixmap.width()) / 2; // Center horizontally
367 const int y = iconHeight - m_pixmap.height(); // Align on bottom
368 painter.drawPixmap(x, y, m_pixmap);
369 } else {
370 const int x = iconHeight - m_pixmap.width(); // Align right
371 const int y = (iconHeight - m_pixmap.height()) / 2; // Center vertically
372 painter.drawPixmap(x, y, m_pixmap);
373 }
374
375 m_pixmap = squarePixmap;
376 } else {
377 m_hoverPixmapRect.setSize(m_pixmap.size());
378 }
379
380 Q_ASSERT(m_pixmap.height() == iconHeight);
381 }
382 if (!m_overlay.isNull()) {
383 QPainter painter(&m_pixmap);
384 painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
385 }
386
387 m_scaledPixmapSize = QSize(scaledIconHeight, scaledIconHeight);
388
389 if (iconOnTop) {
390 m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
391 } else {
392 m_pixmapPos.setX(m_textPos[Name].x() - 2 * option.margin - scaledIconHeight);
393 }
394 m_pixmapPos.setY(option.margin);
395
396 // Center the hover rectangle horizontally and align it on bottom
397 const qreal x = m_pixmapPos.x() + (m_scaledPixmapSize.width() - m_hoverPixmapRect.width()) / 2.0;
398 const qreal y = m_pixmapPos.y() + m_scaledPixmapSize.height() - m_hoverPixmapRect.height();
399 m_hoverPixmapRect.moveTopLeft(QPointF(x, y));
400
401 // Prepare the pixmap that is used when the item gets hovered
402 if (isHovered()) {
403 m_hoverPixmap = m_pixmap;
404 KIconEffect* effect = KIconLoader::global()->iconEffect();
405 // In the KIconLoader terminology, active = hover.
406 if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
407 m_hoverPixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
408 } else {
409 m_hoverPixmap = m_pixmap;
410 }
411 } else if (hoverOpacity() <= 0.0) {
412 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
413 m_hoverPixmap = QPixmap();
414 }
415 }
416
417 void KFileItemListWidget::updateTextsCache()
418 {
419 QTextOption textOption;
420 switch (m_layout) {
421 case IconsLayout:
422 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
423 textOption.setAlignment(Qt::AlignHCenter);
424 break;
425 case CompactLayout:
426 case DetailsLayout:
427 textOption.setAlignment(Qt::AlignLeft);
428 textOption.setWrapMode(QTextOption::NoWrap);
429 break;
430 default:
431 Q_ASSERT(false);
432 break;
433 }
434
435 for (int i = 0; i < TextIdCount; ++i) {
436 m_text[i].setText(QString());
437 m_text[i].setTextOption(textOption);
438 }
439
440 switch (m_layout) {
441 case IconsLayout: updateIconsLayoutTextCache(); break;
442 case CompactLayout: updateCompactLayoutTextCache(); break;
443 case DetailsLayout: updateDetailsLayoutTextCache(); break;
444 default: Q_ASSERT(false); break;
445 }
446 }
447
448 void KFileItemListWidget::updateIconsLayoutTextCache()
449 {
450 // +------+
451 // | Icon |
452 // +------+
453 //
454 // Name role that
455 // might get wrapped above
456 // several lines.
457 // Additional role 1
458 // Additional role 2
459
460 const QHash<QByteArray, QVariant> values = data();
461
462 const KItemListStyleOption& option = styleOption();
463 const qreal maxWidth = size().width() - 2 * option.margin;
464 const qreal widgetHeight = size().height();
465 const qreal fontHeight = option.fontMetrics.height();
466
467 // Initialize properties for the "name" role. It will be used as anchor
468 // for initializing the position of the other roles.
469 m_text[Name].setText(KStringHandler::preProcessWrap(values["name"].toString()));
470
471 // Calculate the number of lines required for the name and the required width
472 int textLinesCountForName = 0;
473 qreal requiredWidthForName = 0;
474 QTextLine line;
475
476 QTextLayout layout(m_text[Name].text(), option.font);
477 layout.setTextOption(m_text[Name].textOption());
478 layout.beginLayout();
479 while ((line = layout.createLine()).isValid()) {
480 line.setLineWidth(maxWidth);
481 requiredWidthForName = qMax(requiredWidthForName, line.naturalTextWidth());
482 ++textLinesCountForName;
483 }
484 layout.endLayout();
485
486 // Use one line for each additional information
487 int textLinesCount = textLinesCountForName;
488 const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
489 textLinesCount += additionalRolesCount;
490
491 m_text[Name].setTextWidth(maxWidth);
492 m_textPos[Name] = QPointF(option.margin, widgetHeight - textLinesCount * fontHeight - option.margin);
493 m_textBoundingRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
494 m_textPos[Name].y(),
495 requiredWidthForName,
496 m_text[Name].size().height());
497
498 // Calculate the position for each additional information
499 qreal y = m_textPos[Name].y() + textLinesCountForName * fontHeight;
500 foreach (const QByteArray& role, m_sortedVisibleRoles) {
501 const TextId textId = roleTextId(role);
502 if (textId == Name) {
503 continue;
504 }
505
506 const QString text = roleText(textId, values[role]);
507 m_text[textId].setText(text);
508
509 qreal requiredWidth = 0;
510
511 QTextLayout layout(text, option.font);
512 layout.setTextOption(m_text[textId].textOption());
513 layout.beginLayout();
514 QTextLine textLine = layout.createLine();
515 if (textLine.isValid()) {
516 textLine.setLineWidth(maxWidth);
517 requiredWidth = textLine.naturalTextWidth();
518 if (textLine.textLength() < text.length()) {
519 // TODO: QFontMetrics::elidedText() works different regarding the given width
520 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
521 // not get elided although it does not fit into the given width. As workaround
522 // the margin is substracted.
523 const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - option.margin);
524 m_text[textId].setText(elidedText);
525 }
526 }
527 layout.endLayout();
528
529 m_textPos[textId] = QPointF(option.margin, y);
530 m_text[textId].setTextWidth(maxWidth);
531
532 const QRectF textBoundingRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
533 m_textBoundingRect |= textBoundingRect;
534
535 y += fontHeight;
536 }
537
538 // Add a margin to the text bounding rectangle
539 const qreal margin = option.margin;
540 m_textBoundingRect.adjust(-margin, -margin, margin, margin);
541 }
542
543 void KFileItemListWidget::updateCompactLayoutTextCache()
544 {
545 // +------+ Name role
546 // | Icon | Additional role 1
547 // +------+ Additional role 2
548
549 const QHash<QByteArray, QVariant> values = data();
550
551 const KItemListStyleOption& option = styleOption();
552 const qreal widgetHeight = size().height();
553 const qreal fontHeight = option.fontMetrics.height();
554 const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * fontHeight;
555 const int scaledIconSize = (textLinesHeight < option.iconSize) ? widgetHeight - 2 * option.margin : option.iconSize;
556
557 qreal maximumRequiredTextWidth = 0;
558 const qreal x = option.margin * 3 + scaledIconSize;
559 qreal y = (widgetHeight - textLinesHeight) / 2;
560 const qreal maxWidth = size().width() - x - option.margin;
561 foreach (const QByteArray& role, m_sortedVisibleRoles) {
562 const TextId textId = roleTextId(role);
563
564 const QString text = roleText(textId, values[role]);
565 m_text[textId].setText(text);
566
567 qreal requiredWidth = option.fontMetrics.width(text);
568 if (requiredWidth > maxWidth) {
569 requiredWidth = maxWidth;
570 const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
571 m_text[textId].setText(elidedText);
572 }
573
574 m_textPos[textId] = QPointF(x, y);
575 m_text[textId].setTextWidth(maxWidth);
576
577 maximumRequiredTextWidth = qMax(maximumRequiredTextWidth, requiredWidth);
578
579 y += fontHeight;
580 }
581
582 m_textBoundingRect = QRectF(x - option.margin, 0, maximumRequiredTextWidth + 2 * option.margin, widgetHeight);
583 }
584
585 void KFileItemListWidget::updateDetailsLayoutTextCache()
586 {
587 // Precondition: Requires already updated m_expansionArea
588 // to determine the left position.
589
590 // +------+
591 // | Icon | Name role Additional role 1 Additional role 2
592 // +------+
593 m_textBoundingRect = QRectF();
594
595 const KItemListStyleOption& option = styleOption();
596 const QHash<QByteArray, QVariant> values = data();
597
598 const qreal widgetHeight = size().height();
599 const int scaledIconSize = widgetHeight - 2 * option.margin;
600 const int fontHeight = option.fontMetrics.height();
601
602 qreal x = m_expansionArea.right() + option.margin * 3 + scaledIconSize;
603 const qreal y = qMax(qreal(option.margin), (widgetHeight - fontHeight) / 2);
604
605 foreach (const QByteArray& role, m_sortedVisibleRoles) {
606 const TextId textId = roleTextId(role);
607
608 const QString text = roleText(textId, values[role]);
609 m_text[textId].setText(text);
610
611 const qreal requiredWidth = option.fontMetrics.width(text);
612 m_textPos[textId] = QPointF(x, y);
613
614 const qreal columnWidth = visibleRolesSizes().value(role, QSizeF(0, 0)).width();
615 x += columnWidth;
616
617 switch (textId) {
618 case Name: {
619 m_textBoundingRect = QRectF(m_textPos[textId].x() - option.margin, 0,
620 requiredWidth + 2 * option.margin, size().height());
621
622 // The column after the name should always be aligned on the same x-position independent
623 // from the expansion-level shown in the name column
624 x -= m_expansionArea.right();
625 break;
626 }
627 case Size:
628 // The values for the size should be right aligned
629 m_textPos[textId].rx() += columnWidth - requiredWidth - 2 * option.margin;
630 break;
631
632 default:
633 break;
634 }
635 }
636 }
637
638 void KFileItemListWidget::updateAdditionalInfoTextColor()
639 {
640 // For the color of the additional info the inactive text color
641 // is not used as this might lead to unreadable text for some color schemes. Instead
642 // the text color is slightly mixed with the background color.
643 const QColor c1 = textColor();
644 const QColor c2 = styleOption().palette.background().color();
645 const int p1 = 70;
646 const int p2 = 100 - p1;
647 m_additionalInfoTextColor = QColor((c1.red() * p1 + c2.red() * p2) / 100,
648 (c1.green() * p1 + c2.green() * p2) / 100,
649 (c1.blue() * p1 + c2.blue() * p2) / 100);
650 }
651
652 QString KFileItemListWidget::roleText(TextId textId, const QVariant& roleValue) const
653 {
654 QString text;
655
656 switch (textId) {
657 case Name:
658 case Permissions:
659 case Owner:
660 case Group:
661 case Type:
662 case Destination:
663 case Path:
664 text = roleValue.toString();
665 break;
666
667 case Size: {
668 if (data().value("isDir").toBool()) {
669 // The item represents a directory. Show the number of sub directories
670 // instead of the file size of the directory.
671 if (!roleValue.isNull()) {
672 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
673 text = i18ncp("@item:intable", "%1 item", "%1 items", size);
674 }
675 } else {
676 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
677 text = KIO::convertSize(size);
678 }
679 break;
680 }
681
682 case Date: {
683 const QDateTime dateTime = roleValue.toDateTime();
684 text = KGlobal::locale()->formatDateTime(dateTime);
685 break;
686 }
687
688 default:
689 Q_ASSERT(false);
690 break;
691 }
692
693 return text;
694 }
695
696 void KFileItemListWidget::drawPixmap(QPainter* painter, const QPixmap& pixmap)
697 {
698 const bool isHiddenItem = m_text[Name].text().startsWith(QLatin1Char('.'));
699 qreal opacity;
700 if (isHiddenItem) {
701 opacity = painter->opacity();
702 painter->setOpacity(opacity * 0.3);
703 }
704
705 if (m_scaledPixmapSize != pixmap.size()) {
706 QPixmap scaledPixmap = pixmap;
707 KPixmapModifier::scale(scaledPixmap, m_scaledPixmapSize);
708 painter->drawPixmap(m_pixmapPos, scaledPixmap);
709
710 #ifdef KFILEITEMLISTWIDGET_DEBUG
711 painter->setPen(Qt::green);
712 painter->drawRect(QRectF(m_pixmapPos, QSizeF(scaledPixmap.size())));
713 #endif
714 } else {
715 painter->drawPixmap(m_pixmapPos, pixmap);
716 }
717
718 if (isHiddenItem) {
719 painter->setOpacity(opacity);
720 }
721 }
722
723 QPixmap KFileItemListWidget::pixmapForIcon(const QString& name, int size)
724 {
725 const KIcon icon(name);
726
727 int requestedSize;
728 if (size <= KIconLoader::SizeSmall) {
729 requestedSize = KIconLoader::SizeSmall;
730 } else if (size <= KIconLoader::SizeSmallMedium) {
731 requestedSize = KIconLoader::SizeSmallMedium;
732 } else if (size <= KIconLoader::SizeMedium) {
733 requestedSize = KIconLoader::SizeMedium;
734 } else if (size <= KIconLoader::SizeLarge) {
735 requestedSize = KIconLoader::SizeLarge;
736 } else if (size <= KIconLoader::SizeHuge) {
737 requestedSize = KIconLoader::SizeHuge;
738 } else if (size <= KIconLoader::SizeEnormous) {
739 requestedSize = KIconLoader::SizeEnormous;
740 } else if (size <= KIconLoader::SizeEnormous * 2) {
741 requestedSize = KIconLoader::SizeEnormous * 2;
742 } else {
743 requestedSize = size;
744 }
745
746 QPixmap pixmap = icon.pixmap(requestedSize, requestedSize);
747 if (requestedSize != size) {
748 KPixmapModifier::scale(pixmap, QSize(size, size));
749 }
750
751 return pixmap;
752 }
753
754 KFileItemListWidget::TextId KFileItemListWidget::roleTextId(const QByteArray& role)
755 {
756 static QHash<QByteArray, TextId> rolesHash;
757 if (rolesHash.isEmpty()) {
758 rolesHash.insert("name", Name);
759 rolesHash.insert("size", Size);
760 rolesHash.insert("date", Date);
761 rolesHash.insert("permissions", Permissions);
762 rolesHash.insert("owner", Owner);
763 rolesHash.insert("group", Group);
764 rolesHash.insert("type", Type);
765 rolesHash.insert("destination", Destination);
766 rolesHash.insert("path", Path);
767 }
768
769 return rolesHash.value(role);
770 }
771
772 #include "kfileitemlistwidget.moc"