Skip to content

Commit 41e60b6

Browse files
IrPlotter: improved mouse-based zooming. Resolves #361.
Minor tweaks in IrPlotter.
1 parent ed76718 commit 41e60b6

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/main/java/org/harctoolbox/guicomponents/IrPlotter.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.awt.Dimension;
2222
import java.awt.Graphics;
2323
import java.awt.event.MouseEvent;
24+
import java.awt.event.MouseWheelEvent;
2425
import java.io.IOException;
2526
import javax.swing.JMenu;
2627
import javax.swing.JMenuItem;
@@ -68,6 +69,10 @@ public class IrPlotter extends HarcPanel {
6869
private final static Color endingColor = Color.GREEN;
6970
private final static int invalid = -1;
7071

72+
private final static int RESET_MOUSE_BUTTON = 6;
73+
private final static double BASIS = 1.05;
74+
private final static double LOGBASIS = Math.log(BASIS);
75+
7176
/** max horizontal coordinate, in microseconds */
7277
private int xmin = 0;
7378
private int xmax = 200000;
@@ -193,7 +198,8 @@ private void setDragBegin(int x) {
193198

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

399405
@Override
400406
public void mouseReleased(java.awt.event.MouseEvent evt) {
401-
if (evt.getButton() == MouseEvent.BUTTON1)
402-
setDragEnd(evt.getX());
403-
else if (evt.getButton() == MouseEvent.BUTTON3)
404-
if (evt.isPopupTrigger())
405-
plotterPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
407+
switch (evt.getButton()) {
408+
case MouseEvent.BUTTON1:
409+
setDragEnd(evt.getX());
410+
break;
411+
case MouseEvent.BUTTON3:
412+
if (evt.isPopupTrigger())
413+
plotterPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
414+
break;
415+
case RESET_MOUSE_BUTTON:
416+
reset();
417+
break;
418+
default:
419+
; // nothing
420+
}
406421
}
407422
});
408423

424+
addMouseWheelListener((MouseWheelEvent evt) -> {
425+
int screenX = evt.getX();
426+
int myX = screenX2x(screenX);
427+
double length = xmax - xmin;
428+
double frac = (myX - xmin) / length;
429+
double noClicks = evt.getPreciseWheelRotation();
430+
double factor = Math.exp(noClicks * LOGBASIS);
431+
double toReduce = (factor - 1.0) * length;
432+
double left = toReduce * frac;
433+
double right = toReduce * (1.0 - frac);
434+
xmin += (int) left;
435+
xmax -= (int) right;
436+
repaint();
437+
});
438+
409439
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
410440
@Override
411441
public void mouseDragged(java.awt.event.MouseEvent evt) {

src/main/java/org/harctoolbox/irscrutinizer/GuiMain.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2904,7 +2904,7 @@
29042904
<BevelBorder bevelType="1"/>
29052905
</Border>
29062906
</Property>
2907-
<Property name="toolTipText" type="java.lang.String" value="Plot of captured IR signal. Press right mouse button for a menu."/>
2907+
<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."/>
29082908
<Property name="autoscrolls" type="boolean" value="true"/>
29092909
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
29102910
<Dimension value="[749, 100]"/>

src/main/java/org/harctoolbox/irscrutinizer/GuiMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3716,7 +3716,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
37163716
plotScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
37173717

37183718
irPlotter.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
3719-
irPlotter.setToolTipText("Plot of captured IR signal. Press right mouse button for a menu.");
3719+
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.");
37203720
irPlotter.setAutoscrolls(true);
37213721
irPlotter.setPreferredSize(new java.awt.Dimension(749, 100));
37223722

0 commit comments

Comments
 (0)