You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
9.1 KiB

//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];
}
}
}