-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
140 lines (96 loc) · 3.11 KB
/
mainwindow.cpp
File metadata and controls
140 lines (96 loc) · 3.11 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "calculator.h"
#include <string>
#include <QDebug>
#include <QPainter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
installEventFilter(this);
// int w = ui->centralWidget->width();
//int h = ui->centralWidget->height();
//ui->graphicsView->setScaledContents(1);
ui->graphicsView->setStyleSheet("QLabel { background-color : white; color : blue; }");
ui->graphicsView->setAlignment(Qt::AlignCenter);
graph = new QPixmap(600, 500);
drawOS();
QPushButton *calcBut = ui->calcButton;
ui->graphicsView->setMinimumSize(1, 1);
connect(calcBut, SIGNAL(clicked()), this, SLOT(calcButton()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent *)
{
m_bUserIsResizing = true;
}
bool MainWindow::eventFilter(QObject* pObj, QEvent* pEvent)
{
printf("%d", 1);
// We need to check for both types of mouse release, because it can vary on which type happens when resizing.
if ((pEvent->type() == QEvent::MouseButtonPress) || (pEvent->type() == QEvent::NonClientAreaMouseButtonRelease)) {
//QMouseEvent* pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
printf("%d", 2);
if (m_bUserIsResizing) {
calcButton();
m_bUserIsResizing = false; // reset user resizing flag
}
}
return QObject::eventFilter(pObj, pEvent); // pass it on without eating it
}
void MainWindow::calcButton()
{
QLineEdit *text = ui->lineEdit;
int w = ui->graphicsView->width();
int h = ui->graphicsView->height();
graph = new QPixmap(w, h);
drawOS();
QPainter paint;
paint.begin(graph);
QPainterPath myPath;
Calculator calc;
QPolygon pol;
for(double i = -graph->width(); i < graph->width(); i+=0.1){
double y;
try{
y = calc.eval(text->text(), i);
pol.append(QPoint(i+graph->width()/2, -(10*y-graph->height()/2)));
} catch(int e){
//printf("%.f\n", i);
/*paint.setPen(QPen(Qt::black,3));
paint.drawPoint(i+graph->width()/2, -(y-graph->height()/2));
paint.setPen(QPen(Qt::black,1));*/
}
}
myPath.addPolygon(pol);
paint.drawPath(myPath);
paint.end();
ui->graphicsView->setPixmap(graph->scaled(w,h,Qt::KeepAspectRatio));
}
void MainWindow::drawOS(){
QPainter paint;
int w = graph->width();
int h = graph->height();
paint.begin(graph);
paint.eraseRect(0, 0, w, h);
paint.drawLine(0, h/2, w, h/2);
paint.drawLine(w/2, 0, w/2, h);
paint.setPen(QPen(Qt::black,3));
double step = 10.0;
for(double i = w/2; i <= w; i+=step)
paint.drawPoint(i, h/2);
for(double i = w/2; i >= 0; i-=step)
paint.drawPoint(i, h/2);
for(double i = h/2; i <= h; i+=step)
paint.drawPoint(w/2, i);
for(double i = h/2; i >= 0; i-=step)
paint.drawPoint(w/2, i);
paint.end();
ui->graphicsView->setPixmap(graph->scaled(w,h,Qt::KeepAspectRatio));
return;
}