fix behavior of ZoomBar used by WaveformCanvas

This commit is contained in:
Eyck Jentzsch 2022-01-07 19:42:27 +01:00
parent 24890f9bbb
commit 20934a9f47
4 changed files with 42 additions and 62 deletions

View File

@ -46,7 +46,7 @@ import com.minres.scviewer.database.ui.IWaveformZoom;
import com.minres.scviewer.database.ui.TrackEntry; import com.minres.scviewer.database.ui.TrackEntry;
import com.minres.scviewer.database.ui.ZoomKind; import com.minres.scviewer.database.ui.ZoomKind;
import com.minres.scviewer.database.ui.swt.Constants; 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{ 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; public static final long ZOOM_FULL = -1;
private static final int INITIAL_ZOOM_BAR_MAX = 1000;
private boolean doubleBuffering = true; private boolean doubleBuffering = true;
IWaveformStyleProvider styleProvider; IWaveformStyleProvider styleProvider;
@ -82,22 +84,17 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
private List<CursorPainter> cursorPainters; private List<CursorPainter> cursorPainters;
private TimeZoomScrollbar horizontal; private ZoomBar horizontal;
private int[] lastHorSelection; private int[] lastHorSelection;
private long sliderScaleFactor = 1;
private ScrollBar vertical; private ScrollBar vertical;
HashMap<IWaveform, IWaveformPainter> wave2painterMap; HashMap<IWaveform, IWaveformPainter> wave2painterMap;
/**
* Constructor for ScrollableCanvas. public WaveformCanvas(final Composite parent, int style, IWaveformStyleProvider styleProvider, ZoomBar.IProvider scrollbarProvider) {
*
* @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) {
super(parent, (style & ~SWT.H_SCROLL) | SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.V_SCROLL ); super(parent, (style & ~SWT.H_SCROLL) | SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.V_SCROLL );
this.styleProvider=styleProvider; this.styleProvider=styleProvider;
addControlListener(new ControlAdapter() { /* resize listener. */ addControlListener(new ControlAdapter() { /* resize listener. */
@ -158,10 +155,6 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
public void setOrigin(int x, int y) { public void setOrigin(int x, int y) {
checkWidget(); checkWidget();
// if(x<=0) horizontal.setSelection(-x);
// x = -horizontal.getSelection()[0];
// if(y<=0) vertical.setSelection(-y);
// y = -vertical.getSelection();
origin.x = x; origin.x = x;
origin.y = y; origin.y = y;
syncSb(); syncSb();
@ -174,6 +167,12 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
public void setMaxTime(long maxTime) { public void setMaxTime(long maxTime) {
this.maxTime = 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(); syncSb();
} }
@ -302,22 +301,22 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
public void widgetSelected(SelectionEvent event) { public void widgetSelected(SelectionEvent event) {
if (!painterList.isEmpty()) { if (!painterList.isEmpty()) {
int[] sel = horizontal.getSelection(); int[] sel = horizontal.getSelection();
long lowerTime = sel[0]*maxTime/horizontal.getMaximum(); long lowerTime = sel[0]*sliderScaleFactor;
long upperTime = sel[1]*maxTime/horizontal.getMaximum(); long upperTime = sel[1]*sliderScaleFactor;
if(sel[1]-sel[0] != lastHorSelection[1]-lastHorSelection[0]) { if(sel[1]-sel[0] != lastHorSelection[1]-lastHorSelection[0]) {
long time_diff = upperTime-lowerTime; long time_diff = upperTime-lowerTime;
long factor = time_diff/getClientArea().width; long factor = time_diff/getClientArea().width;
setScalingFactor(factor, lowerTime+time_diff/2); setScalingFactor(factor, lowerTime+time_diff/2);
} else { } else {
origin.x = -(int) (lowerTime/getScale()); origin.x = -(int) (lowerTime/getScale());
event.widget.getDisplay().asyncExec(() -> {redraw();}); WaveformCanvas.this.getDisplay().asyncExec(() -> {redraw();});
} }
lastHorSelection=sel; lastHorSelection=sel;
} }
} }
}); });
horizontal.setMinimum(0); horizontal.setMinimum(0);
horizontal.setMaximum(10000); horizontal.setMaximum(INITIAL_ZOOM_BAR_MAX);
lastHorSelection = horizontal.getSelection(); lastHorSelection = horizontal.getSelection();
vertical.setEnabled(false); vertical.setEnabled(false);
@ -328,7 +327,7 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
if (!painterList.isEmpty()) { if (!painterList.isEmpty()) {
origin.y=-vertical.getSelection(); origin.y=-vertical.getSelection();
fireSelectionEvent(); fireSelectionEvent();
event.widget.getDisplay().asyncExec(() -> {redraw();}); WaveformCanvas.this.getDisplay().asyncExec(() -> {redraw();});
} }
} }
}); });
@ -345,7 +344,7 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
syncVSb(); syncVSb();
fireSelectionEvent(); fireSelectionEvent();
} }
this.getDisplay().asyncExec(() -> {redraw();}); redraw();
} }
private void syncVSb() { private void syncVSb() {
@ -373,10 +372,9 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
horizontal.setEnabled(wave2painterMap.size()>0); horizontal.setEnabled(wave2painterMap.size()>0);
Rectangle clientArea=getClientArea(); Rectangle clientArea=getClientArea();
int clientWidth = clientArea.width; int clientWidth = clientArea.width;
if(maxTime>0) { if(sliderScaleFactor>0) {
int lower = (int) ( -origin.x * getScale() * horizontal.getMaximum() / maxTime); int lower = (int) ( ( -origin.x * getScale()) / sliderScaleFactor);
int upper = (int) ((clientWidth-origin.x) * getScale() * horizontal.getMaximum() / maxTime); int upper = (int) (((clientWidth-origin.x) * getScale()) / sliderScaleFactor);
int[] curSel = horizontal.getSelection();
lastHorSelection = new int[] {Math.max(lower,0), Math.min(upper, horizontal.getMaximum())}; lastHorSelection = new int[] {Math.max(lower,0), Math.min(upper, horizontal.getMaximum())};
horizontal.setSelection(lastHorSelection); horizontal.setSelection(lastHorSelection);
} }

View File

@ -91,7 +91,7 @@ import com.minres.scviewer.database.ui.IWaveformStyleProvider;
import com.minres.scviewer.database.ui.IWaveformView; import com.minres.scviewer.database.ui.IWaveformView;
import com.minres.scviewer.database.ui.IWaveformZoom; import com.minres.scviewer.database.ui.IWaveformZoom;
import com.minres.scviewer.database.ui.TrackEntry; 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 { public class WaveformView implements IWaveformView {
@ -352,11 +352,11 @@ public class WaveformView implements IWaveformView {
gl_waveformPane.marginHeight = 0; gl_waveformPane.marginHeight = 0;
waveformPane.setLayout(gl_waveformPane); 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 @Override
public TimeZoomScrollbar getScrollBar() { public ZoomBar getScrollBar() {
TimeZoomScrollbar timeSliderPane = new TimeZoomScrollbar(waveformPane, SWT.NONE); ZoomBar timeSliderPane = new ZoomBar(waveformPane, SWT.NONE);
GridData gd_timeSliderPane = new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1); GridData gd_timeSliderPane = new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1);
timeSliderPane.setLayoutData(gd_timeSliderPane); timeSliderPane.setLayoutData(gd_timeSliderPane);
return timeSliderPane; return timeSliderPane;

View File

@ -40,7 +40,6 @@ public class RangeSlider extends Canvas {
private int previousUpperValue, previousLowerValue; private int previousUpperValue, previousLowerValue;
private int startDragUpperValue, startDragLowerValue; private int startDragUpperValue, startDragLowerValue;
private Point startDragPoint; private Point startDragPoint;
private final boolean isSmooth;
private final boolean isFullSelection=false; private final boolean isFullSelection=false;
private final boolean isHighQuality; private final boolean isHighQuality;
private final boolean isOn; private final boolean isOn;
@ -68,7 +67,6 @@ public class RangeSlider extends Canvas {
maximum = upperValue = 100; maximum = upperValue = 100;
increment = 1; increment = 1;
pageIncrement = 10; pageIncrement = 10;
isSmooth = (style & SWT.SMOOTH) == SWT.SMOOTH;
isHighQuality = (style & SWT.HIGH) == SWT.HIGH; isHighQuality = (style & SWT.HIGH) == SWT.HIGH;
isOn = (style & SWT.ON) == SWT.ON; isOn = (style & SWT.ON) == SWT.ON;
selectedElement = NONE; selectedElement = NONE;
@ -86,7 +84,7 @@ public class RangeSlider extends Canvas {
@Override @Override
public int getStyle() { public int getStyle() {
return super.getStyle() | (isSmooth ? SWT.SMOOTH : SWT.NONE) | // return super.getStyle() | //
(isOn ? SWT.ON : SWT.NONE) | // (isOn ? SWT.ON : SWT.NONE) | //
(isFullSelection ? SWT.CONTROL : SWT.NONE) | // (isFullSelection ? SWT.CONTROL : SWT.NONE) | //
(isHighQuality ? SWT.HIGH : SWT.NONE); (isHighQuality ? SWT.HIGH : SWT.NONE);
@ -166,6 +164,7 @@ public class RangeSlider extends Canvas {
} }
} }
private boolean busy = false;
private void handleMouseMove(final Event e) { private void handleMouseMove(final Event e) {
if (selectedElement==NONE) { if (selectedElement==NONE) {
@ -190,28 +189,20 @@ public class RangeSlider extends Canvas {
} }
upperValue = newUpper; upperValue = newUpper;
lowerValue = newLower; lowerValue = newLower;
if (!isSmooth) {
lowerValue = (int) (Math.ceil(lowerValue / increment) * increment) - increment;
upperValue = (int) (Math.ceil(upperValue / increment) * increment) - increment;
}
handleToolTip(lowerValue, upperValue); handleToolTip(lowerValue, upperValue);
} else if (selectedElement == UPPER) { } else if (selectedElement == UPPER) {
upperValue = (int) Math.round((x - 9d) / computePixelSizeForSlider()) + minimum; upperValue = (int) Math.round((double)(x - markerWidth) / computePixelSizeForSlider()) + minimum;
if (!isSmooth) {
upperValue = Math.min(lowerValue, (int) (Math.ceil(upperValue / increment) * increment) - increment);
}
checkUpperValue(); checkUpperValue();
handleToolTip(upperValue); handleToolTip(upperValue);
} else if (selectedElement == LOWER){ } else if (selectedElement == LOWER){
lowerValue = (int) Math.round((x - 9d) / computePixelSizeForSlider()) + minimum; lowerValue = (int) Math.round((double)(x - markerWidth) / computePixelSizeForSlider()) + minimum;
if (!isSmooth) {
lowerValue = Math.max(upperValue, (int) (Math.ceil(lowerValue / increment) * increment) - increment);
}
checkLowerValue(); checkLowerValue();
handleToolTip(lowerValue); handleToolTip(lowerValue);
} }
if (isOn) { if (isOn && !busy) {
validateNewValues(e); validateNewValues(e);
busy=true;
getDisplay().timerExec(50, ()->{busy=false;});
} else { } else {
redraw(); redraw();
} }
@ -565,12 +556,6 @@ public class RangeSlider extends Canvas {
return pageIncrement; return pageIncrement;
} }
// public void setIncrement(final int increment) {
// checkWidget();
// this.increment = increment;
// redraw();
// }
public void setMaximum(final int value) { public void setMaximum(final int value) {
setLimits(minimum, value); setLimits(minimum, value);
} }
@ -616,12 +601,6 @@ public class RangeSlider extends Canvas {
setValues(value, upperValue); setValues(value, upperValue);
} }
// public void setPageIncrement(final int pageIncrement) {
// checkWidget();
// this.pageIncrement = pageIncrement;
// redraw();
// }
public void setValues(final int[] values) { public void setValues(final int[] values) {
if (values.length == 2) { if (values.length == 2) {
setValues(values[0], values[1]); setValues(values[0], values[1]);
@ -642,6 +621,9 @@ public class RangeSlider extends Canvas {
e.type=SWT.Selection; e.type=SWT.Selection;
e.doit=true; e.doit=true;
validateNewValues(e); validateNewValues(e);
} else {
increment = Math.max(1, (upperValue-lowerValue)/100);
pageIncrement = Math.max(1, (upperValue-lowerValue)/2);
} }
redraw(); redraw();
} }

View File

@ -12,10 +12,10 @@ import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager; import org.eclipse.wb.swt.SWTResourceManager;
public class TimeZoomScrollbar extends Composite { public class ZoomBar extends Composite {
static public interface IProvider { static public interface IProvider {
TimeZoomScrollbar getScrollBar(); ZoomBar getScrollBar();
} }
final RangeSlider timeSlider; final RangeSlider timeSlider;
@ -26,7 +26,7 @@ public class TimeZoomScrollbar extends Composite {
* @param parent * @param parent
* @param style * @param style
*/ */
public TimeZoomScrollbar(Composite parent, int style) { public ZoomBar(Composite parent, int style) {
super(parent, SWT.NO_FOCUS); super(parent, SWT.NO_FOCUS);
GridLayout gridLayout = new GridLayout(3, false); GridLayout gridLayout = new GridLayout(3, false);
gridLayout.horizontalSpacing = 0; gridLayout.horizontalSpacing = 0;