24
24
// POSSIBILITY OF SUCH DAMAGE.
25
25
//
26
26
27
- #include < QGraphicsRectItem>
28
27
#include < QGraphicsSceneMouseEvent>
28
+ #include < QPainter>
29
29
30
30
#include " ConfigurationModel.h"
31
31
#include " ConfigurationSpaceScene.h"
@@ -43,11 +43,12 @@ ConfigurationSpaceScene::ConfigurationSpaceScene(QObject* parent) :
43
43
model(nullptr ),
44
44
range(),
45
45
steps(),
46
- collisions(nullptr ),
46
+ collisions(true ),
47
47
configuration(nullptr ),
48
+ data(),
48
49
edges(nullptr ),
50
+ image(),
49
51
path(nullptr ),
50
- scene(nullptr ),
51
52
thread(new ConfigurationSpaceThread(this ))
52
53
{
53
54
this ->axis [0 ] = 0 ;
@@ -57,13 +58,6 @@ ConfigurationSpaceScene::ConfigurationSpaceScene(QObject* parent) :
57
58
58
59
this ->thread ->scene = this ;
59
60
60
- this ->scene = this ->addRect (0 , 0 , 0 , 0 , QPen (Qt::NoPen), QBrush (Qt::white));
61
- this ->scene ->setFlag (QGraphicsItem::ItemClipsChildrenToShape, true );
62
- this ->scene ->setZValue (0 );
63
-
64
- this ->collisions = this ->createItemGroup (QList<QGraphicsItem*>());
65
- this ->collisions ->setZValue (1 );
66
-
67
61
this ->configuration = this ->addEllipse (-3 , -3 , 6 , 6 , QPen (Qt::NoPen), QBrush ((QColor (247 , 127 , 7 ))));
68
62
this ->configuration ->setFlag (QGraphicsItem::ItemIgnoresTransformations);
69
63
this ->configuration ->setVisible (false );
@@ -79,9 +73,9 @@ ConfigurationSpaceScene::ConfigurationSpaceScene(QObject* parent) :
79
73
80
74
QObject::connect (
81
75
this ->thread ,
82
- SIGNAL (addCollision (const qreal &, const qreal &, const qreal&, const qreal&, const int &)),
76
+ SIGNAL (addCollision (const int &, const int &, const unsigned char &)),
83
77
this ,
84
- SLOT (addCollision (const qreal &, const qreal &, const qreal&, const qreal&, const int &))
78
+ SLOT (addCollision (const int &, const int &, const unsigned char &))
85
79
);
86
80
87
81
QObject::connect (this ->thread , SIGNAL (finished ()), this , SIGNAL (evalFinished ()));
@@ -93,18 +87,16 @@ ConfigurationSpaceScene::~ConfigurationSpaceScene()
93
87
}
94
88
95
89
void
96
- ConfigurationSpaceScene::addCollision (const qreal & x, const qreal & y, const qreal& w, const qreal& h, const int & rgb)
97
- {
98
- QGraphicsRectItem* rect = this ->addRect (
99
- x - w * static_cast <qreal>( 0.5 ),
100
- -y - h * static_cast <qreal>( 0.5 ) ,
101
- w ,
102
- h ,
103
- QPen (Qt::NoPen) ,
104
- QBrush ( QColor (rgb, rgb, rgb))
90
+ ConfigurationSpaceScene::addCollision (const int & x, const int & y, const unsigned char & rgb)
91
+ {
92
+ this -> data [y * this ->steps [ 0 ] + x] = rgb;
93
+ this -> invalidate (
94
+ this -> minimum [ 0 ] + x * this -> delta [ 0 ] ,
95
+ this -> minimum [ 1 ] + y * this -> delta [ 1 ] ,
96
+ this -> delta [ 0 ] ,
97
+ this -> delta [ 1 ] ,
98
+ QGraphicsScene::BackgroundLayer
105
99
);
106
-
107
- this ->collisions ->addToGroup (rect);
108
100
}
109
101
110
102
void
@@ -114,12 +106,30 @@ ConfigurationSpaceScene::clear()
114
106
this ->resetCollisions ();
115
107
}
116
108
109
+ void
110
+ ConfigurationSpaceScene::drawBackground (QPainter* painter, const QRectF& rect)
111
+ {
112
+ painter->fillRect (rect, this ->palette ().color (QPalette::Window));
113
+
114
+ if (this ->collisions )
115
+ {
116
+ QRectF target = rect.intersected (this ->sceneRect ());
117
+ QRectF source (
118
+ (target.x () - this ->minimum [0 ]) / this ->delta [0 ],
119
+ (target.y () - this ->minimum [1 ]) / this ->delta [1 ],
120
+ target.width () / this ->delta [0 ],
121
+ target.height () / this ->delta [1 ]
122
+ );
123
+ painter->drawImage (target, image, source, Qt::NoFormatConversion);
124
+ }
125
+ }
126
+
117
127
void
118
128
ConfigurationSpaceScene::drawConfiguration (const rl::math::Vector& q)
119
129
{
120
130
this ->configuration ->setPos (
121
131
q (this ->axis [0 ]),
122
- - q (this ->axis [1 ])
132
+ q (this ->axis [1 ])
123
133
);
124
134
}
125
135
@@ -128,10 +138,10 @@ ConfigurationSpaceScene::drawConfigurationEdge(const rl::math::Vector& u, const
128
138
{
129
139
QGraphicsLineItem* line = this ->addLine (
130
140
u (this ->axis [0 ]),
131
- - u (this ->axis [1 ]),
141
+ u (this ->axis [1 ]),
132
142
v (this ->axis [0 ]),
133
- - v (this ->axis [1 ]),
134
- free ? QPen (QBrush (QColor (0 , 128 , 0 )), 0 ) : QPen ( QBrush ( QColor ( 128 , 0 , 0 )), 0 )
143
+ v (this ->axis [1 ]),
144
+ QPen (QBrush (QColor (96 , 96 , 96 )), 0 )
135
145
);
136
146
137
147
this ->edges ->addToGroup (line);
@@ -153,11 +163,11 @@ ConfigurationSpaceScene::drawConfigurationPath(const rl::plan::VectorList& path)
153
163
}
154
164
155
165
QPainterPath painterPath;
156
- painterPath.moveTo (path.front ()(this ->axis [0 ]), - path.front ()(this ->axis [1 ]));
166
+ painterPath.moveTo (path.front ()(this ->axis [0 ]), path.front ()(this ->axis [1 ]));
157
167
158
168
for (rl::plan::VectorList::const_iterator i = ++path.begin (); i != path.end (); ++i)
159
169
{
160
- painterPath.lineTo ((*i)(this ->axis [0 ]), - (*i)(this ->axis [1 ]));
170
+ painterPath.lineTo ((*i)(this ->axis [0 ]), (*i)(this ->axis [1 ]));
161
171
}
162
172
163
173
this ->path ->setPath (painterPath);
@@ -252,17 +262,25 @@ ConfigurationSpaceScene::init()
252
262
253
263
this ->configuration ->setPos (
254
264
(*MainWindow::instance ()->q )(this ->axis [0 ]),
255
- - (*MainWindow::instance ()->q )(this ->axis [1 ])
265
+ (*MainWindow::instance ()->q )(this ->axis [1 ])
256
266
);
257
267
258
- this ->scene ->setRect (
259
- this ->minimum [0 ],
260
- -this ->maximum [1 ],
261
- this ->range [0 ],
262
- this ->range [1 ]
263
- );
268
+ this ->data .assign (this ->steps [0 ] * this ->steps [1 ], 128 );
269
+
270
+ #if QT_VERSION >= 0x050500
271
+ this ->image = QImage (this ->data .data (), this ->steps [0 ], this ->steps [1 ], this ->steps [0 ], QImage::Format_Grayscale8);
272
+ #else
273
+ this ->image = QImage (this ->data .data (), this ->steps [0 ], this ->steps [1 ], this ->steps [0 ], QImage::Format_Indexed8);
274
+ QVector<QRgb> colors;
275
+ colors.reserve (256 );
276
+ for (int i = 0 ; i < 256 ; ++i)
277
+ {
278
+ colors.push_back (qRgb (i, i, i));
279
+ }
280
+ this ->image .setColorTable (colors);
281
+ #endif
264
282
265
- this ->setSceneRect (this ->scene -> boundingRect () );
283
+ this ->setSceneRect (this ->minimum [ 0 ], this -> minimum [ 1 ], this -> range [ 0 ], this -> range [ 1 ] );
266
284
}
267
285
268
286
void
@@ -288,17 +306,11 @@ ConfigurationSpaceScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
288
306
{
289
307
if (!MainWindow::instance ()->thread ->isRunning ())
290
308
{
291
- if (mouseEvent->scenePos ().x () > this ->minimum [0 ] &&
292
- mouseEvent->scenePos ().x () < this ->maximum [0 ])
293
- {
294
- (*MainWindow::instance ()->q )(this ->axis [0 ]) = mouseEvent->scenePos ().x ();
295
- }
309
+ qreal x = qBound (this ->minimum [0 ], mouseEvent->scenePos ().x (), this ->maximum [0 ]);
310
+ qreal y = qBound (this ->minimum [1 ], mouseEvent->scenePos ().y (), this ->maximum [1 ]);
296
311
297
- if (-mouseEvent->scenePos ().y () > this ->minimum [1 ] &&
298
- -mouseEvent->scenePos ().y () < this ->maximum [1 ])
299
- {
300
- (*MainWindow::instance ()->q )(this ->axis [1 ]) = -mouseEvent->scenePos ().y ();
301
- }
312
+ (*MainWindow::instance ()->q )(this ->axis [0 ]) = x;
313
+ (*MainWindow::instance ()->q )(this ->axis [1 ]) = y;
302
314
303
315
MainWindow::instance ()->configurationModel ->invalidate ();
304
316
this ->drawConfiguration (*MainWindow::instance ()->q );
@@ -319,7 +331,8 @@ ConfigurationSpaceScene::reset()
319
331
void
320
332
ConfigurationSpaceScene::resetCollisions ()
321
333
{
322
- qDeleteAll (this ->collisions ->childItems ());
334
+ std::fill (this ->data .begin (), this ->data .end (), 128 );
335
+ this ->invalidate (this ->sceneRect (), QGraphicsScene::BackgroundLayer);
323
336
}
324
337
325
338
void
@@ -362,7 +375,8 @@ ConfigurationSpaceScene::showMessage(const std::string& message)
362
375
void
363
376
ConfigurationSpaceScene::toggleCollisions (const bool & doOn)
364
377
{
365
- this ->collisions ->setVisible (doOn);
378
+ this ->collisions = doOn;
379
+ this ->invalidate (this ->sceneRect (), QGraphicsScene::BackgroundLayer);
366
380
}
367
381
368
382
void
0 commit comments