summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/guitar/ChordMap.cpp
blob: 64ea463cf527d6fc34f1842d736f0fff27deed95 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio 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        <richard.bown@ferventsoftware.com>

    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    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 "misc/Debug.h"
#include "ChordMap.h"

#include <tqfile.h>
#include <tqtextstream.h>

namespace Rosegarden
{

namespace Guitar
{
    
ChordMap::ChordMap()
    : m_needSave(false)
{
}

void ChordMap::insert(const Chord& c)
{
    m_map.insert(c);
    m_needSave = true;
}


ChordMap::chordarray
ChordMap::getChords(const TQString& root, const TQString& ext) const
{
    chordarray res;
    
    Chord tmp(root, ext);
    NOTATION_DEBUG << "ChordMap::getChords : chord = " << tmp << " - ext is empty : " << ext.isEmpty() << endl;
    
    for (chordset::const_iterator i = m_map.lower_bound(tmp); i != m_map.end(); ++i) {
        NOTATION_DEBUG << "ChordMap::getChords : checking chord " << *i << endl;
        
        if (i->getRoot() != root)
            break;

        if (/* ext.isNull() || */ i->getExt() == ext) {
            NOTATION_DEBUG << "ChordMap::getChords : adding chord " << *i << endl;
            res.push_back(*i);
        } else {
            break;
        }
    }
        
    return res;
}
    
TQStringList
ChordMap::getRootList() const
{
    static TQStringList rootNotes;
    
    if (rootNotes.count() == 0) {
        rootNotes = TQStringList::split(TQString(","), "A,A#/Bb,B,C,C#/Db,D,D#/Eb,E,F,F#/Gb,G,G#/Ab");
    }
    
    // extract roots from map itself - not a very good idea
    //
//    TQString currentRoot;
//    
//    for(chordset::const_iterator i = m_map.begin(); i != m_map.end(); ++i) {
//        const Chord& chord = *i;
//        if (chord.getRoot() != currentRoot) {
//            rootNotes.push_back(chord.getRoot());
//            currentRoot = chord.getRoot();
//        } 
//    }

    return rootNotes;
}

TQStringList
ChordMap::getExtList(const TQString& root) const
{
    TQStringList extList;
    TQString currentExt = "ZZ";
    
    Chord tmp(root);
    
    for(chordset::const_iterator i = m_map.lower_bound(tmp); i != m_map.end(); ++i) {
        const Chord& chord = *i;
//        NOTATION_DEBUG << "ChordMap::getExtList : chord = " << chord << endl;
         
        if (chord.getRoot() != root)
            break;
            
        if (chord.getExt() != currentExt) {
//            NOTATION_DEBUG << "ChordMap::getExtList : adding ext " << chord.getExt() << " for root " << root << endl;
            extList.push_back(chord.getExt());
            currentExt = chord.getExt();
        }        
    }

    return extList;
}

void
ChordMap::substitute(const Chord& oldChord, const Chord& newChord)
{
    remove(oldChord);
    insert(newChord);
}

void
ChordMap::remove(const Chord& c)
{
    m_map.erase(c);
    m_needSave = true;    
}

bool ChordMap::saveDocument(const TQString& filename, bool userChordsOnly, TQString& errMsg)
{
    TQFile file(filename);
    file.open(IO_WriteOnly);
   
    TQTextStream outStream(&file);
    
    outStream.setEncoding(TQTextStream::UnicodeUTF8);
    
    outStream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    << "<!DOCTYPE rosegarden-chord-data>\n"
    << "<rosegarden-chord-data version=\"" << VERSION
    << "\" format-version-major=\"" << FILE_FORMAT_VERSION_MAJOR
    << "\" format-version-minor=\"" << FILE_FORMAT_VERSION_MINOR
    << "\" format-version-point=\"" << FILE_FORMAT_VERSION_POINT
    << "\">\n";
    
    outStream << "<chords>\n";
    
    TQString currentExt, currentRoot;
    
    for(iterator i = begin(); i != end(); ++i) {
        const Chord& chord = *i;
    
        if (userChordsOnly && !chord.isUserChord())
            continue; // skip non-user chords
            
        if (chord.getRoot() != currentRoot) {

            currentRoot = chord.getRoot();
            
            // close current chordset (if there was one)
            if (i != begin())
                outStream << "\n</chordset>\n";

            // open new chordset            
            outStream << "<chordset root=\"" << chord.getRoot() << "\">\n";
            currentExt = "NEWEXT"; // to make sure we open a new chord right after that
        }
    
        if (chord.getExt() != currentExt) {
            
            currentExt = chord.getExt();
            
            // close current chord (if there was one)
            if (i != begin())
                outStream << "</chord>\n";

            // open new chord            
            outStream << "<chord";
            if (!chord.getExt().isEmpty())
                outStream << " ext=\"" << chord.getExt() << "\"";
            if (chord.isUserChord())
                outStream << " user=\"true\"";
                
            outStream << ">\n";
        }
        
        outStream << "<fingering>" << chord.getFingering().toString().c_str() << "</fingering>\n";
    }

    if (!m_map.empty())
        outStream << "</chord>\n"; // close last written chord
        
    outStream << "</chords>\n";    
    outStream << "</rosegarden-chord-data>\n";
  
    return outStream.tqdevice()->status() == IO_Ok;
}

int ChordMap::FILE_FORMAT_VERSION_MAJOR = 1;
int ChordMap::FILE_FORMAT_VERSION_MINOR = 0;
int ChordMap::FILE_FORMAT_VERSION_POINT = 0;


void
ChordMap::debugDump() const
{
    for(chordset::const_iterator i = m_map.begin(); i != m_map.end(); ++i) {
        Chord chord = *i;
        NOTATION_DEBUG << "ChordMap::debugDump " << chord << endl;
    } 
}

}

}