]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistwidget.cpp
Merge branch 'release/20.12'
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
index e7d2951b9fa518866972ba0af9754203f6a0bb2c..b4c14c1a7d7ac14c49b64045f96882f8c17028ed 100644 (file)
@@ -1,35 +1,16 @@
-/***************************************************************************
- *   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 <KDebug>
-
-#include <KGlobalSettings>
 #include <QApplication>
 #include <QPainter>
 #include <QPropertyAnimation>
@@ -44,7 +25,7 @@ KItemListWidgetInformant::~KItemListWidgetInformant()
 }
 
 KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
-    QGraphicsWidget(parent, 0),
+    QGraphicsWidget(parent),
     m_informant(informant),
     m_index(-1),
     m_selected(false),
@@ -58,9 +39,9 @@ KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsI
     m_styleOption(),
     m_siblingsInfo(),
     m_hoverOpacity(0),
-    m_hoverCache(0),
-    m_hoverAnimation(0),
-    m_selectionToggle(0),
+    m_hoverCache(nullptr),
+    m_hoverAnimation(nullptr),
+    m_selectionToggle(nullptr),
     m_editedRole()
 {
 }
@@ -74,7 +55,7 @@ void KItemListWidget::setIndex(int index)
 {
     if (m_index != index) {
         delete m_selectionToggle;
-        m_selectionToggle = 0;
+        m_selectionToggle = nullptr;
 
         if (m_hoverAnimation) {
             m_hoverAnimation->stop();
@@ -99,7 +80,7 @@ void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
         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);
@@ -114,7 +95,7 @@ QHash<QByteArray, QVariant> KItemListWidget::data() const
 
 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
 {
-    Q_UNUSED(option);
+    Q_UNUSED(option)
 
     if (m_alternateBackground) {
         const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
@@ -132,7 +113,7 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
 
     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) {
@@ -181,8 +162,8 @@ QList<QByteArray> KItemListWidget::visibleRoles() const
 
 void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
 {
-    if (m_columnWidths.value(role) != width) {
-        const qreal previousWidth = width;
+    const qreal previousWidth = m_columnWidths.value(role);
+    if (previousWidth != width) {
         m_columnWidths.insert(role, width);
         columnWidthChanged(role, width, previousWidth);
         update();
@@ -196,10 +177,13 @@ qreal KItemListWidget::columnWidth(const QByteArray& role) const
 
 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
 {
+    if (m_styleOption == option) {
+        return;
+    }
+
     const KItemListStyleOption previous = m_styleOption;
     clearHoverCache();
     m_styleOption = option;
-
     styleOptionChanged(option, previous);
     update();
 }
@@ -250,9 +234,9 @@ void KItemListWidget::setHovered(bool hovered)
 
     if (!m_hoverAnimation) {
         m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
-        const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200;
+        const int duration = style()->styleHint(QStyle::SH_Widget_Animate) ? 200 : 1;
         m_hoverAnimation->setDuration(duration);
-        connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
+        connect(m_hoverAnimation, &QPropertyAnimation::finished, this, &KItemListWidget::slotHoverAnimationFinished);
     }
     m_hoverAnimation->stop();
 
@@ -370,7 +354,8 @@ QRectF KItemListWidget::expansionToggleRect() const
 QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
                                           QWidget* widget)
 {
-    QPixmap pixmap(size().toSize());
+    QPixmap pixmap(size().toSize() * widget->devicePixelRatio());
+    pixmap.setDevicePixelRatio(widget->devicePixelRatio());
     pixmap.fill(Qt::transparent);
 
     QPainter painter(&pixmap);
@@ -380,7 +365,6 @@ QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option
     const bool wasHovered = m_hovered;
 
     setAlternateBackground(false);
-    setSelected(false);
     setHovered(false);
 
     paint(&painter, option, widget);
@@ -395,63 +379,63 @@ QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option
 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
                                   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)
 {
-    Q_UNUSED(current);
-    Q_UNUSED(previous);
+    Q_UNUSED(current)
+    Q_UNUSED(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)
 {
-    Q_UNUSED(current);
-    Q_UNUSED(previous);
+    Q_UNUSED(current)
+    Q_UNUSED(previous)
 }
 
 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)
 {
-    Q_UNUSED(current);
-    Q_UNUSED(previous);
+    Q_UNUSED(current)
+    Q_UNUSED(previous)
 }
 
 void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
 {
-    Q_UNUSED(current);
-    Q_UNUSED(previous);
+    Q_UNUSED(current)
+    Q_UNUSED(previous)
 }
 
 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
@@ -473,9 +457,9 @@ qreal KItemListWidget::hoverOpacity() const
 
 void KItemListWidget::slotHoverAnimationFinished()
 {
-    if (!m_hovered) {
-        delete m_selectionToggle;
-        m_selectionToggle = 0;
+    if (!m_hovered && m_selectionToggle) {
+        m_selectionToggle->deleteLater();
+        m_selectionToggle = nullptr;
     }
 }
 
@@ -503,7 +487,7 @@ void KItemListWidget::setHoverOpacity(qreal opacity)
 
     if (m_hoverOpacity <= 0.0) {
         delete m_hoverCache;
-        m_hoverCache = 0;
+        m_hoverCache = nullptr;
     }
 
     update();
@@ -512,18 +496,17 @@ void KItemListWidget::setHoverOpacity(qreal opacity)
 void KItemListWidget::clearHoverCache()
 {
     delete m_hoverCache;
-    m_hoverCache = 0;
+    m_hoverCache = nullptr;
 }
 
 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
 {
-    QStyleOptionViewItemV4 viewItemOption;
-    viewItemOption.initFrom(widget);
+    QStyleOptionViewItem viewItemOption;
+    initStyleOption(&viewItemOption);
     viewItemOption.state = styleState;
-    viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
+    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 "kitemlistwidget.moc"