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.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<CursorPainter> cursorPainters;
private TimeZoomScrollbar horizontal;
private ZoomBar horizontal;
private int[] lastHorSelection;
private long sliderScaleFactor = 1;
private ScrollBar vertical;
HashMap<IWaveform, IWaveformPainter> 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);
}

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.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;

View File

@ -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();
}

View File

@ -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;