Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@5: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: #include "LayerFactory.h" Chris@0: Chris@0: #include "WaveformLayer.h" Chris@0: #include "SpectrogramLayer.h" Chris@0: #include "TimeRulerLayer.h" Chris@0: #include "TimeInstantLayer.h" Chris@0: #include "TimeValueLayer.h" Chris@30: #include "NoteLayer.h" Chris@0: #include "Colour3DPlotLayer.h" Chris@0: Chris@0: #include "model/RangeSummarisableTimeValueModel.h" Chris@0: #include "model/DenseTimeValueModel.h" Chris@0: #include "model/SparseOneDimensionalModel.h" Chris@0: #include "model/SparseTimeValueModel.h" Chris@30: #include "model/NoteModel.h" Chris@0: #include "model/DenseThreeDimensionalModel.h" Chris@0: Chris@0: LayerFactory * Chris@0: LayerFactory::m_instance = new LayerFactory; Chris@0: Chris@0: LayerFactory * Chris@0: LayerFactory::instance() Chris@0: { Chris@0: return m_instance; Chris@0: } Chris@0: Chris@0: LayerFactory::~LayerFactory() Chris@0: { Chris@0: } Chris@0: Chris@0: QString Chris@0: LayerFactory::getLayerPresentationName(LayerType type) Chris@0: { Chris@0: switch (type) { Chris@0: case Waveform: return Layer::tr("Waveform"); Chris@0: case Spectrogram: return Layer::tr("Spectrogram"); Chris@0: case TimeRuler: return Layer::tr("Ruler"); Chris@0: case TimeInstants: return Layer::tr("Time Instants"); Chris@0: case TimeValues: return Layer::tr("Time Values"); Chris@30: case Notes: return Layer::tr("Notes"); Chris@0: case Colour3DPlot: return Layer::tr("Colour 3D Plot"); Chris@0: Chris@0: case MelodicRangeSpectrogram: Chris@0: // The user can change all the parameters of this after the Chris@0: // fact -- there's nothing permanently melodic-range about it Chris@0: // that should be encoded in its name Chris@0: return Layer::tr("Spectrogram"); Chris@11: Chris@11: default: break; Chris@0: } Chris@0: Chris@0: return Layer::tr("Layer"); Chris@0: } Chris@0: Chris@0: LayerFactory::LayerTypeSet Chris@0: LayerFactory::getValidLayerTypes(Model *model) Chris@0: { Chris@0: LayerTypeSet types; Chris@0: Chris@0: if (dynamic_cast(model)) { Chris@0: types.insert(Colour3DPlot); Chris@0: } Chris@0: Chris@0: if (dynamic_cast(model)) { Chris@0: types.insert(Spectrogram); Chris@0: types.insert(MelodicRangeSpectrogram); Chris@0: } Chris@0: Chris@0: if (dynamic_cast(model)) { Chris@0: types.insert(Waveform); Chris@0: } Chris@0: Chris@0: if (dynamic_cast(model)) { Chris@0: types.insert(TimeInstants); Chris@0: } Chris@0: Chris@0: if (dynamic_cast(model)) { Chris@0: types.insert(TimeValues); Chris@0: } Chris@0: Chris@30: if (dynamic_cast(model)) { Chris@30: types.insert(Notes); Chris@30: } Chris@30: Chris@0: // We don't count TimeRuler here as it doesn't actually display Chris@0: // the data, although it can be backed by any model Chris@0: Chris@0: return types; Chris@0: } Chris@0: Chris@17: LayerFactory::LayerTypeSet Chris@17: LayerFactory::getValidEmptyLayerTypes() Chris@17: { Chris@17: LayerTypeSet types; Chris@17: types.insert(TimeInstants); Chris@17: types.insert(TimeValues); Chris@30: types.insert(Notes); Chris@17: //!!! and in principle Colour3DPlot -- now that's a challenge Chris@17: return types; Chris@17: } Chris@17: Chris@0: LayerFactory::LayerType Chris@6: LayerFactory::getLayerType(const Layer *layer) Chris@0: { Chris@6: if (dynamic_cast(layer)) return Waveform; Chris@6: if (dynamic_cast(layer)) return Spectrogram; Chris@6: if (dynamic_cast(layer)) return TimeRuler; Chris@6: if (dynamic_cast(layer)) return TimeInstants; Chris@6: if (dynamic_cast(layer)) return TimeValues; Chris@30: if (dynamic_cast(layer)) return Notes; Chris@6: if (dynamic_cast(layer)) return Colour3DPlot; Chris@6: return UnknownLayer; Chris@6: } Chris@6: Chris@6: QString Chris@17: LayerFactory::getLayerIconName(LayerType type) Chris@17: { Chris@17: switch (type) { Chris@17: case Waveform: return "waveform"; Chris@17: case Spectrogram: return "spectrogram"; Chris@17: case TimeRuler: return "timeruler"; Chris@17: case TimeInstants: return "instants"; Chris@17: case TimeValues: return "values"; Chris@30: case Notes: return "notes"; Chris@17: case Colour3DPlot: return "colour3d"; Chris@17: default: return "unknown"; Chris@17: } Chris@17: } Chris@17: Chris@17: QString Chris@6: LayerFactory::getLayerTypeName(LayerType type) Chris@6: { Chris@6: switch (type) { Chris@6: case Waveform: return "waveform"; Chris@6: case Spectrogram: return "spectrogram"; Chris@6: case TimeRuler: return "timeruler"; Chris@6: case TimeInstants: return "timeinstants"; Chris@6: case TimeValues: return "timevalues"; Chris@30: case Notes: return "notes"; Chris@6: case Colour3DPlot: return "colour3dplot"; Chris@6: default: return "unknown"; Chris@6: } Chris@6: } Chris@6: Chris@6: LayerFactory::LayerType Chris@6: LayerFactory::getLayerTypeForName(QString name) Chris@6: { Chris@6: if (name == "waveform") return Waveform; Chris@6: if (name == "spectrogram") return Spectrogram; Chris@6: if (name == "timeruler") return TimeRuler; Chris@6: if (name == "timeinstants") return TimeInstants; Chris@6: if (name == "timevalues") return TimeValues; Chris@30: if (name == "notes") return Notes; Chris@6: if (name == "colour3dplot") return Colour3DPlot; Chris@0: return UnknownLayer; Chris@0: } Chris@0: Chris@0: void Chris@0: LayerFactory::setModel(Layer *layer, Model *model) Chris@0: { Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: Chris@30: if (trySetModel(layer, model)) Chris@30: return; Chris@30: Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: Chris@0: if (trySetModel(layer, model)) Chris@0: return; Chris@0: } Chris@0: Chris@17: Model * Chris@17: LayerFactory::createEmptyModel(LayerType layerType, Model *baseModel) Chris@17: { Chris@17: if (layerType == TimeInstants) { Chris@17: return new SparseOneDimensionalModel(baseModel->getSampleRate(), 1); Chris@17: } else if (layerType == TimeValues) { Chris@17: return new SparseTimeValueModel(baseModel->getSampleRate(), 1, Chris@17: 0.0, 0.0, true); Chris@30: } else if (layerType == Notes) { Chris@30: return new NoteModel(baseModel->getSampleRate(), 1, Chris@30: 0.0, 0.0, true); Chris@17: } else { Chris@17: return 0; Chris@17: } Chris@17: } Chris@17: Chris@0: Layer * Chris@0: LayerFactory::createLayer(LayerType type, View *view, Chris@0: Model *model, int channel) Chris@0: { Chris@0: Layer *layer = 0; Chris@0: Chris@0: switch (type) { Chris@0: Chris@0: case Waveform: Chris@0: layer = new WaveformLayer(view); Chris@0: static_cast(layer)->setChannel(channel); Chris@0: break; Chris@0: Chris@0: case Spectrogram: Chris@0: layer = new SpectrogramLayer(view); Chris@0: static_cast(layer)->setChannel(channel); Chris@0: break; Chris@0: Chris@0: case TimeRuler: Chris@0: layer = new TimeRulerLayer(view); Chris@0: break; Chris@0: Chris@0: case TimeInstants: Chris@0: layer = new TimeInstantLayer(view); Chris@0: break; Chris@0: Chris@0: case TimeValues: Chris@0: layer = new TimeValueLayer(view); Chris@0: break; Chris@0: Chris@30: case Notes: Chris@30: layer = new NoteLayer(view); Chris@30: break; Chris@30: Chris@0: case Colour3DPlot: Chris@0: layer = new Colour3DPlotLayer(view); Chris@0: break; Chris@0: Chris@0: case MelodicRangeSpectrogram: Chris@0: layer = new SpectrogramLayer(view, SpectrogramLayer::MelodicRange); Chris@0: static_cast(layer)->setChannel(channel); Chris@0: break; Chris@11: Chris@11: default: break; Chris@0: } Chris@0: Chris@0: if (!layer) { Chris@0: std::cerr << "LayerFactory::createLayer: Unknown layer type " Chris@0: << type << std::endl; Chris@0: } else { Chris@8: if (model) setModel(layer, model); Chris@0: std::cerr << "LayerFactory::createLayer: Setting object name " Chris@0: << getLayerPresentationName(type).toStdString() << " on " << layer << std::endl; Chris@0: layer->setObjectName(getLayerPresentationName(type)); Chris@0: } Chris@0: Chris@0: return layer; Chris@0: } Chris@0: