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