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 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
;
171 visibleRolesChanged(roles
, previousRoles
);
175 QList
<QByteArray
> KItemListWidget::visibleRoles() const
177 return m_visibleRoles
;
180 void KItemListWidget::setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
)
182 const QHash
<QByteArray
, QSizeF
> previousRolesSizes
= m_visibleRolesSizes
;
183 m_visibleRolesSizes
= rolesSizes
;
185 visibleRolesSizesChanged(rolesSizes
, previousRolesSizes
);
189 QHash
<QByteArray
, QSizeF
> KItemListWidget::visibleRolesSizes() const
191 return m_visibleRolesSizes
;
194 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
196 const KItemListStyleOption previous
= m_styleOption
;
198 m_styleOption
= option
;
200 styleOptionChanged(option
, previous
);
204 const KItemListStyleOption
& KItemListWidget::styleOption() const
206 return m_styleOption
;
209 void KItemListWidget::setSelected(bool selected
)
211 if (m_selected
!= selected
) {
212 m_selected
= selected
;
213 if (m_selectionToggle
) {
214 m_selectionToggle
->setChecked(selected
);
216 selectedChanged(selected
);
221 bool KItemListWidget::isSelected() const
226 void KItemListWidget::setCurrent(bool current
)
228 if (m_current
!= current
) {
230 currentChanged(current
);
235 bool KItemListWidget::isCurrent() const
240 void KItemListWidget::setHovered(bool hovered
)
242 if (hovered
== m_hovered
) {
248 if (!m_hoverAnimation
) {
249 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
250 const int duration
= (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects
) ? 1 : 200;
251 m_hoverAnimation
->setDuration(duration
);
252 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
254 m_hoverAnimation
->stop();
257 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
258 m_hoverAnimation
->setStartValue(startValue
);
259 m_hoverAnimation
->setEndValue(1.0);
260 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
261 initializeSelectionToggle();
264 m_hoverAnimation
->setStartValue(hoverOpacity());
265 m_hoverAnimation
->setEndValue(0.0);
268 m_hoverAnimation
->start();
270 hoveredChanged(hovered
);
274 bool KItemListWidget::isHovered() const
279 void KItemListWidget::setAlternatingBackgroundColors(bool enable
)
281 if (m_alternatingBackgroundColors
!= enable
) {
282 m_alternatingBackgroundColors
= enable
;
283 alternatingBackgroundColorsChanged(enable
);
288 bool KItemListWidget::alternatingBackgroundColors() const
290 return m_alternatingBackgroundColors
;
293 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
295 if (m_enabledSelectionToggle
!= enable
) {
296 m_enabledSelectionToggle
= enable
;
301 bool KItemListWidget::enabledSelectionToggle() const
303 return m_enabledSelectionToggle
;
306 void KItemListWidget::setSiblingsInformation(const QBitArray
& siblings
)
308 const QBitArray previous
= m_siblingsInfo
;
309 m_siblingsInfo
= siblings
;
310 siblingsInformationChanged(m_siblingsInfo
, previous
);
314 QBitArray
KItemListWidget::siblingsInformation() const
316 return m_siblingsInfo
;
319 bool KItemListWidget::contains(const QPointF
& point
) const
321 if (!QGraphicsWidget::contains(point
)) {
325 return iconRect().contains(point
) ||
326 textRect().contains(point
) ||
327 expansionToggleRect().contains(point
) ||
328 selectionToggleRect().contains(point
);
331 QRectF
KItemListWidget::selectionToggleRect() const
336 QRectF
KItemListWidget::expansionToggleRect() const
341 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
342 const QSet
<QByteArray
>& roles
)
348 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
349 const QList
<QByteArray
>& previous
)
355 void KItemListWidget::visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
,
356 const QHash
<QByteArray
, QSizeF
>& previous
)
362 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
363 const KItemListStyleOption
& previous
)
369 void KItemListWidget::currentChanged(bool current
)
374 void KItemListWidget::selectedChanged(bool selected
)
379 void KItemListWidget::hoveredChanged(bool hovered
)
384 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled
)
389 void KItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
395 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
397 QGraphicsWidget::resizeEvent(event
);
401 qreal
KItemListWidget::hoverOpacity() const
403 return m_hoverOpacity
;
406 void KItemListWidget::slotHoverAnimationFinished()
409 delete m_selectionToggle
;
410 m_selectionToggle
= 0;
414 void KItemListWidget::initializeSelectionToggle()
416 Q_ASSERT(m_enabledSelectionToggle
);
418 if (!m_selectionToggle
) {
419 m_selectionToggle
= new KItemListSelectionToggle(this);
422 const QRectF toggleRect
= selectionToggleRect();
423 m_selectionToggle
->setPos(toggleRect
.topLeft());
424 m_selectionToggle
->resize(toggleRect
.size());
426 m_selectionToggle
->setChecked(isSelected());
429 void KItemListWidget::setHoverOpacity(qreal opacity
)
431 m_hoverOpacity
= opacity
;
432 if (m_selectionToggle
) {
433 m_selectionToggle
->setOpacity(opacity
);
436 if (m_hoverOpacity
<= 0.0) {
444 void KItemListWidget::clearHoverCache()
450 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
452 const QRect textBounds
= textRect().toRect();
454 QStyleOptionViewItemV4 viewItemOption
;
455 viewItemOption
.initFrom(widget
);
456 viewItemOption
.state
= styleState
;
457 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
458 viewItemOption
.showDecorationSelected
= true;
459 viewItemOption
.rect
= textBounds
.adjusted(1, 0, -1, 0);
460 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
463 #include "kitemlistwidget.moc"