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 QStyleOptionFocusRect focusRectOption
;
123 focusRectOption
.initFrom(widget
);
125 const QRect iconBounds
= iconRect().toRect();
126 const QRect textBounds
= textRect().toRect();
127 if (iconBounds
.bottom() > textBounds
.top()) {
128 focusRectOption
.rect
= textBounds
;
130 // See KItemListWidget::drawItemStyleOption(): The selection rectangle
132 focusRectOption
.rect
= textBounds
.adjusted(1, 1, -1, -1);
135 focusRectOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
| QStyle::State_KeyboardFocusChange
;
137 focusRectOption
.state
|= QStyle::State_Selected
;
140 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &focusRectOption
, painter
, widget
);
143 if (m_hoverOpacity
> 0.0) {
145 // Initialize the m_hoverCache pixmap to improve the drawing performance
146 // when fading the hover background
147 m_hoverCache
= new QPixmap(size().toSize());
148 m_hoverCache
->fill(Qt::transparent
);
150 QPainter
pixmapPainter(m_hoverCache
);
151 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
152 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
153 QStyle::State_Enabled
|
154 QStyle::State_MouseOver
|
158 const qreal opacity
= painter
->opacity();
159 painter
->setOpacity(m_hoverOpacity
* opacity
);
160 painter
->drawPixmap(0, 0, *m_hoverCache
);
161 painter
->setOpacity(opacity
);
165 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
167 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
168 m_visibleRoles
= roles
;
169 visibleRolesChanged(roles
, previousRoles
);
172 QList
<QByteArray
> KItemListWidget::visibleRoles() const
174 return m_visibleRoles
;
177 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
179 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
180 m_visibleRolesSizes
= rolesSizes
;
181 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
184 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
186 return m_visibleRolesSizes
;
189 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
191 const KItemListStyleOption previous
= m_styleOption
;
193 m_styleOption
= option
;
195 styleOptionChanged(option
, previous
);
198 const KItemListStyleOption
& KItemListWidget::styleOption() const
200 return m_styleOption
;
203 void KItemListWidget::setSelected(bool selected
)
205 if (m_selected
!= selected
) {
206 m_selected
= selected
;
207 if (m_selectionToggle
) {
208 m_selectionToggle
->setChecked(selected
);
211 selectedChanged(selected
);
216 bool KItemListWidget::isSelected() const
221 void KItemListWidget::setCurrent(bool current
)
223 if (m_current
!= current
) {
226 currentChanged(current
);
231 bool KItemListWidget::isCurrent() const
236 void KItemListWidget::setHovered(bool hovered
)
238 if (hovered
== m_hovered
) {
244 if (!m_hoverAnimation
) {
245 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
246 m_hoverAnimation
->setDuration(200);
247 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
249 m_hoverAnimation
->stop();
252 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
253 m_hoverAnimation
->setStartValue(startValue
);
254 m_hoverAnimation
->setEndValue(1.0);
255 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
256 initializeSelectionToggle();
259 m_hoverAnimation
->setStartValue(hoverOpacity());
260 m_hoverAnimation
->setEndValue(0.0);
263 m_hoverAnimation
->start();
265 hoveredChanged(hovered
);
270 bool KItemListWidget::isHovered() const
275 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
277 if (m_alternatingBackgroundColors
!= enable
) {
278 m_alternatingBackgroundColors
= enable
;
279 alternatingBackgroundColorsChanged(enable
);
284 bool KItemListWidget::alternatingBackgroundColors() const
286 return m_alternatingBackgroundColors
;
289 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
291 if (m_enabledSelectionToggle
!= enable
) {
292 m_enabledSelectionToggle
= enable
;
297 bool KItemListWidget::enabledSelectionToggle() const
299 return m_enabledSelectionToggle
;
302 bool KItemListWidget::contains(const QPointF
& point
) const
304 if (!QGraphicsWidget::contains(point
)) {
308 return iconRect().contains(point
) ||
309 textRect().contains(point
) ||
310 expansionToggleRect().contains(point
) ||
311 selectionToggleRect().contains(point
);
314 QRectF
KItemListWidget::selectionToggleRect() const
319 QRectF
KItemListWidget::expansionToggleRect() const
324 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
325 const QSet
<QByteArray
>& roles
)
332 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
333 const QList
<QByteArray
>& previous
)
340 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
341 const QHash
<QByteArray
, QSizeF
>& previous
)
348 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
349 const KItemListStyleOption
& previous
)
356 void KItemListWidget::currentChanged(bool current
)
361 void KItemListWidget::selectedChanged(bool selected
)
366 void KItemListWidget::hoveredChanged(bool hovered
)
371 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
376 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
378 QGraphicsWidget::resizeEvent(event
);
382 qreal
KItemListWidget::hoverOpacity() const
384 return m_hoverOpacity
;
387 void KItemListWidget::slotHoverAnimationFinished()
390 delete m_selectionToggle
;
391 m_selectionToggle
= 0;
395 void KItemListWidget::initializeSelectionToggle()
397 Q_ASSERT(m_enabledSelectionToggle
);
399 if (!m_selectionToggle
) {
400 m_selectionToggle
= new KItemListSelectionToggle(this);
403 const QRectF toggleRect
= selectionToggleRect();
404 m_selectionToggle
->setPos(toggleRect
.topLeft());
405 m_selectionToggle
->resize(toggleRect
.size());
407 m_selectionToggle
->setChecked(isSelected());
410 void KItemListWidget::setHoverOpacity(qreal opacity
)
412 m_hoverOpacity
= opacity
;
413 if (m_selectionToggle
) {
414 m_selectionToggle
->setOpacity(opacity
);
417 if (m_hoverOpacity
<= 0.0) {
425 void KItemListWidget::clearHoverCache()
431 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
433 const QRect iconBounds
= iconRect().toRect();
434 const QRect textBounds
= textRect().toRect();
436 QStyleOptionViewItemV4 viewItemOption
;
437 viewItemOption
.initFrom(widget
);
438 viewItemOption
.state
= styleState
;
439 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
440 viewItemOption
.showDecorationSelected
= true;
442 if (iconBounds
.bottom() > textBounds
.top()) {
443 viewItemOption
.rect
= iconBounds
| textBounds
;
444 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
446 viewItemOption
.rect
= iconBounds
;
447 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
449 viewItemOption
.rect
= textBounds
.adjusted(1, 1, -1, -1);
450 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
454 #include "kitemlistwidget.moc"