-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartView.h
180 lines (147 loc) · 5.6 KB
/
ChartView.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef CHARTVIEW_H
#define CHARTVIEW_H
#include <QWidget>
#include <QChartView>
#include <QLabel>
#include <QMenu>
class ChartView : public QChartView{
Q_OBJECT
public:
ChartView(QWidget* parent = nullptr) : QChartView(parent){
//setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
m_label = new QLabel(this);
QFont font;
font.setPointSize(10);
m_label->setFont(font);
m_label->setText("0 , 0");
int labelX = width()/2-100;
int labelY = 12;
m_label->setFixedWidth(250);
m_label->move(labelX, labelY);
m_label->setAlignment(Qt::AlignCenter);
m_label->setStyleSheet("background-color: transparent; color:white");
rectangle = new QGraphicsRectItem(rect_);
QColor colorRec(Qt::gray);
colorRec.setAlphaF(0.5);
rectangle->setBrush(QBrush(colorRec));
rectangle->setPen(QPen(colorRec));
chart()->scene()->addItem(rectangle);
contextMenu = new QMenu(this);
clearRectAction = new QAction("Clear Rectangle", nullptr);
setCenter = new QAction("Set Center", nullptr);
contextMenu->addAction(clearRectAction);
contextMenu->addSeparator();
contextMenu->addAction(setCenter);
contextMenu->addSeparator();
connect(clearRectAction, &QAction::triggered, this, [=](bool x){
rectangle->setVisible(false);
chart()->scene()->removeItem(rectangle);
rect_ = QRectF(QPointF(0,0), QSizeF(0,0));
rectangle = new QGraphicsRectItem(rect_);
chart()->scene()->addItem(rectangle);
rectangle->setVisible(false);
emit sigStopPayeshThuraya();
});
connect(setCenter, &QAction::triggered, this, [=](bool x){
//contextMenu.g
emit setCenterFromChartView((endValue.x()+ startValue.x())/2);
clearRectAction->trigger();
});
}
double minRangeX=0;
double maxRangeX=1;
double minRangeY=0;
double maxRangeY=1;
bool isMonitoring = false;
public slots:
void clearRectangleFromMain(){
clearRectAction->trigger();
}
protected:
void resizeEvent(QResizeEvent* event) override
{
QChartView::resizeEvent(event);
int labelX = this->width()/2-100;
int labelY = 12;
m_label->move(labelX, labelY);
if(!rectangle->isVisible())
return;
qreal width_rect = chart()->mapToPosition(endValue).x() - chart()->mapToPosition(startValue).x();
if(width_rect>=0)
rectangle->setRect(QRectF( QPointF(chart()->mapToPosition(startValue).x(), 10), QSizeF(width_rect, this->height()-20)));
else
rectangle->setRect(QRectF( QPointF(chart()->mapToPosition(endValue).x(), 10), QSizeF(-width_rect, this->height()-20)));
}
void mouseMoveEvent(QMouseEvent* event) override
{
QPoint pos = event->pos();
QPointF chartPos = this->chart()->mapToValue(pos);
updateLabelPosition(chartPos.x() , chartPos.y());
if(isPressed && chartPos.x() <= maxRangeX && chartPos.x() >= minRangeX){
endValue = this->chart()->mapToValue(pos);
QPointF releasPoint = event->pos();
qreal width_rect = releasPoint.x() - startPoint.x();
chart()->scene()->removeItem(rectangle);
rectangle = new QGraphicsRectItem(rect_);
QColor colorRec(Qt::gray);
colorRec.setAlphaF(0.5);
rectangle->setBrush(QBrush(colorRec));
rectangle->setPen(QPen(colorRec));
chart()->scene()->addItem(rectangle);
if(width_rect>=0)
rectangle->setRect(QRectF( QPointF(rectangle->rect().right(), 10), QSizeF(width_rect, this->height()-20)));
else
rectangle->setRect(QRectF( QPointF(chart()->mapToPosition(endValue).x(), 10), QSizeF(-width_rect, this->height()-20)));
}
}
void mousePressEvent(QMouseEvent* event) override{
if(event->button() == Qt::LeftButton){
clearRectAction->trigger();
startPoint = event->pos();
startValue = chart()->mapToValue(startPoint);
rect_ = QRectF(startPoint, QSizeF(0,0));
if(startValue.x() >= minRangeX && startValue.x() <= maxRangeX && isMonitoring){
rectangle->setVisible(true);
isPressed = true;
}
}
}
void mouseReleaseEvent(QMouseEvent* event)override{
isPressed = false;
if(event->button() == Qt::LeftButton){
endPoint = event->pos();
}
if(std::abs( endPoint.x() - startPoint.x()) < 3){
rectangle->setVisible(false);
clearRectAction->trigger();
}
}
void updateLabelPosition(double x, double y){
if(x>maxRangeX) x= maxRangeX;
if(x<minRangeX) x=minRangeX;
if(y>maxRangeY) y= maxRangeY;
if(y<minRangeY) y=minRangeY;
m_label->setText("Freq(MHz): "+QString::number(x,'f',3) + " , Power(dB): " + QString::number(y,'f',3));
}
void contextMenuEvent(QContextMenuEvent* event) override
{
contextMenu->exec(event->globalPos());
}
private:
bool isPressed=false;
QLabel* m_label;
QPointF startPoint;
QPointF startValue;
QPointF endPoint;
QPointF endValue;
QRectF rect_;
QGraphicsRectItem* rectangle;
QMenu* contextMenu;
QAction* clearRectAction;
QAction* setCenter;
signals:
void sigStartPayeshMonitoring(double ileft, double iright);
void sigStopPayeshThuraya();
void setCenterFromChartView(double center);
};
#endif // CHARTVIEW_H