svcore  1.9
XmlExportable.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "XmlExportable.h"
00017 #include <map>
00018 #include <QMutex>
00019 #include <QMutexLocker>
00020 #include <QTextStream>
00021 
00022 #include <iostream>
00023 
00024 QString
00025 XmlExportable::toXmlString(QString indent,
00026                            QString extraAttributes) const
00027 {
00028 //    SVDEBUG << "XmlExportable::toXmlString" << endl;
00029 
00030     QString s;
00031 
00032     {
00033         QTextStream out(&s);
00034         toXml(out, indent, extraAttributes);
00035     }
00036 
00037     return s;
00038 }
00039 
00040 QString
00041 XmlExportable::encodeEntities(QString s)
00042 {
00043     s
00044         .replace("&", "&amp;")
00045         .replace("<", "&lt;")
00046         .replace(">", "&gt;")
00047         .replace("\"", "&quot;")
00048         .replace("'", "&apos;");
00049 
00050     return s;
00051 }
00052 
00053 QString
00054 XmlExportable::encodeColour(int ri, int gi, int bi)
00055 {
00056     QString r, g, b;
00057 
00058     r.setNum(ri, 16);
00059     if (ri < 16) r = "0" + r;
00060 
00061     g.setNum(gi, 16);
00062     if (gi < 16) g = "0" + g;
00063 
00064     b.setNum(bi, 16);
00065     if (bi < 16) b = "0" + b;
00066 
00067     return "#" + r + g + b;
00068 }
00069 
00070 int
00071 XmlExportable::getObjectExportId(const void * object)
00072 {
00073     static QMutex mutex;
00074     QMutexLocker locker(&mutex);
00075 
00076     static std::map<const void *, int> idMap;
00077     static int maxId = 0;
00078     
00079     if (idMap.find(object) == idMap.end()) {
00080         idMap[object] = maxId++;
00081     }
00082 
00083     return idMap[object];
00084 }
00085 
00086