tempomap.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. #include "tempomap.h"
  19. #include <QJsonObject>
  20. bool indexCompare(const tempoindex &i1, const tempoindex &i2)
  21. {
  22. return i1.pos < i2.pos;
  23. }
  24. void TempoMap::updateIndexList()
  25. {
  26. qSort(_index.begin(), _index.end(), indexCompare);
  27. }
  28. TempoMap::TempoMap()
  29. {
  30. }
  31. void TempoMap::addTempo(int pos, float tempo)
  32. {
  33. beginInsertRows(QModelIndex(),1,1);
  34. _tempo[pos] = tempo;
  35. tempoindex idx;
  36. idx.pos = pos;
  37. idx.type = 0;
  38. _index.append(idx);
  39. updateIndexList();
  40. endInsertRows();
  41. }
  42. void TempoMap::addTimeSignature(int pos, int numerator, int denominator)
  43. {
  44. beginInsertRows(QModelIndex(),1,1);
  45. fraction time;
  46. time.denominator = denominator;
  47. time.numerator = numerator;
  48. tempoindex idx;
  49. idx.pos = pos;
  50. idx.type = 1;
  51. _index.append(idx);
  52. _time[pos] = time;
  53. updateIndexList();
  54. endInsertRows();
  55. }
  56. void TempoMap::removeEventAt(int index)
  57. {
  58. if(index<_index.size() && index>=0)
  59. {
  60. beginRemoveRows(QModelIndex(),index,index);
  61. if(_index[index].type==1)
  62. {
  63. _time.remove(_index[index].pos);
  64. }
  65. else if(_index[index].type==0)
  66. {
  67. _tempo.remove(_index[index].pos);
  68. }
  69. _index.removeAt(index);
  70. endRemoveRows();
  71. }
  72. }
  73. fraction TempoMap::getTimeSignatureForBar(int pos)
  74. {
  75. fraction time = _time[pos];
  76. while(time.denominator==0 && time.numerator==0)
  77. {
  78. pos--;
  79. time = _time[pos];
  80. }
  81. return time;
  82. }
  83. QModelIndex TempoMap::index(int row, int column, const QModelIndex &parent) const
  84. {
  85. Q_UNUSED( parent );
  86. return createIndex(row,column);
  87. }
  88. QModelIndex TempoMap::parent(const QModelIndex &child) const
  89. {
  90. Q_UNUSED( child );
  91. return QModelIndex();
  92. }
  93. int TempoMap::rowCount(const QModelIndex &parent) const
  94. {
  95. Q_UNUSED( parent );
  96. return _index.count();
  97. }
  98. int TempoMap::columnCount(const QModelIndex &parent) const
  99. {
  100. Q_UNUSED( parent );
  101. return 3;
  102. }
  103. QVariant TempoMap::data(const QModelIndex &index, int role) const
  104. {
  105. if(role!=Qt::DisplayRole) return QVariant();
  106. int row = index.row();
  107. int col = index.column();
  108. int pos = _index[row].pos;
  109. int type = _index[row].type;
  110. if(col==0) return QVariant(pos+1).toString();
  111. if(col==1)
  112. {
  113. if(type==0) return "Tempo";
  114. if(type==1) return "Time Signature";
  115. }
  116. if(col==2)
  117. {
  118. if(type==0) return _tempo[pos];
  119. if(type==1) return QVariant(_time[pos].numerator).toString()+"/"+QVariant(_time[pos].denominator).toString();
  120. }
  121. return QVariant();
  122. }
  123. void TempoMap::beginEditing()
  124. {
  125. _origIndex = _index;
  126. _origTempo = _tempo;
  127. _origTime = _time;
  128. }
  129. void TempoMap::toJson(QJsonArray& array)
  130. {
  131. for(int i=0;i<_index.length();i++)
  132. {
  133. int pos = _index[i].pos;
  134. int type = _index[i].type;
  135. if(type==1)
  136. {
  137. QJsonObject val;
  138. val["pos"] = pos;
  139. val["type"] = "timeSig";
  140. val["denominator"] = _time[pos].denominator;
  141. val["numerator"] = _time[pos].numerator;
  142. array.append(val);
  143. }
  144. else if(type==0)
  145. {
  146. QJsonObject val;
  147. val["pos"] = pos;
  148. val["type"] = "tempo";
  149. val["tempo"] = _tempo[pos];
  150. array.append(val);
  151. }
  152. }
  153. }
  154. void TempoMap::fromJson(QJsonArray &array)
  155. {
  156. _index.clear();
  157. _time.clear();
  158. _tempo.clear();
  159. for(int i=0;i<array.size();i++)
  160. {
  161. QJsonObject val = array[i].toObject();
  162. if(val["type"].toString()=="timeSig")
  163. {
  164. int pos = val["pos"].toInt();
  165. int denominator = val["denominator"].toInt();
  166. int numerator = val["numerator"].toInt();
  167. this->addTimeSignature(pos,numerator,denominator);
  168. }
  169. else if(val["type"].toString()=="tempo")
  170. {
  171. int pos = val["pos"].toInt();
  172. int tempo = val["tempo"].toInt();
  173. this->addTempo(pos,tempo);
  174. }
  175. }
  176. }
  177. void TempoMap::undo()
  178. {
  179. _index = _origIndex;
  180. _tempo = _origTempo;
  181. _time = _origTime;
  182. }