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 "kitemlistselectiontoggle_p.h"
26 #include "kitemlistview.h"
27 #include "kitemmodelbase.h"
31 #include <QApplication>
33 #include <QPropertyAnimation>
34 #include <QStyleOption>
36 KItemListWidget::KItemListWidget(QGraphicsItem
* parent
) :
37 QGraphicsWidget(parent
, 0),
42 m_alternatingBackgroundColors(false),
43 m_enabledSelectionToggle(false),
46 m_visibleRolesSizes(),
55 KItemListWidget::~KItemListWidget()
60 void KItemListWidget::setIndex(int index
)
62 if (m_index
!= index
) {
63 delete m_selectionToggle
;
64 m_selectionToggle
= 0;
66 if (m_hoverAnimation
) {
67 m_hoverAnimation
->stop();
76 int KItemListWidget::index() const
81 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
82 const QSet
<QByteArray
>& roles
)
85 if (roles
.isEmpty()) {
89 foreach (const QByteArray
& role
, roles
) {
90 m_data
[role
] = data
[role
];
92 dataChanged(m_data
, roles
);
96 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
101 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
105 painter
->setRenderHint(QPainter::Antialiasing
);
107 if (m_alternatingBackgroundColors
&& (m_index
& 0x1)) {
108 const QColor backgroundColor
= m_styleOption
.palette
.color(QPalette::AlternateBase
);
109 const QRectF
backgroundRect(0, 0, size().width(), size().height());
110 painter
->fillRect(backgroundRect
, backgroundColor
);
114 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
115 drawItemStyleOption(painter
, widget
, activeState
|
116 QStyle::State_Enabled
|
117 QStyle::State_Selected
|
122 QStyleOptionViewItemV4 viewItemOption
;
123 viewItemOption
.initFrom(widget
);
124 viewItemOption
.rect
= textRect().toRect();
125 viewItemOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
;
126 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
127 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &viewItemOption
, painter
, widget
);
130 if (m_hoverOpacity
> 0.0) {
132 // Initialize the m_hoverCache pixmap to improve the drawing performance
133 // when fading the hover background
134 m_hoverCache
= new QPixmap(size().toSize());
135 m_hoverCache
->fill(Qt::transparent
);
137 QPainter
pixmapPainter(m_hoverCache
);
138 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
139 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
140 QStyle::State_Enabled
|
141 QStyle::State_MouseOver
|
145 const qreal opacity
= painter
->opacity();
146 painter
->setOpacity(m_hoverOpacity
* opacity
);
147 painter
->drawPixmap(0, 0, *m_hoverCache
);
148 painter
->setOpacity(opacity
);
152 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
154 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
155 m_visibleRoles
= roles
;
156 visibleRolesChanged(roles
, previousRoles
);
159 QList
<QByteArray
> KItemListWidget::visibleRoles() const
161 return m_visibleRoles
;
164 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
166 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
167 m_visibleRolesSizes
= rolesSizes
;
168 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
171 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
173 return m_visibleRolesSizes
;
176 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
178 const KItemListStyleOption previous
= m_styleOption
;
180 m_styleOption
= option
;
182 styleOptionChanged(option
, previous
);
185 const KItemListStyleOption
& KItemListWidget::styleOption() const
187 return m_styleOption
;
190 void KItemListWidget::setSelected(bool selected
)
192 if (m_selected
!= selected
) {
193 m_selected
= selected
;
194 if (m_selectionToggle
) {
195 m_selectionToggle
->setChecked(selected
);
198 selectedChanged(selected
);
203 bool KItemListWidget::isSelected() const
208 void KItemListWidget::setCurrent(bool current
)
210 if (m_current
!= current
) {
213 currentChanged(current
);
218 bool KItemListWidget::isCurrent() const
223 void KItemListWidget::setHovered(bool hovered
)
225 if (hovered
== m_hovered
) {
231 if (!m_hoverAnimation
) {
232 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
233 m_hoverAnimation
->setDuration(200);
234 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
236 m_hoverAnimation
->stop();
239 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
240 m_hoverAnimation
->setStartValue(startValue
);
241 m_hoverAnimation
->setEndValue(1.0);
242 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
243 initializeSelectionToggle();
246 m_hoverAnimation
->setStartValue(hoverOpacity());
247 m_hoverAnimation
->setEndValue(0.0);
250 m_hoverAnimation
->start();
252 hoveredChanged(hovered
);
257 bool KItemListWidget::isHovered() const
262 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
264 if (m_alternatingBackgroundColors
!= enable
) {
265 m_alternatingBackgroundColors
= enable
;
266 alternatingBackgroundColorsChanged(enable
);
271 bool KItemListWidget::alternatingBackgroundColors() const
273 return m_alternatingBackgroundColors
;
276 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
278 if (m_enabledSelectionToggle
!= enable
) {
279 m_enabledSelectionToggle
= enable
;
284 bool KItemListWidget::enabledSelectionToggle() const
286 return m_enabledSelectionToggle
;
289 bool KItemListWidget::contains(const QPointF
& point
) const
291 if (!QGraphicsWidget::contains(point
)) {
295 return iconRect().contains(point
) ||
296 textRect().contains(point
) ||
297 expansionToggleRect().contains(point
) ||
298 selectionToggleRect().contains(point
);
301 QRectF
KItemListWidget::selectionToggleRect() const
306 QRectF
KItemListWidget::expansionToggleRect() const
311 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
312 const QSet
<QByteArray
>& roles
)
319 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
320 const QList
<QByteArray
>& previous
)
327 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
328 const QHash
<QByteArray
, QSizeF
>& previous
)
335 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
336 const KItemListStyleOption
& previous
)
343 void KItemListWidget::currentChanged(bool current
)
348 void KItemListWidget::selectedChanged(bool selected
)
353 void KItemListWidget::hoveredChanged(bool hovered
)
358 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
363 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
365 QGraphicsWidget::resizeEvent(event
);
369 qreal
KItemListWidget::hoverOpacity() const
371 return m_hoverOpacity
;
374 void KItemListWidget::slotHoverAnimationFinished()
377 delete m_selectionToggle
;
378 m_selectionToggle
= 0;
382 void KItemListWidget::initializeSelectionToggle()
384 Q_ASSERT(m_enabledSelectionToggle
);
386 if (!m_selectionToggle
) {
387 m_selectionToggle
= new KItemListSelectionToggle(this);
390 const QRectF toggleRect
= selectionToggleRect();
391 m_selectionToggle
->setPos(toggleRect
.topLeft());
392 m_selectionToggle
->resize(toggleRect
.size());
394 m_selectionToggle
->setChecked(isSelected());
397 void KItemListWidget::setHoverOpacity(qreal opacity
)
399 m_hoverOpacity
= opacity
;
400 if (m_selectionToggle
) {
401 m_selectionToggle
->setOpacity(opacity
);
404 if (m_hoverOpacity
<= 0.0) {
412 void KItemListWidget::clearHoverCache()
418 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
420 const QRect iconBounds
= iconRect().toRect();
421 const QRect textBounds
= textRect().toRect();
423 QStyleOptionViewItemV4 viewItemOption
;
424 viewItemOption
.initFrom(widget
);
425 viewItemOption
.state
= styleState
;
426 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
428 const bool drawMerged
= (iconBounds
.top() == textBounds
.top() &&
429 iconBounds
.bottom() == textBounds
.bottom());
432 viewItemOption
.rect
= iconBounds
| textBounds
;
433 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
435 viewItemOption
.rect
= iconBounds
;
436 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
438 viewItemOption
.rect
= textBounds
.adjusted(2, 2, -2, -2);
439 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
443 #include "kitemlistwidget.moc"