2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kstandarditemlistwidget.h"
9 #include "kfileitemlistview.h"
10 #include "kfileitemmodel.h"
11 #include "private/kfileitemclipboard.h"
12 #include "private/kitemlistroleeditor.h"
13 #include "private/kpixmapmodifier.h"
15 #include <KIconEffect>
16 #include <KIconLoader>
17 #include <KRatingPainter>
18 #include <KStringHandler>
20 #include <QGraphicsScene>
21 #include <QGraphicsSceneResizeEvent>
22 #include <QGraphicsView>
23 #include <QGuiApplication>
24 #include <QPixmapCache>
25 #include <QStyleOption>
27 // #define KSTANDARDITEMLISTWIDGET_DEBUG
29 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
30 KItemListWidgetInformant()
34 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
38 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
40 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
41 case KStandardItemListView::IconsLayout
:
42 calculateIconsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
45 case KStandardItemListView::CompactLayout
:
46 calculateCompactLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
49 case KStandardItemListView::DetailsLayout
:
50 calculateDetailsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
59 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
61 const KItemListView
* view
) const
63 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
64 const KItemListStyleOption
& option
= view
->styleOption();
66 const QString text
= roleText(role
, values
);
67 qreal width
= KStandardItemListWidget::columnPadding(option
);
69 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
70 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
72 if (role
== "rating") {
73 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
75 // If current item is a link, we use the customized link font metrics instead of the normal font metrics.
76 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
78 width
+= fontMetrics
.horizontalAdvance(text
);
81 if (view
->supportsItemExpanding()) {
82 // Increase the width by the expansion-toggle and the current expansion level
83 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
84 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, fontMetrics
.height());
85 width
+= (expandedParentsCount
+ 1) * height
;
88 // Increase the width by the required space for the icon
89 width
+= option
.padding
* 2 + option
.iconSize
;
96 QString
KStandardItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
98 return view
->model()->data(index
).value("text").toString();
101 bool KStandardItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
108 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
109 const QHash
<QByteArray
, QVariant
>& values
) const
111 if (role
== "rating") {
112 // Always use an empty text, as the rating is shown by the image m_rating.
115 return values
.value(role
).toString();
118 QFont
KStandardItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
123 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
125 const KItemListStyleOption
& option
= view
->styleOption();
126 const QFont
& normalFont
= option
.font
;
127 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
129 const qreal itemWidth
= view
->itemSize().width();
130 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
131 const qreal additionalRolesSpacing
= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
132 const qreal spacingAndIconHeight
= option
.iconSize
+ option
.padding
* 3;
134 const QFont linkFont
= customizedFontForLinks(normalFont
);
136 QTextOption
textOption(Qt::AlignHCenter
);
137 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
139 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
140 if (logicalHeightHints
.at(index
) > 0.0) {
144 // If the current item is a link, we use the customized link font instead of the normal font.
145 const QFont
& font
= itemIsLink(index
, view
) ? linkFont
: normalFont
;
147 const QString
& text
= KStringHandler::preProcessWrap(itemText(index
, view
));
149 // Calculate the number of lines required for wrapping the name
150 qreal textHeight
= 0;
151 QTextLayout
layout(text
, font
);
152 layout
.setTextOption(textOption
);
153 layout
.beginLayout();
156 while ((line
= layout
.createLine()).isValid()) {
157 line
.setLineWidth(maxWidth
);
158 line
.naturalTextWidth();
159 textHeight
+= line
.height();
162 if (lineCount
== option
.maxTextLines
) {
168 // Add one line for each additional information
169 textHeight
+= additionalRolesSpacing
;
171 logicalHeightHints
[index
] = textHeight
+ spacingAndIconHeight
;
174 logicalWidthHint
= itemWidth
;
177 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
179 const KItemListStyleOption
& option
= view
->styleOption();
180 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
181 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
183 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
184 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
185 const qreal maxWidth
= option
.maxTextWidth
;
186 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
187 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * normalFontMetrics
.lineSpacing());
189 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
191 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
192 if (logicalHeightHints
.at(index
) > 0.0) {
196 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
197 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
199 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
200 // to show all roles without horizontal clipping.
201 qreal maximumRequiredWidth
= 0.0;
203 if (showOnlyTextRole
) {
204 maximumRequiredWidth
= fontMetrics
.horizontalAdvance(itemText(index
, view
));
206 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
207 for (const QByteArray
& role
: visibleRoles
) {
208 const QString
& text
= roleText(role
, values
);
209 const qreal requiredWidth
= fontMetrics
.horizontalAdvance(text
);
210 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
214 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
215 if (maxWidth
> 0 && width
> maxWidth
) {
219 logicalHeightHints
[index
] = width
;
222 logicalWidthHint
= height
;
225 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
227 const KItemListStyleOption
& option
= view
->styleOption();
228 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
229 logicalHeightHints
.fill(height
);
230 logicalWidthHint
= -1.0;
233 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
234 KItemListWidget(informant
, parent
),
239 m_customizedFontMetrics(m_customizedFont
),
240 m_isExpandable(false),
241 m_supportsItemExpanding(false),
243 m_dirtyContent(true),
244 m_dirtyContentRoles(),
245 m_layout(IconsLayout
),
248 m_scaledPixmapSize(),
252 m_sortedVisibleRoles(),
255 m_additionalInfoTextColor(),
258 m_roleEditor(nullptr),
259 m_oldRoleEditor(nullptr)
263 KStandardItemListWidget::~KStandardItemListWidget()
265 qDeleteAll(m_textInfo
);
269 m_roleEditor
->deleteLater();
272 if (m_oldRoleEditor
) {
273 m_oldRoleEditor
->deleteLater();
277 void KStandardItemListWidget::setLayout(Layout layout
)
279 if (m_layout
!= layout
) {
281 m_dirtyLayout
= true;
282 updateAdditionalInfoTextColor();
287 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
292 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
294 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
295 m_supportsItemExpanding
= supportsItemExpanding
;
296 m_dirtyLayout
= true;
301 bool KStandardItemListWidget::supportsItemExpanding() const
303 return m_supportsItemExpanding
;
306 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
308 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
310 KItemListWidget::paint(painter
, option
, widget
);
312 if (!m_expansionArea
.isEmpty()) {
313 drawSiblingsInformation(painter
);
316 const KItemListStyleOption
& itemListStyleOption
= styleOption();
317 if (isHovered() && !m_pixmap
.isNull()) {
318 if (hoverOpacity() < 1.0) {
320 * Linear interpolation between m_pixmap and m_hoverPixmap.
322 * Note that this cannot be achieved by painting m_hoverPixmap over
323 * m_pixmap, even if the opacities are adjusted. For details see
324 * https://git.reviewboard.kde.org/r/109614/
326 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
327 QPixmap
pixmap1(m_pixmap
.size());
328 pixmap1
.setDevicePixelRatio(m_pixmap
.devicePixelRatio());
329 pixmap1
.fill(Qt::transparent
);
331 QPainter
p(&pixmap1
);
332 p
.setOpacity(1.0 - hoverOpacity());
333 p
.drawPixmap(0, 0, m_pixmap
);
336 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
337 QPixmap
pixmap2(pixmap1
.size());
338 pixmap2
.setDevicePixelRatio(pixmap1
.devicePixelRatio());
339 pixmap2
.fill(Qt::transparent
);
341 QPainter
p(&pixmap2
);
342 p
.setOpacity(hoverOpacity());
343 p
.drawPixmap(0, 0, m_hoverPixmap
);
346 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
347 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
348 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
350 QPainter
p(&pixmap1
);
351 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
352 p
.drawPixmap(0, 0, pixmap2
);
355 // Finally paint pixmap1 on the widget
356 drawPixmap(painter
, pixmap1
);
358 drawPixmap(painter
, m_hoverPixmap
);
360 } else if (!m_pixmap
.isNull()) {
361 drawPixmap(painter
, m_pixmap
);
364 painter
->setFont(m_customizedFont
);
365 painter
->setPen(textColor());
366 const TextInfo
* textInfo
= m_textInfo
.value("text");
369 // It seems that we can end up here even if m_textInfo does not contain
370 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
371 // this can only happen if the index is negative. This can happen when
372 // the item is about to be removed, see KItemListView::slotItemsRemoved().
373 // TODO: try to reproduce the crash and find a better fix.
377 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
379 bool clipAdditionalInfoBounds
= false;
380 if (m_supportsItemExpanding
) {
381 // Prevent a possible overlapping of the additional-information texts
382 // with the icon. This can happen if the user has minimized the width
383 // of the name-column to a very small value.
384 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
385 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
386 clipAdditionalInfoBounds
= true;
388 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
392 painter
->setPen(m_additionalInfoTextColor
);
393 painter
->setFont(m_customizedFont
);
395 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
396 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
397 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
400 if (!m_rating
.isNull()) {
401 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
402 QPointF pos
= ratingTextInfo
->pos
;
403 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
404 if (align
& Qt::AlignHCenter
) {
405 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
407 painter
->drawPixmap(pos
, m_rating
);
410 if (clipAdditionalInfoBounds
) {
414 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
415 painter
->setBrush(Qt::NoBrush
);
416 painter
->setPen(Qt::green
);
417 painter
->drawRect(m_iconRect
);
419 painter
->setPen(Qt::blue
);
420 painter
->drawRect(m_textRect
);
422 painter
->setPen(Qt::red
);
423 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
424 painter
->drawRect(rect());
428 QRectF
KStandardItemListWidget::iconRect() const
430 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
434 QRectF
KStandardItemListWidget::textRect() const
436 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
440 QRectF
KStandardItemListWidget::textFocusRect() const
442 // In the compact- and details-layout a larger textRect() is returned to be aligned
443 // with the iconRect(). This is useful to have a larger selection/hover-area
444 // when having a quite large icon size but only one line of text. Still the
445 // focus rectangle should be shown as narrow as possible around the text.
447 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
450 case CompactLayout
: {
451 QRectF rect
= m_textRect
;
452 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
453 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
454 rect
.setTop(topText
->pos
.y());
455 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
459 case DetailsLayout
: {
460 QRectF rect
= m_textRect
;
461 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
462 rect
.setTop(textInfo
->pos
.y());
463 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
465 const KItemListStyleOption
& option
= styleOption();
466 if (option
.extendedSelectionRegion
) {
467 const QString text
= textInfo
->staticText
.text();
468 rect
.setWidth(m_customizedFontMetrics
.horizontalAdvance(text
) + 2 * option
.padding
);
481 QRectF
KStandardItemListWidget::selectionRect() const
483 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
490 case DetailsLayout
: {
491 const int padding
= styleOption().padding
;
492 QRectF adjustedIconRect
= iconRect().adjusted(-padding
, -padding
, padding
, padding
);
493 return adjustedIconRect
| m_textRect
;
504 QRectF
KStandardItemListWidget::expansionToggleRect() const
506 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
507 return m_isExpandable
? m_expansionArea
: QRectF();
510 QRectF
KStandardItemListWidget::selectionToggleRect() const
512 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
514 const int iconHeight
= styleOption().iconSize
;
516 int toggleSize
= KIconLoader::SizeSmall
;
517 if (iconHeight
>= KIconLoader::SizeEnormous
) {
518 toggleSize
= KIconLoader::SizeMedium
;
519 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
520 toggleSize
= KIconLoader::SizeSmallMedium
;
523 QPointF pos
= iconRect().topLeft();
525 // If the selection toggle has a very small distance to the
526 // widget borders, the size of the selection toggle will get
527 // increased to prevent an accidental clicking of the item
528 // when trying to hit the toggle.
529 const int widgetHeight
= size().height();
530 const int widgetWidth
= size().width();
531 const int minMargin
= 2;
533 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
534 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
535 toggleSize
= widgetHeight
;
538 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
539 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
540 toggleSize
= widgetWidth
;
544 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
547 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
550 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
551 if (m_layout
!= DetailsLayout
) {
555 // Only return the content of the text-column as pixmap
556 const int leftClip
= m_pixmapPos
.x();
558 const TextInfo
* textInfo
= m_textInfo
.value("text");
559 const int rightClip
= textInfo
->pos
.x() +
560 textInfo
->staticText
.size().width() +
561 2 * styleOption().padding
;
563 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
564 clippedPixmap
.fill(Qt::transparent
);
566 QPainter
painter(&clippedPixmap
);
567 painter
.drawPixmap(-leftClip
, 0, pixmap
);
569 return clippedPixmap
;
573 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
575 return new KStandardItemListWidgetInformant();
578 void KStandardItemListWidget::invalidateCache()
580 m_dirtyLayout
= true;
581 m_dirtyContent
= true;
584 void KStandardItemListWidget::refreshCache()
588 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
594 bool KStandardItemListWidget::isHidden() const
599 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
604 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
606 return QPalette::Text
;
609 void KStandardItemListWidget::setTextColor(const QColor
& color
)
611 if (color
!= m_customTextColor
) {
612 m_customTextColor
= color
;
613 updateAdditionalInfoTextColor();
618 QColor
KStandardItemListWidget::textColor() const
622 return m_additionalInfoTextColor
;
623 } else if (m_customTextColor
.isValid()) {
624 return m_customTextColor
;
628 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
629 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
630 return styleOption().palette
.color(group
, role
);
633 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
636 m_dirtyContent
= true;
640 QPixmap
KStandardItemListWidget::overlay() const
646 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
647 const QHash
<QByteArray
, QVariant
>& values
) const
649 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
652 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
653 const QSet
<QByteArray
>& roles
)
657 m_dirtyContent
= true;
659 QSet
<QByteArray
> dirtyRoles
;
660 if (roles
.isEmpty()) {
661 const auto visibleRoles
= this->visibleRoles();
662 dirtyRoles
= QSet
<QByteArray
>(visibleRoles
.constBegin(), visibleRoles
.constEnd());
667 // The URL might have changed (i.e., if the sort order of the items has
668 // been changed). Therefore, the "is cut" state must be updated.
669 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
670 const QUrl itemUrl
= data().value("url").toUrl();
671 m_isCut
= clipboard
->isCut(itemUrl
);
673 // The icon-state might depend from other roles and hence is
674 // marked as dirty whenever a role has been changed
675 dirtyRoles
.insert("iconPixmap");
676 dirtyRoles
.insert("iconName");
678 QSetIterator
<QByteArray
> it(dirtyRoles
);
679 while (it
.hasNext()) {
680 const QByteArray
& role
= it
.next();
681 m_dirtyContentRoles
.insert(role
);
685 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
686 const QList
<QByteArray
>& previous
)
689 m_sortedVisibleRoles
= current
;
690 m_dirtyLayout
= true;
693 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
700 m_dirtyLayout
= true;
703 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
704 const KItemListStyleOption
& previous
)
708 updateAdditionalInfoTextColor();
709 m_dirtyLayout
= true;
712 void KStandardItemListWidget::hoveredChanged(bool hovered
)
715 m_dirtyLayout
= true;
718 void KStandardItemListWidget::selectedChanged(bool selected
)
721 updateAdditionalInfoTextColor();
722 m_dirtyContent
= true;
725 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
729 m_dirtyLayout
= true;
732 int KStandardItemListWidget::selectionLength(const QString
& text
) const
734 return text
.length();
737 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
741 QGraphicsView
* parent
= scene()->views()[0];
742 if (current
.isEmpty() || !parent
|| current
!= "text") {
744 Q_EMIT
roleEditingCanceled(index(), current
, data().value(current
));
746 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
747 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
748 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
749 this, &KStandardItemListWidget::slotRoleEditingFinished
);
751 if (m_oldRoleEditor
) {
752 m_oldRoleEditor
->deleteLater();
754 m_oldRoleEditor
= m_roleEditor
;
755 m_roleEditor
->hide();
756 m_roleEditor
= nullptr;
761 Q_ASSERT(!m_roleEditor
);
763 const TextInfo
* textInfo
= m_textInfo
.value("text");
765 m_roleEditor
= new KItemListRoleEditor(parent
);
766 m_roleEditor
->setRole(current
);
767 m_roleEditor
->setFont(styleOption().font
);
769 const QString text
= data().value(current
).toString();
770 m_roleEditor
->setPlainText(text
);
772 QTextOption textOption
= textInfo
->staticText
.textOption();
773 m_roleEditor
->document()->setDefaultTextOption(textOption
);
775 const int textSelectionLength
= selectionLength(text
);
777 if (textSelectionLength
> 0) {
778 QTextCursor cursor
= m_roleEditor
->textCursor();
779 cursor
.movePosition(QTextCursor::StartOfBlock
);
780 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
781 m_roleEditor
->setTextCursor(cursor
);
784 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
785 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
786 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
787 this, &KStandardItemListWidget::slotRoleEditingFinished
);
789 // Adjust the geometry of the editor
790 QRectF rect
= roleEditingRect(current
);
791 const int frameWidth
= m_roleEditor
->frameWidth();
792 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
793 rect
.translate(pos());
794 if (rect
.right() > parent
->width()) {
795 rect
.setWidth(parent
->width() - rect
.left());
797 m_roleEditor
->setGeometry(rect
.toRect());
798 m_roleEditor
->show();
799 m_roleEditor
->setFocus();
802 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
805 setEditedRole(QByteArray());
806 Q_ASSERT(!m_roleEditor
);
809 KItemListWidget::resizeEvent(event
);
811 m_dirtyLayout
= true;
814 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
816 KItemListWidget::showEvent(event
);
818 // Listen to changes of the clipboard to mark the item as cut/uncut
819 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
821 const QUrl itemUrl
= data().value("url").toUrl();
822 m_isCut
= clipboard
->isCut(itemUrl
);
824 connect(clipboard
, &KFileItemClipboard::cutItemsChanged
,
825 this, &KStandardItemListWidget::slotCutItemsChanged
);
828 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
830 disconnect(KFileItemClipboard::instance(), &KFileItemClipboard::cutItemsChanged
,
831 this, &KStandardItemListWidget::slotCutItemsChanged
);
833 KItemListWidget::hideEvent(event
);
836 bool KStandardItemListWidget::event(QEvent
*event
)
838 if (event
->type() == QEvent::WindowDeactivate
|| event
->type() == QEvent::WindowActivate
839 || event
->type() == QEvent::PaletteChange
) {
840 m_dirtyContent
= true;
843 return KItemListWidget::event(event
);
846 void KStandardItemListWidget::finishRoleEditing()
848 if (!editedRole().isEmpty() && m_roleEditor
) {
849 slotRoleEditingFinished(editedRole(), KIO::encodeFileName(m_roleEditor
->toPlainText()));
853 void KStandardItemListWidget::slotCutItemsChanged()
855 const QUrl itemUrl
= data().value("url").toUrl();
856 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
857 if (m_isCut
!= isCut
) {
859 m_pixmap
= QPixmap();
860 m_dirtyContent
= true;
865 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
866 const QVariant
& value
)
869 Q_EMIT
roleEditingCanceled(index(), role
, value
);
870 setEditedRole(QByteArray());
873 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
874 const QVariant
& value
)
877 Q_EMIT
roleEditingFinished(index(), role
, value
);
878 setEditedRole(QByteArray());
881 void KStandardItemListWidget::triggerCacheRefreshing()
883 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
889 const QHash
<QByteArray
, QVariant
> values
= data();
890 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
891 m_isHidden
= isHidden();
892 m_customizedFont
= customizedFont(styleOption().font
);
893 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
895 updateExpansionArea();
899 m_dirtyLayout
= false;
900 m_dirtyContent
= false;
901 m_dirtyContentRoles
.clear();
904 void KStandardItemListWidget::updateExpansionArea()
906 if (m_supportsItemExpanding
) {
907 const QHash
<QByteArray
, QVariant
> values
= data();
908 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
909 if (expandedParentsCount
>= 0) {
910 const KItemListStyleOption
& option
= styleOption();
911 const qreal widgetHeight
= size().height();
912 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
913 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
915 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
920 m_expansionArea
= QRectF();
923 void KStandardItemListWidget::updatePixmapCache()
925 // Precondition: Requires already updated m_textPos values to calculate
926 // the remaining height when the alignment is vertical.
928 const QSizeF widgetSize
= size();
929 const bool iconOnTop
= (m_layout
== IconsLayout
);
930 const KItemListStyleOption
& option
= styleOption();
931 const qreal padding
= option
.padding
;
933 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
934 const int maxIconHeight
= option
.iconSize
;
936 const QHash
<QByteArray
, QVariant
> values
= data();
938 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
939 if (!updatePixmap
&& m_dirtyContent
) {
940 updatePixmap
= m_dirtyContentRoles
.isEmpty()
941 || m_dirtyContentRoles
.contains("iconPixmap")
942 || m_dirtyContentRoles
.contains("iconName")
943 || m_dirtyContentRoles
.contains("iconOverlays");
947 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
948 if (m_pixmap
.isNull()) {
949 // Use the icon that fits to the MIME-type
950 QString iconName
= values
["iconName"].toString();
951 if (iconName
.isEmpty()) {
952 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
953 // use a generic icon as fallback
954 iconName
= QStringLiteral("unknown");
956 const QStringList overlays
= values
["iconOverlays"].toStringList();
957 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
, m_layout
!= IconsLayout
&& isActiveWindow() && isSelected() ? QIcon::Selected
: QIcon::Normal
);
959 } else if (m_pixmap
.width() / m_pixmap
.devicePixelRatio() != maxIconWidth
|| m_pixmap
.height() / m_pixmap
.devicePixelRatio() != maxIconHeight
) {
960 // A custom pixmap has been applied. Assure that the pixmap
961 // is scaled to the maximum available size.
962 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
) * qApp
->devicePixelRatio());
965 if (m_pixmap
.isNull()) {
966 m_hoverPixmap
= QPixmap();
971 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
972 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
976 KIconEffect::semiTransparent(m_pixmap
);
979 if (m_layout
== IconsLayout
&& isSelected()) {
980 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
981 QImage image
= m_pixmap
.toImage();
982 if (image
.isNull()) {
983 m_hoverPixmap
= QPixmap();
986 KIconEffect::colorize(image
, color
, 0.8f
);
987 m_pixmap
= QPixmap::fromImage(image
);
991 if (!m_overlay
.isNull()) {
992 QPainter
painter(&m_pixmap
);
993 painter
.drawPixmap(0, (m_pixmap
.height() - m_overlay
.height()) / m_pixmap
.devicePixelRatio(), m_overlay
);
996 int scaledIconSize
= 0;
998 const TextInfo
* textInfo
= m_textInfo
.value("text");
999 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
1001 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
1002 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
1003 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
1004 widgetSize
.height() - 2 * padding
: maxIconHeight
;
1007 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
1008 const int maxScaledIconHeight
= scaledIconSize
;
1010 m_scaledPixmapSize
= m_pixmap
.size();
1011 m_scaledPixmapSize
.scale(maxScaledIconWidth
* qApp
->devicePixelRatio(), maxScaledIconHeight
* qApp
->devicePixelRatio(), Qt::KeepAspectRatio
);
1012 m_scaledPixmapSize
= m_scaledPixmapSize
/ qApp
->devicePixelRatio();
1015 // Center horizontally and align on bottom within the icon-area
1016 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2.0);
1017 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
1019 // Center horizontally and vertically within the icon-area
1020 const TextInfo
* textInfo
= m_textInfo
.value("text");
1021 m_pixmapPos
.setX(textInfo
->pos
.x() - 2.0 * padding
1022 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2.0);
1024 // Derive icon's vertical center from the center of the text frame, including
1025 // any necessary adjustment if the font's midline is offset from the frame center
1026 const qreal midlineShift
= m_customizedFontMetrics
.height() / 2.0
1027 - m_customizedFontMetrics
.descent()
1028 - m_customizedFontMetrics
.capHeight() / 2.0;
1029 m_pixmapPos
.setY(m_textRect
.center().y() + midlineShift
- m_scaledPixmapSize
.height() / 2.0);
1033 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
1035 // Prepare the pixmap that is used when the item gets hovered
1037 m_hoverPixmap
= m_pixmap
;
1038 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
1039 // In the KIconLoader terminology, active = hover.
1040 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
1041 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
1043 m_hoverPixmap
= m_pixmap
;
1045 } else if (hoverOpacity() <= 0.0) {
1046 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1047 m_hoverPixmap
= QPixmap();
1051 void KStandardItemListWidget::updateTextsCache()
1053 QTextOption textOption
;
1056 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
1057 textOption
.setAlignment(Qt::AlignHCenter
);
1061 textOption
.setAlignment(Qt::AlignLeft
);
1062 textOption
.setWrapMode(QTextOption::NoWrap
);
1069 qDeleteAll(m_textInfo
);
1071 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
1072 TextInfo
* textInfo
= new TextInfo();
1073 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1074 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1075 textInfo
->staticText
.setTextOption(textOption
);
1076 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1080 case IconsLayout
: updateIconsLayoutTextCache(); break;
1081 case CompactLayout
: updateCompactLayoutTextCache(); break;
1082 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1083 default: Q_ASSERT(false); break;
1086 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1087 if (ratingTextInfo
) {
1088 // The text of the rating-role has been set to empty to get
1089 // replaced by a rating-image showing the rating as stars.
1090 const KItemListStyleOption
& option
= styleOption();
1091 QSizeF ratingSize
= preferredRatingSize(option
);
1093 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1094 ? columnWidth("rating") - columnPadding(option
)
1096 if (ratingSize
.width() > availableWidth
) {
1097 ratingSize
.rwidth() = availableWidth
;
1099 const qreal dpr
= qApp
->devicePixelRatio();
1100 m_rating
= QPixmap(ratingSize
.toSize() * dpr
);
1101 m_rating
.setDevicePixelRatio(dpr
);
1102 m_rating
.fill(Qt::transparent
);
1104 QPainter
painter(&m_rating
);
1105 const QRect
rect(QPoint(0, 0), ratingSize
.toSize());
1106 const int rating
= data().value("rating").toInt();
1107 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1108 } else if (!m_rating
.isNull()) {
1109 m_rating
= QPixmap();
1113 QString
KStandardItemListWidget::elideRightKeepExtension(const QString
&text
, int elidingWidth
) const
1115 const auto extensionIndex
= text
.lastIndexOf('.');
1116 if (extensionIndex
!= -1) {
1117 // has file extension
1118 const auto extensionLength
= text
.length() - extensionIndex
;
1119 const auto extensionWidth
= m_customizedFontMetrics
.horizontalAdvance(text
.right(extensionLength
));
1120 if (elidingWidth
> extensionWidth
&& extensionLength
< 6 && (float(extensionWidth
) / float(elidingWidth
)) < 0.3) {
1121 // if we have room to display the file extension and the extension is not too long
1122 QString ret
= m_customizedFontMetrics
.elidedText(text
.chopped(extensionLength
),
1124 elidingWidth
- extensionWidth
);
1125 ret
.append(text
.rightRef(extensionLength
));
1129 return m_customizedFontMetrics
.elidedText(text
,Qt::ElideRight
,
1133 void KStandardItemListWidget::updateIconsLayoutTextCache()
1140 // might get wrapped above
1142 // Additional role 1
1143 // Additional role 2
1145 const QHash
<QByteArray
, QVariant
> values
= data();
1147 const KItemListStyleOption
& option
= styleOption();
1148 const qreal padding
= option
.padding
;
1149 const qreal maxWidth
= size().width() - 2 * padding
;
1150 const qreal widgetHeight
= size().height();
1151 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1153 // Initialize properties for the "text" role. It will be used as anchor
1154 // for initializing the position of the other roles.
1155 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1156 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1157 nameTextInfo
->staticText
.setText(nameText
);
1159 // Calculate the number of lines required for the name and the required width
1160 qreal nameWidth
= 0;
1161 qreal nameHeight
= 0;
1164 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1165 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1166 layout
.beginLayout();
1167 int nameLineIndex
= 0;
1168 while ((line
= layout
.createLine()).isValid()) {
1169 line
.setLineWidth(maxWidth
);
1170 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1171 nameHeight
+= line
.height();
1174 if (nameLineIndex
== option
.maxTextLines
) {
1175 // The maximum number of textlines has been reached. If this is
1176 // the case provide an elided text if necessary.
1177 const int textLength
= line
.textStart() + line
.textLength();
1178 if (textLength
< nameText
.length()) {
1179 // Elide the last line of the text
1180 qreal elidingWidth
= maxWidth
;
1181 qreal lastLineWidth
;
1183 QString lastTextLine
= nameText
.mid(line
.textStart());
1184 lastTextLine
= elideRightKeepExtension(lastTextLine
, elidingWidth
);
1185 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1186 nameTextInfo
->staticText
.setText(elidedText
);
1188 lastLineWidth
= m_customizedFontMetrics
.horizontalAdvance(lastTextLine
);
1190 // We do the text eliding in a loop with decreasing width (1 px / iteration)
1191 // to avoid problems related to different width calculation code paths
1192 // within Qt. (see bug 337104)
1193 elidingWidth
-= 1.0;
1194 } while (lastLineWidth
> maxWidth
);
1196 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1203 // Use one line for each additional information
1204 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1205 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1206 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1208 additionalRolesCount
* lineSpacing
-
1210 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1211 nameTextInfo
->pos
.y(),
1215 // Calculate the position for each additional information
1216 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1217 for (const QByteArray
& role
: qAsConst(m_sortedVisibleRoles
)) {
1218 if (role
== "text") {
1222 const QString text
= roleText(role
, values
);
1223 TextInfo
* textInfo
= m_textInfo
.value(role
);
1224 textInfo
->staticText
.setText(text
);
1226 qreal requiredWidth
= 0;
1228 QTextLayout
layout(text
, m_customizedFont
);
1229 QTextOption textOption
;
1230 textOption
.setWrapMode(QTextOption::NoWrap
);
1231 layout
.setTextOption(textOption
);
1233 layout
.beginLayout();
1234 QTextLine textLine
= layout
.createLine();
1235 if (textLine
.isValid()) {
1236 textLine
.setLineWidth(maxWidth
);
1237 requiredWidth
= textLine
.naturalTextWidth();
1238 if (requiredWidth
> maxWidth
) {
1239 const QString elidedText
= elideRightKeepExtension(text
, maxWidth
);
1240 textInfo
->staticText
.setText(elidedText
);
1241 requiredWidth
= m_customizedFontMetrics
.horizontalAdvance(elidedText
);
1242 } else if (role
== "rating") {
1243 // Use the width of the rating pixmap, because the rating text is empty.
1244 requiredWidth
= m_rating
.width();
1249 textInfo
->pos
= QPointF(padding
, y
);
1250 textInfo
->staticText
.setTextWidth(maxWidth
);
1252 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1253 m_textRect
|= textRect
;
1258 // Add a padding to the text rectangle
1259 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1262 void KStandardItemListWidget::updateCompactLayoutTextCache()
1264 // +------+ Name role
1265 // | Icon | Additional role 1
1266 // +------+ Additional role 2
1268 const QHash
<QByteArray
, QVariant
> values
= data();
1270 const KItemListStyleOption
& option
= styleOption();
1271 const qreal widgetHeight
= size().height();
1272 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1273 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1274 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1276 qreal maximumRequiredTextWidth
= 0;
1277 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1278 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1279 const qreal maxWidth
= size().width() - x
- option
.padding
;
1280 for (const QByteArray
& role
: qAsConst(m_sortedVisibleRoles
)) {
1281 const QString text
= roleText(role
, values
);
1282 TextInfo
* textInfo
= m_textInfo
.value(role
);
1283 textInfo
->staticText
.setText(text
);
1285 qreal requiredWidth
= m_customizedFontMetrics
.horizontalAdvance(text
);
1286 if (requiredWidth
> maxWidth
) {
1287 requiredWidth
= maxWidth
;
1288 const QString elidedText
= elideRightKeepExtension(text
, maxWidth
);
1289 textInfo
->staticText
.setText(elidedText
);
1292 textInfo
->pos
= QPointF(x
, y
);
1293 textInfo
->staticText
.setTextWidth(maxWidth
);
1295 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1300 m_textRect
= QRectF(x
- option
.padding
, 0, maximumRequiredTextWidth
+ 2 * option
.padding
, widgetHeight
);
1303 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1305 // Precondition: Requires already updated m_expansionArea
1306 // to determine the left position.
1309 // | Icon | Name role Additional role 1 Additional role 2
1311 m_textRect
= QRectF();
1313 const KItemListStyleOption
& option
= styleOption();
1314 const QHash
<QByteArray
, QVariant
> values
= data();
1316 const qreal widgetHeight
= size().height();
1317 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1318 const int fontHeight
= m_customizedFontMetrics
.height();
1320 const qreal columnWidthInc
= columnPadding(option
);
1321 qreal firstColumnInc
= scaledIconSize
;
1322 if (m_supportsItemExpanding
) {
1323 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1325 firstColumnInc
+= option
.padding
;
1328 qreal x
= firstColumnInc
;
1329 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1331 for (const QByteArray
& role
: qAsConst(m_sortedVisibleRoles
)) {
1332 QString text
= roleText(role
, values
);
1334 // Elide the text in case it does not fit into the available column-width
1335 qreal requiredWidth
= m_customizedFontMetrics
.horizontalAdvance(text
);
1336 const qreal roleWidth
= columnWidth(role
);
1337 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1339 const bool isTextRole
= (role
== "text");
1341 availableTextWidth
-= firstColumnInc
;
1344 if (requiredWidth
> availableTextWidth
) {
1345 text
= elideRightKeepExtension(text
, availableTextWidth
);
1346 requiredWidth
= m_customizedFontMetrics
.horizontalAdvance(text
);
1349 TextInfo
* textInfo
= m_textInfo
.value(role
);
1350 textInfo
->staticText
.setText(text
);
1351 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1355 const qreal textWidth
= option
.extendedSelectionRegion
1356 ? size().width() - textInfo
->pos
.x()
1357 : requiredWidth
+ 2 * option
.padding
;
1358 m_textRect
= QRectF(textInfo
->pos
.x() - option
.padding
, 0,
1359 textWidth
, size().height());
1361 // The column after the name should always be aligned on the same x-position independent
1362 // from the expansion-level shown in the name column
1363 x
-= firstColumnInc
;
1364 } else if (isRoleRightAligned(role
)) {
1365 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1370 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1373 if (m_customTextColor
.isValid()) {
1374 c1
= m_customTextColor
;
1375 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1376 c1
= styleOption().palette
.highlightedText().color();
1378 c1
= styleOption().palette
.text().color();
1381 // For the color of the additional info the inactive text color
1382 // is not used as this might lead to unreadable text for some color schemes. Instead
1383 // the text color c1 is slightly mixed with the background color.
1384 const QColor c2
= styleOption().palette
.base().color();
1386 const int p2
= 100 - p1
;
1387 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1388 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1389 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1392 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1394 if (m_scaledPixmapSize
!= pixmap
.size() / pixmap
.devicePixelRatio()) {
1395 QPixmap scaledPixmap
= pixmap
;
1396 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
* qApp
->devicePixelRatio());
1397 scaledPixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1398 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1400 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1401 painter
->setPen(Qt::blue
);
1402 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1405 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1409 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1411 const int siblingSize
= size().height();
1412 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1413 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1415 QStyleOption option
;
1416 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1417 bool isItemSibling
= true;
1419 const QBitArray siblings
= siblingsInformation();
1420 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1421 option
.rect
= siblingRect
;
1422 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1424 if (isItemSibling
) {
1425 option
.state
|= QStyle::State_Item
;
1426 if (m_isExpandable
) {
1427 option
.state
|= QStyle::State_Children
;
1429 if (data().value("isExpanded").toBool()) {
1430 option
.state
|= QStyle::State_Open
;
1432 isItemSibling
= false;
1435 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1437 siblingRect
.translate(-siblingRect
.width(), 0);
1441 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1443 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1448 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1449 if (m_layout
== DetailsLayout
) {
1450 rect
.setWidth(columnWidth(role
) - rect
.x());
1456 void KStandardItemListWidget::closeRoleEditor()
1458 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
1459 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
1460 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
1461 this, &KStandardItemListWidget::slotRoleEditingFinished
);
1463 if (m_roleEditor
->hasFocus()) {
1464 // If the editing was not ended by a FocusOut event, we have
1465 // to transfer the keyboard focus back to the KItemListContainer.
1466 scene()->views()[0]->parentWidget()->setFocus();
1469 if (m_oldRoleEditor
) {
1470 m_oldRoleEditor
->deleteLater();
1472 m_oldRoleEditor
= m_roleEditor
;
1473 m_roleEditor
->hide();
1474 m_roleEditor
= nullptr;
1477 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
, QIcon::Mode mode
)
1479 static const QIcon fallbackIcon
= QIcon::fromTheme(QStringLiteral("unknown"));
1481 size
*= qApp
->devicePixelRatio();
1483 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(QLatin1Char(':')) % ":" % QString::number(size
) % ":" % QString::number(mode
);
1486 if (!QPixmapCache::find(key
, &pixmap
)) {
1487 QIcon icon
= QIcon::fromTheme(name
);
1488 if (icon
.isNull()) {
1492 || icon
.pixmap(size
/ qApp
->devicePixelRatio(), size
/ qApp
->devicePixelRatio(), mode
).isNull()) {
1493 icon
= fallbackIcon
;
1496 pixmap
= icon
.pixmap(size
/ qApp
->devicePixelRatio(), size
/ qApp
->devicePixelRatio(), mode
);
1497 if (pixmap
.width() != size
|| pixmap
.height() != size
) {
1498 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1501 // Strangely KFileItem::overlays() returns empty string-values, so
1502 // we need to check first whether an overlay must be drawn at all.
1503 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1504 // assumes that an overlay will be drawn and has some additional
1506 for (const QString
& overlay
: overlays
) {
1507 if (!overlay
.isEmpty()) {
1508 int state
= KIconLoader::DefaultState
;
1514 state
= KIconLoader::ActiveState
;
1516 case QIcon::Disabled
:
1517 state
= KIconLoader::DisabledState
;
1519 case QIcon::Selected
:
1520 state
= KIconLoader::SelectedState
;
1524 // There is at least one overlay, draw all overlays above m_pixmap
1525 // and cancel the check
1526 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
, state
);
1531 QPixmapCache::insert(key
, pixmap
);
1533 pixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1538 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1540 const qreal height
= option
.fontMetrics
.ascent();
1541 return QSizeF(height
* 5, height
);
1544 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1546 return option
.padding
* 6;