Bug Summary

File:librecad/src/actions/drawing/modify/lc_action_modify_line_join.cpp
Warning:line 503, 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-670-g0ffb38d79 -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/libdxfrw/src/intern -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-09-15-234850-5000-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_information.h"
31#include "rs_line.h"
32#include "rs_pen.h"
33#include "rs_polyline.h"
34
35LC_ActionModifyLineJoin::LC_ActionModifyLineJoin(LC_ActionContext *actionContext):
36 LC_AbstractActionWithPreview("ActionModifyLineJoin", actionContext, RS2::ActionModifyLineJoin){
37}
38
39LC_ActionModifyLineJoin::~LC_ActionModifyLineJoin() = default;
40
41void LC_ActionModifyLineJoin::doSaveOptions() {
42 save("Polyline", m_createPolyline);
43 save("RemoveOriginals", m_removeOriginalLines);
44 save("AttributesSource", m_attributesSource);
45 save("Line1EdgeMode", m_line1EdgeMode);
46 save("Line2EdgeMode", m_line2EdgeMode);
47}
48
49void LC_ActionModifyLineJoin::doLoadOptions() {
50 m_createPolyline = loadBool("Polyline", false);
51 m_removeOriginalLines = loadBool("RemoveOriginals", true);
52 m_attributesSource = loadInt("AttributesSource", ATTRIBUTES_ACTIVE_PEN_LAYER);
53 m_line1EdgeMode = loadInt("Line1EdgeMode", EDGE_EXTEND_TRIM);
54 m_line2EdgeMode = loadInt("Line2EdgeMode", EDGE_EXTEND_TRIM);
55}
56
57void LC_ActionModifyLineJoin::init(const int status){
58 m_line1 = nullptr;
59 m_line2 = nullptr;
60 LC_AbstractActionWithPreview::init(status);
61}
62
63void LC_ActionModifyLineJoin::doInitWithContextEntity(RS_Entity* contextEntity, const RS_Vector& clickPos) {
64 if (isLine(contextEntity)) {
65 m_line1 = static_cast<RS_Line*>(contextEntity);
66 m_line1ClickPosition = contextEntity->getNearestPointOnEntity(clickPos, true);
67 setStatus(SetLine2);
68 }
69}
70
71/*
72 * utility method that catches line based on mouse event
73 */
74RS_Line *LC_ActionModifyLineJoin::catchLine(const LC_MouseEvent *e, const bool forPreview) const {
75 RS_Entity *en = forPreview ? catchModifiableAndDescribe(e, m_lineType) :catchModifiableEntity(e, m_lineType);
76 RS_Line *snappedLine = nullptr;
77 // the joined lines may be extended, trimmed or removed
78 if (isLine(en) && RS_Information::isEditable(en)){
79 snappedLine = dynamic_cast<RS_Line *>(en);
80 }
81 return snappedLine;
82}
83
84void LC_ActionModifyLineJoin::drawSnapper() {
85 // disable snapper
86}
87
88void LC_ActionModifyLineJoin::doPreparePreviewEntities(const LC_MouseEvent* e, [[maybe_unused]]RS_Vector &snap, QList<RS_Entity *> &list, const int status){
89 const RS_Line *snappedLine = catchLine(e, true);
90 switch (status) {
91 case SetLine1: {
92 if (snappedLine != nullptr){ // can snap to line
93 highlightHover(snappedLine); // just highlight line 1
94 }
95 break;
96 }
97 case SetLine2: {
98 if (snappedLine == m_line1){ // don't let to snap to the same line again
99 snappedLine = nullptr;
100 }
101 highlightSelected(m_line1);
102 if (snappedLine != nullptr){
103 highlightHover(snappedLine);
104 // here we do not rely on snap point, simply get coordinates from event
105 const RS_Vector coord = e->graphPoint;
106 const LC_LineJoinData *lineJoinData = createLineJoinData(snappedLine, coord);
107 if (lineJoinData != nullptr){
108 RS_Polyline *polyline = lineJoinData->polyline;
109 if (polyline != nullptr){
110 list << polyline;
111 }
112
113 if (m_showRefEntitiesOnPreview) {
114 if (!lineJoinData->parallelLines) {
115 const RS_Vector &intersectionPoint = lineJoinData->intersectPoint;
116 createRefPoint(intersectionPoint, list);
117 }
118
119 const RS_Vector &major1 = lineJoinData->majorPointLine1;
120 if (major1.valid) {
121 createRefPoint(major1, list);
122 }
123
124 const RS_Vector &major2 = lineJoinData->majorPointLine2;
125 if (major2.valid) {
126 createRefPoint(major2, list);
127 }
128 }
129
130 if (isInfoCursorForModificationEnabled()){
131 auto builder = msgStart()
132 .string(tr("Lines Join"));
133 if (lineJoinData->parallelLines) {
134 builder.add(tr("Lines are parallel"));
135 }
136 else{
137 builder.vector(tr("Intersection:"), lineJoinData->intersectPoint);
138 }
139 builder.toInfoCursorZone2(false);
140 }
141
142 // we don't need line joint data so far
143 delete lineJoinData;
144 }
145 }
146 break;
147 }
148 case ResolveFirstLineTrim: {
149 if (snappedLine != m_line1){
150 // 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
151 // should survive trim
152 snappedLine = nullptr;
153 }
154 highlightSelected(m_line1);
155 if (snappedLine != nullptr){
156 // retrieve current mouse position and recalculate line join data considering that mose position denotes part of line 1 that
157 // will survive trim operation
158 const RS_Vector coord = e->graphPoint;
159 updateLine1TrimData(coord);
160
161 const RS_Polyline *polyline = m_linesJoinData->polyline;
162 if (polyline != nullptr){
163 list << polyline->clone();
164 if (m_showRefEntitiesOnPreview) {
165 if (!m_linesJoinData->parallelLines) {
166 const RS_Vector &intersectionPoint = m_linesJoinData->intersectPoint;
167 createRefPoint(intersectionPoint, list);
168 }
169 createRefPoint(m_linesJoinData->majorPointLine1, list);
170 createRefPoint(m_linesJoinData->majorPointLine2, list);
171 }
172 }
173 }
174 break;
175 }
176 default:
177 break;
178 }
179}
180
181void LC_ActionModifyLineJoin::doOnLeftMouseButtonRelease(const LC_MouseEvent* e, const int status, [[maybe_unused]]const RS_Vector &snapPoint){
182 RS_Line *snappedLine = catchLine(e, false);
183 switch (status) {
1
Control jumps to 'case ResolveFirstLineTrim:' at line 247
184 case SetLine1: {
185 if (snappedLine != nullptr){ // just store first line and proceed to selection of second line
186 m_line1 = snappedLine;
187 m_line1ClickPosition = snapPoint;
188 setStatus(SetLine2);
189 } else {
190 commandMessage(tr("No line selected"));
191 }
192 break;
193 }
194 case SetLine2: {
195 if (snappedLine == m_line1){ // don't let to snap to the same line again
196 snappedLine = nullptr;
197 }
198 if (snappedLine != nullptr){
199 m_line2 = snappedLine;
200 const RS_Vector snap = e->graphPoint;
201 LC_LineJoinData *joinData = createLineJoinData(snappedLine, snap);
202 if (joinData != nullptr){
203 // check whether parallel lines were selected
204 if (joinData->parallelLines){
205 // check whether lines are on the same ray
206 if (joinData->straightLinesConnection){
207 // ok, let's merge them
208 m_linesJoinData = joinData;
209 trigger();
210 } else { // truly parallel lines, can't merge them
211 delete joinData;
212 commandMessage(tr("Lines are parallel, can't merge"));
213 }
214 } else { // lines may merge with angle
215 if (joinData->areLinesAlreadyIntersected()){
216 // both lines are already crossed, do nothing
217 commandMessage(tr("Lines already intersects, can't merge"));
218 delete joinData;
219 } else {
220 // check whether intersection is on the first line.
221 // if it so, and mode for line is EXTEND/TRIM, we need to ask the user
222 // which part of the line 1 should be trimmed and which one would
223 // survive trim.
224 // For such case, we need addition selection
225 bool firstLineTrimShouldBeSpecified = joinData->isIntersectionOnLine1();
226 if (m_line1EdgeMode != EDGE_EXTEND_TRIM){
227 firstLineTrimShouldBeSpecified = false;
228 }
229 m_linesJoinData = joinData;
230 if (firstLineTrimShouldBeSpecified){
231 // we need to addition hint from the user for proper trim, so go to the corresponding state
232 setStatus(ResolveFirstLineTrim);
233 highlightEntity(m_line1);
234 redraw();
235 } else {
236 // we are find for joining/trimming lines, just invoke trigger
237 trigger();
238 }
239 }
240 }
241 } else {
242 commandMessage(tr("No line selected"));
243 }
244 }
245 break;
246 }
247 case ResolveFirstLineTrim: {
248 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
249 const RS_Vector snap = e->graphPoint;
250 // update trim data according to selected part of line 1
251 updateLine1TrimData(snap);
4
Calling 'LC_ActionModifyLineJoin::updateLine1TrimData'
252 // check if polyline is built and if it so - trigger action
253 const RS_Polyline *polyline = m_linesJoinData->polyline;
254 if (polyline != nullptr){
255 trigger();
256 }
257 } else {
258 highlightEntity(m_line1);
259 }
260 break;
261 }
262 default:
263 break;
264 }
265}
266
267void LC_ActionModifyLineJoin::doBack(const LC_MouseEvent* e, const int status){
268 LC_AbstractActionWithPreview::doBack(e, status);
269 switch (status) {
270 case SetLine1:
271 init(SetLine1 - 1);
272 break;
273 case SetLine2: {
274 setStatus(SetLine1);
275 break;
276 }
277 case ResolveFirstLineTrim: {
278 setStatus(SetLine2);
279 break;
280 }
281 default:
282 initPrevious(status);
283 break;
284 }
285}
286
287void LC_ActionModifyLineJoin::doAfterTrigger(){
288 LC_AbstractActionWithPreview::doAfterTrigger();
289 delete m_linesJoinData; // cleanup
290 m_linesJoinData = nullptr;
291 redraw();
292 // return to selection first line mode
293 init(SetLine1);
294}
295
296bool LC_ActionModifyLineJoin::doCheckMayTrigger(){
297 return m_linesJoinData != nullptr;
298}
299
300bool LC_ActionModifyLineJoin::isSetActivePenAndLayerOnTrigger(){
301 // the action will handle pen and layer based on ui settings
302 return false;
303}
304
305bool LC_ActionModifyLineJoin::doTriggerEntitiesPrepare(LC_DocumentModificationBatch& ctx){
306 if (m_linesJoinData->parallelLines && m_linesJoinData->straightLinesConnection){ // process straight lines
307 // simply create straight line
308 auto *line = createLine(m_linesJoinData->majorPointLine1, m_linesJoinData->majorPointLine2, ctx.entitiesToAdd);
309 // apply attributes according to settings
310 applyAttributes(line, true);
311 } else { // process lines that are not on the same ray, and are not parallel
312
313 const RS_Vector &intersectionPoint = m_linesJoinData->intersectPoint;
314
315 // during calculation of line data, we've already processed various modes for each line,
316 // therefore we rely on major points there and draw lines from them to intersection point
317 const RS_Vector &major1 = m_linesJoinData->majorPointLine1;
318 const RS_Vector &major2 = m_linesJoinData->majorPointLine2;
319
320 if (m_createPolyline && major1.valid && major2.valid){ // handle polyline mode
321 auto *poly = new RS_Polyline(m_document);
322 poly->addVertex(major1);
323 poly->addVertex(intersectionPoint);
324 poly->addVertex(major2);
325 applyAttributes(poly, true);
326 ctx += poly;
327
328 } else { // handle individual lines
329 if (major1.valid){
330 // proceed line 1 (extend or segment)
331 RS_Line* l1 = createLine(major1, intersectionPoint, ctx.entitiesToAdd);
332 applyAttributes(l1, true);
333 }
334 if (major2.valid){
335 // proceed line 2
336 RS_Line* l2 = createLine(intersectionPoint, major2, ctx.entitiesToAdd);
337 applyAttributes(l2, false);
338 }
339 }
340 }
341 // check whether original lines should be deleted
342 if (m_removeOriginalLines){
343 // proceed line 1
344 if (m_line1EdgeMode == EDGE_EXTEND_TRIM){
345 ctx -= m_line1;
346 }
347 // proceed line 2
348 if (m_line2EdgeMode == EDGE_EXTEND_TRIM){
349 ctx -= m_line2;
350 }
351 }
352 return true;
353}
354
355/**
356 * Apply pen and layer attributes to created lines
357 * @param e created entity
358 * @param forLine1 if true, we'll apply attributes to line 1, false - to line 2
359 */
360void LC_ActionModifyLineJoin::applyAttributes(RS_Entity *e, bool forLine1){
361 RS_Pen pen;
362 RS_Layer *layer = nullptr;
363 switch (m_attributesSource) {
364 case ATTRIBUTES_LINE_1: { // pick attributes from line 1
365 pen = m_line1->getPen(false);
366 e->setPen(pen);
367 layer = m_line1->getLayer(true);
368 e->setLayer(layer);
369 break;
370 }
371 case ATTRIBUTES_BOTH_LINES: {
372 // pick attributes from each original line individually
373 if (forLine1){ // proceed line 1
374 pen = m_line1->getPen(false);
375 e->setPen(pen);
376 layer = m_line1->getLayer(true);
377 e->setLayer(layer);
378 } else { // proceed line 2
379 pen = m_line2->getPen(false);
380 e->setPen(pen);
381 layer = m_line2->getLayer(true);
382 e->setLayer(layer);
383 }
384 break;
385 }
386 case ATTRIBUTES_LINE_2: {
387 // pick attributes from line 2
388 pen = m_line2->getPen(false);
389 e->setPen(pen);
390 layer = m_line2->getLayer(true);
391 e->setLayer(layer);
392 break;
393 }
394 case ATTRIBUTES_ACTIVE_PEN_LAYER: {
395 // just set for active pen and layer
396 setPenAndLayerToActive(e);
397 break;
398 }
399 default:
400 break;
401 }
402}
403
404/**
405 * Create line join data
406 * @param secondLine second line
407 * @param snapPoint snap point
408 * @return created line join data
409 */
410LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::createLineJoinData(const RS_Line *secondLine, const RS_Vector &snapPoint) const {
411 LC_LineJoinData *result = nullptr;
412
413 if (m_line1 != nullptr && secondLine != nullptr){
414
415 // prepare endpoints vectors
416 const RS_Vector line1Start = m_line1->getStartpoint();
417 const RS_Vector line1End = m_line1->getEndpoint();
418 const RS_Vector line2Start = secondLine->getStartpoint();
419 const RS_Vector line2End = secondLine->getEndpoint();
420
421 const RS_Vector intersection = LC_LineMath::getIntersectionLineLine(line1Start, line1End, line2Start, line2End);
422
423 // determine intersection point for line 1 and given line 2
424 if (intersection.valid){// has intersection between lines, proceed them
425 result = proceedNonParallelLines(m_line1ClickPosition, snapPoint, intersection, line1Start, line1End, line2Start, line2End);
426
427 } else {// has intersection between lines, proceed them
428 result = proceedParallelLinesJoin(line1Start, line1End, line2Start, line2End);
429 }
430 }
431 return result;
432}
433
434/**
435 * Determine line data for intersecting (non-parallel) lines
436 * @param line1ClickPoint
437 * @param snapPoint snap point
438 * @param intersectPoint
439 * @param line1Start
440 * @param line1End
441 * @param line2Start
442 * @param line2End
443 * @return
444 */
445LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::proceedNonParallelLines(const RS_Vector& line1ClickPoint, const RS_Vector &snapPoint,
446 const RS_Vector &intersectPoint,
447 const RS_Vector &line1Start, const RS_Vector &line1End,
448 const RS_Vector &line2Start, const RS_Vector &line2End) const {
449 auto *result = new LC_LineJoinData();
450
451 result->intersectPoint = intersectPoint;
452 result->parallelLines = false;
453
454 // resulting polyline
455 auto *polyline = new RS_Polyline(m_document);
456
457 // processing of line 1
458 // determining how intersection and snap points are located relating to line endpoints
459 const LC_PointsDisposition line1Disposition = determine3PointsDisposition(line1Start, line1End, intersectPoint, /*snapPoint*/line1ClickPoint);
460
461 // determine major point that will be used for drawing of resulting entities.
462 // Based on options, major point may be either one of line endpoints or intersection point
463
464 const RS_Vector pointFromLine1 = getMajorPointFromLine(m_line1EdgeMode, line1Start, line1End, line1Disposition);
465 result->majorPointLine1 = pointFromLine1;
466
467 // add major point for line 1 to polyline, if needed
468 if (pointFromLine1.valid){
469 polyline->addVertex(pointFromLine1);
470 }
471
472 // add intersection
473 polyline->addVertex(intersectPoint);
474
475 // processing of line 2
476 // again, determine how endpoints of line are located relating to intersection and snap points
477 const LC_PointsDisposition line2Disposition = determine3PointsDisposition(line2Start, line2End, intersectPoint, snapPoint);
478
479 // determine major point for line 2
480 const RS_Vector pointFromLine2 = getMajorPointFromLine(m_line2EdgeMode, line2Start, line2End, line2Disposition);
481 result->majorPointLine2 = pointFromLine2;
482
483 // add major point from line 2 to polyline, if needed
484 if (pointFromLine2.valid){
485 polyline->addVertex(pointFromLine2);
486 }
487
488 // finalise line point data fields
489 result->polyline = polyline;
490 result->line1Disposition = line1Disposition;
491 result->line2Disposition = line2Disposition;
492 result->intersectPoint = intersectPoint;
493 return result;
494}
495
496/**
497 * Updates previously calculated line join data for resolving trim of line 1 (if intersection of lines belongs to line2).
498 * Snap point specified by the user indicates part of line 1 that should survive trim operation.
499 * @param snap snap point
500 */
501void LC_ActionModifyLineJoin::updateLine1TrimData(const RS_Vector& snap) const {
502
503 const RS_Vector &line1Start = m_line1->getStartpoint();
5
Called C++ object pointer is null
504 const RS_Vector &line1End = m_line1->getEndpoint();
505 RS_Polyline *polyline = m_linesJoinData->polyline;
506
507 delete polyline;// we'll rebuild polyline, so delete original one
508
509 polyline = new RS_Polyline(m_document);
510 m_linesJoinData->polyline = polyline;
511
512 const RS_Vector &intersection = m_linesJoinData->intersectPoint;
513
514 // recalculate disposition of line 1 taking into consideration updated snap point
515 const LC_PointsDisposition line1Disposition = determine3PointsDisposition(line1Start, line1End, intersection, snap);
516
517 // update major point for line 1 based updated disposition
518 const RS_Vector pointFromLine1 = getMajorPointFromLine(m_line1EdgeMode, line1Start, line1End, line1Disposition);
519 m_linesJoinData->majorPointLine1 = pointFromLine1;
520
521 // add major point from line 1 to polyline
522 if (line1Start.valid){
523 polyline->addVertex(pointFromLine1);
524 }
525
526 // add intersection
527 polyline->addVertex(intersection);
528
529 // get major point for line 2
530 const RS_Vector pointFromLine2 = getMajorPointFromLine(m_line2EdgeMode, m_line2->getStartpoint(), m_line2->getEndpoint(), m_linesJoinData->line2Disposition);
531 m_linesJoinData->majorPointLine2 = pointFromLine2;
532
533 // add major point form line 2, if needed
534 if (pointFromLine2.valid){
535 polyline->addVertex(pointFromLine2);
536 }
537}
538
539/**
540 * Determining major point for the line. Major point depends on edges mode set for specific line and may represent either one of endpoints or
541 * intersection point. Major point for the line is the point which will be used for drawing a line from this point to intersection point.
542 * @param edgeMode edge mode for the line
543 * @param lineStart start point of the line
544 * @param lineEnd end point of the line
545 * @param lineDisposition disposition data
546 * @return major point of line
547 */
548RS_Vector LC_ActionModifyLineJoin::getMajorPointFromLine(
549 const int edgeMode,
550 const RS_Vector &lineStart,
551 const RS_Vector &lineEnd,
552 const LC_PointsDisposition &lineDisposition) const{
553 auto result = RS_Vector(false);
554 switch (edgeMode) {
555 case EDGE_EXTEND_TRIM:
556 switch (lineDisposition.dispositionMode) {
557 case LC_PointsDisposition::BOTH_POINTS_ON_RIGHT:
558 case LC_PointsDisposition::BOTH_POINTS_ON_LEFT: {
559 result = lineDisposition.farPoint; // we'll use endpoint of line that is more distant (not closer) from intersection
560 break;
561 }
562 case LC_PointsDisposition::MIDDLE_END_LEFT: {
563 if (lineDisposition.snapSelectionOnLeft){ // determine whether it should be start point or endpoint
564 result = lineEnd;
565 } else {
566 result = lineStart;
567 }
568 break;
569 }
570 case LC_PointsDisposition::MIDDLE_START_LEFT: {
571 if (lineDisposition.snapSelectionOnLeft){
572 result = lineStart;
573 } else {
574 result = lineEnd;
575 }
576 break;
577 }
578 default:
579 break;
580 }
581 break;
582 case EDGE_ADD_SEGMENT:
583 switch (lineDisposition.dispositionMode) {
584 case LC_PointsDisposition::BOTH_POINTS_ON_RIGHT:
585 case LC_PointsDisposition::BOTH_POINTS_ON_LEFT: {
586 // for adding segment, we'll select endpoint that is closest to the intersection
587 // as intersection point is outside of line
588 result = lineDisposition.closestPoint;
589 break;
590 }
591 case LC_PointsDisposition::MIDDLE_END_LEFT:
592 case LC_PointsDisposition::MIDDLE_START_LEFT: {
593 // we don't need major point for adding segment mode if intersection point is
594 // within line endpoints
595 break;
596 }
597 default:
598 break;
599 }
600 break;
601 case EDGE_NO_MODIFICATION:
602 break;
603 default:
604 break;
605 }
606 return result;
607}
608
609/**
610 * Resolving parallel lines - check whether both lines lies on the same ray so they may be joined
611 * @param line1Start
612 * @param line1End
613 * @param line2Start
614 * @param line2End
615 * @return line join data
616 */
617LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::proceedParallelLinesJoin(
618 const RS_Vector &line1Start, const RS_Vector &line1End,
619 const RS_Vector &line2Start, const RS_Vector &line2End) const{
620
621 auto result = new LC_LineJoinData();
622 result->parallelLines = true;
623
624 // check whether these lines are on the same ray vector
625 bool sameRay = LC_LineMath::areLinesOnSameRay(line1Start, line1End, line2Start, line2End);
626
627 if (sameRay){
628 result->straightLinesConnection = true;
629
630 // determine angle from start point to end point
631
632 double angle = line1Start.angleTo(line1End);
633
634 // copy on endpoints
635 auto startOnX1 = RS_Vector(line1Start);
636 auto endOnX1 = RS_Vector(line1End);
637 auto startOnX2 = RS_Vector(line2Start);
638 auto endOnX2 = RS_Vector(line2End);
639
640 // rotate all points around start point of line 1 do they will be on horizontal line (parallel to x-axis)
641 endOnX1.rotate(startOnX1, -angle);
642 startOnX2.rotate(startOnX1, -angle);
643 endOnX2.rotate(startOnX1, -angle);
644
645 // determine positions of each point on x
646 double sx1 = startOnX1.x;
647 double ex1 = endOnX1.x;
648 double sx2 = startOnX2.x;
649 double ex2 = endOnX2.x;
650
651 // orient all lines in one direction, from left to right
652
653 RS_Vector s1 = line1Start;
654 RS_Vector e1 = line1End;
655 RS_Vector s2 = line2Start;
656 RS_Vector e2 = line2End;
657
658 // determine who two lines are positioned relating each other.
659 // there may be either gap between lines or overlapping of lines
660
661 // position of all endpoints in the same direction,
662 // so start points are on the left side of end points
663
664 if (ex1 < sx1){
665 std::swap(sx1, ex1);
666 std::swap(s1, e1);
667 }
668
669 if (ex2 < sx2){
670 std::swap(sx2, ex2);
671 std::swap(s2, e2);
672 }
673
674 RS_Vector leftPoint; // most left point
675 RS_Vector rightPoint; // most right point
676 RS_Vector middleLeftPoint; // left point in the middle
677 RS_Vector middleRightPoint; // right point in the middle
678
679 // check we have intersection of two lines
680 bool hasIntersection = false;
681
682 if (sx1 < sx2){ // start of line 1 is on left from start of line 2
683 hasIntersection = sx2 < ex1; // check whether start of line 2 between start and end of line 1
684 leftPoint = s1;
685 if (hasIntersection){
686 if (ex1 > ex2){
687 rightPoint = e1;
688 } else {
689 rightPoint = e2;
690 }
691 } else {
692 rightPoint = e2;
693 middleLeftPoint = e1;
694 middleRightPoint = s2;
695 }
696 } else {
697 hasIntersection = sx1 < ex2; // check whether start of line 2 between start and end of line 1
698 leftPoint = s2;
699 if (hasIntersection){
700 if (ex1 > ex2){
701 rightPoint = e1;
702 } else {
703 rightPoint = e2;
704 }
705 } else {
706 rightPoint = e1;
707 middleRightPoint = s1;
708 middleLeftPoint = e2;
709 }
710 }
711
712 // if edge modes for both lines are set to Extend/Trim, we'll
713 // merge to lines into single line. Here we don't care whether lines are overlapping
714 // or not - just use external endpoints for the new line
715 if (m_line1EdgeMode == EDGE_EXTEND_TRIM || m_line2EdgeMode == EDGE_EXTEND_TRIM){
716 auto *polyline = new RS_Polyline(m_document);
717
718 // we just use most left and most right point as vertexes
719 polyline->addVertex(leftPoint);
720 polyline->addVertex(rightPoint);
721
722 // and consider them as major points. Potentially, left and right might be from different lines and so
723 // major point may be set for wrong line - yet it seems that's ok for the overall logic of joining
724 // for merging lines, it's hard to properly determine right direction of resulting line and, most probably,
725 // it's direction is not important at all.
726
727 result->majorPointLine1 = leftPoint;
728 result->majorPointLine2 = rightPoint;
729
730 result->polyline = polyline;
731 } else if (m_line1EdgeMode == EDGE_ADD_SEGMENT && m_line2EdgeMode == EDGE_ADD_SEGMENT){
732 // if edge mode is adding segment for both lines, we'll create a single line that
733 // fills a gap between lines if there is no intersection
734 if (!hasIntersection){
735 // we can do this only if lines are not overlapped
736 auto *polyline = new RS_Polyline(m_document);
737
738 // add gap points
739 polyline->addVertex(middleLeftPoint);
740 polyline->addVertex(middleRightPoint);
741
742 // use them as major points
743 result->majorPointLine1 = middleLeftPoint;
744 result->majorPointLine2 = middleRightPoint;
745 result->polyline = polyline;
746 }
747 }
748 } else {
749 result->straightLinesConnection = false;
750 }
751 return result;
752}
753
754/**
755 * Function that determines how endpoints of line are located relating to provided intersection point, and on which side from
756 * intersection point provided snap point is located.
757 * @param start line start point
758 * @param end lint end point
759 * @param intersection intersection point
760 * @param snapPoint snap point
761 * @return points disposition
762 */
763LC_ActionModifyLineJoin::LC_PointsDisposition LC_ActionModifyLineJoin::determine3PointsDisposition(
764 const RS_Vector& start,
765 const RS_Vector& end,
766 const RS_Vector& intersection,
767 const RS_Vector &snapPoint) const{
768
769 // align all 3 points horizontally for simplicity of calculations, rotate them as needed
770
771 // angle of line
772 const double angle = start.angleTo(end);
773
774 // copy of endpoints for rotation
775 auto startOnX = RS_Vector(start);
776 auto endOnX = RS_Vector(end);
777
778 startOnX.rotate(intersection, -angle);
779 endOnX.rotate(intersection, -angle);
780
781 // projection of snap point to line
782 RS_Vector snapProjection = LC_LineMath::getNearestPointOnInfiniteLine(snapPoint, start, end);
783
784 // rotated snap point projection
785 const RS_Vector snapOnX = snapProjection.rotate(intersection, -angle);
786
787 // move coordinates so that the intersection point is in 0
788
789 const double ix = intersection.x;
790 const double delta = ix;
791
792
793 double startX = startOnX.x;
794 double endX = endOnX.x;
795 double snapX = snapOnX.x;
796
797 startX -= delta;
798 endX -= delta;
799 snapX -= delta;
800
801 LC_PointsDisposition result;
802
803 // determine where all points are located relating to intersection point
804 if (startX > 0){
805 if (endX > 0){
806 result.dispositionMode = LC_PointsDisposition::BOTH_POINTS_ON_RIGHT; // intersection is on the left side of line
807 } else {
808 result.dispositionMode = LC_PointsDisposition::MIDDLE_END_LEFT; // intersection is between endpoints, end point is on the left
809 }
810 } else {
811 if (endX > 0){
812 result.dispositionMode = LC_PointsDisposition::MIDDLE_START_LEFT; // intersection is between endpoints, start point is on the left
813 } else {
814 result.dispositionMode = LC_PointsDisposition::BOTH_POINTS_ON_LEFT; // both points are on the right side from intersection
815 }
816 }
817
818 // determine which endpoint of the line is closest to the intersection point
819 if (std::abs(endX) > std::abs(startX)){
820 result.closestPoint = start;
821 result.farPoint = end;
822 } else {
823 result.closestPoint = end;
824 result.farPoint = start;
825 }
826
827 // determine position of snap point relating to intersection point, if any
828 // check whether snap point is on the left side of line and intersection point is on the right
829 result.snapSelectionOnLeft = snapX < 0;
830 result.startPoint = start;
831 result.endPoint = end;
832 return result;
833}
834
835void LC_ActionModifyLineJoin::updateActionPrompt(){
836 switch (getStatus()) {
837 case SetLine1:
838 updatePromptTRCancel(tr("Select first line"));
839 break;
840 case SetLine2:
841 updatePromptTRBack(tr("Select second line"));
842 break;
843 case ResolveFirstLineTrim:
844 updatePromptTRBack(tr("Select part of first line that should remain after trim"));
845 break;
846 default:
847 updatePrompt();
848 break;
849 }
850}
851
852LC_ActionOptionsWidget* LC_ActionModifyLineJoin::createOptionsWidget(){
853 return new LC_LineJoinOptionsWidget();
854}
855
856LC_ActionOptionsPropertiesFiller* LC_ActionModifyLineJoin::createOptionsFiller() {
857 return new LC_LineJoinOptionsFiller();
858}
859
860RS2::CursorType LC_ActionModifyLineJoin::doGetMouseCursor([[maybe_unused]]int status){
861 return RS2::SelectCursor;
862}
863
864void LC_ActionModifyLineJoin::setAttributesSource(const int value){
865 m_attributesSource = value;
866}
867
868void LC_ActionModifyLineJoin::setCreatePolyline(const bool value){
869 m_createPolyline = value;
870}
871
872void LC_ActionModifyLineJoin::setRemoveOriginalLines(const bool value){
873 m_removeOriginalLines = value;
874}
875
876void LC_ActionModifyLineJoin::setLine1EdgeMode(const int value){
877 m_line1EdgeMode = value;
878}
879
880void LC_ActionModifyLineJoin::setLine2EdgeMode(const int value){
881 m_line2EdgeMode = value;
882}