File: viewchanger.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (444 lines) | stat: -rw-r--r-- 12,247 bytes parent folder | download | duplicates (2)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
// viewchanger.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 "application.h"
#include "viewchanger.h"
#include "controller.h"
#include "query_templates.h"
#include "editor.h"
#include "dataview.h"
#include "data.h"
#include "valuerequester.h"
#include "smpte.h"

template <class T>
class MinMaxRequester : public TitledRequester {
public:
	MinMaxRequester(const char* title,
	                const char* minlabel, T& minval, const Range,
	                const char* maxlabel, T& maxval, const Range);
protected:
	redefined void configureRequest(Request *);
protected:
	const char* minLabel;
	const char* maxLabel;
	T &minVal;
	T &maxVal;
	const Range minRange;
	const Range maxRange;
};

template <class T>
MinMaxRequester<T>::MinMaxRequester(const char* title,
		const char* minlabel, T& minval, const Range minrange,
		const char* maxlabel, T& maxval, const Range maxrange)
	: TitledRequester(title), minLabel(minlabel), maxLabel(maxlabel),
	  minVal(minval), maxVal(maxval),
	  minRange(minrange), maxRange(maxrange) {}

template <class T>
void
MinMaxRequester<T>::configureRequest(Request* request) {
	request->appendValue(minLabel, &minVal, minRange);
	request->appendValue(maxLabel, &maxVal, maxRange);
}

//********

ViewChanger::ViewChanger(DataView* view) : myView(view) {
	myView->ref();
}

ViewChanger::~ViewChanger() { Resource::unref(myView); }

Data *
ViewChanger::viewData() { return myView->dataptr; }

int
ViewChanger::apply() {
	return doApply(myView);
}

//********

ScaleOptionsSetter::ScaleOptionsSetter(DataView* view)
	: ViewChanger(view), smpteFormat(SMPTE::currentFormat()) {}

Requester *
ScaleOptionsSetter::createRequester() {
	return new ChoiceRequester("Set Horizontal Scale Options:",
	                    "SMPTE Frame Format:",
	                    SMPTE::formatNames(),
	                    smpteFormat);
}

int
ScaleOptionsSetter::doApply(DataView* view) {
	SMPTE::setFormat(SMPTE::Format(smpteFormat));
	view->resetHorizontalScale();
	return true;
}

//********

VisibleFrameRange::VisibleFrameRange(DataView* view) : ViewChanger(view),
	  frameRange(view->getVisibleFrameRange()),
	  minFrame(frameRange.intMin()), maxFrame(frameRange.intMax()) {}

Requester *
VisibleFrameRange::createRequester() {
	if(myView->frameScaleInFrames()) {
		const Range startRange = viewData()->frameRange(FrameUnit);
		const Range endRange(1, startRange.intMax());
		return new MinMaxRequester<int>(
			"Set Endpoints for Frame Display:",
			"Start Frame:", minFrame, startRange,
			"End Frame:", maxFrame, endRange
		);
	}
	else {
		// convert frame range to time range,
		// i.e., frames (0 -> n-1) becomes (0 -> n) / samprate 
		frameRange.set(frameRange.min(), frameRange.max() + 1);
		frameRange *= 1.0/viewData()->frameRate();
		const Range timeRange = viewData()->frameRange(FrameTimeUnit);
		return new MinMaxRequester<double>(
			"Set Endpoints for Frame Display:",
			"Start Time:", frameRange.min_(), timeRange,
			"End Time:", frameRange.max_(), timeRange
		);
	}
}

int
VisibleFrameRange::doApply(DataView* view) {
	if(!view->frameScaleInFrames())
		frameRange *= viewData()->frameRate();
	else frameRange.set(minFrame, maxFrame);

	view->setVisibleFrameRange(frameRange);
	return true;
}

//********

class InsertPointRequester : public  MinMaxRequester<int> {
public:
	InsertPointRequester(int& point, int& minVal, int& maxVal);
protected:
	redefined void configureRequest(Request *);
private:
	int& thePoint;
};

