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 drawItemStyleOption(painter
, widget
, QStyle::State_Enabled
|
115 QStyle::State_Selected
|
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) {
130 // Initialize the m_hoverCache pixmap to improve the drawing performance
131 // when fading the hover background
132 m_hoverCache
= new QPixmap(size().toSize());
133 m_hoverCache
->fill(Qt::transparent
);
135 QPainter
pixmapPainter(m_hoverCache
);
136 drawItemStyleOption(&pixmapPainter
, widget
, QStyle::State_Enabled
|
137 QStyle::State_MouseOver
|
141 const qreal opacity
= painter
->opacity();
142 painter
->setOpacity(m_hoverOpacity
* opacity
);
143 painter
->drawPixmap(0, 0, *m_hoverCache
);
144 painter
->setOpacity(opacity
);
148 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
150 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
151 m_visibleRoles
= roles
;
152 visibleRolesChanged(roles
, previousRoles
);
155 QList
<QByteArray
> KItemListWidget::visibleRoles() const
157 return m_visibleRoles
;
160 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
162 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
163 m_visibleRolesSizes
= rolesSizes
;
164 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
167 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
169 return m_visibleRolesSizes
;
172 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
174 const KItemListStyleOption previous
= m_styleOption
;
176 m_styleOption
= option
;
178 styleOptionChanged(option
, previous
);
181 const KItemListStyleOption
& KItemListWidget::styleOption() const
183 return m_styleOption
;
186 void KItemListWidget::setSelected(bool selected
)
188 if (m_selected
!= selected
) {
189 m_selected
= selected
;
190 if (m_selectionToggle
) {
191 m_selectionToggle
->setChecked(selected
);
194 selectedChanged(selected
);
199 bool KItemListWidget::isSelected() const
204 void KItemListWidget::setCurrent(bool current
)
206 if (m_current
!= current
) {
209 currentChanged(current
);
214 bool KItemListWidget::isCurrent() const
219 void KItemListWidget::setHovered(bool hovered
)
221 if (hovered
== m_hovered
) {
227 if (!m_hoverAnimation
) {
228 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
229 m_hoverAnimation
->setDuration(200);
230 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
232 m_hoverAnimation
->stop();
235 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
236 m_hoverAnimation
->setStartValue(startValue
);
237 m_hoverAnimation
->setEndValue(1.0);
238 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
239 initializeSelectionToggle();
242 m_hoverAnimation
->setStartValue(hoverOpacity());
243 m_hoverAnimation
->setEndValue(0.0);
246 m_hoverAnimation
->start();
248 hoveredChanged(hovered
);
253 bool KItemListWidget::isHovered() const
258 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
260 if (m_alternatingBackgroundColors
!= enable
) {
261 m_alternatingBackgroundColors
= enable
;
262 alternatingBackgroundColorsChanged(enable
);
267 bool KItemListWidget::alternatingBackgroundColors() const
269 return m_alternatingBackgroundColors
;
272 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
274 if (m_enabledSelectionToggle
!= enable
) {
275 m_enabledSelectionToggle
= enable
;
280 bool KItemListWidget::enabledSelectionToggle() const
282 return m_enabledSelectionToggle
;
285 bool KItemListWidget::contains(const QPointF
& point
) const
287 if (!QGraphicsWidget::contains(point
)) {
291 return iconRect().contains(point
) ||
292 textRect().contains(point
) ||
293 expansionToggleRect().contains(point
) ||
294 selectionToggleRect().contains(point
);
297 QRectF
KItemListWidget::selectionToggleRect() const
302 QRectF
KItemListWidget::expansionToggleRect() const
307 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
308 const QSet
<QByteArray
>& roles
)
315 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
316 const QList
<QByteArray
>& previous
)
323 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
324 const QHash
<QByteArray
, QSizeF
>& previous
)
331 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
332 const KItemListStyleOption
& previous
)
339 void KItemListWidget::currentChanged(bool current
)
344 void KItemListWidget::selectedChanged(bool selected
)
349 void KItemListWidget::hoveredChanged(bool hovered
)
354 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
359 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
361 QGraphicsWidget::resizeEvent(event
);
365 qreal
KItemListWidget::hoverOpacity() const
367 return m_hoverOpacity
;
370 void KItemListWidget::slotHoverAnimationFinished()
373 delete m_selectionToggle
;
374 m_selectionToggle
= 0;
378 void KItemListWidget::initializeSelectionToggle()
380 Q_ASSERT(m_enabledSelectionToggle
);
382 if (!m_selectionToggle
) {
383 m_selectionToggle
= new KItemListSelectionToggle(this);
386 const QRectF toggleRect
= selectionToggleRect();
387 m_selectionToggle
->setPos(toggleRect
.topLeft());
388 m_selectionToggle
->resize(toggleRect
.size());
390 m_selectionToggle
->setChecked(isSelected());
393 void KItemListWidget::setHoverOpacity(qreal opacity
)
395 m_hoverOpacity
= opacity
;
396 if (m_selectionToggle
) {
397 m_selectionToggle
->setOpacity(opacity
);
400 if (m_hoverOpacity
<= 0.0) {
408 void KItemListWidget::clearHoverCache()
414 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
416 const QRect iconBounds
= iconRect().toRect();
417 const QRect textBounds
= textRect().toRect();
419 QStyleOptionViewItemV4 viewItemOption
;
420 viewItemOption
.initFrom(widget
);
421 viewItemOption
.state
= styleState
;
422 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
424 const bool drawMerged
= (iconBounds
.top() == textBounds
.top() &&
425 iconBounds
.bottom() == textBounds
.bottom());
428 viewItemOption
.rect
= iconBounds
| textBounds
;
429 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
431 viewItemOption
.rect
= iconBounds
;
432 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
434 viewItemOption
.rect
= textBounds
.adjusted(2, 2, -2, -2);
435 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
439 #include "kitemlistwidget.moc"