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
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
60 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
61 case KStandardItemListWidget::IconsLayout
:
62 calculateIconsLayoutItemSizeHints(sizeHints
, view
);
65 case KStandardItemListWidget::CompactLayout
:
66 calculateCompactLayoutItemSizeHints(sizeHints
, view
);
69 case KStandardItemListWidget::DetailsLayout
:
70 calculateDetailsLayoutItemSizeHints(sizeHints
, 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
<QSizeF
>& sizeHints
, 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
< sizeHints
.count(); ++index
) {
158 if (!sizeHints
.at(index
).isEmpty()) {
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 sizeHints
[index
] = QSizeF(itemWidth
, textHeight
+ spacingAndIconHeight
);
193 void KStandardItemListWidgetInformant::calculateCompactLayoutItemSizeHints(QVector
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
195 const KItemListStyleOption
& option
= view
->styleOption();
196 const QFontMetrics
& normalFontMetrics
= option
.fontMetrics
;
197 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
199 const QList
<QByteArray
>& visibleRoles
= view
->visibleRoles();
200 const bool showOnlyTextRole
= (visibleRoles
.count() == 1) && (visibleRoles
.first() == "text");
201 const qreal maxWidth
= option
.maxTextWidth
;
202 const qreal paddingAndIconWidth
= option
.padding
* 4 + option
.iconSize
;
203 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * normalFontMetrics
.lineSpacing());
205 const QFontMetrics
linkFontMetrics(customizedFontForLinks(option
.font
));
207 for (int index
= 0; index
< sizeHints
.count(); ++index
) {
208 if (!sizeHints
.at(index
).isEmpty()) {
212 // If the current item is a link, we use the customized link font metrics instead of the normal font metrics.
213 const QFontMetrics
& fontMetrics
= itemIsLink(index
, view
) ? linkFontMetrics
: normalFontMetrics
;
215 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
216 // to show all roles without horizontal clipping.
217 qreal maximumRequiredWidth
= 0.0;
219 if (showOnlyTextRole
) {
220 maximumRequiredWidth
= fontMetrics
.width(itemText(index
, view
));
222 const QHash
<QByteArray
, QVariant
>& values
= view
->model()->data(index
);
223 foreach (const QByteArray
& role
, visibleRoles
) {
224 const QString
& text
= roleText(role
, values
);
225 const qreal requiredWidth
= fontMetrics
.width(text
);
226 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
230 qreal width
= paddingAndIconWidth
+ maximumRequiredWidth
;
231 if (maxWidth
> 0 && width
> maxWidth
) {
235 sizeHints
[index
] = QSizeF(width
, height
);
239 void KStandardItemListWidgetInformant::calculateDetailsLayoutItemSizeHints(QVector
<QSizeF
>& sizeHints
, const KItemListView
* view
) const
241 const KItemListStyleOption
& option
= view
->styleOption();
242 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
244 for (int index
= 0; index
< sizeHints
.count(); ++index
) {
245 if (!sizeHints
.at(index
).isEmpty()) {
249 sizeHints
[index
] = QSizeF(-1, height
);
253 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
254 KItemListWidget(informant
, parent
),
258 m_customizedFontMetrics(m_customizedFont
),
259 m_isExpandable(false),
260 m_supportsItemExpanding(false),
262 m_dirtyContent(true),
263 m_dirtyContentRoles(),
264 m_layout(IconsLayout
),
267 m_scaledPixmapSize(),
272 m_sortedVisibleRoles(),
275 m_additionalInfoTextColor(),
283 KStandardItemListWidget::~KStandardItemListWidget()
285 qDeleteAll(m_textInfo
);
289 m_roleEditor
->deleteLater();
292 if (m_oldRoleEditor
) {
293 m_oldRoleEditor
->deleteLater();
297 void KStandardItemListWidget::setLayout(Layout layout
)
299 if (m_layout
!= layout
) {
301 m_dirtyLayout
= true;
302 updateAdditionalInfoTextColor();
307 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
312 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
314 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
315 m_supportsItemExpanding
= supportsItemExpanding
;
316 m_dirtyLayout
= true;
321 bool KStandardItemListWidget::supportsItemExpanding() const
323 return m_supportsItemExpanding
;
326 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
328 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
330 KItemListWidget::paint(painter
, option
, widget
);
332 if (!m_expansionArea
.isEmpty()) {
333 drawSiblingsInformation(painter
);
336 const KItemListStyleOption
& itemListStyleOption
= styleOption();
338 if (hoverOpacity() < 1.0) {
340 * Linear interpolation between m_pixmap and m_hoverPixmap.
342 * Note that this cannot be achieved by painting m_hoverPixmap over
343 * m_pixmap, even if the opacities are adjusted. For details see
344 * https://git.reviewboard.kde.org/r/109614/
346 // Paint pixmap1 so that pixmap1 = m_pixmap * (1.0 - hoverOpacity())
347 QPixmap
pixmap1(m_pixmap
.size());
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
.fill(Qt::transparent
);
359 QPainter
p(&pixmap2
);
360 p
.setOpacity(hoverOpacity());
361 p
.drawPixmap(0, 0, m_hoverPixmap
);
364 // Paint pixmap2 on pixmap1 using CompositionMode_Plus
365 // Now pixmap1 = pixmap2 + m_pixmap * (1.0 - hoverOpacity())
366 // = m_hoverPixmap * hoverOpacity() + m_pixmap * (1.0 - hoverOpacity())
368 QPainter
p(&pixmap1
);
369 p
.setCompositionMode(QPainter::CompositionMode_Plus
);
370 p
.drawPixmap(0, 0, pixmap2
);
373 // Finally paint pixmap1 on the widget
374 drawPixmap(painter
, pixmap1
);
376 drawPixmap(painter
, m_hoverPixmap
);
379 drawPixmap(painter
, m_pixmap
);
382 painter
->setFont(m_customizedFont
);
383 painter
->setPen(textColor());
384 const TextInfo
* textInfo
= m_textInfo
.value("text");
387 // It seems that we can end up here even if m_textInfo does not contain
388 // the key "text", see bug 306167. According to triggerCacheRefreshing(),
389 // this can only happen if the index is negative. This can happen when
390 // the item is about to be removed, see KItemListView::slotItemsRemoved().
391 // TODO: try to reproduce the crash and find a better fix.
395 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
397 bool clipAdditionalInfoBounds
= false;
398 if (m_supportsItemExpanding
) {
399 // Prevent a possible overlapping of the additional-information texts
400 // with the icon. This can happen if the user has minimized the width
401 // of the name-column to a very small value.
402 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
403 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
404 clipAdditionalInfoBounds
= true;
406 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
410 painter
->setPen(m_additionalInfoTextColor
);
411 painter
->setFont(m_customizedFont
);
413 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
414 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
415 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
418 if (!m_rating
.isNull()) {
419 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
420 QPointF pos
= ratingTextInfo
->pos
;
421 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
422 if (align
& Qt::AlignHCenter
) {
423 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
425 painter
->drawPixmap(pos
, m_rating
);
428 if (clipAdditionalInfoBounds
) {
432 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
433 painter
->setBrush(Qt::NoBrush
);
434 painter
->setPen(Qt::green
);
435 painter
->drawRect(m_iconRect
);
437 painter
->setPen(Qt::blue
);
438 painter
->drawRect(m_textRect
);
440 painter
->setPen(Qt::red
);
441 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
442 painter
->drawRect(rect());
446 QRectF
KStandardItemListWidget::iconRect() const
448 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
452 QRectF
KStandardItemListWidget::textRect() const
454 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
458 QRectF
KStandardItemListWidget::textFocusRect() const
460 // In the compact- and details-layout a larger textRect() is returned to be aligned
461 // with the iconRect(). This is useful to have a larger selection/hover-area
462 // when having a quite large icon size but only one line of text. Still the
463 // focus rectangle should be shown as narrow as possible around the text.
465 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
468 case CompactLayout
: {
469 QRectF rect
= m_textRect
;
470 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
471 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
472 rect
.setTop(topText
->pos
.y());
473 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
477 case DetailsLayout
: {
478 QRectF rect
= m_textRect
;
479 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
480 rect
.setTop(textInfo
->pos
.y());
481 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
483 const KItemListStyleOption
& option
= styleOption();
484 if (option
.extendedSelectionRegion
) {
485 const QString text
= textInfo
->staticText
.text();
486 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
499 QRectF
KStandardItemListWidget::expansionToggleRect() const
501 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
502 return m_isExpandable
? m_expansionArea
: QRectF();
505 QRectF
KStandardItemListWidget::selectionToggleRect() const
507 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
509 const int iconHeight
= styleOption().iconSize
;
511 int toggleSize
= KIconLoader::SizeSmall
;
512 if (iconHeight
>= KIconLoader::SizeEnormous
) {
513 toggleSize
= KIconLoader::SizeMedium
;
514 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
515 toggleSize
= KIconLoader::SizeSmallMedium
;
518 QPointF pos
= iconRect().topLeft();
520 // If the selection toggle has a very small distance to the
521 // widget borders, the size of the selection toggle will get
522 // increased to prevent an accidental clicking of the item
523 // when trying to hit the toggle.
524 const int widgetHeight
= size().height();
525 const int widgetWidth
= size().width();
526 const int minMargin
= 2;
528 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
529 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
530 toggleSize
= widgetHeight
;
533 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
534 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
535 toggleSize
= widgetWidth
;
539 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
542 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
545 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
546 if (m_layout
!= DetailsLayout
) {
550 // Only return the content of the text-column as pixmap
551 const int leftClip
= m_pixmapPos
.x();
553 const TextInfo
* textInfo
= m_textInfo
.value("text");
554 const int rightClip
= textInfo
->pos
.x() +
555 textInfo
->staticText
.size().width() +
556 2 * styleOption().padding
;
558 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
559 clippedPixmap
.fill(Qt::transparent
);
561 QPainter
painter(&clippedPixmap
);
562 painter
.drawPixmap(-leftClip
, 0, pixmap
);
564 return clippedPixmap
;
568 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
570 return new KStandardItemListWidgetInformant();
573 void KStandardItemListWidget::invalidateCache()
575 m_dirtyLayout
= true;
576 m_dirtyContent
= true;
579 void KStandardItemListWidget::refreshCache()
583 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
589 bool KStandardItemListWidget::isHidden() const
594 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
599 QPalette::ColorRole
KStandardItemListWidget::normalTextColorRole() const
601 return QPalette::Text
;
604 void KStandardItemListWidget::setTextColor(const QColor
& color
)
606 if (color
!= m_customTextColor
) {
607 m_customTextColor
= color
;
608 updateAdditionalInfoTextColor();
613 QColor
KStandardItemListWidget::textColor() const
617 return m_additionalInfoTextColor
;
618 } else if (m_customTextColor
.isValid()) {
619 return m_customTextColor
;
623 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
624 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: normalTextColorRole();
625 return styleOption().palette
.color(group
, role
);
628 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
631 m_dirtyContent
= true;
635 QPixmap
KStandardItemListWidget::overlay() const
641 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
642 const QHash
<QByteArray
, QVariant
>& values
) const
644 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
647 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
648 const QSet
<QByteArray
>& roles
)
652 m_dirtyContent
= true;
654 QSet
<QByteArray
> dirtyRoles
;
655 if (roles
.isEmpty()) {
656 dirtyRoles
= visibleRoles().toSet();
661 // The icon-state might depend from other roles and hence is
662 // marked as dirty whenever a role has been changed
663 dirtyRoles
.insert("iconPixmap");
664 dirtyRoles
.insert("iconName");
666 QSetIterator
<QByteArray
> it(dirtyRoles
);
667 while (it
.hasNext()) {
668 const QByteArray
& role
= it
.next();
669 m_dirtyContentRoles
.insert(role
);
673 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
674 const QList
<QByteArray
>& previous
)
677 m_sortedVisibleRoles
= current
;
678 m_dirtyLayout
= true;
681 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
688 m_dirtyLayout
= true;
691 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
692 const KItemListStyleOption
& previous
)
696 updateAdditionalInfoTextColor();
697 m_dirtyLayout
= true;
700 void KStandardItemListWidget::hoveredChanged(bool hovered
)
703 m_dirtyLayout
= true;
706 void KStandardItemListWidget::selectedChanged(bool selected
)
709 updateAdditionalInfoTextColor();
710 m_dirtyContent
= true;
713 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
717 m_dirtyLayout
= true;
720 int KStandardItemListWidget::selectionLength(const QString
& text
) const
722 return text
.length();
725 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
729 QGraphicsView
* parent
= scene()->views()[0];
730 if (current
.isEmpty() || !parent
|| current
!= "text") {
732 emit
roleEditingCanceled(index(), current
, data().value(current
));
734 disconnect(m_roleEditor
, SIGNAL(roleEditingCanceled(QByteArray
,QVariant
)),
735 this, SLOT(slotRoleEditingCanceled(QByteArray
,QVariant
)));
736 disconnect(m_roleEditor
, SIGNAL(roleEditingFinished(QByteArray
,QVariant
)),
737 this, SLOT(slotRoleEditingFinished(QByteArray
,QVariant
)));
739 if (m_oldRoleEditor
) {
740 m_oldRoleEditor
->deleteLater();
742 m_oldRoleEditor
= m_roleEditor
;
743 m_roleEditor
->hide();
749 Q_ASSERT(!m_roleEditor
);
751 const TextInfo
* textInfo
= m_textInfo
.value("text");
753 m_roleEditor
= new KItemListRoleEditor(parent
);
754 m_roleEditor
->setRole(current
);
755 m_roleEditor
->setFont(styleOption().font
);
757 const QString text
= data().value(current
).toString();
758 m_roleEditor
->setPlainText(text
);
760 QTextOption textOption
= textInfo
->staticText
.textOption();
761 m_roleEditor
->document()->setDefaultTextOption(textOption
);
763 const int textSelectionLength
= selectionLength(text
);
765 if (textSelectionLength
> 0) {
766 QTextCursor cursor
= m_roleEditor
->textCursor();
767 cursor
.movePosition(QTextCursor::StartOfBlock
);
768 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
769 m_roleEditor
->setTextCursor(cursor
);
772 connect(m_roleEditor
, SIGNAL(roleEditingCanceled(QByteArray
,QVariant
)),
773 this, SLOT(slotRoleEditingCanceled(QByteArray
,QVariant
)));
774 connect(m_roleEditor
, SIGNAL(roleEditingFinished(QByteArray
,QVariant
)),
775 this, SLOT(slotRoleEditingFinished(QByteArray
,QVariant
)));
777 // Adjust the geometry of the editor
778 QRectF rect
= roleEditingRect(current
);
779 const int frameWidth
= m_roleEditor
->frameWidth();
780 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
781 rect
.translate(pos());
782 if (rect
.right() > parent
->width()) {
783 rect
.setWidth(parent
->width() - rect
.left());
785 m_roleEditor
->setGeometry(rect
.toRect());
786 m_roleEditor
->show();
787 m_roleEditor
->setFocus();
790 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
793 setEditedRole(QByteArray());
794 Q_ASSERT(!m_roleEditor
);
797 KItemListWidget::resizeEvent(event
);
799 m_dirtyLayout
= true;
802 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
804 KItemListWidget::showEvent(event
);
806 // Listen to changes of the clipboard to mark the item as cut/uncut
807 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
809 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
810 m_isCut
= clipboard
->isCut(itemUrl
);
812 connect(clipboard
, SIGNAL(cutItemsChanged()),
813 this, SLOT(slotCutItemsChanged()));
816 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
818 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
819 this, SLOT(slotCutItemsChanged()));
821 KItemListWidget::hideEvent(event
);
824 void KStandardItemListWidget::slotCutItemsChanged()
826 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
827 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
828 if (m_isCut
!= isCut
) {
830 m_pixmap
= QPixmap();
831 m_dirtyContent
= true;
836 void KStandardItemListWidget::slotRoleEditingCanceled(const QByteArray
& role
,
837 const QVariant
& value
)
840 emit
roleEditingCanceled(index(), role
, value
);
841 setEditedRole(QByteArray());
844 void KStandardItemListWidget::slotRoleEditingFinished(const QByteArray
& role
,
845 const QVariant
& value
)
848 emit
roleEditingFinished(index(), role
, value
);
849 setEditedRole(QByteArray());
852 void KStandardItemListWidget::triggerCacheRefreshing()
854 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
860 const QHash
<QByteArray
, QVariant
> values
= data();
861 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
862 m_isHidden
= isHidden();
863 m_customizedFont
= customizedFont(styleOption().font
);
864 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
866 updateExpansionArea();
870 m_dirtyLayout
= false;
871 m_dirtyContent
= false;
872 m_dirtyContentRoles
.clear();
875 void KStandardItemListWidget::updateExpansionArea()
877 if (m_supportsItemExpanding
) {
878 const QHash
<QByteArray
, QVariant
> values
= data();
879 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
880 if (expandedParentsCount
>= 0) {
881 const KItemListStyleOption
& option
= styleOption();
882 const qreal widgetHeight
= size().height();
883 const qreal inc
= (widgetHeight
- option
.iconSize
) / 2;
884 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
886 m_expansionArea
= QRectF(x
, y
, option
.iconSize
, option
.iconSize
);
891 m_expansionArea
= QRectF();
894 void KStandardItemListWidget::updatePixmapCache()
896 // Precondition: Requires already updated m_textPos values to calculate
897 // the remaining height when the alignment is vertical.
899 const QSizeF widgetSize
= size();
900 const bool iconOnTop
= (m_layout
== IconsLayout
);
901 const KItemListStyleOption
& option
= styleOption();
902 const qreal padding
= option
.padding
;
904 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
905 const int maxIconHeight
= option
.iconSize
;
907 const QHash
<QByteArray
, QVariant
> values
= data();
909 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
910 if (!updatePixmap
&& m_dirtyContent
) {
911 updatePixmap
= m_dirtyContentRoles
.isEmpty()
912 || m_dirtyContentRoles
.contains("iconPixmap")
913 || m_dirtyContentRoles
.contains("iconName")
914 || m_dirtyContentRoles
.contains("iconOverlays");
918 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
919 if (m_pixmap
.isNull()) {
920 // Use the icon that fits to the MIME-type
921 QString iconName
= values
["iconName"].toString();
922 if (iconName
.isEmpty()) {
923 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
924 // use a generic icon as fallback
925 iconName
= QLatin1String("unknown");
927 const QStringList overlays
= values
["iconOverlays"].toStringList();
928 m_pixmap
= pixmapForIcon(iconName
, overlays
, maxIconHeight
);
929 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
930 // A custom pixmap has been applied. Assure that the pixmap
931 // is scaled to the maximum available size.
932 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
936 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
937 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
941 KIconEffect::semiTransparent(m_pixmap
);
945 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
946 QImage image
= m_pixmap
.toImage();
947 KIconEffect::colorize(image
, color
, 0.8f
);
948 m_pixmap
= QPixmap::fromImage(image
);
952 if (!m_overlay
.isNull()) {
953 QPainter
painter(&m_pixmap
);
954 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
957 int scaledIconSize
= 0;
959 const TextInfo
* textInfo
= m_textInfo
.value("text");
960 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
962 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
963 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
964 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
965 widgetSize
.height() - 2 * padding
: maxIconHeight
;
968 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
969 const int maxScaledIconHeight
= scaledIconSize
;
971 m_scaledPixmapSize
= m_pixmap
.size();
972 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
975 // Center horizontally and align on bottom within the icon-area
976 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
977 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
979 // Center horizontally and vertically within the icon-area
980 const TextInfo
* textInfo
= m_textInfo
.value("text");
981 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
982 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
983 m_pixmapPos
.setY(padding
984 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
987 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
989 // Prepare the pixmap that is used when the item gets hovered
991 m_hoverPixmap
= m_pixmap
;
992 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
993 // In the KIconLoader terminology, active = hover.
994 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
995 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
997 m_hoverPixmap
= m_pixmap
;
999 } else if (hoverOpacity() <= 0.0) {
1000 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
1001 m_hoverPixmap
= QPixmap();
1005 void KStandardItemListWidget::updateTextsCache()
1007 QTextOption textOption
;
1010 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
1011 textOption
.setAlignment(Qt::AlignHCenter
);
1015 textOption
.setAlignment(Qt::AlignLeft
);
1016 textOption
.setWrapMode(QTextOption::NoWrap
);
1023 qDeleteAll(m_textInfo
);
1025 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
1026 TextInfo
* textInfo
= new TextInfo();
1027 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
1028 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
1029 textInfo
->staticText
.setTextOption(textOption
);
1030 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
1034 case IconsLayout
: updateIconsLayoutTextCache(); break;
1035 case CompactLayout
: updateCompactLayoutTextCache(); break;
1036 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
1037 default: Q_ASSERT(false); break;
1040 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
1041 if (ratingTextInfo
) {
1042 // The text of the rating-role has been set to empty to get
1043 // replaced by a rating-image showing the rating as stars.
1044 const KItemListStyleOption
& option
= styleOption();
1045 QSizeF ratingSize
= preferredRatingSize(option
);
1047 const qreal availableWidth
= (m_layout
== DetailsLayout
)
1048 ? columnWidth("rating") - columnPadding(option
)
1050 if (ratingSize
.width() > availableWidth
) {
1051 ratingSize
.rwidth() = availableWidth
;
1053 m_rating
= QPixmap(ratingSize
.toSize());
1054 m_rating
.fill(Qt::transparent
);
1056 QPainter
painter(&m_rating
);
1057 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
1058 const int rating
= data().value("rating").toInt();
1059 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
1060 } else if (!m_rating
.isNull()) {
1061 m_rating
= QPixmap();
1065 void KStandardItemListWidget::updateIconsLayoutTextCache()
1072 // might get wrapped above
1074 // Additional role 1
1075 // Additional role 2
1077 const QHash
<QByteArray
, QVariant
> values
= data();
1079 const KItemListStyleOption
& option
= styleOption();
1080 const qreal padding
= option
.padding
;
1081 const qreal maxWidth
= size().width() - 2 * padding
;
1082 const qreal widgetHeight
= size().height();
1083 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1085 // Initialize properties for the "text" role. It will be used as anchor
1086 // for initializing the position of the other roles.
1087 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
1088 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
1089 nameTextInfo
->staticText
.setText(nameText
);
1091 // Calculate the number of lines required for the name and the required width
1092 qreal nameWidth
= 0;
1093 qreal nameHeight
= 0;
1096 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
1097 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
1098 layout
.beginLayout();
1099 int nameLineIndex
= 0;
1100 while ((line
= layout
.createLine()).isValid()) {
1101 line
.setLineWidth(maxWidth
);
1102 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
1103 nameHeight
+= line
.height();
1106 if (nameLineIndex
== option
.maxTextLines
) {
1107 // The maximum number of textlines has been reached. If this is
1108 // the case provide an elided text if necessary.
1109 const int textLength
= line
.textStart() + line
.textLength();
1110 if (textLength
< nameText
.length()) {
1111 // Elide the last line of the text
1112 QString lastTextLine
= nameText
.mid(line
.textStart());
1113 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
1116 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
1117 nameTextInfo
->staticText
.setText(elidedText
);
1119 const qreal lastLineWidth
= m_customizedFontMetrics
.boundingRect(lastTextLine
).width();
1120 nameWidth
= qMax(nameWidth
, lastLineWidth
);
1127 // Use one line for each additional information
1128 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
1129 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
1130 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
1132 additionalRolesCount
* lineSpacing
-
1134 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1135 nameTextInfo
->pos
.y(),
1139 // Calculate the position for each additional information
1140 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1141 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1142 if (role
== "text") {
1146 const QString text
= roleText(role
, values
);
1147 TextInfo
* textInfo
= m_textInfo
.value(role
);
1148 textInfo
->staticText
.setText(text
);
1150 qreal requiredWidth
= 0;
1152 QTextLayout
layout(text
, m_customizedFont
);
1153 QTextOption textOption
;
1154 textOption
.setWrapMode(QTextOption::NoWrap
);
1155 layout
.setTextOption(textOption
);
1157 layout
.beginLayout();
1158 QTextLine textLine
= layout
.createLine();
1159 if (textLine
.isValid()) {
1160 textLine
.setLineWidth(maxWidth
);
1161 requiredWidth
= textLine
.naturalTextWidth();
1162 if (requiredWidth
> maxWidth
) {
1163 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1164 textInfo
->staticText
.setText(elidedText
);
1165 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1166 } else if (role
== "rating") {
1167 // Use the width of the rating pixmap, because the rating text is empty.
1168 requiredWidth
= m_rating
.width();
1173 textInfo
->pos
= QPointF(padding
, y
);
1174 textInfo
->staticText
.setTextWidth(maxWidth
);
1176 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1177 m_textRect
|= textRect
;
1182 // Add a padding to the text rectangle
1183 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1186 void KStandardItemListWidget::updateCompactLayoutTextCache()
1188 // +------+ Name role
1189 // | Icon | Additional role 1
1190 // +------+ Additional role 2
1192 const QHash
<QByteArray
, QVariant
> values
= data();
1194 const KItemListStyleOption
& option
= styleOption();
1195 const qreal widgetHeight
= size().height();
1196 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1197 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1198 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1200 qreal maximumRequiredTextWidth
= 0;
1201 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1202 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1203 const qreal maxWidth
= size().width() - x
- option
.padding
;
1204 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1205 const QString text
= roleText(role
, values
);
1206 TextInfo
* textInfo
= m_textInfo
.value(role
);
1207 textInfo
->staticText
.setText(text
);
1209 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1210 if (requiredWidth
> maxWidth
) {
1211 requiredWidth
= maxWidth
;
1212 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1213 textInfo
->staticText
.setText(elidedText
);
1216 textInfo
->pos
= QPointF(x
, y
);
1217 textInfo
->staticText
.setTextWidth(maxWidth
);
1219 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1224 m_textRect
= QRectF(x
- 2 * option
.padding
, 0, maximumRequiredTextWidth
+ 3 * option
.padding
, widgetHeight
);
1227 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1229 // Precondition: Requires already updated m_expansionArea
1230 // to determine the left position.
1233 // | Icon | Name role Additional role 1 Additional role 2
1235 m_textRect
= QRectF();
1237 const KItemListStyleOption
& option
= styleOption();
1238 const QHash
<QByteArray
, QVariant
> values
= data();
1240 const qreal widgetHeight
= size().height();
1241 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1242 const int fontHeight
= m_customizedFontMetrics
.height();
1244 const qreal columnWidthInc
= columnPadding(option
);
1245 qreal firstColumnInc
= scaledIconSize
;
1246 if (m_supportsItemExpanding
) {
1247 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1249 firstColumnInc
+= option
.padding
;
1252 qreal x
= firstColumnInc
;
1253 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1255 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1256 QString text
= roleText(role
, values
);
1258 // Elide the text in case it does not fit into the available column-width
1259 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1260 const qreal roleWidth
= columnWidth(role
);
1261 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1263 const bool isTextRole
= (role
== "text");
1265 availableTextWidth
-= firstColumnInc
;
1268 if (requiredWidth
> availableTextWidth
) {
1269 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1270 requiredWidth
= m_customizedFontMetrics
.width(text
);
1273 TextInfo
* textInfo
= m_textInfo
.value(role
);
1274 textInfo
->staticText
.setText(text
);
1275 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1279 const qreal textWidth
= option
.extendedSelectionRegion
1280 ? size().width() - textInfo
->pos
.x()
1281 : requiredWidth
+ 2 * option
.padding
;
1282 m_textRect
= QRectF(textInfo
->pos
.x() - 2 * option
.padding
, 0,
1283 textWidth
+ option
.padding
, size().height());
1285 // The column after the name should always be aligned on the same x-position independent
1286 // from the expansion-level shown in the name column
1287 x
-= firstColumnInc
;
1288 } else if (isRoleRightAligned(role
)) {
1289 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1294 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1297 if (m_customTextColor
.isValid()) {
1298 c1
= m_customTextColor
;
1299 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1300 c1
= styleOption().palette
.highlightedText().color();
1302 c1
= styleOption().palette
.text().color();
1305 // For the color of the additional info the inactive text color
1306 // is not used as this might lead to unreadable text for some color schemes. Instead
1307 // the text color c1 is slightly mixed with the background color.
1308 const QColor c2
= styleOption().palette
.base().color();
1310 const int p2
= 100 - p1
;
1311 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1312 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1313 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1316 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1318 if (m_scaledPixmapSize
!= pixmap
.size()) {
1319 QPixmap scaledPixmap
= pixmap
;
1320 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
1321 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1323 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1324 painter
->setPen(Qt::blue
);
1325 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1328 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1332 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1334 const int siblingSize
= size().height();
1335 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1336 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1338 QStyleOption option
;
1339 option
.palette
.setColor(QPalette::Text
, option
.palette
.color(normalTextColorRole()));
1340 bool isItemSibling
= true;
1342 const QBitArray siblings
= siblingsInformation();
1343 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1344 option
.rect
= siblingRect
;
1345 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1347 if (isItemSibling
) {
1348 option
.state
|= QStyle::State_Item
;
1349 if (m_isExpandable
) {
1350 option
.state
|= QStyle::State_Children
;
1352 if (data()["isExpanded"].toBool()) {
1353 option
.state
|= QStyle::State_Open
;
1355 isItemSibling
= false;
1358 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1360 siblingRect
.translate(-siblingRect
.width(), 0);
1364 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1366 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1371 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1372 if (m_layout
== DetailsLayout
) {
1373 rect
.setWidth(columnWidth(role
) - rect
.x());
1379 void KStandardItemListWidget::closeRoleEditor()
1381 disconnect(m_roleEditor
, SIGNAL(roleEditingCanceled(QByteArray
,QVariant
)),
1382 this, SLOT(slotRoleEditingCanceled(QByteArray
,QVariant
)));
1383 disconnect(m_roleEditor
, SIGNAL(roleEditingFinished(QByteArray
,QVariant
)),
1384 this, SLOT(slotRoleEditingFinished(QByteArray
,QVariant
)));
1386 if (m_roleEditor
->hasFocus()) {
1387 // If the editing was not ended by a FocusOut event, we have
1388 // to transfer the keyboard focus back to the KItemListContainer.
1389 scene()->views()[0]->parentWidget()->setFocus();
1392 if (m_oldRoleEditor
) {
1393 m_oldRoleEditor
->deleteLater();
1395 m_oldRoleEditor
= m_roleEditor
;
1396 m_roleEditor
->hide();
1400 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, const QStringList
& overlays
, int size
)
1402 const QString key
= "KStandardItemListWidget:" % name
% ":" % overlays
.join(":") % ":" % QString::number(size
);
1405 if (!QPixmapCache::find(key
, pixmap
)) {
1406 const KIcon
icon(name
);
1409 if (size
<= KIconLoader::SizeSmall
) {
1410 requestedSize
= KIconLoader::SizeSmall
;
1411 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1412 requestedSize
= KIconLoader::SizeSmallMedium
;
1413 } else if (size
<= KIconLoader::SizeMedium
) {
1414 requestedSize
= KIconLoader::SizeMedium
;
1415 } else if (size
<= KIconLoader::SizeLarge
) {
1416 requestedSize
= KIconLoader::SizeLarge
;
1417 } else if (size
<= KIconLoader::SizeHuge
) {
1418 requestedSize
= KIconLoader::SizeHuge
;
1419 } else if (size
<= KIconLoader::SizeEnormous
) {
1420 requestedSize
= KIconLoader::SizeEnormous
;
1421 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1422 requestedSize
= KIconLoader::SizeEnormous
* 2;
1424 requestedSize
= size
;
1427 pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
1428 if (requestedSize
!= size
) {
1429 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1432 // Strangely KFileItem::overlays() returns empty string-values, so
1433 // we need to check first whether an overlay must be drawn at all.
1434 // It is more efficient to do it here, as KIconLoader::drawOverlays()
1435 // assumes that an overlay will be drawn and has some additional
1437 foreach (const QString
& overlay
, overlays
) {
1438 if (!overlay
.isEmpty()) {
1439 // There is at least one overlay, draw all overlays above m_pixmap
1440 // and cancel the check
1441 KIconLoader::global()->drawOverlays(overlays
, pixmap
, KIconLoader::Desktop
);
1446 QPixmapCache::insert(key
, pixmap
);
1452 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1454 const qreal height
= option
.fontMetrics
.ascent();
1455 return QSizeF(height
* 5, height
);
1458 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1460 return option
.padding
* 6;
1463 #include "kstandarditemlistwidget.moc"