Skip to content

Commit

Permalink
refactored a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
futurelink committed Oct 12, 2022
1 parent dad3aa2 commit f9f3960
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.futurelink.gerber.panelizer.gui;

import io.qt.gui.QColor;
import io.qt.gui.QPen;
import lombok.Getter;

public class ColorSettings {
@Getter
private final QPen axisPen;
@Getter private final QPen drillPen;
@Getter private final QPen outlinePen;
@Getter private final QPen selectedPen;
@Getter private final QPen validFeaturePen;
@Getter private final QPen invalidFeaturePen;
@Getter private final QPen marksPen;

private static ColorSettings instance = null;

public static ColorSettings getInstance() {
if (instance == null) {
instance = new ColorSettings();
}
return instance;
}

private ColorSettings() {
axisPen = new QPen(new QColor(200, 200, 200), 1);
drillPen = new QPen(new QColor(180, 180, 180), 1);
outlinePen = new QPen(new QColor(0, 0, 0, 255), 1);
selectedPen = new QPen(new QColor(0, 0, 200), 2);
validFeaturePen = new QPen(new QColor(0, 200, 0), 1);
invalidFeaturePen = new QPen(new QColor(200, 0, 0), 1);
marksPen = new QPen(new QColor(200, 0, 200), 1);
}
}

