1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kstandarditemlistwidget.h"
22 #include "kfileitemlistview.h"
23 #include "kfileitemmodel.h"
24 #include "private/kfileitemclipboard.h"
25 #include "private/kitemlistroleeditor.h"
26 #include "private/kpixmapmodifier.h"
28 #include <KIconEffect>
29 #include <KIconLoader>
30 #include <KRatingPainter>
31 #include <KStringHandler>
33 #include <QGraphicsScene>
34 #include <QGraphicsSceneResizeEvent>
35 #include <QGraphicsView>
36 #include <QGuiApplication>
37 #include <QPixmapCache>
38 #include <QStyleOption>
40 // #define KSTANDARDITEMLISTWIDGET_DEBUG
42 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
43 KItemListWidgetInformant()
47 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
51 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
53 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
54 case KStandardItemListView::IconsLayout
:
55 calculateIconsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
58 case KStandardItemListView::CompactLayout
:
59 calculateCompactLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
62 case KStandardItemListView::DetailsLayout
:
63 calculateDetailsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
72 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
74 const KItemListView
* view
) const
76 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
77 const KItemListStyleOption
& option
= view
->styleOption();
79 const QString text
= roleText(role
, values
);
80 qreal width
= KStandardItemListWidget::columnPadding(option
);
82 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
83 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
85 if (role
== "rating") {
86 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
88 // If current item is a link, we use the customized link font metrics instead of the normal font metrics.
89 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
91 width
+= fontMetrics
.width(text
);
94 if (view
->supportsItemExpanding()) {
95 // Increase the width by the expansion-toggle and the current expansion level
96 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
97 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, fontMetrics
.height());
98 width
+= (expandedParentsCount
+ 1) * height
;
101 // Increase the width by the required space for the icon
102 width
+= option
.padding
* 2 + option
.iconSize
;
109 QString
KStandardItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
111 return view
->model()->data(index
).value("text").toString();
114 bool KStandardItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
121 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
122 const QHash
<QByteArray
, QVariant
>& values
) const
124 if (role
== "rating") {
125 // Always use an empty text, as the rating is shown by the image m_rating.
128 return values
.value(role
).toString();
131 QFont
KStandardItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
136 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
138 const KItemListStyleOption
& option
= view
->styleOption();
139 const QFont
& normalFont
= option
.font
;
140 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
142 const qreal itemWidth
= view
->itemSize().width();
143 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
144 const qreal additionalRolesSpacing
= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
145 const qreal spacingAndIconHeight
= option
.iconSize
+ option
.padding
* 3;
147 const QFont linkFont
= customizedFontForLinks(normalFont
);
149 QTextOption
textOption(Qt::AlignHCenter
);
150 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
152 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
153 if (logicalHeightHints
.at(index
) > 0.0) {
157 // If the current item is a link, we use the customized link font instead of the normal font.
158 const QFont
& font
= itemIsLink(index
, view
) ? linkFont
: normalFont
;
160 const QString
& text
= KStringHandler::preProcessWrap(itemText(index
, view
));
162 // Calculate the number of lines required for wrapping the name
163 qreal textHeight
= 0;
164 QTextLayout
layout(text
, font
);
165 layout
.setTextOption(textOption
);
166 layout
.beginLayout();
169 while ((line
= layout
.createLine()).isValid()) {
170 line
.setLineWidth(maxWidth
);
171 line
.naturalTextWidth();
172 textHeight
+= line
.height();
175 if (lineCount
== option
.maxTextLines
) {
181 // Add one line for each additional information
182 textHeight
+= additionalRolesSpacing
;
184 logicalHeightHints
[index
] = textHeight
+ spacingAndIconHeight
;
187 logicalWidthHint
= itemWidth
;
190 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
192 const KItemListStyleOption
& option
= view
->styleOption();
193 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
194 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
196 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
197 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
198 const qreal maxWidth
= option
.maxTextWidth
;
199 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
200 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * normalFontMetrics
.lineSpacing());
202 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
204 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
205 if (logicalHeightHints
.at(index
) > 0.0) {
209 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
210 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
212 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
213 // to show all roles without horizontal clipping.
214 qreal maximumRequiredWidth
= 0.0;
216 if (showOnlyTextRole
) {
217 maximumRequiredWidth
= fontMetrics
.width(itemText(index
, view
));
219 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
220 foreach (const QByteArray
& role
, visibleRoles
) {
221 const QString
& text
= roleText(role
, values
);
222 const qreal requiredWidth
= fontMetrics
.width(text
);
223 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
227 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
228 if (maxWidth
> 0 && width
> maxWidth
) {
232 logicalHeightHints
[index
] = width
;
235 logicalWidthHint
= height
;
238 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
240 const KItemListStyleOption
& option
= view
->styleOption();
241 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
242 logicalHeightHints
.fill(height
);
243 logicalWidthHint
= -1.0;
246 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
247 KItemListWidget(informant
, parent
),
251 m_customizedFontMetrics(m_customizedFont
),
252 m_isExpandable(false),
253 m_supportsItemExpanding(false),
255 m_dirtyContent(true),
256 m_dirtyContentRoles(),
257 m_layout(IconsLayout
),
260 m_scaledPixmapSize(),
265 m_sortedVisibleRoles(),
268 m_additionalInfoTextColor(),
271 m_roleEditor(nullptr),
272 m_oldRoleEditor(nullptr)
276 KStandardItemListWidget::~KStandardItemListWidget()
278 qDeleteAll(m_textInfo
);
282 m_roleEditor
->deleteLater();
285 if (m_oldRoleEditor
) {
286 m_oldRoleEditor
->deleteLater();
290 void KStandardItemListWidget::setLayout(Layout layout
)
292 if (m_layout
!= layout
) {
294 m_dirtyLayout
= true;
295 updateAdditionalInfoTextColor();
300 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
305 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
307 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
308 m_supportsItemExpanding
= supportsItemExpanding
;
309 m_dirtyLayout
= true;
314 bool KStandardItemListWidget::supportsItemExpanding() const
316 return m_supportsItemExpanding
;
319 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
321 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
323 KItemListWidget::paint(painter
, option
, widget
);
325 if (!m_expansionArea
.isEmpty()) {
326 drawSiblingsInformation(painter
);
329 const KItemListStyleOption
& itemListStyleOption
= styleOption();
331 if (hoverOpacity() < 1.0) {
333 * Linear interpolation between m_pixmap and m_hoverPixmap.
335 * Note that this cannot be achieved by painting m_hoverPixmap over
336 * m_pixmap, even if the opacities are adjusted. For details see
337 * https://git.reviewboard.kde.org/r/109614/
339 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
340 QPixmap
pixmap1(m_pixmap
.size());
341 pixmap1
.setDevicePixelRatio(m_pixmap
.devicePixelRatio());
342 pixmap1
.fill(Qt::transparent
);
344 QPainter
p(&pixmap1
);
345 p
.setOpacity(1.0 - hoverOpacity());
346 p
.drawPixmap(0, 0, m_pixmap
);
349 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
350 QPixmap
pixmap2(pixmap1
.size());
351 pixmap2
.setDevicePixelRatio(pixmap1
.devicePixelRatio());
352 pixmap2
.fill(Qt::transparent
);
354 QPainter
p(&pixmap2
);
355 p
.setOpacity(hoverOpacity());
356 p
.drawPixmap(0, 0, m_hoverPixmap
);
359 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
360 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
361 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
363 QPainter
p(&pixmap1
);
364 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
365 p
.drawPixmap(0, 0, pixmap2
);
368 // Finally paint pixmap1 on the widget
369 drawPixmap(painter
, pixmap1
);
371 drawPixmap(painter
, m_hoverPixmap
);
374 drawPixmap(painter
, m_pixmap
);
377 painter
->setFont(m_customizedFont
);
378 painter
->setPen(textColor());
379 const TextInfo
* textInfo
= m_textInfo
.value("text");
382 // It seems that we can end up here even if m_textInfo does not contain
383 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
384 // this can only happen if the index is negative. This can happen when
385 // the item is about to be removed, see KItemListView::slotItemsRemoved().
386 // TODO: try to reproduce the crash and find a better fix.
390 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
392 bool clipAdditionalInfoBounds
= false;
393 if (m_supportsItemExpanding
) {
394 // Prevent a possible overlapping of the additional-information texts
395 // with the icon. This can happen if the user has minimized the width
396 // of the name-column to a very small value.
397 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
398 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
399 clipAdditionalInfoBounds
= true;
401 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
405 painter
->setPen(m_additionalInfoTextColor
);
406 painter
->setFont(m_customizedFont
);
408 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
409 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
410 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
413 if (!m_rating
.isNull()) {
414 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
415 QPointF pos
= ratingTextInfo
->pos
;
416 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
417 if (align
& Qt::AlignHCenter
) {
418 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
420 painter
->drawPixmap(pos
, m_rating
);
423 if (clipAdditionalInfoBounds
) {
427 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
428 painter
->setBrush(Qt::NoBrush
);
429 painter
->setPen(Qt::green
);
430 painter
->drawRect(m_iconRect
);
432 painter
->setPen(Qt::blue
);
433 painter
->drawRect(m_textRect
);
435 painter
->setPen(Qt::red
);
436 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
437 painter
->drawRect(rect());
441 QRectF
KStandardItemListWidget::iconRect() const
443 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
447 QRectF
KStandardItemListWidget::textRect() const
449 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
453 QRectF
KStandardItemListWidget::textFocusRect() const
455 // In the compact- and details-layout a larger textRect() is returned to be aligned
456 // with the iconRect(). This is useful to have a larger selection/hover-area
457 // when having a quite large icon size but only one line of text. Still the
458 // focus rectangle should be shown as narrow as possible around the text.
460 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
463 case CompactLayout
: {
464 QRectF rect
= m_textRect
;
465 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
466 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
467 rect
.setTop(topText
->pos
.y());
468 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
472 case DetailsLayout
: {
473 QRectF rect
= m_textRect
;
474 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
475 rect
.setTop(textInfo
->pos
.y());
476 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
478 const KItemListStyleOption
& option
= styleOption();
479 if (option
.extendedSelectionRegion
) {
480 const QString text
= textInfo
->staticText
.text();
481 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
494 QRectF
KStandardItemListWidget::selectionRect() const
496 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
503 case DetailsLayout
: {
504 const int padding
= styleOption().padding
;
505 QRectF adjustedIconRect
= iconRect().adjusted(-padding
, -padding
, padding
, padding
);
506 return adjustedIconRect
| m_textRect
;
517 QRectF
KStandardItemListWidget::expansionToggleRect() const
519 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
520 return m_isExpandable
? m_expansionArea
: QRectF();
523 QRectF
KStandardItemListWidget::selectionToggleRect() const
525 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
527 const int iconHeight
= styleOption().iconSize
;
529 int toggleSize
= KIconLoader::SizeSmall
;
530 if (iconHeight
>= KIconLoader::SizeEnormous
) {
531 toggleSize
= KIconLoader::SizeMedium
;
532 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
533 toggleSize
= KIconLoader::SizeSmallMedium
;
536 QPointF pos
= iconRect().topLeft();
538 // If the selection toggle has a very small distance to the
539 // widget borders, the size of the selection toggle will get
540 // increased to prevent an accidental clicking of the item
541 // when trying to hit the toggle.
542 const int widgetHeight
= size().height();
543 const int widgetWidth
= size().width();
544 const int minMargin
= 2;
546 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
547 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
548 toggleSize
= widgetHeight
;
551 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
552 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
553 toggleSize
= widgetWidth
;
557 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
560 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
563 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
564 if (m_layout
!= DetailsLayout
) {
568 // Only return the content of the text-column as pixmap
569 const int leftClip
= m_pixmapPos
.x();
571 const TextInfo
* textInfo
= m_textInfo
.value("text");
572 const int rightClip
= textInfo
->pos
.x() +
573 textInfo
->staticText
.size().width() +
574 2 * styleOption().padding
;
576 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
577 clippedPixmap
.fill(Qt::transparent
);
579 QPainter
painter(&clippedPixmap
);
580 painter
.drawPixmap(-leftClip
, 0, pixmap
);
582 return clippedPixmap
;
586 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
588 return new KStandardItemListWidgetInformant();
591 void KStandardItemListWidget::invalidateCache()
593 m_dirtyLayout
= true;
594 m_dirtyContent
= true;
597 void KStandardItemListWidget::refreshCache()
601 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
607 bool KStandardItemListWidget::isHidden() const
612 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
617 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
619 return QPalette::Text
;
622 void KStandardItemListWidget::setTextColor(const QColor
& color
)
624 if (color
!= m_customTextColor
) {
625 m_customTextColor
= color
;
626 updateAdditionalInfoTextColor();
631 QColor
KStandardItemListWidget::textColor() const
635 return m_additionalInfoTextColor
;
636 } else if (m_customTextColor
.isValid()) {
637 return m_customTextColor
;
641 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
642 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
643 return styleOption().palette
.color(group
, role
);
646 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
649 m_dirtyContent
= true;
653 QPixmap
KStandardItemListWidget::overlay() const
659 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
660 const QHash
<QByteArray
, QVariant
>& values
) const
662 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
665 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
666 const QSet
<QByteArray
>& roles
)
670 m_dirtyContent
= true;
672 QSet
<QByteArray
> dirtyRoles
;
673 if (roles
.isEmpty()) {
674 dirtyRoles
= visibleRoles().toSet();
679 // The URL might have changed (i.e., if the sort order of the items has
680 // been changed). Therefore, the "is cut" state must be updated.
681 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
682 const QUrl itemUrl
= data().value("url").toUrl();
683 m_isCut
= clipboard
->isCut(itemUrl
);
685 // The icon-state might depend from other roles and hence is
686 // marked as dirty whenever a role has been changed
687 dirtyRoles
.insert("iconPixmap");
688 dirtyRoles
.insert("iconName");
690 QSetIterator
<QByteArray
> it(dirtyRoles
);
691 while (it
.hasNext()) {
692 const QByteArray
& role
= it
.next();
693 m_dirtyContentRoles
.insert(role
);
697 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
698 const QList
<QByteArray
>& previous
)
701 m_sortedVisibleRoles
= current
;
702 m_dirtyLayout
= true;
705 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
712 m_dirtyLayout
= true;
715 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
716 const KItemListStyleOption
& previous
)
720 updateAdditionalInfoTextColor();
721 m_dirtyLayout
= true;
724 void KStandardItemListWidget::hoveredChanged(bool hovered
)
727 m_dirtyLayout
= true;
730 void KStandardItemListWidget::selectedChanged(bool selected
)
733 updateAdditionalInfoTextColor();
734 m_dirtyContent
= true;
737 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
741 m_dirtyLayout
= true;
744 int KStandardItemListWidget::selectionLength(const QString
& text
) const
746 return text
.length();
749 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
753 QGraphicsView
* parent
= scene()->views()[0];
754 if (current
.isEmpty() || !parent
|| current
!= "text") {
756 emit
roleEditingCanceled(index(), current
, data().value(current
));
758 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
759 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
760 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
761 this, &KStandardItemListWidget::slotRoleEditingFinished
);
763 if (m_oldRoleEditor
) {
764 m_oldRoleEditor
->deleteLater();
766 m_oldRoleEditor
= m_roleEditor
;
767 m_roleEditor
->hide();
768 m_roleEditor
= nullptr;
773 Q_ASSERT(!m_roleEditor
);
775 const TextInfo
* textInfo
= m_textInfo
.value("text");
777 m_roleEditor
= new KItemListRoleEditor(parent
);
778 m_roleEditor
->setRole(current
);
779 m_roleEditor
->setFont(styleOption().font
);
781 const QString text
= data().value(current
).toString();
782 m_roleEditor
->setPlainText(text
);
784 QTextOption textOption
= textInfo
->staticText
.textOption();
785 m_roleEditor
->document()->setDefaultTextOption(textOption
);
787 const int textSelectionLength
= selectionLength(text
);
789 if (textSelectionLength
> 0) {
790 QTextCursor cursor
= m_roleEditor
->textCursor();
791 cursor
.movePosition(QTextCursor::StartOfBlock
);
792 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
793 m_roleEditor
->setTextCursor(cursor
);
796 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
797 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
798 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
799 this, &KStandardItemListWidget::slotRoleEditingFinished
);
801 // Adjust the geometry of the editor
802 QRectF rect
= roleEditingRect(current
);
803 const int frameWidth
= m_roleEditor
->frameWidth();
804 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
805 rect
.translate(pos());
806 if (rect
.right() > parent
->width()) {
807 rect
.setWidth(parent
->width() - rect
.left());
809 m_roleEditor
->setGeometry(rect
.toRect());
810 m_roleEditor
->show();
811 m_roleEditor
->setFocus();
814 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
817 setEditedRole(QByteArray());
818 Q_ASSERT(!m_roleEditor
);
821 KItemListWidget::resizeEvent(event
);
823 m_dirtyLayout
= true;
826 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
828 KItemListWidget::showEvent(event
);
830 // Listen to changes of the clipboard to mark the item as cut/uncut
831 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
833 const QUrl itemUrl
= data().value("url").toUrl();
834 m_isCut
= clipboard
->isCut(itemUrl
);
836 connect(clipboard
, &KFileItemClipboard::cutItemsChanged
,
837 this, &KStandardItemListWidget::slotCutItemsChanged
);
840 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
842 disconnect(KFileItemClipboard::instance(), &KFileItemClipboard::cutItemsChanged
,
843 this, &KStandardItemListWidget::slotCutItemsChanged
);
845 KItemListWidget::hideEvent(event
);
848 bool KStandardItemListWidget::event(QEvent
*event
)
850 if (event
->type() == QEvent::WindowDeactivate
|| event
->type() == QEvent::WindowActivate
851 || event
->type() == QEvent::PaletteChange
) {
852 m_dirtyContent
= true;
855 return KItemListWidget::event(event
);
858 void KStandardItemListWidget::finishRoleEditing()
860 if (!editedRole().isEmpty() && m_roleEditor
) {
861 slotRoleEditingFinished(editedRole(), KIO::encodeFileName(m_roleEditor
->toPlainText()));
865 void KStandardItemListWidget::slotCutItemsChanged()
867 const QUrl itemUrl
= data().value("url").toUrl();
868 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
869 if (m_isCut
!= isCut
) {
871 m_pixmap
= QPixmap();
872 m_dirtyContent
= true;
877 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
878 const QVariant
& value
)
881 emit
roleEditingCanceled(index(), role
, value
);
882 setEditedRole(QByteArray());
885 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
886 const QVariant
& value
)
889 emit
roleEditingFinished(index(), role
, value
);
890 setEditedRole(QByteArray());
893 void KStandardItemListWidget::triggerCacheRefreshing()
895 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
901 const QHash
<QByteArray
, QVariant
> values
= data();
902 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
903 m_isHidden
= isHidden();
904 m_customizedFont
= customizedFont(styleOption().font
);
905 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
907 updateExpansionArea();
911 m_dirtyLayout
= false;
912 m_dirtyContent
= false;
913 m_dirtyContentRoles
.clear();
916 void KStandardItemListWidget::updateExpansionArea()
918 if (m_supportsItemExpanding
) {
919 const QHash
<QByteArray
, QVariant
> values
= data();
920 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
921 if (expandedParentsCount
>= 0) {
922 const KItemListStyleOption
& option
= styleOption();
923 const qreal widgetHeight
= size().height();
924 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
925 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
927 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
932 m_expansionArea
= QRectF();
935 void KStandardItemListWidget::updatePixmapCache()
937 // Precondition: Requires already updated m_textPos values to calculate
938 // the remaining height when the alignment is vertical.
940 const QSizeF widgetSize
= size();
941 const bool iconOnTop
= (m_layout
== IconsLayout
);
942 const KItemListStyleOption
& option
= styleOption();
943 const qreal padding
= option
.padding
;
945 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
946 const int maxIconHeight
= option
.iconSize
;
948 const QHash
<QByteArray
, QVariant
> values
= data();
950 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
951 if (!updatePixmap
&& m_dirtyContent
) {
952 updatePixmap
= m_dirtyContentRoles
.isEmpty()
953 || m_dirtyContentRoles
.contains("iconPixmap")
954 || m_dirtyContentRoles
.contains("iconName")
955 || m_dirtyContentRoles
.contains("iconOverlays");
959 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
960 if (m_pixmap
.isNull()) {
961 // Use the icon that fits to the MIME-type
962 QString iconName
= values
["iconName"].toString();
963 if (iconName
.isEmpty()) {
964 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
965 // use a generic icon as fallback
966 iconName
= QStringLiteral("unknown");
968 const QStringList overlays
= values
["iconOverlays"].toStringList();
969 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
, isSelected() && isActiveWindow() ? QIcon::Selected
: QIcon::Normal
);
971 } else if (m_pixmap
.width() / m_pixmap
.devicePixelRatio() != maxIconWidth
|| m_pixmap
.height() / m_pixmap
.devicePixelRatio() != maxIconHeight
) {
972 // A custom pixmap has been applied. Assure that the pixmap
973 // is scaled to the maximum available size.
974 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
) * qApp
->devicePixelRatio());
978 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
979 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
983 KIconEffect::semiTransparent(m_pixmap
);
986 if (m_layout
== IconsLayout
&& isSelected()) {
987 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
988 QImage image
= m_pixmap
.toImage();
989 KIconEffect::colorize(image
, color
, 0.8f
);
990 m_pixmap
= QPixmap::fromImage(image
);
994 if (!m_overlay
.isNull()) {
995 QPainter
painter(&m_pixmap
);
996 painter
.drawPixmap(0, (m_pixmap
.height() - m_overlay
.height()) / m_pixmap
.devicePixelRatio(), m_overlay
);
999 int scaledIconSize
= 0;
1001 const TextInfo
* textInfo
= m_textInfo
.value("text");
1002 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
1004 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
1005 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
1006 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
1007 widgetSize
.height() - 2 * padding
: maxIconHeight
;
1010 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
1011 const int maxScaledIconHeight
= scaledIconSize
;
1013 m_scaledPixmapSize
= m_pixmap
.size();
1014 m_scaledPixmapSize
.scale(maxScaledIconWidth
* qApp
->devicePixelRatio(), maxScaledIconHeight
* qApp
->devicePixelRatio(), Qt::KeepAspectRatio
);
1015 m_scaledPixmapSize
= m_scaledPixmapSize
/ qApp
->devicePixelRatio();
1018 // Center horizontally and align on bottom within the icon-area
1019 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2.0);
1020 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
1022 // Center horizontally and vertically within the icon-area
1023 const TextInfo
* textInfo
= m_textInfo
.value("text");
1024 m_pixmapPos
.setX(textInfo
->pos
.x() - 2.0 * padding
1025 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2.0);
1027 // Derive icon's vertical center from the center of the text frame, including
1028 // any necessary adjustment if the font's midline is offset from the frame center
1029 const qreal midlineShift
= m_customizedFontMetrics
.height() / 2.0
1030 - m_customizedFontMetrics
.descent()
1031 - m_customizedFontMetrics
.capHeight() / 2.0;
1032 m_pixmapPos
.setY(m_textRect
.center().y() + midlineShift
- m_scaledPixmapSize
.height() / 2.0);
1036 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
1038 // Prepare the pixmap that is used when the item gets hovered
1040 m_hoverPixmap
= m_pixmap
;
1041 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
1042 // In the KIconLoader terminology, active = hover.
1043 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
1044 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
1046 m_hoverPixmap
= m_pixmap
;
1048 } else if (hoverOpacity() <= 0.0) {
1049 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1050 m_hoverPixmap
= QPixmap();
1054 void KStandardItemListWidget::updateTextsCache()
1056 QTextOption textOption
;
1059 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
1060 textOption
.setAlignment(Qt::AlignHCenter
);
1064 textOption
.setAlignment(Qt::AlignLeft
);
1065 textOption
.setWrapMode(QTextOption::NoWrap
);
1072 qDeleteAll(m_textInfo
);
1074 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
1075 TextInfo
* textInfo
= new TextInfo();
1076 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1077 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1078 textInfo
->staticText
.setTextOption(textOption
);
1079 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1083 case IconsLayout
: updateIconsLayoutTextCache(); break;
1084 case CompactLayout
: updateCompactLayoutTextCache(); break;
1085 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1086 default: Q_ASSERT(false); break;
1089 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1090 if (ratingTextInfo
) {
1091 // The text of the rating-role has been set to empty to get
1092 // replaced by a rating-image showing the rating as stars.
1093 const KItemListStyleOption
& option
= styleOption();
1094 QSizeF ratingSize
= preferredRatingSize(option
);
1096 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1097 ? columnWidth("rating") - columnPadding(option
)
1099 if (ratingSize
.width() > availableWidth
) {
1100 ratingSize
.rwidth() = availableWidth
;
1102 const qreal dpr
= qApp
->devicePixelRatio();
1103 m_rating
= QPixmap(ratingSize
.toSize() * dpr
);
1104 m_rating
.setDevicePixelRatio(dpr
);
1105 m_rating
.fill(Qt::transparent
);
1107 QPainter
painter(&m_rating
);
1108 const QRect
rect(QPoint(0, 0), ratingSize
.toSize());
1109 const int rating
= data().value("rating").toInt();
1110 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1111 } else if (!m_rating
.isNull()) {
1112 m_rating
= QPixmap();
1116 void KStandardItemListWidget::updateIconsLayoutTextCache()
1123 // might get wrapped above
1125 // Additional role 1
1126 // Additional role 2
1128 const QHash
<QByteArray
, QVariant
> values
= data();
1130 const KItemListStyleOption
& option
= styleOption();
1131 const qreal padding
= option
.padding
;
1132 const qreal maxWidth
= size().width() - 2 * padding
;
1133 const qreal widgetHeight
= size().height();
1134 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1136 // Initialize properties for the "text" role. It will be used as anchor
1137 // for initializing the position of the other roles.
1138 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1139 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1140 nameTextInfo
->staticText
.setText(nameText
);
1142 // Calculate the number of lines required for the name and the required width
1143 qreal nameWidth
= 0;
1144 qreal nameHeight
= 0;
1147 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1148 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1149 layout
.beginLayout();
1150 int nameLineIndex
= 0;
1151 while ((line
= layout
.createLine()).isValid()) {
1152 line
.setLineWidth(maxWidth
);
1153 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1154 nameHeight
+= line
.height();
1157 if (nameLineIndex
== option
.maxTextLines
) {
1158 // The maximum number of textlines has been reached. If this is
1159 // the case provide an elided text if necessary.
1160 const int textLength
= line
.textStart() + line
.textLength();
1161 if (textLength
< nameText
.length()) {
1162 // Elide the last line of the text
1163 qreal elidingWidth
= maxWidth
;
1164 qreal lastLineWidth
;
1166 QString lastTextLine
= nameText
.mid(line
.textStart());
1167 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
1170 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1171 nameTextInfo
->staticText
.setText(elidedText
);
1173 lastLineWidth
= m_customizedFontMetrics
.boundingRect(lastTextLine
).width();
1175 // We do the text eliding in a loop with decreasing width (1 px / iteration)
1176 // to avoid problems related to different width calculation code paths
1177 // within Qt. (see bug 337104)
1178 elidingWidth
-= 1.0;
1179 } while (lastLineWidth
> maxWidth
);
1181 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1188 // Use one line for each additional information
1189 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1190 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1191 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1193 additionalRolesCount
* lineSpacing
-
1195 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1196 nameTextInfo
->pos
.y(),
1200 // Calculate the position for each additional information
1201 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1202 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1203 if (role
== "text") {
1207 const QString text
= roleText(role
, values
);
1208 TextInfo
* textInfo
= m_textInfo
.value(role
);
1209 textInfo
->staticText
.setText(text
);
1211 qreal requiredWidth
= 0;
1213 QTextLayout
layout(text
, m_customizedFont
);
1214 QTextOption textOption
;
1215 textOption
.setWrapMode(QTextOption::NoWrap
);
1216 layout
.setTextOption(textOption
);
1218 layout
.beginLayout();
1219 QTextLine textLine
= layout
.createLine();
1220 if (textLine
.isValid()) {
1221 textLine
.setLineWidth(maxWidth
);
1222 requiredWidth
= textLine
.naturalTextWidth();
1223 if (requiredWidth
> maxWidth
) {
1224 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1225 textInfo
->staticText
.setText(elidedText
);
1226 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1227 } else if (role
== "rating") {
1228 // Use the width of the rating pixmap, because the rating text is empty.
1229 requiredWidth
= m_rating
.width();
1234 textInfo
->pos
= QPointF(padding
, y
);
1235 textInfo
->staticText
.setTextWidth(maxWidth
);
1237 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1238 m_textRect
|= textRect
;
1243 // Add a padding to the text rectangle
1244 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1247 void KStandardItemListWidget::updateCompactLayoutTextCache()
1249 // +------+ Name role
1250 // | Icon | Additional role 1
1251 // +------+ Additional role 2
1253 const QHash
<QByteArray
, QVariant
> values
= data();
1255 const KItemListStyleOption
& option
= styleOption();
1256 const qreal widgetHeight
= size().height();
1257 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1258 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1259 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1261 qreal maximumRequiredTextWidth
= 0;
1262 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1263 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1264 const qreal maxWidth
= size().width() - x
- option
.padding
;
1265 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1266 const QString text
= roleText(role
, values
);
1267 TextInfo
* textInfo
= m_textInfo
.value(role
);
1268 textInfo
->staticText
.setText(text
);
1270 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1271 if (requiredWidth
> maxWidth
) {
1272 requiredWidth
= maxWidth
;
1273 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1274 textInfo
->staticText
.setText(elidedText
);
1277 textInfo
->pos
= QPointF(x
, y
);
1278 textInfo
->staticText
.setTextWidth(maxWidth
);
1280 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1285 m_textRect
= QRectF(x
- 2 * option
.padding
, 0, maximumRequiredTextWidth
+ 3 * option
.padding
, widgetHeight
);
1288 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1290 // Precondition: Requires already updated m_expansionArea
1291 // to determine the left position.
1294 // | Icon | Name role Additional role 1 Additional role 2
1296 m_textRect
= QRectF();
1298 const KItemListStyleOption
& option
= styleOption();
1299 const QHash
<QByteArray
, QVariant
> values
= data();
1301 const qreal widgetHeight
= size().height();
1302 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1303 const int fontHeight
= m_customizedFontMetrics
.height();
1305 const qreal columnWidthInc
= columnPadding(option
);
1306 qreal firstColumnInc
= scaledIconSize
;
1307 if (m_supportsItemExpanding
) {
1308 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1310 firstColumnInc
+= option
.padding
;
1313 qreal x
= firstColumnInc
;
1314 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1316 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1317 QString text
= roleText(role
, values
);
1319 // Elide the text in case it does not fit into the available column-width
1320 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1321 const qreal roleWidth
= columnWidth(role
);
1322 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1324 const bool isTextRole
= (role
== "text");
1326 availableTextWidth
-= firstColumnInc
;
1329 if (requiredWidth
> availableTextWidth
) {
1330 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1331 requiredWidth
= m_customizedFontMetrics
.width(text
);
1334 TextInfo
* textInfo
= m_textInfo
.value(role
);
1335 textInfo
->staticText
.setText(text
);
1336 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1340 const qreal textWidth
= option
.extendedSelectionRegion
1341 ? size().width() - textInfo
->pos
.x()
1342 : requiredWidth
+ 2 * option
.padding
;
1343 m_textRect
= QRectF(textInfo
->pos
.x() - 2 * option
.padding
, 0,
1344 textWidth
+ option
.padding
, size().height());
1346 // The column after the name should always be aligned on the same x-position independent
1347 // from the expansion-level shown in the name column
1348 x
-= firstColumnInc
;
1349 } else if (isRoleRightAligned(role
)) {
1350 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1355 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1358 if (m_customTextColor
.isValid()) {
1359 c1
= m_customTextColor
;
1360 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1361 c1
= styleOption().palette
.highlightedText().color();
1363 c1
= styleOption().palette
.text().color();
1366 // For the color of the additional info the inactive text color
1367 // is not used as this might lead to unreadable text for some color schemes. Instead
1368 // the text color c1 is slightly mixed with the background color.
1369 const QColor c2
= styleOption().palette
.base().color();
1371 const int p2
= 100 - p1
;
1372 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1373 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1374 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1377 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1379 if (m_scaledPixmapSize
!= pixmap
.size() / pixmap
.devicePixelRatio()) {
1380 QPixmap scaledPixmap
= pixmap
;
1381 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
* qApp
->devicePixelRatio());
1382 scaledPixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1383 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1385 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1386 painter
->setPen(Qt::blue
);
1387 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1390 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1394 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1396 const int siblingSize
= size().height();
1397 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1398 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1400 QStyleOption option
;
1401 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1402 bool isItemSibling
= true;
1404 const QBitArray siblings
= siblingsInformation();
1405 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1406 option
.rect
= siblingRect
;
1407 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1409 if (isItemSibling
) {
1410 option
.state
|= QStyle::State_Item
;
1411 if (m_isExpandable
) {
1412 option
.state
|= QStyle::State_Children
;
1414 if (data().value("isExpanded").toBool()) {
1415 option
.state
|= QStyle::State_Open
;
1417 isItemSibling
= false;
1420 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1422 siblingRect
.translate(-siblingRect
.width(), 0);
1426 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1428 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1433 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1434 if (m_layout
== DetailsLayout
) {
1435 rect
.setWidth(columnWidth(role
) - rect
.x());
1441 void KStandardItemListWidget::closeRoleEditor()
1443 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
1444 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
1445 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
1446 this, &KStandardItemListWidget::slotRoleEditingFinished
);
1448 if (m_roleEditor
->hasFocus()) {
1449 // If the editing was not ended by a FocusOut event, we have
1450 // to transfer the keyboard focus back to the KItemListContainer.
1451 scene()->views()[0]->parentWidget()->setFocus();
1454 if (m_oldRoleEditor
) {
1455 m_oldRoleEditor
->deleteLater();
1457 m_oldRoleEditor
= m_roleEditor
;
1458 m_roleEditor
->hide();
1459 m_roleEditor
= nullptr;
1462 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
, QIcon::Mode mode
)
1464 static const QIcon fallbackIcon
= QIcon::fromTheme(QStringLiteral("unknown"));
1465 size
*= qApp
->devicePixelRatio();
1466 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(QStringLiteral(":")) % ":" % QString::number(size
) % ":" % QString::number(mode
);
1469 if (!QPixmapCache::find(key
, pixmap
)) {
1470 const QIcon icon
= QIcon::fromTheme(name
, fallbackIcon
);
1473 if (size
<= KIconLoader::SizeSmall
) {
1474 requestedSize
= KIconLoader::SizeSmall
;
1475 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1476 requestedSize
= KIconLoader::SizeSmallMedium
;
1477 } else if (size
<= KIconLoader::SizeMedium
) {
1478 requestedSize
= KIconLoader::SizeMedium
;
1479 } else if (size
<= KIconLoader::SizeLarge
) {
1480 requestedSize
= KIconLoader::SizeLarge
;
1481 } else if (size
<= KIconLoader::SizeHuge
) {
1482 requestedSize
= KIconLoader::SizeHuge
;
1483 } else if (size
<= KIconLoader::SizeEnormous
) {
1484 requestedSize
= KIconLoader::SizeEnormous
;
1485 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1486 requestedSize
= KIconLoader::SizeEnormous
* 2;
1488 requestedSize
= size
;
1491 pixmap
= icon
.pixmap(requestedSize
/ qApp
->devicePixelRatio(), requestedSize
/ qApp
->devicePixelRatio(), mode
);
1492 if (requestedSize
!= size
) {
1493 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1496 // Strangely KFileItem::overlays() returns empty string-values, so
1497 // we need to check first whether an overlay must be drawn at all.
1498 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1499 // assumes that an overlay will be drawn and has some additional
1501 foreach (const QString
& overlay
, overlays
) {
1502 if (!overlay
.isEmpty()) {
1503 // There is at least one overlay, draw all overlays above m_pixmap
1504 // and cancel the check
1505 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
);
1510 QPixmapCache::insert(key
, pixmap
);
1512 pixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1517 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1519 const qreal height
= option
.fontMetrics
.ascent();
1520 return QSizeF(height
* 5, height
);
1523 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1525 return option
.padding
* 6;