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),
43 m_visibleRolesSizes(),
51 KItemListWidget::~KItemListWidget()
56 void KItemListWidget::setIndex(int index
)
58 if (m_index
!= index
) {
59 if (m_hoverAnimation
) {
60 m_hoverAnimation
->stop();
69 int KItemListWidget::index() const
74 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
75 const QSet
<QByteArray
>& roles
)
78 if (roles
.isEmpty()) {
82 foreach (const QByteArray
& role
, roles
) {
83 m_data
[role
] = data
[role
];
85 dataChanged(m_data
, roles
);
89 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
94 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
98 const QRect iconBounds
= iconBoundingRect().toRect();
100 QStyleOptionViewItemV4 viewItemOption
;
101 viewItemOption
.initFrom(widget
);
102 viewItemOption
.rect
= iconBounds
;
103 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_Selected
| QStyle::State_Item
;
104 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
105 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
108 if (m_hoverOpacity
<= 0.0) {
113 m_hoverCache
= new QPixmap(iconBounds
.size());
114 m_hoverCache
->fill(Qt::transparent
);
116 QPainter
pixmapPainter(m_hoverCache
);
118 QStyleOptionViewItemV4 viewItemOption
;
119 viewItemOption
.initFrom(widget
);
120 viewItemOption
.rect
= QRect(0, 0, iconBounds
.width(), iconBounds
.height());
121 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_MouseOver
| QStyle::State_Item
;
122 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
124 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, &pixmapPainter
, widget
);
127 const qreal opacity
= painter
->opacity();
128 painter
->setOpacity(m_hoverOpacity
* opacity
);
129 painter
->drawPixmap(iconBounds
.topLeft(), *m_hoverCache
);
130 painter
->setOpacity(opacity
);
133 void KItemListWidget::setVisibleRoles(const QHash
<QByteArray
, int>& roles
)
135 const QHash
<QByteArray
, int> previousRoles
= m_visibleRoles
;
136 m_visibleRoles
= roles
;
137 visibleRolesChanged(roles
, previousRoles
);
140 QHash
<QByteArray
, int> KItemListWidget::visibleRoles() const
142 return m_visibleRoles
;
145 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
147 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
148 m_visibleRolesSizes
= rolesSizes
;
149 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
152 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
154 return m_visibleRolesSizes
;
157 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
159 const KItemListStyleOption previous
= m_styleOption
;
161 m_styleOption
= option
;
163 styleOptionChanged(option
, previous
);
166 const KItemListStyleOption
& KItemListWidget::styleOption() const
168 return m_styleOption
;
171 void KItemListWidget::setSelected(bool selected
)
173 if (m_selected
!= selected
) {
174 m_selected
= selected
;
175 selectedChanged(selected
);
180 bool KItemListWidget::isSelected() const
185 void KItemListWidget::setCurrent(bool current
)
187 if (m_current
!= current
) {
189 currentChanged(current
);
194 bool KItemListWidget::isCurrent() const
199 void KItemListWidget::setHovered(bool hovered
)
201 if (hovered
== m_hovered
) {
207 if (!m_hoverAnimation
) {
208 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
209 m_hoverAnimation
->setDuration(200);
211 m_hoverAnimation
->stop();
214 m_hoverAnimation
->setEndValue(1.0);
216 m_hoverAnimation
->setEndValue(0.0);
219 m_hoverAnimation
->start();
221 hoveredChanged(hovered
);
226 bool KItemListWidget::isHovered() const
231 bool KItemListWidget::contains(const QPointF
& point
) const
233 if (!QGraphicsWidget::contains(point
)) {
237 return iconBoundingRect().contains(point
) ||
238 textBoundingRect().contains(point
) ||
239 expansionToggleRect().contains(point
) ||
240 selectionToggleRect().contains(point
);
243 QRectF
KItemListWidget::selectionToggleRect() const
248 QRectF
KItemListWidget::expansionToggleRect() const
253 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
254 const QSet
<QByteArray
>& roles
)
261 void KItemListWidget::visibleRolesChanged(const QHash
<QByteArray
, int>& current
,
262 const QHash
<QByteArray
, int>& previous
)
269 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
270 const QHash
<QByteArray
, QSizeF
>& previous
)
277 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
278 const KItemListStyleOption
& previous
)
285 void KItemListWidget::currentChanged(bool current
)
290 void KItemListWidget::selectedChanged(bool selected
)
295 void KItemListWidget::hoveredChanged(bool hovered
)
300 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
302 QGraphicsWidget::resizeEvent(event
);
306 qreal
KItemListWidget::hoverOpacity() const
308 return m_hoverOpacity
;
311 void KItemListWidget::setHoverOpacity(qreal opacity
)
313 m_hoverOpacity
= opacity
;
317 void KItemListWidget::clearCache()
323 #include "kitemlistwidget.moc"