50 changes: 15 additions & 35 deletions src/main/java/ru/futurelink/gerber/panelizer/gui/GerberPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,17 @@
public class GerberPainter extends QPainter {
@Getter private final double scale;
@Getter private final QPointF center;
private final Settings settings;
private final ColorSettings colorSettings = ColorSettings.getInstance();
private final static double arcQ = 2880 / Math.PI;

static class Settings {
QPen axisPen;
QPen drillPen;
QPen outlinePen;
QPen selectedPen;
QPen validFeaturePen;
QPen invalidFeaturePen;
QPen marksPen;
Settings() {
axisPen = new QPen(new QColor(200, 200, 200), 1);
drillPen = new QPen(new QColor(180, 180, 180), 1);
outlinePen = new QPen(new QColor(0, 0, 0, 255), 1);
selectedPen = new QPen(new QColor(0, 0, 200), 2);
validFeaturePen = new QPen(new QColor(0, 200, 0), 1);
invalidFeaturePen = new QPen(new QColor(200, 0, 0), 1);
marksPen = new QPen(new QColor(200, 0, 200), 1);
}
}

public GerberPainter(QWidget parent, Settings settings, double scale, QPointF center) {
public GerberPainter(QWidget parent, double scale, QPointF center) {
super(parent);
this.scale = scale;
this.center = center;
this.settings = settings;
}

void drawAxis(QPointF center, int width, int height) {
setPen(settings.axisPen);
public void drawAxis(QPointF center, int width, int height) {
setPen(colorSettings.getAxisPen());
drawLine(
(int) Math.round(center.x() / scale), 0,
(int) Math.round(center.x() / scale), height);
Expand All @@ -60,8 +40,8 @@ void drawAxis(QPointF center, int width, int height) {
width, (int) Math.round(center.y() / scale));
}

void drawBoundingBoxMarks(QRectF box) {
setPen(settings.marksPen);
public void drawBoundingBoxMarks(QRectF box) {
setPen(colorSettings.getMarksPen());

// Bounding box
var topLeft = translatedPoint(box.topLeft(), null);
Expand All @@ -78,7 +58,7 @@ void drawBoundingBoxMarks(QRectF box) {
drawLine((int) cx - 10, (int) bottomRight.y(), (int) cx + 10, (int) bottomRight.y());
}

void drawHoles(QPainter painter, Layer layer, QPointF offset) {
public void drawHoles(QPainter painter, Layer layer, QPointF offset) {
if (layer instanceof Excellon e) {
var hi = e.holes();
while (hi.hasNext()) {
Expand All @@ -90,13 +70,13 @@ void drawHoles(QPainter painter, Layer layer, QPointF offset) {
);
var dia = h.getDiameter() / scale / 2;

painter.setPen(settings.drillPen);
painter.setPen(colorSettings.getDrillPen());
painter.drawEllipse(c, dia, dia);
}
}
}

void drawBatchOutline(QPainter painter, BatchMerger.BatchInstance b, boolean selected) {
public void drawBatchOutline(QPainter painter, BatchMerger.BatchInstance b, boolean selected) {
var rect = new QRectF(
Math.round((b.getTopLeft().getX().doubleValue() + center.x()) / scale),
-Math.round((b.getTopLeft().getY().doubleValue() - center.y()) / scale),
Expand All @@ -106,7 +86,7 @@ void drawBatchOutline(QPainter painter, BatchMerger.BatchInstance b, boolean sel

// Board batch title
// -----------------
painter.setPen(selected ? settings.selectedPen : settings.outlinePen);
painter.setPen(selected ? colorSettings.getSelectedPen() : colorSettings.getOutlinePen());
painter.drawText(
new QPointF(
rect.x() + rect.width() / 2 - (float) textRect.width() / 2,
Expand All @@ -124,12 +104,12 @@ void drawBatchOutline(QPainter painter, BatchMerger.BatchInstance b, boolean sel
// -------------
var outlineLayer = b.getBatch().getLayer(Layer.Type.EdgeCuts);
if (outlineLayer instanceof Gerber g) {
painter.setPen(selected ? settings.selectedPen : new QPen(new QColor(0, 0, 0, 0), 1));
painter.setPen(selected ? colorSettings.getSelectedPen() : new QPen(new QColor(0, 0, 0, 0), 1));
drawGerber(g, new QPointF(b.getOffset().getX().doubleValue(), b.getOffset().getY().doubleValue()));
}
}

void drawGerber(Gerber g, QPointF offset) {
public void drawGerber(Gerber g, QPointF offset) {
var currentInterpolation = Geometry.Interpolation.LINEAR;
var currentPoint = new QPointF(0, 0);
for (var cmd : g.getContents()) {
Expand Down Expand Up @@ -172,14 +152,14 @@ void drawGerber(Gerber g, QPointF offset) {
}
}

void drawFeature(QPainter painter, Feature f, boolean selected) {
public void drawFeature(QPainter painter, Feature f, boolean selected) {
if (f instanceof RoundFeature m) {
var dia = (int) Math.round(m.getRadius() / scale);
var c = translatedPoint(m.getCenter().getX().doubleValue(), m.getCenter().getY().doubleValue());

painter.setPen(selected ?
settings.selectedPen :
m.isValid() ? settings.validFeaturePen : settings.invalidFeaturePen
colorSettings.getSelectedPen() :
m.isValid() ? colorSettings.getValidFeaturePen() : colorSettings.getInvalidFeaturePen()
);

// Draw feature sign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import lombok.Getter;
import ru.futurelink.gerber.panelizer.batch.BatchMerger;
import ru.futurelink.gerber.panelizer.batch.BatchSettings;
import ru.futurelink.gerber.panelizer.gui.widgets.ExportSettingsDialog;
import ru.futurelink.gerber.panelizer.gui.widgets.MergerPanelWidget;
import ru.futurelink.gerber.panelizer.gui.widgets.ProjectManagerWidget;

public class MainWindow extends QMainWindow {
private final static String appName = "Gerber Panelizer";
@Getter private final QMenuBar menuBar;
@Getter private final MergerPanel workArea;
@Getter private final MergerPanelWidget workArea;

private final QLabel statusCoords;
private final QLabel statusSize;
Expand All @@ -27,7 +30,7 @@ public MainWindow() {

batchSettings = BatchSettings.getInstance();

workArea = new MergerPanel(this, new BatchMerger("merged"));
workArea = new MergerPanelWidget(this, new BatchMerger("merged"));
setCentralWidget(workArea);

statusBar = new QStatusBar(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MergerProject extends QObject {
@Getter private final HashMap<UUID, BatchPlacement> batchPlacements;
@Getter private final HashMap<UUID, FeaturePlacement> featurePlacements;

static class BatchPlacement {
public static class BatchPlacement {
@Getter UUID batchUUID;
@Getter double x;
@Getter double y;
Expand All @@ -37,13 +37,13 @@ public BatchPlacement(UUID batchUUID, double x, double y) {
this.y = y;
}

void move(double x, double y) {
public void move(double x, double y) {
this.x = x;
this.y = y;
}
}

static class FeaturePlacement {
public static class FeaturePlacement {
@Getter Class<? extends Feature> featureClass;
@Getter double x;
@Getter double y;
Expand All @@ -53,7 +53,7 @@ public FeaturePlacement(Class<? extends Feature> cls, double x, double y) {
this.y = y;
}

void move(double x, double y) {
public void move(double x, double y) {
this.x = x;
this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ru.futurelink.gerber.panelizer.gui;
package ru.futurelink.gerber.panelizer.gui.widgets;

import io.qt.core.Qt;
import io.qt.gui.QBrush;
import io.qt.gui.QColor;
import io.qt.widgets.*;
import lombok.Getter;
import ru.futurelink.gerber.panelizer.Layer;
import ru.futurelink.gerber.panelizer.batch.BatchSettings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.futurelink.gerber.panelizer.gui;
package ru.futurelink.gerber.panelizer.gui.widgets;

import io.qt.core.*;
import io.qt.gui.*;
Expand All @@ -13,10 +13,13 @@
import ru.futurelink.gerber.panelizer.canvas.fetaures.MouseBites;
import ru.futurelink.gerber.panelizer.exceptions.MergerException;
import ru.futurelink.gerber.panelizer.gbr.Gerber;
import ru.futurelink.gerber.panelizer.gui.ColorSettings;
import ru.futurelink.gerber.panelizer.gui.GerberPainter;
import ru.futurelink.gerber.panelizer.gui.Utils;

import java.util.UUID;

public class MergerPanel extends QWidget {
public class MergerPanelWidget extends QWidget {

@Getter private final BatchMerger merger;
private double scale;
Expand All @@ -26,7 +29,6 @@ public class MergerPanel extends QWidget {
private QPoint mousePressPoint;
private Object instanceUnderMouse;
private Object instanceSelected;
private final GerberPainter.Settings painterSettings;
public final Signal1<QPointF> mouseMoved = new Signal1<>();
public final Signal1<Object> deleteItem = new Signal1<>();
public final Signal1<Object> moveItem = new Signal1<>();
Expand All @@ -37,14 +39,13 @@ public class MergerPanel extends QWidget {
private final QAction addFeatureAction;
private final QAction deleteAction;

public MergerPanel(QWidget parent, BatchMerger m) {
public MergerPanelWidget(QWidget parent, BatchMerger m) {
super(parent);
merger = m;
scale = 0.25;
margin = 0;
center = new QPointF(10, 10);
mousePressPoint = null;
painterSettings = new GerberPainter.Settings();

addFeatureAction = new QAction("Add MouseBites feature");
addFeatureAction.triggered.connect(this, "addMouseBites(boolean)");
Expand Down Expand Up @@ -273,7 +274,7 @@ protected void wheelEvent(QWheelEvent event) {

@Override
protected void paintEvent(QPaintEvent event) {
var painter = new GerberPainter(this, painterSettings, scale, center);
var painter = new GerberPainter(this, scale, center);
painter.setBackground(new QBrush(new QColor(40, 40, 40)));
painter.setRenderHint(QPainter.RenderHint.Antialiasing);

Expand Down Expand Up @@ -338,7 +339,7 @@ private boolean isInCircle(QPointF p, Point center, double radius) {
Math.pow(p.y() - center.getY().doubleValue(), 2) < radius * radius);
}

void mergeDisplayLayers() {
public void mergeDisplayLayers() {
try {
merger.mergeLayer(Layer.Type.EdgeCuts);
merger.mergeLayer(Layer.Type.FrontMask);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.futurelink.gerber.panelizer.gui;
package ru.futurelink.gerber.panelizer.gui.widgets;

import io.qt.core.QFileInfo;
import io.qt.core.QStringList;
Expand All @@ -12,6 +12,7 @@
import ru.futurelink.gerber.panelizer.canvas.fetaures.RoundFeature;
import ru.futurelink.gerber.panelizer.exceptions.GerberException;
import ru.futurelink.gerber.panelizer.exceptions.MergerException;
import ru.futurelink.gerber.panelizer.gui.MergerProject;

import java.io.File;
import java.io.IOException;
Expand All @@ -21,13 +22,14 @@
public class ProjectManagerWidget extends QDockWidget {
private final QTreeWidget projectTree;
@Getter private MergerProject project;
@Getter MergerPanel workArea;
@Getter
MergerPanelWidget workArea;
@Getter private boolean modified;
@Getter private String filename;

public final Signal1<String> projectNameChanged = new Signal1<>();

public ProjectManagerWidget(QWidget parent, MergerPanel workArea) {
public ProjectManagerWidget(QWidget parent, MergerPanelWidget workArea) {
super(parent);
setFeatures(DockWidgetFeature.NoDockWidgetFeatures);
setWindowTitle("Project structure");
Expand Down Expand Up @@ -158,15 +160,15 @@ private void moveItem(Object obj) {
refresh();
}

final void newProject() {
public final void newProject() {
filename = null;
project = new MergerProject(this);
setProject(project);
workArea.clear();
projectNameChanged.emit(getProjectName());
}

final void openProject() {
public final void openProject() {
try {
var dlg = new QFileDialog(this);
dlg.setWindowTitle("Open project");
Expand Down Expand Up @@ -210,7 +212,7 @@ final void openProject() {
}
}

final void saveProject() {
public final void saveProject() {
var settings = BatchSettings.getInstance();
try {
if (filename != null) {
Expand All @@ -233,7 +235,7 @@ final void saveProject() {
}
}

final void saveProjectAs() {
public final void saveProjectAs() {
var settings = BatchSettings.getInstance();
var dlg = new QFileDialog(this);
dlg.setWindowTitle("Save project as...");
Expand All @@ -252,7 +254,7 @@ final void saveProjectAs() {
}
}

final void addBatch() {
public final void addBatch() {
try {
var dlg = new QFileDialog(this);
dlg.setWindowTitle("Add batch ZIP...");
Expand All @@ -268,7 +270,7 @@ final void addBatch() {
}
}

void export() {
public void export() {
var settings = BatchSettings.getInstance();
var dlg = new QFileDialog(this);
dlg.setWindowTitle("Export merged batch ZIP...");
Expand Down

0 comments on commit f9f3960

Please sign in to comment.