summaryrefslogtreecommitdiffstats
path: root/clients/tde/src/widgets/tracewidget.cpp
blob: 9b771bcb19a9e5bee907fd8b5146f24736c80015 (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
//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>

TraceWidget::TraceWidget(TQWidget* parent, const char* name) : TQWidget(parent, name),
	m_samples(0),
	m_horizDivs(0),
	m_vertDivs(0),
	m_leftEdge(0),
	m_rightEdge(0),
	m_topEdge(0),
	m_bottomEdge(0),
	m_sampleArray(0),
	m_graticulePixmap(0) {
	setBackgroundMode(NoBackground);

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

TraceWidget::~TraceWidget() {
	//
}

void TraceWidget::setNumberOfSamples(unsigned int samples) {
	m_samples = samples;
	if (m_sampleArray) {
		delete [] m_sampleArray;
	}
	m_sampleArray = new double[m_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) {
	m_leftEdge = x;
	m_rightEdge = w;
	m_topEdge = y;
	m_bottomEdge = h;
}

double* TraceWidget::samples() {
	return m_sampleArray;
}

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);
	p.setPen(foregroundColor().light(150));

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

		// Draw the points
		unsigned int n;
		int x,y,x2,y2;
		for (n=0; n<m_samples-1; n++) {
			x = abs(((n*1.0)/(m_samples-1))*(m_graticulePixmap->width()));
			y = abs(((m_sampleArray[n]-m_topEdge)/(m_bottomEdge-m_topEdge))*(m_graticulePixmap->height()));
			x2 = abs(((n+1*1.0)/(m_samples-1))*(m_graticulePixmap->width()));
			y2 = abs(((m_sampleArray[n+1]-m_topEdge)/(m_bottomEdge-m_topEdge))*(m_graticulePixmap->height()));
			p.drawLine(x, y, x2, y2);
		}
	}
	else {
		p.fillRect(x(), y(), width(), height(), backgroundColor());
	}
}

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