File: channelview.C

package info (click to toggle)
mixviews 1.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 6,314
  • sloc: cpp: 31,647; ansic: 2,100; makefile: 1,782; sh: 17
file content (404 lines) | stat: -rw-r--r-- 10,457 bytes parent folder | download
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
// channelview.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/box.h>		/* FIX ME: put all box stuff in base class */
#include <InterViews/perspective.h>
#include "block.h"
#include "channelview.h"
#include "channelgraph.h"
#include "data.h"
#include "range.h"
#include "scale.h"
#include "viewchanger.h"
#include <X11/keysym.h>

class ChannelRangeBlock : public Block {
	Range range;
public:
	ChannelRangeBlock(const Range &r) : range(r) {}
	redefined void doIt(Interactor *i) {
		ChannelGraph *c = (ChannelGraph *) i;
		if(range.includes(c->currentChannel()))
			doThis(i);
	}
	virtual void doThis(Interactor *i) = 0;
};

//********

// some private inline methods

inline int
ChannelView::lowestChannel() {
	return channelsShown->cury;
}

inline void
ChannelView::setLowestChannel(int chan) {
	channelsShown->cury = chan;
}

inline int
ChannelView::highestChannel() {
	return channelsShown->cury + channelsShown->curheight - 1;
}

inline int
ChannelView::nchans() {
	return channelsShown->curheight;
}

inline void
ChannelView::setNChans(int chans) {
	channelsShown->curheight = chans;
}

inline int
ChannelView::maxChans() { return dataptr->channels(); }

ChannelView::ChannelView(Controller *c, ViewInfo &info) : DataView(c, info) {
	Init(info);
}

ChannelView::~ChannelView() {
	Resource::unref(channelsShown);
}

void
ChannelView::Init(ViewInfo &info) {
	BUG("ChannelView::Init()");
	SetClassName("ChannelView");
	initPerspective();
	// initialize vertical (channel) perspective
	channelsShown = new Perspective;
	channelsShown->Init(0, 0, 0, maxChans());
	setNChans(0);
	Range chanrange = (!info.channelRange.isNegative()) ?
		info.channelRange : dataptr->channelRange();
	setChannelRange(chanrange);
	if(!info.frameRange.isNegative())
		setVisibleFrameRange(info.frameRange);
}

void
ChannelView::initPerspective() {
	BUG("ChannelView::initPerspective()");
	int width = dataptr->length();
	static const int fixedHeight = 1048576;	// arbitrary large power of 2
	perspective->Init(0, 0, width, fixedHeight);
	perspective->curx = 0;
	perspective->cury = 0;
	const char *a = GetAttribute(plotWidthAttribute());
	if (a != nil)
		perspective->curwidth = min(atoi(a), perspective->width);
	else perspective->curwidth = width;
	perspective->curheight = fixedHeight;
	perspective->lx = perspective->curwidth;
	perspective->ly = 0;
	perspective->sx = perspective->lx/4;	// scroll incr. is 1/4 screen
	perspective->sy = 0;
}

void
ChannelView::checkPerspective() {
	Perspective np = *perspective;
	// the length of the data is all that could have changed
	np.width = dataptr->length();
	*perspective = constrainView(np);
}

void
ChannelView::constrainVerticalView(Perspective& np) {
	register Perspective* p = perspective;
	np.height = p->height;
	np.cury = 0;
	np.curheight = max(np.curheight, 1);	// always
}

boolean
ChannelView::keyCommand(unsigned long sym) {
	boolean interested = true;
	switch (sym) {
	case XK_plus:
		addChannel();
		break;
	case XK_minus:
		removeChannel();
		break;
	case XK_numbersign:
		setHorizScaleTo(FrameUnit);
		break;
	case XK_at:
		setHorizScaleTo(FrameTimeUnit);
		break;
	case XK_exclam:
		setHorizScaleTo(FrameSmpteUnit);
		break;
	case XK_twosuperior:
		setScaleOptions();
		break;
	default:
		interested = DataView::keyCommand(sym);
		break;
	}
	return interested;
}

void
ChannelView::shiftChannelViewUp() {
	Range chans = getChannelRange();
	chans += dataptr->frameChannels();
	setChannelRange(chans);
}

void
ChannelView::shiftChannelViewDown() {
	Range chans = getChannelRange();
	chans += -(dataptr->frameChannels());
	setChannelRange(chans);
}

int
ChannelView::graphsVisible() {
	return nchans();
}

void
ChannelView::incrementGraphsVisible() {
	DataView::incrementGraphsVisible();
	setNChans(nchans() + 1); 
}

void
ChannelView::decrementGraphsVisible() {
	DataView::decrementGraphsVisible();
	setNChans(nchans() - 1); 
}

void
ChannelView::setChannelRange(const Range& chrange) {
	Range range = chrange;
//	1 <= number of chans shown <= maxChans
	int newnchans = min(range.size(), maxChans());
	int oldnchans = graphsVisible();
	int i;
	for(i = oldnchans; i > newnchans; i--)	// remove extras if any
		removeGraph(false);
	int newmax = max(0, min(maxChans() - 1, range.intMax()));
	int newmin = max(0, min(maxChans() - newnchans, range.intMin()));
	range.set(newmin, newmax);
	struct ChanSetter : public Block {
		int chan, maxchan;
		ChanSetter(int startingChan, int endingChan)
			: chan(startingChan), maxchan(endingChan) {}
		redefined void doIt(Interactor *i) {
			ChannelGraph *c = (ChannelGraph *) i;
			if(c->currentChannel() != chan) {
				c->setChannel(chan);
				Scale* scale = c->vScale();
				scale->setLabel(c->verticalScaleLabel());
				scale->Update();	// this should be done by the graph
			}
			if(++chan > maxchan)
				done = true;
		}
	} setter(newmin, newmax);
	doForGraphs(setter);
	setLowestChannel(range.intMin());
	for(i = graphsVisible(); i < newnchans; i++)	// add new if needed
		addChannel(lowestChannel() + i);
	if(newnchans != oldnchans)
		doChange();
}

Range
ChannelView::getChannelRange() {
	return Range(lowestChannel(), highestChannel());
}

void
ChannelView::addChannel() {
	int newchan = lowestChannel() + graphsVisible();
	if(newchan < maxChans()) {
		addGraph(new ChannelGraph(controller, dataptr, newchan));
		doChange();	// public version calls this internally
	}
}

// protected internal versions of this	(these do not do the update)

void
ChannelView::addChannel(int chan) {
	addGraph(new ChannelGraph(controller, dataptr, chan));
}

void
ChannelView::addChannel(int chan, Range &range) {
	addGraph(new ChannelGraph(controller, dataptr, chan, range));
}

void
ChannelView::addGraph(Graph *graph) {
	if(graphsVisible()) {
		ChannelGraph* model = (ChannelGraph *) topGraph();
		ChannelGraph* cgraph = (ChannelGraph *) graph;
		cgraph->useLines = model->useLines;
		// FIX ME:  encapsulate in Style class
	}
	DataView::addGraph(graph);
}

// remove one channel from frame -- always the highest channel displayed

void
ChannelView::removeChannel() {
	removeGraph(true);
}

Range
ChannelView::getVisibleFrameRange() {
	return Range(shown->curx, shown->curx + shown->curwidth - 1);
}

Range
ChannelView::getHorizScaleRange() {
	return hscale->getRange();
}

void
ChannelView::setVisibleFrameRange(const Range &range) {
	BUG("ChannelView::setVisibleFrameRange()");
	Perspective np = *perspective;
	np.curx = range.intMin();
	np.curwidth = range.size();
	Adjust(np);
}

void
ChannelView::toggleGraphMode() {
    struct PlotToggle : public Block {
		redefined void doIt( Interactor *i ) {
			ChannelGraph *g = (ChannelGraph *) i;
			g->togglePlotMode();
		}
    } toggle;
    doForGraphs( toggle );
}

void
ChannelView::setInsertPoint(int point, const Range& chans, boolean scroll) {
	Range chansToSelect = chans;
	unselect();
	Range framesVisible = getVisibleFrameRange();
	Range chansVisible = getChannelRange();
	if(!framesVisible.includes(point)) {
		int margin = int(framesVisible.spread() / 2);
		framesVisible.set(point - margin, point + margin);
		setVisibleFrameRange(framesVisible);
	}		
	if(!chansVisible.includes(chansToSelect) && scroll == true) {
		Range displayChans = chansToSelect;
		displayChans.expandBy(1);
		setChannelRange(displayChans);
	}
    struct InsertSetter : public ChannelRangeBlock {
		int iPoint;
		InsertSetter(const Range &range, int pt)
			: ChannelRangeBlock(range), iPoint(pt) {}
		redefined void doThis( Interactor *i ) {
			Graph *g = (Graph *) i;
			g->setInsertPoint(iPoint);
		}
    } setter(chansToSelect, point);
    doForGraphs( setter );
}

void
ChannelView::setEditRegion(const Range& region, const Range& chans, boolean scroll) {
	Range chansToSelect = chans;
	unselect();
	Range framesVisible = getVisibleFrameRange();
	Range chansVisible = getChannelRange();
	if(!framesVisible.includes(region)) {
		int width = int(framesVisible.spread());
		int newidth = int(region.spread());
		if(newidth > width) {	// widen framesVisible to include region
			framesVisible = region;
			framesVisible.expandBy(int(newidth/2));
		}
		else {		// shift visible range to include region
			double shift = region.intMin()-framesVisible.intMin() - (width-newidth)/2;
			framesVisible += int(shift);
		}
		setVisibleFrameRange(framesVisible);
	}
	if(!chansVisible.includes(chansToSelect) && scroll == true) {
		Range displayChans = chansToSelect;
		displayChans.expandBy(1);
		setChannelRange(displayChans);
	}
    struct RegionSetter : public ChannelRangeBlock {
		Range setregion;
		RegionSetter(const Range &range, const Range &region)
			: ChannelRangeBlock(range), setregion(region) {}
		redefined void doThis( Interactor *i ) {
			Graph *g = (Graph *) i;
			g->setEditRegion(setregion);
		}
    } setter(chansToSelect, region);
    doForGraphs( setter );
}

void
ChannelView::setChannelRange() {
	ChannelChannelRange changer(this);
	if(changer.configure(controller))
		changer.apply();
}

void
ChannelView::setScaleOptions() {
	ScaleOptionsSetter setter(this);
	if(setter.configure(controller))
		setter.apply();
}

#if !defined(sgi) || defined(__GNUG__)

void
ChannelView::resetHorizontalScale() {
	RangeUnit units = horizRangeUnits();
	if(units != FrameUnit) {	// if displaying time or SMPTE
		hscale->setRange(dataptr->frameRange(units));
		hscale->Update();
	}
}

#endif