Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Fix active layer changing when ctrl-clicking

Luna Lovecraft8c0e77b4f3a0
diff --git a/plugins/dockers/layerdocker/NodeDelegate.cpp b/plugins/dockers/layerdocker/NodeDelegate.cpp
index b7171c08f9..b338c6e99e 100644
--- a/plugins/dockers/layerdocker/NodeDelegate.cpp
+++ b/plugins/dockers/layerdocker/NodeDelegate.cpp
@@ -1141,10 +1141,6 @@ bool NodeDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const Q
model->setData(index, true, KisNodeModel::AlternateActiveRole);
return true;
- } else if (mouseEvent->modifiers() == Qt::ControlModifier) {
- //Don't do anything here when ctrl is pressed but still pass through the event
- //If we don't, the internal state of QTreeView WILL break
- return false;
}
return false;
}
diff --git a/plugins/dockers/layerdocker/NodeView.cpp b/plugins/dockers/layerdocker/NodeView.cpp
index 6dc04ee015..8b329d481b 100644
--- a/plugins/dockers/layerdocker/NodeView.cpp
+++ b/plugins/dockers/layerdocker/NodeView.cpp
@@ -73,6 +73,7 @@ public:
#ifdef QT6_SHIFT_SELECTION_WORKAROUND
bool shiftClickFix;
#endif
+ bool ignoreDataChanged = false;
};
@@ -230,7 +231,12 @@ bool NodeView::viewportEvent(QEvent *e)
return QTreeView::viewportEvent(e);
}
if (d->delegate.editorEvent(e, model(), optionForIndex(index), index)) {
- return true;
+ //Sometimes 2 items become active, to prevent that we explicitly turn off all but the first active item
+ std::optional<QModelIndex> active_ind = getActiveItem();
+ if (active_ind.has_value()) {
+ setExclusiveActiveItem(active_ind.value());
+ }
+ return true;
}
} break;
case QEvent::Leave: {
@@ -283,6 +289,7 @@ bool NodeView::viewportEvent(QEvent *e)
default: break;
}
}
+
return QTreeView::viewportEvent(e);
}
@@ -314,6 +321,11 @@ void NodeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottom
{
QTreeView::dataChanged(topLeft, bottomRight);
+ if (d->ignoreDataChanged){
+ d->ignoreDataChanged = false;
+ return;
+ }
+
for (int x = topLeft.row(); x <= bottomRight.row(); ++x) {
for (int y = topLeft.column(); y <= bottomRight.column(); ++y) {
QModelIndex index = topLeft.sibling(x, y);
@@ -508,6 +520,20 @@ void NodeView::dragLeaveEvent(QDragLeaveEvent *e)
DRAG_WHILE_DRAG_WORKAROUND_STOP();
}
+void NodeView::mousePressEvent(QMouseEvent *e) {
+ if (!(e->modifiers() & Qt::ControlModifier)) {
+ QTreeView::mousePressEvent(e);
+ return;
+ }
+
+ std::optional<QModelIndex> ind = getActiveItem();
+ QTreeView::mousePressEvent(e);
+
+ if (ind.has_value()) {
+ setExclusiveActiveItem(ind.value());
+ }
+}
+
bool NodeView::isDragging() const
{
return m_draggingFlag;
@@ -547,3 +573,43 @@ void NodeView::updateSelectedCheckboxColumn()
+ (cfg.useLayerSelectionCheckbox() ? header()->sectionSize(SELECTED_COL)
: -header()->sectionSize(SELECTED_COL)));
}
+
+std::optional<QModelIndex> NodeView::getActiveItem()
+{
+ QAbstractItemModel* mdl = model();
+
+ int rows = mdl->rowCount();
+ int clmns = mdl->columnCount();
+
+ for (int i = 0; i < rows; i++) {
+ for (int j = 0; j < clmns; j++) {
+ auto index = mdl->index(i, j);
+ if (mdl->data(index, KisNodeModel::ActiveRole).toBool()){
+ return index;
+ }
+ }
+ }
+
+ return {};
+}
+
+void NodeView::setExclusiveActiveItem(QModelIndex index)
+{
+ QAbstractItemModel* mdl = model();
+
+ int rows = mdl->rowCount();
+ int clmns = mdl->columnCount();
+
+ for (int i = 0; i < rows; i++) {
+ for (int j = 0; j < clmns; j++) {
+ QModelIndex ind = mdl->index(i, j);
+
+ //Suppress the data changed event, so that it doesn't mess with the selection
+ d->ignoreDataChanged = true;
+ mdl->setData(ind, false, KisNodeModel::ActiveRole);
+ }
+ }
+
+ d->ignoreDataChanged = true;
+ mdl->setData(index, true, KisNodeModel::ActiveRole);
+}
diff --git a/plugins/dockers/layerdocker/NodeView.h b/plugins/dockers/layerdocker/NodeView.h
index 1ccd93ba30..145206fdb7 100644
--- a/plugins/dockers/layerdocker/NodeView.h
+++ b/plugins/dockers/layerdocker/NodeView.h
@@ -9,6 +9,7 @@
#include <QTreeView>
#include <QScroller>
+#include <optional>
#include "kritalayerdocker_export.h"
@@ -63,6 +64,7 @@ public:
void dragMoveEvent(QDragMoveEvent *ev) override;
void dragLeaveEvent(QDragLeaveEvent *e) override;
+ void mousePressEvent(QMouseEvent *e) override;
/**
* Add toggle actions for all the properties associated with the
@@ -143,6 +145,9 @@ private:
void updateSelectedCheckboxColumn();
+ std::optional<QModelIndex> getActiveItem();
+ void setExclusiveActiveItem(QModelIndex index);
+
bool m_draggingFlag;
QStyleOptionViewItem optionForIndex(const QModelIndex &index) const;