File: framegraph.C

package info (click to toggle)
mixviews 1.20-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,920 kB
  • ctags: 5,958
  • sloc: cpp: 32,873; ansic: 2,110; makefile: 411; sh: 17
file content (431 lines) | stat: -rw-r--r-- 11,615 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// framegraph.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include "localdefs.h"
#include <InterViews/brush.h>
#include <InterViews/cursor.h>
#include <InterViews/event.h>
#include <InterViews/rubline.h>
#ifdef index
#define was_index index
#undef index
#define mxv_index_undefined
#endif
#include <InterViews/font.h>
#ifdef mxv_index_undefined
#undef mxv_index_undefined
#define index was_index
#endif
#include <InterViews/painter.h>
#include <InterViews/perspective.h>
#include <InterViews/sensor.h>
#include <InterViews/shape.h>
#include "framegraph.h"
#include "data.h"
#include "graphplot.h"
#include "scale.h"
#include "selector.h"

FrameGraph::FrameGraph(Controller *c, Data *data, const Range& vertRange) 
		: Graph(c, data, vertRange), graphplots(nil), vScaleUnits(Amplitude) {
	Init();
}

FrameGraph::FrameGraph(Controller *c, Data *data)
		: Graph(c, data, data->limits()), graphplots(nil),
		  vScaleUnits(Amplitude) {
	Init();
}

FrameGraph::~FrameGraph() {
	for(int plot=0; plot < maxPlots; plot++)
		Resource::unref(graphplots[plot]);
	delete [] graphplots;
	delete [] frameNumbers;
	Resource::unref(dashBrush);
}

void
FrameGraph::Init() {
	BUG("FrameGraph::Init()");
	SetClassName("FrameGraph");
	oldFramesVisible = 0;
	frameNumbers = nil;
	dashBrush = nil;
	maxPlots = 0;
	vertGrain = 1.0;
	hshift = 50;		// for now, until variable
	vshift = 200;		// for now, until variable
}

void
FrameGraph::Reconfig() {
	BUG("FrameGraph::Reconfig()");
    static int pattern[] = {2, 2};
	if(!isConfigured()) {
		const char *a = GetAttribute("VerticalPlotOffset");
		if (a != nil) vshift = atoi(a);
		a = GetAttribute("HorizontalPlotOffset");
		if (a != nil) hshift = atoi(a);
		a = GetAttribute("VerticalScaleUnits");
		if (a != nil && !strcmp(a, "Decibels"))
			setVerticalScaleUnits(Decibels);
		SetCanvasType(CanvasSaveContents);	// to avoid redrawing
		dashBrush = new Brush(pattern, 2, 0);
		dashBrush->ref();	// to keep it around
	}
	Graph::Reconfig();			// default or attribute-set size done here
}

inline double
FrameGraph::currentVGrain() {
	return vertGrain;
}

inline int
FrameGraph::vertOriginLocation() {
	return Graph::vertOriginLocation() + vertShift();
}

inline const char *
FrameGraph::verticalScaleLabel() {
	return scaleInAmplitude() ? "Magnitude" : "Decibels";		
}

inline int
FrameGraph::framesVisible() {
	return min(perspective->curheight, maxPlots);
}

inline int
FrameGraph::channelsVisible() {
	return perspective->curwidth;
}

inline int
FrameGraph::frameSize() {
	return graphData->frameLength();
}

void
FrameGraph::setShape() {	// called from Graph::Reconfig()
	shape->width += horizShift();
	shape->height += vertShift();
	shape->Rigid(max(0, shape->width - (horizShift() * 2)), hfil,
				 max(0, shape->height/2 - vertShift()), vfil);
}

void
FrameGraph::Redraw(Coord left, Coord bottom, Coord right, Coord top) {
	BUG("FrameGraph::Redraw()");
	output->Clip(canvas, left, bottom, right, top);
	output->ClearRect(canvas, left, bottom, right, top);
	int totalHorizOffset = horizShift();
	int hOrigin = horizOriginLocation();
	int totalVertOffset = vertShift();
	int vOrigin = vertOriginLocation();
	int frameEnd = framesVisible();
	const Brush* oldBrush = output->GetBrush();
	oldBrush->ref();	// to keep it around
	output->SetBrush(dashBrush);
    // draw dotted z origin
	output->Line(canvas, hOrigin, ymax - topBorder(),
		hOrigin, ymax - topBorder() - currentHeight()); 
	output->SetBrush(oldBrush);
	char frameLabel[16];
	const char *label = nil;
	const Font* font = output->GetFont();
	int voffset = -(font->Height() / 2);
	for(int frame=0; frame < frameEnd; frame++) {	// draw plots and labels
		double fraction = double(frame)/frameEnd;
		int horizLoc = hOrigin + int(totalHorizOffset * fraction);
		int vertLoc = vOrigin - int(totalVertOffset * fraction);
		output->SetOrigin(horizLoc, vertLoc);
		// check each plot to see if it needs to be redrawn
		if(graphplots[frame]->contains(left - horizLoc, bottom - vertLoc,
				right - horizLoc, top - vertLoc)) {
			if((label = getLabel(frameLabel, frame)) != NULL) {
				output->Text(canvas, label,
					(-2 - font->Width(label)), voffset);
			}
			graphplots[frame]->draw(canvas, output);
			output->SetBrush(dashBrush);
			output->Line(canvas, 0, 0, currentWidth(), 0);	// base line
			output->SetBrush(oldBrush);
		}
	}
	output->SetOrigin(0, 0);
    // draw dotted y origin
	output->SetBrush(dashBrush);
	output->Line(canvas, hOrigin, vOrigin,
		hOrigin + totalHorizOffset, vOrigin - totalVertOffset); 
	output->SetBrush(oldBrush);
	output->NoClip();
	Resource::unref(oldBrush);
}

// if more than two areas need to be redrawn, it is more efficient to just
// redraw the entire window.  This will be further optimized in the future.

void FrameGraph::RedrawList(
	int n, IntCoord left[], IntCoord bottom[], IntCoord right[], IntCoord top[]
	) {
	if(n > 2)
		Draw();
	else
		Graph::RedrawList(n, left, bottom, right, top);
}

// this is called by Graph::Resize() prior to loading plots

void
FrameGraph::doResize() {
	BUG("FrameGraph::doResize()");
	int newMax = vertShift()/2; // plots can be spaced every 2nd vertical pixel
	int oldMax = maxPlots;
	if(newMax > oldMax) {
		GraphPlot **newplots = new GraphPlot *[newMax];	// create slots
		for(int plot = 0; plot < newMax; plot++) {
			if(plot < oldMax && graphplots[plot] != nil)
				newplots[plot] = graphplots[plot];	// copy plots to new slots
			else
				newplots[plot] = nil;				// and zero new slots
		}
		delete [] graphplots;		// delete old slot array
		graphplots = newplots;		// and copy new array to old
		maxPlots = newMax;
	}
}

void
FrameGraph::Update() {
	BUG("FrameGraph::Update()");
	Graph::Update();
}

boolean
FrameGraph::isViewChanged(Perspective &np) {
	register Perspective *p = perspective;
	return (
		np.curwidth != p->curwidth
		|| np.width != p->width
		|| np.curx != p->curx
		|| np.curheight != p->curheight
		|| np.height != p->height
		|| np.cury != p->cury
	);
}

// compiler sez "sorry, not implemented: aggregate value in COND_EXPR" -- UGH!
// so cant use () ? : here.

void
FrameGraph::setReferenceRange() {
	if(scaleInAmplitude())
		Graph::setReferenceRange(graphData->limits());
	else
		Graph::setReferenceRange(Range(0.0, 90.31));
}

void
FrameGraph::setVerticalGrain(double newgrain) {
	BUG("FrameGraph::setVerticalGrain()");
	if(newgrain != vertGrain) {
		vertGrain = newgrain;
		doAdjust();
		Draw();
		if(vscale) {
			vscale->setRange(verticalScaleRange());
			vscale->Update();
		}
	}
}

void
FrameGraph::setVerticalScaleUnits(VerticalScaleUnits units) {
	if(units != vScaleUnits) {
		vScaleUnits = units;
		Update();
		if(vscale) {
			vscale->setLabel(verticalScaleLabel());
			vscale->Update();
		}
	}
}

void
FrameGraph::createPlot() {
	BUG("FrameGraph::createPlot()");
	for(int frame = 0; frame < maxPlots; frame++) {
		Resource::unref(graphplots[frame]);
		graphplots[frame] = new SolidPlot(this, currentWidth());
		graphplots[frame]->ref();
	}
	delete [] frameNumbers;
	frameNumbers = new int[maxPlots];
	oldFramesVisible = maxPlots;
}

