]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistwidget.cpp
Remove unused #include
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
index 687367adb9fffa04917a78aa2eb83ce1d00a1031..f92309254f2944b0aeeda6c9b013c5f73a587d0d 100644 (file)
 
 #include "kitemlistwidget.h"
 
-#include "kitemlistselectiontoggle_p.h"
 #include "kitemlistview.h"
-#include "kitemmodelbase.h"
 
-#include <KDebug>
+#include "private/kitemlistselectiontoggle.h"
+
 
 #include <QApplication>
 #include <QPainter>
 #include <QPropertyAnimation>
 #include <QStyleOption>
 
-KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
-    QGraphicsWidget(parent, 0),
+KItemListWidgetInformant::KItemListWidgetInformant()
+{
+}
+
+KItemListWidgetInformant::~KItemListWidgetInformant()
+{
+}
+
+KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
+    QGraphicsWidget(parent, nullptr),
+    m_informant(informant),
     m_index(-1),
     m_selected(false),
     m_current(false),
     m_hovered(false),
-    m_alternatingBackgroundColors(false),
+    m_alternateBackground(false),
     m_enabledSelectionToggle(false),
     m_data(),
     m_visibleRoles(),
-    m_visibleRolesSizes(),
+    m_columnWidths(),
     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()
 {
 }
 
@@ -61,7 +71,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();
@@ -91,6 +101,7 @@ void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
         }
         dataChanged(m_data, roles);
     }
+    update();
 }
 
 QHash<QByteArray, QVariant> KItemListWidget::data() const
