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(),
56 KItemListWidget::~KItemListWidget()
61 void KItemListWidget::setIndex(int index
)
63 if (m_index
!= index
) {
64 delete m_selectionToggle
;
65 m_selectionToggle
= 0;
67 if (m_hoverAnimation
) {
68 m_hoverAnimation
->stop();
77 int KItemListWidget::index() const
82 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
83 const QSet
<QByteArray
>& roles
)
86 if (roles
.isEmpty()) {
90 foreach (const QByteArray
& role
, roles
) {
91 m_data
[role
] = data
[role
];
93 dataChanged(m_data
, roles
);
97 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
102 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
106 painter
->setRenderHint(QPainter::Antialiasing
);
108 if (m_alternatingBackgroundColors
&& (m_index
& 0x1)) {
109 const QColor backgroundColor
= m_styleOption
.palette
.color(QPalette::AlternateBase
);
110 const QRectF
backgroundRect(0, 0, size().width(), size().height());
111 painter
->fillRect(backgroundRect
, backgroundColor
);
115 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
116 drawItemStyleOption(painter
, widget
, activeState
|
117 QStyle::State_Enabled
|
118 QStyle::State_Selected
|
123 QStyleOptionFocusRect focusRectOption
;
124 focusRectOption
.initFrom(widget
);
126 const QRect iconBounds
= iconRect().toRect();
127 const QRect textBounds
= textRect().toRect();
128 if (iconBounds
.bottom() > textBounds
.top()) {
129 focusRectOption
.rect
= textBounds
;
131 // See KItemListWidget::drawItemStyleOption(): The selection rectangle
133 focusRectOption
.rect
= textBounds
.adjusted(1, 1, -1, -1);
136 focusRectOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
| QStyle::State_KeyboardFocusChange
;
138 focusRectOption
.state
|= QStyle::State_Selected
;
141 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &focusRectOption
, painter
, widget
);
144 if (m_hoverOpacity
> 0.0) {
146 // Initialize the m_hoverCache pixmap to improve the drawing performance
147 // when fading the hover background
148 m_hoverCache
= new QPixmap(size().toSize());
149 m_hoverCache
->fill(Qt::transparent
);
151 QPainter
pixmapPainter(m_hoverCache
);
152 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
153 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
154 QStyle::State_Enabled
|
155 QStyle::State_MouseOver
|
159 const qreal opacity
= painter
->opacity();
160 painter
->setOpacity(m_hoverOpacity
* opacity
);
161 painter
->drawPixmap(0, 0, *m_hoverCache
);
162 painter
->setOpacity(opacity
);
166 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
168 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
169 m_visibleRoles
= roles
;
170 visibleRolesChanged(roles
, previousRoles
);
173 QList
<QByteArray
> KItemListWidget::visibleRoles() const
175 return m_visibleRoles
;
178 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
180 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
181 m_visibleRolesSizes
= rolesSizes
;
182 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
185 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
187 return m_visibleRolesSizes
;
190 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
192 const KItemListStyleOption previous
= m_styleOption
;
194 m_styleOption
= option
;
196 styleOptionChanged(option
, previous
);
199 const KItemListStyleOption
& KItemListWidget::styleOption() const
201 return m_styleOption
;
204 void KItemListWidget::setSelected(bool selected
)
206 if (m_selected
!= selected
) {
207 m_selected
= selected
;
208 if (m_selectionToggle
) {
209 m_selectionToggle
->setChecked(selected
);
212 selectedChanged(selected
);
217 bool KItemListWidget::isSelected() const
222 void KItemListWidget::setCurrent(bool current
)
224 if (m_current
!= current
) {
227 currentChanged(current
);
232 bool KItemListWidget::isCurrent() const
237 void KItemListWidget::setHovered(bool hovered
)
239 if (hovered
== m_hovered
) {
245 if (!m_hoverAnimation
) {
246 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
247 const int duration
= (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects
) ? 1 : 200;
248 m_hoverAnimation
->setDuration(duration
);
249 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
251 m_hoverAnimation
->stop();
254 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
255 m_hoverAnimation
->setStartValue(startValue
);
256 m_hoverAnimation
->setEndValue(1.0);
257 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
258 initializeSelectionToggle();
261 m_hoverAnimation
->setStartValue(hoverOpacity());
262 m_hoverAnimation
->setEndValue(0.0);
265 m_hoverAnimation
->start();
267 hoveredChanged(hovered
);
272 bool KItemListWidget::isHovered() const
277 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
279 if (m_alternatingBackgroundColors
!= enable
) {
280 m_alternatingBackgroundColors
= enable
;
281 alternatingBackgroundColorsChanged(enable
);
286 bool KItemListWidget::alternatingBackgroundColors() const
288 return m_alternatingBackgroundColors
;
291 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
293 if (m_enabledSelectionToggle
!= enable
) {
294 m_enabledSelectionToggle
= enable
;
299 bool KItemListWidget::enabledSelectionToggle() const
301 return m_enabledSelectionToggle
;
304 bool KItemListWidget::contains(const QPointF
& point
) const
306 if (!QGraphicsWidget::contains(point
)) {
310 return iconRect().contains(point
) ||
311 textRect().contains(point
) ||
312 expansionToggleRect().contains(point
) ||
313 selectionToggleRect().contains(point
);
316 QRectF
KItemListWidget::selectionToggleRect() const
321 QRectF
KItemListWidget::expansionToggleRect() const
326 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
327 const QSet
<QByteArray
>& roles
)
334 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
335 const QList
<QByteArray
>& previous
)
342 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
343 const QHash
<QByteArray
, QSizeF
>& previous
)
350 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
351 const KItemListStyleOption
& previous
)
358 void KItemListWidget::currentChanged(bool current
)
363 void KItemListWidget::selectedChanged(bool selected
)
368 void KItemListWidget::hoveredChanged(bool hovered
)
373 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
378 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
380 QGraphicsWidget::resizeEvent(event
);
384 qreal
KItemListWidget::hoverOpacity() const
386 return m_hoverOpacity
;
389 void KItemListWidget::slotHoverAnimationFinished()
392 delete m_selectionToggle
;
393 m_selectionToggle
= 0;
397 void KItemListWidget::initializeSelectionToggle()
399 Q_ASSERT(m_enabledSelectionToggle
);
401 if (!m_selectionToggle
) {
402 m_selectionToggle
= new KItemListSelectionToggle(this);
405 const QRectF toggleRect
= selectionToggleRect();
406 m_selectionToggle
->setPos(toggleRect
.topLeft());
407 m_selectionToggle
->resize(toggleRect
.size());
409 m_selectionToggle
->setChecked(isSelected());
412 void KItemListWidget::setHoverOpacity(qreal opacity
)
414 m_hoverOpacity
= opacity
;
415 if (m_selectionToggle
) {
416 m_selectionToggle
->setOpacity(opacity
);
419 if (m_hoverOpacity
<= 0.0) {
427 void KItemListWidget::clearHoverCache()
433 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
435 const QRect textBounds
= textRect().toRect();
437 QStyleOptionViewItemV4 viewItemOption
;
438 viewItemOption
.initFrom(widget
);
439 viewItemOption
.state
= styleState
;
440 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
441 viewItemOption
.showDecorationSelected
= true;
442 viewItemOption
.rect
= textBounds
;
443 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
446 #include "kitemlistwidget.moc"