FSUDAQ_Qt6/CustomWidgets.h

183 lines
5.6 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-06-01 17:51:00 -04:00
#include <QMenu>
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
}
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);
2023-06-01 17:51:00 -04:00
if (event->button() == Qt::RightButton) {
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
QAction * a1 = menu->addAction("UnZoom");
QAction *selectedAction = menu->exec(event->globalPosition().toPoint());
2024-01-29 17:23:11 -05:00
if( selectedAction == a1 ) {
chart()->zoomReset();
chart()->axes(Qt::Vertical).first()->setRange(-(0x3FFF), 0x3FFF);
}
2023-06-01 17:51:00 -04:00
}
}
void wheelEvent(QWheelEvent * event) override{
qreal zoomFactor = event->angleDelta().y() > 0 ? 0.9 : 1.1;
chart()->zoom(zoomFactor);
2023-04-18 13:12:05 -04:00
}
2023-06-01 17:51:00 -04:00
2023-04-18 13:12:05 -04:00
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-06-01 17:51:00 -04:00
chart()->zoomReset();
2024-01-29 17:23:11 -05:00
chart()->axes(Qt::Vertical).first()->setRange(-(0x3FFF), 0x3FFF);
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;
QLabel * m_coordinateLabel;
};
2023-04-14 16:12:52 -04:00
#endif