InsertPointRequester::InsertPointRequester(int& point, int& minVal, int& maxVal)
	  : MinMaxRequester<int>(
	  	"Set Insert Point:",
	  	"First Channel:", minVal, NonNegativeIntegers,
	  	"Last Channel:", maxVal, NonNegativeIntegers
	  ),
	  thePoint(point) {}
	 
void
InsertPointRequester::configureRequest(Request *request) {
	request->appendValue("Frame:", &thePoint, NonNegativeIntegers);
	MinMaxRequester<int>::configureRequest(request);
}


//********

class InsertTimeRequester : public MinMaxRequester<int> {
public:
	InsertTimeRequester(float& time, int& minVal, int& maxVal);
protected:
	redefined void configureRequest(Request *);
private:
	float& theTime;
};

InsertTimeRequester::InsertTimeRequester(float& time, int& minVal, int& maxVal)
	  : MinMaxRequester<int>(
	  	"Set Insert Time",
	  	"First Channel:", minVal, NonNegativeIntegers,
	  	"Last Channel:", maxVal, NonNegativeIntegers
	  ),
	  theTime(time) {}

void
InsertTimeRequester::configureRequest(Request *request) {
	request->appendValue("Time:", &theTime, NonNegativeNumbers),
	MinMaxRequester<int>::configureRequest(request);
}

//********

InsertPoint::InsertPoint(DataView* view)
		: ViewChanger(view), insertPoint(0), insertTime(0) {
	Range channelRange = view->getChannelRange();
	minChan = channelRange.intMin();
	maxChan = channelRange.intMax();
}

Requester *
InsertPoint::createRequester() {
	if(myView->frameScaleInFrames())
		return new InsertPointRequester(insertPoint, minChan, maxChan);
	else
		return new InsertTimeRequester(insertTime, minChan, maxChan);
}

int
InsertPoint::doApply(DataView* view) {
	insertPoint = view->frameScaleInFrames() ? insertPoint :
		round(insertTime * viewData()->frameRate());
	int status = true;
	if(insertPoint > viewData()->length()) {
		Response r = Application::choice(
			"This insert point is beyond the end of the file.",
			nil, nil, No,
			"extend file", "adjust insert", "cancel");
		switch(r) {
		case Yes:	// extend it
			status = viewData()->changeLength(insertPoint + 1);
			if(status) viewData()->Notify();
			else Application::alert("Failed to extend file.",
			                        "Cancelling command.");
			break;
		case No:	// adjust insert
			insertPoint = viewData()->length() - 1;
			break;
		case Cancel:
		default:
			status = false;
		}
	}
	if(status)
		view->setInsertPoint(insertPoint, Range(minChan, maxChan));
	return status;
}

//********

class EditFramesRequester : public MinMaxRequester<int> {
public:
	EditFramesRequester(int&, int&, int&, int&, const Range);
protected:
	redefined void configureRequest(Request *);
private:
	int& minFrame;
	int& maxFrame;
};

EditFramesRequester::EditFramesRequester(int& minframe, int& maxframe,
                                         int& startchan, int& endchan,
                                         const Range channelRange)
	: MinMaxRequester<int>(
		"Set Edit Region:",
		"Start Channel:", startchan, channelRange,
		"End Channel:", endchan, channelRange),
		 minFrame(minframe), maxFrame(maxframe) {}

void
EditFramesRequester::configureRequest(Request *request) {
	request->appendValue("Start Frame:", &minFrame, NonNegativeIntegers);
	request->appendValue("End Frame:", &maxFrame, PositiveIntegers);
	MinMaxRequester<int>::configureRequest(request);
}

class EditTimeRequester : public MinMaxRequester<int> {
public:
	EditTimeRequester(double&, double&, int&, int&, const Range);
protected:
	redefined void configureRequest(Request *);
private:
	double& minTime;
	double& maxTime;
};

EditTimeRequester::EditTimeRequester(
		double& mintime, double& maxtime,
		int& startchan, int& endchan,
		const Range channelRange)
	: MinMaxRequester<int>(
		"Set Edit Region:",
		"Start Channel:", startchan, channelRange,
		"End Channel:", endchan, channelRange),
		 minTime(mintime), maxTime(maxtime) {}

