Skip to content

Commit

Permalink
drag markers by mouse press in the scope area
Browse files Browse the repository at this point in the history
  • Loading branch information
dendvz authored and David Gräff committed Jan 8, 2018
1 parent 4f3d56b commit 65f1202
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
18 changes: 13 additions & 5 deletions openhantek/src/dsowidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ DsoWidget::DsoWidget(DsoSettingsScope *scope, DsoSettingsView *view, const Dso::
markerSlider->setIndexVisible(marker, true);
}

connect(mainScope, &GlScope::markerMoved, [this] (int marker, double position) {
double step = this->markerSlider->step(marker);
this->scope->horizontal.marker[marker] =
this->markerSlider->setValue(marker, std::round(position / step) * step);
});

// The table for the settings
settingsTriggerLabel = new QLabel();
settingsTriggerLabel->setMinimumWidth(160);
Expand Down Expand Up @@ -265,12 +271,17 @@ void DsoWidget::updateMarkerDetails() {
double divs = fabs(scope->horizontal.marker[1] - scope->horizontal.marker[0]);
double time = divs * scope->horizontal.timebase;

QString infoLabelPrefix(tr("Markers"));
if (view->zoom) {
markerInfoLabel->setText(tr("Zoom x%L1").arg(DIVS_TIME / divs, -1, 'g', 3));
infoLabelPrefix = tr("Zoom x%L1").arg(DIVS_TIME / divs, -1, 'g', 3);
markerTimebaseLabel->setText(valueToString(time / DIVS_TIME, UNIT_SECONDS, 3) + tr("/div"));
markerFrequencybaseLabel->setText(
valueToString(divs * scope->horizontal.frequencybase / DIVS_TIME, UNIT_HERTZ, 4) + tr("/div"));
}
markerInfoLabel->setText(infoLabelPrefix.append(": %1 %2")
.arg(valueToString(0.5 + scope->horizontal.marker[0] / DIVS_TIME - scope->trigger.position, UNIT_SECONDS, 4))
.arg(valueToString(0.5 + scope->horizontal.marker[1] / DIVS_TIME - scope->trigger.position, UNIT_SECONDS, 4)));

markerTimeLabel->setText(valueToString(time, UNIT_SECONDS, 4));
markerFrequencyLabel->setText(valueToString(1.0 / time, UNIT_HERTZ, 4));
}
Expand Down Expand Up @@ -426,10 +437,7 @@ void DsoWidget::updateZoom(bool enabled) {
markerTimebaseLabel->setVisible(enabled);
markerLayout->setStretch(4, enabled ? 1 : 0);
markerFrequencybaseLabel->setVisible(enabled);
if (enabled)
updateMarkerDetails();
else
markerInfoLabel->setText(tr("Marker 1/2"));
updateMarkerDetails();

repaint();
}
Expand Down
6 changes: 3 additions & 3 deletions openhantek/src/exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ std::unique_ptr<QPrinter> Exporter::printPaintDevice(DsoSettings *settings) {
}

