summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/guitar/ChordXmlHandler.cpp
blob: d2f71b40ee940a99fd9cf54d189b2cc3e83717f0 (plain)
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
// -*- c-basic-offset: 4 -*-

/*
    Rosegarden
    A sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>
 
    The moral right of the authors to claim authorship of this work
    has been asserted.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "ChordXmlHandler.h"
#include "misc/Debug.h"

namespace Rosegarden
{

ChordXmlHandler::ChordXmlHandler(Guitar::ChordMap& map)
    : ProgressReporter(0),
      m_chordMap(map)
{
}

ChordXmlHandler::~ChordXmlHandler()
{
}

bool ChordXmlHandler::startDocument()
{
    // nothing to do ?
    return true;
}

bool ChordXmlHandler::startElement(const TQString& namespaceURI,
                                   const TQString& localName,
                                   const TQString& qName,
                                   const TQXmlAttributes& atts)
{
    TQString lcName = qName.lower();

    if (lcName == "chordset") {
        // start new chord set
        m_currentRoot = atts.value("root").stripWhiteSpace();

    } else if (lcName == "chord") {
        
        m_currentChord = Guitar::Chord(m_currentRoot);
        
        if (atts.index("ext") >= 0)
            m_currentChord.setExt(atts.value("ext").stripWhiteSpace());

        if (atts.index("user") >= 0) {
            TQString userVal = atts.value("user").stripWhiteSpace().lower();
            bool res = (userVal == "yes" || userVal == "1" || userVal == "true");
            m_currentChord.setUserChord(res);
        } else {
            m_currentChord.setUserChord(false);
        }

    } else if (lcName == "fingering") {
        m_inFingering = true;
    }
    
    return true;
}

bool ChordXmlHandler::endElement(const TQString& namespaceURI,
                                 const TQString& localName,
                                 const TQString& qName)
{
    TQString lcName = qName.lower();

    if (lcName == "fingering") {

        m_inFingering = false;
        m_chordMap.insert(m_currentChord);
        NOTATION_DEBUG << "ChordXmlHandler::endElement (fingering) : adding chord " << m_currentChord << endl;            

    } else if (lcName == "chord") {
        
        // adding is done after each parsing of fingering
        //
//        m_chordMap.insert(m_currentChord);

    }
    
    return true;
}

bool ChordXmlHandler::characters(const TQString& ch)
{
    TQString ch2 = ch.simplifyWhiteSpace();
    
    if (!ch2.isEmpty() && m_inFingering) {
        if (!parseFingering(ch2))
            return false;        
    }

    return true;
}

bool ChordXmlHandler::endDocument()
{
    return true;
}

bool ChordXmlHandler::parseFingering(const TQString& ch) {
    
    TQString errString;
    
    Guitar::Fingering fingering = Guitar::Fingering::parseFingering(ch, errString);
    
    if (m_errorString.isEmpty()) {
        NOTATION_DEBUG << "ChordXmlHandler::parseFingering : fingering " << ch << endl;
        m_currentChord.setFingering(fingering);
        return true;    
    } else {
        m_errorString = errString;
        return false;
    }
}

bool
ChordXmlHandler::error(const TQXmlParseException& exception)
{
    m_errorString = TQString("%1 at line %2, column %3")
                    .tqarg(exception.message())
                    .tqarg(exception.lineNumber())
                    .tqarg(exception.columnNumber());
    return TQXmlDefaultHandler::error( exception );
}

bool
ChordXmlHandler::fatalError(const TQXmlParseException& exception)
{
    m_errorString = TQString("%1 at line %2, column %3")
                    .tqarg(exception.message())
                    .tqarg(exception.lineNumber())
                    .tqarg(exception.columnNumber());
    return TQXmlDefaultHandler::fatalError( exception );
}


}