1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
23 #include "kitemlistwidget.h"
25 #include "kitemlistview.h"
26 #include "kitemmodelbase.h"
31 #include <QPropertyAnimation>
33 #include <QStyleOption>
35 KItemListWidget::KItemListWidget(QGraphicsItem
* parent
) :
36 QGraphicsWidget(parent
, 0),
41 m_alternatingBackgroundColors(false),
44 m_visibleRolesSizes(),
52 KItemListWidget::~KItemListWidget()
57 void KItemListWidget::setIndex(int index
)
59 if (m_index
!= index
) {
60 if (m_hoverAnimation
) {
61 m_hoverAnimation
->stop();
70 int KItemListWidget::index() const
75 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
76 const QSet
<QByteArray
>& roles
)
79 if (roles
.isEmpty()) {
83 foreach (const QByteArray
& role
, roles
) {
84 m_data
[role
] = data
[role
];
86 dataChanged(m_data
, roles
);
90 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
95 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
99 painter
->setRenderHint(QPainter::Antialiasing
);
101 if (m_alternatingBackgroundColors
&& (m_index
& 0x1)) {
102 const QColor backgroundColor
= m_styleOption
.palette
.color(QPalette::AlternateBase
);
103 const QRectF
backgroundRect(0, 0, size().width(), size().height());
104 painter
->fillRect(backgroundRect
, backgroundColor
);
107 const QRect iconBounds
= iconRect().toRect();
109 QStyleOptionViewItemV4 viewItemOption
;
110 viewItemOption
.initFrom(widget
);
111 viewItemOption
.rect
= iconBounds
;
112 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_Selected
| QStyle::State_Item
;
113 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
114 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
116 drawTextBackground(painter
);
120 QStyleOptionViewItemV4 viewItemOption
;
121 viewItemOption
.initFrom(widget
);
122 viewItemOption
.rect
= textRect().toRect();
123 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
;
124 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
125 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &viewItemOption
, painter
, widget
);
128 if (m_hoverOpacity
<= 0.0) {
133 // Initialize the m_hoverCache pixmap to improve the drawing performance
134 // when fading the hover background
135 m_hoverCache
= new QPixmap(iconBounds
.size());
136 m_hoverCache
->fill(Qt::transparent
);
138 QPainter
pixmapPainter(m_hoverCache
);
140 QStyleOptionViewItemV4 viewItemOption
;
141 viewItemOption
.initFrom(widget
);
142 viewItemOption
.rect
= QRect(0, 0, iconBounds
.width(), iconBounds
.height());
143 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_MouseOver
| QStyle::State_Item
;
144 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
146 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, &pixmapPainter
, widget
);
149 const qreal opacity
= painter
->opacity();
150 painter
->setOpacity(m_hoverOpacity
* opacity
);
151 painter
->drawPixmap(iconBounds
.topLeft(), *m_hoverCache
);
152 drawTextBackground(painter
);
153 painter
->setOpacity(opacity
);
156 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
158 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
159 m_visibleRoles
= roles
;
160 visibleRolesChanged(roles
, previousRoles
);
163 QList
<QByteArray
> KItemListWidget::visibleRoles() const
165 return m_visibleRoles
;
168 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
170 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
171 m_visibleRolesSizes
= rolesSizes
;
172 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
175 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
177 return m_visibleRolesSizes
;
180 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
182 const KItemListStyleOption previous
= m_styleOption
;
184 m_styleOption
= option
;
186 styleOptionChanged(option
, previous
);
189 const KItemListStyleOption
& KItemListWidget::styleOption() const
191 return m_styleOption
;
194 void KItemListWidget::setSelected(bool selected
)
196 if (m_selected
!= selected
) {
197 m_selected
= selected
;
198 selectedChanged(selected
);
203 bool KItemListWidget::isSelected() const
208 void KItemListWidget::setCurrent(bool current
)
210 if (m_current
!= current
) {
212 currentChanged(current
);
217 bool KItemListWidget::isCurrent() const
222 void KItemListWidget::setHovered(bool hovered
)
224 if (hovered
== m_hovered
) {
230 if (!m_hoverAnimation
) {
231 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
232 m_hoverAnimation
->setDuration(200);
234 m_hoverAnimation
->stop();
237 m_hoverAnimation
->setEndValue(1.0);
239 m_hoverAnimation
->setEndValue(0.0);
242 m_hoverAnimation
->start();
244 hoveredChanged(hovered
);
249 bool KItemListWidget::isHovered() const
254 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
256 if (m_alternatingBackgroundColors
!= enable
) {
257 m_alternatingBackgroundColors
= enable
;
258 alternatingBackgroundColorsChanged(enable
);
263 bool KItemListWidget::alternatingBackgroundColors() const
265 return m_alternatingBackgroundColors
;
268 bool KItemListWidget::contains(const QPointF
& point
) const
270 if (!QGraphicsWidget::contains(point
)) {
274 return iconRect().contains(point
) ||
275 textRect().contains(point
) ||
276 expansionToggleRect().contains(point
) ||
277 selectionToggleRect().contains(point
);
280 QRectF
KItemListWidget::selectionToggleRect() const
285 QRectF
KItemListWidget::expansionToggleRect() const
290 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
291 const QSet
<QByteArray
>& roles
)
298 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
299 const QList
<QByteArray
>& previous
)
306 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
307 const QHash
<QByteArray
, QSizeF
>& previous
)
314 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
315 const KItemListStyleOption
& previous
)
322 void KItemListWidget::currentChanged(bool current
)
327 void KItemListWidget::selectedChanged(bool selected
)
332 void KItemListWidget::hoveredChanged(bool hovered
)
337 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
342 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
344 QGraphicsWidget::resizeEvent(event
);
348 qreal
KItemListWidget::hoverOpacity() const
350 return m_hoverOpacity
;
353 void KItemListWidget::setHoverOpacity(qreal opacity
)
355 m_hoverOpacity
= opacity
;
359 void KItemListWidget::clearHoverCache()
365 void KItemListWidget::drawTextBackground(QPainter
* painter
)
367 const qreal opacity
= painter
->opacity();
369 QRectF textBounds
= textRect();
370 const qreal marginDiff
= m_styleOption
.margin
/ 2;
371 textBounds
.adjust(marginDiff
, marginDiff
, -marginDiff
, -marginDiff
);
372 painter
->setOpacity(opacity
* 0.1);
373 painter
->setPen(Qt::NoPen
);
374 painter
->setBrush(m_styleOption
.palette
.text());
375 painter
->drawRoundedRect(textBounds
, 4, 4);
377 painter
->setOpacity(opacity
);
380 #include "kitemlistwidget.moc"