-/***************************************************************************
- * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
- * *
- * Based on the Itemviews NG project from Trolltech Labs: *
- * http://qt.gitorious.org/qt-labs/itemviews-ng *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the *
- * Free Software Foundation, Inc., *
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
+ *
+ * Based on the Itemviews NG project from Trolltech Labs
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
#include "kitemlistwidget.h"
#include "kitemlistview.h"
-#include "kitemmodelbase.h"
-
#include "private/kitemlistselectiontoggle.h"
+#include <KConfigGroup>
+#include <KSharedConfig>
#include <QApplication>
#include <QPainter>
{
}
-KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
- QGraphicsWidget(parent, 0),
- m_informant(informant),
- m_index(-1),
- m_selected(false),
- m_current(false),
- m_hovered(false),
- m_alternateBackground(false),
- m_enabledSelectionToggle(false),
- m_data(),
- m_visibleRoles(),
- m_columnWidths(),
- m_styleOption(),
- m_siblingsInfo(),
- m_hoverOpacity(0),
- m_hoverCache(0),
- m_hoverAnimation(0),
- m_selectionToggle(0),
- m_editedRole()
-{
+KItemListWidget::KItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent)
+ : QGraphicsWidget(parent)
+ , m_informant(informant)
+ , m_index(-1)
+ , m_selected(false)
+ , m_current(false)
+ , m_hovered(false)
+ , m_expansionAreaHovered(false)
+ , m_alternateBackground(false)
+ , m_enabledSelectionToggle(false)
+ , m_data()
+ , m_visibleRoles()
+ , m_columnWidths()
+ , m_leftPadding(0)
+ , m_rightPadding(0)
+ , m_styleOption()
+ , m_siblingsInfo()
+ , m_hoverOpacity(0)
+ , m_hoverCache(nullptr)
+ , m_hoverSequenceIndex(0)
+ , m_selectionToggle(nullptr)
+ , m_editedRole()
+ , m_iconSize(-1)
+{
+ connect(&m_hoverSequenceTimer, &QTimer::timeout, this, &KItemListWidget::slotHoverSequenceTimerTimeout);
}
KItemListWidget::~KItemListWidget()
{
if (m_index != index) {
delete m_selectionToggle;
- m_selectionToggle = 0;
+ m_selectionToggle = nullptr;
+
+ m_hoverOpacity = 0;
- if (m_hoverAnimation) {
- m_hoverAnimation->stop();
- m_hoverOpacity = 0;
- }
clearHoverCache();
m_index = index;
return m_index;
}
-void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
- const QSet<QByteArray>& roles)
+void KItemListWidget::setData(const QHash<QByteArray, QVariant> &data, const QSet<QByteArray> &roles)
{
clearHoverCache();
if (roles.isEmpty()) {
m_data = data;
dataChanged(m_data);
} else {
- foreach (const QByteArray& role, roles) {
+ for (const QByteArray &role : roles) {
m_data[role] = data[role];
}
dataChanged(m_data, roles);
return m_data;
}
-void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
+QVariant KItemListWidget::value(const QByteArray &key) const
{
- Q_UNUSED(option);
+ return m_data.value(key);
+}
+
+void KItemListWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+ Q_UNUSED(option)
if (m_alternateBackground) {
- const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
+ QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
+ if (!widget->hasFocus()) {
+ QColor baseColor = m_styleOption.palette.color(QPalette::Base);
+ if (baseColor.lightnessF() > 0.5) {
+ // theme seems light
+ backgroundColor = backgroundColor.lighter(101);
+ } else {
+ // theme seems dark
+ backgroundColor = backgroundColor.darker(101);
+ }
+ }
+
const QRectF backgroundRect(0, 0, size().width(), size().height());
painter->fillRect(backgroundRect, backgroundColor);
}
if (m_selected && m_editedRole.isEmpty()) {
- const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
- drawItemStyleOption(painter, widget, activeState |
- QStyle::State_Enabled |
- QStyle::State_Selected |
- QStyle::State_Item);
+ const QStyle::State activeState(isActiveWindow() && widget->hasFocus() ? QStyle::State_Active : 0);
+ drawItemStyleOption(painter, widget, activeState | QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item);
}
if (m_current && m_editedRole.isEmpty()) {
QStyleOptionFocusRect focusRectOption;
- focusRectOption.initFrom(widget);
+ initStyleOption(&focusRectOption);
focusRectOption.rect = textFocusRect().toRect();
focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
- if (m_selected) {
+ if (m_selected && widget->hasFocus()) {
focusRectOption.state |= QStyle::State_Selected;
}
m_hoverCache->fill(Qt::transparent);
QPainter pixmapPainter(m_hoverCache);
- const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
- drawItemStyleOption(&pixmapPainter, widget, activeState |
- QStyle::State_Enabled |
- QStyle::State_MouseOver |
- QStyle::State_Item);
+ const QStyle::State activeState(isActiveWindow() && widget->hasFocus() ? QStyle::State_Active | QStyle::State_Enabled : 0);
+ drawItemStyleOption(&pixmapPainter, widget, activeState | QStyle::State_MouseOver | QStyle::State_Item);
}
const qreal opacity = painter->opacity();
}
}
-void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
+void KItemListWidget::setVisibleRoles(const QList<QByteArray> &roles)
{
const QList<QByteArray> previousRoles = m_visibleRoles;
m_visibleRoles = roles;
return m_visibleRoles;
}
-
-void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
+void KItemListWidget::setColumnWidth(const QByteArray &role, qreal width)
{
const qreal previousWidth = m_columnWidths.value(role);
if (previousWidth != width) {
}
}
-qreal KItemListWidget::columnWidth(const QByteArray& role) const
+qreal KItemListWidget::columnWidth(const QByteArray &role) const
{
return m_columnWidths.value(role);
}
-void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
+void KItemListWidget::setSidePadding(qreal leftPaddingWidth, qreal rightPaddingWidth)
+{
+ bool changed = false;
+ if (m_leftPadding != leftPaddingWidth) {
+ m_leftPadding = leftPaddingWidth;
+ changed = true;
+ }
+
+ if (m_rightPadding != rightPaddingWidth) {
+ m_rightPadding = rightPaddingWidth;
+ changed = true;
+ }
+
+ if (!changed) {
+ return;
+ }
+
+ sidePaddingChanged(leftPaddingWidth, rightPaddingWidth);
+ update();
+}
+
+qreal KItemListWidget::leftPadding() const
{
+ return m_leftPadding;
+}
+
+qreal KItemListWidget::rightPadding() const
+{
+ return m_rightPadding;
+}
+
+void KItemListWidget::setStyleOption(const KItemListStyleOption &option)
+{
+ if (m_styleOption == option) {
+ return;
+ }
+
const KItemListStyleOption previous = m_styleOption;
clearHoverCache();
m_styleOption = option;
-
styleOptionChanged(option, previous);
update();
}
-const KItemListStyleOption& KItemListWidget::styleOption() const
+const KItemListStyleOption &KItemListWidget::styleOption() const
{
return m_styleOption;
}
m_hovered = hovered;
- if (!m_hoverAnimation) {
- m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
- const int duration = style()->styleHint(QStyle::SH_Widget_Animate) ? 200 : 1;
- m_hoverAnimation->setDuration(duration);
- connect(m_hoverAnimation, &QPropertyAnimation::finished, this, &KItemListWidget::slotHoverAnimationFinished);
- }
- m_hoverAnimation->stop();
+ m_hoverSequenceIndex = 0;
if (hovered) {
- const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
- m_hoverAnimation->setStartValue(startValue);
- m_hoverAnimation->setEndValue(1.0);
+ setHoverOpacity(1.0);
+
if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
initializeSelectionToggle();
}
+
+ hoverSequenceStarted();
+
+ const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
+ const int interval = globalConfig.readEntry("HoverSequenceInterval", 700);
+
+ m_hoverSequenceTimer.start(interval);
} else {
- m_hoverAnimation->setStartValue(hoverOpacity());
- m_hoverAnimation->setEndValue(0.0);
- }
+ setHoverOpacity(0.0);
- m_hoverAnimation->start();
+ if (m_selectionToggle) {
+ m_selectionToggle->deleteLater();
+ m_selectionToggle = nullptr;
+ }
+
+ hoverSequenceEnded();
+ m_hoverSequenceTimer.stop();
+ }
hoveredChanged(hovered);
update();
return m_hovered;
}
-void KItemListWidget::setHoverPosition(const QPointF& pos)
+void KItemListWidget::setExpansionAreaHovered(bool hovered)
+{
+ if (hovered == m_expansionAreaHovered) {
+ return;
+ }
+ m_expansionAreaHovered = hovered;
+ update();
+}
+
+bool KItemListWidget::expansionAreaHovered() const
+{
+ return m_expansionAreaHovered;
+}
+
+void KItemListWidget::setHoverPosition(const QPointF &pos)
{
if (m_selectionToggle) {
m_selectionToggle->setHovered(selectionToggleRect().contains(pos));
{
if (m_enabledSelectionToggle != enable) {
m_enabledSelectionToggle = enable;
+
+ // We want the change to take effect immediately.
+ if (m_enabledSelectionToggle) {
+ if (m_hovered) {
+ initializeSelectionToggle();
+ }
+ } else if (m_selectionToggle) {
+ m_selectionToggle->deleteLater();
+ m_selectionToggle = nullptr;
+ }
+
update();
}
}
return m_enabledSelectionToggle;
}
-void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
+void KItemListWidget::setSiblingsInformation(const QBitArray &siblings)
{
const QBitArray previous = m_siblingsInfo;
m_siblingsInfo = siblings;
return m_siblingsInfo;
}
-void KItemListWidget::setEditedRole(const QByteArray& role)
+void KItemListWidget::setEditedRole(const QByteArray &role)
{
if (m_editedRole != role) {
const QByteArray previous = m_editedRole;
return m_editedRole;
}
-bool KItemListWidget::contains(const QPointF& point) const
+void KItemListWidget::setIconSize(int iconSize)
+{
+ if (m_iconSize != iconSize) {
+ const int previousIconSize = m_iconSize;
+ m_iconSize = iconSize;
+ iconSizeChanged(iconSize, previousIconSize);
+ }
+}
+
+int KItemListWidget::iconSize() const
+{
+ return m_iconSize;
+}
+
+bool KItemListWidget::contains(const QPointF &point) const
{
if (!QGraphicsWidget::contains(point)) {
return false;
}
- return iconRect().contains(point) ||
- textRect().contains(point) ||
- expansionToggleRect().contains(point) ||
- selectionToggleRect().contains(point);
+ return iconRect().contains(point) || textRect().contains(point) || expansionToggleRect().contains(point) || selectionToggleRect().contains(point);
}
QRectF KItemListWidget::textFocusRect() const
return QRectF();
}
-QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
- QWidget* widget)
+QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPixmap pixmap(size().toSize() * widget->devicePixelRatio());
pixmap.setDevicePixelRatio(widget->devicePixelRatio());
const bool wasHovered = m_hovered;
setAlternateBackground(false);
- setSelected(false);
setHovered(false);
paint(&painter, option, widget);
return pixmap;
}
-void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
- const QSet<QByteArray>& roles)
+void KItemListWidget::startActivateSoonAnimation(int timeUntilActivation)
+{
+ Q_UNUSED(timeUntilActivation)
+}
+
+void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant> ¤t, const QSet<QByteArray> &roles)
{
- Q_UNUSED(current);
- Q_UNUSED(roles);
+ Q_UNUSED(current)
+ Q_UNUSED(roles)
}
-void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
- const QList<QByteArray>& previous)
+void KItemListWidget::visibleRolesChanged(const QList<QByteArray> ¤t, const QList<QByteArray> &previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
}
-void KItemListWidget::columnWidthChanged(const QByteArray& role,
- qreal current,
- qreal previous)
+void KItemListWidget::columnWidthChanged(const QByteArray &role, qreal current, qreal previous)
{
- Q_UNUSED(role);
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(role)
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
}
-void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
- const KItemListStyleOption& previous)
+void KItemListWidget::sidePaddingChanged(qreal leftPaddingWidth, qreal rightPaddingWidth)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(leftPaddingWidth)
+ Q_UNUSED(rightPaddingWidth)
+}
+
+void KItemListWidget::styleOptionChanged(const KItemListStyleOption ¤t, const KItemListStyleOption &previous)
+{
+ Q_UNUSED(previous)
+
+ // set the initial value of m_iconSize if not set
+ if (m_iconSize == -1) {
+ m_iconSize = current.iconSize;
+ }
}
void KItemListWidget::currentChanged(bool current)
{
- Q_UNUSED(current);
+ Q_UNUSED(current)
}
void KItemListWidget::selectedChanged(bool selected)
{
- Q_UNUSED(selected);
+ Q_UNUSED(selected)
}
void KItemListWidget::hoveredChanged(bool hovered)
{
- Q_UNUSED(hovered);
+ Q_UNUSED(hovered)
}
void KItemListWidget::alternateBackgroundChanged(bool enabled)
{
- Q_UNUSED(enabled);
+ Q_UNUSED(enabled)
}
-void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
+void KItemListWidget::siblingsInformationChanged(const QBitArray ¤t, const QBitArray &previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
}
-void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
+void KItemListWidget::editedRoleChanged(const QByteArray ¤t, const QByteArray &previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
}
-void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
+void KItemListWidget::iconSizeChanged(int current, int previous)
+{
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
+}
+
+void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
QGraphicsWidget::resizeEvent(event);
clearHoverCache();
if (m_selectionToggle) {
- const QRectF& toggleRect = selectionToggleRect();
+ const QRectF &toggleRect = selectionToggleRect();
m_selectionToggle->setPos(toggleRect.topLeft());
m_selectionToggle->resize(toggleRect.size());
}
}
+void KItemListWidget::hoverSequenceStarted()
+{
+}
+
+void KItemListWidget::hoverSequenceIndexChanged(int sequenceIndex)
+{
+ Q_UNUSED(sequenceIndex);
+}
+
+void KItemListWidget::hoverSequenceEnded()
+{
+}
+
qreal KItemListWidget::hoverOpacity() const
{
return m_hoverOpacity;
}
-void KItemListWidget::slotHoverAnimationFinished()
+int KItemListWidget::hoverSequenceIndex() const
{
- if (!m_hovered && m_selectionToggle) {
- m_selectionToggle->deleteLater();
- m_selectionToggle = 0;
- }
+ return m_hoverSequenceIndex;
+}
+
+void KItemListWidget::slotHoverSequenceTimerTimeout()
+{
+ m_hoverSequenceIndex++;
+ hoverSequenceIndexChanged(m_hoverSequenceIndex);
}
void KItemListWidget::initializeSelectionToggle()
if (m_hoverOpacity <= 0.0) {
delete m_hoverCache;
- m_hoverCache = 0;
+ m_hoverCache = nullptr;
}
update();
void KItemListWidget::clearHoverCache()
{
delete m_hoverCache;
- m_hoverCache = 0;
+ m_hoverCache = nullptr;
}
-void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
+void KItemListWidget::drawItemStyleOption(QPainter *painter, QWidget *widget, QStyle::State styleState)
{
QStyleOptionViewItem viewItemOption;
- viewItemOption.initFrom(widget);
+ initStyleOption(&viewItemOption);
viewItemOption.state = styleState;
viewItemOption.viewItemPosition = QStyleOptionViewItem::OnlyOne;
viewItemOption.showDecorationSelected = true;
viewItemOption.rect = selectionRect().toRect();
- widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
+ style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
}
+#include "moc_kitemlistwidget.cpp"