From 4d2e74b8510ec4b8f85d1610a9f69e8cfee24385 Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Tue, 5 Mar 2019 18:22:37 +0100 Subject: [PATCH] Individual camera view state persistence --- plugins/ScreenPanel4.java | 285 +++++++++++++++++++++++++------------- 1 file changed, 192 insertions(+), 93 deletions(-) diff --git a/plugins/ScreenPanel4.java b/plugins/ScreenPanel4.java index 9fe7928..0ff161e 100644 --- a/plugins/ScreenPanel4.java +++ b/plugins/ScreenPanel4.java @@ -50,6 +50,7 @@ import ch.psi.pshell.swing.ValueSelection.ValueSelectionListener; import ch.psi.pshell.ui.Console; import ch.psi.utils.Arr; import ch.psi.utils.ArrayProperties; +import ch.psi.utils.Config; import ch.psi.utils.Convert; import ch.psi.utils.Str; import ch.psi.utils.swing.Editor.EditorDialog; @@ -150,6 +151,90 @@ public class ScreenPanel4 extends Panel { String instanceName; Overlay titleOv = null; int integration = 0; + boolean persistCameraState; + + public class CameraState extends Config { + + public boolean valid; + public boolean showSidePanel; + //public boolean showMarker; Saved in the stream instance config + public boolean showProfile; + public boolean showFit; + public boolean showReticle; + public boolean showScale; + public boolean showTitle; + public double zoom; + public RendererMode mode; + public boolean colormapAutomatic; + public double colormapMin; + public double colormapMax; + public Colormap colormap; + public boolean colormapLogarithmic; + } + + void loadCameraState() { + if (persistCameraState) { + try { + CameraState state = new CameraState(); + state.load(getCameraConfigFile()); + if (state.valid) { + buttonSidePanel.setSelected(state.showSidePanel); + buttonSidePanelActionPerformed(null); + buttonProfile.setSelected(state.showProfile); + buttonProfileActionPerformed(null); + buttonFit.setSelected(state.showFit); + buttonFitActionPerformed(null); + buttonReticle.setSelected(state.showReticle); + buttonReticleActionPerformed(null); + buttonScale.setSelected(state.showScale); + buttonScaleActionPerformed(null); + buttonTitle.setSelected(state.showTitle); + buttonTitleActionPerformed(null); + renderer.setMode(state.mode); + renderer.setZoom(state.zoom); + if (camera instanceof ColormapSource) { + camera.getConfig().colormap = state.colormap; + camera.getConfig().colormapAutomatic = state.colormapAutomatic; + camera.getConfig().colormapLogarithmic = state.colormapLogarithmic; + camera.getConfig().colormapMax = state.colormapMax; + camera.getConfig().colormapMin = state.colormapMin; + updateColormap(); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + + void saveCameraState() { + if (persistCameraState) { + try { + CameraState state = new CameraState(); + state.valid = true; + if (camera instanceof ColormapSource) { + state.colormap = camera.getConfig().colormap; + state.colormapAutomatic = camera.getConfig().colormapAutomatic; + state.colormapLogarithmic = camera.getConfig().colormapLogarithmic; + state.colormapMax = camera.getConfig().colormapMax; + state.colormapMin = camera.getConfig().colormapMin; + } + state.mode = renderer.getMode(); + state.zoom = renderer.getZoom(); + + state.showSidePanel = buttonSidePanel.isSelected(); + state.showProfile = buttonProfile.isSelected(); + state.showFit = buttonFit.isSelected(); + state.showReticle = buttonReticle.isSelected(); + state.showScale = buttonScale.isSelected(); + state.showTitle = buttonTitle.isSelected(); + + state.save(getCameraConfigFile()); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } String pipelineSuffix = "_sp"; @@ -316,6 +401,11 @@ public class ScreenPanel4 extends Panel { if (App.hasArgument("calc")) { useServerStats = false; } + + if (App.hasArgument("persist")) { + persistCameraState = true; + } + if (App.hasArgument("suffix")) { pipelineSuffix = App.getArgumentValue("suffix"); } @@ -554,14 +644,14 @@ public class ScreenPanel4 extends Panel { }); if (MainFrame.isDark()) { textState.setDisabledTextColor(textState.getForeground()); - } + } } catch (Exception ex) { ex.printStackTrace(); } - - console = (!App.hasArgument("console")) ? null : new Console() { - /* + + console = (!App.hasArgument("console")) ? null : new Console() { + /* protected void onConsoleCommand(String name, String[] pars, String trimming) throws Exception { switch (name) { case "cam": @@ -569,29 +659,29 @@ public class ScreenPanel4 extends Panel { break; } }*/ - @Override - protected void onConsoleCommand(String command) { - String[] tokens = command.split(" "); - if ((tokens.length > 1) && tokens[0].equals("cam")){ - try{ - if (!tokens[1].equals(comboCameras.getSelectedItem())){ + @Override + protected void onConsoleCommand(String command) { + String[] tokens = command.split(" "); + if ((tokens.length > 1) && tokens[0].equals("cam")) { + try { + if (!tokens[1].equals(comboCameras.getSelectedItem())) { setComboTypeSelection("All"); updateCameraList(); comboCameras.setSelectedItem(tokens[1]); - if (!tokens[1].equals(comboCameras.getSelectedItem())){ + if (!tokens[1].equals(comboCameras.getSelectedItem())) { comboCameras.setSelectedItem(""); throw new Exception("Invalid camera name : " + tokens[1]); } System.out.println("Console set camera: " + tokens[1]); - } - } catch (Exception ex){ - System.err.println(ex); - } - } else { - System.err.println("Invalid command: " + command); - } - } - }; + } + } catch (Exception ex) { + System.err.println(ex); + } + } else { + System.err.println("Invalid command: " + command); + } + } + }; } void setIntegration(int frames) { @@ -633,6 +723,7 @@ public class ScreenPanel4 extends Panel { public void onStop() { try { if (camera != null) { + saveCameraState(); camera.close(); camera = null; server = null; @@ -647,10 +738,17 @@ public class ScreenPanel4 extends Panel { } } catch (Exception ex) { ex.printStackTrace(); - } + } super.onStop(); } + String getCameraConfigFile() { + if (camera == null) { + return null; + } + return getContext().getSetup().expandPath("{context}/screen_panel/" + camera.getName() + ".properties"); + } + //Overridable callbacks @Override public void onInitialize(int runCount) { @@ -810,12 +908,11 @@ public class ScreenPanel4 extends Panel { userOv = fo; } } - - - void updateDialogTitle(){ + + void updateDialogTitle() { if (App.isDetached()) { getTopLevel().setTitle(cameraName == null ? "ScreenPanel" : cameraName); - } + } } void manageTitleOverlay() { @@ -855,7 +952,7 @@ public class ScreenPanel4 extends Panel { errorOverlay = null; lastMarkerPos = null; lastFrame = null; - lastPipelinePars = null; + lastPipelinePars = null; if (dataTableDialog != null) { dataTableDialog.dispose(); @@ -870,6 +967,7 @@ public class ScreenPanel4 extends Panel { boolean was_server = false; if (camera != null) { + saveCameraState(); //camera.removeAllListeners(); was_server = (server != null); camera.close(); @@ -945,6 +1043,7 @@ public class ScreenPanel4 extends Panel { camera.initialize(); camera.assertInitialized(); System.out.println("Camera initialization OK"); + loadCameraState(); if (server != null) { //server.start(cameraName, false); String pipelineName = cameraName + pipelineSuffix; @@ -1123,7 +1222,7 @@ public class ScreenPanel4 extends Panel { @Override public void onImage(Object o, BufferedImage bi, Data data) { - try{ + try { if (continuous) { buffer.add(data); if (buffer.size() >= numImages) { @@ -1135,7 +1234,7 @@ public class ScreenPanel4 extends Panel { buffer.add(null); //Just to count process(data); } - } catch (Exception ex){ + } catch (Exception ex) { buffer.clear(); integration = null; ImageIntegrator.this.pushData(null); @@ -1171,17 +1270,17 @@ public class ScreenPanel4 extends Panel { } } - + boolean bufferFull = true; - - void setBufferFull(boolean value){ - if (value != bufferFull){ - SwingUtilities.invokeLater(()->{ + + void setBufferFull(boolean value) { + if (value != bufferFull) { + SwingUtilities.invokeLater(() -> { buttonPause.setBackground(value ? buttonSave.getBackground() : buttonSave.getBackground().brighter()); }); bufferFull = value; } - } + } volatile Dimension imageSize; @@ -1197,12 +1296,11 @@ public class ScreenPanel4 extends Panel { renderer.refresh(); } - void checkMarker(Point p) throws IOException { if (camera != null) { if (buttonMarker.isSelected()) { Dimension d = renderer.getImageSize(); - if (p==null){ + if (p == null) { p = (d == null) ? new Point(renderer.getWidth() / 2, renderer.getHeight() / 2) : new Point(d.width / 2, d.height / 2); } Overlay ov = null; @@ -1218,6 +1316,7 @@ public class ScreenPanel4 extends Panel { } Point lastMarkerPos; + void onMarkerChanged() throws IOException { lastMarkerPos = getStreamMarkerPos(); if (marker == null) { @@ -1231,10 +1330,10 @@ public class ScreenPanel4 extends Panel { try { if (server != null) { Point p = getStreamMarkerPos(); - if (p != null) { + if (p != null) { //To prevent a local change being overriden by a message having the old settings. //TODO: This is not bullet-proof, as one can have 2 changes between 2 frames... - if (!p.equals(lastMarkerPos)){ + if (!p.equals(lastMarkerPos)) { if (p.x == Integer.MIN_VALUE) { if (buttonMarker.isSelected()) { buttonMarker.setSelected(false); @@ -1245,7 +1344,7 @@ public class ScreenPanel4 extends Panel { buttonMarker.setSelected(true); checkMarker(p); } else { - if (!p.equals(marker.getPosition())){ + if (!p.equals(marker.getPosition())) { marker.setPosition(p); } } @@ -1257,20 +1356,21 @@ public class ScreenPanel4 extends Panel { ex.printStackTrace(); } } - - Point getStreamMarkerPos() throws IOException{ + + Point getStreamMarkerPos() throws IOException { //System.out.println(server.getInstanceConfig().get("Marker")); - Map pars = server.getProcessingParameters(); - if (pars != null) { - List markerPosition = (List) pars.get("Marker"); - if (markerPosition != null) { - return new Point((Integer)markerPosition.get(0), (Integer)markerPosition.get(1)); - } - return new Point(Integer.MIN_VALUE,Integer.MIN_VALUE); + Map pars = server.getProcessingParameters(); + if (pars != null) { + List markerPosition = (List) pars.get("Marker"); + if (markerPosition != null) { + return new Point((Integer) markerPosition.get(0), (Integer) markerPosition.get(1)); + } + return new Point(Integer.MIN_VALUE, Integer.MIN_VALUE); } return null; } - void clearMarker(){ + + void clearMarker() { marker = null; renderer.setMarker(marker); } @@ -1322,7 +1422,7 @@ public class ScreenPanel4 extends Panel { } else { buttonManual.setSelected(true); } - btFixColormapRange.setVisible(buttonAutomatic.isSelected()); + btFixColormapRange.setVisible(buttonAutomatic.isSelected()); spinnerMin.setEnabled(buttonManual.isSelected()); spinnerMax.setEnabled(buttonManual.isSelected()); if (!Double.isNaN(config.colormapMin)) { @@ -1338,21 +1438,21 @@ public class ScreenPanel4 extends Panel { updatingColormap = false; } - void updatePipelineProperties(){ - goodRegion = checkGoodRegion.isSelected(); - spinnerThreshold.setVisible(checkThreshold.isSelected()); - setGoodRegionOptionsVisible(goodRegion); - slicing = goodRegion && checkSlicing.isSelected(); - setSlicingOptionsVisible(slicing); - updatingServerControls = false; + void updatePipelineProperties() { + goodRegion = checkGoodRegion.isSelected(); + spinnerThreshold.setVisible(checkThreshold.isSelected()); + setGoodRegionOptionsVisible(goodRegion); + slicing = goodRegion && checkSlicing.isSelected(); + setSlicingOptionsVisible(slicing); + updatingServerControls = false; } - + boolean updatingServerControls; void updatePipelineControls() { if (server != null) { updatingServerControls = true; - if (server.isStarted()){ + if (server.isStarted()) { try { checkBackground.setSelected(server.getBackgroundSubtraction()); Double threshold = (server.getThreshold()); @@ -1378,30 +1478,30 @@ public class ScreenPanel4 extends Panel { updatePipelineProperties(); } } - - boolean changedPipelinePars(Map pars1, Map pars2) { - String[] keys = new String[]{"image_background_enable", "image_threshold", "image_good_region", + + boolean changedPipelinePars(Map pars1, Map pars2) { + String[] keys = new String[]{"image_background_enable", "image_threshold", "image_good_region", "threshold", "gfscale", "image_slices", "number_of_slices", "scale", "orientation"}; - for (String key:keys){ + for (String key : keys) { Object o1 = pars1.get(key); Object o2 = pars2.get(key); - if (o1==null){ - if (o2!=null){ + if (o1 == null) { + if (o2 != null) { return true; } - } else if (!o1.equals(o2)){ + } else if (!o1.equals(o2)) { return true; - } + } } return false; } - - void updatePipelineControls(Map pars) { - if (pars!=null){ - updatingServerControls = true; - try { - boolean background = (boolean) pars.get("image_background_enable"); - checkBackground.setSelected(background); + + void updatePipelineControls(Map pars) { + if (pars != null) { + updatingServerControls = true; + try { + boolean background = (boolean) pars.get("image_background_enable"); + checkBackground.setSelected(background); Double threshold = (Double) (pars.get("image_threshold")); checkThreshold.setSelected(threshold != null); spinnerThreshold.setValue((threshold == null) ? 0 : threshold); @@ -1428,7 +1528,7 @@ public class ScreenPanel4 extends Panel { } updatePipelineProperties(); } - } + } void setGoodRegionOptionsVisible(boolean visible) { spinnerGrThreshold.setVisible(visible); @@ -1467,7 +1567,7 @@ public class ScreenPanel4 extends Panel { buttonFit.setEnabled(active); buttonReticle.setEnabled(active && camera.getConfig().isCalibrated()); buttonStreamData.setEnabled(active && (server != null)); - buttonPause.setEnabled(active); + buttonPause.setEnabled(active); if (renderer.isPaused() != buttonPause.isSelected()) { buttonPause.setSelected(renderer.isPaused()); @@ -1485,10 +1585,9 @@ public class ScreenPanel4 extends Panel { updatingButtons = false; } } - - Frame lastFrame = null; - Map lastPipelinePars = null; + Frame lastFrame = null; + Map lastPipelinePars = null; @Override protected void onTimer() { @@ -1513,22 +1612,22 @@ public class ScreenPanel4 extends Panel { updateButtons(); checkHistogram.setSelected((histogramDialog != null) && (histogramDialog.isShowing())); buttonScale.setSelected(renderer.getShowColormapScale()); - try{ + try { Frame frame = getCurrentFrame(); - if (frame!=lastFrame){ + if (frame != lastFrame) { lastFrame = frame; - if (frame!=null){ + if (frame != null) { Map pars = getProcessingParameters(frame.cache); - if((lastPipelinePars==null) || changedPipelinePars(pars, lastPipelinePars)){ + if ((lastPipelinePars == null) || changedPipelinePars(pars, lastPipelinePars)) { //System.out.println("Update pipeline: " + pars); lastPipelinePars = pars; updatePipelineControls(pars); } } } - } catch (Exception ex){ + } catch (Exception ex) { ex.printStackTrace(); - } + } } Pen penFit = new Pen(new Color(192, 105, 0), 0); @@ -1575,7 +1674,7 @@ public class ScreenPanel4 extends Panel { Double xMean = null, xSigma = null, xNorm = null, xCom = null, xRms = null; Double yMean = null, ySigma = null, yNorm = null, yCom = null, yRms = null; double[] pX = null, pY = null, gX = null, gY = null; - PointDouble[] sliceCenters = null; + PointDouble[] sliceCenters = null; if (data != null) { int height = data.getHeight(); int width = data.getWidth(); @@ -2246,8 +2345,8 @@ public class ScreenPanel4 extends Panel { message.append("Data file: ").append(getContext().getExecutionPars().getPath()).append("\n"); message.append("Comment: ").append(textComment.getText()).append("\n"); //Add slicing message - if ((fitOv != null) && (fitOv.length > 5) && (fitOv[fitOv.length-1] instanceof Overlays.Text)) { - Overlays.Text text = (Overlays.Text) fitOv[fitOv.length-1]; + if ((fitOv != null) && (fitOv.length > 5) && (fitOv[fitOv.length - 1] instanceof Overlays.Text)) { + Overlays.Text text = (Overlays.Text) fitOv[fitOv.length - 1]; message.append(text.getText()).append("\n"); } elog((String) comboLogbook.getSelectedItem(), "ScreenPanel Snapshot", message.toString(), new String[]{snapshotFile}); @@ -2416,7 +2515,7 @@ public class ScreenPanel4 extends Panel { SwingUtils.centerComponent(getTopLevel(), dataTableDialog); dataTableDialog.requestFocus(); return; - } + } dataTableModel = new DefaultTableModel(new Object[0][2], new String[]{"Name", "Value"}) { public Class getColumnClass(int columnIndex) { return String.class; @@ -3609,7 +3708,7 @@ public class ScreenPanel4 extends Panel { ChannelInteger setpoint = null; try { int index = comboScreen.getSelectedIndex(); - if (index>=0){ + if (index >= 0) { if (cameraName.contains("DSRM")) { setpoint = new ChannelInteger(null, cameraName + ":POSITION_SP"); } else { @@ -3617,7 +3716,7 @@ public class ScreenPanel4 extends Panel { } setpoint.initialize(); Integer readback = setpoint.read(); - if ((readback==null) || (setpoint.read() != index)) { + if ((readback == null) || (setpoint.read() != index)) { setpoint.write(index); //Must be threaded to control the laser because of sleep in setLaserState /* @@ -3651,7 +3750,7 @@ public class ScreenPanel4 extends Panel { private void comboFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboFilterActionPerformed try { String setpoint = (String) comboFilter.getSelectedItem(); - if (setpoint!=null){ + if (setpoint != null) { if (!setpoint.equals(filter.read())) { filter.write(setpoint); } @@ -4017,7 +4116,7 @@ public class ScreenPanel4 extends Panel { renderer.setShowColormapScale(buttonScale.isSelected()); } catch (Exception ex) { showException(ex); - } + } }//GEN-LAST:event_buttonScaleActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables