comparison layer/Layer.cpp @ 268:70537b0434c4

* Use a command for adding layer measurements
author Chris Cannam
date Tue, 26 Jun 2007 11:08:21 +0000
parents 4ed1446ad604
children 7b58c5e91f20
comparison
equal deleted inserted replaced
267:4ed1446ad604 268:70537b0434c4
14 */ 14 */
15 15
16 #include "Layer.h" 16 #include "Layer.h"
17 #include "view/View.h" 17 #include "view/View.h"
18 #include "data/model/Model.h" 18 #include "data/model/Model.h"
19 #include "base/CommandHistory.h"
19 20
20 #include <iostream> 21 #include <iostream>
21 22
22 #include <QMutexLocker> 23 #include <QMutexLocker>
23 #include <QMouseEvent> 24 #include <QMouseEvent>
67 void 68 void
68 Layer::setObjectName(const QString &name) 69 Layer::setObjectName(const QString &name)
69 { 70 {
70 QObject::setObjectName(name); 71 QObject::setObjectName(name);
71 emit layerNameChanged(); 72 emit layerNameChanged();
73 }
74
75 PlayParameters *
76 Layer::getPlayParameters()
77 {
78 // std::cerr << "Layer (" << this << ", " << objectName().toStdString() << ")::getPlayParameters: model is "<< getModel() << std::endl;
79 const Model *model = getModel();
80 if (model) {
81 return PlayParameterRepository::getInstance()->getPlayParameters(model);
82 }
83 return 0;
84 }
85
86 void
87 Layer::setLayerDormant(const View *v, bool dormant)
88 {
89 const void *vv = (const void *)v;
90 QMutexLocker locker(&m_dormancyMutex);
91 m_dormancy[vv] = dormant;
92 }
93
94 bool
95 Layer::isLayerDormant(const View *v) const
96 {
97 const void *vv = (const void *)v;
98 QMutexLocker locker(&m_dormancyMutex);
99 if (m_dormancy.find(vv) == m_dormancy.end()) return false;
100 return m_dormancy.find(vv)->second;
101 }
102
103 void
104 Layer::showLayer(View *view, bool show)
105 {
106 setLayerDormant(view, !show);
107 emit layerParametersChanged();
108 }
109
110 bool
111 Layer::getXScaleValue(const View *v, int x, float &value, QString &unit) const
112 {
113 if (!hasTimeXAxis()) return false;
114
115 const Model *m = getModel();
116 if (!m) return false;
117
118 value = float(v->getFrameForX(x)) / m->getSampleRate();
119 unit = "s";
120 return true;
121 }
122
123 bool
124 Layer::MeasureRect::operator<(const MeasureRect &mr) const
125 {
126 if (haveFrames) {
127 if (startFrame == mr.startFrame) {
128 if (endFrame != mr.endFrame) {
129 return endFrame < mr.endFrame;
130 }
131 } else {
132 return startFrame < mr.startFrame;
133 }
134 } else {
135 if (pixrect.x() == mr.pixrect.x()) {
136 if (pixrect.width() != mr.pixrect.width()) {
137 return pixrect.width() < mr.pixrect.width();
138 }
139 } else {
140 return pixrect.x() < mr.pixrect.x();
141 }
142 }
143
144 // the two rects are equal in x and width
145
146 if (pixrect.y() == mr.pixrect.y()) {
147 return pixrect.height() < mr.pixrect.height();
148 } else {
149 return pixrect.y() < mr.pixrect.y();
150 }
151 }
152
153 QString
154 Layer::AddMeasurementRectCommand::getName() const
155 {
156 return tr("Make Measurement");
157 }
158
159 void
160 Layer::AddMeasurementRectCommand::execute()
161 {
162 m_layer->addMeasureRect(m_rect);
163 }
164
165 void
166 Layer::AddMeasurementRectCommand::unexecute()
167 {
168 m_layer->deleteMeasureRect(m_rect);
169 }
170
171 void
172 Layer::measureStart(View *v, QMouseEvent *e)
173 {
174 m_draggingRect.pixrect = QRect(e->x(), e->y(), 0, 0);
175 if (hasTimeXAxis()) {
176 m_draggingRect.haveFrames = true;
177 m_draggingRect.startFrame = v->getFrameForX(e->x());
178 m_draggingRect.endFrame = m_draggingRect.startFrame;
179 } else {
180 m_draggingRect.haveFrames = false;
181 }
182 m_haveDraggingRect = true;
183 }
184
185 void
186 Layer::measureDrag(View *v, QMouseEvent *e)
187 {
188 if (!m_haveDraggingRect) return;
189
190 m_draggingRect.pixrect = QRect(m_draggingRect.pixrect.x(),
191 m_draggingRect.pixrect.y(),
192 e->x() - m_draggingRect.pixrect.x(),
193 e->y() - m_draggingRect.pixrect.y())
194 .normalized();
195
196 if (hasTimeXAxis()) {
197 m_draggingRect.endFrame = v->getFrameForX(e->x());
198 }
199 }
200
201 void
202 Layer::measureEnd(View *v, QMouseEvent *e)
203 {
204 if (!m_haveDraggingRect) return;
205 measureDrag(v, e);
206
207 CommandHistory::getInstance()->addCommand
208 (new AddMeasurementRectCommand(this, m_draggingRect));
209
210 m_haveDraggingRect = false;
211 }
212
213 void
214 Layer::paintMeasurementRects(View *v, QPainter &paint) const
215 {
216 if (m_haveDraggingRect) {
217 paintMeasurementRect(v, paint, m_draggingRect);
218 }
219
220 for (MeasureRectSet::const_iterator i = m_measureRects.begin();
221 i != m_measureRects.end(); ++i) {
222 paintMeasurementRect(v, paint, *i);
223 }
224 }
225
226 void
227 Layer::paintMeasurementRect(View *v, QPainter &paint, MeasureRect &r)
228 {
229 if (r.haveFrames) {
230
231 int x0 = -1;
232 int x1 = v->width() + 1;
233
234 if (r.startFrame >= v->getStartFrame()) {
235 x0 = v->getXForFrame(r.startFrame);
236 }
237 if (r.endFrame <= v->getEndFrame()) {
238 x1 = v->getXForFrame(r.endFrame);
239 }
240
241 QRect pr = QRect(x0, r.pixrect.y(),
242 x1 - x0, r.pixrect.height());
243
244 r.pixrect = pr;
245 }
246
247 v->drawMeasurementRect(paint, this, r.pixrect);
72 } 248 }
73 249
74 QString 250 QString
75 Layer::toXmlString(QString indent, QString extraAttributes) const 251 Layer::toXmlString(QString indent, QString extraAttributes) const
76 { 252 {
86 .arg(getObjectExportId(getModel())) 262 .arg(getObjectExportId(getModel()))
87 .arg(extraAttributes); 263 .arg(extraAttributes);
88 264
89 return s; 265 return s;
90 } 266 }
91
92 PlayParameters *
93 Layer::getPlayParameters()
94 {
95 // std::cerr << "Layer (" << this << ", " << objectName().toStdString() << ")::getPlayParameters: model is "<< getModel() << std::endl;
96 const Model *model = getModel();
97 if (model) {
98 return PlayParameterRepository::getInstance()->getPlayParameters(model);
99 }
100 return 0;
101 }
102
103 void
104 Layer::setLayerDormant(const View *v, bool dormant)
105 {
106 const void *vv = (const void *)v;
107 QMutexLocker locker(&m_dormancyMutex);
108 m_dormancy[vv] = dormant;
109 }
110
111 bool
112 Layer::isLayerDormant(const View *v) const
113 {
114 const void *vv = (const void *)v;
115 QMutexLocker locker(&m_dormancyMutex);
116 if (m_dormancy.find(vv) == m_dormancy.end()) return false;
117 return m_dormancy.find(vv)->second;
118 }
119
120 void
121 Layer::showLayer(View *view, bool show)
122 {
123 setLayerDormant(view, !show);
124 emit layerParametersChanged();
125 }
126
127 bool
128 Layer::getXScaleValue(const View *v, int x, float &value, QString &unit) const
129 {
130 if (!hasTimeXAxis()) return false;
131
132 const Model *m = getModel();
133 if (!m) return false;
134
135 value = float(v->getFrameForX(x)) / m->getSampleRate();
136 unit = "s";
137 return true;
138 }
139
140 void
141 Layer::measureStart(View *v, QMouseEvent *e)
142 {
143 m_draggingRect.pixrect = QRect(e->x(), e->y(), 0, 0);
144 if (hasTimeXAxis()) {
145 m_draggingRect.startFrame = v->getFrameForX(e->x());
146 m_draggingRect.endFrame = m_draggingRect.startFrame;
147 }
148 m_haveDraggingRect = true;
149 }
150
151 void
152 Layer::measureDrag(View *v, QMouseEvent *e)
153 {
154 if (!m_haveDraggingRect) return;
155 m_draggingRect.pixrect = QRect(m_draggingRect.pixrect.x(),
156 m_draggingRect.pixrect.y(),
157 e->x() - m_draggingRect.pixrect.x(),
158 e->y() - m_draggingRect.pixrect.y());
159 if (hasTimeXAxis()) {
160 m_draggingRect.endFrame = v->getFrameForX(e->x());
161 }
162 }
163
164 void
165 Layer::measureEnd(View *v, QMouseEvent *e)
166 {
167 //!!! command
168 if (!m_haveDraggingRect) return;
169 measureDrag(v, e);
170 m_measureRectList.push_back(m_draggingRect);
171 m_haveDraggingRect = false;
172 }
173
174 void
175 Layer::paintMeasurementRects(View *v, QPainter &paint) const
176 {
177 if (m_haveDraggingRect) {
178 v->drawMeasurementRect(paint, this, m_draggingRect.pixrect);
179 }
180
181 bool timex = hasTimeXAxis();
182
183 for (MeasureRectList::const_iterator i = m_measureRectList.begin();
184 i != m_measureRectList.end(); ++i) {
185
186 if (timex) {
187 int x0 = v->getXForFrame(i->startFrame);
188 int x1 = v->getXForFrame(i->endFrame);
189 QRect pr = QRect(x0, i->pixrect.y(), x1 - x0, i->pixrect.height());
190 i->pixrect = pr;
191 }
192
193 v->drawMeasurementRect(paint, this, i->pixrect);
194 }
195 }
196