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