void
EditTimeRequester::configureRequest(Request *request) {
	request->appendValue("Start Time:", &minTime, NonNegativeNumbers);
	request->appendValue("End Time:", &maxTime, PositiveNumbers);
	MinMaxRequester<int>::configureRequest(request);
}

//********

EditRegion::EditRegion(DataView* view)
		: ViewChanger(view),
		  inFrames(view->frameScaleInFrames()),
		  editRange(view->getVisibleFrameRange()) {

	if(inFrames) {
		minFrame = editRange.intMin();
		maxFrame = editRange.intMax();
	}
	else {
		// increase by one to cvt to time range from frame range
		editRange.set(editRange.intMin(), editRange.intMax() + 1);
		editRange *= 1.0/viewData()->frameRate();
	}
	
	Range channelRange = view->getChannelRange();
	minChan = channelRange.intMin();
	maxChan = channelRange.intMax();
}

Requester *
EditRegion::createRequester() {
	const Range chanRange = viewData()->channelRange();
	if(inFrames)
		return new EditFramesRequester(
			minFrame, maxFrame, minChan, maxChan, chanRange
		);
	else
		return new EditTimeRequester(
			editRange.min_(), editRange.max_(), minChan, maxChan, chanRange
		);
}

int
EditRegion::doApply(DataView* view) {
	int status = true;
	if(inFrames)
		editRange.set(minFrame, maxFrame);
	else {
		editRange *= viewData()->frameRate();
		// reduce by one to cvt from time range to frame range
		editRange.set(editRange.intMin(), editRange.intMax() - 1);
	}
	int len = viewData()->length();
	if(len <= editRange) {
		Response r = Application::choice(
			"This edit region extends beyond the end of the file.",
			nil, nil, No,
			"extend file", "adjust region", "cancel");
		switch(r) {
		case Yes:	// extend it
			status = viewData()->changeLength(editRange.intMax());
			if(status) viewData()->Notify();
			else Application::alert("Failed to extend file.",
			                        "Cancelling command.");
			break;
		case No:	// truncate region
			editRange.set(editRange.intMin(), len);
			break;
		case Cancel:
		default:
			status = false;
		}
	}
	if(status) {
		Range channelRange(minChan, maxChan);
		view->setEditRegion(editRange, channelRange);
	}
	return status;
}

//********

ChannelRange::ChannelRange(DataView* view, const Range& current)
		: ViewChanger(view), displayRange(current) {
	minVal = current.intMin();
	maxVal = current.intMax();
}

int
ChannelRange::doApply(DataView* view) {
	view->setChannelRange(displayRange);
	return true;
}

//********

ChannelChannelRange::ChannelChannelRange(DataView* view)
	: ChannelRange(view, view->getChannelRange()) {}

Requester *
ChannelChannelRange::createRequester() {
	const Range allChans = viewData()->channelRange();
	return new MinMaxRequester<int>(
		"Set Channel Display Range:",
		"First Channel:", minVal, allChans,
	   	"Last Channel:", maxVal, allChans);
}

void
ChannelChannelRange::initialize() {
	displayRange.set(minVal, maxVal);
	Super::initialize();
}

//********

FrameChannelRange::FrameChannelRange(DataView* view)
		: ChannelRange(view, view->getDisplayedHorizRange()) {
	maxFrames = displayRange.max();
}

Requester *
FrameChannelRange::createRequester() {
	if(myView->horizScaleInFrames()) {
		const Range allFrames = viewData()->frameRange();
		return new MinMaxRequester<int>(
				"Set Horizontal Display Range:",
				"Start:", minVal, allFrames,
				"End:", maxVal, allFrames
		);
	}
	else
		return new MinMaxRequester<double>(
				"Set Horizontal Display Range:",
				"Start:", displayRange.min_(), NonNegativeNumbers,
				"End:", displayRange.max_(), PositiveNumbers
		);
}

void
FrameChannelRange::initialize() {
	int totalFrames = viewData()->frameLength();
	boolean inIntegers = myView->horizScaleInFrames();
	double min = inIntegers ? minVal
		: round(totalFrames * displayRange.min()/maxFrames);
	double max = inIntegers ? maxVal
		: round(totalFrames * displayRange.max()/maxFrames);
	displayRange.set(min, max);
	Super::initialize();
}