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 "kitemlistview.h"
26 #include "kitemmodelbase.h"
28 #include "private/kitemlistselectiontoggle.h"
32 #include <KGlobalSettings>
33 #include <QApplication>
35 #include <QPropertyAnimation>
36 #include <QStyleOption>
38 KItemListWidget::KItemListWidget(QGraphicsItem
* parent
) :
39 QGraphicsWidget(parent
, 0),
44 m_alternateBackground(false),
45 m_enabledSelectionToggle(false),
58 KItemListWidget::~KItemListWidget()
63 void KItemListWidget::setIndex(int index
)
65 if (m_index
!= index
) {
66 delete m_selectionToggle
;
67 m_selectionToggle
= 0;
69 if (m_hoverAnimation
) {
70 m_hoverAnimation
->stop();
79 int KItemListWidget::index() const
84 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
85 const QSet
<QByteArray
>& roles
)
88 if (roles
.isEmpty()) {
92 foreach (const QByteArray
& role
, roles
) {
93 m_data
[role
] = data
[role
];
95 dataChanged(m_data
, roles
);
100 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
105 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
109 if (m_alternateBackground
) {
110 const QColor backgroundColor
= m_styleOption
.palette
.color(QPalette::AlternateBase
);
111 const QRectF
backgroundRect(0, 0, size().width(), size().height());
112 painter
->fillRect(backgroundRect
, backgroundColor
);
116 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
117 drawItemStyleOption(painter
, widget
, activeState
|
118 QStyle::State_Enabled
|
119 QStyle::State_Selected
|
124 QStyleOptionFocusRect focusRectOption
;
125 focusRectOption
.initFrom(widget
);
126 focusRectOption
.rect
= textFocusRect().toRect();
127 focusRectOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
| QStyle::State_KeyboardFocusChange
;
129 focusRectOption
.state
|= QStyle::State_Selected
;
132 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &focusRectOption
, painter
, widget
);
135 if (m_hoverOpacity
> 0.0) {
137 // Initialize the m_hoverCache pixmap to improve the drawing performance
138 // when fading the hover background
139 m_hoverCache
= new QPixmap(size().toSize());
140 m_hoverCache
->fill(Qt::transparent
);
142 QPainter
pixmapPainter(m_hoverCache
);
143 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
144 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
145 QStyle::State_Enabled
|
146 QStyle::State_MouseOver
|
150 const qreal opacity
= painter
->opacity();
151 painter
->setOpacity(m_hoverOpacity
* opacity
);
152 painter
->drawPixmap(0, 0, *m_hoverCache
);
153 painter
->setOpacity(opacity
);
157 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
159 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
160 m_visibleRoles
= roles
;
162 visibleRolesChanged(roles
, previousRoles
);
166 QList
<QByteArray
> KItemListWidget::visibleRoles() const
168 return m_visibleRoles
;
172 void KItemListWidget::setColumnWidth(const QByteArray
& role
, qreal width
)
174 if (m_columnWidths
.value(role
) != width
) {
175 const qreal previousWidth
= width
;
176 m_columnWidths
.insert(role
, width
);
177 columnWidthChanged(role
, width
, previousWidth
);
182 qreal
KItemListWidget::columnWidth(const QByteArray
& role
) const
184 return m_columnWidths
.value(role
);
187 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
189 const KItemListStyleOption previous
= m_styleOption
;
191 m_styleOption
= option
;
193 styleOptionChanged(option
, previous
);
197 const KItemListStyleOption
& KItemListWidget::styleOption() const
199 return m_styleOption
;
202 void KItemListWidget::setSelected(bool selected
)
204 if (m_selected
!= selected
) {
205 m_selected
= selected
;
206 if (m_selectionToggle
) {
207 m_selectionToggle
->setChecked(selected
);
209 selectedChanged(selected
);
214 bool KItemListWidget::isSelected() const
219 void KItemListWidget::setCurrent(bool current
)
221 if (m_current
!= current
) {
223 currentChanged(current
);
228 bool KItemListWidget::isCurrent() const
233 void KItemListWidget::setHovered(bool hovered
)
235 if (hovered
== m_hovered
) {
241 if (!m_hoverAnimation
) {
242 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
243 const int duration
= (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects
) ? 1 : 200;
244 m_hoverAnimation
->setDuration(duration
);
245 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
247 m_hoverAnimation
->stop();
250 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
251 m_hoverAnimation
->setStartValue(startValue
);
252 m_hoverAnimation
->setEndValue(1.0);
253 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
254 initializeSelectionToggle();
257 m_hoverAnimation
->setStartValue(hoverOpacity());
258 m_hoverAnimation
->setEndValue(0.0);
261 m_hoverAnimation
->start();
263 hoveredChanged(hovered
);
267 bool KItemListWidget::isHovered() const
272 void KItemListWidget::setAlternateBackground(bool enable
)
274 if (m_alternateBackground
!= enable
) {
275 m_alternateBackground
= enable
;
276 alternateBackgroundChanged(enable
);
281 bool KItemListWidget::alternateBackground() const
283 return m_alternateBackground
;
286 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
288 if (m_enabledSelectionToggle
!= enable
) {
289 m_enabledSelectionToggle
= enable
;
294 bool KItemListWidget::enabledSelectionToggle() const
296 return m_enabledSelectionToggle
;
299 void KItemListWidget::setSiblingsInformation(const QBitArray
& siblings
)
301 const QBitArray previous
= m_siblingsInfo
;
302 m_siblingsInfo
= siblings
;
303 siblingsInformationChanged(m_siblingsInfo
, previous
);
307 QBitArray
KItemListWidget::siblingsInformation() const
309 return m_siblingsInfo
;
312 bool KItemListWidget::contains(const QPointF
& point
) const
314 if (!QGraphicsWidget::contains(point
)) {
318 return iconRect().contains(point
) ||
319 textRect().contains(point
) ||
320 expansionToggleRect().contains(point
) ||
321 selectionToggleRect().contains(point
);
324 QRectF
KItemListWidget::textFocusRect() const
329 QRectF
KItemListWidget::selectionToggleRect() const
334 QRectF
KItemListWidget::expansionToggleRect() const
339 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
340 const QSet
<QByteArray
>& roles
)
346 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
347 const QList
<QByteArray
>& previous
)
353 void KItemListWidget::columnWidthChanged(const QByteArray
& role
,
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::alternateBackgroundChanged(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 QStyleOptionViewItemV4 viewItemOption
;
453 viewItemOption
.initFrom(widget
);
454 viewItemOption
.state
= styleState
;
455 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
456 viewItemOption
.showDecorationSelected
= true;
457 viewItemOption
.rect
= textRect().toRect();
458 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
461 #include "kitemlistwidget.moc"