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