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 painter
->setRenderHint(QPainter::Antialiasing
);
100 const QRect iconBounds
= iconBoundingRect().toRect();
102 QStyleOptionViewItemV4 viewItemOption
;
103 viewItemOption
.initFrom(widget
);
104 viewItemOption
.rect
= iconBounds
;
105 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_Selected
| QStyle::State_Item
;
106 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
107 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
109 drawTextBackground(painter
);
113 drawFocusIndicator(painter
);
116 if (m_hoverOpacity
<= 0.0) {
121 // Initialize the m_hoverCache pixmap to improve the drawing performance
122 // when fading the hover background
123 m_hoverCache
= new QPixmap(iconBounds
.size());
124 m_hoverCache
->fill(Qt::transparent
);
126 QPainter
pixmapPainter(m_hoverCache
);
128 QStyleOptionViewItemV4 viewItemOption
;
129 viewItemOption
.initFrom(widget
);
130 viewItemOption
.rect
= QRect(0, 0, iconBounds
.width(), iconBounds
.height());
131 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_MouseOver
| QStyle::State_Item
;
132 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
134 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, &pixmapPainter
, widget
);
137 const qreal opacity
= painter
->opacity();
138 painter
->setOpacity(m_hoverOpacity
* opacity
);
139 painter
->drawPixmap(iconBounds
.topLeft(), *m_hoverCache
);
140 drawTextBackground(painter
);
141 painter
->setOpacity(opacity
);
144 void KItemListWidget::setVisibleRoles(const QHash
<QByteArray
, int>& roles
)
146 const QHash
<QByteArray
, int> previousRoles
= m_visibleRoles
;
147 m_visibleRoles
= roles
;
148 visibleRolesChanged(roles
, previousRoles
);
151 QHash
<QByteArray
, int> KItemListWidget::visibleRoles() const
153 return m_visibleRoles
;
156 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
158 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
159 m_visibleRolesSizes
= rolesSizes
;
160 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
163 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
165 return m_visibleRolesSizes
;
168 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
170 const KItemListStyleOption previous
= m_styleOption
;
172 m_styleOption
= option
;
174 styleOptionChanged(option
, previous
);
177 const KItemListStyleOption
& KItemListWidget::styleOption() const
179 return m_styleOption
;
182 void KItemListWidget::setSelected(bool selected
)
184 if (m_selected
!= selected
) {
185 m_selected
= selected
;
186 selectedChanged(selected
);
191 bool KItemListWidget::isSelected() const
196 void KItemListWidget::setCurrent(bool current
)
198 if (m_current
!= current
) {
200 currentChanged(current
);
205 bool KItemListWidget::isCurrent() const
210 void KItemListWidget::setHovered(bool hovered
)
212 if (hovered
== m_hovered
) {
218 if (!m_hoverAnimation
) {
219 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
220 m_hoverAnimation
->setDuration(200);
222 m_hoverAnimation
->stop();
225 m_hoverAnimation
->setEndValue(1.0);
227 m_hoverAnimation
->setEndValue(0.0);
230 m_hoverAnimation
->start();
232 hoveredChanged(hovered
);
237 bool KItemListWidget::isHovered() const
242 bool KItemListWidget::contains(const QPointF
& point
) const
244 if (!QGraphicsWidget::contains(point
)) {
248 return iconBoundingRect().contains(point
) ||
249 textBoundingRect().contains(point
) ||
250 expansionToggleRect().contains(point
) ||
251 selectionToggleRect().contains(point
);
254 QRectF
KItemListWidget::selectionToggleRect() const
259 QRectF
KItemListWidget::expansionToggleRect() const
264 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
265 const QSet
<QByteArray
>& roles
)
272 void KItemListWidget::visibleRolesChanged(const QHash
<QByteArray
, int>& current
,
273 const QHash
<QByteArray
, int>& previous
)
280 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
281 const QHash
<QByteArray
, QSizeF
>& previous
)
288 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
289 const KItemListStyleOption
& previous
)
296 void KItemListWidget::currentChanged(bool current
)
301 void KItemListWidget::selectedChanged(bool selected
)
306 void KItemListWidget::hoveredChanged(bool hovered
)
311 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
313 QGraphicsWidget::resizeEvent(event
);
317 qreal
KItemListWidget::hoverOpacity() const
319 return m_hoverOpacity
;
322 void KItemListWidget::setHoverOpacity(qreal opacity
)
324 m_hoverOpacity
= opacity
;
328 void KItemListWidget::clearCache()
334 void KItemListWidget::drawFocusIndicator(QPainter
* painter
)
336 // Ideally style()->drawPrimitive(QStyle::PE_FrameFocusRect...)
337 // should be used, but Oxygen only draws indicators within classes
338 // derived from QAbstractItemView or Q3ListView. As a workaround
339 // the indicator is drawn manually. Code copied from oxygenstyle.cpp
340 // Copyright ( C ) 2009-2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
341 // TODO: Clarify with Oxygen maintainers how to proceed with this.
343 const KItemListStyleOption
& option
= styleOption();
344 const QPalette palette
= option
.palette
;
345 const QRect rect
= textBoundingRect().toRect().adjusted(0, 0, 0, -1);
347 QLinearGradient
gradient(rect
.bottomLeft(), rect
.bottomRight());
348 gradient
.setColorAt(0.0, Qt::transparent
);
349 gradient
.setColorAt(1.0, Qt::transparent
);
350 gradient
.setColorAt(0.2, palette
.color(QPalette::Text
));
351 gradient
.setColorAt(0.8, palette
.color(QPalette::Text
));
353 painter
->setRenderHint(QPainter::Antialiasing
, false);
354 painter
->setPen(QPen(gradient
, 1));
355 painter
->drawLine(rect
.bottomLeft(), rect
.bottomRight());
356 painter
->setRenderHint(QPainter::Antialiasing
, true);
359 void KItemListWidget::drawTextBackground(QPainter
* painter
)
361 const qreal opacity
= painter
->opacity();
363 QRectF textBounds
= textBoundingRect();
364 const qreal marginDiff
= m_styleOption
.margin
/ 2;
365 textBounds
.adjust(marginDiff
, marginDiff
, -marginDiff
, -marginDiff
);
366 painter
->setOpacity(opacity
* 0.1);
367 painter
->setPen(Qt::NoPen
);
368 painter
->setBrush(m_styleOption
.palette
.text());
369 painter
->drawRoundedRect(textBounds
, 4, 4);
371 painter
->setOpacity(opacity
);
374 #include "kitemlistwidget.moc"