Bug Summary

File:librecad/src/actions/drawing/modify/lc_action_modify_line_join.cpp
Warning:line 501, column 35
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name lc_action_modify_line_join.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/runner/work/LibreCAD/LibreCAD/librecad/src -fcoverage-compilation-dir=/home/runner/work/LibreCAD/LibreCAD/librecad/src -resource-dir /usr/lib/llvm-18/lib/clang/18 -D _REENTRANT -D DWGSUPPORT -D MUPARSER_STATIC -D QC_APPDIR="librecad" -D LC_VERSION=2.2.2_alpha1-607-g330d41cec -D LC_PRERELEASE=true; -D QT_NO_DEBUG -D QT_SVG_LIB -D QT_PRINTSUPPORT_LIB -D QT_WIDGETS_LIB -D QT_GUI_LIB -D QT_NETWORK_LIB -D QT_CORE_LIB -I . -I /usr/include -I ../../libraries/lciconengine -I ../../libraries/libdxfrw/src -I ../../libraries/jwwlib/src -I ../../libraries/shapelib/src -I cmd -I lib/actions -I lib/actions/visual_snap -I lib/actions/options -I lib/creation -I lib/debug -I lib/engine -I lib/engine/document -I lib/engine/document/blocks -I lib/engine/document/container -I lib/engine/document/dimstyles -I lib/engine/document/entities -I lib/engine/document/entities/support -I lib/engine/document/fonts -I lib/engine/document/io -I lib/engine/document/layers -I lib/engine/document/patterns -I lib/engine/document/selection -I lib/engine/document/textstyles -I lib/engine/document/ucs -I lib/engine/document/variables -I lib/engine/document/views -I lib/engine/clipboard -I lib/engine/overlays -I lib/engine/overlays/angles_base -I lib/engine/overlays/highlight -I lib/engine/overlays/preview -I lib/engine/overlays/references -I lib/engine/overlays/crosshair -I lib/engine/overlays/info_cursor -I lib/engine/overlays/overlay_box -I lib/engine/overlays/ucs_mark -I lib/engine/undo -I lib/engine/utils -I lib/engine/settings -I lib/fileio -I lib/filters -I lib/generators -I lib/generators/makercamsvg -I lib/generators/layers -I lib/generators/image -I lib/gui -I lib/gui/grid -I lib/gui/render -I lib/gui/render/headless -I lib/gui/render/widget -I lib/information -I lib/math -I lib/modification -I lib/selection -I lib/selection/metaentity -I lib/selection/metaentity/entities -I lib/printing -I lib/properties -I actions -I actions/dock_widgets -I actions/dock_widgets/block -I actions/dock_widgets/entity_info -I actions/dock_widgets/layer -I actions/dock_widgets/library -I actions/dock_widgets/ucs_list -I actions/drawing -I actions/drawing/draw -I actions/drawing/draw/arc -I actions/drawing/draw/circle -I actions/drawing/draw/curve -I actions/drawing/draw/spline -I actions/drawing/draw/dimensions -I actions/drawing/draw/ellipse -I actions/drawing/draw/hatch -I actions/drawing/draw/image -I actions/drawing/draw/line -I actions/drawing/draw/point -I actions/drawing/draw/rect -I actions/drawing/draw/polygon -I actions/drawing/draw/misc -I actions/drawing/draw/line/shapes -I actions/drawing/draw/line/misc -I actions/drawing/draw/line/shapes/rect -I actions/drawing/draw/line/shapes/polygon -I actions/drawing/draw/polyline -I actions/drawing/draw/text -I actions/drawing/edit -I actions/drawing/info -I actions/drawing/pick -I actions/drawing/modify -I actions/drawing/pen -I actions/drawing/rel_zero -I actions/drawing/selection -I actions/drawing/snap -I actions/drawing/zoom -I actions/file -I actions/options -I actions/print_preview -I ui -I ui/action_options -I ui/action_options/circle -I ui/action_options/curve -I ui/action_options/spline -I ui/action_options/dimensions -I ui/action_options/edit -I ui/action_options/image -I ui/action_options/info -I ui/action_options/insert -I ui/action_options/line -I ui/action_options/rect -I ui/action_options/polygon -I ui/action_options/misc -I ui/action_options/modify -I ui/action_options/ellipse -I ui/action_options/other -I ui/action_options/polyline -I ui/action_options/point -I ui/action_options/print_preview -I ui/action_options/selection -I ui/action_options/snap -I ui/action_options/text -I ui/actions -I ui/components -I ui/components/relative_position_assistant -I ui/components/comboboxes -I ui/components/containers -I ui/components/creators -I ui/components/layouts -I ui/components/pen -I ui/components/status_bar -I ui/components/toolbars -I ui/components/utils -I ui/dialogs -I ui/dialogs/actions -I ui/dialogs/actions/modify -I ui/dialogs/actions/quick_selection -I ui/dialogs/modify -I ui/dialogs/entity -I ui/dialogs/creators -I ui/dialogs/file -I ui/dialogs/file/export -I ui/dialogs/file/export/layers -I ui/dialogs/file/export/image -I ui/dialogs/file/export/makercam -I ui/dialogs/main -I ui/dialogs/settings -I ui/dialogs/settings/dimstyles -I ui/dialogs/settings/dimstyles/dimstyle_manager -I ui/dialogs/settings/dimstyles/dimstyle_manager/support -I ui/dialogs/settings/options_device -I ui/dialogs/settings/options_drawing -I ui/dialogs/settings/options_general -I ui/dialogs/settings/options_widget -I ui/dialogs/settings/shortcuts -I ui/dock_widgets -I ui/dock_widgets/block_widget -I ui/dock_widgets/command_line -I ui/dock_widgets/entity_info -I ui/dock_widgets/layer_widget -I ui/dock_widgets/layers_tree -I ui/dock_widgets/library_widget -I ui/dock_widgets/pen_palette -I ui/dock_widgets/pen_wizard -I ui/dock_widgets/property_sheet -I ui/dock_widgets/property_sheet/lib -I ui/dock_widgets/property_sheet/lib/properties -I ui/dock_widgets/property_sheet/lib/view -I ui/dock_widgets/property_sheet/lib/view/edit -I ui/dock_widgets/property_sheet/lib/widgets -I ui/dock_widgets/property_sheet/lib/widgets/sheet -I ui/dock_widgets/property_sheet/metaentity -I ui/dock_widgets/property_sheet/metaentity/entities -I ui/dock_widgets/property_sheet/metaentity/entities/document -I ui/dock_widgets/property_sheet/properties -I ui/dock_widgets/property_sheet/properties/action -I ui/dock_widgets/property_sheet/properties/bool -I ui/dock_widgets/property_sheet/properties/color -I ui/dock_widgets/property_sheet/properties/double -I ui/dock_widgets/property_sheet/properties/enum -I ui/dock_widgets/property_sheet/properties/int -I ui/dock_widgets/property_sheet/properties/layer -I ui/dock_widgets/property_sheet/properties/linetype -I ui/dock_widgets/property_sheet/properties/linewidth -I ui/dock_widgets/property_sheet/properties/rect -I ui/dock_widgets/property_sheet/properties/rsvector -I ui/dock_widgets/property_sheet/properties/string -I ui/dock_widgets/views_list -I ui/dock_widgets/ucs_list -I ui/dock_widgets/workspaces -I ui/dock_widgets/cad -I ui/main -I ui/main/init -I ui/main/persistence -I ui/main/release_check -I ui/main/support -I ui/main/fontviewer -I ui/main/workspaces -I ui/view -I main -I main/console_dxf2pdf -I test -I plugins -I ../res -I ../res/arrows -I ../res/controls -I ../res/dxf -I ../res/gdt -I ../res/icons -I ../res/images -I ../../../Qt/6.9.0/gcc_64/include -I ../../../Qt/6.9.0/gcc_64/include/QtSvg -I ../../../Qt/6.9.0/gcc_64/include/QtPrintSupport -I ../../../Qt/6.9.0/gcc_64/include/QtWidgets -I ../../../Qt/6.9.0/gcc_64/include/QtGui -I ../../../Qt/6.9.0/gcc_64/include/QtNetwork -I ../../../Qt/6.9.0/gcc_64/include/QtCore -I ../../generated/librecad/moc -I ../../generated/librecad/ui -I ../../../Qt/6.9.0/gcc_64/mkspecs/linux-g++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=gnu++1z -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/runner/work/LibreCAD/LibreCAD/out/2026-08-04-154929-5069-1 -x c++ actions/drawing/modify/lc_action_modify_line_join.cpp
1/*******************************************************************************
2 *
3 This file is part of the LibreCAD project, a 2D CAD program
4
5 Copyright (C) 2024 LibreCAD.org
6 Copyright (C) 2024 sand1024
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 ******************************************************************************/
22
23#include "lc_action_modify_line_join.h"
24
25#include "lc_actioninfomessagebuilder.h"
26#include "lc_line_join_options_filler.h"
27#include "lc_line_join_options_widget.h"
28#include "lc_linemath.h"
29#include "rs_document.h"
30#include "rs_line.h"
31#include "rs_pen.h"
32#include "rs_polyline.h"
33
34LC_ActionModifyLineJoin::LC_ActionModifyLineJoin(LC_ActionContext *actionContext):
35 LC_AbstractActionWithPreview("ActionModifyLineJoin", actionContext, RS2::ActionModifyLineJoin){
36}
37
38LC_ActionModifyLineJoin::~LC_ActionModifyLineJoin() = default;
39
40void LC_ActionModifyLineJoin::doSaveOptions() {
41 save("Polyline", m_createPolyline);
42 save("RemoveOriginals", m_removeOriginalLines);
43 save("AttributesSource", m_attributesSource);
44 save("Line1EdgeMode", m_line1EdgeMode);
45 save("Line2EdgeMode", m_line2EdgeMode);
46}
47
48void LC_ActionModifyLineJoin::doLoadOptions() {
49 m_createPolyline = loadBool("Polyline", false);
50 m_removeOriginalLines = loadBool("RemoveOriginals", true);
51 m_attributesSource = loadInt("AttributesSource", ATTRIBUTES_ACTIVE_PEN_LAYER);
52 m_line1EdgeMode = loadInt("Line1EdgeMode", EDGE_EXTEND_TRIM);
53 m_line2EdgeMode = loadInt("Line2EdgeMode", EDGE_EXTEND_TRIM);
54}
55
56void LC_ActionModifyLineJoin::init(const int status){
57 m_line1 = nullptr;
58 m_line2 = nullptr;
59 LC_AbstractActionWithPreview::init(status);
60}
61
62void LC_ActionModifyLineJoin::doInitWithContextEntity(RS_Entity* contextEntity, const RS_Vector& clickPos) {
63 if (isLine(contextEntity)) {
64 m_line1 = static_cast<RS_Line*>(contextEntity);
65 m_line1ClickPosition = contextEntity->getNearestPointOnEntity(clickPos, true);
66 setStatus(SetLine2);
67 }
68}
69
70/*
71 * utility method that catches line based on mouse event
72 */
73RS_Line *LC_ActionModifyLineJoin::catchLine(const LC_MouseEvent *e, const bool forPreview) const {
74 RS_Entity *en = forPreview ? catchModifiableAndDescribe(e, m_lineType) :catchModifiableEntity(e, m_lineType);
75 RS_Line *snappedLine = nullptr;
76 if (isLine(en)){
77 snappedLine = dynamic_cast<RS_Line *>(en);
78 }
79 return snappedLine;
80}
81
82void LC_ActionModifyLineJoin::drawSnapper() {
83 // disable snapper
84}
85
86void LC_ActionModifyLineJoin::doPreparePreviewEntities(const LC_MouseEvent* e, [[maybe_unused]]RS_Vector &snap, QList<RS_Entity *> &list, const int status){
87 const RS_Line *snappedLine = catchLine(e, true);
88 switch (status) {
89 case SetLine1: {
90 if (snappedLine != nullptr){ // can snap to line
91 highlightHover(snappedLine); // just highlight line 1
92 }
93 break;
94 }
95 case SetLine2: {
96 if (snappedLine == m_line1){ // don't let to snap to the same line again
97 snappedLine = nullptr;
98 }
99 highlightSelected(m_line1);
100 if (snappedLine != nullptr){
101 highlightHover(snappedLine);
102 // here we do not rely on snap point, simply get coordinates from event
103 const RS_Vector coord = e->graphPoint;
104 const LC_LineJoinData *lineJoinData = createLineJoinData(snappedLine, coord);
105 if (lineJoinData != nullptr){
106 RS_Polyline *polyline = lineJoinData->polyline;
107 if (polyline != nullptr){
108 list << polyline;
109 }
110
111 if (m_showRefEntitiesOnPreview) {
112 if (!lineJoinData->parallelLines) {
113 const RS_Vector &intersectionPoint = lineJoinData->intersectPoint;
114 createRefPoint(intersectionPoint, list);
115 }
116
117 const RS_Vector &major1 = lineJoinData->majorPointLine1;
118 if (major1.valid) {
119 createRefPoint(major1, list);
120 }
121
122 const RS_Vector &major2 = lineJoinData->majorPointLine2;
123 if (major2.valid) {
124 createRefPoint(major2, list);
125 }
126 }
127
128 if (isInfoCursorForModificationEnabled()){
129 auto builder = msgStart()
130 .string(tr("Lines Join"));
131 if (lineJoinData->parallelLines) {
132 builder.add(tr("Lines are parallel"));
133 }
134 else{
135 builder.vector(tr("Intersection:"), lineJoinData->intersectPoint);
136 }
137 builder.toInfoCursorZone2(false);
138 }
139
140 // we don't need line joint data so far
141 delete lineJoinData;
142 }
143 }
144 break;
145 }
146 case ResolveFirstLineTrim: {
147 if (snappedLine != m_line1){
148 // don't let to snap on the other line except line 1 (as we need a point on the line 1 to determine which side from intersection
149 // should survive trim
150 snappedLine = nullptr;
151 }
152 highlightSelected(m_line1);
153 if (snappedLine != nullptr){
154 // retrieve current mouse position and recalculate line join data considering that mose position denotes part of line 1 that
155 // will survive trim operation
156 const RS_Vector coord = e->graphPoint;
157 updateLine1TrimData(coord);
158
159 const RS_Polyline *polyline = m_linesJoinData->polyline;
160 if (polyline != nullptr){
161 list << polyline->clone();
162 if (m_showRefEntitiesOnPreview) {
163 if (!m_linesJoinData->parallelLines) {
164 const RS_Vector &intersectionPoint = m_linesJoinData->intersectPoint;
165 createRefPoint(intersectionPoint, list);
166 }
167 createRefPoint(m_linesJoinData->majorPointLine1, list);
168 createRefPoint(m_linesJoinData->majorPointLine2, list);
169 }
170 }
171 }
172 break;
173 }
174 default:
175 break;
176 }
177}
178
179void LC_ActionModifyLineJoin::doOnLeftMouseButtonRelease(const LC_MouseEvent* e, const int status, [[maybe_unused]]const RS_Vector &snapPoint){
180 RS_Line *snappedLine = catchLine(e, false);
181 switch (status) {
1
Control jumps to 'case ResolveFirstLineTrim:' at line 245
182 case SetLine1: {
183 if (snappedLine != nullptr){ // just store first line and proceed to selection of second line
184 m_line1 = snappedLine;
185 m_line1ClickPosition = snapPoint;
186 setStatus(SetLine2);
187 } else {
188 commandMessage(tr("No line selected"));
189 }
190 break;
191 }
192 case SetLine2: {
193 if (snappedLine == m_line1){ // don't let to snap to the same line again
194 snappedLine = nullptr;
195 }
196 if (snappedLine != nullptr){
197 m_line2 = snappedLine;
198 const RS_Vector snap = e->graphPoint;
199 LC_LineJoinData *joinData = createLineJoinData(snappedLine, snap);
200 if (joinData != nullptr){
201 // check whether parallel lines were selected
202 if (joinData->parallelLines){
203 // check whether lines are on the same ray
204 if (joinData->straightLinesConnection){
205 // ok, let's merge them
206 m_linesJoinData = joinData;
207 trigger();
208 } else { // truly parallel lines, can't merge them
209 delete joinData;
210 commandMessage(tr("Lines are parallel, can't merge"));
211 }
212 } else { // lines may merge with angle
213 if (joinData->areLinesAlreadyIntersected()){
214 // both lines are already crossed, do nothing
215 commandMessage(tr("Lines already intersects, can't merge"));
216 delete joinData;
217 } else {
218 // check whether intersection is on the first line.
219 // if it so, and mode for line is EXTEND/TRIM, we need to ask the user
220 // which part of the line 1 should be trimmed and which one would
221 // survive trim.
222 // For such case, we need addition selection
223 bool firstLineTrimShouldBeSpecified = joinData->isIntersectionOnLine1();
224 if (m_line1EdgeMode != EDGE_EXTEND_TRIM){
225 firstLineTrimShouldBeSpecified = false;
226 }
227 m_linesJoinData = joinData;
228 if (firstLineTrimShouldBeSpecified){
229 // we need to addition hint from the user for proper trim, so go to the corresponding state
230 setStatus(ResolveFirstLineTrim);
231 highlightEntity(m_line1);
232 redraw();
233 } else {
234 // we are find for joining/trimming lines, just invoke trigger
235 trigger();
236 }
237 }
238 }
239 } else {
240 commandMessage(tr("No line selected"));
241 }
242 }
243 break;
244 }
245 case ResolveFirstLineTrim: {
246 if (snappedLine == m_line1){ // we need trim hint on the first line
2
Assuming 'snappedLine' is equal to field 'm_line1'
3
Taking true branch
247 const RS_Vector snap = e->graphPoint;
248 // update trim data according to selected part of line 1
249 updateLine1TrimData(snap);
4
Calling 'LC_ActionModifyLineJoin::updateLine1TrimData'
250 // check if polyline is built and if it so - trigger action
251 const RS_Polyline *polyline = m_linesJoinData->polyline;
252 if (polyline != nullptr){
253 trigger();
254 }
255 } else {
256 highlightEntity(m_line1);
257 }
258 break;
259 }
260 default:
261 break;
262 }
263}
264
265void LC_ActionModifyLineJoin::doBack(const LC_MouseEvent* e, const int status){
266 LC_AbstractActionWithPreview::doBack(e, status);
267 switch (status) {
268 case SetLine1:
269 init(SetLine1 - 1);
270 break;
271 case SetLine2: {
272 setStatus(SetLine1);
273 break;
274 }
275 case ResolveFirstLineTrim: {
276 setStatus(SetLine2);
277 break;
278 }
279 default:
280 initPrevious(status);
281 break;
282 }
283}
284
285void LC_ActionModifyLineJoin::doAfterTrigger(){
286 LC_AbstractActionWithPreview::doAfterTrigger();
287 delete m_linesJoinData; // cleanup
288 m_linesJoinData = nullptr;
289 redraw();
290 // return to selection first line mode
291 init(SetLine1);
292}
293
294bool LC_ActionModifyLineJoin::doCheckMayTrigger(){
295 return m_linesJoinData != nullptr;
296}
297
298bool LC_ActionModifyLineJoin::isSetActivePenAndLayerOnTrigger(){
299 // the action will handle pen and layer based on ui settings
300 return false;
301}
302
303bool LC_ActionModifyLineJoin::doTriggerEntitiesPrepare(LC_DocumentModificationBatch& ctx){
304 if (m_linesJoinData->parallelLines && m_linesJoinData->straightLinesConnection){ // process straight lines
305 // simply create straight line
306 auto *line = createLine(m_linesJoinData->majorPointLine1, m_linesJoinData->majorPointLine2, ctx.entitiesToAdd);
307 // apply attributes according to settings
308 applyAttributes(line, true);
309 } else { // process lines that are not on the same ray, and are not parallel
310
311 const RS_Vector &intersectionPoint = m_linesJoinData->intersectPoint;
312
313 // during calculation of line data, we've already processed various modes for each line,
314 // therefore we rely on major points there and draw lines from them to intersection point
315 const RS_Vector &major1 = m_linesJoinData->majorPointLine1;
316 const RS_Vector &major2 = m_linesJoinData->majorPointLine2;
317
318 if (m_createPolyline && major1.valid && major2.valid){ // handle polyline mode
319 auto *poly = new RS_Polyline(m_document);
320 poly->addVertex(major1);
321 poly->addVertex(intersectionPoint);
322 poly->addVertex(major2);
323 applyAttributes(poly, true);
324 ctx += poly;
325
326 } else { // handle individual lines
327 if (major1.valid){
328 // proceed line 1 (extend or segment)
329 RS_Line* l1 = createLine(major1, intersectionPoint, ctx.entitiesToAdd);
330 applyAttributes(l1, true);
331 }
332 if (major2.valid){
333 // proceed line 2
334 RS_Line* l2 = createLine(intersectionPoint, major2, ctx.entitiesToAdd);
335 applyAttributes(l2, false);
336 }
337 }
338 }
339 // check whether original lines should be deleted
340 if (m_removeOriginalLines){
341 // proceed line 1
342 if (m_line1EdgeMode == EDGE_EXTEND_TRIM){
343 ctx -= m_line1;
344 }
345 // proceed line 2
346 if (m_line2EdgeMode == EDGE_EXTEND_TRIM){
347 ctx -= m_line2;
348 }
349 }
350 return true;
351}
352
353/**
354 * Apply pen and layer attributes to created lines
355 * @param e created entity
356 * @param forLine1 if true, we'll apply attributes to line 1, false - to line 2
357 */
358void LC_ActionModifyLineJoin::applyAttributes(RS_Entity *e, bool forLine1){
359 RS_Pen pen;
360 RS_Layer *layer = nullptr;
361 switch (m_attributesSource) {
362 case ATTRIBUTES_LINE_1: { // pick attributes from line 1
363 pen = m_line1->getPen(false);
364 e->setPen(pen);
365 layer = m_line1->getLayer(true);
366 e->setLayer(layer);
367 break;
368 }
369 case ATTRIBUTES_BOTH_LINES: {
370 // pick attributes from each original line individually
371 if (forLine1){ // proceed line 1
372 pen = m_line1->getPen(false);
373 e->setPen(pen);
374 layer = m_line1->getLayer(true);
375 e->setLayer(layer);
376 } else { // proceed line 2
377 pen = m_line2->getPen(false);
378 e->setPen(pen);
379 layer = m_line2->getLayer(true);
380 e->setLayer(layer);
381 }
382 break;
383 }
384 case ATTRIBUTES_LINE_2: {
385 // pick attributes from line 2
386 pen = m_line2->getPen(false);
387 e->setPen(pen);
388 layer = m_line2->getLayer(true);
389 e->setLayer(layer);
390 break;
391 }
392 case ATTRIBUTES_ACTIVE_PEN_LAYER: {
393 // just set for active pen and layer
394 setPenAndLayerToActive(e);
395 break;
396 }
397 default:
398 break;
399 }
400}
401
402/**
403 * Create line join data
404 * @param secondLine second line
405 * @param snapPoint snap point
406 * @return created line join data
407 */
408LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::createLineJoinData(const RS_Line *secondLine, const RS_Vector &snapPoint) const {
409 LC_LineJoinData *result = nullptr;
410
411 if (m_line1 != nullptr && secondLine != nullptr){
412
413 // prepare endpoints vectors
414 const RS_Vector line1Start = m_line1->getStartpoint();
415 const RS_Vector line1End = m_line1->getEndpoint();
416 const RS_Vector line2Start = secondLine->getStartpoint();
417 const RS_Vector line2End = secondLine->getEndpoint();
418
419 const RS_Vector intersection = LC_LineMath::getIntersectionLineLine(line1Start, line1End, line2Start, line2End);
420
421 // determine intersection point for line 1 and given line 2
422 if (intersection.valid){// has intersection between lines, proceed them
423 result = proceedNonParallelLines(m_line1ClickPosition, snapPoint, intersection, line1Start, line1End, line2Start, line2End);
424
425 } else {// has intersection between lines, proceed them
426 result = proceedParallelLinesJoin(line1Start, line1End, line2Start, line2End);
427 }
428 }
429 return result;
430}
431
432/**
433 * Determine line data for intersecting (non-parallel) lines
434 * @param line1ClickPoint
435 * @param snapPoint snap point
436 * @param intersectPoint
437 * @param line1Start
438 * @param line1End
439 * @param line2Start
440 * @param line2End
441 * @return
442 */
443LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::proceedNonParallelLines(const RS_Vector& line1ClickPoint, const RS_Vector &snapPoint,
444 const RS_Vector &intersectPoint,
445 const RS_Vector &line1Start, const RS_Vector &line1End,
446 const RS_Vector &line2Start, const RS_Vector &line2End) const {
447 auto *result = new LC_LineJoinData();
448
449 result->intersectPoint = intersectPoint;
450 result->parallelLines = false;
451
452 // resulting polyline
453 auto *polyline = new RS_Polyline(m_document);
454
455 // processing of line 1
456 // determining how intersection and snap points are located relating to line endpoints
457 const LC_PointsDisposition line1Disposition = determine3PointsDisposition(line1Start, line1End, intersectPoint, /*snapPoint*/line1ClickPoint);
458
459 // determine major point that will be used for drawing of resulting entities.
460 // Based on options, major point may be either one of line endpoints or intersection point
461
462 const RS_Vector pointFromLine1 = getMajorPointFromLine(m_line1EdgeMode, line1Start, line1End, line1Disposition);
463 result->majorPointLine1 = pointFromLine1;
464
465 // add major point for line 1 to polyline, if needed
466 if (pointFromLine1.valid){
467 polyline->addVertex(pointFromLine1);
468 }
469
470 // add intersection
471 polyline->addVertex(intersectPoint);
472
473 // processing of line 2
474 // again, determine how endpoints of line are located relating to intersection and snap points
475 const LC_PointsDisposition line2Disposition = determine3PointsDisposition(line2Start, line2End, intersectPoint, snapPoint);
476
477 // determine major point for line 2
478 const RS_Vector pointFromLine2 = getMajorPointFromLine(m_line2EdgeMode, line2Start, line2End, line2Disposition);
479 result->majorPointLine2 = pointFromLine2;
480
481 // add major point from line 2 to polyline, if needed
482 if (pointFromLine2.valid){
483 polyline->addVertex(pointFromLine2);
484 }
485
486 // finalise line point data fields
487 result->polyline = polyline;
488 result->line1Disposition = line1Disposition;
489 result->line2Disposition = line2Disposition;
490 result->intersectPoint = intersectPoint;
491 return result;
492}
493
494/**
495 * Updates previously calculated line join data for resolving trim of line 1 (if intersection of lines belongs to line2).
496 * Snap point specified by the user indicates part of line 1 that should survive trim operation.
497 * @param snap snap point
498 */
499void LC_ActionModifyLineJoin::updateLine1TrimData(const RS_Vector& snap) const {
500
501 const RS_Vector &line1Start = m_line1->getStartpoint();
5
Called C++ object pointer is null
502 const RS_Vector &line1End = m_line1->getEndpoint();
503 RS_Polyline *polyline = m_linesJoinData->polyline;
504
505 delete polyline;// we'll rebuild polyline, so delete original one
506
507 polyline = new RS_Polyline(m_document);
508 m_linesJoinData->polyline = polyline;
509
510 const RS_Vector &intersection = m_linesJoinData->intersectPoint;
511
512 // recalculate disposition of line 1 taking into consideration updated snap point
513 const LC_PointsDisposition line1Disposition = determine3PointsDisposition(line1Start, line1End, intersection, snap);
514
515 // update major point for line 1 based updated disposition
516 const RS_Vector pointFromLine1 = getMajorPointFromLine(m_line1EdgeMode, line1Start, line1End, line1Disposition);
517 m_linesJoinData->majorPointLine1 = pointFromLine1;
518
519 // add major point from line 1 to polyline
520 if (line1Start.valid){
521 polyline->addVertex(pointFromLine1);
522 }
523
524 // add intersection
525 polyline->addVertex(intersection);
526
527 // get major point for line 2
528 const RS_Vector pointFromLine2 = getMajorPointFromLine(m_line2EdgeMode, m_line2->getStartpoint(), m_line2->getEndpoint(), m_linesJoinData->line2Disposition);
529 m_linesJoinData->majorPointLine2 = pointFromLine2;
530
531 // add major point form line 2, if needed
532 if (pointFromLine2.valid){
533 polyline->addVertex(pointFromLine2);
534 }
535}
536
537/**
538 * Determining major point for the line. Major point depends on edges mode set for specific line and may represent either one of endpoints or
539 * intersection point. Major point for the line is the point which will be used for drawing a line from this point to intersection point.
540 * @param edgeMode edge mode for the line
541 * @param lineStart start point of the line
542 * @param lineEnd end point of the line
543 * @param lineDisposition disposition data
544 * @return major point of line
545 */
546RS_Vector LC_ActionModifyLineJoin::getMajorPointFromLine(
547 const int edgeMode,
548 const RS_Vector &lineStart,
549 const RS_Vector &lineEnd,
550 const LC_PointsDisposition &lineDisposition) const{
551 auto result = RS_Vector(false);
552 switch (edgeMode) {
553 case EDGE_EXTEND_TRIM:
554 switch (lineDisposition.dispositionMode) {
555 case LC_PointsDisposition::BOTH_POINTS_ON_RIGHT:
556 case LC_PointsDisposition::BOTH_POINTS_ON_LEFT: {
557 result = lineDisposition.farPoint; // we'll use endpoint of line that is more distant (not closer) from intersection
558 break;
559 }
560 case LC_PointsDisposition::MIDDLE_END_LEFT: {
561 if (lineDisposition.snapSelectionOnLeft){ // determine whether it should be start point or endpoint
562 result = lineEnd;
563 } else {
564 result = lineStart;
565 }
566 break;
567 }
568 case LC_PointsDisposition::MIDDLE_START_LEFT: {
569 if (lineDisposition.snapSelectionOnLeft){
570 result = lineStart;
571 } else {
572 result = lineEnd;
573 }
574 break;
575 }
576 default:
577 break;
578 }
579 break;
580 case EDGE_ADD_SEGMENT:
581 switch (lineDisposition.dispositionMode) {
582 case LC_PointsDisposition::BOTH_POINTS_ON_RIGHT:
583 case LC_PointsDisposition::BOTH_POINTS_ON_LEFT: {
584 // for adding segment, we'll select endpoint that is closest to the intersection
585 // as intersection point is outside of line
586 result = lineDisposition.closestPoint;
587 break;
588 }
589 case LC_PointsDisposition::MIDDLE_END_LEFT:
590 case LC_PointsDisposition::MIDDLE_START_LEFT: {
591 // we don't need major point for adding segment mode if intersection point is
592 // within line endpoints
593 break;
594 }
595 default:
596 break;
597 }
598 break;
599 case EDGE_NO_MODIFICATION:
600 break;
601 default:
602 break;
603 }
604 return result;
605}
606
607/**
608 * Resolving parallel lines - check whether both lines lies on the same ray so they may be joined
609 * @param line1Start
610 * @param line1End
611 * @param line2Start
612 * @param line2End
613 * @return line join data
614 */
615LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::proceedParallelLinesJoin(
616 const RS_Vector &line1Start, const RS_Vector &line1End,
617 const RS_Vector &line2Start, const RS_Vector &line2End) const{
618
619 auto result = new LC_LineJoinData();
620 result->parallelLines = true;
621
622 // check whether these lines are on the same ray vector
623 bool sameRay = LC_LineMath::areLinesOnSameRay(line1Start, line1End, line2Start, line2End);
624
625 if (sameRay){
626 result->straightLinesConnection = true;
627
628 // determine angle from start point to end point
629
630 double angle = line1Start.angleTo(line1End);
631
632 // copy on endpoints
633 auto startOnX1 = RS_Vector(line1Start);
634 auto endOnX1 = RS_Vector(line1End);
635 auto startOnX2 = RS_Vector(line2Start);
636 auto endOnX2 = RS_Vector(line2End);
637
638 // rotate all points around start point of line 1 do they will be on horizontal line (parallel to x-axis)
639 endOnX1.rotate(startOnX1, -angle);
640 startOnX2.rotate(startOnX1, -angle);
641 endOnX2.rotate(startOnX1, -angle);
642
643 // determine positions of each point on x
644 double sx1 = startOnX1.x;
645 double ex1 = endOnX1.x;
646 double sx2 = startOnX2.x;
647 double ex2 = endOnX2.x;
648
649 // orient all lines in one direction, from left to right
650
651 RS_Vector s1 = line1Start;
652 RS_Vector e1 = line1End;
653 RS_Vector s2 = line2Start;
654 RS_Vector e2 = line2End;
655
656 // determine who two lines are positioned relating each other.
657 // there may be either gap between lines or overlapping of lines
658
659 // position of all endpoints in the same direction,
660 // so start points are on the left side of end points
661
662 if (ex1 < sx1){
663 std::swap(sx1, ex1);
664 std::swap(s1, e1);
665 }
666
667 if (ex2 < sx2){
668 std::swap(sx2, ex2);
669 std::swap(s2, e2);
670 }
671
672 RS_Vector leftPoint; // most left point
673 RS_Vector rightPoint; // most right point
674 RS_Vector middleLeftPoint; // left point in the middle
675 RS_Vector middleRightPoint; // right point in the middle
676
677 // check we have intersection of two lines
678 bool hasIntersection = false;
679
680 if (sx1 < sx2){ // start of line 1 is on left from start of line 2
681 hasIntersection = sx2 < ex1; // check whether start of line 2 between start and end of line 1
682 leftPoint = s1;
683 if (hasIntersection){
684 if (ex1 > ex2){
685 rightPoint = e1;
686 } else {
687 rightPoint = e2;
688 }
689 } else {
690 rightPoint = e2;
691 middleLeftPoint = e1;
692 middleRightPoint = s2;
693 }
694 } else {
695 hasIntersection = sx1 < ex2; // check whether start of line 2 between start and end of line 1
696 leftPoint = s2;
697 if (hasIntersection){
698 if (ex1 > ex2){
699 rightPoint = e1;
700 } else {
701 rightPoint = e2;
702 }
703 } else {
704 rightPoint = e1;
705 middleRightPoint = s1;
706 middleLeftPoint = e2;
707 }
708 }
709
710 // if edge modes for both lines are set to Extend/Trim, we'll
711 // merge to lines into single line. Here we don't care whether lines are overlapping
712 // or not - just use external endpoints for the new line
713 if (m_line1EdgeMode == EDGE_EXTEND_TRIM || m_line2EdgeMode == EDGE_EXTEND_TRIM){
714 auto *polyline = new RS_Polyline(m_document);
715
716 // we just use most left and most right point as vertexes
717 polyline->addVertex(leftPoint);
718 polyline->addVertex(rightPoint);
719
720 // and consider them as major points. Potentially, left and right might be from different lines and so
721 // major point may be set for wrong line - yet it seems that's ok for the overall logic of joining
722 // for merging lines, it's hard to properly determine right direction of resulting line and, most probably,
723 // it's direction is not important at all.
724
725 result->majorPointLine1 = leftPoint;
726 result->majorPointLine2 = rightPoint;
727
728 result->polyline = polyline;
729 } else if (m_line1EdgeMode == EDGE_ADD_SEGMENT && m_line2EdgeMode == EDGE_ADD_SEGMENT){
730 // if edge mode is adding segment for both lines, we'll create a single line that
731 // fills a gap between lines if there is no intersection
732 if (!hasIntersection){
733 // we can do this only if lines are not overlapped
734 auto *polyline = new RS_Polyline(m_document);
735
736 // add gap points
737 polyline->addVertex(middleLeftPoint);
738 polyline->addVertex(middleRightPoint);
739
740 // use them as major points
741 result->majorPointLine1 = middleLeftPoint;
742 result->majorPointLine2 = middleRightPoint;
743 result->polyline = polyline;
744 }
745 }
746 } else {
747 result->straightLinesConnection = false;
748 }
749 return result;
750}
751
752/**
753 * Function that determines how endpoints of line are located relating to provided intersection point, and on which side from
754 * intersection point provided snap point is located.
755 * @param start line start point
756 * @param end lint end point
757 * @param intersection intersection point
758 * @param snapPoint snap point
759 * @return points disposition
760 */
761LC_ActionModifyLineJoin::LC_PointsDisposition LC_ActionModifyLineJoin::determine3PointsDisposition(
762 const RS_Vector& start,
763 const RS_Vector& end,
764 const RS_Vector& intersection,
765 const RS_Vector &snapPoint) const{
766
767 // align all 3 points horizontally for simplicity of calculations, rotate them as needed
768
769 // angle of line
770 const double angle = start.angleTo(end);
771
772 // copy of endpoints for rotation
773 auto startOnX = RS_Vector(start);
774 auto endOnX = RS_Vector(end);
775
776 startOnX.rotate(intersection, -angle);
777 endOnX.rotate(intersection, -angle);
778
779 // projection of snap point to line
780 RS_Vector snapProjection = LC_LineMath::getNearestPointOnInfiniteLine(snapPoint, start, end);
781
782 // rotated snap point projection
783 const RS_Vector snapOnX = snapProjection.rotate(intersection, -angle);
784
785 // move coordinates so that the intersection point is in 0
786
787 const double ix = intersection.x;
788 const double delta = ix;
789
790
791 double startX = startOnX.x;
792 double endX = endOnX.x;
793 double snapX = snapOnX.x;
794
795 startX -= delta;
796 endX -= delta;
797 snapX -= delta;
798
799 LC_PointsDisposition result;
800
801 // determine where all points are located relating to intersection point
802 if (startX > 0){
803 if (endX > 0){
804 result.dispositionMode = LC_PointsDisposition::BOTH_POINTS_ON_RIGHT; // intersection is on the left side of line
805 } else {
806 result.dispositionMode = LC_PointsDisposition::MIDDLE_END_LEFT; // intersection is between endpoints, end point is on the left
807 }
808 } else {
809 if (endX > 0){
810 result.dispositionMode = LC_PointsDisposition::MIDDLE_START_LEFT; // intersection is between endpoints, start point is on the left
811 } else {
812 result.dispositionMode = LC_PointsDisposition::BOTH_POINTS_ON_LEFT; // both points are on the right side from intersection
813 }
814 }
815
816 // determine which endpoint of the line is closest to the intersection point
817 if (std::abs(endX) > std::abs(startX)){
818 result.closestPoint = start;
819 result.farPoint = end;
820 } else {
821 result.closestPoint = end;
822 result.farPoint = start;
823 }
824
825 // determine position of snap point relating to intersection point, if any
826 // check whether snap point is on the left side of line and intersection point is on the right
827 result.snapSelectionOnLeft = snapX < 0;
828 result.startPoint = start;
829 result.endPoint = end;
830 return result;
831}
832
833void LC_ActionModifyLineJoin::updateActionPrompt(){
834 switch (getStatus()) {
835 case SetLine1:
836 updatePromptTRCancel(tr("Select first line"));
837 break;
838 case SetLine2:
839 updatePromptTRBack(tr("Select second line"));
840 break;
841 case ResolveFirstLineTrim:
842 updatePromptTRBack(tr("Select part of first line that should remain after trim"));
843 break;
844 default:
845 updatePrompt();
846 break;
847 }
848}
849
850LC_ActionOptionsWidget* LC_ActionModifyLineJoin::createOptionsWidget(){
851 return new LC_LineJoinOptionsWidget();
852}
853
854LC_ActionOptionsPropertiesFiller* LC_ActionModifyLineJoin::createOptionsFiller() {
855 return new LC_LineJoinOptionsFiller();
856}
857
858RS2::CursorType LC_ActionModifyLineJoin::doGetMouseCursor([[maybe_unused]]int status){
859 return RS2::SelectCursor;
860}
861
862void LC_ActionModifyLineJoin::setAttributesSource(const int value){
863 m_attributesSource = value;
864}
865
866void LC_ActionModifyLineJoin::setCreatePolyline(const bool value){
867 m_createPolyline = value;
868}
869
870void LC_ActionModifyLineJoin::setRemoveOriginalLines(const bool value){
871 m_removeOriginalLines = value;
872}
873
874void LC_ActionModifyLineJoin::setLine1EdgeMode(const int value){
875 m_line1EdgeMode = value;
876}
877
878void LC_ActionModifyLineJoin::setLine2EdgeMode(const int value){
879 m_line2EdgeMode = value;
880}