1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2018 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12
13 #include "scorelistmodel.h"
14
15 #include "libmscore/score.h"
16 #include "scoretab.h"
17 #include "scoreview.h"
18
19 namespace Ms {
20
21 constexpr ScoreVersionIndex ScoreVersion::INDEX_CURRENT;
22 constexpr ScoreVersionIndex ScoreVersion::INDEX_LAST_SAVED;
23 constexpr ScoreVersionIndex ScoreVersion::INDEX_SESSION_START;
24
25 //---------------------------------------------------------
26 // ScoreListModel::ScoreListModel
27 //---------------------------------------------------------
28
ScoreListModel(const QList<MasterScore * > * scoreList,ScoreTab * tab,QObject * parent)29 ScoreListModel::ScoreListModel(const QList<MasterScore*>* scoreList, ScoreTab* tab, QObject* parent)
30 : QAbstractListModel(parent), _scoreList(scoreList), _tab(tab)
31 {
32 // Need to be synchronized with ScoreListModel::data()
33 _usedRoles.push_back(Qt::DisplayRole);
34
35 connect(tab, &ScoreTab::tabInserted, this, &ScoreListModel::tabInserted);
36 connect(tab, &ScoreTab::tabRemoved, this, &ScoreListModel::tabRemoved);
37 connect(tab, &ScoreTab::tabRenamed, this, &ScoreListModel::tabRenamed);
38 connect(tab->getTab(), &QTabBar::tabMoved, this, &ScoreListModel::tabMoved);
39 }
40
41 //---------------------------------------------------------
42 // ScoreListModel::rowCount
43 //---------------------------------------------------------
44
rowCount(const QModelIndex & parent) const45 int ScoreListModel::rowCount(const QModelIndex& parent) const
46 {
47 if (parent.isValid())
48 return 0;
49 return _scoreList->size();
50 }
51
52 //---------------------------------------------------------
53 // ScoreListModel::data
54 //---------------------------------------------------------
55
data(const QModelIndex & index,int role) const56 QVariant ScoreListModel::data(const QModelIndex& index, int role) const
57 {
58 const int row = index.row();
59 if (row < 0 || row >= _scoreList->size())
60 return QVariant();
61
62 switch(role) {
63 case Qt::DisplayRole:
64 return _tab->getTab()->tabText(row);
65 default:
66 break;
67 }
68 return QVariant();
69 }
70
71 //---------------------------------------------------------
72 // ScoreListModel::getScore
73 //---------------------------------------------------------
74
getScore(int idx) const75 Score* ScoreListModel::getScore(int idx) const
76 {
77 if (idx < 0 || idx >= _scoreList->size())
78 return nullptr;
79 return (*_scoreList)[idx];
80 }
81
82 //---------------------------------------------------------
83 // ScoreListModel::tabInserted
84 //---------------------------------------------------------
85
tabInserted(int idx)86 void ScoreListModel::tabInserted(int idx)
87 {
88 emit dataChanged(index(idx, 0), index(_scoreList->size() - 1, 0), _usedRoles);
89 }
90
91 //---------------------------------------------------------
92 // ScoreListModel::tabRemoved
93 //---------------------------------------------------------
94
tabRemoved(int idx)95 void ScoreListModel::tabRemoved(int idx)
96 {
97 emit dataChanged(index(idx, 0), index(_scoreList->size(), 0), _usedRoles);
98 }
99
100 //---------------------------------------------------------
101 // ScoreListModel::tabRenamed
102 //---------------------------------------------------------
103
tabRenamed(int idx)104 void ScoreListModel::tabRenamed(int idx)
105 {
106 QModelIndex tabIndex(index(idx, 0));
107 emit dataChanged(tabIndex, tabIndex, _usedRoles);
108 }
109
110 //---------------------------------------------------------
111 // ScoreListModel::tabMoved
112 //---------------------------------------------------------
113
tabMoved(int from,int to)114 void ScoreListModel::tabMoved(int from, int to)
115 {
116 emit dataChanged(index(qMin(from, to), 0), index(qMax(from, to), 0), _usedRoles);
117 }
118
119 //---------------------------------------------------------
120 // ScoreVersionListModel::ScoreVersionListModel
121 //---------------------------------------------------------
122
ScoreVersionListModel(MasterScore * score,QObject * parent)123 ScoreVersionListModel::ScoreVersionListModel(MasterScore* score, QObject* parent)
124 : QAbstractListModel(parent), _score(score)
125 {
126 update();
127 }
128
129 //---------------------------------------------------------
130 // ScoreVersionListModel::update
131 //---------------------------------------------------------
132
update()133 void ScoreVersionListModel::update()
134 {
135 beginResetModel();
136 _versions.clear();
137 if (_score) {
138 _versions.emplace_back(_score, tr("Current version"), ScoreVersion::INDEX_CURRENT, /* recent */ true);
139 if (_score->fileInfo() && _score->fileInfo()->isReadable())
140 _versions.emplace_back(_score, tr("Last saved version"), ScoreVersion::INDEX_LAST_SAVED, *_score->fileInfo(), false);
141 if (_score->sessionStartBackupInfo().isReadable())
142 _versions.emplace_back(_score, tr("Session start"), ScoreVersion::INDEX_SESSION_START, _score->sessionStartBackupInfo(), false);
143 }
144 endResetModel();
145 }
146
147 //---------------------------------------------------------
148 // ScoreVersionListModel::setScore
149 //---------------------------------------------------------
150
setScore(MasterScore * s)151 void ScoreVersionListModel::setScore(MasterScore* s)
152 {
153 _score = s;
154 update();
155 }
156
157 //---------------------------------------------------------
158 // ScoreVersionListModel::rowCount
159 //---------------------------------------------------------
160
rowCount(const QModelIndex & parent) const161 int ScoreVersionListModel::rowCount(const QModelIndex& parent) const
162 {
163 if (parent.isValid())
164 return 0;
165 return int(_versions.size());
166 }
167
168 //---------------------------------------------------------
169 // ScoreVersionListModel::data
170 //---------------------------------------------------------
171
data(const QModelIndex & index,int role) const172 QVariant ScoreVersionListModel::data(const QModelIndex& index, int role) const
173 {
174 const int row = index.row();
175 if (row < 0 || row >= int(_versions.size()))
176 return QVariant();
177
178 switch(role) {
179 case Qt::DisplayRole:
180 return _versions[row].name;
181 default:
182 break;
183 }
184 return QVariant();
185 }
186
187 //---------------------------------------------------------
188 // ScoreVersionListModel::getPosition
189 // Returns a position of the version with the given
190 // index in the versions list.
191 // Returns -1 if there is no such a version.
192 //---------------------------------------------------------
193
getPosition(ScoreVersionIndex index) const194 int ScoreVersionListModel::getPosition(ScoreVersionIndex index) const
195 {
196 for (size_t i = 0; i < _versions.size(); ++i) {
197 if (_versions[i].index == index)
198 return static_cast<int>(i);
199 }
200 return -1;
201 }
202 }
203