annotate layer/LayerFactory.cpp @ 52:f2fe98a7c57e

* Use commands for add/delete pane in main window * Add compound command collection to command history (for add pane, import file etc) * Add hide/show pane and hidden pane list to PaneStack * Various other fixes
author Chris Cannam
date Mon, 13 Mar 2006 17:55:19 +0000
parents ad214997dddb
children 68869408f5c7
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@5 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "LayerFactory.h"
Chris@0 11
Chris@0 12 #include "WaveformLayer.h"
Chris@0 13 #include "SpectrogramLayer.h"
Chris@0 14 #include "TimeRulerLayer.h"
Chris@0 15 #include "TimeInstantLayer.h"
Chris@0 16 #include "TimeValueLayer.h"
Chris@30 17 #include "NoteLayer.h"
Chris@35 18 #include "TextLayer.h"
Chris@0 19 #include "Colour3DPlotLayer.h"
Chris@0 20
Chris@0 21 #include "model/RangeSummarisableTimeValueModel.h"
Chris@0 22 #include "model/DenseTimeValueModel.h"
Chris@0 23 #include "model/SparseOneDimensionalModel.h"
Chris@0 24 #include "model/SparseTimeValueModel.h"
Chris@30 25 #include "model/NoteModel.h"
Chris@35 26 #include "model/TextModel.h"
Chris@0 27 #include "model/DenseThreeDimensionalModel.h"
Chris@0 28
Chris@0 29 LayerFactory *
Chris@0 30 LayerFactory::m_instance = new LayerFactory;
Chris@0 31
Chris@0 32 LayerFactory *
Chris@0 33 LayerFactory::instance()
Chris@0 34 {
Chris@0 35 return m_instance;
Chris@0 36 }
Chris@0 37
Chris@0 38 LayerFactory::~LayerFactory()
Chris@0 39 {
Chris@0 40 }
Chris@0 41
Chris@0 42 QString
Chris@0 43 LayerFactory::getLayerPresentationName(LayerType type)
Chris@0 44 {
Chris@0 45 switch (type) {
Chris@0 46 case Waveform: return Layer::tr("Waveform");
Chris@0 47 case Spectrogram: return Layer::tr("Spectrogram");
Chris@0 48 case TimeRuler: return Layer::tr("Ruler");
Chris@0 49 case TimeInstants: return Layer::tr("Time Instants");
Chris@0 50 case TimeValues: return Layer::tr("Time Values");
Chris@30 51 case Notes: return Layer::tr("Notes");
Chris@35 52 case Text: return Layer::tr("Text");
Chris@0 53 case Colour3DPlot: return Layer::tr("Colour 3D Plot");
Chris@0 54
Chris@0 55 case MelodicRangeSpectrogram:
Chris@0 56 // The user can change all the parameters of this after the
Chris@0 57 // fact -- there's nothing permanently melodic-range about it
Chris@0 58 // that should be encoded in its name
Chris@0 59 return Layer::tr("Spectrogram");
Chris@11 60
Chris@37 61 case PeakFrequencySpectrogram:
Chris@37 62 // likewise
Chris@37 63 return Layer::tr("Spectrogram");
Chris@37 64
Chris@11 65 default: break;
Chris@0 66 }
Chris@0 67
Chris@0 68 return Layer::tr("Layer");
Chris@0 69 }
Chris@0 70
Chris@0 71 LayerFactory::LayerTypeSet
Chris@0 72 LayerFactory::getValidLayerTypes(Model *model)
Chris@0 73 {
Chris@0 74 LayerTypeSet types;
Chris@0 75
Chris@0 76 if (dynamic_cast<DenseThreeDimensionalModel *>(model)) {
Chris@0 77 types.insert(Colour3DPlot);
Chris@0 78 }
Chris@0 79
Chris@0 80 if (dynamic_cast<DenseTimeValueModel *>(model)) {
Chris@0 81 types.insert(Spectrogram);
Chris@0 82 types.insert(MelodicRangeSpectrogram);
Chris@37 83 types.insert(PeakFrequencySpectrogram);
Chris@0 84 }
Chris@0 85
Chris@0 86 if (dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
Chris@0 87 types.insert(Waveform);
Chris@0 88 }
Chris@0 89
Chris@0 90 if (dynamic_cast<SparseOneDimensionalModel *>(model)) {
Chris@0 91 types.insert(TimeInstants);
Chris@0 92 }
Chris@0 93
Chris@0 94 if (dynamic_cast<SparseTimeValueModel *>(model)) {
Chris@0 95 types.insert(TimeValues);
Chris@35 96
Chris@35 97 }
Chris@35 98 if (dynamic_cast<NoteModel *>(model)) {
Chris@35 99 types.insert(Notes);
Chris@0 100 }
Chris@0 101
Chris@35 102 if (dynamic_cast<TextModel *>(model)) {
Chris@35 103 types.insert(Text);
Chris@30 104 }
Chris@30 105
Chris@0 106 // We don't count TimeRuler here as it doesn't actually display
Chris@0 107 // the data, although it can be backed by any model
Chris@0 108
Chris@0 109 return types;
Chris@0 110 }
Chris@0 111
Chris@17 112 LayerFactory::LayerTypeSet
Chris@17 113 LayerFactory::getValidEmptyLayerTypes()
Chris@17 114 {
Chris@17 115 LayerTypeSet types;
Chris@17 116 types.insert(TimeInstants);
Chris@17 117 types.insert(TimeValues);
Chris@30 118 types.insert(Notes);
Chris@35 119 types.insert(Text);
Chris@17 120 //!!! and in principle Colour3DPlot -- now that's a challenge
Chris@17 121 return types;
Chris@17 122 }
Chris@17 123
Chris@0 124 LayerFactory::LayerType
Chris@6 125 LayerFactory::getLayerType(const Layer *layer)
Chris@0 126 {
Chris@6 127 if (dynamic_cast<const WaveformLayer *>(layer)) return Waveform;
Chris@6 128 if (dynamic_cast<const SpectrogramLayer *>(layer)) return Spectrogram;
Chris@6 129 if (dynamic_cast<const TimeRulerLayer *>(layer)) return TimeRuler;
Chris@6 130 if (dynamic_cast<const TimeInstantLayer *>(layer)) return TimeInstants;
Chris@6 131 if (dynamic_cast<const TimeValueLayer *>(layer)) return TimeValues;
Chris@30 132 if (dynamic_cast<const NoteLayer *>(layer)) return Notes;
Chris@35 133 if (dynamic_cast<const TextLayer *>(layer)) return Text;
Chris@6 134 if (dynamic_cast<const Colour3DPlotLayer *>(layer)) return Colour3DPlot;
Chris@6 135 return UnknownLayer;
Chris@6 136 }
Chris@6 137
Chris@6 138 QString
Chris@17 139 LayerFactory::getLayerIconName(LayerType type)
Chris@17 140 {
Chris@17 141 switch (type) {
Chris@17 142 case Waveform: return "waveform";
Chris@17 143 case Spectrogram: return "spectrogram";
Chris@17 144 case TimeRuler: return "timeruler";
Chris@17 145 case TimeInstants: return "instants";
Chris@17 146 case TimeValues: return "values";
Chris@30 147 case Notes: return "notes";
Chris@35 148 case Text: return "text";
Chris@17 149 case Colour3DPlot: return "colour3d";
Chris@17 150 default: return "unknown";
Chris@17 151 }
Chris@17 152 }
Chris@17 153
Chris@17 154 QString
Chris@6 155 LayerFactory::getLayerTypeName(LayerType type)
Chris@6 156 {
Chris@6 157 switch (type) {
Chris@6 158 case Waveform: return "waveform";
Chris@6 159 case Spectrogram: return "spectrogram";
Chris@6 160 case TimeRuler: return "timeruler";
Chris@6 161 case TimeInstants: return "timeinstants";
Chris@6 162 case TimeValues: return "timevalues";
Chris@30 163 case Notes: return "notes";
Chris@35 164 case Text: return "text";
Chris@6 165 case Colour3DPlot: return "colour3dplot";
Chris@6 166 default: return "unknown";
Chris@6 167 }
Chris@6 168 }
Chris@6 169
Chris@6 170 LayerFactory::LayerType
Chris@6 171 LayerFactory::getLayerTypeForName(QString name)
Chris@6 172 {
Chris@6 173 if (name == "waveform") return Waveform;
Chris@6 174 if (name == "spectrogram") return Spectrogram;
Chris@6 175 if (name == "timeruler") return TimeRuler;
Chris@6 176 if (name == "timeinstants") return TimeInstants;
Chris@6 177 if (name == "timevalues") return TimeValues;
Chris@30 178 if (name == "notes") return Notes;
Chris@35 179 if (name == "text") return Text;
Chris@6 180 if (name == "colour3dplot") return Colour3DPlot;
Chris@0 181 return UnknownLayer;
Chris@0 182 }
Chris@0 183
Chris@0 184 void
Chris@0 185 LayerFactory::setModel(Layer *layer, Model *model)
Chris@0 186 {
Chris@0 187 if (trySetModel<WaveformLayer, RangeSummarisableTimeValueModel>(layer, model))
Chris@0 188 return;
Chris@0 189
Chris@0 190 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
Chris@0 191 return;
Chris@0 192
Chris@0 193 if (trySetModel<TimeRulerLayer, Model>(layer, model))
Chris@0 194 return;
Chris@0 195
Chris@0 196 if (trySetModel<TimeInstantLayer, SparseOneDimensionalModel>(layer, model))
Chris@0 197 return;
Chris@0 198
Chris@0 199 if (trySetModel<TimeValueLayer, SparseTimeValueModel>(layer, model))
Chris@0 200 return;
Chris@0 201
Chris@30 202 if (trySetModel<NoteLayer, NoteModel>(layer, model))
Chris@30 203 return;
Chris@30 204
Chris@35 205 if (trySetModel<TextLayer, TextModel>(layer, model))
Chris@35 206 return;
Chris@35 207
Chris@0 208 if (trySetModel<Colour3DPlotLayer, DenseThreeDimensionalModel>(layer, model))
Chris@0 209 return;
Chris@0 210
Chris@0 211 if (trySetModel<SpectrogramLayer, DenseTimeValueModel>(layer, model))
Chris@0 212 return;
Chris@0 213 }
Chris@0 214
Chris@17 215 Model *
Chris@17 216 LayerFactory::createEmptyModel(LayerType layerType, Model *baseModel)
Chris@17 217 {
Chris@17 218 if (layerType == TimeInstants) {
Chris@17 219 return new SparseOneDimensionalModel(baseModel->getSampleRate(), 1);
Chris@17 220 } else if (layerType == TimeValues) {
Chris@17 221 return new SparseTimeValueModel(baseModel->getSampleRate(), 1,
Chris@17 222 0.0, 0.0, true);
Chris@30 223 } else if (layerType == Notes) {
Chris@30 224 return new NoteModel(baseModel->getSampleRate(), 1,
Chris@30 225 0.0, 0.0, true);
Chris@35 226 } else if (layerType == Text) {
Chris@35 227 return new TextModel(baseModel->getSampleRate(), 1, true);
Chris@17 228 } else {
Chris@17 229 return 0;
Chris@17 230 }
Chris@17 231 }
Chris@17 232
Chris@0 233 Layer *
Chris@52 234 LayerFactory::createLayer(LayerType type, int channel)
Chris@0 235 {
Chris@0 236 Layer *layer = 0;
Chris@0 237
Chris@0 238 switch (type) {
Chris@0 239
Chris@0 240 case Waveform:
Chris@44 241 layer = new WaveformLayer;
Chris@0 242 static_cast<WaveformLayer *>(layer)->setChannel(channel);
Chris@0 243 break;
Chris@0 244
Chris@0 245 case Spectrogram:
Chris@44 246 layer = new SpectrogramLayer;
Chris@0 247 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
Chris@0 248 break;
Chris@0 249
Chris@0 250 case TimeRuler:
Chris@44 251 layer = new TimeRulerLayer;
Chris@0 252 break;
Chris@0 253
Chris@0 254 case TimeInstants:
Chris@44 255 layer = new TimeInstantLayer;
Chris@0 256 break;
Chris@0 257
Chris@0 258 case TimeValues:
Chris@44 259 layer = new TimeValueLayer;
Chris@0 260 break;
Chris@0 261
Chris@30 262 case Notes:
Chris@44 263 layer = new NoteLayer;
Chris@30 264 break;
Chris@30 265
Chris@35 266 case Text:
Chris@44 267 layer = new TextLayer;
Chris@35 268 break;
Chris@35 269
Chris@0 270 case Colour3DPlot:
Chris@44 271 layer = new Colour3DPlotLayer;
Chris@0 272 break;
Chris@0 273
Chris@0 274 case MelodicRangeSpectrogram:
Chris@44 275 layer = new SpectrogramLayer(SpectrogramLayer::MelodicRange);
Chris@0 276 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
Chris@0 277 break;
Chris@11 278
Chris@37 279 case PeakFrequencySpectrogram:
Chris@44 280 layer = new SpectrogramLayer(SpectrogramLayer::MelodicPeaks);
Chris@37 281 static_cast<SpectrogramLayer *>(layer)->setChannel(channel);
Chris@37 282 break;
Chris@37 283
Chris@11 284 default: break;
Chris@0 285 }
Chris@0 286
Chris@0 287 if (!layer) {
Chris@0 288 std::cerr << "LayerFactory::createLayer: Unknown layer type "
Chris@0 289 << type << std::endl;
Chris@0 290 } else {
Chris@0 291 std::cerr << "LayerFactory::createLayer: Setting object name "
Chris@0 292 << getLayerPresentationName(type).toStdString() << " on " << layer << std::endl;
Chris@0 293 layer->setObjectName(getLayerPresentationName(type));
Chris@0 294 }
Chris@0 295
Chris@0 296 return layer;
Chris@0 297 }
Chris@0 298