bool Exporter::exportSamples(const DataAnalyzerResult *result) {
if (this->format == EXPORT_FORMAT_CSV) { return exportCVS(result); }
if (this->format == EXPORT_FORMAT_CSV) { return exportCSV(result); }

// Choose the color values we need
DsoSettingsColorValues *colorValues;
Expand Down Expand Up @@ -136,7 +136,7 @@ bool Exporter::exportSamples(const DataAnalyzerResult *result) {
// Draw the measurement table
stretchBase = (double)(paintDevice->width() - lineHeight * 6) / 10;
int channelCount = 0;
for (ChannelID channel = settings->scope.voltage.size() - 1; channel >= 0; channel--) {
for (int channel = settings->scope.voltage.size() - 1; channel >= 0; channel--) {
if ((settings->scope.voltage[channel].used || settings->scope.spectrum[channel].used) &&
result->data(channel)) {
++channelCount;
Expand Down Expand Up @@ -329,7 +329,7 @@ bool Exporter::exportSamples(const DataAnalyzerResult *result) {
return true;
}

bool Exporter::exportCVS(const DataAnalyzerResult *result) {
bool Exporter::exportCSV(const DataAnalyzerResult *result) {
QFile csvFile(this->filename);
if (!csvFile.open(QIODevice::WriteOnly | QIODevice::Text)) return false;

Expand Down
2 changes: 1 addition & 1 deletion openhantek/src/exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Exporter {
private:
Exporter(DsoSettings *settings, const QString &filename, ExportFormat format);
void setFormat(ExportFormat format);
bool exportCVS(const DataAnalyzerResult *result);
bool exportCSV(const DataAnalyzerResult *result);
static std::unique_ptr<QPrinter> printPaintDevice(DsoSettings *settings);
void drawGrids(QPainter &painter, DsoSettingsColorValues *colorValues, double lineHeight, double scopeHeight,
int scopeWidth);
Expand Down
51 changes: 51 additions & 0 deletions openhantek/src/glscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>

#include <QColor>
#include <QMouseEvent>

#include "glscope.h"

Expand Down Expand Up @@ -70,6 +71,7 @@ void GlScope::paintGL() {
vaMarker[marker][0] = (GLfloat)scope->horizontal.marker[marker];
vaMarker[marker][2] = (GLfloat)scope->horizontal.marker[marker];

glLineWidth((marker == selectedMarker) ? 3 : 1);
glVertexPointer(2, GL_FLOAT, 0, &vaMarker[marker].front());
glDrawArrays(GL_LINES, 0, (GLsizei)vaMarker[marker].size() / 2);
}
Expand Down Expand Up @@ -97,6 +99,55 @@ void GlScope::resizeGL(int width, int height) {
glMatrixMode(GL_MODELVIEW);
}

void GlScope::mousePressEvent(QMouseEvent *event) {
if (!zoomed && event->button() == Qt::LeftButton) {
double position = (double)(event->x() - width() / 2) * DIVS_TIME / (double)width();
double distance = DIVS_TIME;
selectedMarker = NO_MARKER;
// Capture nearest marker located within snap area (+/- 1% of full scale).
for (int marker = 0; marker < MARKER_COUNT; ++marker) {
if (!scope->horizontal.marker_visible[marker]) continue;
if (fabs(scope->horizontal.marker[marker] - position) < std::min(distance, DIVS_TIME / 100.0)) {
distance = fabs(scope->horizontal.marker[marker] - position);
selectedMarker = marker;
}
}
if (selectedMarker != NO_MARKER) {
emit markerMoved(selectedMarker, position);
}
}
event->accept();
}

void GlScope::mouseMoveEvent(QMouseEvent *event) {
if (!zoomed && (event->buttons() & Qt::LeftButton) != 0) {
double position = (double)(event->x() - width() / 2) * DIVS_TIME / (double)width();
if (selectedMarker == NO_MARKER) {
// User started draging outside the snap area of any marker:
// move all markers to current position and select last marker in the array.
for (int marker = 0; marker < MARKER_COUNT; ++marker) {
emit markerMoved(marker, position);
selectedMarker = marker;
}
}
else if (selectedMarker < MARKER_COUNT) {
emit markerMoved(selectedMarker, position);
}
}
event->accept();
}

void GlScope::mouseReleaseEvent(QMouseEvent *event) {
if (!zoomed && event->button() == Qt::LeftButton) {
double position = (double)(event->x() - width() / 2) * DIVS_TIME / (double)width();
if (selectedMarker != NO_MARKER && selectedMarker < MARKER_COUNT) {
emit markerMoved(selectedMarker, position);
}
selectedMarker = NO_MARKER;
}
event->accept();
}

void GlScope::drawGrid() {
glDisable(GL_POINT_SMOOTH);
glDisable(GL_LINE_SMOOTH);
Expand Down
9 changes: 9 additions & 0 deletions openhantek/src/glscope.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class GlScope : public GL_WIDGET_CLASS {
/// \param height The new height of the widget.
void resizeGL(int width, int height) override;

void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;

/// \brief Draw the grid.
void drawGrid();

Expand All @@ -59,12 +63,17 @@ class GlScope : public GL_WIDGET_CLASS {
*/
bool channelUsed(Dso::ChannelMode mode, ChannelID channel);

signals:
void markerMoved(int marker, double position);

private:
const int NO_MARKER = -1;
DsoSettingsScope *scope;
DsoSettingsView *view;
const GlGenerator *generator;
std::vector<int> fadingFactor;

std::vector<GLfloat> vaMarker[2];
bool zoomed = false;
int selectedMarker = NO_MARKER;
};

0 comments on commit 65f1202

Please sign in to comment.