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) 2024 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 : #include "actionsviewmodel.h"
23 :
24 : using namespace muse::diagnostics;
25 : using namespace muse::actions;
26 : using namespace muse::ui;
27 :
28 0 : ActionsViewModel::ActionsViewModel(QObject* parent)
29 0 : : QAbstractListModel(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
30 : {
31 0 : }
32 :
33 : template<typename T>
34 0 : static std::list<T> toList(const std::vector<T>& vec)
35 : {
36 0 : std::list<T> l;
37 0 : for (const T& v : vec) {
38 0 : l.push_back(v);
39 : }
40 0 : return l;
41 0 : }
42 :
43 0 : void ActionsViewModel::load()
44 : {
45 0 : ActionCodeList actions = actionsDispatcher()->actionList();
46 0 : std::list<UiAction> uiactions = toList(uiActionsRegister()->actionList());
47 :
48 0 : auto takeByCode = [](std::list<UiAction>& uia, const ActionCode& code) -> UiAction {
49 0 : auto it = std::find_if(uia.begin(), uia.end(), [code](const UiAction& a) {
50 0 : return a.code == code;
51 : });
52 :
53 0 : if (it == uia.end()) {
54 0 : return UiAction();
55 : }
56 :
57 0 : UiAction a = *it;
58 0 : uia.erase(it);
59 0 : return a;
60 0 : };
61 :
62 0 : beginResetModel();
63 :
64 0 : for (const ActionCode& a : actions) {
65 0 : Item item;
66 0 : item.isReg = true;
67 0 : item.actionCode = QString::fromStdString(a);
68 :
69 0 : UiAction uia = takeByCode(uiactions, a);
70 0 : item.isHasUi = uia.isValid();
71 0 : if (item.isHasUi) {
72 0 : item.actionTitle = uia.title.qTranslatedWithoutMnemonic();
73 : } else {
74 0 : item.actionTitle = "none";
75 : }
76 :
77 0 : m_allItems.append(item);
78 0 : }
79 :
80 0 : for (const UiAction& uia : uiactions) {
81 0 : Item item;
82 0 : item.isReg = false;
83 0 : item.actionCode = QString::fromStdString(uia.code);
84 0 : item.isHasUi = true;
85 0 : item.actionTitle = uia.title.qTranslatedWithoutMnemonic();
86 0 : m_allItems.append(item);
87 0 : }
88 :
89 0 : for (Item& item : m_allItems) {
90 0 : item.formatted = QString("action: %1, isreg: %2, title: %3, ishasui: %4")
91 0 : .arg(item.actionCode).arg(item.isReg).arg(item.actionTitle).arg(item.isHasUi);
92 : }
93 :
94 0 : std::sort(m_allItems.begin(), m_allItems.end(), [](const Item& f, const Item& s) {
95 0 : return f.actionCode < s.actionCode;
96 : });
97 :
98 0 : m_items = m_allItems;
99 :
100 0 : endResetModel();
101 0 : }
102 :
103 0 : QVariant ActionsViewModel::data(const QModelIndex& index, int role) const
104 : {
105 0 : if (!index.isValid()) {
106 0 : return QVariant();
107 : }
108 :
109 0 : switch (role) {
110 0 : case rItemData: return QString::number(index.row()) + ". " + m_items.at(index.row()).formatted;
111 : }
112 :
113 0 : return QVariant();
114 : }
115 :
116 0 : int ActionsViewModel::rowCount(const QModelIndex&) const
117 : {
118 0 : return m_items.count();
119 : }
120 :
121 0 : QHash<int, QByteArray> ActionsViewModel::roleNames() const
122 : {
123 0 : return { { rItemData, "itemData" } };
124 0 : }
125 :
126 0 : void ActionsViewModel::find(const QString& str)
127 : {
128 0 : beginResetModel();
129 :
130 0 : m_searchText = str;
131 :
132 0 : if (m_searchText.isEmpty()) {
133 0 : m_items = m_allItems;
134 : } else {
135 0 : m_items.clear();
136 0 : for (const Item& item : m_allItems) {
137 0 : if (item.formatted.contains(m_searchText, Qt::CaseInsensitive)) {
138 0 : m_items.append(item);
139 : }
140 : }
141 : }
142 :
143 0 : endResetModel();
144 0 : }
145 :
146 0 : void ActionsViewModel::print()
147 : {
148 0 : for (const Item& item : m_items) {
149 0 : std::cout << item.formatted.toStdString() << std::endl;
150 : }
151 0 : }
|