XmlExportable.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "XmlExportable.h"
17 #include <map>
18 #include <QMutex>
19 #include <QMutexLocker>
20 #include <QTextStream>
21 
22 #include <iostream>
23 
24 QString
26  QString extraAttributes) const
27 {
28 // SVDEBUG << "XmlExportable::toXmlString" << endl;
29 
30  QString s;
31 
32  {
33  QTextStream out(&s);
34  toXml(out, indent, extraAttributes);
35  }
36 
37  return s;
38 }
39 
40 QString
42 {
43  s
44  .replace("&", "&amp;")
45  .replace("<", "&lt;")
46  .replace(">", "&gt;")
47  .replace("\"", "&quot;")
48  .replace("'", "&apos;");
49 
50  return s;
51 }
52 
53 QString
54 XmlExportable::encodeColour(int ri, int gi, int bi)
55 {
56  QString r, g, b;
57 
58  r.setNum(ri, 16);
59  if (ri < 16) r = "0" + r;
60 
61  g.setNum(gi, 16);
62  if (gi < 16) g = "0" + g;
63 
64  b.setNum(bi, 16);
65  if (bi < 16) b = "0" + b;
66 
67  return "#" + r + g + b;
68 }
69 
70 int
72 {
73  if (m_exportId == -1) {
74  static QMutex mutex;
75  static int nextId = 0;
76  QMutexLocker locker(&mutex);
77  if (m_exportId == -1) {
78  m_exportId = nextId;
79  ++nextId;
80  }
81  }
82  return m_exportId;
83 }
84 
85 
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const =0
Stream this exportable object out to XML on a text stream.
static QMutex mutex
Definition: Debug.cpp:30
virtual QString toXmlString(QString indent="", QString extraAttributes="") const
Convert this exportable object to XML in a string.
static QString encodeEntities(QString)
static QString encodeColour(int r, int g, int b)
ExportId getExportId() const
Return the numerical export identifier for this object.