Changeset 18965 in josm for trunk/src/org
- Timestamp:
- 2024-01-30T20:06:45+01:00 (10 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/mapmode/ImproveWayAccuracyAction.java
r18274 r18965 32 32 import org.openstreetmap.josm.data.UndoRedoHandler; 33 33 import org.openstreetmap.josm.data.coor.EastNorth; 34 import org.openstreetmap.josm.data.coor.ILatLon; 34 35 import org.openstreetmap.josm.data.osm.DataSelectionListener; 35 36 import org.openstreetmap.josm.data.osm.DataSet; … … 269 270 // Non-native highlighting is used here as well. 270 271 271 // Finding endpoints272 Node p1 = null;273 Node p2 = null;274 272 if (ctrl && candidateSegment != null) { 275 g.setStroke(ADD_NODE_STROKE.get()); 276 try { 277 p1 = candidateSegment.getFirstNode(); 278 p2 = candidateSegment.getSecondNode(); 279 } catch (ArrayIndexOutOfBoundsException e) { 280 Logging.error(e); 281 } 273 paintAddNodeStroke(g); 282 274 } else if (!alt && !ctrl && candidateNode != null) { 283 g.setStroke(MOVE_NODE_STROKE.get()); 284 List<Pair<Node, Node>> wpps = targetWay.getNodePairs(false); 285 for (Pair<Node, Node> wpp : wpps) { 286 if (wpp.a == candidateNode) { 287 p1 = wpp.b; 288 } 289 if (wpp.b == candidateNode) { 290 p2 = wpp.a; 291 } 292 if (p1 != null && p2 != null) { 293 break; 294 } 295 } 275 paintMoveNodeStroke(g); 296 276 } else if (alt && !ctrl && candidateNode != null) { 297 g.setStroke(DELETE_NODE_STROKE.get()); 298 List<Node> nodes = targetWay.getNodes(); 299 int index = nodes.indexOf(candidateNode); 300 301 // Only draw line if node is not first and/or last 302 if (index > 0 && index < (nodes.size() - 1)) { 303 p1 = nodes.get(index - 1); 304 p2 = nodes.get(index + 1); 305 } else if (targetWay.isClosed()) { 306 p1 = targetWay.getNode(1); 307 p2 = targetWay.getNode(nodes.size() - 2); 308 } 309 // TODO: indicate what part that will be deleted? (for end nodes) 310 } 311 312 313 // Drawing preview lines 314 MapViewPath b = new MapViewPath(mv); 315 if (alt && !ctrl) { 316 // In delete mode 317 if (p1 != null && p2 != null) { 318 b.moveTo(p1); 319 b.lineTo(p2); 320 } 321 } else { 322 // In add or move mode 323 if (p1 != null) { 324 b.moveTo(mousePos.x, mousePos.y); 325 b.lineTo(p1); 326 } 327 if (p2 != null) { 328 b.moveTo(mousePos.x, mousePos.y); 329 b.lineTo(p2); 330 } 331 } 277 paintDeleteStroke(g); 278 } 279 } 280 } 281 282 /** 283 * Paint the add stroke for {@link State#IMPROVING} 284 * @param g The graphics 285 */ 286 private void paintAddNodeStroke(Graphics2D g) { 287 g.setStroke(ADD_NODE_STROKE.get()); 288 ILatLon p1 = null; 289 ILatLon p2 = null; 290 try { 291 p1 = candidateSegment.getFirstNode(); 292 p2 = candidateSegment.getSecondNode(); 293 } catch (ArrayIndexOutOfBoundsException e) { 294 Logging.error(e); 295 } 296 paintImprovingPreviewLines(g, p1, p2); 297 } 298 299 /** 300 * Paint the move stroke for {@link State#IMPROVING} 301 * @param g The graphics 302 */ 303 private void paintMoveNodeStroke(Graphics2D g) { 304 g.setStroke(MOVE_NODE_STROKE.get()); 305 List<Pair<Node, Node>> wpps = targetWay.getNodePairs(false); 306 ILatLon p1 = null; 307 ILatLon p2 = null; 308 for (Pair<Node, Node> wpp : wpps) { 309 if (wpp.a == candidateNode) { 310 p1 = wpp.b; 311 } 312 if (wpp.b == candidateNode) { 313 p2 = wpp.a; 314 } 315 if (p1 != null && p2 != null) { 316 break; 317 } 318 } 319 paintImprovingPreviewLines(g, p1, p2); 320 } 321 322 /** 323 * Paint the delete stroke for {@link State#IMPROVING} 324 * @param g The graphics 325 */ 326 private void paintDeleteStroke(Graphics2D g) { 327 g.setStroke(DELETE_NODE_STROKE.get()); 328 List<Node> nodes = targetWay.getNodes(); 329 int index = nodes.indexOf(candidateNode); 330 ILatLon p1 = null; 331 ILatLon p2 = null; 332 // Only draw line if node is not first and/or last 333 if (index > 0 && index < (nodes.size() - 1)) { 334 p1 = nodes.get(index - 1); 335 p2 = nodes.get(index + 1); 336 } else if (targetWay.isClosed()) { 337 p1 = targetWay.getNode(1); 338 p2 = targetWay.getNode(nodes.size() - 2); 339 } 340 paintImprovingPreviewLines(g, p1, p2); 341 // TODO: indicate what part that will be deleted? (for end nodes) 342 } 343 344 /** 345 * Paint the preview lines for {@link State#IMPROVING} 346 * @param g The graphics 347 * @param p1 The first endpoint 348 * @param p2 The second endpoint 349 */ 350 private void paintImprovingPreviewLines(Graphics2D g, ILatLon p1, ILatLon p2) { 351 // Drawing preview lines 352 MapViewPath b = new MapViewPath(mv); 353 if (alt && !ctrl) { 354 // In delete mode 355 if (p1 != null && p2 != null) { 356 b.moveTo(p1); 357 b.lineTo(p2); 358 } 359 } else { 360 // In add or move mode 361 if (p1 != null) { 362 b.moveTo(mousePos.x, mousePos.y); 363 b.lineTo(p1); 364 } 365 if (p2 != null) { 366 b.moveTo(mousePos.x, mousePos.y); 367 b.lineTo(p2); 368 } 369 } 370 g.draw(b.computeClippedLine(g.getStroke())); 371 372 // Highlighting candidateNode 373 if (candidateNode != null) { 374 p1 = candidateNode; 375 g.fill(new MapViewPath(mv).shapeAround(p1, SymbolShape.SQUARE, DOT_SIZE.get())); 376 } 377 378 if (!alt && !ctrl && candidateNode != null) { 379 b.reset(); 380 drawIntersectingWayHelperLines(mv, b); 381 g.setStroke(MOVE_NODE_INTERSECTING_STROKE.get()); 332 382 g.draw(b.computeClippedLine(g.getStroke())); 333 334 // Highlighting candidateNode335 if (candidateNode != null) {336 p1 = candidateNode;337 g.fill(new MapViewPath(mv).shapeAround(p1, SymbolShape.SQUARE, DOT_SIZE.get()));338 }339 340 if (!alt && !ctrl && candidateNode != null) {341 b.reset();342 drawIntersectingWayHelperLines(mv, b);343 g.setStroke(MOVE_NODE_INTERSECTING_STROKE.get());344 g.draw(b.computeClippedLine(g.getStroke()));345 }346 347 383 } 348 384 } … … 440 476 441 477 if (ctrl && !alt && candidateSegment != null) { 442 // Add a new node to the highlighted segment. 443 Collection<WaySegment> virtualSegments = new LinkedList<>(); 444 445 // Check if other ways have the same segment. 446 // We have to make sure that we add the new node to all of them. 447 Set<Way> commonParentWays = new HashSet<>(candidateSegment.getFirstNode().getParentWays()); 448 commonParentWays.retainAll(candidateSegment.getSecondNode().getParentWays()); 449 for (Way w : commonParentWays) { 450 for (int i = 0; i < w.getNodesCount() - 1; i++) { 451 WaySegment testWS = new WaySegment(w, i); 452 if (testWS.isSimilar(candidateSegment)) { 453 virtualSegments.add(testWS); 454 } 455 } 456 } 457 458 Collection<Command> virtualCmds = new LinkedList<>(); 459 // Create the new node 460 Node virtualNode = new Node(mv.getEastNorth(mousePos.x, mousePos.y)); 461 virtualCmds.add(new AddCommand(ds, virtualNode)); 462 463 // Adding the node to all segments found 464 for (WaySegment virtualSegment : virtualSegments) { 465 Way w = virtualSegment.getWay(); 466 List<Node> modNodes = w.getNodes(); 467 modNodes.add(virtualSegment.getUpperIndex(), virtualNode); 468 virtualCmds.add(new ChangeNodesCommand(w, modNodes)); 469 } 470 471 // Finishing the sequence command 472 String text = trn("Add a new node to way", 473 "Add a new node to {0} ways", 474 virtualSegments.size(), virtualSegments.size()); 475 476 UndoRedoHandler.getInstance().add(new SequenceCommand(text, virtualCmds)); 477 478 addNode(ds); 478 479 } else if (alt && !ctrl && candidateNode != null) { 479 // Deleting the highlighted node 480 481 //check to see if node is in use by more than one object 482 long referrersCount = candidateNode.referrers(OsmPrimitive.class).count(); 483 long referrerWayCount = candidateNode.referrers(Way.class).count(); 484 if (referrersCount != 1 || referrerWayCount != 1) { 485 // detach node from way 486 final List<Node> nodes = targetWay.getNodes(); 487 nodes.remove(candidateNode); 488 if (nodes.size() < 2) { 489 final Command deleteCmd = DeleteCommand.delete(Collections.singleton(targetWay), true); 490 if (deleteCmd != null) { 491 UndoRedoHandler.getInstance().add(deleteCmd); 492 } 493 } else { 494 UndoRedoHandler.getInstance().add(new ChangeNodesCommand(targetWay, nodes)); 495 } 496 } else if (candidateNode.isTagged()) { 497 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 498 tr("Cannot delete node that has tags"), 499 tr("Error"), JOptionPane.ERROR_MESSAGE); 500 } else { 501 final Command deleteCmd = DeleteCommand.delete(Collections.singleton(candidateNode), true); 502 if (deleteCmd != null) { 503 UndoRedoHandler.getInstance().add(deleteCmd); 504 } 505 } 506 480 deleteHighlightedNode(); 507 481 } else if (candidateNode != null) { 508 // Moving the highlighted node 509 EastNorth nodeEN = candidateNode.getEastNorth(); 510 EastNorth cursorEN = mv.getEastNorth(mousePos.x, mousePos.y); 511 512 UndoRedoHandler.getInstance().add( 513 new MoveCommand(candidateNode, cursorEN.east() - nodeEN.east(), cursorEN.north() - nodeEN.north())); 514 515 SelectAction.checkCommandForLargeDistance(UndoRedoHandler.getInstance().getLastCommand()); 482 moveHighlightedNode(); 516 483 } 517 484 } … … 523 490 } 524 491 492 /** 493 * Add a new node to the currently highlighted segment 494 * @param ds The dataset to add the node to 495 */ 496 private void addNode(DataSet ds) { 497 // Add a new node to the highlighted segment. 498 Collection<WaySegment> virtualSegments = new LinkedList<>(); 499 500 // Check if other ways have the same segment. 501 // We have to make sure that we add the new node to all of them. 502 Set<Way> commonParentWays = new HashSet<>(candidateSegment.getFirstNode().getParentWays()); 503 commonParentWays.retainAll(candidateSegment.getSecondNode().getParentWays()); 504 for (Way w : commonParentWays) { 505 for (int i = 0; i < w.getNodesCount() - 1; i++) { 506 WaySegment testWS = new WaySegment(w, i); 507 if (testWS.isSimilar(candidateSegment)) { 508 virtualSegments.add(testWS); 509 } 510 } 511 } 512 513 Collection<Command> virtualCmds = new LinkedList<>(); 514 // Create the new node 515 Node virtualNode = new Node(mv.getEastNorth(mousePos.x, mousePos.y)); 516 virtualCmds.add(new AddCommand(ds, virtualNode)); 517 518 // Adding the node to all segments found 519 for (WaySegment virtualSegment : virtualSegments) { 520 Way w = virtualSegment.getWay(); 521 List<Node> modNodes = w.getNodes(); 522 modNodes.add(virtualSegment.getUpperIndex(), virtualNode); 523 virtualCmds.add(new ChangeNodesCommand(w, modNodes)); 524 } 525 526 // Finishing the sequence command 527 String text = trn("Add a new node to way", 528 "Add a new node to {0} ways", 529 virtualSegments.size(), virtualSegments.size()); 530 531 UndoRedoHandler.getInstance().add(new SequenceCommand(text, virtualCmds)); 532 } 533 534 /** 535 * Delete the highlighted node 536 */ 537 private void deleteHighlightedNode() { 538 // Deleting the highlighted node 539 540 //check to see if node is in use by more than one object 541 long referrersCount = candidateNode.referrers(OsmPrimitive.class).count(); 542 long referrerWayCount = candidateNode.referrers(Way.class).count(); 543 if (referrersCount != 1 || referrerWayCount != 1) { 544 // detach node from way 545 final List<Node> nodes = targetWay.getNodes(); 546 nodes.remove(candidateNode); 547 if (nodes.size() < 2) { 548 final Command deleteCmd = DeleteCommand.delete(Collections.singleton(targetWay), true); 549 if (deleteCmd != null) { 550 UndoRedoHandler.getInstance().add(deleteCmd); 551 } 552 } else { 553 UndoRedoHandler.getInstance().add(new ChangeNodesCommand(targetWay, nodes)); 554 } 555 } else if (candidateNode.isTagged()) { 556 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 557 tr("Cannot delete node that has tags"), 558 tr("Error"), JOptionPane.ERROR_MESSAGE); 559 } else { 560 final Command deleteCmd = DeleteCommand.delete(Collections.singleton(candidateNode), true); 561 if (deleteCmd != null) { 562 UndoRedoHandler.getInstance().add(deleteCmd); 563 } 564 } 565 } 566 567 /** 568 * Move the highlighted node 569 */ 570 private void moveHighlightedNode() { 571 // Moving the highlighted node 572 EastNorth nodeEN = candidateNode.getEastNorth(); 573 EastNorth cursorEN = mv.getEastNorth(mousePos.x, mousePos.y); 574 575 UndoRedoHandler.getInstance().add( 576 new MoveCommand(candidateNode, cursorEN.east() - nodeEN.east(), cursorEN.north() - nodeEN.north())); 577 578 SelectAction.checkCommandForLargeDistance(UndoRedoHandler.getInstance().getLastCommand()); 579 } 580 525 581 @Override 526 582 public void mouseExited(MouseEvent e) { … … 551 607 : cursorSelectHover, this); 552 608 } else if (state == State.IMPROVING) { 553 if (alt && !ctrl) { 554 mv.setNewCursor(cursorImproveDelete, this); 555 } else if (shift || dragging) { 556 if (ctrl) { 557 mv.setNewCursor(cursorImproveAddLock, this); 558 } else { 559 mv.setNewCursor(cursorImproveLock, this); 560 } 561 } else if (ctrl && !alt) { 562 mv.setNewCursor(cursorImproveAdd, this); 609 updateCursorImproving(); 610 } 611 } 612 613 /** 614 * Update the mouse cursor for the {@link State#IMPROVING} mode 615 */ 616 private void updateCursorImproving() { 617 if (alt && !ctrl) { 618 mv.setNewCursor(cursorImproveDelete, this); 619 } else if (shift || dragging) { 620 if (ctrl) { 621 mv.setNewCursor(cursorImproveAddLock, this); 563 622 } else { 564 mv.setNewCursor(cursorImprove, this); 565 } 623 mv.setNewCursor(cursorImproveLock, this); 624 } 625 } else if (ctrl && !alt) { 626 mv.setNewCursor(cursorImproveAdd, this); 627 } else { 628 mv.setNewCursor(cursorImprove, this); 566 629 } 567 630 } … … 642 705 */ 643 706 private void updateStateByCurrentSelection() { 707 final DataSet ds = getLayerManager().getEditDataSet(); 708 if (ds != null && selectWay(ds)) { 709 return; 710 } 711 712 // Starting selecting by default 713 startSelecting(); 714 } 715 716 /** 717 * Select the initial way 718 * @param ds The dataset to get the selection from 719 * @return {@code true} if a way was selected 720 */ 721 private boolean selectWay(DataSet ds) { 644 722 final List<Node> nodeList = new ArrayList<>(); 645 723 final List<Way> wayList = new ArrayList<>(); 646 final DataSet ds = getLayerManager().getEditDataSet(); 647 if (ds != null) { 648 final Collection<OsmPrimitive> sel = ds.getSelected(); 649 650 // Collecting nodes and ways from the selection 651 for (OsmPrimitive p : sel) { 652 if (p instanceof Way) { 653 wayList.add((Way) p); 654 } 655 if (p instanceof Node) { 656 nodeList.add((Node) p); 657 } 658 } 659 660 if (wayList.size() == 1) { 661 // Starting improving the single selected way 662 startImproving(wayList.get(0)); 663 return; 664 } else if (nodeList.size() == 1) { 665 // Starting improving the only way of the single selected node 666 List<OsmPrimitive> r = nodeList.get(0).getReferrers(); 667 if (r.size() == 1 && (r.get(0) instanceof Way)) { 668 startImproving((Way) r.get(0)); 669 return; 670 } 671 } 672 } 673 674 // Starting selecting by default 675 startSelecting(); 724 final Collection<OsmPrimitive> sel = ds.getSelected(); 725 726 // Collecting nodes and ways from the selection 727 for (OsmPrimitive p : sel) { 728 if (p instanceof Way) { 729 wayList.add((Way) p); 730 } 731 if (p instanceof Node) { 732 nodeList.add((Node) p); 733 } 734 } 735 736 if (wayList.size() == 1) { 737 // Starting improving the single selected way 738 startImproving(wayList.get(0)); 739 return true; 740 } else if (nodeList.size() == 1) { 741 // Starting improving the only way of the single selected node 742 List<OsmPrimitive> r = nodeList.get(0).getReferrers(); 743 r.removeIf(osm -> !osm.isUsable()); 744 if (r.size() == 1 && (r.get(0) instanceof Way)) { 745 startImproving((Way) r.get(0)); 746 return true; 747 } 748 } 749 return false; 676 750 } 677 751
Note:
See TracChangeset
for help on using the changeset viewer.