FSUDAQ_Qt6/CustomWidgets.h

182 lines
5.5 KiB
C
Raw Normal View History

2023-04-14 16:12:52 -04:00
#ifndef CustomWidgets_H
#define CustomWidgets_H
#include <QLabel>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QComboBox>
#include <QWheelEvent>
2023-04-18 13:12:05 -04:00
#include <QChart>
#include <QChartView>
#include <QValueAxis>
2023-04-18 13:12:05 -04:00
#include <QRubberBand>
#include <QMouseEvent>
#include <QGestureEvent>
#include <QLineSeries>
#include <QAreaSeries>
#include <QValueAxis>
2023-04-18 13:12:05 -04:00
//^====================================================
2023-04-14 16:12:52 -04:00
class RSpinBox : public QDoubleSpinBox{
Q_OBJECT
public :
RSpinBox(QWidget * parent = nullptr, int decimal = 0): QDoubleSpinBox(parent){
setFocusPolicy(Qt::StrongFocus);
setDecimals(decimal);
}
void SetToolTip(double min = -1){
if( min == -1 ){
setToolTip("(" + QString::number(minimum()) + " - " + QString::number(maximum()) + ", " + QString::number(singleStep()) + ")");
}else{
setToolTip("(" + QString::number(min) + " - " + QString::number(maximum()) + ", " + QString::number(singleStep()) + ")");
}
setToolTipDuration(-1);
}
signals:
void returnPressed();
protected:
void wheelEvent(QWheelEvent * event) override{ event->ignore(); }
void keyPressEvent(QKeyEvent * event) override{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
emit returnPressed();
} else {
QDoubleSpinBox::keyPressEvent(event);
}
}
};
//^====================================================
2023-04-14 16:12:52 -04:00
class RComboBox : public QComboBox{
public :
RComboBox(QWidget * parent = nullptr): QComboBox(parent){
setFocusPolicy(Qt::StrongFocus);
}
protected:
void wheelEvent(QWheelEvent * event) override{ event->ignore(); }
};
2023-04-18 13:12:05 -04:00
//^====================================================
2023-05-26 18:06:37 -04:00
class RChart : public QChart{
2023-04-18 13:12:05 -04:00
public:
2023-05-26 18:06:37 -04:00
explicit RChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = {})
2023-04-18 13:12:05 -04:00
: QChart(QChart::ChartTypeCartesian, parent, wFlags){
grabGesture(Qt::PanGesture);
grabGesture(Qt::PinchGesture);
}
2023-05-26 18:06:37 -04:00
~RChart(){}
2023-04-18 13:12:05 -04:00
protected:
bool sceneEvent(QEvent *event){
if (event->type() == QEvent::Gesture) return gestureEvent(static_cast<QGestureEvent *>(event));
return QChart::event(event);
}
private:
bool gestureEvent(QGestureEvent *event){
if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
QPanGesture *pan = static_cast<QPanGesture *>(gesture);
QChart::scroll(-(pan->delta().x()), pan->delta().y());
}
if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) QChart::zoom(pinch->scaleFactor());
}
return true;
}
private:
};
//^====================================================
2023-05-26 18:06:37 -04:00
class RChartView : public QChartView{
2023-04-18 13:12:05 -04:00
public:
2023-05-26 18:06:37 -04:00
RChartView(QChart * chart, QWidget * parent = nullptr): QChartView(chart, parent){
2023-04-18 13:12:05 -04:00
m_isTouching = false;
this->setRubberBand(QChartView::RectangleRubberBand);
m_coordinateLabel = new QLabel(this);
m_coordinateLabel->setStyleSheet("QLabel { color : black; }");
m_coordinateLabel->setVisible(false);
m_coordinateLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
setMouseTracking(true);
setRenderHints(QPainter::Antialiasing);
2023-05-19 16:23:04 -04:00
vRangeMin = -(0x1FFF);
vRangeMax = 0x1FFF;
2023-05-26 18:06:37 -04:00
hRangeMin = 0;
hRangeMax = 0;
2023-04-18 13:12:05 -04:00
}
void SetHRange(int min, int max) {
this->hRangeMin = min;
this->hRangeMax = max;
2023-05-19 16:23:04 -04:00
}
void SetVRange(int min, int max) {
this->vRangeMin = min;
this->vRangeMax = max;
}
2023-04-18 13:12:05 -04:00
protected:
bool viewportEvent(QEvent *event) override{
if (event->type() == QEvent::TouchBegin) {
m_isTouching = true;
chart()->setAnimationOptions(QChart::NoAnimation);
}
return QChartView::viewportEvent(event);
}
void mousePressEvent(QMouseEvent *event) override{
if (m_isTouching) return;
QChartView::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event) override{
QPointF chartPoint = this->chart()->mapToValue(event->pos());
QString coordinateText = QString("x: %1, y: %2").arg(QString::number(chartPoint.x(), 'f', 0)).arg(QString::number(chartPoint.y(), 'f', 0));
m_coordinateLabel->setText(coordinateText);
m_coordinateLabel->move(event->pos() + QPoint(10, -10));
m_coordinateLabel->setVisible(true);
if (m_isTouching) return;
QChartView::mouseMoveEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event) override{
if (m_isTouching) m_isTouching = false;
chart()->setAnimationOptions(QChart::SeriesAnimations);
QChartView::mouseReleaseEvent(event);
}
void leaveEvent(QEvent *event) override {
m_coordinateLabel->setVisible(false);
QChartView::leaveEvent(event);
}
void keyPressEvent(QKeyEvent *event) override{
switch (event->key()) {
case Qt::Key_Plus: chart()->zoomIn(); break;
case Qt::Key_Minus: chart()->zoomOut(); break;
case Qt::Key_Left: chart()->scroll(-10, 0); break;
case Qt::Key_Right: chart()->scroll(10, 0); break;
case Qt::Key_Up: chart()->scroll(0, 10); break;
case Qt::Key_Down: chart()->scroll(0, -10); break;
case Qt::Key_R :
2023-05-19 16:23:04 -04:00
//chart()->axes(Qt::Vertical).first()->setRange(-(0x1FFF), 0x1FFF);
chart()->axes(Qt::Vertical).first()->setRange(vRangeMin, vRangeMax);
2023-05-26 18:06:37 -04:00
if( hRangeMax != hRangeMin ) chart()->axes(Qt::Horizontal).first()->setRange(hRangeMin, hRangeMax);
break;
2023-04-18 13:12:05 -04:00
default: QGraphicsView::keyPressEvent(event); break;
}
}
2023-05-19 16:23:04 -04:00
2023-04-18 13:12:05 -04:00
private:
bool m_isTouching;
int hRangeMin;
int hRangeMax;
2023-05-19 16:23:04 -04:00
int vRangeMin;
int vRangeMax;
2023-04-18 13:12:05 -04:00
QLabel * m_coordinateLabel;
};
2023-04-14 16:12:52 -04:00
#endif