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

#include "tracewidget.h"

#include <stdlib.h>

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

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

TraceData::TraceData() {
	color = TQColor(0, 255, 0);
	numberOfSamples = 0;
	leftEdge = 0;
	rightEdge = 0;
	topEdge = 0;
	bottomEdge = 0;
	enabled = false;
}

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(((n*1.0)/(numberOfSamples-1))*(graticule_width));
			y = abs(((sampleArray[n]-topEdge)/(bottomEdge-topEdge))*(graticule_height));
			x2 = abs(((n+1*1.0)/(numberOfSamples-1))*(graticule_width));
			y2 = abs(((sampleArray[n+1]-topEdge)/(bottomEdge-topEdge))*(graticule_height));
			p->drawLine(x, y, x2, y2);
		}
	}
}

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

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

TraceWidget::~TraceWidget() {
	resizeTraceArray(0);
}

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

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

	updateGraticule();
}

void TraceWidget::setNumberOfHorizontalDivisions(unsigned int divisions) {
	m_horizDivs = divisions;
	updateGraticule();
}

void TraceWidget::setNumberOfVerticalDivisions(unsigned int divisions) {
	m_vertDivs = divisions;
	updateGraticule();
}

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

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

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

	return m_traceArray[traceNumber]->sampleArray;
}

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

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

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

	return m_traceArray[traceNumber]->color;
}

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

	m_traceArray[traceNumber]->color = color;
}

bool TraceWidget::traceEnabled(uint traceNumber) {
	VERIFY_TRACE_ARRAY_SIZE

	return m_traceArray[traceNumber]->enabled;
}

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

	m_traceArray[traceNumber]->enabled = enabled;
}

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;
		}
	}
	else {
		m_traceArray.resize(newsize);
		for (uint i=newsize;i<oldcount;i++) {
			delete m_traceArray[i];
		}
	}
}

void TraceWidget::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_vertDivs > 0) {
		s = m_graticulePixmap->width() / m_vertDivs;
		x = 0;
		for (d=0; d<m_vertDivs; d++) {
			p.drawLine(x, 0, x, m_graticulePixmap->height());
			x += s;
		}
	}
	if (m_horizDivs > 0) {
		s = m_graticulePixmap->height() / m_horizDivs;
		y = 0;
		for (d=0; d<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 TraceWidget::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_traceArray.count();trace++) {
			m_traceArray[trace]->drawTrace(&p, m_graticulePixmap->width(), m_graticulePixmap->height());
		}
	}
	else {
		p.fillRect(x(), y(), width(), height(), backgroundColor());
	}
}

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