]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
Fix selection style issues
[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 return m_customTextColor.isValid() ? m_customTextColor : styleOption().palette.text().color();
283 }
284
285 void KFileItemListWidget::setOverlay(const QPixmap& overlay)
286 {
287 m_overlay = overlay;
288 m_dirtyContent = true;
289 update();
290 }
291
292 QPixmap KFileItemListWidget::overlay() const
293 {
294 return m_overlay;
295 }
296
297 void KFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
298 const QSet<QByteArray>& roles)
299 {
300 KItemListWidget::dataChanged(current, roles);
301 m_dirtyContent = true;
302
303 QSet<QByteArray> dirtyRoles;
304 if (roles.isEmpty()) {
305 dirtyRoles = visibleRoles().toSet();
306 dirtyRoles.insert("iconPixmap");
307 dirtyRoles.insert("iconName");
308 } else {
309 dirtyRoles = roles;
310 }
311
312 QSetIterator<QByteArray> it(dirtyRoles);
313 while (it.hasNext()) {
314 const QByteArray& role = it.next();
315 m_dirtyContentRoles.insert(role);
316 }
317 }
318
319 void KFileItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
320 const QList<QByteArray>& previous)
321 {
322 KItemListWidget::visibleRolesChanged(current, previous);
323 m_sortedVisibleRoles = current;
324 m_dirtyLayout = true;
325 }
326
327 void KFileItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
328 const QHash<QByteArray, QSizeF>& previous)
329 {
330 KItemListWidget::visibleRolesSizesChanged(current, previous);
331 m_dirtyLayout = true;
332 }
333
334 void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
335 const KItemListStyleOption& previous)
336 {
337 KItemListWidget::styleOptionChanged(current, previous);
338 updateAdditionalInfoTextColor();
339 m_dirtyLayout = true;
340 }
341
342 void KFileItemListWidget::hoveredChanged(bool hovered)
343 {
344 Q_UNUSED(hovered);
345 m_dirtyLayout = true;
346 }
347
348 void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
349 {
350 KItemListWidget::resizeEvent(event);
351 m_dirtyLayout = true;
352 }
353
354 void KFileItemListWidget::triggerCacheRefreshing()
355 {
356 if ((!m_dirtyContent && !m_dirtyLayout) || index() < 0) {
357 return;
358 }
359
360 refreshCache();
361
362 m_isDir = data()["isDir"].toBool();
363
364 updateExpansionArea();
365 updateTextsCache();
366 updatePixmapCache();
367
368 m_dirtyLayout = false;
369 m_dirtyContent = false;
370 m_dirtyContentRoles.clear();
371 }
372
373 void KFileItemListWidget::updateExpansionArea()
374 {
375 if (m_layout == DetailsLayout) {
376 const QHash<QByteArray, QVariant> values = data();
377 Q_ASSERT(values.contains("expansionLevel"));
378 const KItemListStyleOption& option = styleOption();
379 const int expansionLevel = values.value("expansionLevel", 0).toInt();
380
381 const qreal widgetHeight = size().height();
382 const qreal expansionLevelSize = KIconLoader::SizeSmall;
383 const qreal x = option.margin + expansionLevel * widgetHeight;
384 const qreal y = (widgetHeight - expansionLevelSize) / 2;
385 m_expansionArea = QRectF(x, y, expansionLevelSize, expansionLevelSize);
386 } else {
387 m_expansionArea = QRectF();
388 }
389 }
390
391 void KFileItemListWidget::updatePixmapCache()
392 {
393 // Precondition: Requires already updated m_textPos values to calculate
394 // the remaining height when the alignment is vertical.
395
396 const bool iconOnTop = (m_layout == IconsLayout);
397 const KItemListStyleOption& option = styleOption();
398 const int iconHeight = option.iconSize;
399
400 const QHash<QByteArray, QVariant> values = data();
401 const QSizeF widgetSize = size();
402
403 int scaledIconHeight = 0;
404 if (iconOnTop) {
405 scaledIconHeight = static_cast<int>(m_textPos[Name].y() - 3 * option.margin);
406 } else {
407 const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
408 const qreal requiredTextHeight = textRowsCount * option.fontMetrics.height();
409 scaledIconHeight = (requiredTextHeight < iconHeight) ? widgetSize.height() - 2 * option.margin : iconHeight;
410 }
411
412 bool updatePixmap = (iconHeight != m_pixmap.height());
413 if (!updatePixmap && m_dirtyContent) {
414 updatePixmap = m_dirtyContentRoles.isEmpty()
415 || m_dirtyContentRoles.contains("iconPixmap")
416 || m_dirtyContentRoles.contains("iconName");
417 }
418
419 if (updatePixmap) {
420 // The selection toggle should always be applied to the top/left
421 // of the pixmap
422 m_selectionTogglePos = QPointF(-option.margin, -option.margin);
423
424 m_pixmap = values["iconPixmap"].value<QPixmap>();
425 if (m_pixmap.isNull()) {
426 // Use the icon that fits to the MIME-type
427 QString iconName = values["iconName"].toString();
428 if (iconName.isEmpty()) {
429 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
430 // use a generic icon as fallback
431 iconName = QLatin1String("unknown");
432 }
433 m_pixmap = pixmapForIcon(iconName, iconHeight);
434 m_hoverPixmapRect.setSize(m_pixmap.size());
435 } else if (m_pixmap.size() != QSize(iconHeight, iconHeight)) {
436 // A custom pixmap has been applied. Assure that the pixmap
437 // is scaled to the available size.
438 const bool scale = m_pixmap.width() > iconHeight || m_pixmap.height() > iconHeight ||
439 (m_pixmap.width() < iconHeight && m_pixmap.height() < iconHeight);
440 if (scale) {
441 KPixmapModifier::scale(m_pixmap, QSize(iconHeight, iconHeight));
442 }
443 m_hoverPixmapRect.setSize(m_pixmap.size());
444
445 // To simplify the handling of scaling the original pixmap
446 // will be embedded into a square pixmap.
447 QPixmap squarePixmap(iconHeight, iconHeight);
448 squarePixmap.fill(Qt::transparent);
449
450 QPainter painter(&squarePixmap);
451 int x, y;
452 if (iconOnTop) {
453 x = (iconHeight - m_pixmap.width()) / 2; // Center horizontally
454 y = iconHeight - m_pixmap.height(); // Align on bottom
455 painter.drawPixmap(x, y, m_pixmap);
456 } else {
457 x = iconHeight - m_pixmap.width(); // Align right
458 y = (iconHeight - m_pixmap.height()) / 2; // Center vertically
459 painter.drawPixmap(x, y, m_pixmap);
460 }
461 m_selectionTogglePos += QPointF(x, y);
462
463 m_pixmap = squarePixmap;
464 } else {
465 m_hoverPixmapRect.setSize(m_pixmap.size());
466 }
467
468 Q_ASSERT(m_pixmap.height() == iconHeight);
469 }
470 if (!m_overlay.isNull()) {
471 QPainter painter(&m_pixmap);
472 painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
473 }
474
475 m_scaledPixmapSize = QSize(scaledIconHeight, scaledIconHeight);
476
477 if (iconOnTop) {
478 m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
479 } else {
480 m_pixmapPos.setX(m_textPos[Name].x() - 2 * option.margin - scaledIconHeight);
481 }
482 m_pixmapPos.setY(option.margin);
483
484 if (updatePixmap) {
485 m_selectionTogglePos += m_pixmapPos;
486 }
487
488 // Center the hover rectangle horizontally and align it on bottom
489 const qreal x = m_pixmapPos.x() + (m_scaledPixmapSize.width() - m_hoverPixmapRect.width()) / 2.0;
490 const qreal y = m_pixmapPos.y() + m_scaledPixmapSize.height() - m_hoverPixmapRect.height();
491 m_hoverPixmapRect.moveTopLeft(QPointF(x, y));
492
493 // Prepare the pixmap that is used when the item gets hovered
494 if (isHovered()) {
495 m_hoverPixmap = m_pixmap;
496 KIconEffect* effect = KIconLoader::global()->iconEffect();
497 // In the KIconLoader terminology, active = hover.
498 if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
499 m_hoverPixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
500 } else {
501 m_hoverPixmap = m_pixmap;
502 }
503 } else if (hoverOpacity() <= 0.0) {
504 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
505 m_hoverPixmap = QPixmap();
506 }
507 }
508
509 void KFileItemListWidget::updateTextsCache()
510 {
511 QTextOption textOption;
512 switch (m_layout) {
513 case IconsLayout:
514 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
515 textOption.setAlignment(Qt::AlignHCenter);
516 break;
517 case CompactLayout:
518 case DetailsLayout:
519 textOption.setAlignment(Qt::AlignLeft);
520 textOption.setWrapMode(QTextOption::NoWrap);
521 break;
522 default:
523 Q_ASSERT(false);
524 break;
525 }
526
527 for (int i = 0; i < TextIdCount; ++i) {
528 m_text[i].setText(QString());
529 m_text[i].setTextOption(textOption);
530 }
531
532 switch (m_layout) {
533 case IconsLayout: updateIconsLayoutTextCache(); break;
534 case CompactLayout: updateCompactLayoutTextCache(); break;
535 case DetailsLayout: updateDetailsLayoutTextCache(); break;
536 default: Q_ASSERT(false); break;
537 }
538 }
539
540 void KFileItemListWidget::updateIconsLayoutTextCache()
541 {
542 // +------+
543 // | Icon |
544 // +------+
545 //
546 // Name role that
547 // might get wrapped above
548 // several lines.
549 // Additional role 1
550 // Additional role 2
551
552 const QHash<QByteArray, QVariant> values = data();
553
554 const KItemListStyleOption& option = styleOption();
555 const qreal maxWidth = size().width() - 2 * option.margin;
556 const qreal widgetHeight = size().height();
557 const qreal fontHeight = option.fontMetrics.height();
558
559 // Initialize properties for the "name" role. It will be used as anchor
560 // for initializing the position of the other roles.
561 m_text[Name].setText(KStringHandler::preProcessWrap(values["name"].toString()));
562
563 // Calculate the number of lines required for the name and the required width
564 int textLinesCountForName = 0;
565 qreal requiredWidthForName = 0;
566 QTextLine line;
567
568 QTextLayout layout(m_text[Name].text(), option.font);
569 layout.setTextOption(m_text[Name].textOption());
570 layout.beginLayout();
571 while ((line = layout.createLine()).isValid()) {
572 line.setLineWidth(maxWidth);
573 requiredWidthForName = qMax(requiredWidthForName, line.naturalTextWidth());
574 ++textLinesCountForName;
575 }
576 layout.endLayout();
577
578 // Use one line for each additional information
579 int textLinesCount = textLinesCountForName;
580 const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
581 textLinesCount += additionalRolesCount;
582
583 m_text[Name].setTextWidth(maxWidth);
584 m_textPos[Name] = QPointF(option.margin, widgetHeight - textLinesCount * fontHeight - option.margin);
585 m_textRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
586 m_textPos[Name].y(),
587 requiredWidthForName,
588 m_text[Name].size().height());
589
590 // Calculate the position for each additional information
591 qreal y = m_textPos[Name].y() + textLinesCountForName * fontHeight;
592 foreach (const QByteArray& role, m_sortedVisibleRoles) {
593 const TextId textId = roleTextId(role);
594 if (textId == Name) {
595 continue;
596 }
597
598 const QString text = roleText(role, values);
599 m_text[textId].setText(text);
600
601 qreal requiredWidth = 0;
602
603 QTextLayout layout(text, option.font);
604 layout.setTextOption(m_text[textId].textOption());
605 layout.beginLayout();
606 QTextLine textLine = layout.createLine();
607 if (textLine.isValid()) {
608 textLine.setLineWidth(maxWidth);
609 requiredWidth = textLine.naturalTextWidth();
610 if (textLine.textLength() < text.length()) {
611 // TODO: QFontMetrics::elidedText() works different regarding the given width
612 // in comparison to QTextLine::setLineWidth(). It might happen that the text does
613 // not get elided although it does not fit into the given width. As workaround
614 // the margin is substracted.
615 const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - option.margin);
616 m_text[textId].setText(elidedText);
617 }
618 }
619 layout.endLayout();
620
621 m_textPos[textId] = QPointF(option.margin, y);
622 m_text[textId].setTextWidth(maxWidth);
623
624 const QRectF textRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
625 m_textRect |= textRect;
626
627 y += fontHeight;
628 }
629
630 // Add a margin to the text rectangle
631 const qreal margin = option.margin;
632 m_textRect.adjust(-margin, -margin, margin, margin);
633 }
634
635 void KFileItemListWidget::updateCompactLayoutTextCache()
636 {
637 // +------+ Name role
638 // | Icon | Additional role 1
639 // +------+ Additional role 2
640
641 const QHash<QByteArray, QVariant> values = data();
642
643 const KItemListStyleOption& option = styleOption();
644 const qreal widgetHeight = size().height();
645 const qreal fontHeight = option.fontMetrics.height();
646 const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * fontHeight;
647 const int scaledIconSize = (textLinesHeight < option.iconSize) ? widgetHeight - 2 * option.margin : option.iconSize;
648
649 qreal maximumRequiredTextWidth = 0;
650 const qreal x = option.margin * 3 + scaledIconSize;
651 qreal y = (widgetHeight - textLinesHeight) / 2;
652 const qreal maxWidth = size().width() - x - option.margin;
653 foreach (const QByteArray& role, m_sortedVisibleRoles) {
654 const TextId textId = roleTextId(role);
655
656 const QString text = roleText(role, values);
657 m_text[textId].setText(text);
658
659 qreal requiredWidth = option.fontMetrics.width(text);
660 if (requiredWidth > maxWidth) {
661 requiredWidth = maxWidth;
662 const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
663 m_text[textId].setText(elidedText);
664 }
665
666 m_textPos[textId] = QPointF(x, y);
667 m_text[textId].setTextWidth(maxWidth);
668
669 maximumRequiredTextWidth = qMax(maximumRequiredTextWidth, requiredWidth);
670
671 y += fontHeight;
672 }
673
674 m_textRect = QRectF(x - option.margin, 0, maximumRequiredTextWidth + 2 * option.margin, widgetHeight);
675 }
676
677 void KFileItemListWidget::updateDetailsLayoutTextCache()
678 {
679 // Precondition: Requires already updated m_expansionArea
680 // to determine the left position.
681
682 // +------+
683 // | Icon | Name role Additional role 1 Additional role 2
684 // +------+
685 m_textRect = QRectF();
686
687 const KItemListStyleOption& option = styleOption();
688 const QHash<QByteArray, QVariant> values = data();
689
690 const qreal widgetHeight = size().height();
691 const int scaledIconSize = widgetHeight - 2 * option.margin;
692 const int fontHeight = option.fontMetrics.height();
693
694 const qreal columnMargin = option.margin * 3;
695 const qreal firstColumnInc = m_expansionArea.right() + option.margin * 2 + scaledIconSize;
696 qreal x = firstColumnInc;
697 const qreal y = qMax(qreal(option.margin), (widgetHeight - fontHeight) / 2);
698
699 foreach (const QByteArray& role, m_sortedVisibleRoles) {
700 const TextId textId = roleTextId(role);
701
702 QString text = roleText(role, values);
703
704 // Elide the text in case it does not fit into the available column-width
705 qreal requiredWidth = option.fontMetrics.width(text);
706 const qreal columnWidth = visibleRolesSizes().value(role, QSizeF(0, 0)).width();
707 qreal availableTextWidth = columnWidth - 2 * columnMargin;
708 if (textId == Name) {
709 availableTextWidth -= firstColumnInc;
710 }
711
712 if (requiredWidth > availableTextWidth) {
713 text = option.fontMetrics.elidedText(text, Qt::ElideRight, availableTextWidth);
714 requiredWidth = option.fontMetrics.width(text);
715 }
716
717 m_text[textId].setText(text);
718 m_textPos[textId] = QPointF(x + columnMargin, y);
719 x += columnWidth;
720
721 switch (textId) {
722 case Name: {
723 m_textRect = QRectF(m_textPos[textId].x() - option.margin, 0,
724 requiredWidth + 2 * option.margin, size().height());
725
726 // The column after the name should always be aligned on the same x-position independent
727 // from the expansion-level shown in the name column
728 x -= firstColumnInc;
729 break;
730 }
731 case Size:
732 // The values for the size should be right aligned
733 m_textPos[textId].rx() += columnWidth - requiredWidth - 2 * columnMargin;
734 break;
735
736 default:
737 break;
738 }
739 }
740 }
741
742 void KFileItemListWidget::updateAdditionalInfoTextColor()
743 {
744 // For the color of the additional info the inactive text color
745 // is not used as this might lead to unreadable text for some color schemes. Instead
746 // the text color is slightly mixed with the background color.
747 const QColor c1 = textColor();
748 const QColor c2 = styleOption().palette.base().color();
749 const int p1 = 70;
750 const int p2 = 100 - p1;
751 m_additionalInfoTextColor = QColor((c1.red() * p1 + c2.red() * p2) / 100,
752 (c1.green() * p1 + c2.green() * p2) / 100,
753 (c1.blue() * p1 + c2.blue() * p2) / 100);
754 }
755
756 void KFileItemListWidget::drawPixmap(QPainter* painter, const QPixmap& pixmap)
757 {
758 const bool isHiddenItem = m_text[Name].text().startsWith(QLatin1Char('.'));
759 qreal opacity;
760 if (isHiddenItem) {
761 opacity = painter->opacity();
762 painter->setOpacity(opacity * 0.3);
763 }
764
765 if (m_scaledPixmapSize != pixmap.size()) {
766 QPixmap scaledPixmap = pixmap;
767 KPixmapModifier::scale(scaledPixmap, m_scaledPixmapSize);
768 painter->drawPixmap(m_pixmapPos, scaledPixmap);
769
770 #ifdef KFILEITEMLISTWIDGET_DEBUG
771 painter->setPen(Qt::green);
772 painter->drawRect(QRectF(m_pixmapPos, QSizeF(scaledPixmap.size())));
773 #endif
774 } else {
775 painter->drawPixmap(m_pixmapPos, pixmap);
776 }
777
778 if (isHiddenItem) {
779 painter->setOpacity(opacity);
780 }
781 }
782
783 QPixmap KFileItemListWidget::pixmapForIcon(const QString& name, int size)
784 {
785 const KIcon icon(name);
786
787 int requestedSize;
788 if (size <= KIconLoader::SizeSmall) {
789 requestedSize = KIconLoader::SizeSmall;
790 } else if (size <= KIconLoader::SizeSmallMedium) {
791 requestedSize = KIconLoader::SizeSmallMedium;
792 } else if (size <= KIconLoader::SizeMedium) {
793 requestedSize = KIconLoader::SizeMedium;
794 } else if (size <= KIconLoader::SizeLarge) {
795 requestedSize = KIconLoader::SizeLarge;
796 } else if (size <= KIconLoader::SizeHuge) {
797 requestedSize = KIconLoader::SizeHuge;
798 } else if (size <= KIconLoader::SizeEnormous) {
799 requestedSize = KIconLoader::SizeEnormous;
800 } else if (size <= KIconLoader::SizeEnormous * 2) {
801 requestedSize = KIconLoader::SizeEnormous * 2;
802 } else {
803 requestedSize = size;
804 }
805
806 QPixmap pixmap = icon.pixmap(requestedSize, requestedSize);
807 if (requestedSize != size) {
808 KPixmapModifier::scale(pixmap, QSize(size, size));
809 }
810
811 return pixmap;
812 }
813
814 KFileItemListWidget::TextId KFileItemListWidget::roleTextId(const QByteArray& role)
815 {
816 static QHash<QByteArray, TextId> rolesHash;
817 if (rolesHash.isEmpty()) {
818 rolesHash.insert("name", Name);
819 rolesHash.insert("size", Size);
820 rolesHash.insert("date", Date);
821 rolesHash.insert("permissions", Permissions);
822 rolesHash.insert("owner", Owner);
823 rolesHash.insert("group", Group);
824 rolesHash.insert("type", Type);
825 rolesHash.insert("destination", Destination);
826 rolesHash.insert("path", Path);
827 }
828
829 return rolesHash.value(role);
830 }
831
832 #include "kfileitemlistwidget.moc"