Bug Summary

File:librecad/src/actions/drawing/modify/lc_action_modify_line_join.cpp
Warning:line 509, 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 -O2 -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-21/lib/clang/21 -D _REENTRANT -D DWGSUPPORT -D MUPARSER_STATIC -D LC_WITH_OPENTYPE=0 -D QC_APPDIR=librecad -D LC_VERSION=2.2.2_alpha1-853-ge2a8c2931 -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 ../../libraries/qtbidi -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/linetypes -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/lib/gcc/x86_64-linux-gnu/15/../../../../include/c++/15 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../include/x86_64-linux-gnu/c++/15 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../include/c++/15/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -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-11-06-031751-4837-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 if (status == ResolveFirstLineTrim) {
269 // Back to the choice of line 2, keeping line 1: the base doBack() runs
270 // init(), which forgets both lines (#3196).
271 delete m_linesJoinData->polyline;
272 delete m_linesJoinData;
273 m_linesJoinData = nullptr;
274 m_line2 = nullptr;
275 setStatus(SetLine2);
276 return;
277 }
278 LC_AbstractActionWithPreview::doBack(e, status);
279 switch (status) {
280 case SetLine1:
281 init(SetLine1 - 1);
282 break;
283 case SetLine2: {
284 setStatus(SetLine1);
285 break;
286 }
287 default:
288 initPrevious(status);
289 break;
290 }
291}
292
293void LC_ActionModifyLineJoin::doAfterTrigger(){
294 LC_AbstractActionWithPreview::doAfterTrigger();
295 delete m_linesJoinData; // cleanup
296 m_linesJoinData = nullptr;
297 redraw();
298 // return to selection first line mode
299 init(SetLine1);
300}
301
302bool LC_ActionModifyLineJoin::doCheckMayTrigger(){
303 return m_linesJoinData != nullptr;
304}
305
306bool LC_ActionModifyLineJoin::isSetActivePenAndLayerOnTrigger(){
307 // the action will handle pen and layer based on ui settings
308 return false;
309}
310
311bool LC_ActionModifyLineJoin::doTriggerEntitiesPrepare(LC_DocumentModificationBatch& ctx){
312 if (m_linesJoinData->parallelLines && m_linesJoinData->straightLinesConnection){ // process straight lines
313 // simply create straight line
314 auto *line = createLine(m_linesJoinData->majorPointLine1, m_linesJoinData->majorPointLine2, ctx.entitiesToAdd);
315 // apply attributes according to settings
316 applyAttributes(line, true);
317 } else { // process lines that are not on the same ray, and are not parallel
318
319 const RS_Vector &intersectionPoint = m_linesJoinData->intersectPoint;
320
321 // during calculation of line data, we've already processed various modes for each line,
322 // therefore we rely on major points there and draw lines from them to intersection point
323 const RS_Vector &major1 = m_linesJoinData->majorPointLine1;
324 const RS_Vector &major2 = m_linesJoinData->majorPointLine2;
325
326 if (m_createPolyline && major1.valid && major2.valid){ // handle polyline mode
327 auto *poly = new RS_Polyline(m_document);
328 poly->addVertex(major1);
329 poly->addVertex(intersectionPoint);
330 poly->addVertex(major2);
331 applyAttributes(poly, true);
332 ctx += poly;
333
334 } else { // handle individual lines
335 if (major1.valid){
336 // proceed line 1 (extend or segment)
337 RS_Line* l1 = createLine(major1, intersectionPoint, ctx.entitiesToAdd);
338 applyAttributes(l1, true);
339 }
340 if (major2.valid){
341 // proceed line 2
342 RS_Line* l2 = createLine(intersectionPoint, major2, ctx.entitiesToAdd);
343 applyAttributes(l2, false);
344 }
345 }
346 }
347 // check whether original lines should be deleted
348 if (m_removeOriginalLines){
349 // proceed line 1
350 if (m_line1EdgeMode == EDGE_EXTEND_TRIM){
351 ctx -= m_line1;
352 }
353 // proceed line 2
354 if (m_line2EdgeMode == EDGE_EXTEND_TRIM){
355 ctx -= m_line2;
356 }
357 }
358 return true;
359}
360
361/**
362 * Apply pen and layer attributes to created lines
363 * @param e created entity
364 * @param forLine1 if true, we'll apply attributes to line 1, false - to line 2
365 */
366void LC_ActionModifyLineJoin::applyAttributes(RS_Entity *e, bool forLine1){
367 RS_Pen pen;
368 RS_Layer *layer = nullptr;
369 switch (m_attributesSource) {
370 case ATTRIBUTES_LINE_1: { // pick attributes from line 1
371 pen = m_line1->getPen(false);
372 e->setPen(pen);
373 layer = m_line1->getLayer(true);
374 e->setLayer(layer);
375 break;
376 }
377 case ATTRIBUTES_BOTH_LINES: {
378 // pick attributes from each original line individually
379 if (forLine1){ // proceed line 1
380 pen = m_line1->getPen(false);
381 e->setPen(pen);
382 layer = m_line1->getLayer(true);
383 e->setLayer(layer);
384 } else { // proceed line 2
385 pen = m_line2->getPen(false);
386 e->setPen(pen);
387 layer = m_line2->getLayer(true);
388 e->setLayer(layer);
389 }
390 break;
391 }
392 case ATTRIBUTES_LINE_2: {
393 // pick attributes from line 2
394 pen = m_line2->getPen(false);
395 e->setPen(pen);
396 layer = m_line2->getLayer(true);
397 e->setLayer(layer);
398 break;
399 }
400 case ATTRIBUTES_ACTIVE_PEN_LAYER: {
401 // just set for active pen and layer
402 setPenAndLayerToActive(e);
403 break;
404 }
405 default:
406 break;
407 }
408}
409
410/**
411 * Create line join data
412 * @param secondLine second line
413 * @param snapPoint snap point
414 * @return created line join data
415 */
416LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::createLineJoinData(const RS_Line *secondLine, const RS_Vector &snapPoint) const {
417 LC_LineJoinData *result = nullptr;
418
419 if (m_line1 != nullptr && secondLine != nullptr){
420
421 // prepare endpoints vectors
422 const RS_Vector line1Start = m_line1->getStartpoint();
423 const RS_Vector line1End = m_line1->getEndpoint();
424 const RS_Vector line2Start = secondLine->getStartpoint();
425 const RS_Vector line2End = secondLine->getEndpoint();
426
427 const RS_Vector intersection = LC_LineMath::getIntersectionLineLine(line1Start, line1End, line2Start, line2End);
428
429 // determine intersection point for line 1 and given line 2
430 if (intersection.valid){// has intersection between lines, proceed them
431 result = proceedNonParallelLines(m_line1ClickPosition, snapPoint, intersection, line1Start, line1End, line2Start, line2End);
432
433 } else {// has intersection between lines, proceed them
434 result = proceedParallelLinesJoin(line1Start, line1End, line2Start, line2End);
435 }
436 }
437 return result;
438}
439
440/**
441 * Determine line data for intersecting (non-parallel) lines
442 * @param line1ClickPoint
443 * @param snapPoint snap point
444 * @param intersectPoint
445 * @param line1Start
446 * @param line1End
447 * @param line2Start
448 * @param line2End
449 * @return
450 */
451LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::proceedNonParallelLines(const RS_Vector& line1ClickPoint, const RS_Vector &snapPoint,
452 const RS_Vector &intersectPoint,
453 const RS_Vector &line1Start, const RS_Vector &line1End,
454 const RS_Vector &line2Start, const RS_Vector &line2End) const {
455 auto *result = new LC_LineJoinData();
456
457 result->intersectPoint = intersectPoint;
458 result->parallelLines = false;
459
460 // resulting polyline
461 auto *polyline = new RS_Polyline(m_document);
462
463 // processing of line 1
464 // determining how intersection and snap points are located relating to line endpoints
465 const LC_PointsDisposition line1Disposition = determine3PointsDisposition(line1Start, line1End, intersectPoint, /*snapPoint*/line1ClickPoint);
466
467 // determine major point that will be used for drawing of resulting entities.
468 // Based on options, major point may be either one of line endpoints or intersection point
469
470 const RS_Vector pointFromLine1 = getMajorPointFromLine(m_line1EdgeMode, line1Start, line1End, line1Disposition);
471 result->majorPointLine1 = pointFromLine1;
472
473 // add major point for line 1 to polyline, if needed
474 if (pointFromLine1.valid){
475 polyline->addVertex(pointFromLine1);
476 }
477
478 // add intersection
479 polyline->addVertex(intersectPoint);
480
481 // processing of line 2
482 // again, determine how endpoints of line are located relating to intersection and snap points
483 const LC_PointsDisposition line2Disposition = determine3PointsDisposition(line2Start, line2End, intersectPoint, snapPoint);
484
485 // determine major point for line 2
486 const RS_Vector pointFromLine2 = getMajorPointFromLine(m_line2EdgeMode, line2Start, line2End, line2Disposition);
487 result->majorPointLine2 = pointFromLine2;
488
489 // add major point from line 2 to polyline, if needed
490 if (pointFromLine2.valid){
491 polyline->addVertex(pointFromLine2);
492 }
493
494 // finalise line point data fields
495 result->polyline = polyline;
496 result->line1Disposition = line1Disposition;
497 result->line2Disposition = line2Disposition;
498 result->intersectPoint = intersectPoint;
499 return result;
500}
501
502/**
503 * Updates previously calculated line join data for resolving trim of line 1 (if intersection of lines belongs to line2).
504 * Snap point specified by the user indicates part of line 1 that should survive trim operation.
505 * @param snap snap point
506 */
507void LC_ActionModifyLineJoin::updateLine1TrimData(const RS_Vector& snap) const {
508
509 const RS_Vector &line1Start = m_line1->getStartpoint();
5
Called C++ object pointer is null
510 const RS_Vector &line1End = m_line1->getEndpoint();
511 RS_Polyline *polyline = m_linesJoinData->polyline;
512
513 delete polyline;// we'll rebuild polyline, so delete original one
514
515 polyline = new RS_Polyline(m_document);
516 m_linesJoinData->polyline = polyline;
517
518 const RS_Vector &intersection = m_linesJoinData->intersectPoint;
519
520 // recalculate disposition of line 1 taking into consideration updated snap point
521 const LC_PointsDisposition line1Disposition = determine3PointsDisposition(line1Start, line1End, intersection, snap);
522
523 // update major point for line 1 based updated disposition
524 const RS_Vector pointFromLine1 = getMajorPointFromLine(m_line1EdgeMode, line1Start, line1End, line1Disposition);
525 m_linesJoinData->majorPointLine1 = pointFromLine1;
526
527 // add major point from line 1 to polyline
528 if (line1Start.valid){
529 polyline->addVertex(pointFromLine1);
530 }
531
532 // add intersection
533 polyline->addVertex(intersection);
534
535 // get major point for line 2
536 const RS_Vector pointFromLine2 = getMajorPointFromLine(m_line2EdgeMode, m_line2->getStartpoint(), m_line2->getEndpoint(), m_linesJoinData->line2Disposition);
537 m_linesJoinData->majorPointLine2 = pointFromLine2;
538
539 // add major point form line 2, if needed
540 if (pointFromLine2.valid){
541 polyline->addVertex(pointFromLine2);
542 }
543}
544
545/**
546 * Determining major point for the line. Major point depends on edges mode set for specific line and may represent either one of endpoints or
547 * intersection point. Major point for the line is the point which will be used for drawing a line from this point to intersection point.
548 * @param edgeMode edge mode for the line
549 * @param lineStart start point of the line
550 * @param lineEnd end point of the line
551 * @param lineDisposition disposition data
552 * @return major point of line
553 */
554RS_Vector LC_ActionModifyLineJoin::getMajorPointFromLine(
555 const int edgeMode,
556 const RS_Vector &lineStart,
557 const RS_Vector &lineEnd,
558 const LC_PointsDisposition &lineDisposition) const{
559 auto result = RS_Vector(false);
560 switch (edgeMode) {
561 case EDGE_EXTEND_TRIM:
562 switch (lineDisposition.dispositionMode) {
563 case LC_PointsDisposition::BOTH_POINTS_ON_RIGHT:
564 case LC_PointsDisposition::BOTH_POINTS_ON_LEFT: {
565 result = lineDisposition.farPoint; // we'll use endpoint of line that is more distant (not closer) from intersection
566 break;
567 }
568 case LC_PointsDisposition::MIDDLE_END_LEFT: {
569 if (lineDisposition.snapSelectionOnLeft){ // determine whether it should be start point or endpoint
570 result = lineEnd;
571 } else {
572 result = lineStart;
573 }
574 break;
575 }
576 case LC_PointsDisposition::MIDDLE_START_LEFT: {
577 if (lineDisposition.snapSelectionOnLeft){
578 result = lineStart;
579 } else {
580 result = lineEnd;
581 }
582 break;
583 }
584 default:
585 break;
586 }
587 break;
588 case EDGE_ADD_SEGMENT:
589 switch (lineDisposition.dispositionMode) {
590 case LC_PointsDisposition::BOTH_POINTS_ON_RIGHT:
591 case LC_PointsDisposition::BOTH_POINTS_ON_LEFT: {
592 // for adding segment, we'll select endpoint that is closest to the intersection
593 // as intersection point is outside of line
594 result = lineDisposition.closestPoint;
595 break;
596 }
597 case LC_PointsDisposition::MIDDLE_END_LEFT:
598 case LC_PointsDisposition::MIDDLE_START_LEFT: {
599 // we don't need major point for adding segment mode if intersection point is
600 // within line endpoints
601 break;
602 }
603 default:
604 break;
605 }
606 break;
607 case EDGE_NO_MODIFICATION:
608 break;
609 default:
610 break;
611 }
612 return result;
613}
614
615/**
616 * Resolving parallel lines - check whether both lines lies on the same ray so they may be joined
617 * @param line1Start
618 * @param line1End
619 * @param line2Start
620 * @param line2End
621 * @return line join data
622 */
623LC_ActionModifyLineJoin::LC_LineJoinData *LC_ActionModifyLineJoin::proceedParallelLinesJoin(
624 const RS_Vector &line1Start, const RS_Vector &line1End,
625 const RS_Vector &line2Start, const RS_Vector &line2End) const{
626
627 auto result = new LC_LineJoinData();
628 result->parallelLines = true;
629
630 // check whether these lines are on the same ray vector
631 bool sameRay = LC_LineMath::areLinesOnSameRay(line1Start, line1End, line2Start, line2End);
632
633 if (sameRay){
634 result->straightLinesConnection = true;
635
636 // determine angle from start point to end point
637
638 double angle = line1Start.angleTo(line1End);
639
640 // copy on endpoints
641 auto startOnX1 = RS_Vector(line1Start);
642 auto endOnX1 = RS_Vector(line1End);
643 auto startOnX2 = RS_Vector(line2Start);
644 auto endOnX2 = RS_Vector(line2End);
645
646 // rotate all points around start point of line 1 do they will be on horizontal line (parallel to x-axis)
647 endOnX1.rotate(startOnX1, -angle);
648 startOnX2.rotate(startOnX1, -angle);
649 endOnX2.rotate(startOnX1, -angle);
650
651 // determine positions of each point on x
652 double sx1 = startOnX1.x;
653 double ex1 = endOnX1.x;
654 double sx2 = startOnX2.x;
655 double ex2 = endOnX2.x;
656
657 // orient all lines in one direction, from left to right
658
659 RS_Vector s1 = line1Start;
660 RS_Vector e1 = line1End;
661 RS_Vector s2 = line2Start;
662 RS_Vector e2 = line2End;
663
664 // determine who two lines are positioned relating each other.
665 // there may be either gap between lines or overlapping of lines
666
667 // position of all endpoints in the same direction,
668 // so start points are on the left side of end points
669
670 if (ex1 < sx1){
671 std::swap(sx1, ex1);
672 std::swap(s1, e1);
673 }
674
675 if (ex2 < sx2){
676 std::swap(sx2, ex2);
677 std::swap(s2, e2);
678 }
679
680 RS_Vector leftPoint; // most left point
681 RS_Vector rightPoint; // most right point
682 RS_Vector middleLeftPoint; // left point in the middle
683 RS_Vector middleRightPoint; // right point in the middle
684
685 // check we have intersection of two lines
686 bool hasIntersection = false;
687
688 if (sx1 < sx2){ // start of line 1 is on left from start of line 2
689 hasIntersection = sx2 < ex1; // check whether start of line 2 between start and end of line 1
690 leftPoint = s1;
691 if (hasIntersection){
692 if (ex1 > ex2){
693 rightPoint = e1;
694 } else {
695 rightPoint = e2;
696 }
697 } else {
698 rightPoint = e2;
699 middleLeftPoint = e1;
700 middleRightPoint = s2;
701 }
702 } else {
703 hasIntersection = sx1 < ex2; // check whether start of line 2 between start and end of line 1
704 leftPoint = s2;
705 if (hasIntersection){
706 if (ex1 > ex2){
707 rightPoint = e1;
708 } else {
709 rightPoint = e2;
710 }
711 } else {
712 rightPoint = e1;
713 middleRightPoint = s1;
714 middleLeftPoint = e2;
715 }
716 }
717
718 // if edge modes for both lines are set to Extend/Trim, we'll
719 // merge to lines into single line. Here we don't care whether lines are overlapping
720 // or not - just use external endpoints for the new line
721 if (m_line1EdgeMode == EDGE_EXTEND_TRIM || m_line2EdgeMode == EDGE_EXTEND_TRIM){
722 auto *polyline = new RS_Polyline(m_document);
723
724 // we just use most left and most right point as vertexes
725 polyline->addVertex(leftPoint);
726 polyline->addVertex(rightPoint);
727
728 // and consider them as major points. Potentially, left and right might be from different lines and so
729 // major point may be set for wrong line - yet it seems that's ok for the overall logic of joining
730 // for merging lines, it's hard to properly determine right direction of resulting line and, most probably,
731 // it's direction is not important at all.
732
733 result->majorPointLine1 = leftPoint;
734 result->majorPointLine2 = rightPoint;
735
736 result->polyline = polyline;
737 } else if (m_line1EdgeMode == EDGE_ADD_SEGMENT && m_line2EdgeMode == EDGE_ADD_SEGMENT){
738 // if edge mode is adding segment for both lines, we'll create a single line that
739 // fills a gap between lines if there is no intersection
740 if (!hasIntersection){
741 // we can do this only if lines are not overlapped
742 auto *polyline = new RS_Polyline(m_document);
743
744 // add gap points
745 polyline->addVertex(middleLeftPoint);
746 polyline->addVertex(middleRightPoint);
747
748 // use them as major points
749 result->majorPointLine1 = middleLeftPoint;
750 result->majorPointLine2 = middleRightPoint;
751 result->polyline = polyline;
752 }
753 }
754 } else {
755 result->straightLinesConnection = false;
756 }
757 return result;
758}
759
760/**
761 * Function that determines how endpoints of line are located relating to provided intersection point, and on which side from
762 * intersection point provided snap point is located.
763 * @param start line start point
764 * @param end lint end point
765 * @param intersection intersection point
766 * @param snapPoint snap point
767 * @return points disposition
768 */
769LC_ActionModifyLineJoin::LC_PointsDisposition LC_ActionModifyLineJoin::determine3PointsDisposition(
770 const RS_Vector& start,
771 const RS_Vector& end,
772 const RS_Vector& intersection,
773 const RS_Vector &snapPoint) const{
774
775 // align all 3 points horizontally for simplicity of calculations, rotate them as needed
776
777 // angle of line
778 const double angle = start.angleTo(end);
779
780 // copy of endpoints for rotation
781 auto startOnX = RS_Vector(start);
782 auto endOnX = RS_Vector(end);
783
784 startOnX.rotate(intersection, -angle);
785 endOnX.rotate(intersection, -angle);
786
787 // projection of snap point to line
788 RS_Vector snapProjection = LC_LineMath::getNearestPointOnInfiniteLine(snapPoint, start, end);
789
790 // rotated snap point projection
791 const RS_Vector snapOnX = snapProjection.rotate(intersection, -angle);
792
793 // move coordinates so that the intersection point is in 0
794
795 const double ix = intersection.x;
796 const double delta = ix;
797
798
799 double startX = startOnX.x;
800 double endX = endOnX.x;
801 double snapX = snapOnX.x;
802
803 startX -= delta;
804 endX -= delta;
805 snapX -= delta;
806
807 LC_PointsDisposition result;
808
809 // determine where all points are located relating to intersection point
810 if (startX > 0){
811 if (endX > 0){
812 result.dispositionMode = LC_PointsDisposition::BOTH_POINTS_ON_RIGHT; // intersection is on the left side of line
813 } else {
814 result.dispositionMode = LC_PointsDisposition::MIDDLE_END_LEFT; // intersection is between endpoints, end point is on the left
815 }
816 } else {
817 if (endX > 0){
818 result.dispositionMode = LC_PointsDisposition::MIDDLE_START_LEFT; // intersection is between endpoints, start point is on the left
819 } else {
820 result.dispositionMode = LC_PointsDisposition::BOTH_POINTS_ON_LEFT; // both points are on the right side from intersection
821 }
822 }
823
824 // determine which endpoint of the line is closest to the intersection point
825 if (std::abs(endX) > std::abs(startX)){
826 result.closestPoint = start;
827 result.farPoint = end;
828 } else {
829 result.closestPoint = end;
830 result.farPoint = start;
831 }
832
833 // determine position of snap point relating to intersection point, if any
834 // check whether snap point is on the left side of line and intersection point is on the right
835 result.snapSelectionOnLeft = snapX < 0;
836 result.startPoint = start;
837 result.endPoint = end;
838 return result;
839}
840
841void LC_ActionModifyLineJoin::updateActionPrompt(){
842 switch (getStatus()) {
843 case SetLine1:
844 updatePromptTRCancel(tr("Select first line"));
845 break;
846 case SetLine2:
847 updatePromptTRBack(tr("Select second line"));
848 break;
849 case ResolveFirstLineTrim:
850 updatePromptTRBack(tr("Select part of first line that should remain after trim"));
851 break;
852 default:
853 updatePrompt();
854 break;
855 }
856}
857
858LC_ActionOptionsWidget* LC_ActionModifyLineJoin::createOptionsWidget(){
859 return new LC_LineJoinOptionsWidget();
860}
861
862LC_ActionOptionsPropertiesFiller* LC_ActionModifyLineJoin::createOptionsFiller() {
863 return new LC_LineJoinOptionsFiller();
864}
865
866RS2::CursorType LC_ActionModifyLineJoin::doGetMouseCursor([[maybe_unused]]int status){
867 return RS2::SelectCursor;
868}
869
870void LC_ActionModifyLineJoin::setAttributesSource(const int value){
871 m_attributesSource = value;
872}
873
874void LC_ActionModifyLineJoin::setCreatePolyline(const bool value){
875 m_createPolyline = value;
876}
877
878void LC_ActionModifyLineJoin::setRemoveOriginalLines(const bool value){
879 m_removeOriginalLines = value;
880}
881
882void LC_ActionModifyLineJoin::setLine1EdgeMode(const int value){
883 m_line1EdgeMode = value;
884}
885
886void LC_ActionModifyLineJoin::setLine2EdgeMode(const int value){
887 m_line2EdgeMode = value;
888}