]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistwidget.cpp
Remove current item highlighting in the Places Panel
[dolphin.git] / src / kitemviews / kstandarditemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 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 "kstandarditemlistwidget.h"
21
22 #include "kfileitemlistview.h"
23 #include "kfileitemmodel.h"
24
25 #include <KIcon>
26 #include <KIconEffect>
27 #include <KIconLoader>
28 #include <KLocale>
29 #include <kratingpainter.h>
30 #include <KStringHandler>
31 #include <KDebug>
32
33 #include "private/kfileitemclipboard.h"
34 #include "private/kitemlistroleeditor.h"
35 #include "private/kpixmapmodifier.h"
36
37 #include <QFontMetricsF>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneResizeEvent>
40 #include <QGraphicsView>
41 #include <QPainter>
42 #include <QStyleOption>
43 #include <QTextLayout>
44 #include <QTextLine>
45 #include <QPixmapCache>
46
47 // #define KSTANDARDITEMLISTWIDGET_DEBUG
48
49 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
50 KItemListWidgetInformant()
51 {
52 }
53
54 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
55 {
56 }
57
58 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const
59 {
60 switch (static_cast<const KStandardItemListView*>(view)->itemLayout()) {
61 case KStandardItemListWidget::IconsLayout:
62 calculateIconsLayoutItemSizeHints(logicalHeightHints, logicalWidthHint, view);
63 break;
64
65 case KStandardItemListWidget::CompactLayout:
66 calculateCompactLayoutItemSizeHints(logicalHeightHints, logicalWidthHint, view);
67 break;
68
69 case KStandardItemListWidget::DetailsLayout:
70 calculateDetailsLayoutItemSizeHints(logicalHeightHints, logicalWidthHint, view);
71 break;
72
73 default:
74 Q_ASSERT(false);
75 break;
76 }
77 }
78
79 qreal KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray& role,
80 int index,
81 const KItemListView* view) const
82 {
83 const QHash<QByteArray, QVariant> values = view->model()->data(index);
84 const KItemListStyleOption& option = view->styleOption();
85
86 const QString text = roleText(role, values);
87 qreal width = KStandardItemListWidget::columnPadding(option);
88
89 const QFontMetrics& normalFontMetrics = option.fontMetrics;
90 const QFontMetrics linkFontMetrics(customizedFontForLinks(option.font));
91
92 if (role == "rating") {
93 width += KStandardItemListWidget::preferredRatingSize(option).width();
94 } else {
95 // If current item is a link, we use the customized link font metrics instead of the normal font metrics.
96 const QFontMetrics& fontMetrics = itemIsLink(index, view) ? linkFontMetrics : normalFontMetrics;
97
98 width += fontMetrics.width(text);
99
100 if (role == "text") {
101 if (view->supportsItemExpanding()) {
102 // Increase the width by the expansion-toggle and the current expansion level
103 const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
104 const qreal height = option.padding * 2 + qMax(option.iconSize, fontMetrics.height());
105 width += (expandedParentsCount + 1) * height;
106 }
107
108 // Increase the width by the required space for the icon
109 width += option.padding * 2 + option.iconSize;
110 }
111 }
112
113 return width;
114 }
115
116 QString KStandardItemListWidgetInformant::itemText(int index, const KItemListView* view) const
117 {
118 return view->model()->data(index).value("text").toString();
119 }
120
121 bool KStandardItemListWidgetInformant::itemIsLink(int index, const KItemListView* view) const
122 {
123 return false;
124 }
125
126 QString KStandardItemListWidgetInformant::roleText(const QByteArray& role,
127 const QHash<QByteArray, QVariant>& values) const
128 {
129 if (role == "rating") {
130 // Always use an empty text, as the rating is shown by the image m_rating.
131 return QString();
132 }
133 return values.value(role).toString();
134 }
135
136 QFont KStandardItemListWidgetInformant::customizedFontForLinks(const QFont& baseFont) const
137 {
138 return baseFont;
139 }
140
141 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const
142 {
143 const KItemListStyleOption& option = view->styleOption();
144 const QFont& normalFont = option.font;
145 const int additionalRolesCount = qMax(view->visibleRoles().count() - 1, 0);
146
147 const qreal itemWidth = view->itemSize().width();
148 const qreal maxWidth = itemWidth - 2 * option.padding;
149 const qreal additionalRolesSpacing = additionalRolesCount * option.fontMetrics.lineSpacing();
150 const qreal spacingAndIconHeight = option.iconSize + option.padding * 3;
151
152 const QFont linkFont = customizedFontForLinks(normalFont);
153
154 QTextOption textOption(Qt::AlignHCenter);
155 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
156
157 for (int index = 0; index < logicalHeightHints.count(); ++index) {
158 if (logicalHeightHints.at(index) > 0.0) {
159 continue;
160 }
161
162 // If the current item is a link, we use the customized link font instead of the normal font.
163 const QFont& font = itemIsLink(index, view) ? linkFont : normalFont;
164
165 const QString& text = KStringHandler::preProcessWrap(itemText(index, view));
166
167 // Calculate the number of lines required for wrapping the name
168 qreal textHeight = 0;
169 QTextLayout layout(text, font);
170 layout.setTextOption(textOption);
171 layout.beginLayout();
172 QTextLine line;
173 int lineCount = 0;
174 while ((line = layout.createLine()).isValid()) {
175 line.setLineWidth(maxWidth);
176 line.naturalTextWidth();
177 textHeight += line.height();
178
179 ++lineCount;
180 if (lineCount == option.maxTextLines) {
181 break;
182 }
183 }
184 layout.endLayout();
185
186 // Add one line for each additional information
187 textHeight += additionalRolesSpacing;
188
189 logicalHeightHints[index] = textHeight + spacingAndIconHeight;
190 }
191
192 logicalWidthHint = itemWidth;
193 }
194
195 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const
196 {
197 const KItemListStyleOption& option = view->styleOption();
198 const QFontMetrics& normalFontMetrics = option.fontMetrics;
199 const int additionalRolesCount = qMax(view->visibleRoles().count() - 1, 0);
200
201 const QList<QByteArray>& visibleRoles = view->visibleRoles();
202 const bool showOnlyTextRole = (visibleRoles.count() == 1) && (visibleRoles.first() == "text");
203 const qreal maxWidth = option.maxTextWidth;
204 const qreal paddingAndIconWidth = option.padding * 4 + option.iconSize;
205 const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * normalFontMetrics.lineSpacing());
206
207 const QFontMetrics linkFontMetrics(customizedFontForLinks(option.font));
208
209 for (int index = 0; index < logicalHeightHints.count(); ++index) {
210 if (logicalHeightHints.at(index) > 0.0) {
211 continue;
212 }
213
214 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
215 const QFontMetrics& fontMetrics = itemIsLink(index, view) ? linkFontMetrics : normalFontMetrics;
216
217 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
218 // to show all roles without horizontal clipping.
219 qreal maximumRequiredWidth = 0.0;
220
221 if (showOnlyTextRole) {
222 maximumRequiredWidth = fontMetrics.width(itemText(index, view));
223 } else {
224 const QHash<QByteArray, QVariant>& values = view->model()->data(index);
225 foreach (const QByteArray& role, visibleRoles) {
226 const QString& text = roleText(role, values);
227 const qreal requiredWidth = fontMetrics.width(text);
228 maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
229 }
230 }
231
232 qreal width = paddingAndIconWidth + maximumRequiredWidth;
233 if (maxWidth > 0 && width > maxWidth) {
234 width = maxWidth;
235 }
236
237 logicalHeightHints[index] = width;
238 }
239
240 logicalWidthHint = height;
241 }
242
243 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const
244 {
245 const KItemListStyleOption& option = view->styleOption();
246 const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height());
247 logicalHeightHints.fill(height);
248 logicalWidthHint = -1.0;
249 }
250
251 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
252 KItemListWidget(informant, parent),
253 m_isCut(false),
254 m_isHidden(false),
255 m_customizedFont(),
256 m_customizedFontMetrics(m_customizedFont),
257 m_isExpandable(false),
258 m_supportsItemExpanding(false),
259 m_dirtyLayout(true),
260 m_dirtyContent(true),
261 m_dirtyContentRoles(),
262 m_layout(IconsLayout),
263 m_pixmapPos(),
264 m_pixmap(),
265 m_scaledPixmapSize(),
266 m_iconRect(),
267 m_hoverPixmap(),
268 m_textInfo(),
269 m_textRect(),
270 m_sortedVisibleRoles(),
271 m_expansionArea(),
272 m_customTextColor(),
273 m_additionalInfoTextColor(),
274 m_overlay(),
275 m_rating(),
276 m_roleEditor(0),
277 m_oldRoleEditor(0)
278 {
279 }
280
281 KStandardItemListWidget::~KStandardItemListWidget()
282 {
283 qDeleteAll(m_textInfo);
284 m_textInfo.clear();
285
286 if (m_roleEditor) {
287 m_roleEditor->deleteLater();
288 }
289
290 if (m_oldRoleEditor) {
291 m_oldRoleEditor->deleteLater();
292 }
293 }
294
295 void KStandardItemListWidget::setLayout(Layout layout)
296 {
297 if (m_layout != layout) {
298 m_layout = layout;
299 m_dirtyLayout = true;
300 updateAdditionalInfoTextColor();
301 update();
302 }
303 }
304
305 KStandardItemListWidget::Layout KStandardItemListWidget::layout() const
306 {
307 return m_layout;
308 }
309
310 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding)
311 {
312 if (m_supportsItemExpanding != supportsItemExpanding) {
313 m_supportsItemExpanding = supportsItemExpanding;
314 m_dirtyLayout = true;
315 update();
316 }
317 }
318
319 bool KStandardItemListWidget::supportsItemExpanding() const
320 {
321 return m_supportsItemExpanding;
322 }
323
324 void KStandardItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
325 {
326 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
327
328 KItemListWidget::paint(painter, option, widget);
329
330 if (!m_expansionArea.isEmpty()) {
331 drawSiblingsInformation(painter);
332 }
333
334 const KItemListStyleOption& itemListStyleOption = styleOption();
335 if (isHovered()) {
336 if (hoverOpacity() < 1.0) {
337 /*
338 * Linear interpolation between m_pixmap and m_hoverPixmap.
339 *
340 * Note that this cannot be achieved by painting m_hoverPixmap over
341 * m_pixmap, even if the opacities are adjusted. For details see
342 * https://git.reviewboard.kde.org/r/109614/
343 */
344 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
345 QPixmap pixmap1(m_pixmap.size());
346 pixmap1.fill(Qt::transparent);
347 {
348 QPainter p(&pixmap1);
349 p.setOpacity(1.0 - hoverOpacity());
350 p.drawPixmap(0, 0, m_pixmap);
351 }
352
353 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
354 QPixmap pixmap2(pixmap1.size());
355 pixmap2.fill(Qt::transparent);
356 {
357 QPainter p(&pixmap2);
358 p.setOpacity(hoverOpacity());
359 p.drawPixmap(0, 0, m_hoverPixmap);
360 }
361
362 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
363 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
364 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
365 {
366 QPainter p(&pixmap1);
367 p.setCompositionMode(QPainter::CompositionMode_Plus);
368 p.drawPixmap(0, 0, pixmap2);
369 }
370
371 // Finally paint pixmap1 on the widget
372 drawPixmap(painter, pixmap1);
373 } else {
374 drawPixmap(painter, m_hoverPixmap);
375 }
376 } else {
377 drawPixmap(painter, m_pixmap);
378 }
379
380 painter->setFont(m_customizedFont);
381 painter->setPen(textColor());
382 const TextInfo* textInfo = m_textInfo.value("text");
383
384 if (!textInfo) {
385 // It seems that we can end up here even if m_textInfo does not contain
386 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
387 // this can only happen if the index is negative. This can happen when
388 // the item is about to be removed, see KItemListView::slotItemsRemoved().
389 // TODO: try to reproduce the crash and find a better fix.
390 return;
391 }
392
393 painter->drawStaticText(textInfo->pos, textInfo->staticText);
394
395 bool clipAdditionalInfoBounds = false;
396 if (m_supportsItemExpanding) {
397 // Prevent a possible overlapping of the additional-information texts
398 // with the icon. This can happen if the user has minimized the width
399 // of the name-column to a very small value.
400 const qreal minX = m_pixmapPos.x() + m_pixmap.width() + 4 * itemListStyleOption.padding;
401 if (textInfo->pos.x() + columnWidth("text") > minX) {
402 clipAdditionalInfoBounds = true;
403 painter->save();
404 painter->setClipRect(minX, 0, size().width() - minX, size().height(), Qt::IntersectClip);
405 }
406 }
407
408 painter->setPen(m_additionalInfoTextColor);
409 painter->setFont(m_customizedFont);
410
411 for (int i = 1; i < m_sortedVisibleRoles.count(); ++i) {
412 const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles[i]);
413 painter->drawStaticText(textInfo->pos, textInfo->staticText);
414 }
415
416 if (!m_rating.isNull()) {
417 const TextInfo* ratingTextInfo = m_textInfo.value("rating");
418 QPointF pos = ratingTextInfo->pos;
419 const Qt::Alignment align = ratingTextInfo->staticText.textOption().alignment();
420 if (align & Qt::AlignHCenter) {
421 pos.rx() += (size().width() - m_rating.width()) / 2 - 2;
422 }
423 painter->drawPixmap(pos, m_rating);
424 }
425
426 if (clipAdditionalInfoBounds) {
427 painter->restore();
428 }
429
430 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
431 painter->setBrush(Qt::NoBrush);
432 painter->setPen(Qt::green);
433 painter->drawRect(m_iconRect);
434
435 painter->setPen(Qt::blue);
436 painter->drawRect(m_textRect);
437
438 painter->setPen(Qt::red);
439 painter->drawText(QPointF(0, m_customizedFontMetrics.height()), QString::number(index()));
440 painter->drawRect(rect());
441 #endif
442 }
443
444 QRectF KStandardItemListWidget::iconRect() const
445 {
446 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
447 return m_iconRect;
448 }
449
450 QRectF KStandardItemListWidget::textRect() const
451 {
452 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
453 return m_textRect;
454 }
455
456 QRectF KStandardItemListWidget::textFocusRect() const
457 {
458 // In the compact- and details-layout a larger textRect() is returned to be aligned
459 // with the iconRect(). This is useful to have a larger selection/hover-area
460 // when having a quite large icon size but only one line of text. Still the
461 // focus rectangle should be shown as narrow as possible around the text.
462
463 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
464
465 switch (m_layout) {
466 case CompactLayout: {
467 QRectF rect = m_textRect;
468 const TextInfo* topText = m_textInfo.value(m_sortedVisibleRoles.first());
469 const TextInfo* bottomText = m_textInfo.value(m_sortedVisibleRoles.last());
470 rect.setTop(topText->pos.y());
471 rect.setBottom(bottomText->pos.y() + bottomText->staticText.size().height());
472 return rect;
473 }
474
475 case DetailsLayout: {
476 QRectF rect = m_textRect;
477 const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles.first());
478 rect.setTop(textInfo->pos.y());
479 rect.setBottom(textInfo->pos.y() + textInfo->staticText.size().height());
480
481 const KItemListStyleOption& option = styleOption();
482 if (option.extendedSelectionRegion) {
483 const QString text = textInfo->staticText.text();
484 rect.setWidth(m_customizedFontMetrics.width(text) + 2 * option.padding);
485 }
486
487 return rect;
488 }
489
490 default:
491 break;
492 }
493
494 return m_textRect;
495 }
496
497 QRectF KStandardItemListWidget::expansionToggleRect() const
498 {
499 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
500 return m_isExpandable ? m_expansionArea : QRectF();
501 }
502
503 QRectF KStandardItemListWidget::selectionToggleRect() const
504 {
505 const_cast<KStandardItemListWidget*>(this)->triggerCacheRefreshing();
506
507 const int iconHeight = styleOption().iconSize;
508
509 int toggleSize = KIconLoader::SizeSmall;
510 if (iconHeight >= KIconLoader::SizeEnormous) {
511 toggleSize = KIconLoader::SizeMedium;
512 } else if (iconHeight >= KIconLoader::SizeLarge) {
513 toggleSize = KIconLoader::SizeSmallMedium;
514 }
515
516 QPointF pos = iconRect().topLeft();
517
518 // If the selection toggle has a very small distance to the
519 // widget borders, the size of the selection toggle will get
520 // increased to prevent an accidental clicking of the item
521 // when trying to hit the toggle.
522 const int widgetHeight = size().height();
523 const int widgetWidth = size().width();
524 const int minMargin = 2;
525
526 if (toggleSize + minMargin * 2 >= widgetHeight) {
527 pos.rx() -= (widgetHeight - toggleSize) / 2;
528 toggleSize = widgetHeight;
529 pos.setY(0);
530 }
531 if (toggleSize + minMargin * 2 >= widgetWidth) {
532 pos.ry() -= (widgetWidth - toggleSize) / 2;
533 toggleSize = widgetWidth;
534 pos.setX(0);
535 }
536
537 return QRectF(pos, QSizeF(toggleSize, toggleSize));
538 }
539
540 QPixmap KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
541 QWidget* widget)
542 {
543 QPixmap pixmap = KItemListWidget::createDragPixmap(option, widget);
544 if (m_layout != DetailsLayout) {
545 return pixmap;
546 }
547
548 // Only return the content of the text-column as pixmap
549 const int leftClip = m_pixmapPos.x();
550
551 const TextInfo* textInfo = m_textInfo.value("text");
552 const int rightClip = textInfo->pos.x() +
553 textInfo->staticText.size().width() +
554 2 * styleOption().padding;
555
556 QPixmap clippedPixmap(rightClip - leftClip + 1, pixmap.height());
557 clippedPixmap.fill(Qt::transparent);
558
559 QPainter painter(&clippedPixmap);
560 painter.drawPixmap(-leftClip, 0, pixmap);
561
562 return clippedPixmap;
563 }
564
565
566 KItemListWidgetInformant* KStandardItemListWidget::createInformant()
567 {
568 return new KStandardItemListWidgetInformant();
569 }
570
571 void KStandardItemListWidget::invalidateCache()
572 {
573 m_dirtyLayout = true;
574 m_dirtyContent = true;
575 }
576
577 void KStandardItemListWidget::refreshCache()
578 {
579 }
580
581 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray& role) const
582 {
583 Q_UNUSED(role);
584 return false;
585 }
586
587 bool KStandardItemListWidget::isHidden() const
588 {
589 return false;
590 }
591
592 QFont KStandardItemListWidget::customizedFont(const QFont& baseFont) const
593 {
594 return baseFont;
595 }
596
597 QPalette::ColorRole KStandardItemListWidget::normalTextColorRole() const
598 {
599 return QPalette::Text;
600 }
601
602 void KStandardItemListWidget::setTextColor(const QColor& color)
603 {
604 if (color != m_customTextColor) {
605 m_customTextColor = color;
606 updateAdditionalInfoTextColor();
607 update();
608 }
609 }
610
611 QColor KStandardItemListWidget::textColor() const
612 {
613 if (!isSelected()) {
614 if (m_isHidden) {
615 return m_additionalInfoTextColor;
616 } else if (m_customTextColor.isValid()) {
617 return m_customTextColor;
618 }
619 }
620
621 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
622 const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : normalTextColorRole();
623 return styleOption().palette.color(group, role);
624 }
625
626 void KStandardItemListWidget::setOverlay(const QPixmap& overlay)
627 {
628 m_overlay = overlay;
629 m_dirtyContent = true;
630 update();
631 }
632
633 QPixmap KStandardItemListWidget::overlay() const
634 {
635 return m_overlay;
636 }
637
638
639 QString KStandardItemListWidget::roleText(const QByteArray& role,
640 const QHash<QByteArray, QVariant>& values) const
641 {
642 return static_cast<const KStandardItemListWidgetInformant*>(informant())->roleText(role, values);
643 }
644
645 void KStandardItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
646 const QSet<QByteArray>& roles)
647 {
648 Q_UNUSED(current);
649
650 m_dirtyContent = true;
651
652 QSet<QByteArray> dirtyRoles;
653 if (roles.isEmpty()) {
654 dirtyRoles = visibleRoles().toSet();
655 } else {
656 dirtyRoles = roles;
657 }
658
659 // The URL might have changed (i.e., if the sort order of the items has
660 // been changed). Therefore, the "is cut" state must be updated.
661 KFileItemClipboard* clipboard = KFileItemClipboard::instance();
662 const KUrl itemUrl = data().value("url").value<KUrl>();
663 m_isCut = clipboard->isCut(itemUrl);
664
665 // The icon-state might depend from other roles and hence is
666 // marked as dirty whenever a role has been changed
667 dirtyRoles.insert("iconPixmap");
668 dirtyRoles.insert("iconName");
669
670 QSetIterator<QByteArray> it(dirtyRoles);
671 while (it.hasNext()) {
672 const QByteArray& role = it.next();
673 m_dirtyContentRoles.insert(role);
674 }
675 }
676
677 void KStandardItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
678 const QList<QByteArray>& previous)
679 {
680 Q_UNUSED(previous);
681 m_sortedVisibleRoles = current;
682 m_dirtyLayout = true;
683 }
684
685 void KStandardItemListWidget::columnWidthChanged(const QByteArray& role,
686 qreal current,
687 qreal previous)
688 {
689 Q_UNUSED(role);
690 Q_UNUSED(current);
691 Q_UNUSED(previous);
692 m_dirtyLayout = true;
693 }
694
695 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
696 const KItemListStyleOption& previous)
697 {
698 Q_UNUSED(current);
699 Q_UNUSED(previous);
700 updateAdditionalInfoTextColor();
701 m_dirtyLayout = true;
702 }
703
704 void KStandardItemListWidget::hoveredChanged(bool hovered)
705 {
706 Q_UNUSED(hovered);
707 m_dirtyLayout = true;
708 }
709
710 void KStandardItemListWidget::selectedChanged(bool selected)
711 {
712 Q_UNUSED(selected);
713 updateAdditionalInfoTextColor();
714 m_dirtyContent = true;
715 }
716
717 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
718 {
719 Q_UNUSED(current);
720 Q_UNUSED(previous);
721 m_dirtyLayout = true;
722 }
723
724 int KStandardItemListWidget::selectionLength(const QString& text) const
725 {
726 return text.length();
727 }
728
729 void KStandardItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
730 {
731 Q_UNUSED(previous);
732
733 QGraphicsView* parent = scene()->views()[0];
734 if (current.isEmpty() || !parent || current != "text") {
735 if (m_roleEditor) {
736 emit roleEditingCanceled(index(), current, data().value(current));
737
738 disconnect(m_roleEditor, SIGNAL(roleEditingCanceled(QByteArray,QVariant)),
739 this, SLOT(slotRoleEditingCanceled(QByteArray,QVariant)));
740 disconnect(m_roleEditor, SIGNAL(roleEditingFinished(QByteArray,QVariant)),
741 this, SLOT(slotRoleEditingFinished(QByteArray,QVariant)));
742
743 if (m_oldRoleEditor) {
744 m_oldRoleEditor->deleteLater();
745 }
746 m_oldRoleEditor = m_roleEditor;
747 m_roleEditor->hide();
748 m_roleEditor = 0;
749 }
750 return;
751 }
752
753 Q_ASSERT(!m_roleEditor);
754
755 const TextInfo* textInfo = m_textInfo.value("text");
756
757 m_roleEditor = new KItemListRoleEditor(parent);
758 m_roleEditor->setRole(current);
759 m_roleEditor->setFont(styleOption().font);
760
761 const QString text = data().value(current).toString();
762 m_roleEditor->setPlainText(text);
763
764 QTextOption textOption = textInfo->staticText.textOption();
765 m_roleEditor->document()->setDefaultTextOption(textOption);
766
767 const int textSelectionLength = selectionLength(text);
768
769 if (textSelectionLength > 0) {
770 QTextCursor cursor = m_roleEditor->textCursor();
771 cursor.movePosition(QTextCursor::StartOfBlock);
772 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, textSelectionLength);
773 m_roleEditor->setTextCursor(cursor);
774 }
775
776 connect(m_roleEditor, SIGNAL(roleEditingCanceled(QByteArray,QVariant)),
777 this, SLOT(slotRoleEditingCanceled(QByteArray,QVariant)));
778 connect(m_roleEditor, SIGNAL(roleEditingFinished(QByteArray,QVariant)),
779 this, SLOT(slotRoleEditingFinished(QByteArray,QVariant)));
780
781 // Adjust the geometry of the editor
782 QRectF rect = roleEditingRect(current);
783 const int frameWidth = m_roleEditor->frameWidth();
784 rect.adjust(-frameWidth, -frameWidth, frameWidth, frameWidth);
785 rect.translate(pos());
786 if (rect.right() > parent->width()) {
787 rect.setWidth(parent->width() - rect.left());
788 }
789 m_roleEditor->setGeometry(rect.toRect());
790 m_roleEditor->show();
791 m_roleEditor->setFocus();
792 }
793
794 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
795 {
796 if (m_roleEditor) {
797 setEditedRole(QByteArray());
798 Q_ASSERT(!m_roleEditor);
799 }
800
801 KItemListWidget::resizeEvent(event);
802
803 m_dirtyLayout = true;
804 }
805
806 void KStandardItemListWidget::showEvent(QShowEvent* event)
807 {
808 KItemListWidget::showEvent(event);
809
810 // Listen to changes of the clipboard to mark the item as cut/uncut
811 KFileItemClipboard* clipboard = KFileItemClipboard::instance();
812
813 const KUrl itemUrl = data().value("url").value<KUrl>();
814 m_isCut = clipboard->isCut(itemUrl);
815
816 connect(clipboard, SIGNAL(cutItemsChanged()),
817 this, SLOT(slotCutItemsChanged()));
818 }
819
820 void KStandardItemListWidget::hideEvent(QHideEvent* event)
821 {
822 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
823 this, SLOT(slotCutItemsChanged()));
824
825 KItemListWidget::hideEvent(event);
826 }
827
828 void KStandardItemListWidget::slotCutItemsChanged()
829 {
830 const KUrl itemUrl = data().value("url").value<KUrl>();
831 const bool isCut = KFileItemClipboard::instance()->isCut(itemUrl);
832 if (m_isCut != isCut) {
833 m_isCut = isCut;
834 m_pixmap = QPixmap();
835 m_dirtyContent = true;
836 update();
837 }
838 }
839
840 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray& role,
841 const QVariant& value)
842 {
843 closeRoleEditor();
844 emit roleEditingCanceled(index(), role, value);
845 setEditedRole(QByteArray());
846 }
847
848 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray& role,
849 const QVariant& value)
850 {
851 closeRoleEditor();
852 emit roleEditingFinished(index(), role, value);
853 setEditedRole(QByteArray());
854 }
855
856 void KStandardItemListWidget::triggerCacheRefreshing()
857 {
858 if ((!m_dirtyContent && !m_dirtyLayout) || index() < 0) {
859 return;
860 }
861
862 refreshCache();
863
864 const QHash<QByteArray, QVariant> values = data();
865 m_isExpandable = m_supportsItemExpanding && values["isExpandable"].toBool();
866 m_isHidden = isHidden();
867 m_customizedFont = customizedFont(styleOption().font);
868 m_customizedFontMetrics = QFontMetrics(m_customizedFont);
869
870 updateExpansionArea();
871 updateTextsCache();
872 updatePixmapCache();
873
874 m_dirtyLayout = false;
875 m_dirtyContent = false;
876 m_dirtyContentRoles.clear();
877 }
878
879 void KStandardItemListWidget::updateExpansionArea()
880 {
881 if (m_supportsItemExpanding) {
882 const QHash<QByteArray, QVariant> values = data();
883 const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
884 if (expandedParentsCount >= 0) {
885 const KItemListStyleOption& option = styleOption();
886 const qreal widgetHeight = size().height();
887 const qreal inc = (widgetHeight - option.iconSize) / 2;
888 const qreal x = expandedParentsCount * widgetHeight + inc;
889 const qreal y = inc;
890 m_expansionArea = QRectF(x, y, option.iconSize, option.iconSize);
891 return;
892 }
893 }
894
895 m_expansionArea = QRectF();
896 }
897
898 void KStandardItemListWidget::updatePixmapCache()
899 {
900 // Precondition: Requires already updated m_textPos values to calculate
901 // the remaining height when the alignment is vertical.
902
903 const QSizeF widgetSize = size();
904 const bool iconOnTop = (m_layout == IconsLayout);
905 const KItemListStyleOption& option = styleOption();
906 const qreal padding = option.padding;
907
908 const int maxIconWidth = iconOnTop ? widgetSize.width() - 2 * padding : option.iconSize;
909 const int maxIconHeight = option.iconSize;
910
911 const QHash<QByteArray, QVariant> values = data();
912
913 bool updatePixmap = (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight);
914 if (!updatePixmap && m_dirtyContent) {
915 updatePixmap = m_dirtyContentRoles.isEmpty()
916 || m_dirtyContentRoles.contains("iconPixmap")
917 || m_dirtyContentRoles.contains("iconName")
918 || m_dirtyContentRoles.contains("iconOverlays");
919 }
920
921 if (updatePixmap) {
922 m_pixmap = values["iconPixmap"].value<QPixmap>();
923 if (m_pixmap.isNull()) {
924 // Use the icon that fits to the MIME-type
925 QString iconName = values["iconName"].toString();
926 if (iconName.isEmpty()) {
927 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
928 // use a generic icon as fallback
929 iconName = QLatin1String("unknown");
930 }
931 const QStringList overlays = values["iconOverlays"].toStringList();
932 m_pixmap = pixmapForIcon(iconName, overlays, maxIconHeight);
933 } else if (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight) {
934 // A custom pixmap has been applied. Assure that the pixmap
935 // is scaled to the maximum available size.
936 KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight));
937 }
938
939 if (m_isCut) {
940 KIconEffect* effect = KIconLoader::global()->iconEffect();
941 m_pixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
942 }
943
944 if (m_isHidden) {
945 KIconEffect::semiTransparent(m_pixmap);
946 }
947
948 if (isSelected()) {
949 const QColor color = palette().brush(QPalette::Normal, QPalette::Highlight).color();
950 QImage image = m_pixmap.toImage();
951 KIconEffect::colorize(image, color, 0.8f);
952 m_pixmap = QPixmap::fromImage(image);
953 }
954 }
955
956 if (!m_overlay.isNull()) {
957 QPainter painter(&m_pixmap);
958 painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay);
959 }
960
961 int scaledIconSize = 0;
962 if (iconOnTop) {
963 const TextInfo* textInfo = m_textInfo.value("text");
964 scaledIconSize = static_cast<int>(textInfo->pos.y() - 2 * padding);
965 } else {
966 const int textRowsCount = (m_layout == CompactLayout) ? visibleRoles().count() : 1;
967 const qreal requiredTextHeight = textRowsCount * m_customizedFontMetrics.height();
968 scaledIconSize = (requiredTextHeight < maxIconHeight) ?
969 widgetSize.height() - 2 * padding : maxIconHeight;
970 }
971
972 const int maxScaledIconWidth = iconOnTop ? widgetSize.width() - 2 * padding : scaledIconSize;
973 const int maxScaledIconHeight = scaledIconSize;
974
975 m_scaledPixmapSize = m_pixmap.size();
976 m_scaledPixmapSize.scale(maxScaledIconWidth, maxScaledIconHeight, Qt::KeepAspectRatio);
977
978 if (iconOnTop) {
979 // Center horizontally and align on bottom within the icon-area
980 m_pixmapPos.setX((widgetSize.width() - m_scaledPixmapSize.width()) / 2);
981 m_pixmapPos.setY(padding + scaledIconSize - m_scaledPixmapSize.height());
982 } else {
983 // Center horizontally and vertically within the icon-area
984 const TextInfo* textInfo = m_textInfo.value("text");
985 m_pixmapPos.setX(textInfo->pos.x() - 2 * padding
986 - (scaledIconSize + m_scaledPixmapSize.width()) / 2);
987 m_pixmapPos.setY(padding
988 + (scaledIconSize - m_scaledPixmapSize.height()) / 2);
989 }
990
991 m_iconRect = QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize));
992
993 // Prepare the pixmap that is used when the item gets hovered
994 if (isHovered()) {
995 m_hoverPixmap = m_pixmap;
996 KIconEffect* effect = KIconLoader::global()->iconEffect();
997 // In the KIconLoader terminology, active = hover.
998 if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
999 m_hoverPixmap = effect->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
1000 } else {
1001 m_hoverPixmap = m_pixmap;
1002 }
1003 } else if (hoverOpacity() <= 0.0) {
1004 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1005 m_hoverPixmap = QPixmap();
1006 }
1007 }
1008
1009 void KStandardItemListWidget::updateTextsCache()
1010 {
1011 QTextOption textOption;
1012 switch (m_layout) {
1013 case IconsLayout:
1014 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
1015 textOption.setAlignment(Qt::AlignHCenter);
1016 break;
1017 case CompactLayout:
1018 case DetailsLayout:
1019 textOption.setAlignment(Qt::AlignLeft);
1020 textOption.setWrapMode(QTextOption::NoWrap);
1021 break;
1022 default:
1023 Q_ASSERT(false);
1024 break;
1025 }
1026
1027 qDeleteAll(m_textInfo);
1028 m_textInfo.clear();
1029 for (int i = 0; i < m_sortedVisibleRoles.count(); ++i) {
1030 TextInfo* textInfo = new TextInfo();
1031 textInfo->staticText.setTextFormat(Qt::PlainText);
1032 textInfo->staticText.setPerformanceHint(QStaticText::AggressiveCaching);
1033 textInfo->staticText.setTextOption(textOption);
1034 m_textInfo.insert(m_sortedVisibleRoles[i], textInfo);
1035 }
1036
1037 switch (m_layout) {
1038 case IconsLayout: updateIconsLayoutTextCache(); break;
1039 case CompactLayout: updateCompactLayoutTextCache(); break;
1040 case DetailsLayout: updateDetailsLayoutTextCache(); break;
1041 default: Q_ASSERT(false); break;
1042 }
1043
1044 const TextInfo* ratingTextInfo = m_textInfo.value("rating");
1045 if (ratingTextInfo) {
1046 // The text of the rating-role has been set to empty to get
1047 // replaced by a rating-image showing the rating as stars.
1048 const KItemListStyleOption& option = styleOption();
1049 QSizeF ratingSize = preferredRatingSize(option);
1050
1051 const qreal availableWidth = (m_layout == DetailsLayout)
1052 ? columnWidth("rating") - columnPadding(option)
1053 : size().width();
1054 if (ratingSize.width() > availableWidth) {
1055 ratingSize.rwidth() = availableWidth;
1056 }
1057 m_rating = QPixmap(ratingSize.toSize());
1058 m_rating.fill(Qt::transparent);
1059
1060 QPainter painter(&m_rating);
1061 const QRect rect(0, 0, m_rating.width(), m_rating.height());
1062 const int rating = data().value("rating").toInt();
1063 KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating);
1064 } else if (!m_rating.isNull()) {
1065 m_rating = QPixmap();
1066 }
1067 }
1068
1069 void KStandardItemListWidget::updateIconsLayoutTextCache()
1070 {
1071 // +------+
1072 // | Icon |
1073 // +------+
1074 //
1075 // Name role that
1076 // might get wrapped above
1077 // several lines.
1078 // Additional role 1
1079 // Additional role 2
1080
1081 const QHash<QByteArray, QVariant> values = data();
1082
1083 const KItemListStyleOption& option = styleOption();
1084 const qreal padding = option.padding;
1085 const qreal maxWidth = size().width() - 2 * padding;
1086 const qreal widgetHeight = size().height();
1087 const qreal lineSpacing = m_customizedFontMetrics.lineSpacing();
1088
1089 // Initialize properties for the "text" role. It will be used as anchor
1090 // for initializing the position of the other roles.
1091 TextInfo* nameTextInfo = m_textInfo.value("text");
1092 const QString nameText = KStringHandler::preProcessWrap(values["text"].toString());
1093 nameTextInfo->staticText.setText(nameText);
1094
1095 // Calculate the number of lines required for the name and the required width
1096 qreal nameWidth = 0;
1097 qreal nameHeight = 0;
1098 QTextLine line;
1099
1100 QTextLayout layout(nameTextInfo->staticText.text(), m_customizedFont);
1101 layout.setTextOption(nameTextInfo->staticText.textOption());
1102 layout.beginLayout();
1103 int nameLineIndex = 0;
1104 while ((line = layout.createLine()).isValid()) {
1105 line.setLineWidth(maxWidth);
1106 nameWidth = qMax(nameWidth, line.naturalTextWidth());
1107 nameHeight += line.height();
1108
1109 ++nameLineIndex;
1110 if (nameLineIndex == option.maxTextLines) {
1111 // The maximum number of textlines has been reached. If this is
1112 // the case provide an elided text if necessary.
1113 const int textLength = line.textStart() + line.textLength();
1114 if (textLength < nameText.length()) {
1115 // Elide the last line of the text
1116 QString lastTextLine = nameText.mid(line.textStart());
1117 lastTextLine = m_customizedFontMetrics.elidedText(lastTextLine,
1118 Qt::ElideRight,
1119 maxWidth);
1120 const QString elidedText = nameText.left(line.textStart()) + lastTextLine;
1121 nameTextInfo->staticText.setText(elidedText);
1122
1123 const qreal lastLineWidth = m_customizedFontMetrics.boundingRect(lastTextLine).width();
1124 nameWidth = qMax(nameWidth, lastLineWidth);
1125 }
1126 break;
1127 }
1128 }
1129 layout.endLayout();
1130
1131 // Use one line for each additional information
1132 const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
1133 nameTextInfo->staticText.setTextWidth(maxWidth);
1134 nameTextInfo->pos = QPointF(padding, widgetHeight -
1135 nameHeight -
1136 additionalRolesCount * lineSpacing -
1137 padding);
1138 m_textRect = QRectF(padding + (maxWidth - nameWidth) / 2,
1139 nameTextInfo->pos.y(),
1140 nameWidth,
1141 nameHeight);
1142
1143 // Calculate the position for each additional information
1144 qreal y = nameTextInfo->pos.y() + nameHeight;
1145 foreach (const QByteArray& role, m_sortedVisibleRoles) {
1146 if (role == "text") {
1147 continue;
1148 }
1149
1150 const QString text = roleText(role, values);
1151 TextInfo* textInfo = m_textInfo.value(role);
1152 textInfo->staticText.setText(text);
1153
1154 qreal requiredWidth = 0;
1155
1156 QTextLayout layout(text, m_customizedFont);
1157 QTextOption textOption;
1158 textOption.setWrapMode(QTextOption::NoWrap);
1159 layout.setTextOption(textOption);
1160
1161 layout.beginLayout();
1162 QTextLine textLine = layout.createLine();
1163 if (textLine.isValid()) {
1164 textLine.setLineWidth(maxWidth);
1165 requiredWidth = textLine.naturalTextWidth();
1166 if (requiredWidth > maxWidth) {
1167 const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
1168 textInfo->staticText.setText(elidedText);
1169 requiredWidth = m_customizedFontMetrics.width(elidedText);
1170 } else if (role == "rating") {
1171 // Use the width of the rating pixmap, because the rating text is empty.
1172 requiredWidth = m_rating.width();
1173 }
1174 }
1175 layout.endLayout();
1176
1177 textInfo->pos = QPointF(padding, y);
1178 textInfo->staticText.setTextWidth(maxWidth);
1179
1180 const QRectF textRect(padding + (maxWidth - requiredWidth) / 2, y, requiredWidth, lineSpacing);
1181 m_textRect |= textRect;
1182
1183 y += lineSpacing;
1184 }
1185
1186 // Add a padding to the text rectangle
1187 m_textRect.adjust(-padding, -padding, padding, padding);
1188 }
1189
1190 void KStandardItemListWidget::updateCompactLayoutTextCache()
1191 {
1192 // +------+ Name role
1193 // | Icon | Additional role 1
1194 // +------+ Additional role 2
1195
1196 const QHash<QByteArray, QVariant> values = data();
1197
1198 const KItemListStyleOption& option = styleOption();
1199 const qreal widgetHeight = size().height();
1200 const qreal lineSpacing = m_customizedFontMetrics.lineSpacing();
1201 const qreal textLinesHeight = qMax(visibleRoles().count(), 1) * lineSpacing;
1202 const int scaledIconSize = (textLinesHeight < option.iconSize) ? widgetHeight - 2 * option.padding : option.iconSize;
1203
1204 qreal maximumRequiredTextWidth = 0;
1205 const qreal x = option.padding * 3 + scaledIconSize;
1206 qreal y = qRound((widgetHeight - textLinesHeight) / 2);
1207 const qreal maxWidth = size().width() - x - option.padding;
1208 foreach (const QByteArray& role, m_sortedVisibleRoles) {
1209 const QString text = roleText(role, values);
1210 TextInfo* textInfo = m_textInfo.value(role);
1211 textInfo->staticText.setText(text);
1212
1213 qreal requiredWidth = m_customizedFontMetrics.width(text);
1214 if (requiredWidth > maxWidth) {
1215 requiredWidth = maxWidth;
1216 const QString elidedText = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, maxWidth);
1217 textInfo->staticText.setText(elidedText);
1218 }
1219
1220 textInfo->pos = QPointF(x, y);
1221 textInfo->staticText.setTextWidth(maxWidth);
1222
1223 maximumRequiredTextWidth = qMax(maximumRequiredTextWidth, requiredWidth);
1224
1225 y += lineSpacing;
1226 }
1227
1228 m_textRect = QRectF(x - 2 * option.padding, 0, maximumRequiredTextWidth + 3 * option.padding, widgetHeight);
1229 }
1230
1231 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1232 {
1233 // Precondition: Requires already updated m_expansionArea
1234 // to determine the left position.
1235
1236 // +------+
1237 // | Icon | Name role Additional role 1 Additional role 2
1238 // +------+
1239 m_textRect = QRectF();
1240
1241 const KItemListStyleOption& option = styleOption();
1242 const QHash<QByteArray, QVariant> values = data();
1243
1244 const qreal widgetHeight = size().height();
1245 const int scaledIconSize = widgetHeight - 2 * option.padding;
1246 const int fontHeight = m_customizedFontMetrics.height();
1247
1248 const qreal columnWidthInc = columnPadding(option);
1249 qreal firstColumnInc = scaledIconSize;
1250 if (m_supportsItemExpanding) {
1251 firstColumnInc += (m_expansionArea.left() + m_expansionArea.right() + widgetHeight) / 2;
1252 } else {
1253 firstColumnInc += option.padding;
1254 }
1255
1256 qreal x = firstColumnInc;
1257 const qreal y = qMax(qreal(option.padding), (widgetHeight - fontHeight) / 2);
1258
1259 foreach (const QByteArray& role, m_sortedVisibleRoles) {
1260 QString text = roleText(role, values);
1261
1262 // Elide the text in case it does not fit into the available column-width
1263 qreal requiredWidth = m_customizedFontMetrics.width(text);
1264 const qreal roleWidth = columnWidth(role);
1265 qreal availableTextWidth = roleWidth - columnWidthInc;
1266
1267 const bool isTextRole = (role == "text");
1268 if (isTextRole) {
1269 availableTextWidth -= firstColumnInc;
1270 }
1271
1272 if (requiredWidth > availableTextWidth) {
1273 text = m_customizedFontMetrics.elidedText(text, Qt::ElideRight, availableTextWidth);
1274 requiredWidth = m_customizedFontMetrics.width(text);
1275 }
1276
1277 TextInfo* textInfo = m_textInfo.value(role);
1278 textInfo->staticText.setText(text);
1279 textInfo->pos = QPointF(x + columnWidthInc / 2, y);
1280 x += roleWidth;
1281
1282 if (isTextRole) {
1283 const qreal textWidth = option.extendedSelectionRegion
1284 ? size().width() - textInfo->pos.x()
1285 : requiredWidth + 2 * option.padding;
1286 m_textRect = QRectF(textInfo->pos.x() - 2 * option.padding, 0,
1287 textWidth + option.padding, size().height());
1288
1289 // The column after the name should always be aligned on the same x-position independent
1290 // from the expansion-level shown in the name column
1291 x -= firstColumnInc;
1292 } else if (isRoleRightAligned(role)) {
1293 textInfo->pos.rx() += roleWidth - requiredWidth - columnWidthInc;
1294 }
1295 }
1296 }
1297
1298 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1299 {
1300 QColor c1;
1301 if (m_customTextColor.isValid()) {
1302 c1 = m_customTextColor;
1303 } else if (isSelected() && m_layout != DetailsLayout) {
1304 c1 = styleOption().palette.highlightedText().color();
1305 } else {
1306 c1 = styleOption().palette.text().color();
1307 }
1308
1309 // For the color of the additional info the inactive text color
1310 // is not used as this might lead to unreadable text for some color schemes. Instead
1311 // the text color c1 is slightly mixed with the background color.
1312 const QColor c2 = styleOption().palette.base().color();
1313 const int p1 = 70;
1314 const int p2 = 100 - p1;
1315 m_additionalInfoTextColor = QColor((c1.red() * p1 + c2.red() * p2) / 100,
1316 (c1.green() * p1 + c2.green() * p2) / 100,
1317 (c1.blue() * p1 + c2.blue() * p2) / 100);
1318 }
1319
1320 void KStandardItemListWidget::drawPixmap(QPainter* painter, const QPixmap& pixmap)
1321 {
1322 if (m_scaledPixmapSize != pixmap.size()) {
1323 QPixmap scaledPixmap = pixmap;
1324 KPixmapModifier::scale(scaledPixmap, m_scaledPixmapSize);
1325 painter->drawPixmap(m_pixmapPos, scaledPixmap);
1326
1327 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1328 painter->setPen(Qt::blue);
1329 painter->drawRect(QRectF(m_pixmapPos, QSizeF(m_scaledPixmapSize)));
1330 #endif
1331 } else {
1332 painter->drawPixmap(m_pixmapPos, pixmap);
1333 }
1334 }
1335
1336 void KStandardItemListWidget::drawSiblingsInformation(QPainter* painter)
1337 {
1338 const int siblingSize = size().height();
1339 const int x = (m_expansionArea.left() + m_expansionArea.right() - siblingSize) / 2;
1340 QRect siblingRect(x, 0, siblingSize, siblingSize);
1341
1342 QStyleOption option;
1343 option.palette.setColor(QPalette::Text, option.palette.color(normalTextColorRole()));
1344 bool isItemSibling = true;
1345
1346 const QBitArray siblings = siblingsInformation();
1347 for (int i = siblings.count() - 1; i >= 0; --i) {
1348 option.rect = siblingRect;
1349 option.state = siblings.at(i) ? QStyle::State_Sibling : QStyle::State_None;
1350
1351 if (isItemSibling) {
1352 option.state |= QStyle::State_Item;
1353 if (m_isExpandable) {
1354 option.state |= QStyle::State_Children;
1355 }
1356 if (data()["isExpanded"].toBool()) {
1357 option.state |= QStyle::State_Open;
1358 }
1359 isItemSibling = false;
1360 }
1361
1362 style()->drawPrimitive(QStyle::PE_IndicatorBranch, &option, painter);
1363
1364 siblingRect.translate(-siblingRect.width(), 0);
1365 }
1366 }
1367
1368 QRectF KStandardItemListWidget::roleEditingRect(const QByteArray& role) const
1369 {
1370 const TextInfo* textInfo = m_textInfo.value(role);
1371 if (!textInfo) {
1372 return QRectF();
1373 }
1374
1375 QRectF rect(textInfo->pos, textInfo->staticText.size());
1376 if (m_layout == DetailsLayout) {
1377 rect.setWidth(columnWidth(role) - rect.x());
1378 }
1379
1380 return rect;
1381 }
1382
1383 void KStandardItemListWidget::closeRoleEditor()
1384 {
1385 disconnect(m_roleEditor, SIGNAL(roleEditingCanceled(QByteArray,QVariant)),
1386 this, SLOT(slotRoleEditingCanceled(QByteArray,QVariant)));
1387 disconnect(m_roleEditor, SIGNAL(roleEditingFinished(QByteArray,QVariant)),
1388 this, SLOT(slotRoleEditingFinished(QByteArray,QVariant)));
1389
1390 if (m_roleEditor->hasFocus()) {
1391 // If the editing was not ended by a FocusOut event, we have
1392 // to transfer the keyboard focus back to the KItemListContainer.
1393 scene()->views()[0]->parentWidget()->setFocus();
1394 }
1395
1396 if (m_oldRoleEditor) {
1397 m_oldRoleEditor->deleteLater();
1398 }
1399 m_oldRoleEditor = m_roleEditor;
1400 m_roleEditor->hide();
1401 m_roleEditor = 0;
1402 }
1403
1404 QPixmap KStandardItemListWidget::pixmapForIcon(const QString& name, const QStringList& overlays, int size)
1405 {
1406 const QString key = "KStandardItemListWidget:" % name % ":" % overlays.join(":") % ":" % QString::number(size);
1407 QPixmap pixmap;
1408
1409 if (!QPixmapCache::find(key, pixmap)) {
1410 const KIcon icon(name);
1411
1412 int requestedSize;
1413 if (size <= KIconLoader::SizeSmall) {
1414 requestedSize = KIconLoader::SizeSmall;
1415 } else if (size <= KIconLoader::SizeSmallMedium) {
1416 requestedSize = KIconLoader::SizeSmallMedium;
1417 } else if (size <= KIconLoader::SizeMedium) {
1418 requestedSize = KIconLoader::SizeMedium;
1419 } else if (size <= KIconLoader::SizeLarge) {
1420 requestedSize = KIconLoader::SizeLarge;
1421 } else if (size <= KIconLoader::SizeHuge) {
1422 requestedSize = KIconLoader::SizeHuge;
1423 } else if (size <= KIconLoader::SizeEnormous) {
1424 requestedSize = KIconLoader::SizeEnormous;
1425 } else if (size <= KIconLoader::SizeEnormous * 2) {
1426 requestedSize = KIconLoader::SizeEnormous * 2;
1427 } else {
1428 requestedSize = size;
1429 }
1430
1431 pixmap = icon.pixmap(requestedSize, requestedSize);
1432 if (requestedSize != size) {
1433 KPixmapModifier::scale(pixmap, QSize(size, size));
1434 }
1435
1436 // Strangely KFileItem::overlays() returns empty string-values, so
1437 // we need to check first whether an overlay must be drawn at all.
1438 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1439 // assumes that an overlay will be drawn and has some additional
1440 // setup time.
1441 foreach (const QString& overlay, overlays) {
1442 if (!overlay.isEmpty()) {
1443 // There is at least one overlay, draw all overlays above m_pixmap
1444 // and cancel the check
1445 KIconLoader::global()->drawOverlays(overlays, pixmap, KIconLoader::Desktop);
1446 break;
1447 }
1448 }
1449
1450 QPixmapCache::insert(key, pixmap);
1451 }
1452
1453 return pixmap;
1454 }
1455
1456 QSizeF KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption& option)
1457 {
1458 const qreal height = option.fontMetrics.ascent();
1459 return QSizeF(height * 5, height);
1460 }
1461
1462 qreal KStandardItemListWidget::columnPadding(const KItemListStyleOption& option)
1463 {
1464 return option.padding * 6;
1465 }
1466
1467 #include "kstandarditemlistwidget.moc"