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) 2026 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 "pagetabsmodel.h"
24 :
25 : using namespace muse::uicomponents;
26 :
27 0 : PageTabsModel::PageTabsModel(QObject* parent)
28 0 : : QAbstractListModel(parent)
29 : {
30 0 : }
31 :
32 0 : QVariant PageTabsModel::data(const QModelIndex& index, int role) const
33 : {
34 0 : if (!index.isValid() || index.row() >= rowCount()) {
35 0 : return QVariant();
36 : }
37 :
38 0 : const PageTab& tab = m_tabs.at(index.row());
39 0 : switch (role) {
40 0 : case TitleRole: return tab.title;
41 0 : case UriRole: return tab.uri;
42 0 : case IsTitleBoldRole: return tab.isTitleBold;
43 0 : case IsCheckedRole: return isChecked(tab);
44 : }
45 :
46 0 : return QVariant();
47 : }
48 :
49 0 : int PageTabsModel::rowCount(const QModelIndex&) const
50 : {
51 0 : return static_cast<int>(m_tabs.size());
52 : }
53 :
54 0 : QHash<int, QByteArray> PageTabsModel::roleNames() const
55 : {
56 : static const QHash<int, QByteArray> roles = {
57 0 : { TitleRole, "title" },
58 0 : { UriRole, "uri" },
59 0 : { IsTitleBoldRole, "isTitleBold" },
60 0 : { IsCheckedRole, "isChecked" },
61 0 : };
62 :
63 0 : return roles;
64 0 : }
65 :
66 0 : void PageTabsModel::load()
67 : {
68 0 : }
69 :
70 0 : QString PageTabsModel::currentUri() const
71 : {
72 0 : return m_currentUri;
73 : }
74 :
75 0 : void PageTabsModel::setCurrentUri(const QString& uri)
76 : {
77 0 : if (m_currentUri == uri) {
78 0 : return;
79 : }
80 :
81 0 : m_currentUri = uri;
82 0 : emit currentUriChanged();
83 :
84 0 : if (!m_tabs.isEmpty()) {
85 0 : emit dataChanged(index(0), index(rowCount() - 1), { IsCheckedRole });
86 : }
87 : }
88 :
89 0 : void PageTabsModel::setTabs(const QList<PageTab>& tabs)
90 : {
91 0 : beginResetModel();
92 0 : m_tabs = tabs;
93 0 : endResetModel();
94 0 : }
95 :
96 0 : void PageTabsModel::setTab(int index, const PageTab& tab)
97 : {
98 0 : if (index < 0 || index >= rowCount()) {
99 0 : return;
100 : }
101 :
102 0 : m_tabs[index] = tab;
103 :
104 0 : QModelIndex modelIndex = this->index(index);
105 0 : emit dataChanged(modelIndex, modelIndex);
106 : }
107 :
108 0 : bool PageTabsModel::isChecked(const PageTab& tab) const
109 : {
110 0 : return tab.uri == m_currentUri || tab.alsoCheckedOnUris.contains(m_currentUri);
111 : }
|