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