void PowerTabOldImporter::convert(const PowerTabDocument::KeySignature &oldKey,
                                  KeySignature &key)
{
    key.setKeyType(static_cast<KeySignature::KeyType>(oldKey.GetKeyType()));
    key.setNumAccidentals(oldKey.GetKeyAccidentalsIncludingCancel());
    key.setSharps(oldKey.UsesSharps());
    key.setVisible(oldKey.IsShown());
    key.setCancellation(oldKey.IsCancellation());
}
void EditKeySignature::updateFollowingKeySignatures(const KeySignature &oldKey,
        const KeySignature &newKey)
{
    Score &score = myLocation.getScore();
    const int startSystem = myLocation.getSystemIndex();

    for (unsigned int i = startSystem; i < score.getSystems().size(); ++i)
    {
        for (Barline &bar : score.getSystems()[i].getBarlines())
        {
            if (i == startSystem &&
                    bar.getPosition() <= myLocation.getPositionIndex())
            {
                continue;
            }

            const KeySignature &currentKey = bar.getKeySignature();
            if (currentKey.getKeyType() == oldKey.getKeyType() &&
                    currentKey.getNumAccidentals() == oldKey.getNumAccidentals() &&
                    currentKey.usesSharps() == oldKey.usesSharps())
            {
                KeySignature key;
                key.setVisible(bar.getKeySignature().isVisible());
                key.setCancellation(bar.getKeySignature().isCancellation());
                key.setSharps(newKey.usesSharps());
                key.setKeyType(newKey.getKeyType());
                key.setNumAccidentals(newKey.getNumAccidentals());
                bar.setKeySignature(key);
            }
            else
            {
                return;
            }
        }
    }
}
void PowerTabOldImporter::convert(const PowerTabDocument::Score &oldScore,
                                  PowerTabDocument::Score::SystemConstPtr oldSystem,
                                  System &system)
{
    // Ensure that there are a reasonable number of positions in the staff
    // so that things aren't too stretched out.
    int lastPosition = 30;

    // Import barlines.
    Barline &startBar = system.getBarlines()[0];
    convert(*oldSystem->GetStartBar(), startBar);

    Barline &endBar = system.getBarlines()[1];
    convert(*oldSystem->GetEndBar(), endBar);

    for (size_t i = 0; i < oldSystem->GetBarlineCount(); ++i)
    {
        Barline bar;
        convert(*oldSystem->GetBarline(i), bar);
        system.insertBarline(bar);
        lastPosition = std::max(lastPosition, bar.getPosition());

        // Copy the key and time signature of the last bar into the end bar,
        // since the v2.0 file format expects this.
        if (i == oldSystem->GetBarlineCount() - 1)
        {
            KeySignature key = bar.getKeySignature();
            key.setVisible(false);
            system.getBarlines().back().setKeySignature(key);

            TimeSignature time = bar.getTimeSignature();
            time.setVisible(false);
            system.getBarlines().back().setTimeSignature(time);
        }
    }

    // Import tempo markers.
    std::vector<std::shared_ptr<PowerTabDocument::TempoMarker>> tempos;
    oldScore.GetTempoMarkersInSystem(tempos, oldSystem);
    for (auto &tempo : tempos)
    {
        TempoMarker marker;
        convert(*tempo, marker);
        system.insertTempoMarker(marker);
    }

    // Import alternate endings.
    std::vector<std::shared_ptr<PowerTabDocument::AlternateEnding>> endings;
    oldScore.GetAlternateEndingsInSystem(endings, oldSystem);
    for (auto &ending : endings)
    {
        AlternateEnding newEnding;
        convert(*ending, newEnding);
        system.insertAlternateEnding(newEnding);
    }

    // Import directions.
    for (size_t i = 0; i < oldSystem->GetDirectionCount(); ++i)
    {
        Direction direction;
        convert(*oldSystem->GetDirection(i), direction);
        system.insertDirection(direction);
    }

    // Import chord text symbols.
    for (size_t i = 0; i < oldSystem->GetChordTextCount(); ++i)
    {
        ChordText chord;
        convert(*oldSystem->GetChordText(i), chord);
        system.insertChord(chord);
    }

    std::vector<PowerTabDocument::Score::DynamicPtr> dynamics;
    oldScore.GetDynamicsInSystem(dynamics, oldSystem);

    // Import staves.
    for (size_t i = 0; i < oldSystem->GetStaffCount(); ++i)
    {
        // Dynamics are now stored in the staff instead of the system.
        std::vector<PowerTabDocument::Score::DynamicPtr> dynamicsInStaff;
        for (auto &dynamic : dynamics)
        {
            if (dynamic->GetStaff() == i)
                dynamicsInStaff.push_back(dynamic);
        }

        Staff staff;
        int lastPosInStaff = convert(*oldSystem->GetStaff(i), dynamicsInStaff,
                                     staff);
        system.insertStaff(staff);
        lastPosition = std::max(lastPosition, lastPosInStaff);
    }

    system.getBarlines().back().setPosition(lastPosition + 1);
}