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_alternateBackground(false),
44 m_enabledSelectionToggle(false),
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_alternateBackground
) {
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
);
125 focusRectOption
.rect
= textFocusRect().toRect();
126 focusRectOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
| QStyle::State_KeyboardFocusChange
;
128 focusRectOption
.state
|= QStyle::State_Selected
;
131 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &focusRectOption
, painter
, widget
);
134 if (m_hoverOpacity
> 0.0) {
136 // Initialize the m_hoverCache pixmap to improve the drawing performance
137 // when fading the hover background
138 m_hoverCache
= new QPixmap(size().toSize());
139 m_hoverCache
->fill(Qt::transparent
);
141 QPainter
pixmapPainter(m_hoverCache
);
142 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
143 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
144 QStyle::State_Enabled
|
145 QStyle::State_MouseOver
|
149 const qreal opacity
= painter
->opacity();
150 painter
->setOpacity(m_hoverOpacity
* opacity
);
151 painter
->drawPixmap(0, 0, *m_hoverCache
);
152 painter
->setOpacity(opacity
);
156 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
158 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
159 m_visibleRoles
= roles
;
161 visibleRolesChanged(roles
, previousRoles
);
165 QList
<QByteArray
> KItemListWidget::visibleRoles() const
167 return m_visibleRoles
;
171 void KItemListWidget::setColumnWidth(const QByteArray
& role
, qreal width
)
173 if (m_columnWidths
.value(role
) != width
) {
174 const qreal previousWidth
= width
;
175 m_columnWidths
.insert(role
, width
);
176 columnWidthChanged(role
, width
, previousWidth
);
181 qreal
KItemListWidget::columnWidth(const QByteArray
& role
) const
183 return m_columnWidths
.value(role
);
186 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
188 const KItemListStyleOption previous
= m_styleOption
;
190 m_styleOption
= option
;
192 styleOptionChanged(option
, previous
);
196 const KItemListStyleOption
& KItemListWidget::styleOption() const
198 return m_styleOption
;
201 void KItemListWidget::setSelected(bool selected
)
203 if (m_selected
!= selected
) {
204 m_selected
= selected
;
205 if (m_selectionToggle
) {
206 m_selectionToggle
->setChecked(selected
);
208 selectedChanged(selected
);
213 bool KItemListWidget::isSelected() const
218 void KItemListWidget::setCurrent(bool current
)
220 if (m_current
!= current
) {
222 currentChanged(current
);
227 bool KItemListWidget::isCurrent() const
232 void KItemListWidget::setHovered(bool hovered
)
234 if (hovered
== m_hovered
) {
240 if (!m_hoverAnimation
) {
241 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
242 const int duration
= (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects
) ? 1 : 200;
243 m_hoverAnimation
->setDuration(duration
);
244 connect(m_hoverAnimation
, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
246 m_hoverAnimation
->stop();
249 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
250 m_hoverAnimation
->setStartValue(startValue
);
251 m_hoverAnimation
->setEndValue(1.0);
252 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
253 initializeSelectionToggle();
256 m_hoverAnimation
->setStartValue(hoverOpacity());
257 m_hoverAnimation
->setEndValue(0.0);
260 m_hoverAnimation
->start();
262 hoveredChanged(hovered
);
266 bool KItemListWidget::isHovered() const
271 void KItemListWidget::setAlternateBackground(bool enable
)
273 if (m_alternateBackground
!= enable
) {
274 m_alternateBackground
= enable
;
275 alternateBackgroundChanged(enable
);
280 bool KItemListWidget::alternateBackground() const
282 return m_alternateBackground
;
285 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
287 if (m_enabledSelectionToggle
!= enable
) {
288 m_enabledSelectionToggle
= enable
;
293 bool KItemListWidget::enabledSelectionToggle() const
295 return m_enabledSelectionToggle
;
298 void KItemListWidget::setSiblingsInformation(const QBitArray
& siblings
)
300 const QBitArray previous
= m_siblingsInfo
;
301 m_siblingsInfo
= siblings
;
302 siblingsInformationChanged(m_siblingsInfo
, previous
);
306 QBitArray
KItemListWidget::siblingsInformation() const
308 return m_siblingsInfo
;
311 bool KItemListWidget::contains(const QPointF
& point
) const
313 if (!QGraphicsWidget::contains(point
)) {
317 return iconRect().contains(point
) ||
318 textRect().contains(point
) ||
319 expansionToggleRect().contains(point
) ||
320 selectionToggleRect().contains(point
);
323 QRectF
KItemListWidget::textFocusRect() const
328 QRectF
KItemListWidget::selectionToggleRect() const
333 QRectF
KItemListWidget::expansionToggleRect() const
338 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
339 const QSet
<QByteArray
>& roles
)
345 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
346 const QList
<QByteArray
>& previous
)
352 void KItemListWidget::columnWidthChanged(const QByteArray
& role
,
361 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
362 const KItemListStyleOption
& previous
)
368 void KItemListWidget::currentChanged(bool current
)
373 void KItemListWidget::selectedChanged(bool selected
)
378 void KItemListWidget::hoveredChanged(bool hovered
)
383 void KItemListWidget::alternateBackgroundChanged(bool enabled
)
388 void KItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
394 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
396 QGraphicsWidget::resizeEvent(event
);
400 qreal
KItemListWidget::hoverOpacity() const
402 return m_hoverOpacity
;
405 void KItemListWidget::slotHoverAnimationFinished()
408 delete m_selectionToggle
;
409 m_selectionToggle
= 0;
413 void KItemListWidget::initializeSelectionToggle()
415 Q_ASSERT(m_enabledSelectionToggle
);
417 if (!m_selectionToggle
) {
418 m_selectionToggle
= new KItemListSelectionToggle(this);
421 const QRectF toggleRect
= selectionToggleRect();
422 m_selectionToggle
->setPos(toggleRect
.topLeft());
423 m_selectionToggle
->resize(toggleRect
.size());
425 m_selectionToggle
->setChecked(isSelected());
428 void KItemListWidget::setHoverOpacity(qreal opacity
)
430 m_hoverOpacity
= opacity
;
431 if (m_selectionToggle
) {
432 m_selectionToggle
->setOpacity(opacity
);
435 if (m_hoverOpacity
<= 0.0) {
443 void KItemListWidget::clearHoverCache()
449 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
451 QStyleOptionViewItemV4 viewItemOption
;
452 viewItemOption
.initFrom(widget
);
453 viewItemOption
.state
= styleState
;
454 viewItemOption
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
455 viewItemOption
.showDecorationSelected
= true;
456 viewItemOption
.rect
= textRect().toRect();
457 widget
->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);
460 #include "kitemlistwidget.moc"