Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new feature: buttons for "wipe all" and "autoscale" #18

Merged
merged 10 commits into from
Jun 20, 2022
1 change: 1 addition & 0 deletions QtPMbrowser/pmbrowserwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ void PMbrowserWindow::resizeEvent(QResizeEvent* event)
auto s = event->size();
ui->widget->resize(s);
ui->splitterH->setGeometry(5, 5, s.width() - 10, s.height() - 30);
QMainWindow::resizeEvent(event);
}

void PMbrowserWindow::closeEvent(QCloseEvent* event)
Expand Down
77 changes: 57 additions & 20 deletions QtPMbrowser/renderarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@
#include "DisplayTrace.h"
#include "qstring_helper.h"

constexpr auto BUTTON_HEIGHT = 23, BUTTON_WIDTH = 55;

RenderArea::RenderArea(QWidget* parent) :
QWidget(parent), ndatapoints{},
QWidget(parent),
btnWipe{"wipe", this},
btnAutoScale{"auto", this},
btnVertShrink{"v.shrink", this},
btnHrzShrink{"h.shrink", this},
ndatapoints{},
xTrace{}, yTrace{}, tracebuffer{}, background_traces_hidden{ false },
clipped{ false },
x_min{ 0.0 }, x_max{ 0.0 },
Expand All @@ -49,6 +56,17 @@ RenderArea::RenderArea(QWidget* parent) :
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
setFocusPolicy(Qt::WheelFocus);

QObject::connect(&btnWipe, &QPushButton::clicked, this, &RenderArea::wipeAll);
QObject::connect(&btnAutoScale, &QPushButton::clicked, this, &RenderArea::autoScale);
QObject::connect(&btnVertShrink, &QPushButton::clicked, this, &RenderArea::verticalShrink);
QObject::connect(&btnHrzShrink, &QPushButton::clicked, this, &RenderArea::horizontalShrink);

btnWipe.setGeometry(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
btnAutoScale.setGeometry(BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
btnVertShrink.setGeometry(2 * BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
btnHrzShrink.setGeometry(3 * BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HEIGHT);

//ui->setupUi(this);
}

Expand Down Expand Up @@ -83,7 +101,7 @@ void RenderArea::paintEvent(QPaintEvent* event)
font.setPixelSize(24);
painter.setFont(font);
//painter.drawPath(path);
const QRect rectangle = QRect(0, 0, width(), height());
const QRect rectangle = QRect(0, BUTTON_HEIGHT, width(), height() - BUTTON_HEIGHT);
if(noData()) {
painter.drawText(rectangle,Qt::AlignHCenter|Qt::AlignVCenter,"no data to display");
} else {
Expand Down Expand Up @@ -206,11 +224,17 @@ void RenderArea::doContextMenu(QContextMenuEvent* event)
QMenu menu(this);
auto actZoomOut = menu.addAction("zoom out");
auto actShrinkV = menu.addAction("vertical shrink");
auto actShrinkH = menu.addAction("horizontal shrink");
auto actAutoScale = menu.addAction("autoscale");
auto actCopy = menu.addAction("copy");
menu.addSeparator();
// auto actWipeBK = menu.addAction("wipe background traces");
auto actASOL = menu.addAction("autoscale on load");

QObject::connect(actShrinkH, &QAction::triggered, this, &RenderArea::horizontalShrink);
QObject::connect(actShrinkV, &QAction::triggered, this, &RenderArea::verticalShrink);
QObject::connect(actAutoScale, &QAction::triggered, this, &RenderArea::autoScale);

actASOL->setCheckable(true);
actASOL->setChecked(do_autoscale_on_load);
QAction* actToggleBK = menu.addAction("show background traces");
Expand All @@ -222,32 +246,18 @@ void RenderArea::doContextMenu(QContextMenuEvent* event)
double x, y;
scaleFromPixToXY(event->x(), event->y(), x, y);
zoomIn(x, y, 0.5);
event->accept();
}
else if (response == actShrinkV) {
double nymin = 1.5 * y_min - 0.5 * y_max,
nymax = 1.5 * y_max - 0.5 * y_min;
y_min = nymin;
y_max = nymax;
update();
event->accept();
}
else if (response == actAutoScale) {
autoScale();
event->accept();
}
else if (response == actASOL) {
do_autoscale_on_load = !do_autoscale_on_load;
// settings_modified = true;
}
else if (response == actToggleBK) {
background_traces_hidden = !background_traces_hidden;
update();
event->accept();
} else if (response == actCopy) {
copyToClipboard();
event->accept();
}
event->accept();
}

void RenderArea::mousePressEvent(QMouseEvent* event)
Expand Down Expand Up @@ -307,6 +317,14 @@ void RenderArea::leaveEvent(QEvent* event)
event->accept();
}