@@ -102,15 +113,13 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
 {
     Q_UNUSED(option);
 
-    painter->setRenderHint(QPainter::Antialiasing);
-
-    if (m_alternatingBackgroundColors && (m_index & 0x1)) {
+    if (m_alternateBackground) {
         const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
         const QRectF backgroundRect(0, 0, size().width(), size().height());
         painter->fillRect(backgroundRect, backgroundColor);
     }
 
-    if (m_selected) {
+    if (m_selected && m_editedRole.isEmpty()) {
         const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
         drawItemStyleOption(painter, widget, activeState |
                                              QStyle::State_Enabled |
@@ -118,27 +127,16 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
                                              QStyle::State_Item);
     }
 
-    if (isCurrent()) {
-        QStyleOptionViewItemV4 viewItemOption;
-        viewItemOption.initFrom(widget);
-
-        const QRect iconBounds = iconRect().toRect();
-        const QRect textBounds = textRect().toRect();
-        if (iconBounds.bottom() >= textBounds.top()) {
-            viewItemOption.rect = textBounds;
-        } else {
-            // See KItemListWidget::drawItemStyleOption(): The selection rectangle
-            // gets decreased.
-            viewItemOption.rect = textBounds.adjusted(1, 1, -1, -1);
-        }
-
-        viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
+    if (m_current && m_editedRole.isEmpty()) {
+        QStyleOptionFocusRect focusRectOption;
+        focusRectOption.initFrom(widget);
+        focusRectOption.rect = textFocusRect().toRect();
+        focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
         if (m_selected) {
-            viewItemOption.state |= QStyle::State_Selected;
+            focusRectOption.state |= QStyle::State_Selected;
         }
 
-        viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
-        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget);
+        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
     }
 
     if (m_hoverOpacity > 0.0) {
@@ -167,7 +165,9 @@ void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
 {
     const QList<QByteArray> previousRoles = m_visibleRoles;
     m_visibleRoles = roles;
+
     visibleRolesChanged(roles, previousRoles);
+    update();
 }
 
 QList<QByteArray> KItemListWidget::visibleRoles() const
@@ -175,16 +175,20 @@ QList<QByteArray> KItemListWidget::visibleRoles() const
     return m_visibleRoles;
 }
 
-void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
+
+void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
 {
-    const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
-    m_visibleRolesSizes = rolesSizes;
-    visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
+    const qreal previousWidth = m_columnWidths.value(role);
+    if (previousWidth != width) {
+        m_columnWidths.insert(role, width);
+        columnWidthChanged(role, width, previousWidth);
+        update();
+    }
 }
 
-QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
+qreal KItemListWidget::columnWidth(const QByteArray& role) const
 {
-    return m_visibleRolesSizes;
+    return m_columnWidths.value(role);
 }
 
 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
@@ -194,6 +198,7 @@ void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
     m_styleOption = option;
 
     styleOptionChanged(option, previous);
+    update();
 }
 
 const KItemListStyleOption& KItemListWidget::styleOption() const
@@ -208,7 +213,6 @@ void KItemListWidget::setSelected(bool selected)
         if (m_selectionToggle) {
             m_selectionToggle->setChecked(selected);
         }
-
         selectedChanged(selected);
         update();
     }
@@ -223,7 +227,6 @@ void KItemListWidget::setCurrent(bool current)
 {
     if (m_current != current) {
         m_current = current;
-
         currentChanged(current);
         update();
     }
@@ -244,8 +247,9 @@ void KItemListWidget::setHovered(bool hovered)
 
     if (!m_hoverAnimation) {
         m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
-        m_hoverAnimation->setDuration(200);
-        connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
+        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();
 
@@ -264,7 +268,6 @@ void KItemListWidget::setHovered(bool hovered)
     m_hoverAnimation->start();
 
     hoveredChanged(hovered);
-
     update();
 }
 
@@ -273,18 +276,25 @@ bool KItemListWidget::isHovered() const
     return m_hovered;
 }
 
-void KItemListWidget::setAlternatingBackgroundColors(bool enable)
+void KItemListWidget::setHoverPosition(const QPointF& pos)
 {
-    if (m_alternatingBackgroundColors != enable) {
-        m_alternatingBackgroundColors = enable;
-        alternatingBackgroundColorsChanged(enable);
+    if (m_selectionToggle) {
+        m_selectionToggle->setHovered(selectionToggleRect().contains(pos));
+    }
+}
+
+void KItemListWidget::setAlternateBackground(bool enable)
+{
+    if (m_alternateBackground != enable) {
+        m_alternateBackground = enable;
+        alternateBackgroundChanged(enable);
         update();
     }
 }
 
-bool KItemListWidget::alternatingBackgroundColors() const
+bool KItemListWidget::alternateBackground() const
 {
-    return m_alternatingBackgroundColors;
+    return m_alternateBackground;
 }
 
 void KItemListWidget::setEnabledSelectionToggle(bool enable)
@@ -300,6 +310,33 @@ bool KItemListWidget::enabledSelectionToggle() const
     return m_enabledSelectionToggle;
 }
 
+void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
+{
+    const QBitArray previous = m_siblingsInfo;
+    m_siblingsInfo = siblings;
+    siblingsInformationChanged(m_siblingsInfo, previous);
+    update();
+}
+
+QBitArray KItemListWidget::siblingsInformation() const
+{
+    return m_siblingsInfo;
+}
+
+void KItemListWidget::setEditedRole(const QByteArray& role)
+{
+    if (m_editedRole != role) {
+        const QByteArray previous = m_editedRole;
+        m_editedRole = role;
+        editedRoleChanged(role, previous);
+    }
+}
+
+QByteArray KItemListWidget::editedRole() const
+{
+    return m_editedRole;
+}
+
 bool KItemListWidget::contains(const QPointF& point) const
 {
     if (!QGraphicsWidget::contains(point)) {
@@ -312,6 +349,11 @@ bool KItemListWidget::contains(const QPointF& point) const
            selectionToggleRect().contains(point);
 }
 
+QRectF KItemListWidget::textFocusRect() const
+{
+    return textRect();
+}
+
 QRectF KItemListWidget::selectionToggleRect() const
 {
     return QRectF();
@@ -322,12 +364,37 @@ QRectF KItemListWidget::expansionToggleRect() const
     return QRectF();
 }
 
+QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
+                                          QWidget* widget)
+{
+    QPixmap pixmap(size().toSize() * widget->devicePixelRatio());
+    pixmap.setDevicePixelRatio(widget->devicePixelRatio());
+    pixmap.fill(Qt::transparent);
+
+    QPainter painter(&pixmap);
+
+    const bool oldAlternateBackground = m_alternateBackground;
+    const bool wasSelected = m_selected;
+    const bool wasHovered = m_hovered;
+
+    setAlternateBackground(false);
+    setSelected(false);
+    setHovered(false);
+
+    paint(&painter, option, widget);
+
+    setAlternateBackground(oldAlternateBackground);
+    setSelected(wasSelected);
+    setHovered(wasHovered);
+
+    return pixmap;
+}
+
 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
                                   const QSet<QByteArray>& roles)
 {
     Q_UNUSED(current);
     Q_UNUSED(roles);
-    update();
 }
 
 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
