Chris@127
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@127
|
2
|
Chris@127
|
3 /*
|
Chris@127
|
4 Sonic Visualiser
|
Chris@127
|
5 An audio file viewer and annotation editor.
|
Chris@127
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@182
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@127
|
8
|
Chris@127
|
9 This program is free software; you can redistribute it and/or
|
Chris@127
|
10 modify it under the terms of the GNU General Public License as
|
Chris@127
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@127
|
12 License, or (at your option) any later version. See the file
|
Chris@127
|
13 COPYING included with this distribution for more information.
|
Chris@127
|
14 */
|
Chris@127
|
15
|
Chris@127
|
16 #include "Layer.h"
|
Chris@128
|
17 #include "view/View.h"
|
Chris@128
|
18 #include "data/model/Model.h"
|
Chris@127
|
19
|
Chris@127
|
20 #include <iostream>
|
Chris@127
|
21
|
Chris@131
|
22 #include <QMutexLocker>
|
Chris@267
|
23 #include <QMouseEvent>
|
Chris@131
|
24
|
Chris@131
|
25 #include "LayerFactory.h"
|
Chris@128
|
26 #include "base/PlayParameterRepository.h"
|
Chris@127
|
27
|
Chris@267
|
28 Layer::Layer() :
|
Chris@267
|
29 m_haveDraggingRect(false)
|
Chris@127
|
30 {
|
Chris@127
|
31 }
|
Chris@127
|
32
|
Chris@127
|
33 Layer::~Layer()
|
Chris@127
|
34 {
|
Chris@127
|
35 // std::cerr << "Layer::~Layer(" << this << ")" << std::endl;
|
Chris@127
|
36 }
|
Chris@127
|
37
|
Chris@127
|
38 QString
|
Chris@127
|
39 Layer::getPropertyContainerIconName() const
|
Chris@127
|
40 {
|
Chris@127
|
41 return LayerFactory::getInstance()->getLayerIconName
|
Chris@127
|
42 (LayerFactory::getInstance()->getLayerType(this));
|
Chris@127
|
43 }
|
Chris@127
|
44
|
Chris@127
|
45 QString
|
Chris@127
|
46 Layer::getLayerPresentationName() const
|
Chris@127
|
47 {
|
Chris@203
|
48 // QString layerName = objectName();
|
Chris@203
|
49
|
Chris@203
|
50 LayerFactory *factory = LayerFactory::getInstance();
|
Chris@203
|
51 QString layerName = factory->getLayerPresentationName
|
Chris@203
|
52 (factory->getLayerType(this));
|
Chris@203
|
53
|
Chris@127
|
54 QString modelName;
|
Chris@127
|
55 if (getModel()) modelName = getModel()->objectName();
|
Chris@127
|
56
|
Chris@127
|
57 QString text;
|
Chris@127
|
58 if (modelName != "") {
|
Chris@127
|
59 text = QString("%1: %2").arg(modelName).arg(layerName);
|
Chris@127
|
60 } else {
|
Chris@127
|
61 text = layerName;
|
Chris@127
|
62 }
|
Chris@127
|
63
|
Chris@127
|
64 return text;
|
Chris@127
|
65 }
|
Chris@127
|
66
|
Chris@127
|
67 void
|
Chris@127
|
68 Layer::setObjectName(const QString &name)
|
Chris@127
|
69 {
|
Chris@127
|
70 QObject::setObjectName(name);
|
Chris@127
|
71 emit layerNameChanged();
|
Chris@127
|
72 }
|
Chris@127
|
73
|
Chris@127
|
74 QString
|
Chris@127
|
75 Layer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@127
|
76 {
|
Chris@127
|
77 QString s;
|
Chris@127
|
78
|
Chris@127
|
79 s += indent;
|
Chris@127
|
80
|
Chris@127
|
81 s += QString("<layer id=\"%2\" type=\"%1\" name=\"%3\" model=\"%4\" %5/>\n")
|
Chris@127
|
82 .arg(encodeEntities(LayerFactory::getInstance()->getLayerTypeName
|
Chris@127
|
83 (LayerFactory::getInstance()->getLayerType(this))))
|
Chris@127
|
84 .arg(getObjectExportId(this))
|
Chris@127
|
85 .arg(encodeEntities(objectName()))
|
Chris@127
|
86 .arg(getObjectExportId(getModel()))
|
Chris@127
|
87 .arg(extraAttributes);
|
Chris@127
|
88
|
Chris@127
|
89 return s;
|
Chris@127
|
90 }
|
Chris@127
|
91
|
Chris@127
|
92 PlayParameters *
|
Chris@127
|
93 Layer::getPlayParameters()
|
Chris@127
|
94 {
|
Chris@127
|
95 // std::cerr << "Layer (" << this << ", " << objectName().toStdString() << ")::getPlayParameters: model is "<< getModel() << std::endl;
|
Chris@127
|
96 const Model *model = getModel();
|
Chris@127
|
97 if (model) {
|
Chris@127
|
98 return PlayParameterRepository::getInstance()->getPlayParameters(model);
|
Chris@127
|
99 }
|
Chris@127
|
100 return 0;
|
Chris@127
|
101 }
|
Chris@127
|
102
|
Chris@127
|
103 void
|
Chris@131
|
104 Layer::setLayerDormant(const View *v, bool dormant)
|
Chris@131
|
105 {
|
Chris@131
|
106 const void *vv = (const void *)v;
|
Chris@131
|
107 QMutexLocker locker(&m_dormancyMutex);
|
Chris@131
|
108 m_dormancy[vv] = dormant;
|
Chris@131
|
109 }
|
Chris@131
|
110
|
Chris@131
|
111 bool
|
Chris@131
|
112 Layer::isLayerDormant(const View *v) const
|
Chris@131
|
113 {
|
Chris@131
|
114 const void *vv = (const void *)v;
|
Chris@131
|
115 QMutexLocker locker(&m_dormancyMutex);
|
Chris@131
|
116 if (m_dormancy.find(vv) == m_dormancy.end()) return false;
|
Chris@131
|
117 return m_dormancy.find(vv)->second;
|
Chris@131
|
118 }
|
Chris@131
|
119
|
Chris@131
|
120 void
|
Chris@127
|
121 Layer::showLayer(View *view, bool show)
|
Chris@127
|
122 {
|
Chris@127
|
123 setLayerDormant(view, !show);
|
Chris@127
|
124 emit layerParametersChanged();
|
Chris@127
|
125 }
|
Chris@127
|
126
|
Chris@260
|
127 bool
|
Chris@267
|
128 Layer::getXScaleValue(const View *v, int x, float &value, QString &unit) const
|
Chris@260
|
129 {
|
Chris@260
|
130 if (!hasTimeXAxis()) return false;
|
Chris@260
|
131
|
Chris@260
|
132 const Model *m = getModel();
|
Chris@260
|
133 if (!m) return false;
|
Chris@260
|
134
|
Chris@260
|
135 value = float(v->getFrameForX(x)) / m->getSampleRate();
|
Chris@260
|
136 unit = "s";
|
Chris@260
|
137 return true;
|
Chris@260
|
138 }
|
Chris@260
|
139
|
Chris@267
|
140 void
|
Chris@267
|
141 Layer::measureStart(View *v, QMouseEvent *e)
|
Chris@267
|
142 {
|
Chris@267
|
143 m_draggingRect.pixrect = QRect(e->x(), e->y(), 0, 0);
|
Chris@267
|
144 if (hasTimeXAxis()) {
|
Chris@267
|
145 m_draggingRect.startFrame = v->getFrameForX(e->x());
|
Chris@267
|
146 m_draggingRect.endFrame = m_draggingRect.startFrame;
|
Chris@267
|
147 }
|
Chris@267
|
148 m_haveDraggingRect = true;
|
Chris@267
|
149 }
|
Chris@267
|
150
|
Chris@267
|
151 void
|
Chris@267
|
152 Layer::measureDrag(View *v, QMouseEvent *e)
|
Chris@267
|
153 {
|
Chris@267
|
154 if (!m_haveDraggingRect) return;
|
Chris@267
|
155 m_draggingRect.pixrect = QRect(m_draggingRect.pixrect.x(),
|
Chris@267
|
156 m_draggingRect.pixrect.y(),
|
Chris@267
|
157 e->x() - m_draggingRect.pixrect.x(),
|
Chris@267
|
158 e->y() - m_draggingRect.pixrect.y());
|
Chris@267
|
159 if (hasTimeXAxis()) {
|
Chris@267
|
160 m_draggingRect.endFrame = v->getFrameForX(e->x());
|
Chris@267
|
161 }
|
Chris@267
|
162 }
|
Chris@267
|
163
|
Chris@267
|
164 void
|
Chris@267
|
165 Layer::measureEnd(View *v, QMouseEvent *e)
|
Chris@267
|
166 {
|
Chris@267
|
167 //!!! command
|
Chris@267
|
168 if (!m_haveDraggingRect) return;
|
Chris@267
|
169 measureDrag(v, e);
|
Chris@267
|
170 m_measureRectList.push_back(m_draggingRect);
|
Chris@267
|
171 m_haveDraggingRect = false;
|
Chris@267
|
172 }
|
Chris@267
|
173
|
Chris@267
|
174 void
|
Chris@267
|
175 Layer::paintMeasurementRects(View *v, QPainter &paint) const
|
Chris@267
|
176 {
|
Chris@267
|
177 if (m_haveDraggingRect) {
|
Chris@267
|
178 v->drawMeasurementRect(paint, this, m_draggingRect.pixrect);
|
Chris@267
|
179 }
|
Chris@267
|
180
|
Chris@267
|
181 bool timex = hasTimeXAxis();
|
Chris@267
|
182
|
Chris@267
|
183 for (MeasureRectList::const_iterator i = m_measureRectList.begin();
|
Chris@267
|
184 i != m_measureRectList.end(); ++i) {
|
Chris@267
|
185
|
Chris@267
|
186 if (timex) {
|
Chris@267
|
187 int x0 = v->getXForFrame(i->startFrame);
|
Chris@267
|
188 int x1 = v->getXForFrame(i->endFrame);
|
Chris@267
|
189 QRect pr = QRect(x0, i->pixrect.y(), x1 - x0, i->pixrect.height());
|
Chris@267
|
190 i->pixrect = pr;
|
Chris@267
|
191 }
|
Chris@267
|
192
|
Chris@267
|
193 v->drawMeasurementRect(paint, this, i->pixrect);
|
Chris@267
|
194 }
|
Chris@267
|
195 }
|
Chris@267
|
196
|