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 <KGlobalSettings>
32 #include <QApplication>
34 #include <QPropertyAnimation>
35 #include <QStyleOption>
37 KItemListWidget::KItemListWidget(QGraphicsItem
* parent
) :
38 QGraphicsWidget(parent
, 0),
43 m_alternatingBackgroundColors(false),
44 m_enabledSelectionToggle(false),
47 m_visibleRolesSizes(),
57 KItemListWidget::~KItemListWidget()
62 void KItemListWidget::setIndex(int index
)
64 if (m_index
!= index
) {
65 delete m_selectionToggle
;
66 m_selectionToggle
= 0;
68 if (m_hoverAnimation
) {
69 m_hoverAnimation
->stop();
78 int KItemListWidget::index() const
83 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
84 const QSet
<QByteArray
>& roles
)
87 if (roles
.isEmpty()) {
91 foreach (const QByteArray
& role
, roles
) {
92 m_data
[role
] = data
[role
];
94 dataChanged(m_data
, roles
);
99 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
104 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
108 painter
->setRenderHint(QPainter::Antialiasing
);
110 if (m_alternatingBackgroundColors
&& (m_index
& 0x1)) {
111 const QColor backgroundColor
= m_styleOption
.palette
.color(QPalette::AlternateBase
);
112 const QRectF
backgroundRect(0, 0, size().width(), size().height());
113 painter
->fillRect(backgroundRect
, backgroundColor
);
117 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
118 drawItemStyleOption(painter
, widget
, activeState
|
119 QStyle::State_Enabled
|
120 QStyle::State_Selected
|
125 QStyleOptionFocusRect focusRectOption
;
126 focusRectOption
.initFrom(widget
);
128 const QRect iconBounds
= iconRect().toRect();
129 const QRect textBounds
= textRect().toRect();
130 if (iconBounds
.bottom() > textBounds
.top()) {
131 focusRectOption
.rect
= textBounds
;
133 // See KItemListWidget::drawItemStyleOption(): The selection rectangle
135 focusRectOption
.rect
= textBounds
.adjusted(1, 1, -1, -1);
138 focusRectOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
| QStyle::State_KeyboardFocusChange
;
140 focusRectOption
.state
|= QStyle::State_Selected
;
143 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &focusRectOption
, painter
, widget
);
146 if (m_hoverOpacity
> 0.0) {
148 // Initialize the m_hoverCache pixmap to improve the drawing performance
149 // when fading the hover background
150 m_hoverCache
= new QPixmap(size().toSize());
151 m_hoverCache
->fill(Qt::transparent
);
153 QPainter
pixmapPainter(m_hoverCache
);
154 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
155 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
156 QStyle::State_Enabled
|
157 QStyle::State_MouseOver
|
161 const qreal opacity
= painter
->opacity();
162 painter
->setOpacity(m_hoverOpacity
* opacity
);
163 painter
->drawPixmap(0, 0, *m_hoverCache
);
164 painter
->setOpacity(opacity
);
168 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
170 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
171 m_visibleRoles
= roles
;
173 visibleRolesChanged(roles
, previousRoles
);
177 QList
<QByteArray
> KItemListWidget::visibleRoles() const
179 return m_visibleRoles
;
182 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
184 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
185 m_visibleRolesSizes
= rolesSizes
;
187 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
191 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
193 return m_visibleRolesSizes
;
196 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
198 const KItemListStyleOption previous
= m_styleOption
;
200 m_styleOption
= option
;
202 styleOptionChanged(option
, previous
);
206 const KItemListStyleOption
& KItemListWidget::styleOption() const
208 return m_styleOption
;
211 void KItemListWidget::setSelected(bool selected
)
213 if (m_selected
!= selected
) {
214 m_selected
= selected
;
215 if (m_selectionToggle
) {
216 m_selectionToggle
->setChecked(selected
);
218 selectedChanged(selected
);
223 bool KItemListWidget::isSelected() const
228 void KItemListWidget::setCurrent(bool current
)
230 if (m_current
!= current
) {
232 currentChanged(current
);
237 bool KItemListWidget::isCurrent() const
242 void KItemListWidget::setHovered(bool hovered
)
244 if (hovered
== m_hovered
) {
250 if (!m_hoverAnimation
) {
251 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
252 const int duration
= (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects
) ? 1 : 200;
253 m_hoverAnimation
->setDuration(duration
);
254 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
256 m_hoverAnimation
->stop();
259 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
260 m_hoverAnimation
->setStartValue(startValue
);
261 m_hoverAnimation
->setEndValue(1.0);
262 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
263 initializeSelectionToggle();
266 m_hoverAnimation
->setStartValue(hoverOpacity());
267 m_hoverAnimation
->setEndValue(0.0);
270 m_hoverAnimation
->start();
272 hoveredChanged(hovered
);
276 bool KItemListWidget::isHovered() const
281 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
283 if (m_alternatingBackgroundColors
!= enable
) {
284 m_alternatingBackgroundColors
= enable
;
285 alternatingBackgroundColorsChanged(enable
);
290 bool KItemListWidget::alternatingBackgroundColors() const
292 return m_alternatingBackgroundColors
;
295 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
297 if (m_enabledSelectionToggle
!= enable
) {
298 m_enabledSelectionToggle
= enable
;
303 bool KItemListWidget::enabledSelectionToggle() const
305 return m_enabledSelectionToggle
;
308 void KItemListWidget::setSiblingsInformation(const QBitArray
& siblings
)
310 const QBitArray previous
= m_siblingsInfo
;
311 m_siblingsInfo
= siblings
;
312 siblingsInformationChanged(m_siblingsInfo
, previous
);
316 QBitArray
KItemListWidget::siblingsInformation() const
318 return m_siblingsInfo
;
321 bool KItemListWidget::contains(const QPointF
& point
) const
323 if (!QGraphicsWidget::contains(point
)) {
327 return iconRect().contains(point
) ||
328 textRect().contains(point
) ||
329 expansionToggleRect().contains(point
) ||
330 selectionToggleRect().contains(point
);
333 QRectF
KItemListWidget::selectionToggleRect() const
338 QRectF
KItemListWidget::expansionToggleRect() const
343 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
344 const QSet
<QByteArray
>& roles
)
350 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
351 const QList
<QByteArray
>& previous
)
357 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
358 const QHash
<QByteArray
, QSizeF
>& previous
)
364 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
365 const KItemListStyleOption
& previous
)
371 void KItemListWidget::currentChanged(bool current
)
376 void KItemListWidget::selectedChanged(bool selected
)
381 void KItemListWidget::hoveredChanged(bool hovered
)
386 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
391 void KItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
397 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
399 QGraphicsWidget::resizeEvent(event
);
403 qreal
KItemListWidget::hoverOpacity() const
405 return m_hoverOpacity
;
408 void KItemListWidget::slotHoverAnimationFinished()
411 delete m_selectionToggle
;
412 m_selectionToggle
= 0;
416 void KItemListWidget::initializeSelectionToggle()
418 Q_ASSERT(m_enabledSelectionToggle
);
420 if (!m_selectionToggle
) {
421 m_selectionToggle
= new KItemListSelectionToggle(this);
424 const QRectF toggleRect
= selectionToggleRect();
425 m_selectionToggle
->setPos(toggleRect
.topLeft());
426 m_selectionToggle
->resize(toggleRect
.size());
428 m_selectionToggle
->setChecked(isSelected());
431 void KItemListWidget::setHoverOpacity(qreal opacity
)
433 m_hoverOpacity
= opacity
;
434 if (m_selectionToggle
) {
435 m_selectionToggle
->setOpacity(opacity
);
438 if (m_hoverOpacity
<= 0.0) {
446 void KItemListWidget::clearHoverCache()
452 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
454 const QRect textBounds
= textRect().toRect();
456 QStyleOptionViewItemV4 viewItemOption
;
457 viewItemOption
.initFrom(widget
);
458 viewItemOption
.state
= styleState
;
459 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
460 viewItemOption
.showDecorationSelected
= true;
461 viewItemOption
.rect
= textBounds
.adjusted(1, 0, -1, 0);
462 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
465 #include "kitemlistwidget.moc"