File: scorediffmodel.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 210,672 kB
  • sloc: cpp: 291,093; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 240; perl: 82; pascal: 79
file content (132 lines) | stat: -rw-r--r-- 4,378 bytes parent folder | download | duplicates (4)
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
//=============================================================================
//  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 "scorediffmodel.h"

#include "libmscore/scorediff.h"

namespace Ms {

//---------------------------------------------------------
//   RawScoreDiffModel::RawScoreDiffModel
//---------------------------------------------------------

RawScoreDiffModel::RawScoreDiffModel(ScoreDiff* d, bool skipEqual, QObject* parent)
   : QAbstractListModel(parent), _diff(d), _skipEqual(skipEqual)
      {
      update();
      }

//---------------------------------------------------------
//   RawScoreDiffModel::update
//---------------------------------------------------------

void RawScoreDiffModel::update() {
      beginResetModel();
      _textDiffs.clear();
      for (const TextDiff& diff : _diff->textDiffs()) {
            if (_skipEqual && diff.type == DiffType::EQUAL)
                  continue;
            if (diff.type == DiffType::REPLACE) {
                  // Push this item twice. First will be shown as DELETE,
                  // second as INSERT.
                  _textDiffs.push_back(&diff);
                  }
            _textDiffs.push_back(&diff);
            }
      endResetModel();
      }

//---------------------------------------------------------
//   RawScoreDiffModel::rowCount
//---------------------------------------------------------

int RawScoreDiffModel::rowCount(const QModelIndex& parent) const
      {
      if (parent.isValid())
            return 0;
      return int(_textDiffs.size());
      }

//---------------------------------------------------------
//   RawScoreDiffModel::data
//---------------------------------------------------------

QVariant RawScoreDiffModel::data(const QModelIndex& index, int role) const
      {
      const int row = index.row();
      if (row < 0 || row >= int(_textDiffs.size()))
            return QVariant();

      const TextDiff& diff = *_textDiffs[row];
      switch(role) {
            case Qt::DisplayRole:
                  {
                  DiffType type = diff.type;
                  if (diff.type == DiffType::REPLACE)
                        type = (row == 0 || _textDiffs[row - 1] != &diff) ? DiffType::DELETE : DiffType::INSERT;
                  return diff.toString(type);
                  }
            case Qt::BackgroundRole:
                  switch(diff.type) {
                        case DiffType::DELETE:
                              return QBrush(Qt::red);
                        case DiffType::INSERT:
                              return QBrush(Qt::green);
                        default:
                              return QVariant();
                        }
            }
      return QVariant();
      }

//---------------------------------------------------------
//   ScoreDiffModel::rowCount
//---------------------------------------------------------

int ScoreDiffModel::rowCount(const QModelIndex& parent) const
      {
      if (parent.isValid())
            return 0;
      return int(_diff->diffs().size());
      }

//---------------------------------------------------------
//   ScoreDiffModel::data
//---------------------------------------------------------

QVariant ScoreDiffModel::data(const QModelIndex& index, int role) const
      {
      const int row = index.row();
      if (row < 0 || row >= int(_diff->diffs().size()))
            return QVariant();

      const BaseDiff& diff = *_diff->diffs()[row];
      switch(role) {
            case Qt::DisplayRole:
                  return diff.toString();
            }
      return QVariant();
      }

//---------------------------------------------------------
//   ScoreDiffModel::diffItem
//---------------------------------------------------------

const BaseDiff* ScoreDiffModel::diffItem(const QModelIndex& index) const
      {
      const int row = index.row();
      if (row < 0 || row >= int(_diff->diffs().size()))
            return nullptr;
      return _diff->diffs()[row];
      }
}