Skip to content

Commit

Permalink
IrPlotter: improved mouse-based zooming. Resolves #361.
Browse files Browse the repository at this point in the history
Minor tweaks in IrPlotter.
  • Loading branch information
bengtmartensson committed Jan 6, 2024
1 parent ed76718 commit 41e60b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
44 changes: 37 additions & 7 deletions src/main/java/org/harctoolbox/guicomponents/IrPlotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.IOException;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
Expand Down Expand Up @@ -68,6 +69,10 @@ public class IrPlotter extends HarcPanel {
private final static Color endingColor = Color.GREEN;
private final static int invalid = -1;

private final static int RESET_MOUSE_BUTTON = 6;
private final static double BASIS = 1.05;
private final static double LOGBASIS = Math.log(BASIS);

/** max horizontal coordinate, in microseconds */
private int xmin = 0;
private int xmax = 200000;
Expand Down Expand Up @@ -193,7 +198,8 @@ private void setDragBegin(int x) {

private void setDragEnd(int x) {
if (dragBeginX != invalid && Math.abs(dragBeginX - x) > dragThreshold) {
xmin = screenX2x(Math.min(dragBeginX, x));
// Nothing interesting is going on for negative x's, so force xmin non-negative
xmin = Math.max(screenX2x(Math.min(dragBeginX, x)), 0);
xmax = screenX2x(Math.max(dragBeginX, x));
//System.err.println("End: " + x + " " + screenX2x(x));
}
Expand Down Expand Up @@ -329,7 +335,7 @@ private int[] getTickValues(int xmin, int xmax, int pixelWidth) {
int tickWidth = rounder((int)Math.round(goalWidth));
useMilliSeconds = tickWidth >= 5000;
int newXmin = xmin/tickWidth*tickWidth;
this.xmin = newXmin;
//this.xmin = newXmin;
int noTicks = (int) Math.ceil(((double)(xmax-newXmin))/tickWidth) + 1;
if (noTicks <= 0) {
System.err.println(">>>>>>>>>>>>>>" + xmax + " " + newXmin + " " + tickWidth + " " + goalWidth);
Expand Down Expand Up @@ -398,14 +404,38 @@ else if (evt.getButton() == MouseEvent.BUTTON3)

@Override
public void mouseReleased(java.awt.event.MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1)
setDragEnd(evt.getX());
else if (evt.getButton() == MouseEvent.BUTTON3)
if (evt.isPopupTrigger())
plotterPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
switch (evt.getButton()) {
case MouseEvent.BUTTON1:
setDragEnd(evt.getX());
break;
case MouseEvent.BUTTON3:
if (evt.isPopupTrigger())
plotterPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
break;
case RESET_MOUSE_BUTTON:
reset();
break;
default:
; // nothing
}
}
});

addMouseWheelListener((MouseWheelEvent evt) -> {
int screenX = evt.getX();
int myX = screenX2x(screenX);
double length = xmax - xmin;
double frac = (myX - xmin) / length;
double noClicks = evt.getPreciseWheelRotation();
double factor = Math.exp(noClicks * LOGBASIS);
double toReduce = (factor - 1.0) * length;
double left = toReduce * frac;
double right = toReduce * (1.0 - frac);
xmin += (int) left;
xmax -= (int) right;
repaint();
});

addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/harctoolbox/irscrutinizer/GuiMain.form
Original file line number Diff line number Diff line change
Expand Up @@ -2904,7 +2904,7 @@
<BevelBorder bevelType="1"/>
</Border>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Plot of captured IR signal. Press right mouse button for a menu."/>
<Property name="toolTipText" type="java.lang.String" value="Plot of IR signal above. Press right mouse button for a menu, mouse wheel to zoom, left mouse buttol + drag to zoom selection."/>
<Property name="autoscrolls" type="boolean" value="true"/>
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[749, 100]"/>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/harctoolbox/irscrutinizer/GuiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3716,7 +3716,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
plotScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

irPlotter.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
irPlotter.setToolTipText("Plot of captured IR signal. Press right mouse button for a menu.");
irPlotter.setToolTipText("Plot of IR signal above. Press right mouse button for a menu, mouse wheel to zoom, left mouse buttol + drag to zoom selection.");
irPlotter.setAutoscrolls(true);
irPlotter.setPreferredSize(new java.awt.Dimension(749, 100));

Expand Down

0 comments on commit 41e60b6

Please sign in to comment.