]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Add focusOut and focusIn event support for KFileItemListView and update selected...
authorMéven Car <meven@kde.org>
Sat, 2 Sep 2023 09:30:25 +0000 (11:30 +0200)
committerMéven Car <meven@kde.org>
Sat, 2 Sep 2023 12:42:54 +0000 (14:42 +0200)
src/kitemviews/kfileitemlistview.cpp
src/kitemviews/kfileitemlistview.h
src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemlistwidget.h
src/kitemviews/kitemlistcontainer.cpp
src/kitemviews/kitemlistcontainer.h
src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistwidget.cpp
src/kitemviews/kstandarditemlistwidget.cpp
src/kitemviews/kstandarditemlistwidget.h

index 2ff5c9fb93ef3eb77058c28f2350549ae62ec6d0..954e6ab881ada04f009d328659808884d0920c8d 100644 (file)
@@ -326,6 +326,31 @@ void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent *event)
     triggerVisibleIndexRangeUpdate();
 }
 
+void KFileItemListView::focusInEvent(QFocusEvent *event)
+{
+    Q_UNUSED(event)
+    updateSelectedWidgets();
+}
+
+void KFileItemListView::focusOutEvent(QFocusEvent *event)
+{
+    Q_UNUSED(event)
+    updateSelectedWidgets();
+}
+
+void KFileItemListView::updateSelectedWidgets()
+{
+    const auto visibleWidgets = visibleItemListWidgets();
+    for (KItemListWidget *widget : visibleWidgets) {
+        if (widget->isSelected()) {
+            auto w = qobject_cast<KFileItemListWidget *>(widget);
+            if (w) {
+                w->forceUpdate();
+            }
+        }
+    }
+}
+
 void KFileItemListView::slotItemsRemoved(const KItemRangeList &itemRanges)
 {
     KStandardItemListView::slotItemsRemoved(itemRanges);
index b4be0093e7d2d675351b8c49805280639911d855..4c48c52ab23def40302410ae52947635ff9fb13a 100644 (file)
@@ -98,6 +98,8 @@ protected:
     void onTransactionBegin() override;
     void onTransactionEnd() override;
     void resizeEvent(QGraphicsSceneResizeEvent *event) override;
+    void focusInEvent(QFocusEvent *event) override;
+    void focusOutEvent(QFocusEvent *event) override;
 
 protected Q_SLOTS:
     void slotItemsRemoved(const KItemRangeList &itemRanges) override;
@@ -127,6 +129,8 @@ private:
     QSize availableIconSize() const;
 
 private:
+    void updateSelectedWidgets();
+
     KFileItemModelRolesUpdater *m_modelRolesUpdater;
     QTimer *m_updateVisibleIndexRangeTimer;
     QTimer *m_updateIconSizeTimer;
index 771403b7018049cfc37bcf6540d98bfeb4600cfc..9802ca7d1607c98e14ba5404d690c2011ca9092d 100644 (file)
@@ -203,6 +203,17 @@ void KFileItemListWidget::hoverSequenceStarted()
     view->setHoverSequenceState(itemUrl, 0);
 }
 
+void KFileItemListWidget::forceUpdate()
+{
+    updateAdditionalInfoTextColor();
+    // icon layout does not include the icons in the item selection rectangle
+    // so its icon does not need updating
+    if (listView()->itemLayout() != KStandardItemListView::ItemLayout::IconsLayout) {
+        invalidateIconCache();
+    }
+    update();
+}
+
 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex)
 {
     KFileItemListView *view = listView();
index 27fcf7f29d24207e9f49147499ec49effb9d377b..7f33c548700637a9fd798e71b2f3a56ddd6d920d 100644 (file)
@@ -35,6 +35,9 @@ public:
 
     static KItemListWidgetInformant *createInformant();
 
+    /// Force-update the displayed icon
+    void forceUpdate();
+
 protected:
     virtual void hoverSequenceStarted() override;
     virtual void hoverSequenceIndexChanged(int sequenceIndex) override;
index 3893ceaea0c25cf7785f0d776769f8e097885c89..f89a6c8ce3eea3a18b199bb09783b2582f62c8b2 100644 (file)
@@ -175,6 +175,22 @@ void KItemListContainer::wheelEvent(QWheelEvent *event)
     smoothScroller->handleWheelEvent(event);
 }
 
+void KItemListContainer::focusInEvent(QFocusEvent *event)
+{
+    KItemListView *view = m_controller->view();
+    if (view) {
+        QApplication::sendEvent(view, event);
+    }
+}
+
+void KItemListContainer::focusOutEvent(QFocusEvent *event)
+{
+    KItemListView *view = m_controller->view();
+    if (view) {
+        QApplication::sendEvent(view, event);
+    }
+}
+
 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
 {
     Q_UNUSED(previous)
index 6bb28469e9ca35227dcb16d508b12a88404afe31..40b0753764dcbeaec5038adbd53fe90b396537d3 100644 (file)
@@ -50,6 +50,8 @@ protected:
     void resizeEvent(QResizeEvent *event) override;
     void scrollContentsBy(int dx, int dy) override;
     void wheelEvent(QWheelEvent *event) override;
+    void focusInEvent(QFocusEvent *event) override;
+    void focusOutEvent(QFocusEvent *event) override;
 
 private Q_SLOTS:
     void slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous);
index 7a3cbbcb6a7eb7cf75ceca588611b1023695eaa7..5363fa253e9e74d186676a9c3d4030412c1235bc 100644 (file)
@@ -961,6 +961,18 @@ bool KItemListView::event(QEvent *event)
         updateFont();
         break;
 
+    case QEvent::FocusIn:
+        focusInEvent(static_cast<QFocusEvent *>(event));
+        event->accept();
+        return true;
+        break;
+
+    case QEvent::FocusOut:
+        focusOutEvent(static_cast<QFocusEvent *>(event));
+        event->accept();
+        return true;
+        break;
+
     default:
         // Forward all other events to the controller and handle them there
         if (!m_editingRole && m_controller && m_controller->processEvent(event, transform())) {
index cc3cf0dfa69813177ee4c81faa30c6c7a1a0f793..cfaf89175260a80670b57da4ad1a150dfe5aa4ef 100644 (file)
@@ -115,12 +115,12 @@ void KItemListWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
         drawItemStyleOption(painter, widget, activeState | QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item);
     }
 
-    if (m_current && m_editedRole.isEmpty() && widget->hasFocus()) {
+    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) {
+        if (m_selected && widget->hasFocus()) {
             focusRectOption.state |= QStyle::State_Selected;
         }
 
index 24a9864193640488c6052bdbf2b42ed9852493fd..01b135038d3d1707f3a6f1f9c896f832c0cc6be4 100644 (file)
@@ -386,7 +386,7 @@ void KStandardItemListWidget::paint(QPainter *painter, const QStyleOptionGraphic
     }
 
     painter->setFont(m_customizedFont);
-    painter->setPen(textColor(widget));
+    painter->setPen(textColor(*widget));
     const TextInfo *textInfo = m_textInfo.value("text");
 
     if (!textInfo) {
@@ -645,7 +645,7 @@ void KStandardItemListWidget::setTextColor(const QColor &color)
     }
 }
 
-QColor KStandardItemListWidget::textColor(QWidget *widget) const
+QColor KStandardItemListWidget::textColor(const QWidget &widget) const
 {
     if (!isSelected()) {
         if (m_isHidden) {
@@ -655,7 +655,7 @@ QColor KStandardItemListWidget::textColor(QWidget *widget) const
         }
     }
 
-    const QPalette::ColorGroup group = isActiveWindow() && widget->hasFocus() ? QPalette::Active : QPalette::Inactive;
+    const QPalette::ColorGroup group = isActiveWindow() && widget.hasFocus() ? QPalette::Active : QPalette::Inactive;
     const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : normalTextColorRole();
     return styleOption().palette.color(group, role);
 }
@@ -1436,9 +1436,10 @@ void KStandardItemListWidget::updateDetailsLayoutTextCache()
 void KStandardItemListWidget::updateAdditionalInfoTextColor()
 {
     QColor c1;
+    const bool hasFocus = scene()->views()[0]->parentWidget()->hasFocus();
     if (m_customTextColor.isValid()) {
         c1 = m_customTextColor;
-    } else if (isSelected() && (m_layout != DetailsLayout || m_highlightEntireRow)) {
+    } else if (isSelected() && hasFocus && (m_layout != DetailsLayout || m_highlightEntireRow)) {
         // The detail text colour needs to match the main text (HighlightedText) for the same level
         // of readability. We short circuit early here to avoid interpolating with another colour.
         m_additionalInfoTextColor = styleOption().palette.color(QPalette::HighlightedText);
index d146a02fb9b8ffa96cc0aeaf3d24a186232cc236..c4f80af1ddbdc1b35f1211d87c24e748254128f4 100644 (file)
@@ -139,7 +139,7 @@ protected:
     virtual QPalette::ColorRole normalTextColorRole() const;
 
     void setTextColor(const QColor &color);
-    QColor textColor(QWidget *widget) const;
+    QColor textColor(const QWidget &widget) const;
 
     void setOverlay(const QPixmap &overlay);
     QPixmap overlay() const;
@@ -181,6 +181,7 @@ protected:
         QPointF pos;
         QStaticText staticText;
     };
+    void updateAdditionalInfoTextColor();
 
 public Q_SLOTS:
     void finishRoleEditing();
@@ -200,8 +201,6 @@ private:
     void updateCompactLayoutTextCache();
     void updateDetailsLayoutTextCache();
 
-    void updateAdditionalInfoTextColor();
-
     void drawPixmap(QPainter *painter, const QPixmap &pixmap);
     void drawSiblingsInformation(QPainter *painter);