Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "itemmultiselectionmodel.h"
24 :
25 : #include <QGuiApplication>
26 :
27 : using namespace muse::uicomponents;
28 :
29 0 : ItemMultiSelectionModel::ItemMultiSelectionModel(QAbstractItemModel* parent)
30 0 : : QItemSelectionModel(parent)
31 : {
32 0 : setSingleItemSelectionMode(false);
33 0 : }
34 :
35 0 : void ItemMultiSelectionModel::setAllowedModifiers(Qt::KeyboardModifiers modifiers)
36 : {
37 0 : m_allowedModifiers = modifiers;
38 0 : }
39 :
40 0 : void ItemMultiSelectionModel::setSingleItemSelectionMode(bool on)
41 : {
42 0 : if (on) {
43 0 : m_allowedModifiers = {};
44 : } else {
45 0 : m_allowedModifiers = (Qt::ShiftModifier | Qt::ControlModifier);
46 : }
47 0 : }
48 :
49 0 : QList<int> ItemMultiSelectionModel::selectedRows() const
50 : {
51 0 : QList<int> rows;
52 :
53 0 : for (const QModelIndex& index : selectedIndexes()) {
54 0 : rows << index.row();
55 0 : }
56 :
57 0 : return rows;
58 0 : }
59 :
60 0 : void ItemMultiSelectionModel::select(const QModelIndex& index)
61 : {
62 0 : Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
63 :
64 : //! NOTE: always treat simultaneously pressed Ctrl and Shift as Ctrl
65 0 : if (modifiers.testFlag(Qt::ShiftModifier) && modifiers.testFlag(Qt::ControlModifier)) {
66 0 : modifiers = Qt::ControlModifier;
67 : }
68 :
69 0 : bool modifiersAllowed = m_allowedModifiers & modifiers;
70 0 : QModelIndex startIndex = index;
71 :
72 0 : if (modifiersAllowed) {
73 0 : if (modifiers == Qt::ShiftModifier && hasSelection()) {
74 0 : startIndex = selectedIndexes().last();
75 0 : } else if (modifiers == Qt::ControlModifier && isSelected(index)) {
76 0 : QItemSelectionModel::select(index, SelectionFlag::Deselect);
77 0 : return;
78 : }
79 : }
80 :
81 0 : QSet<QModelIndex> uniqueIndexes;
82 0 : bool needClearSelection = !modifiersAllowed;
83 :
84 0 : if (needClearSelection) {
85 0 : uniqueIndexes << index;
86 : } else {
87 0 : QItemSelection selection = this->selection();
88 0 : selection.select(startIndex, index);
89 0 : QModelIndexList indexes = selection.indexes();
90 0 : uniqueIndexes = QSet<QModelIndex>(indexes.begin(), indexes.end());
91 0 : }
92 :
93 0 : QItemSelection newSelection;
94 0 : for (const QModelIndex& selectedIndex : uniqueIndexes) {
95 0 : newSelection.select(selectedIndex, selectedIndex);
96 : }
97 :
98 0 : QItemSelectionModel::select(newSelection, SelectionFlag::ClearAndSelect);
99 0 : }
|