summaryrefslogtreecommitdiffstats
path: root/clients/tde/src/widgets/tracewidget.cpp
blob: 1fe3ff4df2cbc0f530af799114fe74584d61882e (plain)
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
//Author:    Timothy Pearson <kb9vqf@pearsoncomputing.net>, (C) 2012
//Copyright: See COPYING file that comes with this distribution

#include "tracewidget.h"

#include <stdlib.h>
#include <cmath>

#include <tqpixmap.h>
#include <tqpainter.h>

#include <tqlabel.h>
#include <tqlayout.h>

#include <klocale.h>

#define VERIFY_TRACE_ARRAY_SIZE if (traceNumber >= m_traceArray.count()) resizeTraceArray(traceNumber+1);

TraceData::TraceData(TQWidget* labelParent) {
	color = TQColor(0, 255, 0);
	numberOfSamples = 0;
	leftEdge = 0;
	rightEdge = 0;
	topEdge = 0;
	bottomEdge = 0;
	traceName = i18n("Unknown");
	horizontalUnits = i18n("Units");
	verticalUnits = i18n("Units");
	enabled = false;

	if (labelParent) {
		infoLabel = new TQLabel(labelParent);
		infoLabel->setPaletteBackgroundColor(labelParent->paletteBackgroundColor());
		infoLabel->setPaletteForegroundColor(color);
		infoLabel->setAlignment(TQt::AlignHCenter|TQt::AlignVCenter|TQt::SingleLine);
		infoLabel->hide();
	}
	else {
		infoLabel = NULL;
	}
}

TraceData::~TraceData() {
	//
}

// RAJA FIXME
// Add cursor support

// RAJA FIXME
// Add offset (x and y) support

// RAJA FIXME
// Add scaling support

void TraceData::drawTrace(TQPainter* p, int graticule_width, int graticule_height) {
	p->setPen(color);

	if ((bottomEdge != topEdge) && (enabled)) {
		// Draw the points
		unsigned int n;
		int x,y,x2,y2;
		for (n=0; n<numberOfSamples-1; n++) {
			x = abs(((positionArray[n]-leftEdge)/(rightEdge-leftEdge))*(graticule_width));
			y = abs(((sampleArray[n]-topEdge)/(bottomEdge-topEdge))*(graticule_height));
			x2 = abs(((positionArray[n+1]-leftEdge)/(rightEdge-leftEdge))*(graticule_width));
			y2 = abs(((sampleArray[n+1]-topEdge)/(bottomEdge-topEdge))*(graticule_height));
			p->drawLine(x, y, x2, y2);
		}
	}
}

GraticuleWidget::GraticuleWidget(TraceWidget* parent, const char* name) : TQWidget(parent, name),
	m_base(parent),
	m_graticulePixmap(0) {
	setBackgroundMode(NoBackground);
	setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding));

	setPaletteBackgroundColor(TQt::black);
	setPaletteForegroundColor(TQColor(0,128,0));
}

GraticuleWidget::~GraticuleWidget() {
	//
}

void GraticuleWidget::updateGraticule() {
	unsigned int d,s,x,y;

	if (m_graticulePixmap) {
		delete m_graticulePixmap;
	}
	m_graticulePixmap = new TQPixmap(width(), height());

	// Draw the graticule into the pixmap
	TQPainter p(m_graticulePixmap);
	p.setPen(TQPen(foregroundColor(), 1, TQt::SolidLine));
	p.fillRect(0, 0, m_graticulePixmap->width(), m_graticulePixmap->height(), backgroundColor());
	p.setPen(TQPen(foregroundColor(), 1, TQt::DotLine));
	if (m_base->m_vertDivs > 0) {
		s = m_graticulePixmap->width() / m_base->m_vertDivs;
		x = 0;
		for (d=0; d<m_base->m_vertDivs; d++) {
			p.drawLine(x, 0, x, m_graticulePixmap->height());
			x += s;
		}
	}
	if (m_base->m_horizDivs > 0) {
		s = m_graticulePixmap->height() / m_base->m_horizDivs;
		y = 0;
		for (d=0; d<m_base->m_horizDivs; d++) {
			p.drawLine(0, y, m_graticulePixmap->width(), y);
			y += s;
		}
	}
	p.setPen(TQPen(foregroundColor(), 1, TQt::SolidLine));
	p.drawRect(0, 0, m_graticulePixmap->width(), m_graticulePixmap->height());

	// Repaint the widget
	repaint();
}

void GraticuleWidget::paintEvent(TQPaintEvent*) {
	TQPainter p(this);

	if (m_graticulePixmap) {
		// Draw the graticule pixmap to erase the widget
		p.drawPixmap(0, 0, *m_graticulePixmap);

		// Draw the traces
		for (uint trace=0;trace<m_base->m_traceArray.count();trace++) {
			m_base->m_traceArray[trace]->drawTrace(&p, m_graticulePixmap->width(), m_graticulePixmap->height());
		}
	}
	else {
		p.fillRect(x(), y(), width(), height(), backgroundColor());
	}
}

void GraticuleWidget::resizeEvent(TQResizeEvent *) {
	updateGraticule();
}

