MuseScore/libmscore/keylist.cpp

134 lines
3.5 KiB
C++
Raw Normal View History

2014-06-03 15:28:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2014 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 "keylist.h"
#include "xml.h"
#include "score.h"
namespace Ms {
//---------------------------------------------------------
// key
//
// locates the key sig currently in effect at tick
//---------------------------------------------------------
KeySigEvent KeyList::key(int tick) const
2014-06-03 15:28:10 +02:00
{
KeySigEvent ke;
ke.setKey(Key::C);
2014-06-03 15:28:10 +02:00
if (empty())
return ke;
2014-06-03 15:28:10 +02:00
auto i = upper_bound(tick);
if (i == begin())
return ke;
2014-06-03 15:28:10 +02:00
return (--i)->second;
}
//---------------------------------------------------------
// setKey
//---------------------------------------------------------
void KeyList::setKey(int tick, KeySigEvent k)
2014-06-03 15:28:10 +02:00
{
auto i = find(tick);
if (i == end())
insert(std::pair<int, KeySigEvent>(tick, k));
else
i->second = k;
2014-06-03 15:28:10 +02:00
}
//---------------------------------------------------------
// nextKeyTick
//
// return the tick at which the key sig after tick is located
// return 0, if no such a key sig
//---------------------------------------------------------
int KeyList::nextKeyTick(int tick) const
{
if (empty())
return 0;
auto i = upper_bound(tick+1);
if (i == end())
return 0;
return i->first;
}
//---------------------------------------------------------
// prevKey
//
// returns the key before the current key for tick
//---------------------------------------------------------
KeySigEvent KeyList::prevKey(int tick) const
2014-06-03 15:28:10 +02:00
{
KeySigEvent kc;
kc.setKey(Key::C);
2014-06-03 15:28:10 +02:00
if (empty())
return kc;
2014-06-03 15:28:10 +02:00
auto i = upper_bound(tick);
if (i == begin())
return kc;
2014-06-03 15:28:10 +02:00
--i;
if (i == begin())
return kc;
2014-06-03 15:28:10 +02:00
return (--i)->second;
}
//---------------------------------------------------------
// currentKeyTick
//
// return the tick position of the key currently
// in effect at tick
//---------------------------------------------------------
int KeyList::currentKeyTick(int tick) const
{
if (empty())
return 0;
auto i = upper_bound(tick);
if (i == begin())
return 0;
--i;
return i->first;
}
//---------------------------------------------------------
// KeyList::read
//---------------------------------------------------------
void KeyList::read(XmlReader& e, Score* cs)
{
while (e.readNextStartElement()) {
if (e.name() == "key") {
2014-06-20 17:07:22 +02:00
Key k;
2014-06-03 15:28:10 +02:00
int tick = e.intAttribute("tick", 0);
if (e.hasAttribute("custom"))
2014-06-20 17:07:22 +02:00
k = Key::C; // ke.setCustomType(e.intAttribute("custom"));
2014-06-03 15:28:10 +02:00
else
2014-06-20 17:07:22 +02:00
k = Key(e.intAttribute("idx"));
KeySigEvent ke;
ke.setKey(k);
(*this)[cs->fileDivision(tick)] = ke;
2014-06-03 15:28:10 +02:00
e.readNext();
}
else
e.unknown();
}
}
}