src/layers/edittools.cpp

changeset 374
75efc3ba5a56
parent 372
b2914aaeec1a
child 379
8d88adffb779
equal deleted inserted replaced
373:e34d6a30b96d 374:75efc3ba5a56
33 33
34 EditTools::~EditTools() 34 EditTools::~EditTools()
35 { 35 {
36 } 36 }
37 37
38 void EditTools::setEditMode(EditingMode newMode) 38 void EditTools::setEditMode(editing_mode_e newMode)
39 { 39 {
40 this->mode = newMode; 40 this->mode = newMode;
41 switch (this->mode) { 41 switch (this->mode) {
42 case SelectMode: 42 case editing_mode_e::select:
43 Q_EMIT this->suggestCursor(Qt::ArrowCursor); 43 Q_EMIT this->suggestCursor(Qt::ArrowCursor);
44 break; 44 break;
45 case DrawMode: 45 case editing_mode_e::draw:
46 case CircleMode: 46 case editing_mode_e::circle:
47 Q_EMIT this->suggestCursor(Qt::CrossCursor); 47 Q_EMIT this->suggestCursor(Qt::CrossCursor);
48 break; 48 break;
49 } 49 }
50 } 50 }
51 51
106 static std::vector<std::vector<glm::vec3>> polygonsToBeInserted(const ModelAction& action) 106 static std::vector<std::vector<glm::vec3>> polygonsToBeInserted(const ModelAction& action)
107 { 107 {
108 std::vector<std::vector<glm::vec3>> result; 108 std::vector<std::vector<glm::vec3>> result;
109 if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) { 109 if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) {
110 const ModelElement& newElement = append->newElement; 110 const ModelElement& newElement = append->newElement;
111 if (const LineSegment* seg = std::get_if<Colored<LineSegment>>(&newElement)) { 111 if (const Colored<LineSegment>* seg = std::get_if<Colored<LineSegment>>(&newElement)) {
112 result.push_back({seg->p1, seg->p2}); 112 result.push_back({seg->element.p1, seg->element.p2});
113 } 113 }
114 else if (const Triangle* tri = std::get_if<Colored<Triangle>>(&newElement)) { 114 else if (const Colored<Triangle>* tri = std::get_if<Colored<Triangle>>(&newElement)) {
115 result.push_back({tri->p1, tri->p2, tri->p3}); 115 result.push_back({tri->element.p1, tri->element.p2, tri->element.p3});
116 } 116 }
117 else if (const Quadrilateral* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) { 117 else if (const Colored<Quadrilateral>* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) {
118 result.push_back({quad->p1, quad->p2, quad->p3, quad->p4}); 118 result.push_back({quad->element.p1, quad->element.p2, quad->element.p3, quad->element.p4});
119 } 119 }
120 else if (const CircularPrimitive* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) { 120 else if (const Colored<CircularPrimitive>* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) {
121 // rasterize the circle down to polygons, and append them to the result. 121 // rasterize the circle down to polygons, and append them to the result.
122 rasterize(*circ, [&](const PlainPolygonElement& poly, const ColorIndex color){ 122 rasterize(circ->element, [&](const PlainPolygonElement& poly, const ColorIndex color){
123 AppendToModel append{elementFromPolygonAndColor(poly, color)}; 123 AppendToModel append{elementFromPolygonAndColor(poly, color)};
124 const auto& subpoints = polygonsToBeInserted(append); 124 const auto& subpoints = polygonsToBeInserted(append);
125 std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result)); 125 std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result));
126 }); 126 });
127 } 127 }
181 } 181 }
182 182
183 const std::vector<ModelAction> EditTools::modelActions() const 183 const std::vector<ModelAction> EditTools::modelActions() const
184 { 184 {
185 switch(this->mode) { 185 switch(this->mode) {
186 case SelectMode: 186 case editing_mode_e::select:
187 return {}; 187 return {};
188 case DrawMode: 188 case editing_mode_e::draw:
189 return drawModeActions(); 189 return this->drawModeActions();
190 case CircleMode: 190 case editing_mode_e::circle:
191 return circleModeActions(); 191 return this->circleModeActions();
192 } 192 }
193 } 193 }
194 194
195 void EditTools::renderPreview(QPainter* painter, const void* pensptr) 195 void EditTools::renderPreview(QPainter* painter, const void* pensptr)
196 { 196 {
215 painter->setBrush(pens.pointBrush); 215 painter->setBrush(pens.pointBrush);
216 painter->setPen(pens.pointPen); 216 painter->setPen(pens.pointPen);
217 for (const glm::vec3& point : this->inputPolygon) { 217 for (const glm::vec3& point : this->inputPolygon) {
218 drawWorldPoint(painter, point, this->renderer); 218 drawWorldPoint(painter, point, this->renderer);
219 } 219 }
220 if (this->mode == CircleMode and this->inputPolygon.polygonSize() >= 2) { 220 if (this->mode == editing_mode_e::circle and this->inputPolygon.polygonSize() >= 2) {
221 const glm::vec3 circleOrigin = this->inputPolygon[0]; 221 const glm::vec3 circleOrigin = this->inputPolygon[0];
222 const QPointF originScreen = this->renderer->modelToScreenCoordinates(circleOrigin); 222 const QPointF originScreen = this->renderer->modelToScreenCoordinates(circleOrigin);
223 const auto extremity = [this, &originScreen](const glm::vec3& p){ 223 const auto extremity = [this, &originScreen](const glm::vec3& p){
224 const QPointF s2 = this->renderer->modelToScreenCoordinates(p); 224 const QPointF s2 = this->renderer->modelToScreenCoordinates(p);
225 const auto intersection = rayRectangleIntersection( 225 const auto intersection = rayRectangleIntersection(
280 return {}; 280 return {};
281 } 281 }
282 } 282 }
283 } 283 }
284 284
285 EditingMode EditTools::currentEditingMode() const 285 editing_mode_e EditTools::currentEditingMode() const
286 { 286 {
287 return this->mode; 287 return this->mode;
288 } 288 }
289 289
290 void EditTools::mouseClick(const QMouseEvent* event) 290 void EditTools::mouseClick(const QMouseEvent* event)
291 { 291 {
292 switch(this->mode) { 292 switch(this->mode) {
293 case SelectMode: 293 case editing_mode_e::select:
294 if (event->button() == Qt::LeftButton) { 294 if (event->button() == Qt::LeftButton) {
295 const std::int32_t highlighted = this->renderer->pick(event->pos()); 295 const std::int32_t highlighted = this->renderer->pick(event->pos());
296 Q_EMIT this->select({highlighted}, false); 296 Q_EMIT this->select({highlighted}, false);
297 } 297 }
298 break; 298 break;
299 case DrawMode: 299 case editing_mode_e::draw:
300 if (event->button() == Qt::LeftButton and this->worldPosition.has_value()) { 300 if (event->button() == Qt::LeftButton and this->worldPosition.has_value()) {
301 if (this->inputPolygon.currentPointOnExistingPoint()) { 301 if (this->inputPolygon.currentPointOnExistingPoint()) {
302 this->closeShape(); 302 this->closeShape();
303 } 303 }
304 else { 304 else {
305 this->inputPolygon.finishCurrentPoint(); 305 this->inputPolygon.finishCurrentPoint();
306 } 306 }
307 } 307 }
308 break; 308 break;
309 case CircleMode: 309 case editing_mode_e::circle:
310 if (event->button() == Qt::LeftButton) { 310 if (event->button() == Qt::LeftButton) {
311 if (this->inputPolygon.bufferSize() == 3) { 311 if (this->inputPolygon.bufferSize() == 3) {
312 this->closeShape(); 312 this->closeShape();
313 } 313 }
314 else if (this->worldPosition.has_value()) { 314 else if (this->worldPosition.has_value()) {
368 } 368 }
369 369
370 bool EditTools::usePolygon() const 370 bool EditTools::usePolygon() const
371 { 371 {
372 switch (this->mode) { 372 switch (this->mode) {
373 case SelectMode: 373 case editing_mode_e::select:
374 return false; 374 return false;
375 case DrawMode: 375 case editing_mode_e::draw:
376 case CircleMode: 376 case editing_mode_e::circle:
377 return true; 377 return true;
378 } 378 }
379 return {}; 379 return {};
380 } 380 }
381 381

mercurial