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"
26 #include <KIconEffect>
27 #include <KIconLoader>
28 #include <KRatingPainter>
29 #include <KStringHandler>
31 #include "private/kfileitemclipboard.h"
32 #include "private/kitemlistroleeditor.h"
33 #include "private/kpixmapmodifier.h"
35 #include <QGraphicsScene>
36 #include <QGraphicsSceneResizeEvent>
37 #include <QGraphicsView>
39 #include <QStyleOption>
40 #include <QTextLayout>
42 #include <QPixmapCache>
44 #include <QGuiApplication>
46 // #define KSTANDARDITEMLISTWIDGET_DEBUG
48 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
49 KItemListWidgetInformant()
53 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
57 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
59 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
60 case KStandardItemListWidget::IconsLayout
:
61 calculateIconsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
64 case KStandardItemListWidget::CompactLayout
:
65 calculateCompactLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
68 case KStandardItemListWidget::DetailsLayout
:
69 calculateDetailsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
78 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
80 const KItemListView
* view
) const
82 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
83 const KItemListStyleOption
& option
= view
->styleOption();
85 const QString text
= roleText(role
, values
);
86 qreal width
= KStandardItemListWidget::columnPadding(option
);
88 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
89 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
91 if (role
== "rating") {
92 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
94 // If current item is a link, we use the customized link font metrics instead of the normal font metrics.
95 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
97 width
+= fontMetrics
.width(text
);
100 if (view
->supportsItemExpanding()) {
101 // Increase the width by the expansion-toggle and the current expansion level
102 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
103 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, fontMetrics
.height());
104 width
+= (expandedParentsCount
+ 1) * height
;
107 // Increase the width by the required space for the icon
108 width
+= option
.padding
* 2 + option
.iconSize
;
115 QString
KStandardItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
117 return view
->model()->data(index
).value("text").toString();
120 bool KStandardItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
127 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
128 const QHash
<QByteArray
, QVariant
>& values
) const
130 if (role
== "rating") {
131 // Always use an empty text, as the rating is shown by the image m_rating.
134 return values
.value(role
).toString();
137 QFont
KStandardItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
142 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
144 const KItemListStyleOption
& option
= view
->styleOption();
145 const QFont
& normalFont
= option
.font
;
146 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
148 const qreal itemWidth
= view
->itemSize().width();
149 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
150 const qreal additionalRolesSpacing
= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
151 const qreal spacingAndIconHeight
= option
.iconSize
+ option
.padding
* 3;
153 const QFont linkFont
= customizedFontForLinks(normalFont
);
155 QTextOption
textOption(Qt::AlignHCenter
);
156 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
158 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
159 if (logicalHeightHints
.at(index
) > 0.0) {
163 // If the current item is a link, we use the customized link font instead of the normal font.
164 const QFont
& font
= itemIsLink(index
, view
) ? linkFont
: normalFont
;
166 const QString
& text
= KStringHandler::preProcessWrap(itemText(index
, view
));
168 // Calculate the number of lines required for wrapping the name
169 qreal textHeight
= 0;
170 QTextLayout
layout(text
, font
);
171 layout
.setTextOption(textOption
);
172 layout
.beginLayout();
175 while ((line
= layout
.createLine()).isValid()) {
176 line
.setLineWidth(maxWidth
);
177 line
.naturalTextWidth();
178 textHeight
+= line
.height();
181 if (lineCount
== option
.maxTextLines
) {
187 // Add one line for each additional information
188 textHeight
+= additionalRolesSpacing
;
190 logicalHeightHints
[index
] = textHeight
+ spacingAndIconHeight
;
193 logicalWidthHint
= itemWidth
;
196 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
198 const KItemListStyleOption
& option
= view
->styleOption();
199 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
200 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
202 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
203 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
204 const qreal maxWidth
= option
.maxTextWidth
;
205 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
206 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * normalFontMetrics
.lineSpacing());
208 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
210 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
211 if (logicalHeightHints
.at(index
) > 0.0) {
215 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
216 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
218 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
219 // to show all roles without horizontal clipping.
220 qreal maximumRequiredWidth
= 0.0;
222 if (showOnlyTextRole
) {
223 maximumRequiredWidth
= fontMetrics
.width(itemText(index
, view
));
225 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
226 foreach (const QByteArray
& role
, visibleRoles
) {
227 const QString
& text
= roleText(role
, values
);
228 const qreal requiredWidth
= fontMetrics
.width(text
);
229 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
233 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
234 if (maxWidth
> 0 && width
> maxWidth
) {
238 logicalHeightHints
[index
] = width
;
241 logicalWidthHint
= height
;
244 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
246 const KItemListStyleOption
& option
= view
->styleOption();
247 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
248 logicalHeightHints
.fill(height
);
249 logicalWidthHint
= -1.0;
252 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
253 KItemListWidget(informant
, parent
),
257 m_customizedFontMetrics(m_customizedFont
),
258 m_isExpandable(false),
259 m_supportsItemExpanding(false),
261 m_dirtyContent(true),
262 m_dirtyContentRoles(),
263 m_layout(IconsLayout
),
266 m_scaledPixmapSize(),
271 m_sortedVisibleRoles(),
274 m_additionalInfoTextColor(),
282 KStandardItemListWidget::~KStandardItemListWidget()
284 qDeleteAll(m_textInfo
);
288 m_roleEditor
->deleteLater();
291 if (m_oldRoleEditor
) {
292 m_oldRoleEditor
->deleteLater();
296 void KStandardItemListWidget::setLayout(Layout layout
)
298 if (m_layout
!= layout
) {
300 m_dirtyLayout
= true;
301 updateAdditionalInfoTextColor();
306 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
311 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
313 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
314 m_supportsItemExpanding
= supportsItemExpanding
;
315 m_dirtyLayout
= true;
320 bool KStandardItemListWidget::supportsItemExpanding() const
322 return m_supportsItemExpanding
;
325 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
327 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
329 KItemListWidget::paint(painter
, option
, widget
);
331 if (!m_expansionArea
.isEmpty()) {
332 drawSiblingsInformation(painter
);
335 const KItemListStyleOption
& itemListStyleOption
= styleOption();
337 if (hoverOpacity() < 1.0) {
339 * Linear interpolation between m_pixmap and m_hoverPixmap.
341 * Note that this cannot be achieved by painting m_hoverPixmap over
342 * m_pixmap, even if the opacities are adjusted. For details see
343 * https://git.reviewboard.kde.org/r/109614/
345 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
346 QPixmap
pixmap1(m_pixmap
.size());
347 pixmap1
.setDevicePixelRatio(m_pixmap
.devicePixelRatio());
348 pixmap1
.fill(Qt::transparent
);
350 QPainter
p(&pixmap1
);
351 p
.setOpacity(1.0 - hoverOpacity());
352 p
.drawPixmap(0, 0, m_pixmap
);
355 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
356 QPixmap
pixmap2(pixmap1
.size());
357 pixmap2
.setDevicePixelRatio(pixmap1
.devicePixelRatio());
358 pixmap2
.fill(Qt::transparent
);
360 QPainter
p(&pixmap2
);
361 p
.setOpacity(hoverOpacity());
362 p
.drawPixmap(0, 0, m_hoverPixmap
);
365 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
366 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
367 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
369 QPainter
p(&pixmap1
);
370 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
371 p
.drawPixmap(0, 0, pixmap2
);
374 // Finally paint pixmap1 on the widget
375 drawPixmap(painter
, pixmap1
);
377 drawPixmap(painter
, m_hoverPixmap
);
380 drawPixmap(painter
, m_pixmap
);
383 painter
->setFont(m_customizedFont
);
384 painter
->setPen(textColor());
385 const TextInfo
* textInfo
= m_textInfo
.value("text");
388 // It seems that we can end up here even if m_textInfo does not contain
389 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
390 // this can only happen if the index is negative. This can happen when
391 // the item is about to be removed, see KItemListView::slotItemsRemoved().
392 // TODO: try to reproduce the crash and find a better fix.
396 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
398 bool clipAdditionalInfoBounds
= false;
399 if (m_supportsItemExpanding
) {
400 // Prevent a possible overlapping of the additional-information texts
401 // with the icon. This can happen if the user has minimized the width
402 // of the name-column to a very small value.
403 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
404 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
405 clipAdditionalInfoBounds
= true;
407 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
411 painter
->setPen(m_additionalInfoTextColor
);
412 painter
->setFont(m_customizedFont
);
414 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
415 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
416 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
419 if (!m_rating
.isNull()) {
420 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
421 QPointF pos
= ratingTextInfo
->pos
;
422 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
423 if (align
& Qt::AlignHCenter
) {
424 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
426 painter
->drawPixmap(pos
, m_rating
);
429 if (clipAdditionalInfoBounds
) {
433 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
434 painter
->setBrush(Qt::NoBrush
);
435 painter
->setPen(Qt::green
);
436 painter
->drawRect(m_iconRect
);
438 painter
->setPen(Qt::blue
);
439 painter
->drawRect(m_textRect
);
441 painter
->setPen(Qt::red
);
442 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
443 painter
->drawRect(rect());
447 QRectF
KStandardItemListWidget::iconRect() const
449 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
453 QRectF
KStandardItemListWidget::textRect() const
455 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
459 QRectF
KStandardItemListWidget::textFocusRect() const
461 // In the compact- and details-layout a larger textRect() is returned to be aligned
462 // with the iconRect(). This is useful to have a larger selection/hover-area
463 // when having a quite large icon size but only one line of text. Still the
464 // focus rectangle should be shown as narrow as possible around the text.
466 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
469 case CompactLayout
: {
470 QRectF rect
= m_textRect
;
471 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
472 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
473 rect
.setTop(topText
->pos
.y());
474 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
478 case DetailsLayout
: {
479 QRectF rect
= m_textRect
;
480 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
481 rect
.setTop(textInfo
->pos
.y());
482 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
484 const KItemListStyleOption
& option
= styleOption();
485 if (option
.extendedSelectionRegion
) {
486 const QString text
= textInfo
->staticText
.text();
487 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
500 QRectF
KStandardItemListWidget::selectionRect() const
502 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
509 case DetailsLayout
: {
510 const int padding
= styleOption().padding
;
511 QRectF adjustedIconRect
= iconRect().adjusted(-padding
, -padding
, padding
, padding
);
512 return adjustedIconRect
| m_textRect
;
523 QRectF
KStandardItemListWidget::expansionToggleRect() const
525 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
526 return m_isExpandable
? m_expansionArea
: QRectF();
529 QRectF
KStandardItemListWidget::selectionToggleRect() const
531 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
533 const int iconHeight
= styleOption().iconSize
;
535 int toggleSize
= KIconLoader::SizeSmall
;
536 if (iconHeight
>= KIconLoader::SizeEnormous
) {
537 toggleSize
= KIconLoader::SizeMedium
;
538 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
539 toggleSize
= KIconLoader::SizeSmallMedium
;
542 QPointF pos
= iconRect().topLeft();
544 // If the selection toggle has a very small distance to the
545 // widget borders, the size of the selection toggle will get
546 // increased to prevent an accidental clicking of the item
547 // when trying to hit the toggle.
548 const int widgetHeight
= size().height();
549 const int widgetWidth
= size().width();
550 const int minMargin
= 2;
552 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
553 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
554 toggleSize
= widgetHeight
;
557 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
558 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
559 toggleSize
= widgetWidth
;
563 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
566 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
569 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
570 if (m_layout
!= DetailsLayout
) {
574 // Only return the content of the text-column as pixmap
575 const int leftClip
= m_pixmapPos
.x();
577 const TextInfo
* textInfo
= m_textInfo
.value("text");
578 const int rightClip
= textInfo
->pos
.x() +
579 textInfo
->staticText
.size().width() +
580 2 * styleOption().padding
;
582 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
583 clippedPixmap
.fill(Qt::transparent
);
585 QPainter
painter(&clippedPixmap
);
586 painter
.drawPixmap(-leftClip
, 0, pixmap
);
588 return clippedPixmap
;
592 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
594 return new KStandardItemListWidgetInformant();
597 void KStandardItemListWidget::invalidateCache()
599 m_dirtyLayout
= true;
600 m_dirtyContent
= true;
603 void KStandardItemListWidget::refreshCache()
607 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
613 bool KStandardItemListWidget::isHidden() const
618 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
623 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
625 return QPalette::Text
;
628 void KStandardItemListWidget::setTextColor(const QColor
& color
)
630 if (color
!= m_customTextColor
) {
631 m_customTextColor
= color
;
632 updateAdditionalInfoTextColor();
637 QColor
KStandardItemListWidget::textColor() const
641 return m_additionalInfoTextColor
;
642 } else if (m_customTextColor
.isValid()) {
643 return m_customTextColor
;
647 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
648 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
649 return styleOption().palette
.color(group
, role
);
652 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
655 m_dirtyContent
= true;
659 QPixmap
KStandardItemListWidget::overlay() const
665 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
666 const QHash
<QByteArray
, QVariant
>& values
) const
668 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
671 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
672 const QSet
<QByteArray
>& roles
)
676 m_dirtyContent
= true;
678 QSet
<QByteArray
> dirtyRoles
;
679 if (roles
.isEmpty()) {
680 dirtyRoles
= visibleRoles().toSet();
685 // The URL might have changed (i.e., if the sort order of the items has
686 // been changed). Therefore, the "is cut" state must be updated.
687 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
688 const QUrl itemUrl
= data().value("url").value
<QUrl
>();
689 m_isCut
= clipboard
->isCut(itemUrl
);
691 // The icon-state might depend from other roles and hence is
692 // marked as dirty whenever a role has been changed
693 dirtyRoles
.insert("iconPixmap");
694 dirtyRoles
.insert("iconName");
696 QSetIterator
<QByteArray
> it(dirtyRoles
);
697 while (it
.hasNext()) {
698 const QByteArray
& role
= it
.next();
699 m_dirtyContentRoles
.insert(role
);
703 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
704 const QList
<QByteArray
>& previous
)
707 m_sortedVisibleRoles
= current
;
708 m_dirtyLayout
= true;
711 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
718 m_dirtyLayout
= true;
721 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
722 const KItemListStyleOption
& previous
)
726 updateAdditionalInfoTextColor();
727 m_dirtyLayout
= true;
730 void KStandardItemListWidget::hoveredChanged(bool hovered
)
733 m_dirtyLayout
= true;
736 void KStandardItemListWidget::selectedChanged(bool selected
)
739 updateAdditionalInfoTextColor();
740 m_dirtyContent
= true;
743 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
747 m_dirtyLayout
= true;
750 int KStandardItemListWidget::selectionLength(const QString
& text
) const
752 return text
.length();
755 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
759 QGraphicsView
* parent
= scene()->views()[0];
760 if (current
.isEmpty() || !parent
|| current
!= "text") {
762 emit
roleEditingCanceled(index(), current
, data().value(current
));
764 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
765 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
766 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
767 this, &KStandardItemListWidget::slotRoleEditingFinished
);
769 if (m_oldRoleEditor
) {
770 m_oldRoleEditor
->deleteLater();
772 m_oldRoleEditor
= m_roleEditor
;
773 m_roleEditor
->hide();
779 Q_ASSERT(!m_roleEditor
);
781 const TextInfo
* textInfo
= m_textInfo
.value("text");
783 m_roleEditor
= new KItemListRoleEditor(parent
);
784 m_roleEditor
->setRole(current
);
785 m_roleEditor
->setFont(styleOption().font
);
787 const QString text
= data().value(current
).toString();
788 m_roleEditor
->setPlainText(text
);
790 QTextOption textOption
= textInfo
->staticText
.textOption();
791 m_roleEditor
->document()->setDefaultTextOption(textOption
);
793 const int textSelectionLength
= selectionLength(text
);
795 if (textSelectionLength
> 0) {
796 QTextCursor cursor
= m_roleEditor
->textCursor();
797 cursor
.movePosition(QTextCursor::StartOfBlock
);
798 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
799 m_roleEditor
->setTextCursor(cursor
);
802 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
803 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
804 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
805 this, &KStandardItemListWidget::slotRoleEditingFinished
);
807 // Adjust the geometry of the editor
808 QRectF rect
= roleEditingRect(current
);
809 const int frameWidth
= m_roleEditor
->frameWidth();
810 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
811 rect
.translate(pos());
812 if (rect
.right() > parent
->width()) {
813 rect
.setWidth(parent
->width() - rect
.left());
815 m_roleEditor
->setGeometry(rect
.toRect());
816 m_roleEditor
->show();
817 m_roleEditor
->setFocus();
820 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
823 setEditedRole(QByteArray());
824 Q_ASSERT(!m_roleEditor
);
827 KItemListWidget::resizeEvent(event
);
829 m_dirtyLayout
= true;
832 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
834 KItemListWidget::showEvent(event
);
836 // Listen to changes of the clipboard to mark the item as cut/uncut
837 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
839 const QUrl itemUrl
= data().value("url").value
<QUrl
>();
840 m_isCut
= clipboard
->isCut(itemUrl
);
842 connect(clipboard
, &KFileItemClipboard::cutItemsChanged
,
843 this, &KStandardItemListWidget::slotCutItemsChanged
);
846 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
848 disconnect(KFileItemClipboard::instance(), &KFileItemClipboard::cutItemsChanged
,
849 this, &KStandardItemListWidget::slotCutItemsChanged
);
851 KItemListWidget::hideEvent(event
);
854 void KStandardItemListWidget::slotCutItemsChanged()
856 const QUrl itemUrl
= data().value("url").value
<QUrl
>();
857 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
858 if (m_isCut
!= isCut
) {
860 m_pixmap
= QPixmap();
861 m_dirtyContent
= true;
866 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
867 const QVariant
& value
)
870 emit
roleEditingCanceled(index(), role
, value
);
871 setEditedRole(QByteArray());
874 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
875 const QVariant
& value
)
878 emit
roleEditingFinished(index(), role
, value
);
879 setEditedRole(QByteArray());
882 void KStandardItemListWidget::triggerCacheRefreshing()
884 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
890 const QHash
<QByteArray
, QVariant
> values
= data();
891 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
892 m_isHidden
= isHidden();
893 m_customizedFont
= customizedFont(styleOption().font
);
894 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
896 updateExpansionArea();
900 m_dirtyLayout
= false;
901 m_dirtyContent
= false;
902 m_dirtyContentRoles
.clear();
905 void KStandardItemListWidget::updateExpansionArea()
907 if (m_supportsItemExpanding
) {
908 const QHash
<QByteArray
, QVariant
> values
= data();
909 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
910 if (expandedParentsCount
>= 0) {
911 const KItemListStyleOption
& option
= styleOption();
912 const qreal widgetHeight
= size().height();
913 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
914 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
916 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
921 m_expansionArea
= QRectF();
924 void KStandardItemListWidget::updatePixmapCache()
926 // Precondition: Requires already updated m_textPos values to calculate
927 // the remaining height when the alignment is vertical.
929 const QSizeF widgetSize
= size();
930 const bool iconOnTop
= (m_layout
== IconsLayout
);
931 const KItemListStyleOption
& option
= styleOption();
932 const qreal padding
= option
.padding
;
934 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
935 const int maxIconHeight
= option
.iconSize
;
937 const QHash
<QByteArray
, QVariant
> values
= data();
939 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
940 if (!updatePixmap
&& m_dirtyContent
) {
941 updatePixmap
= m_dirtyContentRoles
.isEmpty()
942 || m_dirtyContentRoles
.contains("iconPixmap")
943 || m_dirtyContentRoles
.contains("iconName")
944 || m_dirtyContentRoles
.contains("iconOverlays");
948 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
949 if (m_pixmap
.isNull()) {
950 // Use the icon that fits to the MIME-type
951 QString iconName
= values
["iconName"].toString();
952 if (iconName
.isEmpty()) {
953 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
954 // use a generic icon as fallback
955 iconName
= QLatin1String("unknown");
957 const QStringList overlays
= values
["iconOverlays"].toStringList();
958 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
);
959 // qDebug() << "attempt 2 - setting pixmap to one of size " << m_pixmap.size() << m_pixmap.devicePixelRatio();
961 } else if (m_pixmap
.width() / qApp
->devicePixelRatio() != maxIconWidth
|| m_pixmap
.height() / qApp
->devicePixelRatio() != maxIconHeight
) {
962 // A custom pixmap has been applied. Assure that the pixmap
963 // is scaled to the maximum available size.
964 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
) * qApp
->devicePixelRatio());
968 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
969 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
973 KIconEffect::semiTransparent(m_pixmap
);
976 if (m_layout
== IconsLayout
&& isSelected()) {
977 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
978 QImage image
= m_pixmap
.toImage();
979 KIconEffect::colorize(image
, color
, 0.8f
);
980 m_pixmap
= QPixmap::fromImage(image
);
984 if (!m_overlay
.isNull()) {
985 QPainter
painter(&m_pixmap
);
986 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
989 int scaledIconSize
= 0;
991 const TextInfo
* textInfo
= m_textInfo
.value("text");
992 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
994 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
995 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
996 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
997 widgetSize
.height() - 2 * padding
: maxIconHeight
;
1000 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
1001 const int maxScaledIconHeight
= scaledIconSize
;
1003 m_scaledPixmapSize
= m_pixmap
.size();
1004 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
1007 // Center horizontally and align on bottom within the icon-area
1008 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
1009 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
1011 // Center horizontally and vertically within the icon-area
1012 const TextInfo
* textInfo
= m_textInfo
.value("text");
1013 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
1014 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
1015 m_pixmapPos
.setY(padding
1016 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
1019 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
1021 // Prepare the pixmap that is used when the item gets hovered
1023 m_hoverPixmap
= m_pixmap
;
1024 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
1025 // In the KIconLoader terminology, active = hover.
1026 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
1027 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
1029 m_hoverPixmap
= m_pixmap
;
1031 } else if (hoverOpacity() <= 0.0) {
1032 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1033 m_hoverPixmap
= QPixmap();
1037 void KStandardItemListWidget::updateTextsCache()
1039 QTextOption textOption
;
1042 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
1043 textOption
.setAlignment(Qt::AlignHCenter
);
1047 textOption
.setAlignment(Qt::AlignLeft
);
1048 textOption
.setWrapMode(QTextOption::NoWrap
);
1055 qDeleteAll(m_textInfo
);
1057 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
1058 TextInfo
* textInfo
= new TextInfo();
1059 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1060 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1061 textInfo
->staticText
.setTextOption(textOption
);
1062 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1066 case IconsLayout
: updateIconsLayoutTextCache(); break;
1067 case CompactLayout
: updateCompactLayoutTextCache(); break;
1068 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1069 default: Q_ASSERT(false); break;
1072 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1073 if (ratingTextInfo
) {
1074 // The text of the rating-role has been set to empty to get
1075 // replaced by a rating-image showing the rating as stars.
1076 const KItemListStyleOption
& option
= styleOption();
1077 QSizeF ratingSize
= preferredRatingSize(option
);
1079 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1080 ? columnWidth("rating") - columnPadding(option
)
1082 if (ratingSize
.width() > availableWidth
) {
1083 ratingSize
.rwidth() = availableWidth
;
1085 m_rating
= QPixmap(ratingSize
.toSize());
1086 m_rating
.fill(Qt::transparent
);
1088 QPainter
painter(&m_rating
);
1089 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
1090 const int rating
= data().value("rating").toInt();
1091 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1092 } else if (!m_rating
.isNull()) {
1093 m_rating
= QPixmap();
1097 void KStandardItemListWidget::updateIconsLayoutTextCache()
1104 // might get wrapped above
1106 // Additional role 1
1107 // Additional role 2
1109 const QHash
<QByteArray
, QVariant
> values
= data();
1111 const KItemListStyleOption
& option
= styleOption();
1112 const qreal padding
= option
.padding
;
1113 const qreal maxWidth
= size().width() - 2 * padding
;
1114 const qreal widgetHeight
= size().height();
1115 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1117 // Initialize properties for the "text" role. It will be used as anchor
1118 // for initializing the position of the other roles.
1119 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1120 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1121 nameTextInfo
->staticText
.setText(nameText
);
1123 // Calculate the number of lines required for the name and the required width
1124 qreal nameWidth
= 0;
1125 qreal nameHeight
= 0;
1128 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1129 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1130 layout
.beginLayout();
1131 int nameLineIndex
= 0;
1132 while ((line
= layout
.createLine()).isValid()) {
1133 line
.setLineWidth(maxWidth
);
1134 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1135 nameHeight
+= line
.height();
1138 if (nameLineIndex
== option
.maxTextLines
) {
1139 // The maximum number of textlines has been reached. If this is
1140 // the case provide an elided text if necessary.
1141 const int textLength
= line
.textStart() + line
.textLength();
1142 if (textLength
< nameText
.length()) {
1143 // Elide the last line of the text
1144 qreal elidingWidth
= maxWidth
;
1145 qreal lastLineWidth
;
1147 QString lastTextLine
= nameText
.mid(line
.textStart());
1148 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
1151 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1152 nameTextInfo
->staticText
.setText(elidedText
);
1154 lastLineWidth
= m_customizedFontMetrics
.boundingRect(lastTextLine
).width();
1156 // We do the text eliding in a loop with decreasing width (1 px / iteration)
1157 // to avoid problems related to different width calculation code paths
1158 // within Qt. (see bug 337104)
1159 elidingWidth
-= 1.0;
1160 } while (lastLineWidth
> maxWidth
);
1162 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1169 // Use one line for each additional information
1170 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1171 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1172 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1174 additionalRolesCount
* lineSpacing
-
1176 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1177 nameTextInfo
->pos
.y(),
1181 // Calculate the position for each additional information
1182 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1183 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1184 if (role
== "text") {
1188 const QString text
= roleText(role
, values
);
1189 TextInfo
* textInfo
= m_textInfo
.value(role
);
1190 textInfo
->staticText
.setText(text
);
1192 qreal requiredWidth
= 0;
1194 QTextLayout
layout(text
, m_customizedFont
);
1195 QTextOption textOption
;
1196 textOption
.setWrapMode(QTextOption::NoWrap
);
1197 layout
.setTextOption(textOption
);
1199 layout
.beginLayout();
1200 QTextLine textLine
= layout
.createLine();
1201 if (textLine
.isValid()) {
1202 textLine
.setLineWidth(maxWidth
);
1203 requiredWidth
= textLine
.naturalTextWidth();
1204 if (requiredWidth
> maxWidth
) {
1205 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1206 textInfo
->staticText
.setText(elidedText
);
1207 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1208 } else if (role
== "rating") {
1209 // Use the width of the rating pixmap, because the rating text is empty.
1210 requiredWidth
= m_rating
.width();
1215 textInfo
->pos
= QPointF(padding
, y
);
1216 textInfo
->staticText
.setTextWidth(maxWidth
);
1218 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1219 m_textRect
|= textRect
;
1224 // Add a padding to the text rectangle
1225 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1228 void KStandardItemListWidget::updateCompactLayoutTextCache()
1230 // +------+ Name role
1231 // | Icon | Additional role 1
1232 // +------+ Additional role 2
1234 const QHash
<QByteArray
, QVariant
> values
= data();
1236 const KItemListStyleOption
& option
= styleOption();
1237 const qreal widgetHeight
= size().height();
1238 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1239 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1240 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1242 qreal maximumRequiredTextWidth
= 0;
1243 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1244 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1245 const qreal maxWidth
= size().width() - x
- option
.padding
;
1246 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1247 const QString text
= roleText(role
, values
);
1248 TextInfo
* textInfo
= m_textInfo
.value(role
);
1249 textInfo
->staticText
.setText(text
);
1251 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1252 if (requiredWidth
> maxWidth
) {
1253 requiredWidth
= maxWidth
;
1254 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1255 textInfo
->staticText
.setText(elidedText
);
1258 textInfo
->pos
= QPointF(x
, y
);
1259 textInfo
->staticText
.setTextWidth(maxWidth
);
1261 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1266 m_textRect
= QRectF(x
- 2 * option
.padding
, 0, maximumRequiredTextWidth
+ 3 * option
.padding
, widgetHeight
);
1269 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1271 // Precondition: Requires already updated m_expansionArea
1272 // to determine the left position.
1275 // | Icon | Name role Additional role 1 Additional role 2
1277 m_textRect
= QRectF();
1279 const KItemListStyleOption
& option
= styleOption();
1280 const QHash
<QByteArray
, QVariant
> values
= data();
1282 const qreal widgetHeight
= size().height();
1283 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1284 const int fontHeight
= m_customizedFontMetrics
.height();
1286 const qreal columnWidthInc
= columnPadding(option
);
1287 qreal firstColumnInc
= scaledIconSize
;
1288 if (m_supportsItemExpanding
) {
1289 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1291 firstColumnInc
+= option
.padding
;
1294 qreal x
= firstColumnInc
;
1295 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1297 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1298 QString text
= roleText(role
, values
);
1300 // Elide the text in case it does not fit into the available column-width
1301 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1302 const qreal roleWidth
= columnWidth(role
);
1303 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1305 const bool isTextRole
= (role
== "text");
1307 availableTextWidth
-= firstColumnInc
;
1310 if (requiredWidth
> availableTextWidth
) {
1311 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1312 requiredWidth
= m_customizedFontMetrics
.width(text
);
1315 TextInfo
* textInfo
= m_textInfo
.value(role
);
1316 textInfo
->staticText
.setText(text
);
1317 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1321 const qreal textWidth
= option
.extendedSelectionRegion
1322 ? size().width() - textInfo
->pos
.x()
1323 : requiredWidth
+ 2 * option
.padding
;
1324 m_textRect
= QRectF(textInfo
->pos
.x() - 2 * option
.padding
, 0,
1325 textWidth
+ option
.padding
, size().height());
1327 // The column after the name should always be aligned on the same x-position independent
1328 // from the expansion-level shown in the name column
1329 x
-= firstColumnInc
;
1330 } else if (isRoleRightAligned(role
)) {
1331 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1336 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1339 if (m_customTextColor
.isValid()) {
1340 c1
= m_customTextColor
;
1341 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1342 c1
= styleOption().palette
.highlightedText().color();
1344 c1
= styleOption().palette
.text().color();
1347 // For the color of the additional info the inactive text color
1348 // is not used as this might lead to unreadable text for some color schemes. Instead
1349 // the text color c1 is slightly mixed with the background color.
1350 const QColor c2
= styleOption().palette
.base().color();
1352 const int p2
= 100 - p1
;
1353 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1354 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1355 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1358 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1360 if (m_scaledPixmapSize
* qApp
->devicePixelRatio() != pixmap
.size()) {
1361 QPixmap scaledPixmap
= pixmap
;
1362 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
* qApp
->devicePixelRatio());
1363 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1365 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1366 painter
->setPen(Qt::blue
);
1367 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1370 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1374 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1376 const int siblingSize
= size().height();
1377 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1378 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1380 QStyleOption option
;
1381 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1382 bool isItemSibling
= true;
1384 const QBitArray siblings
= siblingsInformation();
1385 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1386 option
.rect
= siblingRect
;
1387 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1389 if (isItemSibling
) {
1390 option
.state
|= QStyle::State_Item
;
1391 if (m_isExpandable
) {
1392 option
.state
|= QStyle::State_Children
;
1394 if (data()["isExpanded"].toBool()) {
1395 option
.state
|= QStyle::State_Open
;
1397 isItemSibling
= false;
1400 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1402 siblingRect
.translate(-siblingRect
.width(), 0);
1406 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1408 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1413 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1414 if (m_layout
== DetailsLayout
) {
1415 rect
.setWidth(columnWidth(role
) - rect
.x());
1421 void KStandardItemListWidget::closeRoleEditor()
1423 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
1424 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
1425 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
1426 this, &KStandardItemListWidget::slotRoleEditingFinished
);
1428 if (m_roleEditor
->hasFocus()) {
1429 // If the editing was not ended by a FocusOut event, we have
1430 // to transfer the keyboard focus back to the KItemListContainer.
1431 scene()->views()[0]->parentWidget()->setFocus();
1434 if (m_oldRoleEditor
) {
1435 m_oldRoleEditor
->deleteLater();
1437 m_oldRoleEditor
= m_roleEditor
;
1438 m_roleEditor
->hide();
1442 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
)
1444 size
*= qApp
->devicePixelRatio();
1445 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(":") % ":" % QString::number(size
);
1448 if (!QPixmapCache::find(key
, pixmap
)) {
1449 const QIcon icon
= QIcon::fromTheme(name
);
1452 if (size
<= KIconLoader::SizeSmall
) {
1453 requestedSize
= KIconLoader::SizeSmall
;
1454 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1455 requestedSize
= KIconLoader::SizeSmallMedium
;
1456 } else if (size
<= KIconLoader::SizeMedium
) {
1457 requestedSize
= KIconLoader::SizeMedium
;
1458 } else if (size
<= KIconLoader::SizeLarge
) {
1459 requestedSize
= KIconLoader::SizeLarge
;
1460 } else if (size
<= KIconLoader::SizeHuge
) {
1461 requestedSize
= KIconLoader::SizeHuge
;
1462 } else if (size
<= KIconLoader::SizeEnormous
) {
1463 requestedSize
= KIconLoader::SizeEnormous
;
1464 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1465 requestedSize
= KIconLoader::SizeEnormous
* 2;
1467 requestedSize
= size
;
1470 pixmap
= icon
.pixmap(requestedSize
/ qApp
->devicePixelRatio(), requestedSize
/ qApp
->devicePixelRatio());
1471 if (requestedSize
!= size
) {
1472 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1475 // Strangely KFileItem::overlays() returns empty string-values, so
1476 // we need to check first whether an overlay must be drawn at all.
1477 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1478 // assumes that an overlay will be drawn and has some additional
1480 foreach (const QString
& overlay
, overlays
) {
1481 if (!overlay
.isEmpty()) {
1482 // There is at least one overlay, draw all overlays above m_pixmap
1483 // and cancel the check
1484 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
);
1489 QPixmapCache::insert(key
, pixmap
);
1491 pixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1496 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1498 const qreal height
= option
.fontMetrics
.ascent();
1499 return QSizeF(height
* 5, height
);
1502 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1504 return option
.padding
* 6;