-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
188 lines (151 loc) · 4.46 KB
/
mainwindow.cpp
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
181
182
183
184
185
186
187
188
#include "mainwindow.h"
#include <QMenuBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
area(nullptr),
formula(nullptr),
bnew_func(nullptr),
main_label(nullptr),
main_layout(nullptr),
top_layout(nullptr),
bottom_layout(nullptr),
func_layout(nullptr),
func_list(nullptr),
func_box(nullptr)
{
main_label = new QWidget(this);
main_layout = new QVBoxLayout;
top_layout = new QHBoxLayout;
//Top layout
//Right Side
area = new RenderArea(this);
//Left side
func_layout = new QVBoxLayout;
func_list = new QScrollArea(this);
func_list->setMinimumSize(QSize(250, height()));
func_list->setMaximumWidth(250);
func_list->setWidgetResizable(true);
func_box = new QGroupBox(QString("Fonctions :"), this);
func_box->setLayout(func_layout);
secondary_layout = new QVBoxLayout;
secondary_layout->setAlignment(Qt::AlignTop);
func_layout->addLayout(secondary_layout);
func_list->setWidget(func_box);
top_layout->addWidget(func_list);
top_layout->addWidget(area);
//Bottom layout
bottom_layout = new QHBoxLayout;
QLabel* test= new QLabel("Formule :", this);
formula = new QLineEdit;
bnew_func = new QPushButton("Nouvelle fonction");
bottom_layout->addWidget(test);
bottom_layout->addWidget(formula);
bottom_layout->addWidget(bnew_func);
//Main Layout
main_layout->addLayout(top_layout);
main_layout->addLayout(bottom_layout);
//Main label
main_label->setLayout(main_layout);
setCentralWidget(main_label);
//Menu Bar
file_menu = new QMenu("Fichier",this);
open_file = new QAction("Ouvrir", this);
open_file->setShortcut(QKeySequence::Open);
save_file = new QAction("Sauvegarder", this);
//Actions
file_menu->addAction(open_file);
file_menu->addAction(save_file);
menuBar()->addMenu(file_menu);
//Connections
QObject::connect(bnew_func,SIGNAL(clicked(bool)), this, SLOT(new_func()));
QObject::connect(open_file, SIGNAL(triggered(bool)), this, SLOT(load_file()));
QObject::connect(save_file, SIGNAL(triggered(bool)), this, SLOT(fsave_file()));
}
MainWindow::~MainWindow()
{
delete area;
delete main_label;
delete main_layout;
delete func_layout;
delete file_menu;
delete quit;
delete open_file;
}
void MainWindow::new_func()
{
create_function(formula->text());
}
void MainWindow::load_file()
{
QString path = QFileDialog::getOpenFileName(this,"Ouvrir un fichier fonction");
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return; /* need improvement */
QTextStream stream(&file);
while (!stream.atEnd()) {
QString line = stream.readLine();
create_function(line);
}
}
void MainWindow::fsave_file()
{
QString path = QFileDialog::getOpenFileName(this,"Choisir un fichier fonction");
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return; /* need improvement */
QTextStream stream(&file);
for (auto i : storage )
stream << i->getFormula() << '\n';
stream.flush();
}
void MainWindow::updateFunction()
{
for (auto i : storage)
i->update();
}
void MainWindow::create_function(const QString &input)
{
try {
AbstractFunction* f = AbstractFunction::loadFunction(input,this);
storage.append(f);
area->add_function(f);
secondary_layout->addWidget(f);
} catch (...) {
std::cerr << "Une erreur est survenue" << std::endl;
/* need improvement */
}
updateFunctions();
}
void MainWindow::keyPressEvent(QKeyEvent * event)
{
if ( event->key() == Qt::Key_Up )
area->move_up();
else if ( event->key() == Qt::Key_Right)
area->move_right();
else if (event->key() == Qt::Key_Down)
area->move_down();
else if (event->key() == Qt::Key_Left)
area->move_left();
}
void MainWindow::updateSelected(AbstractFunction *func)
{
for (auto i : storage)
i->new_select(func);
updateFunction();
}
void MainWindow::delete_func(AbstractFunction *func)
{
for (unsigned i = 0; i < (unsigned) storage.length(); i++ )
if (storage[i] == func ) {
storage.remove(i);
delete func;
}
}
void MainWindow::updateFunctions()
{
area->update();
}