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>
46 // #define KSTANDARDITEMLISTWIDGET_DEBUG
48 KStandardItemListWidgetInformant::KStandardItemListWidgetInformant() :
49 KItemListWidgetInformant()
53 KStandardItemListWidgetInformant::~KStandardItemListWidgetInformant()
57 QSizeF
KStandardItemListWidgetInformant::itemSizeHint(int index
, const KItemListView
* view
) const
59 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
60 const KItemListStyleOption
& option
= view
->styleOption();
61 const int additionalRolesCount
= qMax(view
->visibleRoles().count() - 1, 0);
63 switch (static_cast<const KStandardItemListView
*>(view
)->itemLayout()) {
64 case KStandardItemListWidget::IconsLayout
: {
65 const QString text
= KStringHandler::preProcessWrap(values
["text"].toString());
67 const qreal itemWidth
= view
->itemSize().width();
68 const qreal maxWidth
= itemWidth
- 2 * option
.padding
;
71 // Calculate the number of lines required for wrapping the name
72 QTextOption
textOption(Qt::AlignHCenter
);
73 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
76 QTextLayout
layout(text
, option
.font
);
77 layout
.setTextOption(textOption
);
79 while ((line
= layout
.createLine()).isValid()) {
80 line
.setLineWidth(maxWidth
);
81 line
.naturalTextWidth();
82 textHeight
+= line
.height();
86 // Add one line for each additional information
87 textHeight
+= additionalRolesCount
* option
.fontMetrics
.lineSpacing();
89 const qreal maxTextHeight
= option
.maxTextSize
.height();
90 if (maxTextHeight
> 0 && textHeight
> maxTextHeight
) {
91 textHeight
= maxTextHeight
;
94 return QSizeF(itemWidth
, textHeight
+ option
.iconSize
+ option
.padding
* 3);
97 case KStandardItemListWidget::CompactLayout
: {
98 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
99 // to show all roles without horizontal clipping.
100 qreal maximumRequiredWidth
= 0.0;
102 foreach (const QByteArray
& role
, view
->visibleRoles()) {
103 const QString text
= roleText(role
, values
);
104 const qreal requiredWidth
= option
.fontMetrics
.width(text
);
105 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
108 qreal width
= option
.padding
* 4 + option
.iconSize
+ maximumRequiredWidth
;
109 const qreal maxWidth
= option
.maxTextSize
.width();
110 if (maxWidth
> 0 && width
> maxWidth
) {
113 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * option
.fontMetrics
.lineSpacing());
114 return QSizeF(width
, height
);
117 case KStandardItemListWidget::DetailsLayout
: {
118 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
119 return QSizeF(-1, height
);
130 qreal
KStandardItemListWidgetInformant::preferredRoleColumnWidth(const QByteArray
& role
,
132 const KItemListView
* view
) const
134 const QHash
<QByteArray
, QVariant
> values
= view
->model()->data(index
);
135 const KItemListStyleOption
& option
= view
->styleOption();
137 const QString text
= roleText(role
, values
);
138 qreal width
= KStandardItemListWidget::columnPadding(option
);
140 if (role
== "rating") {
141 width
+= KStandardItemListWidget::preferredRatingSize(option
).width();
143 width
+= option
.fontMetrics
.width(text
);
145 if (role
== "text") {
146 if (view
->supportsItemExpanding()) {
147 // Increase the width by the expansion-toggle and the current expansion level
148 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
149 const qreal height
= option
.padding
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
150 width
+= (expandedParentsCount
+ 1) * height
;
153 // Increase the width by the required space for the icon
154 width
+= option
.padding
* 2 + option
.iconSize
;
161 QString
KStandardItemListWidgetInformant::roleText(const QByteArray
& role
,
162 const QHash
<QByteArray
, QVariant
>& values
) const
164 if (role
== "rating") {
165 // Always use an empty text, as the rating is shown by the image m_rating.
168 return values
.value(role
).toString();
171 KStandardItemListWidget::KStandardItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
172 KItemListWidget(informant
, parent
),
176 m_customizedFontMetrics(m_customizedFont
),
177 m_isExpandable(false),
178 m_supportsItemExpanding(false),
180 m_dirtyContent(true),
181 m_dirtyContentRoles(),
182 m_layout(IconsLayout
),
185 m_scaledPixmapSize(),
190 m_sortedVisibleRoles(),
193 m_additionalInfoTextColor(),
200 KStandardItemListWidget::~KStandardItemListWidget()
202 qDeleteAll(m_textInfo
);
208 void KStandardItemListWidget::setLayout(Layout layout
)
210 if (m_layout
!= layout
) {
212 m_dirtyLayout
= true;
213 updateAdditionalInfoTextColor();
218 KStandardItemListWidget::Layout
KStandardItemListWidget::layout() const
223 void KStandardItemListWidget::setSupportsItemExpanding(bool supportsItemExpanding
)
225 if (m_supportsItemExpanding
!= supportsItemExpanding
) {
226 m_supportsItemExpanding
= supportsItemExpanding
;
227 m_dirtyLayout
= true;
232 bool KStandardItemListWidget::supportsItemExpanding() const
234 return m_supportsItemExpanding
;
237 void KStandardItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
239 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
241 KItemListWidget::paint(painter
, option
, widget
);
243 if (!m_expansionArea
.isEmpty()) {
244 drawSiblingsInformation(painter
);
247 const KItemListStyleOption
& itemListStyleOption
= styleOption();
249 // Blend the unhovered and hovered pixmap if the hovering
250 // animation is ongoing
251 if (hoverOpacity() < 1.0) {
252 drawPixmap(painter
, m_pixmap
);
255 const qreal opacity
= painter
->opacity();
256 painter
->setOpacity(hoverOpacity() * opacity
);
257 drawPixmap(painter
, m_hoverPixmap
);
258 painter
->setOpacity(opacity
);
260 drawPixmap(painter
, m_pixmap
);
263 painter
->setFont(m_customizedFont
);
264 painter
->setPen(m_isHidden
? m_additionalInfoTextColor
: textColor());
265 const TextInfo
* textInfo
= m_textInfo
.value("text");
266 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
268 bool clipAdditionalInfoBounds
= false;
269 if (m_supportsItemExpanding
) {
270 // Prevent a possible overlapping of the additional-information texts
271 // with the icon. This can happen if the user has minimized the width
272 // of the name-column to a very small value.
273 const qreal minX
= m_pixmapPos
.x() + m_pixmap
.width() + 4 * itemListStyleOption
.padding
;
274 if (textInfo
->pos
.x() + columnWidth("text") > minX
) {
275 clipAdditionalInfoBounds
= true;
277 painter
->setClipRect(minX
, 0, size().width() - minX
, size().height(), Qt::IntersectClip
);
281 painter
->setPen(m_additionalInfoTextColor
);
282 painter
->setFont(m_customizedFont
);
284 for (int i
= 1; i
< m_sortedVisibleRoles
.count(); ++i
) {
285 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
[i
]);
286 painter
->drawStaticText(textInfo
->pos
, textInfo
->staticText
);
289 if (!m_rating
.isNull()) {
290 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
291 QPointF pos
= ratingTextInfo
->pos
;
292 const Qt::Alignment align
= ratingTextInfo
->staticText
.textOption().alignment();
293 if (align
& Qt::AlignHCenter
) {
294 pos
.rx() += (size().width() - m_rating
.width()) / 2 - 2;
296 painter
->drawPixmap(pos
, m_rating
);
299 if (clipAdditionalInfoBounds
) {
303 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
304 painter
->setBrush(Qt::NoBrush
);
305 painter
->setPen(Qt::green
);
306 painter
->drawRect(m_iconRect
);
308 painter
->setPen(Qt::red
);
309 painter
->drawText(QPointF(0, m_customizedFontMetrics
.height()), QString::number(index()));
310 painter
->drawRect(rect());
314 QRectF
KStandardItemListWidget::iconRect() const
316 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
320 QRectF
KStandardItemListWidget::textRect() const
322 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
326 QRectF
KStandardItemListWidget::textFocusRect() const
328 // In the compact- and details-layout a larger textRect() is returned to be aligned
329 // with the iconRect(). This is useful to have a larger selection/hover-area
330 // when having a quite large icon size but only one line of text. Still the
331 // focus rectangle should be shown as narrow as possible around the text.
333 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
336 case CompactLayout
: {
337 QRectF rect
= m_textRect
;
338 const TextInfo
* topText
= m_textInfo
.value(m_sortedVisibleRoles
.first());
339 const TextInfo
* bottomText
= m_textInfo
.value(m_sortedVisibleRoles
.last());
340 rect
.setTop(topText
->pos
.y());
341 rect
.setBottom(bottomText
->pos
.y() + bottomText
->staticText
.size().height());
345 case DetailsLayout
: {
346 QRectF rect
= m_textRect
;
347 const TextInfo
* textInfo
= m_textInfo
.value(m_sortedVisibleRoles
.first());
348 rect
.setTop(textInfo
->pos
.y());
349 rect
.setBottom(textInfo
->pos
.y() + textInfo
->staticText
.size().height());
351 const KItemListStyleOption
& option
= styleOption();
352 if (option
.extendedSelectionRegion
) {
353 const QString text
= textInfo
->staticText
.text();
354 rect
.setWidth(m_customizedFontMetrics
.width(text
) + 2 * option
.padding
);
367 QRectF
KStandardItemListWidget::expansionToggleRect() const
369 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
370 return m_isExpandable
? m_expansionArea
: QRectF();
373 QRectF
KStandardItemListWidget::selectionToggleRect() const
375 const_cast<KStandardItemListWidget
*>(this)->triggerCacheRefreshing();
377 const int iconHeight
= styleOption().iconSize
;
379 int toggleSize
= KIconLoader::SizeSmall
;
380 if (iconHeight
>= KIconLoader::SizeEnormous
) {
381 toggleSize
= KIconLoader::SizeMedium
;
382 } else if (iconHeight
>= KIconLoader::SizeLarge
) {
383 toggleSize
= KIconLoader::SizeSmallMedium
;
386 QPointF pos
= iconRect().topLeft();
388 // If the selection toggle has a very small distance to the
389 // widget borders, the size of the selection toggle will get
390 // increased to prevent an accidental clicking of the item
391 // when trying to hit the toggle.
392 const int widgetHeight
= size().height();
393 const int widgetWidth
= size().width();
394 const int minMargin
= 2;
396 if (toggleSize
+ minMargin
* 2 >= widgetHeight
) {
397 pos
.rx() -= (widgetHeight
- toggleSize
) / 2;
398 toggleSize
= widgetHeight
;
401 if (toggleSize
+ minMargin
* 2 >= widgetWidth
) {
402 pos
.ry() -= (widgetWidth
- toggleSize
) / 2;
403 toggleSize
= widgetWidth
;
407 return QRectF(pos
, QSizeF(toggleSize
, toggleSize
));
410 QPixmap
KStandardItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
413 QPixmap pixmap
= KItemListWidget::createDragPixmap(option
, widget
);
414 if (m_layout
!= DetailsLayout
) {
418 // Only return the content of the text-column as pixmap
419 const int leftClip
= m_pixmapPos
.x();
421 const TextInfo
* textInfo
= m_textInfo
.value("text");
422 const int rightClip
= textInfo
->pos
.x() +
423 textInfo
->staticText
.size().width() +
424 2 * styleOption().padding
;
426 QPixmap
clippedPixmap(rightClip
- leftClip
+ 1, pixmap
.height());
427 clippedPixmap
.fill(Qt::transparent
);
429 QPainter
painter(&clippedPixmap
);
430 painter
.drawPixmap(-leftClip
, 0, pixmap
);
432 return clippedPixmap
;
436 KItemListWidgetInformant
* KStandardItemListWidget::createInformant()
438 return new KStandardItemListWidgetInformant();
441 void KStandardItemListWidget::invalidateCache()
443 m_dirtyLayout
= true;
444 m_dirtyContent
= true;
447 void KStandardItemListWidget::refreshCache()
451 bool KStandardItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
457 bool KStandardItemListWidget::isHidden() const
462 QFont
KStandardItemListWidget::customizedFont(const QFont
& baseFont
) const
467 void KStandardItemListWidget::setTextColor(const QColor
& color
)
469 if (color
!= m_customTextColor
) {
470 m_customTextColor
= color
;
471 updateAdditionalInfoTextColor();
476 QColor
KStandardItemListWidget::textColor() const
478 if (m_customTextColor
.isValid() && !isSelected()) {
479 return m_customTextColor
;
482 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
483 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Text
;
484 return styleOption().palette
.brush(group
, role
).color();
487 void KStandardItemListWidget::setOverlay(const QPixmap
& overlay
)
490 m_dirtyContent
= true;
494 QPixmap
KStandardItemListWidget::overlay() const
500 QString
KStandardItemListWidget::roleText(const QByteArray
& role
,
501 const QHash
<QByteArray
, QVariant
>& values
) const
503 return static_cast<const KStandardItemListWidgetInformant
*>(informant())->roleText(role
, values
);
506 void KStandardItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
507 const QSet
<QByteArray
>& roles
)
511 m_dirtyContent
= true;
513 QSet
<QByteArray
> dirtyRoles
;
514 if (roles
.isEmpty()) {
515 dirtyRoles
= visibleRoles().toSet();
520 // The icon-state might depend from other roles and hence is
521 // marked as dirty whenever a role has been changed
522 dirtyRoles
.insert("iconPixmap");
523 dirtyRoles
.insert("iconName");
525 QSetIterator
<QByteArray
> it(dirtyRoles
);
526 while (it
.hasNext()) {
527 const QByteArray
& role
= it
.next();
528 m_dirtyContentRoles
.insert(role
);
532 void KStandardItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
533 const QList
<QByteArray
>& previous
)
536 m_sortedVisibleRoles
= current
;
537 m_dirtyLayout
= true;
540 void KStandardItemListWidget::columnWidthChanged(const QByteArray
& role
,
547 m_dirtyLayout
= true;
550 void KStandardItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
551 const KItemListStyleOption
& previous
)
555 updateAdditionalInfoTextColor();
556 m_dirtyLayout
= true;
559 void KStandardItemListWidget::hoveredChanged(bool hovered
)
562 m_dirtyLayout
= true;
565 void KStandardItemListWidget::selectedChanged(bool selected
)
568 updateAdditionalInfoTextColor();
569 m_dirtyContent
= true;
572 void KStandardItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
576 m_dirtyLayout
= true;
579 int KStandardItemListWidget::selectionLength(const QString
& text
) const
581 return text
.length();
584 void KStandardItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
588 QGraphicsView
* parent
= scene()->views()[0];
589 if (current
.isEmpty() || !parent
|| current
!= "text") {
591 emit
roleEditingCanceled(index(), current
, data().value(current
));
592 m_roleEditor
->deleteLater();
598 Q_ASSERT(!m_roleEditor
);
600 const TextInfo
* textInfo
= m_textInfo
.value("text");
602 m_roleEditor
= new KItemListRoleEditor(parent
);
603 m_roleEditor
->setIndex(index());
604 m_roleEditor
->setRole(current
);
605 m_roleEditor
->setFont(styleOption().font
);
607 const QString text
= data().value(current
).toString();
608 m_roleEditor
->setPlainText(text
);
610 QTextOption textOption
= textInfo
->staticText
.textOption();
611 m_roleEditor
->document()->setDefaultTextOption(textOption
);
613 const int textSelectionLength
= selectionLength(text
);
615 if (textSelectionLength
> 0) {
616 QTextCursor cursor
= m_roleEditor
->textCursor();
617 cursor
.movePosition(QTextCursor::StartOfBlock
);
618 cursor
.movePosition(QTextCursor::NextCharacter
, QTextCursor::KeepAnchor
, textSelectionLength
);
619 m_roleEditor
->setTextCursor(cursor
);
622 connect(m_roleEditor
, SIGNAL(roleEditingCanceled(int,QByteArray
,QVariant
)),
623 this, SLOT(slotRoleEditingCanceled(int,QByteArray
,QVariant
)));
624 connect(m_roleEditor
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
625 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
627 // Adjust the geometry of the editor
628 QRectF rect
= roleEditingRect(current
);
629 const int frameWidth
= m_roleEditor
->frameWidth();
630 rect
.adjust(-frameWidth
, -frameWidth
, frameWidth
, frameWidth
);
631 rect
.translate(pos());
632 if (rect
.right() > parent
->width()) {
633 rect
.setWidth(parent
->width() - rect
.left());
635 m_roleEditor
->setGeometry(rect
.toRect());
636 m_roleEditor
->show();
637 m_roleEditor
->setFocus();
640 void KStandardItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
643 setEditedRole(QByteArray());
644 Q_ASSERT(!m_roleEditor
);
647 KItemListWidget::resizeEvent(event
);
649 m_dirtyLayout
= true;
652 void KStandardItemListWidget::showEvent(QShowEvent
* event
)
654 KItemListWidget::showEvent(event
);
656 // Listen to changes of the clipboard to mark the item as cut/uncut
657 KFileItemClipboard
* clipboard
= KFileItemClipboard::instance();
659 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
660 m_isCut
= clipboard
->isCut(itemUrl
);
662 connect(clipboard
, SIGNAL(cutItemsChanged()),
663 this, SLOT(slotCutItemsChanged()));
666 void KStandardItemListWidget::hideEvent(QHideEvent
* event
)
668 disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
669 this, SLOT(slotCutItemsChanged()));
671 KItemListWidget::hideEvent(event
);
674 void KStandardItemListWidget::slotCutItemsChanged()
676 const KUrl itemUrl
= data().value("url").value
<KUrl
>();
677 const bool isCut
= KFileItemClipboard::instance()->isCut(itemUrl
);
678 if (m_isCut
!= isCut
) {
680 m_pixmap
= QPixmap();
681 m_dirtyContent
= true;
686 void KStandardItemListWidget::slotRoleEditingCanceled(int index
,
687 const QByteArray
& role
,
688 const QVariant
& value
)
691 emit
roleEditingCanceled(index
, role
, value
);
692 setEditedRole(QByteArray());
695 void KStandardItemListWidget::slotRoleEditingFinished(int index
,
696 const QByteArray
& role
,
697 const QVariant
& value
)
700 emit
roleEditingFinished(index
, role
, value
);
701 setEditedRole(QByteArray());
704 void KStandardItemListWidget::triggerCacheRefreshing()
706 if ((!m_dirtyContent
&& !m_dirtyLayout
) || index() < 0) {
712 const QHash
<QByteArray
, QVariant
> values
= data();
713 m_isExpandable
= m_supportsItemExpanding
&& values
["isExpandable"].toBool();
714 m_isHidden
= isHidden();
715 m_customizedFont
= customizedFont(styleOption().font
);
716 m_customizedFontMetrics
= QFontMetrics(m_customizedFont
);
718 updateExpansionArea();
722 m_dirtyLayout
= false;
723 m_dirtyContent
= false;
724 m_dirtyContentRoles
.clear();
727 void KStandardItemListWidget::updateExpansionArea()
729 if (m_supportsItemExpanding
) {
730 const QHash
<QByteArray
, QVariant
> values
= data();
731 Q_ASSERT(values
.contains("expandedParentsCount"));
732 const int expandedParentsCount
= values
.value("expandedParentsCount", 0).toInt();
733 if (expandedParentsCount
>= 0) {
734 const qreal widgetHeight
= size().height();
735 const qreal inc
= (widgetHeight
- KIconLoader::SizeSmall
) / 2;
736 const qreal x
= expandedParentsCount
* widgetHeight
+ inc
;
738 m_expansionArea
= QRectF(x
, y
, KIconLoader::SizeSmall
, KIconLoader::SizeSmall
);
743 m_expansionArea
= QRectF();
746 void KStandardItemListWidget::updatePixmapCache()
748 // Precondition: Requires already updated m_textPos values to calculate
749 // the remaining height when the alignment is vertical.
751 const QSizeF widgetSize
= size();
752 const bool iconOnTop
= (m_layout
== IconsLayout
);
753 const KItemListStyleOption
& option
= styleOption();
754 const qreal padding
= option
.padding
;
756 const int maxIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: option
.iconSize
;
757 const int maxIconHeight
= option
.iconSize
;
759 const QHash
<QByteArray
, QVariant
> values
= data();
761 bool updatePixmap
= (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
);
762 if (!updatePixmap
&& m_dirtyContent
) {
763 updatePixmap
= m_dirtyContentRoles
.isEmpty()
764 || m_dirtyContentRoles
.contains("iconPixmap")
765 || m_dirtyContentRoles
.contains("iconName")
766 || m_dirtyContentRoles
.contains("iconOverlays");
770 m_pixmap
= values
["iconPixmap"].value
<QPixmap
>();
771 if (m_pixmap
.isNull()) {
772 // Use the icon that fits to the MIME-type
773 QString iconName
= values
["iconName"].toString();
774 if (iconName
.isEmpty()) {
775 // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
776 // use a generic icon as fallback
777 iconName
= QLatin1String("unknown");
779 m_pixmap
= pixmapForIcon(iconName
, maxIconHeight
);
780 } else if (m_pixmap
.width() != maxIconWidth
|| m_pixmap
.height() != maxIconHeight
) {
781 // A custom pixmap has been applied. Assure that the pixmap
782 // is scaled to the maximum available size.
783 KPixmapModifier::scale(m_pixmap
, QSize(maxIconWidth
, maxIconHeight
));
786 const QStringList overlays
= values
["iconOverlays"].toStringList();
788 // Strangely KFileItem::overlays() returns empty string-values, so
789 // we need to check first whether an overlay must be drawn at all.
790 // It is more efficient to do it here, as KIconLoader::drawOverlays()
791 // assumes that an overlay will be drawn and has some additional
793 foreach (const QString
& overlay
, overlays
) {
794 if (!overlay
.isEmpty()) {
795 // There is at least one overlay, draw all overlays above m_pixmap
796 // and cancel the check
797 KIconLoader::global()->drawOverlays(overlays
, m_pixmap
, KIconLoader::Desktop
);
803 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
804 m_pixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
808 KIconEffect::semiTransparent(m_pixmap
);
812 const QColor color
= palette().brush(QPalette::Normal
, QPalette::Highlight
).color();
813 QImage image
= m_pixmap
.toImage();
814 KIconEffect::colorize(image
, color
, 1.0f
);
815 m_pixmap
= QPixmap::fromImage(image
);
819 if (!m_overlay
.isNull()) {
820 QPainter
painter(&m_pixmap
);
821 painter
.drawPixmap(0, m_pixmap
.height() - m_overlay
.height(), m_overlay
);
824 int scaledIconSize
= 0;
826 const TextInfo
* textInfo
= m_textInfo
.value("text");
827 scaledIconSize
= static_cast<int>(textInfo
->pos
.y() - 2 * padding
);
829 const int textRowsCount
= (m_layout
== CompactLayout
) ? visibleRoles().count() : 1;
830 const qreal requiredTextHeight
= textRowsCount
* m_customizedFontMetrics
.height();
831 scaledIconSize
= (requiredTextHeight
< maxIconHeight
) ?
832 widgetSize
.height() - 2 * padding
: maxIconHeight
;
835 const int maxScaledIconWidth
= iconOnTop
? widgetSize
.width() - 2 * padding
: scaledIconSize
;
836 const int maxScaledIconHeight
= scaledIconSize
;
838 m_scaledPixmapSize
= m_pixmap
.size();
839 m_scaledPixmapSize
.scale(maxScaledIconWidth
, maxScaledIconHeight
, Qt::KeepAspectRatio
);
842 // Center horizontally and align on bottom within the icon-area
843 m_pixmapPos
.setX((widgetSize
.width() - m_scaledPixmapSize
.width()) / 2);
844 m_pixmapPos
.setY(padding
+ scaledIconSize
- m_scaledPixmapSize
.height());
846 // Center horizontally and vertically within the icon-area
847 const TextInfo
* textInfo
= m_textInfo
.value("text");
848 m_pixmapPos
.setX(textInfo
->pos
.x() - 2 * padding
849 - (scaledIconSize
+ m_scaledPixmapSize
.width()) / 2);
850 m_pixmapPos
.setY(padding
851 + (scaledIconSize
- m_scaledPixmapSize
.height()) / 2);
854 m_iconRect
= QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
));
856 // Prepare the pixmap that is used when the item gets hovered
858 m_hoverPixmap
= m_pixmap
;
859 KIconEffect
* effect
= KIconLoader::global()->iconEffect();
860 // In the KIconLoader terminology, active = hover.
861 if (effect
->hasEffect(KIconLoader::Desktop
, KIconLoader::ActiveState
)) {
862 m_hoverPixmap
= effect
->apply(m_pixmap
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
864 m_hoverPixmap
= m_pixmap
;
866 } else if (hoverOpacity() <= 0.0) {
867 // No hover animation is ongoing. Clear m_hoverPixmap to save memory.
868 m_hoverPixmap
= QPixmap();
872 void KStandardItemListWidget::updateTextsCache()
874 QTextOption textOption
;
877 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
878 textOption
.setAlignment(Qt::AlignHCenter
);
882 textOption
.setAlignment(Qt::AlignLeft
);
883 textOption
.setWrapMode(QTextOption::NoWrap
);
890 qDeleteAll(m_textInfo
);
892 for (int i
= 0; i
< m_sortedVisibleRoles
.count(); ++i
) {
893 TextInfo
* textInfo
= new TextInfo();
894 textInfo
->staticText
.setTextFormat(Qt::PlainText
);
895 textInfo
->staticText
.setPerformanceHint(QStaticText::AggressiveCaching
);
896 textInfo
->staticText
.setTextOption(textOption
);
897 m_textInfo
.insert(m_sortedVisibleRoles
[i
], textInfo
);
901 case IconsLayout
: updateIconsLayoutTextCache(); break;
902 case CompactLayout
: updateCompactLayoutTextCache(); break;
903 case DetailsLayout
: updateDetailsLayoutTextCache(); break;
904 default: Q_ASSERT(false); break;
907 const TextInfo
* ratingTextInfo
= m_textInfo
.value("rating");
908 if (ratingTextInfo
) {
909 // The text of the rating-role has been set to empty to get
910 // replaced by a rating-image showing the rating as stars.
911 const KItemListStyleOption
& option
= styleOption();
912 QSizeF ratingSize
= preferredRatingSize(option
);
914 const qreal availableWidth
= (m_layout
== DetailsLayout
)
915 ? columnWidth("rating") - columnPadding(option
)
917 if (ratingSize
.width() > availableWidth
) {
918 ratingSize
.rwidth() = availableWidth
;
920 m_rating
= QPixmap(ratingSize
.toSize());
921 m_rating
.fill(Qt::transparent
);
923 QPainter
painter(&m_rating
);
924 const QRect
rect(0, 0, m_rating
.width(), m_rating
.height());
925 const int rating
= data().value("rating").toInt();
926 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
927 } else if (!m_rating
.isNull()) {
928 m_rating
= QPixmap();
932 void KStandardItemListWidget::updateIconsLayoutTextCache()
939 // might get wrapped above
944 const QHash
<QByteArray
, QVariant
> values
= data();
946 const KItemListStyleOption
& option
= styleOption();
947 const qreal padding
= option
.padding
;
948 const qreal maxWidth
= size().width() - 2 * padding
;
949 const qreal widgetHeight
= size().height();
950 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
952 // Initialize properties for the "text" role. It will be used as anchor
953 // for initializing the position of the other roles.
954 TextInfo
* nameTextInfo
= m_textInfo
.value("text");
955 const QString nameText
= KStringHandler::preProcessWrap(values
["text"].toString());
956 nameTextInfo
->staticText
.setText(nameText
);
958 // Calculate the number of lines required for the name and the required width
960 qreal nameHeight
= 0;
963 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
964 const int maxNameLines
= (option
.maxTextSize
.height() / int(lineSpacing
)) - additionalRolesCount
;
966 QTextLayout
layout(nameTextInfo
->staticText
.text(), m_customizedFont
);
967 layout
.setTextOption(nameTextInfo
->staticText
.textOption());
968 layout
.beginLayout();
969 int nameLineIndex
= 0;
970 while ((line
= layout
.createLine()).isValid()) {
971 line
.setLineWidth(maxWidth
);
972 nameWidth
= qMax(nameWidth
, line
.naturalTextWidth());
973 nameHeight
+= line
.height();
976 if (nameLineIndex
== maxNameLines
) {
977 // The maximum number of textlines has been reached. If this is
978 // the case provide an elided text if necessary.
979 const int textLength
= line
.textStart() + line
.textLength();
980 if (textLength
< nameText
.length()) {
981 // Elide the last line of the text
982 QString lastTextLine
= nameText
.mid(line
.textStart(), line
.textLength());
983 lastTextLine
= m_customizedFontMetrics
.elidedText(lastTextLine
,
985 line
.naturalTextWidth() - 1);
986 const QString elidedText
= nameText
.left(line
.textStart()) + lastTextLine
;
987 nameTextInfo
->staticText
.setText(elidedText
);
994 // Use one line for each additional information
995 nameTextInfo
->staticText
.setTextWidth(maxWidth
);
996 nameTextInfo
->pos
= QPointF(padding
, widgetHeight
-
998 additionalRolesCount
* lineSpacing
-
1000 m_textRect
= QRectF(padding
+ (maxWidth
- nameWidth
) / 2,
1001 nameTextInfo
->pos
.y(),
1005 // Calculate the position for each additional information
1006 qreal y
= nameTextInfo
->pos
.y() + nameHeight
;
1007 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1008 if (role
== "text") {
1012 const QString text
= roleText(role
, values
);
1013 TextInfo
* textInfo
= m_textInfo
.value(role
);
1014 textInfo
->staticText
.setText(text
);
1016 qreal requiredWidth
= 0;
1018 QTextLayout
layout(text
, m_customizedFont
);
1019 QTextOption textOption
;
1020 textOption
.setWrapMode(QTextOption::NoWrap
);
1021 layout
.setTextOption(textOption
);
1023 layout
.beginLayout();
1024 QTextLine textLine
= layout
.createLine();
1025 if (textLine
.isValid()) {
1026 textLine
.setLineWidth(maxWidth
);
1027 requiredWidth
= textLine
.naturalTextWidth();
1028 if (requiredWidth
> maxWidth
) {
1029 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1030 textInfo
->staticText
.setText(elidedText
);
1031 requiredWidth
= m_customizedFontMetrics
.width(elidedText
);
1032 } else if (role
== "rating") {
1033 // Use the width of the rating pixmap, because the rating text is empty.
1034 requiredWidth
= m_rating
.width();
1039 textInfo
->pos
= QPointF(padding
, y
);
1040 textInfo
->staticText
.setTextWidth(maxWidth
);
1042 const QRectF
textRect(padding
+ (maxWidth
- requiredWidth
) / 2, y
, requiredWidth
, lineSpacing
);
1043 m_textRect
|= textRect
;
1048 // Add a padding to the text rectangle
1049 m_textRect
.adjust(-padding
, -padding
, padding
, padding
);
1052 void KStandardItemListWidget::updateCompactLayoutTextCache()
1054 // +------+ Name role
1055 // | Icon | Additional role 1
1056 // +------+ Additional role 2
1058 const QHash
<QByteArray
, QVariant
> values
= data();
1060 const KItemListStyleOption
& option
= styleOption();
1061 const qreal widgetHeight
= size().height();
1062 const qreal lineSpacing
= m_customizedFontMetrics
.lineSpacing();
1063 const qreal textLinesHeight
= qMax(visibleRoles().count(), 1) * lineSpacing
;
1064 const int scaledIconSize
= (textLinesHeight
< option
.iconSize
) ? widgetHeight
- 2 * option
.padding
: option
.iconSize
;
1066 qreal maximumRequiredTextWidth
= 0;
1067 const qreal x
= option
.padding
* 3 + scaledIconSize
;
1068 qreal y
= qRound((widgetHeight
- textLinesHeight
) / 2);
1069 const qreal maxWidth
= size().width() - x
- option
.padding
;
1070 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1071 const QString text
= roleText(role
, values
);
1072 TextInfo
* textInfo
= m_textInfo
.value(role
);
1073 textInfo
->staticText
.setText(text
);
1075 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1076 if (requiredWidth
> maxWidth
) {
1077 requiredWidth
= maxWidth
;
1078 const QString elidedText
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, maxWidth
);
1079 textInfo
->staticText
.setText(elidedText
);
1082 textInfo
->pos
= QPointF(x
, y
);
1083 textInfo
->staticText
.setTextWidth(maxWidth
);
1085 maximumRequiredTextWidth
= qMax(maximumRequiredTextWidth
, requiredWidth
);
1090 m_textRect
= QRectF(x
- option
.padding
, 0, maximumRequiredTextWidth
+ 2 * option
.padding
, widgetHeight
);
1093 void KStandardItemListWidget::updateDetailsLayoutTextCache()
1095 // Precondition: Requires already updated m_expansionArea
1096 // to determine the left position.
1099 // | Icon | Name role Additional role 1 Additional role 2
1101 m_textRect
= QRectF();
1103 const KItemListStyleOption
& option
= styleOption();
1104 const QHash
<QByteArray
, QVariant
> values
= data();
1106 const qreal widgetHeight
= size().height();
1107 const int scaledIconSize
= widgetHeight
- 2 * option
.padding
;
1108 const int fontHeight
= m_customizedFontMetrics
.height();
1110 const qreal columnWidthInc
= columnPadding(option
);
1111 qreal firstColumnInc
= scaledIconSize
;
1112 if (m_supportsItemExpanding
) {
1113 firstColumnInc
+= (m_expansionArea
.left() + m_expansionArea
.right() + widgetHeight
) / 2;
1115 firstColumnInc
+= option
.padding
;
1118 qreal x
= firstColumnInc
;
1119 const qreal y
= qMax(qreal(option
.padding
), (widgetHeight
- fontHeight
) / 2);
1121 foreach (const QByteArray
& role
, m_sortedVisibleRoles
) {
1122 QString text
= roleText(role
, values
);
1124 // Elide the text in case it does not fit into the available column-width
1125 qreal requiredWidth
= m_customizedFontMetrics
.width(text
);
1126 const qreal roleWidth
= columnWidth(role
);
1127 qreal availableTextWidth
= roleWidth
- columnWidthInc
;
1129 const bool isTextRole
= (role
== "text");
1131 availableTextWidth
-= firstColumnInc
;
1134 if (requiredWidth
> availableTextWidth
) {
1135 text
= m_customizedFontMetrics
.elidedText(text
, Qt::ElideRight
, availableTextWidth
);
1136 requiredWidth
= m_customizedFontMetrics
.width(text
);
1139 TextInfo
* textInfo
= m_textInfo
.value(role
);
1140 textInfo
->staticText
.setText(text
);
1141 textInfo
->pos
= QPointF(x
+ columnWidthInc
/ 2, y
);
1145 const qreal textWidth
= option
.extendedSelectionRegion
1146 ? size().width() - textInfo
->pos
.x()
1147 : requiredWidth
+ 2 * option
.padding
;
1148 m_textRect
= QRectF(textInfo
->pos
.x() - option
.padding
, 0,
1149 textWidth
, size().height());
1151 // The column after the name should always be aligned on the same x-position independent
1152 // from the expansion-level shown in the name column
1153 x
-= firstColumnInc
;
1154 } else if (isRoleRightAligned(role
)) {
1155 textInfo
->pos
.rx() += roleWidth
- requiredWidth
- columnWidthInc
;
1160 void KStandardItemListWidget::updateAdditionalInfoTextColor()
1163 if (m_customTextColor
.isValid()) {
1164 c1
= m_customTextColor
;
1165 } else if (isSelected() && m_layout
!= DetailsLayout
) {
1166 c1
= styleOption().palette
.highlightedText().color();
1168 c1
= styleOption().palette
.text().color();
1171 // For the color of the additional info the inactive text color
1172 // is not used as this might lead to unreadable text for some color schemes. Instead
1173 // the text color c1 is slightly mixed with the background color.
1174 const QColor c2
= styleOption().palette
.base().color();
1176 const int p2
= 100 - p1
;
1177 m_additionalInfoTextColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
1178 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
1179 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
1182 void KStandardItemListWidget::drawPixmap(QPainter
* painter
, const QPixmap
& pixmap
)
1184 if (m_scaledPixmapSize
!= pixmap
.size()) {
1185 QPixmap scaledPixmap
= pixmap
;
1186 KPixmapModifier::scale(scaledPixmap
, m_scaledPixmapSize
);
1187 painter
->drawPixmap(m_pixmapPos
, scaledPixmap
);
1189 #ifdef KSTANDARDITEMLISTWIDGET_DEBUG
1190 painter
->setPen(Qt::blue
);
1191 painter
->drawRect(QRectF(m_pixmapPos
, QSizeF(m_scaledPixmapSize
)));
1194 painter
->drawPixmap(m_pixmapPos
, pixmap
);
1198 void KStandardItemListWidget::drawSiblingsInformation(QPainter
* painter
)
1200 const int siblingSize
= size().height();
1201 const int x
= (m_expansionArea
.left() + m_expansionArea
.right() - siblingSize
) / 2;
1202 QRect
siblingRect(x
, 0, siblingSize
, siblingSize
);
1204 QStyleOption option
;
1205 bool isItemSibling
= true;
1207 const QBitArray siblings
= siblingsInformation();
1208 for (int i
= siblings
.count() - 1; i
>= 0; --i
) {
1209 option
.rect
= siblingRect
;
1210 option
.state
= siblings
.at(i
) ? QStyle::State_Sibling
: QStyle::State_None
;
1212 if (isItemSibling
) {
1213 option
.state
|= QStyle::State_Item
;
1214 if (m_isExpandable
) {
1215 option
.state
|= QStyle::State_Children
;
1217 if (data()["isExpanded"].toBool()) {
1218 option
.state
|= QStyle::State_Open
;
1220 isItemSibling
= false;
1223 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &option
, painter
);
1225 siblingRect
.translate(-siblingRect
.width(), 0);
1229 QRectF
KStandardItemListWidget::roleEditingRect(const QByteArray
& role
) const
1231 const TextInfo
* textInfo
= m_textInfo
.value(role
);
1236 QRectF
rect(textInfo
->pos
, textInfo
->staticText
.size());
1237 if (m_layout
== DetailsLayout
) {
1238 rect
.setWidth(columnWidth(role
) - rect
.x());
1244 void KStandardItemListWidget::closeRoleEditor()
1246 if (m_roleEditor
->hasFocus()) {
1247 // If the editing was not ended by a FocusOut event, we have
1248 // to transfer the keyboard focus back to the KItemListContainer.
1249 scene()->views()[0]->parentWidget()->setFocus();
1251 m_roleEditor
->deleteLater();
1255 QPixmap
KStandardItemListWidget::pixmapForIcon(const QString
& name
, int size
)
1257 const KIcon
icon(name
);
1260 if (size
<= KIconLoader::SizeSmall
) {
1261 requestedSize
= KIconLoader::SizeSmall
;
1262 } else if (size
<= KIconLoader::SizeSmallMedium
) {
1263 requestedSize
= KIconLoader::SizeSmallMedium
;
1264 } else if (size
<= KIconLoader::SizeMedium
) {
1265 requestedSize
= KIconLoader::SizeMedium
;
1266 } else if (size
<= KIconLoader::SizeLarge
) {
1267 requestedSize
= KIconLoader::SizeLarge
;
1268 } else if (size
<= KIconLoader::SizeHuge
) {
1269 requestedSize
= KIconLoader::SizeHuge
;
1270 } else if (size
<= KIconLoader::SizeEnormous
) {
1271 requestedSize
= KIconLoader::SizeEnormous
;
1272 } else if (size
<= KIconLoader::SizeEnormous
* 2) {
1273 requestedSize
= KIconLoader::SizeEnormous
* 2;
1275 requestedSize
= size
;
1278 QPixmap pixmap
= icon
.pixmap(requestedSize
, requestedSize
);
1279 if (requestedSize
!= size
) {
1280 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
1286 QSizeF
KStandardItemListWidget::preferredRatingSize(const KItemListStyleOption
& option
)
1288 const qreal height
= option
.fontMetrics
.ascent();
1289 return QSizeF(height
* 5, height
);
1292 qreal
KStandardItemListWidget::columnPadding(const KItemListStyleOption
& option
)
1294 return option
.padding
* 6;
1297 #include "kstandarditemlistwidget.moc"