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>
29 #include <kratingpainter.h>
30 #include <KStringHandler>
33 #include "private/kfileitemclipboard.h"
34 #include "private/kitemlistroleeditor.h"
35 #include "private/kpixmapmodifier.h"
37 #include <QFontMetricsF>
38 #include <QGraphicsScene>
39 #include <QGraphicsSceneResizeEvent>
40 #include <QGraphicsView>
42 #include <QStyleOption>
43 #include <QTextLayout>
45 #include <QPixmapCache>
47 // #define KSTANDARDITEMLISTWIDGET_DEBUG
49 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
50 KItemListWidgetInformant()
54 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
58 void KStandardItemListWidgetInformant::calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
60 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
61 case KStandardItemListWidget::IconsLayout
:
62 calculateIconsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
65 case KStandardItemListWidget::CompactLayout
:
66 calculateCompactLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
69 case KStandardItemListWidget::DetailsLayout
:
70 calculateDetailsLayoutItemSizeHints(logicalHeightHints
, logicalWidthHint
, view
);
79 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
81 const KItemListView
* view
) const
83 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
84 const KItemListStyleOption
& option
= view
->styleOption();
86 const QString text
= roleText(role
, values
);
87 qreal width
= KStandardItemListWidget::columnPadding(option
);
89 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
90 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
92 if (role
== "rating") {
93 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
95 // If current item is a link, we use the customized link font metrics instead of the normal font metrics.
96 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
98 width
+= fontMetrics
.width(text
);
100 if (role
== "text") {
101 if (view
->supportsItemExpanding()) {
102 // Increase the width by the expansion-toggle and the current expansion level
103 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
104 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, fontMetrics
.height());
105 width
+= (expandedParentsCount
+ 1) * height
;
108 // Increase the width by the required space for the icon
109 width
+= option
.padding
* 2 + option
.iconSize
;
116 QString
KStandardItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
118 return view
->model()->data(index
).value("text").toString();
121 bool KStandardItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
126 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
127 const QHash
<QByteArray
, QVariant
>& values
) const
129 if (role
== "rating") {
130 // Always use an empty text, as the rating is shown by the image m_rating.
133 return values
.value(role
).toString();
136 QFont
KStandardItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
141 void KStandardItemListWidgetInformant::calculateIconsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
143 const KItemListStyleOption
& option
= view
->styleOption();
144 const QFont
& normalFont
= option
.font
;
145 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
147 const qreal itemWidth
= view
->itemSize().width();
148 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
149 const qreal additionalRolesSpacing
= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
150 const qreal spacingAndIconHeight
= option
.iconSize
+ option
.padding
* 3;
152 const QFont linkFont
= customizedFontForLinks(normalFont
);
154 QTextOption
textOption(Qt::AlignHCenter
);
155 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
157 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
158 if (logicalHeightHints
.at(index
) > 0.0) {
162 // If the current item is a link, we use the customized link font instead of the normal font.
163 const QFont
& font
= itemIsLink(index
, view
) ? linkFont
: normalFont
;
165 const QString
& text
= KStringHandler::preProcessWrap(itemText(index
, view
));
167 // Calculate the number of lines required for wrapping the name
168 qreal textHeight
= 0;
169 QTextLayout
layout(text
, font
);
170 layout
.setTextOption(textOption
);
171 layout
.beginLayout();
174 while ((line
= layout
.createLine()).isValid()) {
175 line
.setLineWidth(maxWidth
);
176 line
.naturalTextWidth();
177 textHeight
+= line
.height();
180 if (lineCount
== option
.maxTextLines
) {
186 // Add one line for each additional information
187 textHeight
+= additionalRolesSpacing
;
189 logicalHeightHints
[index
] = textHeight
+ spacingAndIconHeight
;
192 logicalWidthHint
= itemWidth
;
195 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
197 const KItemListStyleOption
& option
= view
->styleOption();
198 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
199 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
201 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
202 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
203 const qreal maxWidth
= option
.maxTextWidth
;
204 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
205 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * normalFontMetrics
.lineSpacing());
207 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
209 for (int index
= 0; index
< logicalHeightHints
.count(); ++index
) {
210 if (logicalHeightHints
.at(index
) > 0.0) {
214 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
215 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
217 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
218 // to show all roles without horizontal clipping.
219 qreal maximumRequiredWidth
= 0.0;
221 if (showOnlyTextRole
) {
222 maximumRequiredWidth
= fontMetrics
.width(itemText(index
, view
));
224 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
225 foreach (const QByteArray
& role
, visibleRoles
) {
226 const QString
& text
= roleText(role
, values
);
227 const qreal requiredWidth
= fontMetrics
.width(text
);
228 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
232 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
233 if (maxWidth
> 0 && width
> maxWidth
) {
237 logicalHeightHints
[index
] = width
;
240 logicalWidthHint
= height
;
243 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const
245 const KItemListStyleOption
& option
= view
->styleOption();
246 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
247 logicalHeightHints
.fill(height
);
248 logicalWidthHint
= -1.0;
251 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
252 KItemListWidget(informant
, parent
),
256 m_customizedFontMetrics(m_customizedFont
),
257 m_isExpandable(false),
258 m_supportsItemExpanding(false),
260 m_dirtyContent(true),
261 m_dirtyContentRoles(),
262 m_layout(IconsLayout
),
265 m_scaledPixmapSize(),
270 m_sortedVisibleRoles(),
273 m_additionalInfoTextColor(),
281 KStandardItemListWidget::~KStandardItemListWidget()
283 qDeleteAll(m_textInfo
);
287 m_roleEditor
->deleteLater();
290 if (m_oldRoleEditor
) {
291 m_oldRoleEditor
->deleteLater();
295 void KStandardItemListWidget::setLayout(Layout layout
)
297 if (m_layout
!= layout
) {
299 m_dirtyLayout
= true;
300 updateAdditionalInfoTextColor();
305 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
310 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
312 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
313 m_supportsItemExpanding
= supportsItemExpanding
;
314 m_dirtyLayout
= true;
319 bool KStandardItemListWidget::supportsItemExpanding() const
321 return m_supportsItemExpanding
;
324 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
326 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
328 KItemListWidget::paint(painter
, option
, widget
);
330 if (!m_expansionArea
.isEmpty()) {
331 drawSiblingsInformation(painter
);
334 const KItemListStyleOption
& itemListStyleOption
= styleOption();
336 if (hoverOpacity() < 1.0) {
338 * Linear interpolation between m_pixmap and m_hoverPixmap.
340 * Note that this cannot be achieved by painting m_hoverPixmap over
341 * m_pixmap, even if the opacities are adjusted. For details see
342 * https://git.reviewboard.kde.org/r/109614/
344 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
345 QPixmap
pixmap1(m_pixmap
.size());
346 pixmap1
.fill(Qt::transparent
);
348 QPainter
p(&pixmap1
);
349 p
.setOpacity(1.0 - hoverOpacity());
350 p
.drawPixmap(0, 0, m_pixmap
);
353 // Paint pixmap2 so that pixmap2 = m_hoverPixmap * hoverOpacity()
354 QPixmap
pixmap2(pixmap1
.size());
355 pixmap2
.fill(Qt::transparent
);
357 QPainter
p(&pixmap2
);
358 p
.setOpacity(hoverOpacity());
359 p
.drawPixmap(0, 0, m_hoverPixmap
);
362 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
363 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
364 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
366 QPainter
p(&pixmap1
);
367 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
368 p
.drawPixmap(0, 0, pixmap2
);
371 // Finally paint pixmap1 on the widget
372 drawPixmap(painter
, pixmap1
);
374 drawPixmap(painter
, m_hoverPixmap
);
377 drawPixmap(painter
, m_pixmap
);
380 painter
->setFont(m_customizedFont
);
381 painter
->setPen(textColor());
382 const TextInfo
* textInfo
= m_textInfo
.value("text");
385 // It seems that we can end up here even if m_textInfo does not contain
386 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
387 // this can only happen if the index is negative. This can happen when
388 // the item is about to be removed, see KItemListView::slotItemsRemoved().
389 // TODO: try to reproduce the crash and find a better fix.
393 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
395 bool clipAdditionalInfoBounds
= false;
396 if (m_supportsItemExpanding
) {
397 // Prevent a possible overlapping of the additional-information texts
398 // with the icon. This can happen if the user has minimized the width
399 // of the name-column to a very small value.
400 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
401 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
402 clipAdditionalInfoBounds
= true;
404 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
408 painter
->setPen(m_additionalInfoTextColor
);
409 painter
->setFont(m_customizedFont
);
411 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
412 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
413 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
416 if (!m_rating
.isNull()) {
417 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
418 QPointF pos
= ratingTextInfo
->pos
;
419 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
420 if (align
& Qt::AlignHCenter
) {
421 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
423 painter
->drawPixmap(pos
, m_rating
);
426 if (clipAdditionalInfoBounds
) {
430 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
431 painter
->setBrush(Qt::NoBrush
);
432 painter
->setPen(Qt::green
);
433 painter
->drawRect(m_iconRect
);
435 painter
->setPen(Qt::blue
);
436 painter
->drawRect(m_textRect
);
438 painter
->setPen(Qt::red
);
439 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
440 painter
->drawRect(rect());
444 QRectF
KStandardItemListWidget::iconRect() const
446 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
450 QRectF
KStandardItemListWidget::textRect() const
452 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
456 QRectF
KStandardItemListWidget::textFocusRect() const
458 // In the compact- and details-layout a larger textRect() is returned to be aligned
459 // with the iconRect(). This is useful to have a larger selection/hover-area
460 // when having a quite large icon size but only one line of text. Still the
461 // focus rectangle should be shown as narrow as possible around the text.
463 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
466 case CompactLayout
: {
467 QRectF rect
= m_textRect
;
468 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
469 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
470 rect
.setTop(topText
->pos
.y());
471 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
475 case DetailsLayout
: {
476 QRectF rect
= m_textRect
;
477 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
478 rect
.setTop(textInfo
->pos
.y());
479 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
481 const KItemListStyleOption
& option
= styleOption();
482 if (option
.extendedSelectionRegion
) {
483 const QString text
= textInfo
->staticText
.text();
484 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
497 QRectF
KStandardItemListWidget::expansionToggleRect() const
499 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
500 return m_isExpandable
? m_expansionArea
: QRectF();
503 QRectF
KStandardItemListWidget::selectionToggleRect() const
505 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
507 const int iconHeight
= styleOption().iconSize
;
509 int toggleSize
= KIconLoader::SizeSmall
;
510 if (iconHeight
>= KIconLoader::SizeEnormous
) {
511 toggleSize
= KIconLoader::SizeMedium
;
512 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
513 toggleSize
= KIconLoader::SizeSmallMedium
;
516 QPointF pos
= iconRect().topLeft();
518 // If the selection toggle has a very small distance to the
519 // widget borders, the size of the selection toggle will get
520 // increased to prevent an accidental clicking of the item
521 // when trying to hit the toggle.
522 const int widgetHeight
= size().height();
523 const int widgetWidth
= size().width();
524 const int minMargin
= 2;
526 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
527 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
528 toggleSize
= widgetHeight
;
531 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
532 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
533 toggleSize
= widgetWidth
;
537 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
540 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
543 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
544 if (m_layout
!= DetailsLayout
) {
548 // Only return the content of the text-column as pixmap
549 const int leftClip
= m_pixmapPos
.x();
551 const TextInfo
* textInfo
= m_textInfo
.value("text");
552 const int rightClip
= textInfo
->pos
.x() +
553 textInfo
->staticText
.size().width() +
554 2 * styleOption().padding
;
556 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
557 clippedPixmap
.fill(Qt::transparent
);
559 QPainter
painter(&clippedPixmap
);
560 painter
.drawPixmap(-leftClip
, 0, pixmap
);
562 return clippedPixmap
;
566 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
568 return new KStandardItemListWidgetInformant();
571 void KStandardItemListWidget::invalidateCache()
573 m_dirtyLayout
= true;
574 m_dirtyContent
= true;
577 void KStandardItemListWidget::refreshCache()
581 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
587 bool KStandardItemListWidget::isHidden() const
592 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
597 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
599 return QPalette::Text
;
602 void KStandardItemListWidget::setTextColor(const QColor
& color
)
604 if (color
!= m_customTextColor
) {
605 m_customTextColor
= color
;
606 updateAdditionalInfoTextColor();
611 QColor
KStandardItemListWidget::textColor() const
615 return m_additionalInfoTextColor
;
616 } else if (m_customTextColor
.isValid()) {
617 return m_customTextColor
;
621 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
622 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
623 return styleOption().palette
.color(group
, role
);
626 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
629 m_dirtyContent
= true;
633 QPixmap
KStandardItemListWidget::overlay() const
639 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
640 const QHash
<QByteArray
, QVariant
>& values
) const
642 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
645 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
646 const QSet
<QByteArray
>& roles
)
650 m_dirtyContent
= true;
652 QSet
<QByteArray
> dirtyRoles
;
653 if (roles
.isEmpty()) {
654 dirtyRoles
= visibleRoles().toSet();
659 // The URL might have changed (i.e., if the sort order of the items has
660 // been changed). Therefore, the "is cut" state must be updated.
661 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
662 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
663 m_isCut
= clipboard
->isCut(itemUrl
);
665 // The icon-state might depend from other roles and hence is
666 // marked as dirty whenever a role has been changed
667 dirtyRoles
.insert("iconPixmap");
668 dirtyRoles
.insert("iconName");
670 QSetIterator
<QByteArray
> it(dirtyRoles
);
671 while (it
.hasNext()) {
672 const QByteArray
& role
= it
.next();
673 m_dirtyContentRoles
.insert(role
);
677 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
678 const QList
<QByteArray
>& previous
)
681 m_sortedVisibleRoles
= current
;
682 m_dirtyLayout
= true;
685 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
692 m_dirtyLayout
= true;
695 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
696 const KItemListStyleOption
& previous
)
700 updateAdditionalInfoTextColor();
701 m_dirtyLayout
= true;
704 void KStandardItemListWidget::hoveredChanged(bool hovered
)
707 m_dirtyLayout
= true;
710 void KStandardItemListWidget::selectedChanged(bool selected
)
713 updateAdditionalInfoTextColor();
714 m_dirtyContent
= true;
717 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
721 m_dirtyLayout
= true;
724 int KStandardItemListWidget::selectionLength(const QString
& text
) const
726 return text
.length();
729 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
733 QGraphicsView
* parent
= scene()->views()[0];
734 if (current
.isEmpty() || !parent
|| current
!= "text") {
736 emit
roleEditingCanceled(index(), current
, data().value(current
));
738 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
739 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
740 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
741 this, &KStandardItemListWidget::slotRoleEditingFinished
);
743 if (m_oldRoleEditor
) {
744 m_oldRoleEditor
->deleteLater();
746 m_oldRoleEditor
= m_roleEditor
;
747 m_roleEditor
->hide();
753 Q_ASSERT(!m_roleEditor
);
755 const TextInfo
* textInfo
= m_textInfo
.value("text");
757 m_roleEditor
= new KItemListRoleEditor(parent
);
758 m_roleEditor
->setRole(current
);
759 m_roleEditor
->setFont(styleOption().font
);
761 const QString text
= data().value(current
).toString();
762 m_roleEditor
->setPlainText(text
);
764 QTextOption textOption
= textInfo
->staticText
.textOption();
765 m_roleEditor
->document()->setDefaultTextOption(textOption
);
767 const int textSelectionLength
= selectionLength(text
);
769 if (textSelectionLength
> 0) {
770 QTextCursor cursor
= m_roleEditor
->textCursor();
771 cursor
.movePosition(QTextCursor::StartOfBlock
);
772 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
773 m_roleEditor
->setTextCursor(cursor
);
776 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
777 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
778 connect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
779 this, &KStandardItemListWidget::slotRoleEditingFinished
);
781 // Adjust the geometry of the editor
782 QRectF rect
= roleEditingRect(current
);
783 const int frameWidth
= m_roleEditor
->frameWidth();
784 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
785 rect
.translate(pos());
786 if (rect
.right() > parent
->width()) {
787 rect
.setWidth(parent
->width() - rect
.left());
789 m_roleEditor
->setGeometry(rect
.toRect());
790 m_roleEditor
->show();
791 m_roleEditor
->setFocus();
794 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
797 setEditedRole(QByteArray());
798 Q_ASSERT(!m_roleEditor
);
801 KItemListWidget::resizeEvent(event
);
803 m_dirtyLayout
= true;
806 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
808 KItemListWidget::showEvent(event
);
810 // Listen to changes of the clipboard to mark the item as cut/uncut
811 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
813 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
814 m_isCut
= clipboard
->isCut(itemUrl
);
816 connect(clipboard
, &KFileItemClipboard::cutItemsChanged
,
817 this, &KStandardItemListWidget::slotCutItemsChanged
);
820 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
822 disconnect(KFileItemClipboard::instance(), &KFileItemClipboard::cutItemsChanged
,
823 this, &KStandardItemListWidget::slotCutItemsChanged
);
825 KItemListWidget::hideEvent(event
);
828 void KStandardItemListWidget::slotCutItemsChanged()
830 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
831 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
832 if (m_isCut
!= isCut
) {
834 m_pixmap
= QPixmap();
835 m_dirtyContent
= true;
840 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
841 const QVariant
& value
)
844 emit
roleEditingCanceled(index(), role
, value
);
845 setEditedRole(QByteArray());
848 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
849 const QVariant
& value
)
852 emit
roleEditingFinished(index(), role
, value
);
853 setEditedRole(QByteArray());
856 void KStandardItemListWidget::triggerCacheRefreshing()
858 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
864 const QHash
<QByteArray
, QVariant
> values
= data();
865 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
866 m_isHidden
= isHidden();
867 m_customizedFont
= customizedFont(styleOption().font
);
868 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
870 updateExpansionArea();
874 m_dirtyLayout
= false;
875 m_dirtyContent
= false;
876 m_dirtyContentRoles
.clear();
879 void KStandardItemListWidget::updateExpansionArea()
881 if (m_supportsItemExpanding
) {
882 const QHash
<QByteArray
, QVariant
> values
= data();
883 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
884 if (expandedParentsCount
>= 0) {
885 const KItemListStyleOption
& option
= styleOption();
886 const qreal widgetHeight
= size().height();
887 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
888 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
890 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
895 m_expansionArea
= QRectF();
898 void KStandardItemListWidget::updatePixmapCache()
900 // Precondition: Requires already updated m_textPos values to calculate
901 // the remaining height when the alignment is vertical.
903 const QSizeF widgetSize
= size();
904 const bool iconOnTop
= (m_layout
== IconsLayout
);
905 const KItemListStyleOption
& option
= styleOption();
906 const qreal padding
= option
.padding
;
908 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
909 const int maxIconHeight
= option
.iconSize
;
911 const QHash
<QByteArray
, QVariant
> values
= data();
913 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
914 if (!updatePixmap
&& m_dirtyContent
) {
915 updatePixmap
= m_dirtyContentRoles
.isEmpty()
916 || m_dirtyContentRoles
.contains("iconPixmap")
917 || m_dirtyContentRoles
.contains("iconName")
918 || m_dirtyContentRoles
.contains("iconOverlays");
922 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
923 if (m_pixmap
.isNull()) {
924 // Use the icon that fits to the MIME-type
925 QString iconName
= values
["iconName"].toString();
926 if (iconName
.isEmpty()) {
927 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
928 // use a generic icon as fallback
929 iconName
= QLatin1String("unknown");
931 const QStringList overlays
= values
["iconOverlays"].toStringList();
932 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
);
933 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
934 // A custom pixmap has been applied. Assure that the pixmap
935 // is scaled to the maximum available size.
936 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
940 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
941 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
945 KIconEffect::semiTransparent(m_pixmap
);
949 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
950 QImage image
= m_pixmap
.toImage();
951 KIconEffect::colorize(image
, color
, 0.8f
);
952 m_pixmap
= QPixmap::fromImage(image
);
956 if (!m_overlay
.isNull()) {
957 QPainter
painter(&m_pixmap
);
958 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
961 int scaledIconSize
= 0;
963 const TextInfo
* textInfo
= m_textInfo
.value("text");
964 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
966 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
967 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
968 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
969 widgetSize
.height() - 2 * padding
: maxIconHeight
;
972 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
973 const int maxScaledIconHeight
= scaledIconSize
;
975 m_scaledPixmapSize
= m_pixmap
.size();
976 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
979 // Center horizontally and align on bottom within the icon-area
980 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
981 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
983 // Center horizontally and vertically within the icon-area
984 const TextInfo
* textInfo
= m_textInfo
.value("text");
985 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
986 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
987 m_pixmapPos
.setY(padding
988 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
991 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
993 // Prepare the pixmap that is used when the item gets hovered
995 m_hoverPixmap
= m_pixmap
;
996 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
997 // In the KIconLoader terminology, active = hover.
998 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
999 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
1001 m_hoverPixmap
= m_pixmap
;
1003 } else if (hoverOpacity() <= 0.0) {
1004 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1005 m_hoverPixmap
= QPixmap();
1009 void KStandardItemListWidget::updateTextsCache()
1011 QTextOption textOption
;
1014 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
1015 textOption
.setAlignment(Qt::AlignHCenter
);
1019 textOption
.setAlignment(Qt::AlignLeft
);
1020 textOption
.setWrapMode(QTextOption::NoWrap
);
1027 qDeleteAll(m_textInfo
);
1029 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
1030 TextInfo
* textInfo
= new TextInfo();
1031 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1032 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1033 textInfo
->staticText
.setTextOption(textOption
);
1034 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1038 case IconsLayout
: updateIconsLayoutTextCache(); break;
1039 case CompactLayout
: updateCompactLayoutTextCache(); break;
1040 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1041 default: Q_ASSERT(false); break;
1044 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1045 if (ratingTextInfo
) {
1046 // The text of the rating-role has been set to empty to get
1047 // replaced by a rating-image showing the rating as stars.
1048 const KItemListStyleOption
& option
= styleOption();
1049 QSizeF ratingSize
= preferredRatingSize(option
);
1051 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1052 ? columnWidth("rating") - columnPadding(option
)
1054 if (ratingSize
.width() > availableWidth
) {
1055 ratingSize
.rwidth() = availableWidth
;
1057 m_rating
= QPixmap(ratingSize
.toSize());
1058 m_rating
.fill(Qt::transparent
);
1060 QPainter
painter(&m_rating
);
1061 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
1062 const int rating
= data().value("rating").toInt();
1063 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1064 } else if (!m_rating
.isNull()) {
1065 m_rating
= QPixmap();
1069 void KStandardItemListWidget::updateIconsLayoutTextCache()
1076 // might get wrapped above
1078 // Additional role 1
1079 // Additional role 2
1081 const QHash
<QByteArray
, QVariant
> values
= data();
1083 const KItemListStyleOption
& option
= styleOption();
1084 const qreal padding
= option
.padding
;
1085 const qreal maxWidth
= size().width() - 2 * padding
;
1086 const qreal widgetHeight
= size().height();
1087 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1089 // Initialize properties for the "text" role. It will be used as anchor
1090 // for initializing the position of the other roles.
1091 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1092 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1093 nameTextInfo
->staticText
.setText(nameText
);
1095 // Calculate the number of lines required for the name and the required width
1096 qreal nameWidth
= 0;
1097 qreal nameHeight
= 0;
1100 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1101 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1102 layout
.beginLayout();
1103 int nameLineIndex
= 0;
1104 while ((line
= layout
.createLine()).isValid()) {
1105 line
.setLineWidth(maxWidth
);
1106 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1107 nameHeight
+= line
.height();
1110 if (nameLineIndex
== option
.maxTextLines
) {
1111 // The maximum number of textlines has been reached. If this is
1112 // the case provide an elided text if necessary.
1113 const int textLength
= line
.textStart() + line
.textLength();
1114 if (textLength
< nameText
.length()) {
1115 // Elide the last line of the text
1116 QString lastTextLine
= nameText
.mid(line
.textStart());
1117 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
1120 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1121 nameTextInfo
->staticText
.setText(elidedText
);
1123 const qreal lastLineWidth
= m_customizedFontMetrics
.boundingRect(lastTextLine
).width();
1124 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1131 // Use one line for each additional information
1132 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1133 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1134 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1136 additionalRolesCount
* lineSpacing
-
1138 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1139 nameTextInfo
->pos
.y(),
1143 // Calculate the position for each additional information
1144 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1145 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1146 if (role
== "text") {
1150 const QString text
= roleText(role
, values
);
1151 TextInfo
* textInfo
= m_textInfo
.value(role
);
1152 textInfo
->staticText
.setText(text
);
1154 qreal requiredWidth
= 0;
1156 QTextLayout
layout(text
, m_customizedFont
);
1157 QTextOption textOption
;
1158 textOption
.setWrapMode(QTextOption::NoWrap
);
1159 layout
.setTextOption(textOption
);
1161 layout
.beginLayout();
1162 QTextLine textLine
= layout
.createLine();
1163 if (textLine
.isValid()) {
1164 textLine
.setLineWidth(maxWidth
);
1165 requiredWidth
= textLine
.naturalTextWidth();
1166 if (requiredWidth
> maxWidth
) {
1167 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1168 textInfo
->staticText
.setText(elidedText
);
1169 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1170 } else if (role
== "rating") {
1171 // Use the width of the rating pixmap, because the rating text is empty.
1172 requiredWidth
= m_rating
.width();
1177 textInfo
->pos
= QPointF(padding
, y
);
1178 textInfo
->staticText
.setTextWidth(maxWidth
);
1180 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1181 m_textRect
|= textRect
;
1186 // Add a padding to the text rectangle
1187 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1190 void KStandardItemListWidget::updateCompactLayoutTextCache()
1192 // +------+ Name role
1193 // | Icon | Additional role 1
1194 // +------+ Additional role 2
1196 const QHash
<QByteArray
, QVariant
> values
= data();
1198 const KItemListStyleOption
& option
= styleOption();
1199 const qreal widgetHeight
= size().height();
1200 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1201 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1202 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1204 qreal maximumRequiredTextWidth
= 0;
1205 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1206 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1207 const qreal maxWidth
= size().width() - x
- option
.padding
;
1208 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1209 const QString text
= roleText(role
, values
);
1210 TextInfo
* textInfo
= m_textInfo
.value(role
);
1211 textInfo
->staticText
.setText(text
);
1213 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1214 if (requiredWidth
> maxWidth
) {
1215 requiredWidth
= maxWidth
;
1216 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1217 textInfo
->staticText
.setText(elidedText
);
1220 textInfo
->pos
= QPointF(x
, y
);
1221 textInfo
->staticText
.setTextWidth(maxWidth
);
1223 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1228 m_textRect
= QRectF(x
- 2 * option
.padding
, 0, maximumRequiredTextWidth
+ 3 * option
.padding
, widgetHeight
);
1231 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1233 // Precondition: Requires already updated m_expansionArea
1234 // to determine the left position.
1237 // | Icon | Name role Additional role 1 Additional role 2
1239 m_textRect
= QRectF();
1241 const KItemListStyleOption
& option
= styleOption();
1242 const QHash
<QByteArray
, QVariant
> values
= data();
1244 const qreal widgetHeight
= size().height();
1245 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1246 const int fontHeight
= m_customizedFontMetrics
.height();
1248 const qreal columnWidthInc
= columnPadding(option
);
1249 qreal firstColumnInc
= scaledIconSize
;
1250 if (m_supportsItemExpanding
) {
1251 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1253 firstColumnInc
+= option
.padding
;
1256 qreal x
= firstColumnInc
;
1257 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1259 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1260 QString text
= roleText(role
, values
);
1262 // Elide the text in case it does not fit into the available column-width
1263 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1264 const qreal roleWidth
= columnWidth(role
);
1265 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1267 const bool isTextRole
= (role
== "text");
1269 availableTextWidth
-= firstColumnInc
;
1272 if (requiredWidth
> availableTextWidth
) {
1273 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1274 requiredWidth
= m_customizedFontMetrics
.width(text
);
1277 TextInfo
* textInfo
= m_textInfo
.value(role
);
1278 textInfo
->staticText
.setText(text
);
1279 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1283 const qreal textWidth
= option
.extendedSelectionRegion
1284 ? size().width() - textInfo
->pos
.x()
1285 : requiredWidth
+ 2 * option
.padding
;
1286 m_textRect
= QRectF(textInfo
->pos
.x() - 2 * option
.padding
, 0,
1287 textWidth
+ option
.padding
, size().height());
1289 // The column after the name should always be aligned on the same x-position independent
1290 // from the expansion-level shown in the name column
1291 x
-= firstColumnInc
;
1292 } else if (isRoleRightAligned(role
)) {
1293 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1298 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1301 if (m_customTextColor
.isValid()) {
1302 c1
= m_customTextColor
;
1303 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1304 c1
= styleOption().palette
.highlightedText().color();
1306 c1
= styleOption().palette
.text().color();
1309 // For the color of the additional info the inactive text color
1310 // is not used as this might lead to unreadable text for some color schemes. Instead
1311 // the text color c1 is slightly mixed with the background color.
1312 const QColor c2
= styleOption().palette
.base().color();
1314 const int p2
= 100 - p1
;
1315 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1316 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1317 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1320 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1322 if (m_scaledPixmapSize
!= pixmap
.size()) {
1323 QPixmap scaledPixmap
= pixmap
;
1324 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
1325 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1327 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1328 painter
->setPen(Qt::blue
);
1329 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1332 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1336 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1338 const int siblingSize
= size().height();
1339 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1340 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1342 QStyleOption option
;
1343 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1344 bool isItemSibling
= true;
1346 const QBitArray siblings
= siblingsInformation();
1347 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1348 option
.rect
= siblingRect
;
1349 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1351 if (isItemSibling
) {
1352 option
.state
|= QStyle::State_Item
;
1353 if (m_isExpandable
) {
1354 option
.state
|= QStyle::State_Children
;
1356 if (data()["isExpanded"].toBool()) {
1357 option
.state
|= QStyle::State_Open
;
1359 isItemSibling
= false;
1362 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1364 siblingRect
.translate(-siblingRect
.width(), 0);
1368 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1370 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1375 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1376 if (m_layout
== DetailsLayout
) {
1377 rect
.setWidth(columnWidth(role
) - rect
.x());
1383 void KStandardItemListWidget::closeRoleEditor()
1385 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingCanceled
,
1386 this, &KStandardItemListWidget::slotRoleEditingCanceled
);
1387 disconnect(m_roleEditor
, &KItemListRoleEditor::roleEditingFinished
,
1388 this, &KStandardItemListWidget::slotRoleEditingFinished
);
1390 if (m_roleEditor
->hasFocus()) {
1391 // If the editing was not ended by a FocusOut event, we have
1392 // to transfer the keyboard focus back to the KItemListContainer.
1393 scene()->views()[0]->parentWidget()->setFocus();
1396 if (m_oldRoleEditor
) {
1397 m_oldRoleEditor
->deleteLater();
1399 m_oldRoleEditor
= m_roleEditor
;
1400 m_roleEditor
->hide();
1404 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
)
1406 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(":") % ":" % QString::number(size
);
1409 if (!QPixmapCache::find(key
, pixmap
)) {
1410 const KIcon
icon(name
);
1413 if (size
<= KIconLoader::SizeSmall
) {
1414 requestedSize
= KIconLoader::SizeSmall
;
1415 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1416 requestedSize
= KIconLoader::SizeSmallMedium
;
1417 } else if (size
<= KIconLoader::SizeMedium
) {
1418 requestedSize
= KIconLoader::SizeMedium
;
1419 } else if (size
<= KIconLoader::SizeLarge
) {
1420 requestedSize
= KIconLoader::SizeLarge
;
1421 } else if (size
<= KIconLoader::SizeHuge
) {
1422 requestedSize
= KIconLoader::SizeHuge
;
1423 } else if (size
<= KIconLoader::SizeEnormous
) {
1424 requestedSize
= KIconLoader::SizeEnormous
;
1425 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1426 requestedSize
= KIconLoader::SizeEnormous
* 2;
1428 requestedSize
= size
;
1431 pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
1432 if (requestedSize
!= size
) {
1433 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1436 // Strangely KFileItem::overlays() returns empty string-values, so
1437 // we need to check first whether an overlay must be drawn at all.
1438 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1439 // assumes that an overlay will be drawn and has some additional
1441 foreach (const QString
& overlay
, overlays
) {
1442 if (!overlay
.isEmpty()) {
1443 // There is at least one overlay, draw all overlays above m_pixmap
1444 // and cancel the check
1445 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
);
1450 QPixmapCache::insert(key
, pixmap
);
1456 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1458 const qreal height
= option
.fontMetrics
.ascent();
1459 return QSizeF(height
* 5, height
);
1462 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1464 return option
.padding
* 6;
1467 #include "kstandarditemlistwidget.moc"