-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
306 lines (265 loc) · 7.27 KB
/
Copy pathwindow.cpp
File metadata and controls
306 lines (265 loc) · 7.27 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "window.h"
///
/// \brief Window::Window
/// Constructeur par defaut de la classe Window
/// \n Crée les differents objets nécessaire
///
Window::Window() : QMainWindow()
{
Str1 = "Create Fractal";
//Type SDI
SDI_Area = new QWidget;
GridLayout = new QGridLayout;
GridLayout2 = new QGridLayout;
B_createOrAff = new QPushButton(Str1);
B_erase = new QPushButton("Erase");
B_next = new QPushButton("Next Step");
B_load = new QPushButton("Load");
scene = new QGraphicsScene;
view = new QGraphicsView(scene);
fractale=NULL;
WGF = new WindowGenFractale;
this->setWindowTitle("Fractale");
//Configuration de la vue
GridLayout->addWidget(view);
view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
view->setDragMode(QGraphicsView::ScrollHandDrag);
//Configuration Pen
Pen1.setWidth(1);
Pen1.setCosmetic(true);
//Filtre d'événement pour capturer roulette
view->scene()->installEventFilter(this);
//ToolBar
ToolBar = addToolBar("ToolBar");
ToolBar->setMovable(false);
ToolBar->addWidget(B_next);
ToolBar->addWidget(B_load);
ToolBar->addWidget(B_createOrAff);
ToolBar->addSeparator();
ToolBar->addWidget(B_erase);
B_next->setDisabled(true);//To test new Fractale in main
SDI_Area->setLayout(GridLayout);
setCentralWidget(SDI_Area);
setWindowTitle("QMainWindow");
this->resize(800,800);
connect(B_next, SIGNAL(clicked(bool)), this, SLOT(refreshView()));
connect(B_load, SIGNAL(clicked(bool)), this, SLOT(load()));
connect(B_erase, SIGNAL(clicked(bool)), this, SLOT(erase()));
connect(B_createOrAff, SIGNAL(clicked(bool)), this, SLOT(createOrAff()));
step=0;
tweak = false;
srand(time(NULL));
this->show();
this->load();
}
void Window::createOrAff()
{
Forme forme;
forme.AddPoint(QPointF(0.,0.));
int nbr=rand() % 3+1;
if (fractale != NULL)
delete fractale;
view->scene()->clear();
step=0;
fractale = new Fractale;
for (int i = 1; i < nbr; ++i)
{
float r1 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/2.));
float r2 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/2.));
forme.AddPoint(QPointF(r1,r2));
}
fractale->AddForme(forme);
for (int i = 0; i < nbr; ++i)
{
float k = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/.9));
float r1 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(2.*M_PI)));
Application A;
A.setCentre(forme.GetPoint(i));
A.setK(k);
A.setm11(qCos(r1));
A.setm12(-qSin(r1));
A.setm21(qSin(r1));
A.setm22(qCos(r1));
r1 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/2.));
float r2 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/2.));
A.setv1(r1);
A.setv2(r2);
fractale->AddApplication(A);
qDebug() <<"Rapport" <<A.getK();
}
refreshView();
B_next->setDisabled(false);
}
void Window::erase()
{
delete fractale;
fractale=NULL;
B_next->setDisabled(true);
refreshView();
}
///
/// \brief Window::load
/// Affiche une fenetre permettant de choisir une fractale par defaut
///
void Window::load()
{
QStringList items;
items << "Cantor" << "triangle de sierpinski" << "Courbe de Koch" << "Flocon de Koch" << "Hata's tree-like set" << "Lévy Curve" << "PentaKun" << "sierpinski carpet";
bool ok;
QString item = QInputDialog::getItem(this, "Séléction Fractale", "Fractale:", items, 0, false, &ok);
if (ok && !item.isEmpty())
{
if (fractale != NULL)
delete fractale;
view->scene()->clear();
tweak=false;
step=0;
fractale = new Fractale;
fractale->generateExisting(items.indexOf(item));
if(items.indexOf(item)==1 || items.indexOf(item)==7)
tweak=1;
refreshView();
B_next->setDisabled(false);// !!!!!!!!!!!
}
}
///
/// \brief Window::refreshView
/// Dessine la fractale
///
void Window::refreshView()
{
//Permet d'afficher la fractale a l'écran
if(fractale == NULL)
{
scene->clear();
return;//Si Aucune fractale n'a été créé on n'affiche rien
}
if(step!=0)
fractale->RunOnce();
if(fractale->isLikeCantor())
{
refreshViewSpecialCantor();
return;
}
else if(tweak)
{
refreshViewColor();
return;
}
scene->clear();//Efface l'écran
for(int i=0; i<fractale->getSizeEnsForme();++i)
{
QPainterPath path;
Forme tmpForme=fractale->getFromEnsForme(i);
for(int j=0; j<tmpForme.GetSize();++j)
{
if (j==0)
path.moveTo(tmpForme.GetPoint(j).x(), -tmpForme.GetPoint(j).y());//On doit mettre des moints car l'axe des y est orienté negativement
else if(j<tmpForme.GetSize()-1)
path.lineTo(tmpForme.GetPoint(j).x(), -tmpForme.GetPoint(j).y());
else
{
path.lineTo(tmpForme.GetPoint(j).x(), -tmpForme.GetPoint(j).y());
path.lineTo(tmpForme.GetPoint(0).x(), -tmpForme.GetPoint(0).y());
}
view->scene()->addPath(path, Pen1);
}
}
//Mise au point
if (step==0)
{
view->fitInView( view->scene()->sceneRect(), Qt::KeepAspectRatio );
}
++step;
}
///
/// \brief Window::refreshViewSpecialCantor
/// Dessine de la fractale de typeCantor
///
void Window::refreshViewSpecialCantor()
{
qreal h=1./128.;//decalage
for(int i=0; i<fractale->getSizeEnsForme();++i)
{
//Special pour cantor car on dessine les ligne une en dessous de l'autre
//Segment S=F.getFromE(i);
Forme tmp = fractale->getFromEnsForme(i);
qreal x=tmp.GetPoint(0).x();
qreal y=tmp.GetPoint(1).x();
scene->addLine(x,h*step,y,h*step,Pen1);
}
//Mise au point
if (step==0)
{
view->fitInView( view->scene()->sceneRect(), Qt::KeepAspectRatio );
}
++step;
}
///
/// \brief Window::refreshViewColor
/// Dessine la fractale en colorant l'intérieur de la figure
///
void Window::refreshViewColor()
{
scene->clear();
for(int i=0; i<fractale->getSizeEnsForme();++i)
{
QPainterPath path;
Forme tmpForme=fractale->getFromEnsForme(i);
for(int j=0; j<tmpForme.GetSize();++j)
{
if (j==0)
path.moveTo(tmpForme.GetPoint(j).x(), -tmpForme.GetPoint(j).y());//On doit mettre des moints car l'axe des y est orrienté negativement
else if(j<tmpForme.GetSize()-1)
path.lineTo(tmpForme.GetPoint(j).x(), -tmpForme.GetPoint(j).y());
else
{
path.lineTo(tmpForme.GetPoint(j).x(), -tmpForme.GetPoint(j).y());
path.lineTo(tmpForme.GetPoint(0).x(), -tmpForme.GetPoint(0).y());
}
view->scene()->addPath(path, Qt::NoPen, QBrush(QColor("dark")));
}
}
//Mise au point
if (step==0)
{
view->fitInView( view->scene()->sceneRect(), Qt::KeepAspectRatio );
}
++step;
}
///
/// \brief Window::eventFilter
/// Filtre d'évenement pour capturer les evenemts spécifique
/// \param obj
/// \param event
/// \return
///
bool Window::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::GraphicsSceneWheel)
{
qDebug()<< "catch whell";
Zoom(static_cast<QGraphicsSceneWheelEvent*> (event));
event->accept();//On ne propage pas l'event
return true;
}
return false;
}
///
/// \brief Window::Zoom
/// Est Appelé quand l'évenement QEvent::GraphicsSceneWheel est capturé
/// \param event
///
void Window::Zoom(QGraphicsSceneWheelEvent *event)
{
qreal scaleFactor=pow((double)2, event->delta() / 240.0);//Calcul le Facteur de zoom
//qreal factor = view->transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
view->scale(scaleFactor, scaleFactor);//Applique le zoom
}
/*QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.ps");
QPainter painter;
painter.begin(&printer);
view->render(&painter);
painter.end();
*/