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 "private/kitemlistselectiontoggle.h"
28 #include <QApplication>
30 #include <QPropertyAnimation>
31 #include <QStyleOption>
33 KItemListWidgetInformant::KItemListWidgetInformant()
37 KItemListWidgetInformant::~KItemListWidgetInformant()
41 KItemListWidget::KItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
42 QGraphicsWidget(parent
, nullptr),
43 m_informant(informant
),
48 m_alternateBackground(false),
49 m_enabledSelectionToggle(false),
56 m_hoverCache(nullptr),
57 m_hoverAnimation(nullptr),
58 m_selectionToggle(nullptr),
63 KItemListWidget::~KItemListWidget()
68 void KItemListWidget::setIndex(int index
)
70 if (m_index
!= index
) {
71 delete m_selectionToggle
;
72 m_selectionToggle
= nullptr;
74 if (m_hoverAnimation
) {
75 m_hoverAnimation
->stop();
84 int KItemListWidget::index() const
89 void KItemListWidget::setData(const QHash
<QByteArray
, QVariant
>& data
,
90 const QSet
<QByteArray
>& roles
)
93 if (roles
.isEmpty()) {
97 foreach (const QByteArray
& role
, roles
) {
98 m_data
[role
] = data
[role
];
100 dataChanged(m_data
, roles
);
105 QHash
<QByteArray
, QVariant
> KItemListWidget::data() const
110 void KItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
114 if (m_alternateBackground
) {
115 const QColor backgroundColor
= m_styleOption
.palette
.color(QPalette::AlternateBase
);
116 const QRectF
backgroundRect(0, 0, size().width(), size().height());
117 painter
->fillRect(backgroundRect
, backgroundColor
);
120 if (m_selected
&& m_editedRole
.isEmpty()) {
121 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
122 drawItemStyleOption(painter
, widget
, activeState
|
123 QStyle::State_Enabled
|
124 QStyle::State_Selected
|
128 if (m_current
&& m_editedRole
.isEmpty()) {
129 QStyleOptionFocusRect focusRectOption
;
130 initStyleOption(&focusRectOption
);
131 focusRectOption
.rect
= textFocusRect().toRect();
132 focusRectOption
.state
= QStyle::State_Enabled
| QStyle::State_Item
| QStyle::State_KeyboardFocusChange
;
134 focusRectOption
.state
|= QStyle::State_Selected
;
137 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &focusRectOption
, painter
, widget
);
140 if (m_hoverOpacity
> 0.0) {
142 // Initialize the m_hoverCache pixmap to improve the drawing performance
143 // when fading the hover background
144 m_hoverCache
= new QPixmap(size().toSize());
145 m_hoverCache
->fill(Qt::transparent
);
147 QPainter
pixmapPainter(m_hoverCache
);
148 const QStyle::State
activeState(isActiveWindow() ? QStyle::State_Active
: 0);
149 drawItemStyleOption(&pixmapPainter
, widget
, activeState
|
150 QStyle::State_Enabled
|
151 QStyle::State_MouseOver
|
155 const qreal opacity
= painter
->opacity();
156 painter
->setOpacity(m_hoverOpacity
* opacity
);
157 painter
->drawPixmap(0, 0, *m_hoverCache
);
158 painter
->setOpacity(opacity
);
162 void KItemListWidget::setVisibleRoles(const QList
<QByteArray
>& roles
)
164 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
165 m_visibleRoles
= roles
;
167 visibleRolesChanged(roles
, previousRoles
);
171 QList
<QByteArray
> KItemListWidget::visibleRoles() const
173 return m_visibleRoles
;
177 void KItemListWidget::setColumnWidth(const QByteArray
& role
, qreal width
)
179 const qreal previousWidth
= m_columnWidths
.value(role
);
180 if (previousWidth
!= width
) {
181 m_columnWidths
.insert(role
, width
);
182 columnWidthChanged(role
, width
, previousWidth
);
187 qreal
KItemListWidget::columnWidth(const QByteArray
& role
) const
189 return m_columnWidths
.value(role
);
192 void KItemListWidget::setStyleOption(const KItemListStyleOption
& option
)
194 if (m_styleOption
== option
) {
198 const KItemListStyleOption previous
= m_styleOption
;
200 m_styleOption
= option
;
201 styleOptionChanged(option
, previous
);
205 const KItemListStyleOption
& KItemListWidget::styleOption() const
207 return m_styleOption
;
210 void KItemListWidget::setSelected(bool selected
)
212 if (m_selected
!= selected
) {
213 m_selected
= selected
;
214 if (m_selectionToggle
) {
215 m_selectionToggle
->setChecked(selected
);
217 selectedChanged(selected
);
222 bool KItemListWidget::isSelected() const
227 void KItemListWidget::setCurrent(bool current
)
229 if (m_current
!= current
) {
231 currentChanged(current
);
236 bool KItemListWidget::isCurrent() const
241 void KItemListWidget::setHovered(bool hovered
)
243 if (hovered
== m_hovered
) {
249 if (!m_hoverAnimation
) {
250 m_hoverAnimation
= new QPropertyAnimation(this, "hoverOpacity", this);
251 const int duration
= style()->styleHint(QStyle::SH_Widget_Animate
) ? 200 : 1;
252 m_hoverAnimation
->setDuration(duration
);
253 connect(m_hoverAnimation
, &QPropertyAnimation::finished
, this, &KItemListWidget::slotHoverAnimationFinished
);
255 m_hoverAnimation
->stop();
258 const qreal startValue
= qMax(hoverOpacity(), qreal(0.1));
259 m_hoverAnimation
->setStartValue(startValue
);
260 m_hoverAnimation
->setEndValue(1.0);
261 if (m_enabledSelectionToggle
&& !(QApplication::mouseButtons() & Qt::LeftButton
)) {
262 initializeSelectionToggle();
265 m_hoverAnimation
->setStartValue(hoverOpacity());
266 m_hoverAnimation
->setEndValue(0.0);
269 m_hoverAnimation
->start();
271 hoveredChanged(hovered
);
275 bool KItemListWidget::isHovered() const
280 void KItemListWidget::setHoverPosition(const QPointF
& pos
)
282 if (m_selectionToggle
) {
283 m_selectionToggle
->setHovered(selectionToggleRect().contains(pos
));
287 void KItemListWidget::setAlternateBackground(bool enable
)
289 if (m_alternateBackground
!= enable
) {
290 m_alternateBackground
= enable
;
291 alternateBackgroundChanged(enable
);
296 bool KItemListWidget::alternateBackground() const
298 return m_alternateBackground
;
301 void KItemListWidget::setEnabledSelectionToggle(bool enable
)
303 if (m_enabledSelectionToggle
!= enable
) {
304 m_enabledSelectionToggle
= enable
;
309 bool KItemListWidget::enabledSelectionToggle() const
311 return m_enabledSelectionToggle
;
314 void KItemListWidget::setSiblingsInformation(const QBitArray
& siblings
)
316 const QBitArray previous
= m_siblingsInfo
;
317 m_siblingsInfo
= siblings
;
318 siblingsInformationChanged(m_siblingsInfo
, previous
);
322 QBitArray
KItemListWidget::siblingsInformation() const
324 return m_siblingsInfo
;
327 void KItemListWidget::setEditedRole(const QByteArray
& role
)
329 if (m_editedRole
!= role
) {
330 const QByteArray previous
= m_editedRole
;
332 editedRoleChanged(role
, previous
);
336 QByteArray
KItemListWidget::editedRole() const
341 bool KItemListWidget::contains(const QPointF
& point
) const
343 if (!QGraphicsWidget::contains(point
)) {
347 return iconRect().contains(point
) ||
348 textRect().contains(point
) ||
349 expansionToggleRect().contains(point
) ||
350 selectionToggleRect().contains(point
);
353 QRectF
KItemListWidget::textFocusRect() const
358 QRectF
KItemListWidget::selectionToggleRect() const
363 QRectF
KItemListWidget::expansionToggleRect() const
368 QPixmap
KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem
* option
,
371 QPixmap
pixmap(size().toSize() * widget
->devicePixelRatio());
372 pixmap
.setDevicePixelRatio(widget
->devicePixelRatio());
373 pixmap
.fill(Qt::transparent
);
375 QPainter
painter(&pixmap
);
377 const bool oldAlternateBackground
= m_alternateBackground
;
378 const bool wasSelected
= m_selected
;
379 const bool wasHovered
= m_hovered
;
381 setAlternateBackground(false);
385 paint(&painter
, option
, widget
);
387 setAlternateBackground(oldAlternateBackground
);
388 setSelected(wasSelected
);
389 setHovered(wasHovered
);
394 void KItemListWidget::dataChanged(const QHash
<QByteArray
, QVariant
>& current
,
395 const QSet
<QByteArray
>& roles
)
401 void KItemListWidget::visibleRolesChanged(const QList
<QByteArray
>& current
,
402 const QList
<QByteArray
>& previous
)
408 void KItemListWidget::columnWidthChanged(const QByteArray
& role
,
417 void KItemListWidget::styleOptionChanged(const KItemListStyleOption
& current
,
418 const KItemListStyleOption
& previous
)
424 void KItemListWidget::currentChanged(bool current
)
429 void KItemListWidget::selectedChanged(bool selected
)
434 void KItemListWidget::hoveredChanged(bool hovered
)
439 void KItemListWidget::alternateBackgroundChanged(bool enabled
)
444 void KItemListWidget::siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
)
450 void KItemListWidget::editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
456 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent
* event
)
458 QGraphicsWidget::resizeEvent(event
);
461 if (m_selectionToggle
) {
462 const QRectF
& toggleRect
= selectionToggleRect();
463 m_selectionToggle
->setPos(toggleRect
.topLeft());
464 m_selectionToggle
->resize(toggleRect
.size());
468 qreal
KItemListWidget::hoverOpacity() const
470 return m_hoverOpacity
;
473 void KItemListWidget::slotHoverAnimationFinished()
475 if (!m_hovered
&& m_selectionToggle
) {
476 m_selectionToggle
->deleteLater();
477 m_selectionToggle
= nullptr;
481 void KItemListWidget::initializeSelectionToggle()
483 Q_ASSERT(m_enabledSelectionToggle
);
485 if (!m_selectionToggle
) {
486 m_selectionToggle
= new KItemListSelectionToggle(this);
489 const QRectF toggleRect
= selectionToggleRect();
490 m_selectionToggle
->setPos(toggleRect
.topLeft());
491 m_selectionToggle
->resize(toggleRect
.size());
493 m_selectionToggle
->setChecked(isSelected());
496 void KItemListWidget::setHoverOpacity(qreal opacity
)
498 m_hoverOpacity
= opacity
;
499 if (m_selectionToggle
) {
500 m_selectionToggle
->setOpacity(opacity
);
503 if (m_hoverOpacity
<= 0.0) {
505 m_hoverCache
= nullptr;
511 void KItemListWidget::clearHoverCache()
514 m_hoverCache
= nullptr;
517 void KItemListWidget::drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
)
519 QStyleOptionViewItem viewItemOption
;
520 initStyleOption(&viewItemOption
);
521 viewItemOption
.state
= styleState
;
522 viewItemOption
.viewItemPosition
= QStyleOptionViewItem::OnlyOne
;
523 viewItemOption
.showDecorationSelected
= true;
524 viewItemOption
.rect
= selectionRect().toRect();
525 style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewItemOption
, painter
, widget
);