//void RenderArea::resizeEvent(QResizeEvent* event)
//{
// btnWipe.move(0, 0);
// auto h = btnAutoScale.height();
// btnAutoScale.move(0, h);
// QWidget::resizeEvent(event);
//}

void RenderArea::mouseReleaseEvent(QMouseEvent* event)
{
if (noData()) {
Expand Down Expand Up @@ -382,6 +400,7 @@ void RenderArea::wheelEvent(QWheelEvent* event)

void RenderArea::autoScale()
{
if (noData()) return;
if (isXYmode()) {
x_min = *std::min_element(xTrace.data.cbegin(), xTrace.data.cend());
x_max = *std::max_element(xTrace.data.cbegin(), xTrace.data.cend());
Expand All @@ -400,6 +419,24 @@ void RenderArea::autoScale()
update();
}

void RenderArea::verticalShrink()
{
double nymin = 1.5 * y_min - 0.5 * y_max,
nymax = 1.5 * y_max - 0.5 * y_min;
y_min = nymin;
y_max = nymax;
update();
}

void RenderArea::horizontalShrink()
{
double nxmin = 1.5 * x_min - 0.5 * x_max,
nxmax = 1.5 * x_max - 0.5 * x_min;
x_min = nxmin;
x_max = nxmax;
update();
}

void RenderArea::toggleDoAutoscale(bool checked)
{
do_autoscale_on_load = checked;
Expand Down Expand Up @@ -547,10 +584,10 @@ void RenderArea::clearTrace()

void RenderArea::setScaling(double x_0, double x_1, double y_0, double y_1)
{
double h = height() - 1, w = width() - 1;
double h = height() - 1 - BUTTON_HEIGHT, w = width() - 1;
a_x = -w*x_0/(x_1-x_0);
b_x = w/(x_1-x_0);
a_y = h*y_1/(y_1-y_0);
a_y = h*y_1/(y_1-y_0) + BUTTON_HEIGHT;
b_y = -h/(y_1-y_0);
}

Expand All @@ -562,7 +599,7 @@ QPointF RenderArea::scaleToQPF(double x, double y)
void RenderArea::scaleFromPixToXY(int px, int py, double& x, double& y)
{
x = x_min + double(px) / double(width()) * (x_max - x_min);
y = y_max - double(py) / double(height()) * (y_max - y_min);
y = y_max - double(py - BUTTON_HEIGHT) / double(height() - BUTTON_HEIGHT) * (y_max - y_min);
}

void RenderArea::shiftByPixel(QPoint shift)
Expand Down
7 changes: 7 additions & 0 deletions QtPMbrowser/renderarea.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QQueue>
#include <QPointF>
#include <QPixmap>
#include <QPushButton>
#include <istream>
#include "hkTree.h"
#include "DisplayTrace.h"
Expand Down Expand Up @@ -74,6 +75,8 @@ class RenderArea : public QWidget
public slots:
void showSettingsDialog();
void autoScale();
void verticalShrink();
void horizontalShrink();
void toggleDoAutoscale(bool checked);
void wipeAll() { clearTrace(); };
void wipeBuffer();
Expand All @@ -92,6 +95,7 @@ public slots:
void contextMenuEvent(QContextMenuEvent* event) override;
void enterEvent(QEvent* event) override;
void leaveEvent(QEvent* event) override;
//void resizeEvent(QResizeEvent* event) override;

private:
void setScaling(double x_0, double x_1, double y_0, double y_1);
Expand All @@ -101,6 +105,9 @@ public slots:
void zoomIn(double x_center, double y_center, double factor);
void drawMarquee(QPainter& painter);
void doContextMenu(QContextMenuEvent* event);

QPushButton btnWipe, btnAutoScale, btnVertShrink, btnHrzShrink;

size_t ndatapoints;
DisplayTrace xTrace, yTrace; // TODO at least yTrace should be a pointer?
QQueue<DisplayTrace*> tracebuffer;
Expand Down