void
FrameGraph::loadPlot() {
	BUG("FrameGraph::loadPlot()");
	register Perspective *p = GetPerspective();
	int framesize = min(frameSize(), channelsVisible());
	int frameoffset = p->curx;
	double frameIncrement = currentFrameGrain();
	int startFrame = p->cury;
	for(int plot = 0, frame = 0; plot < framesVisible(); plot++) {
		GraphPlot* graphplot = graphplots[plot];
		graphplot->setPlotLength(min(framesize, currentWidth()));
		frame = startFrame + int(plot * frameIncrement);
		frameNumbers[plot] = frame;		// for later display
		Data* frameData = graphData->cloneFrame(
			frame, Range(frameoffset, frameoffset + framesize - 1)
		);
		frameData->ref();
		graphplot->load(frameData,
			scaleInAmplitude() ? PeakToPeakAmplitude : ::Decibels);
		Resource::unref(frameData);
	}
}

void
FrameGraph::doPlot() {
	for(int frame=0; frame < framesVisible(); frame++)
		graphplots[frame]->plot();
}

// currentWidth redefined to represent the width of each plot rather than
// the width of the canvas

int
FrameGraph::currentWidth() {
	return Graph::currentWidth() - horizShift();
}

// currentHeight redefined to represent the height of rearmost plot

int
FrameGraph::currentHeight() {
	return Graph::currentHeight() - vertShift();
}

void
FrameGraph::grab(Event &e) {
	if(!e.control_is_down())
		return;
	SetCursor(crosshairs);
    static int pattern[] = {2, 2};
	const Brush* oldBrush = output->GetBrush();
	oldBrush->ref();	// to keep it around
	output->SetBrush(new Brush(pattern, 2, 2));
	Listen(allEvents);
	int horigin = horizOriginLocation();
	int vorigin = vertOriginLocation();
	RubberLine line(output, canvas, horigin, vorigin,
		horigin + horizShift(), vorigin + vertShift());
	line.Track(e.x, e.y);
	int xloc = e.x;
	int yloc = e.y;
    do {
		switch (e.eventType) {
			  case MotionEvent:
				e.target->GetRelative(e.x, e.y, this);
				constrainOffsets(e.x, e.y);
				line.Track(e.x, e.y);
				xloc = e.x;
				yloc = e.y;
				break;
			  default:
				break;
		}
		Read(e);
    } while (e.eventType != UpEvent);
    Listen(input);
	output->SetBrush(oldBrush);
	Resource::unref(oldBrush);
	SetCursor(defaultCursor);
	setAxisOffsets(xloc - horigin, vorigin - yloc);
}

void
FrameGraph::constrainOffsets(int& x, int& y) {
	x = max(horizOriginLocation(), min(xmax / 2, x));
	y = min(vertOriginLocation(), y);
}

void
FrameGraph::setAxisOffsets(int horiz, int vert) {
	setHorizShift(horiz);
	setVertShift(vert);
	doResize();
	createPlot();
	Update();
	Parent()->Change(this);
}
	
void
FrameGraph::setVertShift(int vsh) {
	static const int minVertOffset = 2;
	vshift = min(Graph::currentHeight() - 50, 
		max(vsh, minVertOffset * framesVisible()));
}

void
FrameGraph::setHorizShift(int hsh) {
	hshift = hsh;
}

double
FrameGraph::currentFrameGrain() {
	return max(1.0, double(perspective->curheight)/framesVisible());
}

const char *
FrameGraph::getLabel(char *label, int frame) {
	int frameNumber = frameNumbers[frame];
	int visible = framesVisible();
	double grain = currentFrameGrain();
	int thresh = int(grain);
	boolean retval;
	int increment = 1;
	if(grain == 1.0 && visible < 10)
		retval = true;
	else if(grain <= 1.9 && visible < 50)
		retval = (frameNumber % (increment=5) == 0);
	else if(grain < 3.0 && visible < 100)
		retval = (frameNumber % (increment=10) < thresh);
	else
		retval = (frameNumber % (increment=50) < thresh);
	if(retval) {
		toString(label, int(roundDownToNearest(frameNumber, increment)));
		return label;
	}
	return NULL;
}