TraceWidget::TraceWidget(TQWidget* parent, const char* name) : TQWidget(parent, name),
	m_horizDivs(0),
	m_vertDivs(0) {
	setBackgroundMode(NoBackground);

	m_primaryLayout = new TQVBoxLayout(this);
	m_graticuleWidget = new GraticuleWidget(this);
	m_primaryLayout->addWidget(m_graticuleWidget);
	m_traceLabelLayout = new TQGridLayout(m_primaryLayout);
	m_traceLabelLayout->addItem(new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum), 0, 255);

	setPaletteBackgroundColor(TQt::black);
	setPaletteForegroundColor(TQColor(0,128,0));
}

TraceWidget::~TraceWidget() {
	for (uint i=0;i<m_traceArray.count();i++) {
		delete m_traceArray[i];
	}
}

void TraceWidget::setNumberOfSamples(uint traceNumber, unsigned int samples) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->numberOfSamples = samples;
	m_traceArray[traceNumber]->sampleArray.resize(samples);
	m_traceArray[traceNumber]->positionArray.resize(samples);

	m_graticuleWidget->updateGraticule();
	updateTraceText();
}

void TraceWidget::setNumberOfHorizontalDivisions(unsigned int divisions) {
	m_horizDivs = divisions;
	m_graticuleWidget->updateGraticule();
	updateTraceText();
}

void TraceWidget::setNumberOfVerticalDivisions(unsigned int divisions) {
	m_vertDivs = divisions;
	m_graticuleWidget->updateGraticule();
	updateTraceText();
}

void TraceWidget::setDisplayLimits(uint traceNumber, double x, double y, double w, double h) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->leftEdge = x;
	m_traceArray[traceNumber]->rightEdge = w;
	m_traceArray[traceNumber]->topEdge = y;
	m_traceArray[traceNumber]->bottomEdge = h;

	updateTraceText();
}

void TraceWidget::updateTraceText() {
	// RAJA FIXME
	// Display current scaling and offset values for all traces

	// RAJA FIXME
	// Display upper/lower/left/right boundary values,
	// possibly in a different routine

	for (uint trace=0;trace<m_traceArray.count();trace++) {
		double horizontal_units_per_division;
		double vertical_units_per_division;

		horizontal_units_per_division = fabs(m_traceArray[trace]->rightEdge-m_traceArray[trace]->leftEdge)/m_horizDivs;
		vertical_units_per_division = fabs(m_traceArray[trace]->topEdge-m_traceArray[trace]->bottomEdge)/m_vertDivs;
		m_traceArray[trace]->infoLabel->setPaletteBackgroundColor(paletteBackgroundColor());
		m_traceArray[trace]->infoLabel->setPaletteForegroundColor(m_traceArray[trace]->color);
		m_traceArray[trace]->infoLabel->setText(TQString("<qt>%1<br>%2 %3/div<br>%4 %5/div</qt>").arg(m_traceArray[trace]->traceName).arg(horizontal_units_per_division).arg(m_traceArray[trace]->horizontalUnits).arg(vertical_units_per_division).arg(m_traceArray[trace]->verticalUnits));
	}
}

TQDoubleArray& TraceWidget::samples(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->sampleArray;
}

void TraceWidget::setSamples(uint traceNumber, TQDoubleArray& tqda) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->sampleArray = tqda;
	m_traceArray[traceNumber]->numberOfSamples = tqda.size();
}

TQDoubleArray& TraceWidget::positions(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->positionArray;
}

void TraceWidget::setPositions(uint traceNumber, TQDoubleArray& tqda) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->positionArray = tqda;
	m_traceArray[traceNumber]->numberOfSamples = tqda.size();
}

TQColor& TraceWidget::traceColor(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->color;
}

void TraceWidget::setTraceColor(uint traceNumber, TQColor& color) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->color = color;

	m_graticuleWidget->updateGraticule();
	updateTraceText();
}

bool TraceWidget::traceEnabled(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->enabled;
}

void TraceWidget::setTraceEnabled(uint traceNumber, bool enabled) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->enabled = enabled;
	if (enabled) {
		m_traceArray[traceNumber]->infoLabel->show();
	}
	else {
		m_traceArray[traceNumber]->infoLabel->hide();
	}

	m_graticuleWidget->updateGraticule();
	updateTraceText();
}

TQString TraceWidget::traceName(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->traceName;
}

void TraceWidget::setTraceName(uint traceNumber, TQString name) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->traceName = name;
	updateTraceText();
}

TQString TraceWidget::traceHorizontalUnits(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->horizontalUnits;
}

void TraceWidget::setTraceHorizontalUnits(uint traceNumber, TQString units) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->horizontalUnits = units;
	updateTraceText();
}

TQString TraceWidget::traceVerticalUnits(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->verticalUnits;
}

void TraceWidget::setTraceVerticalUnits(uint traceNumber, TQString units) {
	VERIFY_TRACE_ARRAY_SIZE

	m_traceArray[traceNumber]->verticalUnits = units;
	updateTraceText();
}

void TraceWidget::resizeTraceArray(uint newsize) {
	uint oldcount = m_traceArray.count();

	if (newsize > oldcount) {
		m_traceArray.resize(newsize);
		for (uint i=oldcount;i<newsize;i++) {
			m_traceArray[i] = new TraceData(this);
			m_traceLabelLayout->addWidget(m_traceArray[i]->infoLabel, 0, i);
		}
	}
	else {
		m_traceArray.resize(newsize);
		for (uint i=newsize;i<oldcount;i++) {
			m_traceLabelLayout->remove(m_traceArray[i]->infoLabel);
			delete m_traceArray[i];
		}
	}
}