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
;
127 viewItemOption
.state
|= QStyle::State_Selected
;
129 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
130 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &viewItemOption
, painter
, widget
);
133 if (m_hoverOpacity
> 0.0) {
135 // Initialize the m_hoverCache pixmap to improve the drawing performance
136 // when fading the hover background
137 m_hoverCache
= new QPixmap(size().toSize());
138 m_hoverCache
->fill(Qt::transparent
);
140 QPainter
pixmapPainter(m_hoverCache
);
141 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
142 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
143 QStyle::State_Enabled
|
144 QStyle::State_MouseOver
|
148 const qreal opacity
= painter
->opacity();
149 painter
->setOpacity(m_hoverOpacity
* opacity
);
150 painter
->drawPixmap(0, 0, *m_hoverCache
);
151 painter
->setOpacity(opacity
);
155 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
157 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
158 m_visibleRoles
= roles
;
159 visibleRolesChanged(roles
, previousRoles
);
162 QList
<QByteArray
> KItemListWidget::visibleRoles() const
164 return m_visibleRoles
;
167 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
169 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
170 m_visibleRolesSizes
= rolesSizes
;
171 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
174 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
176 return m_visibleRolesSizes
;
179 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
181 const KItemListStyleOption previous
= m_styleOption
;
183 m_styleOption
= option
;
185 styleOptionChanged(option
, previous
);
188 const KItemListStyleOption
& KItemListWidget::styleOption() const
190 return m_styleOption
;
193 void KItemListWidget::setSelected(bool selected
)
195 if (m_selected
!= selected
) {
196 m_selected
= selected
;
197 if (m_selectionToggle
) {
198 m_selectionToggle
->setChecked(selected
);
201 selectedChanged(selected
);
206 bool KItemListWidget::isSelected() const
211 void KItemListWidget::setCurrent(bool current
)
213 if (m_current
!= current
) {
216 currentChanged(current
);
221 bool KItemListWidget::isCurrent() const
226 void KItemListWidget::setHovered(bool hovered
)
228 if (hovered
== m_hovered
) {
234 if (!m_hoverAnimation
) {
235 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
236 m_hoverAnimation
->setDuration(200);
237 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
239 m_hoverAnimation
->stop();
242 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
243 m_hoverAnimation
->setStartValue(startValue
);
244 m_hoverAnimation
->setEndValue(1.0);
245 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
246 initializeSelectionToggle();
249 m_hoverAnimation
->setStartValue(hoverOpacity());
250 m_hoverAnimation
->setEndValue(0.0);
253 m_hoverAnimation
->start();
255 hoveredChanged(hovered
);
260 bool KItemListWidget::isHovered() const
265 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
267 if (m_alternatingBackgroundColors
!= enable
) {
268 m_alternatingBackgroundColors
= enable
;
269 alternatingBackgroundColorsChanged(enable
);
274 bool KItemListWidget::alternatingBackgroundColors() const
276 return m_alternatingBackgroundColors
;
279 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
281 if (m_enabledSelectionToggle
!= enable
) {
282 m_enabledSelectionToggle
= enable
;
287 bool KItemListWidget::enabledSelectionToggle() const
289 return m_enabledSelectionToggle
;
292 bool KItemListWidget::contains(const QPointF
& point
) const
294 if (!QGraphicsWidget::contains(point
)) {
298 return iconRect().contains(point
) ||
299 textRect().contains(point
) ||
300 expansionToggleRect().contains(point
) ||
301 selectionToggleRect().contains(point
);
304 QRectF
KItemListWidget::selectionToggleRect() const
309 QRectF
KItemListWidget::expansionToggleRect() const
314 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
315 const QSet
<QByteArray
>& roles
)
322 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
323 const QList
<QByteArray
>& previous
)
330 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
331 const QHash
<QByteArray
, QSizeF
>& previous
)
338 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
339 const KItemListStyleOption
& previous
)
346 void KItemListWidget::currentChanged(bool current
)
351 void KItemListWidget::selectedChanged(bool selected
)
356 void KItemListWidget::hoveredChanged(bool hovered
)
361 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
366 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
368 QGraphicsWidget::resizeEvent(event
);
372 qreal
KItemListWidget::hoverOpacity() const
374 return m_hoverOpacity
;
377 void KItemListWidget::slotHoverAnimationFinished()
380 delete m_selectionToggle
;
381 m_selectionToggle
= 0;
385 void KItemListWidget::initializeSelectionToggle()
387 Q_ASSERT(m_enabledSelectionToggle
);
389 if (!m_selectionToggle
) {
390 m_selectionToggle
= new KItemListSelectionToggle(this);
393 const QRectF toggleRect
= selectionToggleRect();
394 m_selectionToggle
->setPos(toggleRect
.topLeft());
395 m_selectionToggle
->resize(toggleRect
.size());
397 m_selectionToggle
->setChecked(isSelected());
400 void KItemListWidget::setHoverOpacity(qreal opacity
)
402 m_hoverOpacity
= opacity
;
403 if (m_selectionToggle
) {
404 m_selectionToggle
->setOpacity(opacity
);
407 if (m_hoverOpacity
<= 0.0) {
415 void KItemListWidget::clearHoverCache()
421 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
423 const QRect iconBounds
= iconRect().toRect();
424 const QRect textBounds
= textRect().toRect();
426 QStyleOptionViewItemV4 viewItemOption
;
427 viewItemOption
.initFrom(widget
);
428 viewItemOption
.state
= styleState
;
429 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
431 const bool drawMerged
= (iconBounds
.top() == textBounds
.top() &&
432 iconBounds
.bottom() == textBounds
.bottom());
435 viewItemOption
.rect
= iconBounds
| textBounds
;
436 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
438 viewItemOption
.rect
= iconBounds
;
439 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
441 viewItemOption
.rect
= textBounds
.adjusted(2, 2, -2, -2);
442 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
446 #include "kitemlistwidget.moc"