, m_expansionAreaHovered(false)
, m_alternateBackground(false)
, m_enabledSelectionToggle(false)
+ , m_clickHighlighted(false)
, m_data()
, m_visibleRoles()
, m_columnWidths()
- , m_sidePadding(0)
+ , m_leftPadding(0)
+ , m_rightPadding(0)
, m_styleOption()
, m_siblingsInfo()
, m_hoverOpacity(0)
, m_hoverCache(nullptr)
- , m_hoverAnimation(nullptr)
, m_hoverSequenceIndex(0)
, m_selectionToggle(nullptr)
, m_editedRole()
delete m_selectionToggle;
m_selectionToggle = nullptr;
- if (m_hoverAnimation) {
- m_hoverAnimation->stop();
- m_hoverOpacity = 0;
- }
+ m_hoverOpacity = 0;
+
clearHoverCache();
m_index = index;
return m_data;
}
+QVariant KItemListWidget::value(const QByteArray &key) const
+{
+ 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);
+ if ((m_selected || m_current) && m_editedRole.isEmpty()) {
+ 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;
- initStyleOption(&focusRectOption);
- focusRectOption.rect = textFocusRect().toRect();
- focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
- if (m_selected) {
- focusRectOption.state |= QStyle::State_Selected;
- }
-
- style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
- }
-
if (m_hoverOpacity > 0.0) {
if (!m_hoverCache) {
// Initialize the m_hoverCache pixmap to improve the drawing performance
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();
return m_columnWidths.value(role);
}
-qreal KItemListWidget::sidePadding() const
+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_sidePadding;
+ return m_leftPadding;
}
-void KItemListWidget::setSidePadding(qreal width)
+qreal KItemListWidget::rightPadding() const
{
- if (m_sidePadding != width) {
- m_sidePadding = width;
- sidePaddingChanged(width);
- update();
- }
+ return m_rightPadding;
}
void KItemListWidget::setStyleOption(const KItemListStyleOption &option)
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(), "PreviewSettings");
+ 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);
+
+ if (m_selectionToggle) {
+ m_selectionToggle->deleteLater();
+ m_selectionToggle = nullptr;
+ }
hoverSequenceEnded();
m_hoverSequenceTimer.stop();
}
- m_hoverAnimation->start();
-
hoveredChanged(hovered);
update();
}
return false;
}
- return iconRect().contains(point) || textRect().contains(point) || expansionToggleRect().contains(point) || selectionToggleRect().contains(point);
+ return selectionRectFull().contains(point) || expansionToggleRect().contains(point);
}
QRectF KItemListWidget::textFocusRect() const
return pixmap;
}
+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(previous)
}
-void KItemListWidget::sidePaddingChanged(qreal width)
+void KItemListWidget::sidePaddingChanged(qreal leftPaddingWidth, qreal rightPaddingWidth)
{
- Q_UNUSED(width)
+ Q_UNUSED(leftPaddingWidth)
+ Q_UNUSED(rightPaddingWidth)
}
void KItemListWidget::styleOptionChanged(const KItemListStyleOption ¤t, const KItemListStyleOption &previous)
return m_hoverSequenceIndex;
}
-void KItemListWidget::slotHoverAnimationFinished()
-{
- if (!m_hovered && m_selectionToggle) {
- m_selectionToggle->deleteLater();
- m_selectionToggle = nullptr;
- }
-}
-
void KItemListWidget::slotHoverSequenceTimerTimeout()
{
m_hoverSequenceIndex++;
m_hoverCache = nullptr;
}
+bool KItemListWidget::isPressed() const
+{
+ return m_clickHighlighted;
+}
+
+void KItemListWidget::setPressed(bool enabled)
+{
+ if (m_clickHighlighted != enabled) {
+ m_clickHighlighted = enabled;
+ clearHoverCache();
+ update();
+ }
+}
+
void KItemListWidget::drawItemStyleOption(QPainter *painter, QWidget *widget, QStyle::State styleState)
{
QStyleOptionViewItem viewItemOption;
+ constexpr int roundness = 5; // From Breeze style.
+ constexpr qreal penWidth = 1.5;
initStyleOption(&viewItemOption);
viewItemOption.state = styleState;
viewItemOption.viewItemPosition = QStyleOptionViewItem::OnlyOne;
viewItemOption.showDecorationSelected = true;
- viewItemOption.rect = selectionRect().toRect();
- style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
+ viewItemOption.rect = selectionRectFull().toRect();
+ QPainterPath path;
+ path.addRoundedRect(selectionRectFull().adjusted(penWidth, penWidth, -penWidth, -penWidth), roundness, roundness);
+ QColor backgroundColor{widget->palette().color(QPalette::Accent)};
+ painter->setRenderHint(QPainter::Antialiasing);
+ bool current = m_current && styleState & QStyle::State_Active;
+
+ // Background item, alpha values are from
+ // https://invent.kde.org/plasma/libplasma/-/blob/master/src/desktoptheme/breeze/widgets/viewitem.svg
+ backgroundColor.setAlphaF(0.0);
+
+ if (m_clickHighlighted) {
+ backgroundColor.setAlphaF(1.0);
+ } else {
+ if (m_selected && m_hovered) {
+ backgroundColor.setAlphaF(0.40);
+ } else if (m_selected) {
+ backgroundColor.setAlphaF(0.32);
+ } else if (m_hovered) {
+ backgroundColor = widget->palette().color(QPalette::Text);
+ backgroundColor.setAlphaF(0.06);
+ }
+ }
+
+ painter->fillPath(path, backgroundColor);
+
+ // Focus decoration
+ if (current) {
+ QColor focusColor{widget->palette().color(QPalette::Accent)};
+ focusColor = m_styleOption.palette.color(QPalette::Base).lightnessF() > 0.5 ? focusColor.darker(110) : focusColor.lighter(110);
+ focusColor.setAlphaF(m_selected || m_hovered ? 0.8 : 0.6);
+ // Set the pen color lighter or darker depending on background color
+ QPen pen{focusColor, penWidth};
+ pen.setCosmetic(true);
+ painter->setPen(pen);
+ painter->drawPath(path);
+ }
}
#include "moc_kitemlistwidget.cpp"