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