add flexible leads to some components through the menu

This commit is contained in:
bancika
2020-04-18 10:26:14 +02:00
parent f41a028236
commit f0219fce0b
14 changed files with 557 additions and 293 deletions
@@ -375,5 +375,12 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
/** /**
* @return size of extra space around the canvas * @return size of extra space around the canvas
*/ */
double getExtraSpace(); double getExtraSpace();
/**
* Applies changes to the current project as specified by the provided {@link IProjectEditor}.
*
* @param editor
*/
void applyEditor(IProjectEditor editor);
} }
@@ -0,0 +1,34 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.common;
import java.util.Set;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
public interface IProjectEditor {
Set<IDIYComponent<?>> edit(Project project, Set<IDIYComponent<?>> selection);
String getEditAction();
}
@@ -66,6 +66,7 @@ import org.diylc.common.IKeyProcessor;
import org.diylc.common.INetlistAnalyzer; import org.diylc.common.INetlistAnalyzer;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.common.IProjectEditor;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.common.VariantPackage; import org.diylc.common.VariantPackage;
import org.diylc.core.ExpansionMode; import org.diylc.core.ExpansionMode;
@@ -2288,6 +2289,29 @@ public class Presenter implements IPlugInPort {
drawingManager.fireZoomChanged(); drawingManager.fireZoomChanged();
} }
} }
@Override
public void applyEditor(IProjectEditor editor) {
LOG.debug(String.format("applyEditor(%s)", editor.getEditAction()));
Project oldProject = currentProject.clone();
try {
Set<IDIYComponent<?>> newSelection = editor.edit(currentProject, selectedComponents);
if (newSelection != null)
updateSelection(newSelection);
} catch (Exception e) {
LOG.error("Could not apply editor", e);
view.showMessage("Could not apply " + editor.getEditAction() + ". Check the log for details.", "Error", IView.ERROR_MESSAGE);
} finally {
// Notify the listeners.
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
editor.getEditAction());
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
drawingManager.fireZoomChanged();
}
}
@Override @Override
public ComponentType getNewComponentTypeSlot() { public ComponentType getNewComponentTypeSlot() {
Binary file not shown.
@@ -69,6 +69,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
public static Color LEAD_COLOR_ICON = LEAD_COLOR.darker().darker(); public static Color LEAD_COLOR_ICON = LEAD_COLOR.darker().darker();
public static Size LEAD_THICKNESS = new Size(0.6d, SizeUnit.mm); public static Size LEAD_THICKNESS = new Size(0.6d, SizeUnit.mm);
public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in); public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in);
private static final int LEAD_RENDER_THRESHOLD = 2;
protected Size length; protected Size length;
protected Size width; protected Size width;
@@ -203,32 +204,35 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
float leadThickness = getLeadThickness(); float leadThickness = getLeadThickness();
double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2; double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2;
if (shouldShadeLeads()) { // only draw leads longer than threshold
Stroke leadStroke = ObjectCache.getInstance().fetchBasicStroke(leadThickness - 1); if (leadLength > LEAD_RENDER_THRESHOLD) {
Color leadColor = getLeadColorForPainting(componentState); if (shouldShadeLeads()) {
Stroke leadStroke = ObjectCache.getInstance().fetchBasicStroke(leadThickness - 1);
Area leadArea = new Area(); Color leadColor = getLeadColorForPainting(componentState);
int endX = (int) (getPoints()[0].x + Math.cos(theta) * leadLength); Area leadArea = new Area();
int endY = (int) (getPoints()[0].y + Math.sin(theta) * leadLength);
Line2D line = new Line2D.Double(getPoints()[0].x, getPoints()[0].y, endX, endY); int endX = (int) (getPoints()[0].x + Math.cos(theta) * leadLength);
leadArea.add(new Area(leadStroke.createStrokedShape(line))); int endY = (int) (getPoints()[0].y + Math.sin(theta) * leadLength);
Line2D line = new Line2D.Double(getPoints()[0].x, getPoints()[0].y, endX, endY);
endX = (int) (getPoints()[1].x + Math.cos(theta - Math.PI) * leadLength); leadArea.add(new Area(leadStroke.createStrokedShape(line)));
endY = (int) (getPoints()[1].y + Math.sin(theta - Math.PI) * leadLength);
line = new Line2D.Double(getPoints()[1].x, getPoints()[1].y, endX, endY); endX = (int) (getPoints()[1].x + Math.cos(theta - Math.PI) * leadLength);
leadArea.add(new Area(leadStroke.createStrokedShape(line))); endY = (int) (getPoints()[1].y + Math.sin(theta - Math.PI) * leadLength);
line = new Line2D.Double(getPoints()[1].x, getPoints()[1].y, endX, endY);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f)); leadArea.add(new Area(leadStroke.createStrokedShape(line)));
g2d.setColor(leadColor);
g2d.fill(leadArea); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
g2d.setColor(leadColor.darker()); g2d.setColor(leadColor);
g2d.draw(leadArea); g2d.fill(leadArea);
} else { g2d.setColor(leadColor.darker());
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness)); g2d.draw(leadArea);
Color leadColor = getLeadColorForPainting(componentState); } else {
g2d.setColor(leadColor); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
drawLeads(g2d, theta, leadLength); Color leadColor = getLeadColorForPainting(componentState);
g2d.setColor(leadColor);
drawLeads(g2d, theta, leadLength);
}
} }
// g2d.setTransform(textTransform); // g2d.setTransform(textTransform);
} }
@@ -0,0 +1,103 @@
package org.diylc.editor;
import java.awt.Point;
import java.util.HashSet;
import java.util.Set;
import org.diylc.common.IProjectEditor;
import org.diylc.components.AbstractCurvedComponent.PointCount;
import org.diylc.components.AbstractLeadedComponent;
import org.diylc.components.connectivity.AWG;
import org.diylc.components.connectivity.HookupWire;
import org.diylc.components.semiconductors.AbstractTransistorPackage;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.measures.Size;
import org.diylc.presenter.InstantiationManager;
public class FlexibleLeadsEditor implements IProjectEditor {
private InstantiationManager instantiationManager;
@Override
public Set<IDIYComponent<?>> edit(Project project, Set<IDIYComponent<?>> selection) {
Set<IDIYComponent<?>> newSelection = new HashSet<IDIYComponent<?>>(selection);
for (IDIYComponent<?> c : selection) {
if (c instanceof AbstractLeadedComponent<?>)
addLeads((AbstractLeadedComponent<?>) c, project, newSelection);
}
return newSelection;
}
private void addLeads(AbstractLeadedComponent<?> c, Project project, Set<IDIYComponent<?>> newSelection) {
AbstractLeadedComponent<?> leaded = (AbstractLeadedComponent<?>)c;
Size l = leaded.getLength();
// do not apply to jumpers, etc
if (l == null)
return;
Point p1 = new Point(leaded.getControlPoint(0));
Point p2 = new Point(leaded.getControlPoint(1));
double d = p1.distance(p2);
double len = l.convertToPixels() + 2;
double diff = (d - len) / 2;
if (diff < 20)
return;
// find the center and angle
double centerX = (p1.x + p2.x) / 2.0;
double centerY = (p1.y + p2.y) / 2.0;
double theta = Math.atan2(p2.y - p1.y, p2.x - p1.x);
// shorten the component to match the size of the body
Point newP1 = new Point((int)Math.round(centerX - Math.cos(theta) * len / 2), (int)Math.round(centerY - Math.sin(theta) * len / 2));
Point newP2 = new Point((int)Math.round(centerX + Math.cos(theta) * len / 2), (int)Math.round(centerY + Math.sin(theta) * len / 2));
leaded.setControlPoint(newP1, 0);
leaded.setControlPoint(newP2, 1);
// create leads
HookupWire w1 = new HookupWire();
w1.setGauge(AWG._26);
w1.setLeadColor(leaded.getLeadColor());
w1.setPointCount(PointCount.THREE);
w1.setControlPoint(p1, 0);
w1.setControlPoint(new Point((int)Math.round(centerX - Math.cos(theta) * (len / 2 + diff / 2)), (int)Math.round(centerY - Math.sin(theta) * (len / 2 + diff / 2))), 1);
w1.setControlPoint(newP1, 2);
newSelection.add(w1);
HookupWire w2 = new HookupWire();
w2.setGauge(AWG._26);
w2.setLeadColor(leaded.getLeadColor());
w2.setPointCount(PointCount.THREE);
w2.setControlPoint(p2, 0);
w2.setControlPoint(new Point((int)Math.round(centerX + Math.cos(theta) * (len / 2 + diff / 2)), (int)Math.round(centerY + Math.sin(theta) * (len / 2 + diff / 2))), 1);
w2.setControlPoint(newP2, 2);
newSelection.add(w2);
// inject leads right before the component
int index = project.getComponents().indexOf(c);
project.getComponents().add(index, w1);
project.getComponents().add(index, w2);
}
private void addLeads(AbstractTransistorPackage c, Project project, Set<IDIYComponent<?>> newSelection) {
if (c.getFolded()) {
} else {
}
}
@Override
public String getEditAction() {
return "Add Flexible Leads";
}
public InstantiationManager getInstantiationManager() {
if (instantiationManager == null)
instantiationManager = new InstantiationManager();
return instantiationManager;
}
}
Binary file not shown.
Binary file not shown.
@@ -21,12 +21,12 @@
*/ */
package org.diylc.images; package org.diylc.images;
import java.awt.Image; import java.awt.Image;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
/** /**
@@ -65,7 +65,7 @@ public enum IconLoader {
"splash_electrolytic.png"), SplashFilm("splash_film.png"), Splash("splash.png"), Help("help2.png"), ScrollInformation("scroll_information.png"), "splash_electrolytic.png"), SplashFilm("splash_film.png"), Splash("splash.png"), Help("help2.png"), ScrollInformation("scroll_information.png"),
Node("node.png"), Web("web.png"), Guitar("guitar.png"), Scientist("scientist.png"), StarGrey("star_grey.png"), StarBlue("star_blue.png"), Node("node.png"), Web("web.png"), Guitar("guitar.png"), Scientist("scientist.png"), StarGrey("star_grey.png"), StarBlue("star_blue.png"),
JarBeanInto("jar_bean_into.png"), GraphEdgeDirected("graph_edge_directed.png"), LaserPointer("laserpointer.png"), TapeMeasure("tape_measure1.png"), JarBeanInto("jar_bean_into.png"), GraphEdgeDirected("graph_edge_directed.png"), LaserPointer("laserpointer.png"), TapeMeasure("tape_measure1.png"),
Elements("elements1.png"), GraphNodes("graph_nodes.png"), User("user1.png"); Elements("elements1.png"), GraphNodes("graph_nodes.png"), User("user1.png"), FlexibleLeads("flexible_leads.png"), Pads("pads.png");
protected String name; protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

File diff suppressed because it is too large Load Diff
@@ -103,6 +103,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
private ActionFactory.RotateSelectionAction rotateCounterclockwiseAction; private ActionFactory.RotateSelectionAction rotateCounterclockwiseAction;
private ActionFactory.MirrorSelectionAction mirrorHorizontallyAction; private ActionFactory.MirrorSelectionAction mirrorHorizontallyAction;
private ActionFactory.MirrorSelectionAction mirrorVerticallyAction; private ActionFactory.MirrorSelectionAction mirrorVerticallyAction;
private ActionFactory.FlexibleLeadsAction flexibleLeadsAction;
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
private ISwingUI swingUI; private ISwingUI swingUI;
@@ -289,6 +290,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
getRotateCounterclockwiseAction().setEnabled(enabled); getRotateCounterclockwiseAction().setEnabled(enabled);
getMirrorHorizontallyAction().setEnabled(enabled); getMirrorHorizontallyAction().setEnabled(enabled);
getMirrorVerticallyAction().setEnabled(enabled); getMirrorVerticallyAction().setEnabled(enabled);
getFlexibleLeadsAction().setEnabled(enabled);
getSaveAsTemplateAction() getSaveAsTemplateAction()
.setEnabled(plugInPort.getSelectedComponents().size() == 1); .setEnabled(plugInPort.getSelectedComponents().size() == 1);
@@ -453,6 +455,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
popupMenu.add(getSaveAsBlockAction()); popupMenu.add(getSaveAsBlockAction());
popupMenu.add(getExpandMenu()); popupMenu.add(getExpandMenu());
popupMenu.addSeparator(); popupMenu.addSeparator();
popupMenu.add(getFlexibleLeadsAction());
popupMenu.addSeparator();
popupMenu.add(ActionFactory.getInstance().createEditProjectAction(plugInPort)); popupMenu.add(ActionFactory.getInstance().createEditProjectAction(plugInPort));
} }
return popupMenu; return popupMenu;
@@ -630,6 +634,12 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
} }
return mirrorVerticallyAction; return mirrorVerticallyAction;
} }
public ActionFactory.FlexibleLeadsAction getFlexibleLeadsAction() {
if (flexibleLeadsAction == null)
flexibleLeadsAction = ActionFactory.getInstance().createFlexibleLeadsAction(plugInPort);
return flexibleLeadsAction;
}
public ActionFactory.SaveAsTemplateAction getSaveAsTemplateAction() { public ActionFactory.SaveAsTemplateAction getSaveAsTemplateAction() {
if (saveAsTemplateAction == null) { if (saveAsTemplateAction == null) {
@@ -21,24 +21,24 @@
*/ */
package org.diylc.swing.plugins.edit; package org.diylc.swing.plugins.edit;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.FlavorEvent; import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener; import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.util.EnumSet; import java.util.EnumSet;
import org.diylc.appframework.undo.IUndoListener; import org.diylc.appframework.undo.IUndoListener;
import org.diylc.appframework.undo.UndoHandler; import org.diylc.appframework.undo.UndoHandler;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.IComponentTransformer; import org.diylc.common.IComponentTransformer;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.ExpansionMode; import org.diylc.core.ExpansionMode;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.images.IconLoader; import org.diylc.images.IconLoader;
import org.diylc.swing.ActionFactory; import org.diylc.swing.ActionFactory;
import org.diylc.swing.ISwingUI; import org.diylc.swing.ISwingUI;
public class EditMenuPlugin implements IPlugIn, ClipboardOwner { public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
@@ -75,7 +75,8 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
private ActionFactory.RotateSelectionAction rotateCounterClockwiseAction; private ActionFactory.RotateSelectionAction rotateCounterClockwiseAction;
private ActionFactory.MirrorSelectionAction mirrorHorizontallyAction; private ActionFactory.MirrorSelectionAction mirrorHorizontallyAction;
private ActionFactory.MirrorSelectionAction mirrorVerticallyAction; private ActionFactory.MirrorSelectionAction mirrorVerticallyAction;
private ActionFactory.FindAction findAction; private ActionFactory.FindAction findAction;
private ActionFactory.FlexibleLeadsAction flexibleLeadsAction;
private UndoHandler<Project> undoHandler; private UndoHandler<Project> undoHandler;
@@ -268,6 +269,12 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
if (findAction == null) if (findAction == null)
findAction = ActionFactory.getInstance().createFindAction(plugInPort, swingUI); findAction = ActionFactory.getInstance().createFindAction(plugInPort, swingUI);
return findAction; return findAction;
}
public ActionFactory.FlexibleLeadsAction getFlexibleLeadsAction() {
if (flexibleLeadsAction == null)
flexibleLeadsAction = ActionFactory.getInstance().createFlexibleLeadsAction(plugInPort);
return flexibleLeadsAction;
} }
@Override @Override
@@ -311,6 +318,8 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
swingUI.injectMenuAction(getExpandSelectionAllAction(), EXPAND_TITLE); swingUI.injectMenuAction(getExpandSelectionAllAction(), EXPAND_TITLE);
swingUI.injectMenuAction(getExpandSelectionImmediateAction(), EXPAND_TITLE); swingUI.injectMenuAction(getExpandSelectionImmediateAction(), EXPAND_TITLE);
swingUI.injectMenuAction(getExpandSelectionSameTypeAction(), EXPAND_TITLE); swingUI.injectMenuAction(getExpandSelectionSameTypeAction(), EXPAND_TITLE);
swingUI.injectMenuAction(null, EDIT_TITLE);
swingUI.injectMenuAction(getFlexibleLeadsAction(), EDIT_TITLE);
swingUI.injectMenuAction(null, EDIT_TITLE); swingUI.injectMenuAction(null, EDIT_TITLE);
swingUI.injectMenuAction(actionFactory.createEditProjectAction(plugInPort), EDIT_TITLE); swingUI.injectMenuAction(actionFactory.createEditProjectAction(plugInPort), EDIT_TITLE);
@@ -364,7 +373,8 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
getMirrorHorizontallyAction().setEnabled(enabled); getMirrorHorizontallyAction().setEnabled(enabled);
getMirrorVerticallyAction().setEnabled(enabled); getMirrorVerticallyAction().setEnabled(enabled);
getSaveAsTemplateAction().setEnabled(enabled); getSaveAsTemplateAction().setEnabled(enabled);
getSaveAsBlockAction().setEnabled(enabled); getSaveAsBlockAction().setEnabled(enabled);
getFlexibleLeadsAction().setEnabled(enabled);
} }
// ClipboardOwner // ClipboardOwner