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 // #define KSTANDARDITEMLISTWIDGET_DEBUG
46 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
47 KItemListWidgetInformant()
51 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
55 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
57 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
58 case KStandardItemListWidget::IconsLayout
:
59 calculateIconsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
62 case KStandardItemListWidget::CompactLayout
:
63 calculateCompactLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
66 case KStandardItemListWidget::DetailsLayout
:
67 calculateDetailsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
76 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
78 const KItemListView
* view
) const
80 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
81 const KItemListStyleOption
& option
= view
->styleOption();
83 const QString text
= roleText(role
, values
);
84 qreal width
= KStandardItemListWidget::columnPadding(option
);
86 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
87 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
89 if (role
== "rating") {
90 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
92 // If current item is a link, we use the customized link font metrics instead of the normal font metrics.
93 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
95 width
+= fontMetrics
.width(text
);
98 if (view
->supportsItemExpanding()) {
99 // Increase the width by the expansion-toggle and the current expansion level
100 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
101 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, fontMetrics
.height());
102 width
+= (expandedParentsCount
+ 1) * height
;
105 // Increase the width by the required space for the icon
106 width
+= option
.padding
* 2 + option
.iconSize
;
113 QString
KStandardItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
115 return view
->model()->data(index
).value("text").toString();
118 bool KStandardItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
125 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
126 const QHash
<QByteArray
, QVariant
>& values
) const
128 if (role
== "rating") {
129 // Always use an empty text, as the rating is shown by the image m_rating.
132 return values
.value(role
).toString();
135 QFont
KStandardItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
140 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
142 const KItemListStyleOption
& option
= view
->styleOption();
143 const QFont
& normalFont
= option
.font
;
144 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
146 const qreal itemWidth
= view
->itemSize().width();
147 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
148 const qreal additionalRolesSpacing
= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
149 const qreal spacingAndIconHeight
= option
.iconSize
+ option
.padding
* 3;
151 const QFont linkFont
= customizedFontForLinks(normalFont
);
153 QTextOption
textOption(Qt::AlignHCenter
);
154 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
156 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
157 if (logicalHeightHints
.at(index
) > 0.0) {
161 // If the current item is a link, we use the customized link font instead of the normal font.
162 const QFont
& font
= itemIsLink(index
, view
) ? linkFont
: normalFont
;
164 const QString
& text
= KStringHandler::preProcessWrap(itemText(index
, view
));
166 // Calculate the number of lines required for wrapping the name
167 qreal textHeight
= 0;
168 QTextLayout
layout(text
, font
);
169 layout
.setTextOption(textOption
);
170 layout
.beginLayout();
173 while ((line
= layout
.createLine()).isValid()) {
174 line
.setLineWidth(maxWidth
);
175 line
.naturalTextWidth();
176 textHeight
+= line
.height();
179 if (lineCount
== option
.maxTextLines
) {
185 // Add one line for each additional information
186 textHeight
+= additionalRolesSpacing
;
188 logicalHeightHints
[index
] = textHeight
+ spacingAndIconHeight
;
191 logicalWidthHint
= itemWidth
;
194 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
196 const KItemListStyleOption
& option
= view
->styleOption();
197 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
198 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
200 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
201 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
202 const qreal maxWidth
= option
.maxTextWidth
;
203 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
204 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * normalFontMetrics
.lineSpacing());
206 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
208 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
209 if (logicalHeightHints
.at(index
) > 0.0) {
213 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
214 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
216 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
217 // to show all roles without horizontal clipping.
218 qreal maximumRequiredWidth
= 0.0;
220 if (showOnlyTextRole
) {
221 maximumRequiredWidth
= fontMetrics
.width(itemText(index
, view
));
223 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
224 foreach (const QByteArray
& role
, visibleRoles
) {
225 const QString
& text
= roleText(role
, values
);
226 const qreal requiredWidth
= fontMetrics
.width(text
);
227 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
231 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
232 if (maxWidth
> 0 && width
> maxWidth
) {
236 logicalHeightHints
[index
] = width
;
239 logicalWidthHint
= height
;
242 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
244 const KItemListStyleOption
& option
= view
->styleOption();
245 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
246 logicalHeightHints
.fill(height
);
247 logicalWidthHint
= -1.0;
250 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
251 KItemListWidget(informant
, parent
),
255 m_customizedFontMetrics(m_customizedFont
),
256 m_isExpandable(false),
257 m_supportsItemExpanding(false),
259 m_dirtyContent(true),
260 m_dirtyContentRoles(),
261 m_layout(IconsLayout
),
264 m_scaledPixmapSize(),
269 m_sortedVisibleRoles(),
272 m_additionalInfoTextColor(),
280 KStandardItemListWidget::~KStandardItemListWidget()
282 qDeleteAll(m_textInfo
);
286 m_roleEditor
->deleteLater();
289 if (m_oldRoleEditor
) {
290 m_oldRoleEditor
->deleteLater();
294 void KStandardItemListWidget::setLayout(Layout layout
)
296 if (m_layout
!= layout
) {
298 m_dirtyLayout
= true;
299 updateAdditionalInfoTextColor();
304 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
309 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
311 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
312 m_supportsItemExpanding
= supportsItemExpanding
;
313 m_dirtyLayout
= true;
318 bool KStandardItemListWidget::supportsItemExpanding() const
320 return m_supportsItemExpanding
;
323 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
325 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
327 KItemListWidget::paint(painter
, option
, widget
);
329 if (!m_expansionArea
.isEmpty()) {
330 drawSiblingsInformation(painter
);
333 const KItemListStyleOption
& itemListStyleOption
= styleOption();
335 if (hoverOpacity() < 1.0) {
337 * Linear interpolation between m_pixmap and m_hoverPixmap.
339 * Note that this cannot be achieved by painting m_hoverPixmap over
340 * m_pixmap, even if the opacities are adjusted. For details see
341 * https://git.reviewboard.kde.org/r/109614/
343 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
344 QPixmap
pixmap1(m_pixmap
.size());
345 pixmap1
.fill(Qt::transparent
);
347 QPainter
p(&pixmap1
);
348 p
.setOpacity(1.0 - hoverOpacity());
349 p
.drawPixmap(0, 0, m_pixmap
);
352 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
353 QPixmap
pixmap2(pixmap1
.size());
354 pixmap2
.fill(Qt::transparent
);
356 QPainter
p(&pixmap2
);
357 p
.setOpacity(hoverOpacity());
358 p
.drawPixmap(0, 0, m_hoverPixmap
);
361 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
362 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
363 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
365 QPainter
p(&pixmap1
);
366 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
367 p
.drawPixmap(0, 0, pixmap2
);
370 // Finally paint pixmap1 on the widget
371 drawPixmap(painter
, pixmap1
);
373 drawPixmap(painter
, m_hoverPixmap
);
376 drawPixmap(painter
, m_pixmap
);
379 painter
->setFont(m_customizedFont
);
380 painter
->setPen(textColor());
381 const TextInfo
* textInfo
= m_textInfo
.value("text");
384 // It seems that we can end up here even if m_textInfo does not contain
385 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
386 // this can only happen if the index is negative. This can happen when
387 // the item is about to be removed, see KItemListView::slotItemsRemoved().
388 // TODO: try to reproduce the crash and find a better fix.
392 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
394 bool clipAdditionalInfoBounds
= false;
395 if (m_supportsItemExpanding
) {
396 // Prevent a possible overlapping of the additional-information texts
397 // with the icon. This can happen if the user has minimized the width
398 // of the name-column to a very small value.
399 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
400 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
401 clipAdditionalInfoBounds
= true;
403 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
407 painter
->setPen(m_additionalInfoTextColor
);
408 painter
->setFont(m_customizedFont
);
410 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
411 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
412 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
415 if (!m_rating
.isNull()) {
416 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
417 QPointF pos
= ratingTextInfo
->pos
;
418 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
419 if (align
& Qt::AlignHCenter
) {
420 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
422 painter
->drawPixmap(pos
, m_rating
);
425 if (clipAdditionalInfoBounds
) {
429 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
430 painter
->setBrush(Qt::NoBrush
);
431 painter
->setPen(Qt::green
);
432 painter
->drawRect(m_iconRect
);
434 painter
->setPen(Qt::blue
);
435 painter
->drawRect(m_textRect
);
437 painter
->setPen(Qt::red
);
438 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
439 painter
->drawRect(rect());
443 QRectF
KStandardItemListWidget::iconRect() const
445 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
449 QRectF
KStandardItemListWidget::textRect() const
451 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
455 QRectF
KStandardItemListWidget::textFocusRect() const
457 // In the compact- and details-layout a larger textRect() is returned to be aligned
458 // with the iconRect(). This is useful to have a larger selection/hover-area
459 // when having a quite large icon size but only one line of text. Still the
460 // focus rectangle should be shown as narrow as possible around the text.
462 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
465 case CompactLayout
: {
466 QRectF rect
= m_textRect
;
467 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
468 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
469 rect
.setTop(topText
->pos
.y());
470 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
474 case DetailsLayout
: {
475 QRectF rect
= m_textRect
;
476 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
477 rect
.setTop(textInfo
->pos
.y());
478 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
480 const KItemListStyleOption
& option
= styleOption();
481 if (option
.extendedSelectionRegion
) {
482 const QString text
= textInfo
->staticText
.text();
483 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
496 QRectF
KStandardItemListWidget::selectionRect() const
498 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
505 case DetailsLayout
: {
506 const int padding
= styleOption().padding
;
507 QRectF adjustedIconRect
= iconRect().adjusted(-padding
, -padding
, padding
, padding
);
508 return adjustedIconRect
| m_textRect
;
519 QRectF
KStandardItemListWidget::expansionToggleRect() const
521 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
522 return m_isExpandable
? m_expansionArea
: QRectF();
525 QRectF
KStandardItemListWidget::selectionToggleRect() const
527 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
529 const int iconHeight
= styleOption().iconSize
;
531 int toggleSize
= KIconLoader::SizeSmall
;
532 if (iconHeight
>= KIconLoader::SizeEnormous
) {
533 toggleSize
= KIconLoader::SizeMedium
;
534 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
535 toggleSize
= KIconLoader::SizeSmallMedium
;
538 QPointF pos
= iconRect().topLeft();
540 // If the selection toggle has a very small distance to the
541 // widget borders, the size of the selection toggle will get
542 // increased to prevent an accidental clicking of the item
543 // when trying to hit the toggle.
544 const int widgetHeight
= size().height();
545 const int widgetWidth
= size().width();
546 const int minMargin
= 2;
548 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
549 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
550 toggleSize
= widgetHeight
;
553 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
554 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
555 toggleSize
= widgetWidth
;
559 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
562 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
565 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
566 if (m_layout
!= DetailsLayout
) {
570 // Only return the content of the text-column as pixmap
571 const int leftClip
= m_pixmapPos
.x();
573 const TextInfo
* textInfo
= m_textInfo
.value("text");
574 const int rightClip
= textInfo
->pos
.x() +
575 textInfo
->staticText
.size().width() +
576 2 * styleOption().padding
;
578 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
579 clippedPixmap
.fill(Qt::transparent
);
581 QPainter
painter(&clippedPixmap
);
582 painter
.drawPixmap(-leftClip
, 0, pixmap
);
584 return clippedPixmap
;
588 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
590 return new KStandardItemListWidgetInformant();
593 void KStandardItemListWidget::invalidateCache()
595 m_dirtyLayout
= true;
596 m_dirtyContent
= true;
599 void KStandardItemListWidget::refreshCache()
603 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
609 bool KStandardItemListWidget::isHidden() const
614 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
619 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
621 return QPalette::Text
;
624 void KStandardItemListWidget::setTextColor(const QColor
& color
)
626 if (color
!= m_customTextColor
) {
627 m_customTextColor
= color
;
628 updateAdditionalInfoTextColor();
633 QColor
KStandardItemListWidget::textColor() const
637 return m_additionalInfoTextColor
;
638 } else if (m_customTextColor
.isValid()) {
639 return m_customTextColor
;
643 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
644 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
645 return styleOption().palette
.color(group
, role
);
648 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
651 m_dirtyContent
= true;
655 QPixmap
KStandardItemListWidget::overlay() const
661 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
662 const QHash
<QByteArray
, QVariant
>& values
) const
664 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
667 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
668 const QSet
<QByteArray
>& roles
)
672 m_dirtyContent
= true;
674 QSet
<QByteArray
> dirtyRoles
;
675 if (roles
.isEmpty()) {
676 dirtyRoles
= visibleRoles().toSet();
681 // The URL might have changed (i.e., if the sort order of the items has
682 // been changed). Therefore, the "is cut" state must be updated.
683 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
684 const QUrl itemUrl
= data().value("url").value
<QUrl
>();
685 m_isCut
= clipboard
->isCut(itemUrl
);
687 // The icon-state might depend from other roles and hence is
688 // marked as dirty whenever a role has been changed
689 dirtyRoles
.insert("iconPixmap");
690 dirtyRoles
.insert("iconName");
692 QSetIterator
<QByteArray
> it(dirtyRoles
);
693 while (it
.hasNext()) {
694 const QByteArray
& role
= it
.next();
695 m_dirtyContentRoles
.insert(role
);
699 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
700 const QList
<QByteArray
>& previous
)
703 m_sortedVisibleRoles
= current
;
704 m_dirtyLayout
= true;
707 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
714 m_dirtyLayout
= true;
717 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
718 const KItemListStyleOption
& previous
)
722 updateAdditionalInfoTextColor();
723 m_dirtyLayout
= true;
726 void KStandardItemListWidget::hoveredChanged(bool hovered
)
729 m_dirtyLayout
= true;
732 void KStandardItemListWidget::selectedChanged(bool selected
)
735 updateAdditionalInfoTextColor();
736 m_dirtyContent
= true;
739 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
743 m_dirtyLayout
= true;
746 int KStandardItemListWidget::selectionLength(const QString
& text
) const
748 return text
.length();
751 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
755 QGraphicsView
* parent
= scene()->views()[0];
756 if (current
.isEmpty() || !parent
|| current
!= "text") {
758 emit
roleEditingCanceled(index(), current
, data().value(current
));
760 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
761 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
762 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
763 this, &KStandardItemListWidget::slotRoleEditingFinished
);
765 if (m_oldRoleEditor
) {
766 m_oldRoleEditor
->deleteLater();
768 m_oldRoleEditor
= m_roleEditor
;
769 m_roleEditor
->hide();
775 Q_ASSERT(!m_roleEditor
);
777 const TextInfo
* textInfo
= m_textInfo
.value("text");
779 m_roleEditor
= new KItemListRoleEditor(parent
);
780 m_roleEditor
->setRole(current
);
781 m_roleEditor
->setFont(styleOption().font
);
783 const QString text
= data().value(current
).toString();
784 m_roleEditor
->setPlainText(text
);
786 QTextOption textOption
= textInfo
->staticText
.textOption();
787 m_roleEditor
->document()->setDefaultTextOption(textOption
);
789 const int textSelectionLength
= selectionLength(text
);
791 if (textSelectionLength
> 0) {
792 QTextCursor cursor
= m_roleEditor
->textCursor();
793 cursor
.movePosition(QTextCursor::StartOfBlock
);
794 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
795 m_roleEditor
->setTextCursor(cursor
);
798 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
799 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
800 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
801 this, &KStandardItemListWidget::slotRoleEditingFinished
);
803 // Adjust the geometry of the editor
804 QRectF rect
= roleEditingRect(current
);
805 const int frameWidth
= m_roleEditor
->frameWidth();
806 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
807 rect
.translate(pos());
808 if (rect
.right() > parent
->width()) {
809 rect
.setWidth(parent
->width() - rect
.left());
811 m_roleEditor
->setGeometry(rect
.toRect());
812 m_roleEditor
->show();
813 m_roleEditor
->setFocus();
816 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
819 setEditedRole(QByteArray());
820 Q_ASSERT(!m_roleEditor
);
823 KItemListWidget::resizeEvent(event
);
825 m_dirtyLayout
= true;
828 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
830 KItemListWidget::showEvent(event
);
832 // Listen to changes of the clipboard to mark the item as cut/uncut
833 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
835 const QUrl itemUrl
= data().value("url").value
<QUrl
>();
836 m_isCut
= clipboard
->isCut(itemUrl
);
838 connect(clipboard
, &KFileItemClipboard::cutItemsChanged
,
839 this, &KStandardItemListWidget::slotCutItemsChanged
);
842 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
844 disconnect(KFileItemClipboard::instance(), &KFileItemClipboard::cutItemsChanged
,
845 this, &KStandardItemListWidget::slotCutItemsChanged
);
847 KItemListWidget::hideEvent(event
);
850 void KStandardItemListWidget::slotCutItemsChanged()
852 const QUrl itemUrl
= data().value("url").value
<QUrl
>();
853 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
854 if (m_isCut
!= isCut
) {
856 m_pixmap
= QPixmap();
857 m_dirtyContent
= true;
862 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
863 const QVariant
& value
)
866 emit
roleEditingCanceled(index(), role
, value
);
867 setEditedRole(QByteArray());
870 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
871 const QVariant
& value
)
874 emit
roleEditingFinished(index(), role
, value
);
875 setEditedRole(QByteArray());
878 void KStandardItemListWidget::triggerCacheRefreshing()
880 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
886 const QHash
<QByteArray
, QVariant
> values
= data();
887 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
888 m_isHidden
= isHidden();
889 m_customizedFont
= customizedFont(styleOption().font
);
890 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
892 updateExpansionArea();
896 m_dirtyLayout
= false;
897 m_dirtyContent
= false;
898 m_dirtyContentRoles
.clear();
901 void KStandardItemListWidget::updateExpansionArea()
903 if (m_supportsItemExpanding
) {
904 const QHash
<QByteArray
, QVariant
> values
= data();
905 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
906 if (expandedParentsCount
>= 0) {
907 const KItemListStyleOption
& option
= styleOption();
908 const qreal widgetHeight
= size().height();
909 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
910 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
912 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
917 m_expansionArea
= QRectF();
920 void KStandardItemListWidget::updatePixmapCache()
922 // Precondition: Requires already updated m_textPos values to calculate
923 // the remaining height when the alignment is vertical.
925 const QSizeF widgetSize
= size();
926 const bool iconOnTop
= (m_layout
== IconsLayout
);
927 const KItemListStyleOption
& option
= styleOption();
928 const qreal padding
= option
.padding
;
930 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
931 const int maxIconHeight
= option
.iconSize
;
933 const QHash
<QByteArray
, QVariant
> values
= data();
935 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
936 if (!updatePixmap
&& m_dirtyContent
) {
937 updatePixmap
= m_dirtyContentRoles
.isEmpty()
938 || m_dirtyContentRoles
.contains("iconPixmap")
939 || m_dirtyContentRoles
.contains("iconName")
940 || m_dirtyContentRoles
.contains("iconOverlays");
944 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
945 if (m_pixmap
.isNull()) {
946 // Use the icon that fits to the MIME-type
947 QString iconName
= values
["iconName"].toString();
948 if (iconName
.isEmpty()) {
949 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
950 // use a generic icon as fallback
951 iconName
= QLatin1String("unknown");
953 const QStringList overlays
= values
["iconOverlays"].toStringList();
954 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
);
955 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
956 // A custom pixmap has been applied. Assure that the pixmap
957 // is scaled to the maximum available size.
958 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
962 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
963 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
967 KIconEffect::semiTransparent(m_pixmap
);
970 if (m_layout
== IconsLayout
&& isSelected()) {
971 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
972 QImage image
= m_pixmap
.toImage();
973 KIconEffect::colorize(image
, color
, 0.8f
);
974 m_pixmap
= QPixmap::fromImage(image
);
978 if (!m_overlay
.isNull()) {
979 QPainter
painter(&m_pixmap
);
980 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
983 int scaledIconSize
= 0;
985 const TextInfo
* textInfo
= m_textInfo
.value("text");
986 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
988 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
989 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
990 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
991 widgetSize
.height() - 2 * padding
: maxIconHeight
;
994 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
995 const int maxScaledIconHeight
= scaledIconSize
;
997 m_scaledPixmapSize
= m_pixmap
.size();
998 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
1001 // Center horizontally and align on bottom within the icon-area
1002 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
1003 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
1005 // Center horizontally and vertically within the icon-area
1006 const TextInfo
* textInfo
= m_textInfo
.value("text");
1007 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
1008 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
1009 m_pixmapPos
.setY(padding
1010 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
1013 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
1015 // Prepare the pixmap that is used when the item gets hovered
1017 m_hoverPixmap
= m_pixmap
;
1018 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
1019 // In the KIconLoader terminology, active = hover.
1020 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
1021 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
1023 m_hoverPixmap
= m_pixmap
;
1025 } else if (hoverOpacity() <= 0.0) {
1026 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1027 m_hoverPixmap
= QPixmap();
1031 void KStandardItemListWidget::updateTextsCache()
1033 QTextOption textOption
;
1036 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
1037 textOption
.setAlignment(Qt::AlignHCenter
);
1041 textOption
.setAlignment(Qt::AlignLeft
);
1042 textOption
.setWrapMode(QTextOption::NoWrap
);
1049 qDeleteAll(m_textInfo
);
1051 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
1052 TextInfo
* textInfo
= new TextInfo();
1053 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1054 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1055 textInfo
->staticText
.setTextOption(textOption
);
1056 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1060 case IconsLayout
: updateIconsLayoutTextCache(); break;
1061 case CompactLayout
: updateCompactLayoutTextCache(); break;
1062 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1063 default: Q_ASSERT(false); break;
1066 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1067 if (ratingTextInfo
) {
1068 // The text of the rating-role has been set to empty to get
1069 // replaced by a rating-image showing the rating as stars.
1070 const KItemListStyleOption
& option
= styleOption();
1071 QSizeF ratingSize
= preferredRatingSize(option
);
1073 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1074 ? columnWidth("rating") - columnPadding(option
)
1076 if (ratingSize
.width() > availableWidth
) {
1077 ratingSize
.rwidth() = availableWidth
;
1079 m_rating
= QPixmap(ratingSize
.toSize());
1080 m_rating
.fill(Qt::transparent
);
1082 QPainter
painter(&m_rating
);
1083 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
1084 const int rating
= data().value("rating").toInt();
1085 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1086 } else if (!m_rating
.isNull()) {
1087 m_rating
= QPixmap();
1091 void KStandardItemListWidget::updateIconsLayoutTextCache()
1098 // might get wrapped above
1100 // Additional role 1
1101 // Additional role 2
1103 const QHash
<QByteArray
, QVariant
> values
= data();
1105 const KItemListStyleOption
& option
= styleOption();
1106 const qreal padding
= option
.padding
;
1107 const qreal maxWidth
= size().width() - 2 * padding
;
1108 const qreal widgetHeight
= size().height();
1109 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1111 // Initialize properties for the "text" role. It will be used as anchor
1112 // for initializing the position of the other roles.
1113 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1114 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1115 nameTextInfo
->staticText
.setText(nameText
);
1117 // Calculate the number of lines required for the name and the required width
1118 qreal nameWidth
= 0;
1119 qreal nameHeight
= 0;
1122 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1123 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1124 layout
.beginLayout();
1125 int nameLineIndex
= 0;
1126 while ((line
= layout
.createLine()).isValid()) {
1127 line
.setLineWidth(maxWidth
);
1128 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1129 nameHeight
+= line
.height();
1132 if (nameLineIndex
== option
.maxTextLines
) {
1133 // The maximum number of textlines has been reached. If this is
1134 // the case provide an elided text if necessary.
1135 const int textLength
= line
.textStart() + line
.textLength();
1136 if (textLength
< nameText
.length()) {
1137 // Elide the last line of the text
1138 qreal elidingWidth
= maxWidth
;
1139 qreal lastLineWidth
;
1141 QString lastTextLine
= nameText
.mid(line
.textStart());
1142 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
1145 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1146 nameTextInfo
->staticText
.setText(elidedText
);
1148 lastLineWidth
= m_customizedFontMetrics
.boundingRect(lastTextLine
).width();
1150 // We do the text eliding in a loop with decreasing width (1 px / iteration)
1151 // to avoid problems related to different width calculation code paths
1152 // within Qt. (see bug 337104)
1153 elidingWidth
-= 1.0;
1154 } while (lastLineWidth
> maxWidth
);
1156 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1163 // Use one line for each additional information
1164 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1165 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1166 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1168 additionalRolesCount
* lineSpacing
-
1170 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1171 nameTextInfo
->pos
.y(),
1175 // Calculate the position for each additional information
1176 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1177 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1178 if (role
== "text") {
1182 const QString text
= roleText(role
, values
);
1183 TextInfo
* textInfo
= m_textInfo
.value(role
);
1184 textInfo
->staticText
.setText(text
);
1186 qreal requiredWidth
= 0;
1188 QTextLayout
layout(text
, m_customizedFont
);
1189 QTextOption textOption
;
1190 textOption
.setWrapMode(QTextOption::NoWrap
);
1191 layout
.setTextOption(textOption
);
1193 layout
.beginLayout();
1194 QTextLine textLine
= layout
.createLine();
1195 if (textLine
.isValid()) {
1196 textLine
.setLineWidth(maxWidth
);
1197 requiredWidth
= textLine
.naturalTextWidth();
1198 if (requiredWidth
> maxWidth
) {
1199 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1200 textInfo
->staticText
.setText(elidedText
);
1201 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1202 } else if (role
== "rating") {
1203 // Use the width of the rating pixmap, because the rating text is empty.
1204 requiredWidth
= m_rating
.width();
1209 textInfo
->pos
= QPointF(padding
, y
);
1210 textInfo
->staticText
.setTextWidth(maxWidth
);
1212 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1213 m_textRect
|= textRect
;
1218 // Add a padding to the text rectangle
1219 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1222 void KStandardItemListWidget::updateCompactLayoutTextCache()
1224 // +------+ Name role
1225 // | Icon | Additional role 1
1226 // +------+ Additional role 2
1228 const QHash
<QByteArray
, QVariant
> values
= data();
1230 const KItemListStyleOption
& option
= styleOption();
1231 const qreal widgetHeight
= size().height();
1232 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1233 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1234 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1236 qreal maximumRequiredTextWidth
= 0;
1237 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1238 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1239 const qreal maxWidth
= size().width() - x
- option
.padding
;
1240 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1241 const QString text
= roleText(role
, values
);
1242 TextInfo
* textInfo
= m_textInfo
.value(role
);
1243 textInfo
->staticText
.setText(text
);
1245 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1246 if (requiredWidth
> maxWidth
) {
1247 requiredWidth
= maxWidth
;
1248 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1249 textInfo
->staticText
.setText(elidedText
);
1252 textInfo
->pos
= QPointF(x
, y
);
1253 textInfo
->staticText
.setTextWidth(maxWidth
);
1255 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1260 m_textRect
= QRectF(x
- 2 * option
.padding
, 0, maximumRequiredTextWidth
+ 3 * option
.padding
, widgetHeight
);
1263 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1265 // Precondition: Requires already updated m_expansionArea
1266 // to determine the left position.
1269 // | Icon | Name role Additional role 1 Additional role 2
1271 m_textRect
= QRectF();
1273 const KItemListStyleOption
& option
= styleOption();
1274 const QHash
<QByteArray
, QVariant
> values
= data();
1276 const qreal widgetHeight
= size().height();
1277 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1278 const int fontHeight
= m_customizedFontMetrics
.height();
1280 const qreal columnWidthInc
= columnPadding(option
);
1281 qreal firstColumnInc
= scaledIconSize
;
1282 if (m_supportsItemExpanding
) {
1283 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1285 firstColumnInc
+= option
.padding
;
1288 qreal x
= firstColumnInc
;
1289 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1291 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1292 QString text
= roleText(role
, values
);
1294 // Elide the text in case it does not fit into the available column-width
1295 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1296 const qreal roleWidth
= columnWidth(role
);
1297 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1299 const bool isTextRole
= (role
== "text");
1301 availableTextWidth
-= firstColumnInc
;
1304 if (requiredWidth
> availableTextWidth
) {
1305 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1306 requiredWidth
= m_customizedFontMetrics
.width(text
);
1309 TextInfo
* textInfo
= m_textInfo
.value(role
);
1310 textInfo
->staticText
.setText(text
);
1311 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1315 const qreal textWidth
= option
.extendedSelectionRegion
1316 ? size().width() - textInfo
->pos
.x()
1317 : requiredWidth
+ 2 * option
.padding
;
1318 m_textRect
= QRectF(textInfo
->pos
.x() - 2 * option
.padding
, 0,
1319 textWidth
+ option
.padding
, size().height());
1321 // The column after the name should always be aligned on the same x-position independent
1322 // from the expansion-level shown in the name column
1323 x
-= firstColumnInc
;
1324 } else if (isRoleRightAligned(role
)) {
1325 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1330 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1333 if (m_customTextColor
.isValid()) {
1334 c1
= m_customTextColor
;
1335 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1336 c1
= styleOption().palette
.highlightedText().color();
1338 c1
= styleOption().palette
.text().color();
1341 // For the color of the additional info the inactive text color
1342 // is not used as this might lead to unreadable text for some color schemes. Instead
1343 // the text color c1 is slightly mixed with the background color.
1344 const QColor c2
= styleOption().palette
.base().color();
1346 const int p2
= 100 - p1
;
1347 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1348 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1349 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1352 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1354 if (m_scaledPixmapSize
!= pixmap
.size()) {
1355 QPixmap scaledPixmap
= pixmap
;
1356 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
1357 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1359 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1360 painter
->setPen(Qt::blue
);
1361 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1364 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1368 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1370 const int siblingSize
= size().height();
1371 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1372 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1374 QStyleOption option
;
1375 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1376 bool isItemSibling
= true;
1378 const QBitArray siblings
= siblingsInformation();
1379 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1380 option
.rect
= siblingRect
;
1381 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1383 if (isItemSibling
) {
1384 option
.state
|= QStyle::State_Item
;
1385 if (m_isExpandable
) {
1386 option
.state
|= QStyle::State_Children
;
1388 if (data()["isExpanded"].toBool()) {
1389 option
.state
|= QStyle::State_Open
;
1391 isItemSibling
= false;
1394 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1396 siblingRect
.translate(-siblingRect
.width(), 0);
1400 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1402 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1407 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1408 if (m_layout
== DetailsLayout
) {
1409 rect
.setWidth(columnWidth(role
) - rect
.x());
1415 void KStandardItemListWidget::closeRoleEditor()
1417 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
1418 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
1419 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
1420 this, &KStandardItemListWidget::slotRoleEditingFinished
);
1422 if (m_roleEditor
->hasFocus()) {
1423 // If the editing was not ended by a FocusOut event, we have
1424 // to transfer the keyboard focus back to the KItemListContainer.
1425 scene()->views()[0]->parentWidget()->setFocus();
1428 if (m_oldRoleEditor
) {
1429 m_oldRoleEditor
->deleteLater();
1431 m_oldRoleEditor
= m_roleEditor
;
1432 m_roleEditor
->hide();
1436 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
)
1438 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(":") % ":" % QString::number(size
);
1441 if (!QPixmapCache::find(key
, pixmap
)) {
1442 const QIcon icon
= QIcon::fromTheme(name
);
1445 if (size
<= KIconLoader::SizeSmall
) {
1446 requestedSize
= KIconLoader::SizeSmall
;
1447 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1448 requestedSize
= KIconLoader::SizeSmallMedium
;
1449 } else if (size
<= KIconLoader::SizeMedium
) {
1450 requestedSize
= KIconLoader::SizeMedium
;
1451 } else if (size
<= KIconLoader::SizeLarge
) {
1452 requestedSize
= KIconLoader::SizeLarge
;
1453 } else if (size
<= KIconLoader::SizeHuge
) {
1454 requestedSize
= KIconLoader::SizeHuge
;
1455 } else if (size
<= KIconLoader::SizeEnormous
) {
1456 requestedSize
= KIconLoader::SizeEnormous
;
1457 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1458 requestedSize
= KIconLoader::SizeEnormous
* 2;
1460 requestedSize
= size
;
1463 pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
1464 if (requestedSize
!= size
) {
1465 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1468 // Strangely KFileItem::overlays() returns empty string-values, so
1469 // we need to check first whether an overlay must be drawn at all.
1470 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1471 // assumes that an overlay will be drawn and has some additional
1473 foreach (const QString
& overlay
, overlays
) {
1474 if (!overlay
.isEmpty()) {
1475 // There is at least one overlay, draw all overlays above m_pixmap
1476 // and cancel the check
1477 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
);
1482 QPixmapCache::insert(key
, pixmap
);
1488 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1490 const qreal height
= option
.fontMetrics
.ascent();
1491 return QSizeF(height
* 5, height
);
1494 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1496 return option
.padding
* 6;