SVFileReader.h
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 and QMUL.
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 #ifndef SV_FILE_READER_H
17 #define SV_FILE_READER_H
18 
19 #include "layer/LayerFactory.h"
20 #include "transform/Transform.h"
21 
22 #include <QXmlDefaultHandler>
23 
24 #include <map>
25 
26 class Pane;
27 class Model;
28 class Path;
29 class Document;
30 class PlayParameters;
31 
33 {
34 public:
35  virtual ~SVFileReaderPaneCallback();
36  virtual Pane *addPane() = 0;
37  virtual void setWindowSize(int width, int height) = 0;
38  virtual void addSelection(sv_frame_t start, sv_frame_t end) = 0;
39 };
40 
166 class SVFileReader : public QObject, QXmlDefaultHandler
167 {
168  Q_OBJECT
169 
170 public:
171  SVFileReader(Document *document,
172  SVFileReaderPaneCallback &callback,
173  QString location = ""); // for audio file locate mechanism
174  virtual ~SVFileReader();
175 
176  void parse(const QString &xmlData);
177  void parse(QXmlInputSource &source);
178 
179  bool isOK();
180  QString getErrorString() const { return m_errorString; }
181 
182  // For loading a single layer onto an existing pane
183  void setCurrentPane(Pane *pane) { m_currentPane = pane; }
184 
185  bool startElement(const QString &namespaceURI,
186  const QString &localName,
187  const QString &qName,
188  const QXmlAttributes& atts) override;
189 
190  bool characters(const QString &) override;
191 
192  bool endElement(const QString &namespaceURI,
193  const QString &localName,
194  const QString &qName) override;
195 
196  bool error(const QXmlParseException &exception) override;
197  bool fatalError(const QXmlParseException &exception) override;
198 
199  enum FileType
200  {
203  UnknownFileType
204  };
205 
206  static FileType identifyXmlFile(QString path);
207 
208 signals:
209  void modelRegenerationFailed(QString layerName, QString transformName,
210  QString message);
211  void modelRegenerationWarning(QString layerName, QString transformName,
212  QString message);
213 
214 protected:
215  bool readWindow(const QXmlAttributes &);
216  bool readModel(const QXmlAttributes &);
217  bool readView(const QXmlAttributes &);
218  bool readLayer(const QXmlAttributes &);
219  bool readDatasetStart(const QXmlAttributes &);
220  bool addBinToDataset(const QXmlAttributes &);
221  bool addPointToDataset(const QXmlAttributes &);
222  bool addRowToDataset(const QXmlAttributes &);
223  bool readRowData(const QString &);
224  bool readDerivation(const QXmlAttributes &);
225  bool readPlayParameters(const QXmlAttributes &);
226  bool readPlugin(const QXmlAttributes &);
227  bool readPluginForTransform(const QXmlAttributes &);
228  bool readPluginForPlayback(const QXmlAttributes &);
229  bool readTransform(const QXmlAttributes &);
230  bool readParameter(const QXmlAttributes &);
231  bool readSelection(const QXmlAttributes &);
232  bool readMeasurement(const QXmlAttributes &);
233 
234  void makeAggregateModels();
235  void addUnaddedModels();
236 
237  // We use the term "pending" of things that have been referred to
238  // but not yet constructed because their definitions are
239  // incomplete. They are just referred to with an ExportId. Models
240  // that have been constructed are always added straight away to
241  // ById and are referred to with a ModelId (everywhere where
242  // previously we would have used a Model *). m_models maps from
243  // ExportId (as read from the file) to complete Models, or to a
244  // ModelId of None for any model that could not be constructed for
245  // some reason.
246 
247  typedef XmlExportable::ExportId ExportId;
248 
249  bool haveModel(ExportId id) {
250  return (m_models.find(id) != m_models.end()) && !m_models[id].isNone();
251  }
252 
254  QString name;
255  sv_samplerate_t sampleRate;
256  std::vector<ExportId> components;
257  };
258 
261  QString m_location;
263  std::map<ExportId, Layer *> m_layers;
264  std::map<ExportId, ModelId> m_models;
265  std::map<ExportId, Path *> m_paths;
266  std::set<ModelId> m_addedModels; // i.e. added to Document, not just ById
267  std::map<ExportId, PendingAggregateRec> m_pendingAggregates;
268 
269  // A model element often contains a dataset id, and the dataset
270  // then follows it. When the model is read, an entry in this map
271  // is added, mapping from the dataset's export id (the actual
272  // dataset has not been read yet) back to the export id of the
273  // object that needs it. We map to export id rather than model id,
274  // because the object might be a path rather than a model.
275  std::map<ExportId, ExportId> m_awaitingDatasets;
276 
277  // And then this is the model or path that a dataset element is
278  // currently being read into, i.e. the value looked up from
279  // m_awaitingDatasets at the point where the dataset is found.
281 
285  std::shared_ptr<PlayParameters> m_currentPlayParameters;
291  bool m_inRow;
292  bool m_inLayer;
293  bool m_inView;
294  bool m_inData;
297  QString m_errorString;
298  bool m_ok;
299 };
300 
301 #endif
void setCurrentPane(Pane *pane)
Definition: SVFileReader.h:183
virtual void addSelection(sv_frame_t start, sv_frame_t end)=0
Transform m_currentTransform
Definition: SVFileReader.h:286
std::map< ExportId, PendingAggregateRec > m_pendingAggregates
Definition: SVFileReader.h:267
bool haveModel(ExportId id)
Definition: SVFileReader.h:249
SVFileReaderPaneCallback & m_paneCallback
Definition: SVFileReader.h:260
XmlExportable::ExportId ExportId
Definition: SVFileReader.h:247
ModelId m_currentDerivedModel
Definition: SVFileReader.h:283
std::map< ExportId, ExportId > m_awaitingDatasets
Definition: SVFileReader.h:275
ModelId m_currentTransformSource
Definition: SVFileReader.h:287
ExportId m_pendingDerivedModel
Definition: SVFileReader.h:284
QString m_datasetSeparator
Definition: SVFileReader.h:290
QString m_location
Definition: SVFileReader.h:261
SVFileReader loads Sonic Visualiser XML files.
Definition: SVFileReader.h:166
Layer * m_currentLayer
Definition: SVFileReader.h:282
bool m_inSelections
Definition: SVFileReader.h:295
Pane * m_currentPane
Definition: SVFileReader.h:262
QString m_errorString
Definition: SVFileReader.h:297
virtual void setWindowSize(int width, int height)=0
Document * m_document
Definition: SVFileReader.h:259
std::map< ExportId, Layer * > m_layers
Definition: SVFileReader.h:263
virtual Pane * addPane()=0
QString getErrorString() const
Definition: SVFileReader.h:180
bool m_currentTransformIsNewStyle
Definition: SVFileReader.h:289
ExportId m_currentDataset
Definition: SVFileReader.h:280
A Sonic Visualiser document consists of a set of data models, and also the visualisation layers used ...
Definition: Document.h:71
std::set< ModelId > m_addedModels
Definition: SVFileReader.h:266
std::shared_ptr< PlayParameters > m_currentPlayParameters
Definition: SVFileReader.h:285
std::vector< ExportId > components
Definition: SVFileReader.h:256
std::map< ExportId, Path * > m_paths
Definition: SVFileReader.h:265
int m_currentTransformChannel
Definition: SVFileReader.h:288
std::map< ExportId, ModelId > m_models
Definition: SVFileReader.h:264