@@ -335,15 +402,15 @@ void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
 {
     Q_UNUSED(current);
     Q_UNUSED(previous);
-    update();
 }
 
-void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
-                                               const QHash<QByteArray, QSizeF>& previous)
+void KItemListWidget::columnWidthChanged(const QByteArray& role,
+                                         qreal current,
+                                         qreal previous)
 {
+    Q_UNUSED(role);
     Q_UNUSED(current);
     Q_UNUSED(previous);
-    update();
 }
 
 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
@@ -351,7 +418,6 @@ void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
 {
     Q_UNUSED(current);
     Q_UNUSED(previous);
-    update();
 }
 
 void KItemListWidget::currentChanged(bool current)
@@ -369,15 +435,33 @@ void KItemListWidget::hoveredChanged(bool hovered)
     Q_UNUSED(hovered);
 }
 
-void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
+void KItemListWidget::alternateBackgroundChanged(bool enabled)
 {
     Q_UNUSED(enabled);
 }
 
+void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
+{
+    Q_UNUSED(current);
+    Q_UNUSED(previous);
+}
+
+void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
+{
+    Q_UNUSED(current);
+    Q_UNUSED(previous);
+}
+
 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
 {
     QGraphicsWidget::resizeEvent(event);
     clearHoverCache();
+
+    if (m_selectionToggle) {
+        const QRectF& toggleRect = selectionToggleRect();
+        m_selectionToggle->setPos(toggleRect.topLeft());
+        m_selectionToggle->resize(toggleRect.size());
+    }
 }
 
 qreal KItemListWidget::hoverOpacity() const
@@ -387,9 +471,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;
     }
 }
 
@@ -417,7 +501,7 @@ void KItemListWidget::setHoverOpacity(qreal opacity)
 
     if (m_hoverOpacity <= 0.0) {
         delete m_hoverCache;
-        m_hoverCache = 0;
+        m_hoverCache = nullptr;
     }
 
     update();
@@ -426,29 +510,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)
 {
-    const QRect iconBounds = iconRect().toRect();
-    const QRect textBounds = textRect().toRect();
-
-    QStyleOptionViewItemV4 viewItemOption;
+    QStyleOptionViewItem viewItemOption;
     viewItemOption.initFrom(widget);
     viewItemOption.state = styleState;
-    viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
-
-    if (iconBounds.bottom() >= textBounds.top()) {
-        viewItemOption.rect = iconBounds | textBounds;
-        widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
-    } else {
-        viewItemOption.rect = iconBounds;
-        widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
-
-        viewItemOption.rect = textBounds.adjusted(1, 1, -1, -1);
-        widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
-    }
+    viewItemOption.viewItemPosition = QStyleOptionViewItem::OnlyOne;
+    viewItemOption.showDecorationSelected = true;
+    viewItemOption.rect = selectionRect().toRect();
+    widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
 }
 
-#include "kitemlistwidget.moc"