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) 2025 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 "docktabsmodel.h"
24 :
25 : #include "kddockwidgets/src/core/DockWidget.h"
26 : #include "kddockwidgets/src/qtquick/views/DockWidget.h"
27 : #include "kddockwidgets/src/qtquick/views/View.h"
28 :
29 : #include "docktypes.h"
30 :
31 : using namespace muse::dock;
32 :
33 0 : DockTabsModel::DockTabsModel(QObject* parent)
34 0 : : QAbstractListModel{parent}
35 : {
36 0 : }
37 :
38 0 : QVariant DockTabsModel::data(const QModelIndex& index, int role) const
39 : {
40 0 : if (!index.isValid() || index.row() >= rowCount()) {
41 0 : return QVariant();
42 : }
43 :
44 0 : const DockTab& tab = m_dockTabs.at(index.row());
45 :
46 0 : switch (role) {
47 0 : case Title: return tab.title;
48 0 : case ContextMenu: return tab.contextMenu;
49 0 : case ToolBarComponent: return tab.toolBarComponent;
50 : }
51 :
52 0 : return QVariant();
53 : }
54 :
55 0 : QHash<int, QByteArray> DockTabsModel::roleNames() const
56 : {
57 : static const QHash<int, QByteArray> roles = {
58 0 : { Title, "title" },
59 0 : { ContextMenu, "contextMenu" },
60 0 : { ToolBarComponent, "toolBarComponent" },
61 0 : };
62 0 : return roles;
63 0 : }
64 :
65 0 : void DockTabsModel::init(const QVector<KDDockWidgets::Core::DockWidget*>& widgets)
66 : {
67 0 : beginResetModel();
68 :
69 0 : m_dockTabs.clear();
70 :
71 0 : for (const KDDockWidgets::Core::DockWidget* dock : widgets) {
72 0 : const auto* view = qobject_cast<const KDDockWidgets::QtQuick::DockWidget*>(
73 0 : KDDockWidgets::QtQuick::asQQuickItem(const_cast<KDDockWidgets::Core::DockWidget*>(dock)));
74 0 : DockTab tab;
75 0 : tab.title = dock->title();
76 0 : tab.contextMenu = view ? view->property(CONTEXT_MENU_MODEL_PROPERTY) : QVariant();
77 0 : tab.toolBarComponent = view ? view->property(TOOLBAR_COMPONENT_PROPERTY) : QVariant();
78 0 : m_dockTabs.emplaceBack(tab);
79 0 : }
80 :
81 0 : endResetModel();
82 0 : emit numTabsChanged();
83 0 : }
84 :
85 0 : void DockTabsModel::toolBarComponentChanged(const KDDockWidgets::QtQuick::DockWidget* dock)
86 : {
87 0 : const int idx = indexOfDock(dock->title());
88 0 : if (idx < 0 || idx >= m_dockTabs.size()) {
89 0 : return;
90 : }
91 :
92 0 : m_dockTabs[idx].toolBarComponent = dock->property(TOOLBAR_COMPONENT_PROPERTY);
93 :
94 0 : const QModelIndex qIdx = index(idx, 0);
95 0 : emit dataChanged(qIdx, qIdx, { ToolBarComponent });
96 : }
97 :
98 0 : void DockTabsModel::contextMenuChanged(const KDDockWidgets::QtQuick::DockWidget* dock)
99 : {
100 0 : const int idx = indexOfDock(dock->title());
101 0 : if (idx < 0 || idx >= m_dockTabs.size()) {
102 0 : return;
103 : }
104 :
105 0 : m_dockTabs[idx].contextMenu = dock->property(CONTEXT_MENU_MODEL_PROPERTY);
106 :
107 0 : const QModelIndex qIdx = index(idx, 0);
108 0 : emit dataChanged(qIdx, qIdx, { ContextMenu });
109 : }
110 :
111 0 : int DockTabsModel::indexOfDock(const QString& title)
112 : {
113 0 : for (int i = 0; i < m_dockTabs.size(); ++i) {
114 0 : const DockTab& tab = m_dockTabs.at(i);
115 0 : if (tab.title == title) {
116 0 : return i;
117 : }
118 : }
119 0 : return -1;
120 : }
|