From 20934a9f47388248babfd7842ad81d789a013826 Mon Sep 17 00:00:00 2001 From: Eyck Jentzsch Date: Fri, 7 Jan 2022 19:42:27 +0100 Subject: [PATCH] fix behavior of ZoomBar used by WaveformCanvas --- .../ui/swt/internal/WaveformCanvas.java | 50 +++++++++---------- .../ui/swt/internal/WaveformView.java | 8 +-- .../ui/swt/internal/slider/RangeSlider.java | 40 ++++----------- .../{TimeZoomScrollbar.java => ZoomBar.java} | 6 +-- 4 files changed, 42 insertions(+), 62 deletions(-) rename plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/{TimeZoomScrollbar.java => ZoomBar.java} (96%) diff --git a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformCanvas.java b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformCanvas.java index 5f7cde9..ec07c38 100644 --- a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformCanvas.java +++ b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformCanvas.java @@ -46,7 +46,7 @@ import com.minres.scviewer.database.ui.IWaveformZoom; import com.minres.scviewer.database.ui.TrackEntry; import com.minres.scviewer.database.ui.ZoomKind; import com.minres.scviewer.database.ui.swt.Constants; -import com.minres.scviewer.database.ui.swt.internal.slider.TimeZoomScrollbar; +import com.minres.scviewer.database.ui.swt.internal.slider.ZoomBar; public class WaveformCanvas extends Canvas implements IWaveformZoom{ @@ -54,6 +54,8 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ public static final long ZOOM_FULL = -1; + private static final int INITIAL_ZOOM_BAR_MAX = 1000; + private boolean doubleBuffering = true; IWaveformStyleProvider styleProvider; @@ -82,22 +84,17 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ private List cursorPainters; - private TimeZoomScrollbar horizontal; + private ZoomBar horizontal; private int[] lastHorSelection; + private long sliderScaleFactor = 1; + private ScrollBar vertical; HashMap wave2painterMap; - /** - * Constructor for ScrollableCanvas. - * - * @param parent - * the parent of this control.super(parent, style | SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); - * @param style - * the style of this control. - */ - public WaveformCanvas(final Composite parent, int style, IWaveformStyleProvider styleProvider, TimeZoomScrollbar.IProvider scrollbarProvider) { + + public WaveformCanvas(final Composite parent, int style, IWaveformStyleProvider styleProvider, ZoomBar.IProvider scrollbarProvider) { super(parent, (style & ~SWT.H_SCROLL) | SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.V_SCROLL ); this.styleProvider=styleProvider; addControlListener(new ControlAdapter() { /* resize listener. */ @@ -158,10 +155,6 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ public void setOrigin(int x, int y) { checkWidget(); -// if(x<=0) horizontal.setSelection(-x); -// x = -horizontal.getSelection()[0]; -// if(y<=0) vertical.setSelection(-y); -// y = -vertical.getSelection(); origin.x = x; origin.y = y; syncSb(); @@ -174,6 +167,12 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ public void setMaxTime(long maxTime) { this.maxTime = maxTime; + if(maxTime>INITIAL_ZOOM_BAR_MAX) { + long maxBarTime = maxTime; + while(maxBarTime>Integer.MAX_VALUE) maxBarTime/=1000; + horizontal.setMaximum((int) maxBarTime); + } + sliderScaleFactor = maxTime/horizontal.getMaximum(); syncSb(); } @@ -302,22 +301,22 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ public void widgetSelected(SelectionEvent event) { if (!painterList.isEmpty()) { int[] sel = horizontal.getSelection(); - long lowerTime = sel[0]*maxTime/horizontal.getMaximum(); - long upperTime = sel[1]*maxTime/horizontal.getMaximum(); + long lowerTime = sel[0]*sliderScaleFactor; + long upperTime = sel[1]*sliderScaleFactor; if(sel[1]-sel[0] != lastHorSelection[1]-lastHorSelection[0]) { long time_diff = upperTime-lowerTime; long factor = time_diff/getClientArea().width; setScalingFactor(factor, lowerTime+time_diff/2); } else { - origin.x = -(int) (lowerTime / getScale()); - event.widget.getDisplay().asyncExec(() -> {redraw();}); + origin.x = -(int) (lowerTime/getScale()); + WaveformCanvas.this.getDisplay().asyncExec(() -> {redraw();}); } lastHorSelection=sel; } } }); horizontal.setMinimum(0); - horizontal.setMaximum(10000); + horizontal.setMaximum(INITIAL_ZOOM_BAR_MAX); lastHorSelection = horizontal.getSelection(); vertical.setEnabled(false); @@ -328,7 +327,7 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ if (!painterList.isEmpty()) { origin.y=-vertical.getSelection(); fireSelectionEvent(); - event.widget.getDisplay().asyncExec(() -> {redraw();}); + WaveformCanvas.this.getDisplay().asyncExec(() -> {redraw();}); } } }); @@ -345,7 +344,7 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ syncVSb(); fireSelectionEvent(); } - this.getDisplay().asyncExec(() -> {redraw();}); + redraw(); } private void syncVSb() { @@ -373,10 +372,9 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{ horizontal.setEnabled(wave2painterMap.size()>0); Rectangle clientArea=getClientArea(); int clientWidth = clientArea.width; - if(maxTime>0) { - int lower = (int) ( -origin.x * getScale() * horizontal.getMaximum() / maxTime); - int upper = (int) ((clientWidth-origin.x) * getScale() * horizontal.getMaximum() / maxTime); - int[] curSel = horizontal.getSelection(); + if(sliderScaleFactor>0) { + int lower = (int) ( ( -origin.x * getScale()) / sliderScaleFactor); + int upper = (int) (((clientWidth-origin.x) * getScale()) / sliderScaleFactor); lastHorSelection = new int[] {Math.max(lower,0), Math.min(upper, horizontal.getMaximum())}; horizontal.setSelection(lastHorSelection); } diff --git a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformView.java b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformView.java index 11cdd0f..ae2347a 100644 --- a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformView.java +++ b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/WaveformView.java @@ -91,7 +91,7 @@ import com.minres.scviewer.database.ui.IWaveformStyleProvider; import com.minres.scviewer.database.ui.IWaveformView; import com.minres.scviewer.database.ui.IWaveformZoom; import com.minres.scviewer.database.ui.TrackEntry; -import com.minres.scviewer.database.ui.swt.internal.slider.TimeZoomScrollbar; +import com.minres.scviewer.database.ui.swt.internal.slider.ZoomBar; public class WaveformView implements IWaveformView { @@ -352,11 +352,11 @@ public class WaveformView implements IWaveformView { gl_waveformPane.marginHeight = 0; waveformPane.setLayout(gl_waveformPane); - waveformCanvas = new WaveformCanvas(waveformPane, SWT.NONE | SWT.V_SCROLL /*| SWT.H_SCROLL*/, styleProvider, new TimeZoomScrollbar.IProvider() { + waveformCanvas = new WaveformCanvas(waveformPane, SWT.NONE | SWT.V_SCROLL /*| SWT.H_SCROLL*/, styleProvider, new ZoomBar.IProvider() { @Override - public TimeZoomScrollbar getScrollBar() { - TimeZoomScrollbar timeSliderPane = new TimeZoomScrollbar(waveformPane, SWT.NONE); + public ZoomBar getScrollBar() { + ZoomBar timeSliderPane = new ZoomBar(waveformPane, SWT.NONE); GridData gd_timeSliderPane = new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1); timeSliderPane.setLayoutData(gd_timeSliderPane); return timeSliderPane; diff --git a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/RangeSlider.java b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/RangeSlider.java index 9d6847c..d484730 100644 --- a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/RangeSlider.java +++ b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/RangeSlider.java @@ -40,7 +40,6 @@ public class RangeSlider extends Canvas { private int previousUpperValue, previousLowerValue; private int startDragUpperValue, startDragLowerValue; private Point startDragPoint; - private final boolean isSmooth; private final boolean isFullSelection=false; private final boolean isHighQuality; private final boolean isOn; @@ -68,7 +67,6 @@ public class RangeSlider extends Canvas { maximum = upperValue = 100; increment = 1; pageIncrement = 10; - isSmooth = (style & SWT.SMOOTH) == SWT.SMOOTH; isHighQuality = (style & SWT.HIGH) == SWT.HIGH; isOn = (style & SWT.ON) == SWT.ON; selectedElement = NONE; @@ -86,7 +84,7 @@ public class RangeSlider extends Canvas { @Override public int getStyle() { - return super.getStyle() | (isSmooth ? SWT.SMOOTH : SWT.NONE) | // + return super.getStyle() | // (isOn ? SWT.ON : SWT.NONE) | // (isFullSelection ? SWT.CONTROL : SWT.NONE) | // (isHighQuality ? SWT.HIGH : SWT.NONE); @@ -166,7 +164,8 @@ public class RangeSlider extends Canvas { } } - + private boolean busy = false; + private void handleMouseMove(final Event e) { if (selectedElement==NONE) { final boolean wasUpper = upperHover; @@ -190,28 +189,20 @@ public class RangeSlider extends Canvas { } upperValue = newUpper; lowerValue = newLower; - if (!isSmooth) { - lowerValue = (int) (Math.ceil(lowerValue / increment) * increment) - increment; - upperValue = (int) (Math.ceil(upperValue / increment) * increment) - increment; - } handleToolTip(lowerValue, upperValue); } else if (selectedElement == UPPER) { - upperValue = (int) Math.round((x - 9d) / computePixelSizeForSlider()) + minimum; - if (!isSmooth) { - upperValue = Math.min(lowerValue, (int) (Math.ceil(upperValue / increment) * increment) - increment); - } + upperValue = (int) Math.round((double)(x - markerWidth) / computePixelSizeForSlider()) + minimum; checkUpperValue(); handleToolTip(upperValue); } else if (selectedElement == LOWER){ - lowerValue = (int) Math.round((x - 9d) / computePixelSizeForSlider()) + minimum; - if (!isSmooth) { - lowerValue = Math.max(upperValue, (int) (Math.ceil(lowerValue / increment) * increment) - increment); - } + lowerValue = (int) Math.round((double)(x - markerWidth) / computePixelSizeForSlider()) + minimum; checkLowerValue(); handleToolTip(lowerValue); } - if (isOn) { + if (isOn && !busy) { validateNewValues(e); + busy=true; + getDisplay().timerExec(50, ()->{busy=false;}); } else { redraw(); } @@ -565,12 +556,6 @@ public class RangeSlider extends Canvas { return pageIncrement; } -// public void setIncrement(final int increment) { -// checkWidget(); -// this.increment = increment; -// redraw(); -// } - public void setMaximum(final int value) { setLimits(minimum, value); } @@ -616,12 +601,6 @@ public class RangeSlider extends Canvas { setValues(value, upperValue); } -// public void setPageIncrement(final int pageIncrement) { -// checkWidget(); -// this.pageIncrement = pageIncrement; -// redraw(); -// } - public void setValues(final int[] values) { if (values.length == 2) { setValues(values[0], values[1]); @@ -642,6 +621,9 @@ public class RangeSlider extends Canvas { e.type=SWT.Selection; e.doit=true; validateNewValues(e); + } else { + increment = Math.max(1, (upperValue-lowerValue)/100); + pageIncrement = Math.max(1, (upperValue-lowerValue)/2); } redraw(); } diff --git a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/TimeZoomScrollbar.java b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/ZoomBar.java similarity index 96% rename from plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/TimeZoomScrollbar.java rename to plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/ZoomBar.java index 3bac523..593a5a2 100644 --- a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/TimeZoomScrollbar.java +++ b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/swt/internal/slider/ZoomBar.java @@ -12,10 +12,10 @@ import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.wb.swt.SWTResourceManager; -public class TimeZoomScrollbar extends Composite { +public class ZoomBar extends Composite { static public interface IProvider { - TimeZoomScrollbar getScrollBar(); + ZoomBar getScrollBar(); } final RangeSlider timeSlider; @@ -26,7 +26,7 @@ public class TimeZoomScrollbar extends Composite { * @param parent * @param style */ - public TimeZoomScrollbar(Composite parent, int style) { + public ZoomBar(Composite parent, int style) { super(parent, SWT.NO_FOCUS); GridLayout gridLayout = new GridLayout(3, false); gridLayout.horizontalSpacing = 0;