1
0
mirror of https://invent.kde.org/system/dolphin synced 2024-07-07 10:51:45 +00:00

Add focusOut and focusIn event support for KFileItemListView and update selected widget on focus change

This commit is contained in:
Méven Car 2023-09-02 11:30:25 +02:00
parent 012f8a224a
commit fae5fec964
10 changed files with 82 additions and 9 deletions

View 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);

View 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;

View 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();

View 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;

View 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)

View 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);

View 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())) {

View 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;
}

View 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);

View 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);