Skip to content

Commit

Permalink
Added API to reset zoom/scale
Browse files Browse the repository at this point in the history
Aligned labels on sections
  • Loading branch information
serjiokov committed May 20, 2020
1 parent 3edd8e1 commit 6f2fe8e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 41 deletions.
Expand Up @@ -77,4 +77,8 @@ public void structure(List<Class<?>> types) {
public void refresh() {
stage.refresh();
}

public void reset() {
stage.reset();
}
}
Expand Up @@ -26,6 +26,7 @@
*/
public interface ChronographObjectLabelRenderer<D> {

public void drawLabel(String label, Position brickPosition, GC gc, Rectangle objectBounds, int vOffset, int scale);
public void drawLabel(String label, Position brickPosition, GC gc, Rectangle objectBounds, int vOffset, int scale,
int zoom);

}
Expand Up @@ -33,38 +33,54 @@ public class GroupRendererImpl implements ChronographGroupRenderer {
@Override
public void draw(GC gc, String label, Rectangle groupBound, Display display, int width, int hintY) {
int fontHeight = gc.getFontMetrics().getHeight();
final Rectangle groupNameRectangle = new Rectangle(groupBound.x, groupBound.y - hintY, width,
groupBound.height);
final Rectangle groupRectangle = new Rectangle(groupBound.x, groupBound.y - hintY, width, groupBound.height);
gc.setForeground(GroupStyler.GROUP_TOP_COLOR);
gc.setBackground(GroupStyler.GROUP_BTM_COLOR);
gc.setAntialias(SWT.ON);
gc.setForeground(GroupStyler.GROUP_BTM_COLOR);
gc.setBackground(GroupStyler.GROUP_TOP_COLOR);
gc.fillRoundRectangle(groupNameRectangle.x, groupNameRectangle.y, groupNameRectangle.width,
groupNameRectangle.height, width, width);
gc.drawRoundRectangle(groupNameRectangle.x, groupNameRectangle.y, groupNameRectangle.width,
groupNameRectangle.height, width, width);
gc.fillRoundRectangle(groupRectangle.x, groupRectangle.y, groupRectangle.width, groupRectangle.height, width,
width);
gc.drawRoundRectangle(groupRectangle.x, groupRectangle.y, groupRectangle.width, groupRectangle.height, width,
width);
gc.setForeground(GroupStyler.GROUP_BTM_COLOR);
gc.drawRoundRectangle(groupBound.x, groupBound.y - hintY, groupBound.width, groupBound.height, width, width);

Point stringExtent = gc.stringExtent(label);
String msg = label; // $NON-NLS-1$
if (stringExtent.x > groupNameRectangle.height) {
msg = label.substring(0, 5) + "..."; //$NON-NLS-1$
}

String msg = calculateLabel(gc, label, groupRectangle, stringExtent);

stringExtent = gc.stringExtent(msg);
Transform tr = new Transform(display);
tr.translate(groupNameRectangle.x, groupNameRectangle.y);
tr.translate(groupRectangle.x, groupRectangle.y);
tr.rotate(-90);
gc.setTransform(tr);
gc.setForeground(GroupStyler.GROUP_TEXT_COLOR);
gc.drawString(msg, -groupNameRectangle.height + (groupNameRectangle.height - stringExtent.x) / 2,
fontHeight / 2, true);
gc.drawString(msg, -groupRectangle.height + (groupRectangle.height - stringExtent.x) / 2, fontHeight / 2, true);
// gc.setBackground(GroupStyler.GROUP_BTM_COLOR);
// gc.fillOval(-groupNameRectangle.height, 0, width, width);

tr.dispose();
gc.setTransform(null);
}

private String calculateLabel(GC gc, String label, final Rectangle groupRectangle, Point stringExtent) {
String msg = ""; //$NON-NLS-1$
String ends = "..."; //$NON-NLS-1$
Point endsExt = gc.stringExtent(ends);
int chWidths = 0;
if (stringExtent.x > groupRectangle.height) {
for (char ch : label.toCharArray()) {
chWidths += gc.getCharWidth(ch);
if (chWidths < groupRectangle.height - endsExt.x) {
msg += ch;
}
}
msg += ends;
} else {
msg = label;
}
return msg;
}

}
Expand Up @@ -33,7 +33,8 @@
public class ObjectLabelRendererImpl<D> implements ChronographObjectLabelRenderer<D> {

@Override
public void drawLabel(String label, Position brickPosition, GC gc, Rectangle objectBounds, int vOffset, int scale) {
public void drawLabel(String label, Position brickPosition, GC gc, Rectangle objectBounds, int vOffset, int scale,
int zoom) {
FontMetrics fontMetrics = gc.getFontMetrics();
int height = fontMetrics.getHeight();
gc.setForeground(StageStyler.STAGE_TEXT_COLOR);
Expand All @@ -42,8 +43,10 @@ public void drawLabel(String label, Position brickPosition, GC gc, Rectangle obj

String msg = String.valueOf(UnitConverter.unitsToLocalDate((int) brickPosition.start()));
gc.drawString(msg, objectBounds.x + BrickStyler.getHeight(), objectBounds.y + mediana, true);
if (scale > 1) {
if (zoom > 1) {
gc.drawString(label, objectBounds.x + height / 2, objectBounds.y - height, true);
}
if (scale > 1) {
msg = String.valueOf(UnitConverter.unitsToLocalDate((int) brickPosition.end()));
Point msgExtent = gc.textExtent(msg);
gc.drawString(msg, objectBounds.x + objectBounds.width - (msgExtent.x + msgExtent.y),
Expand Down
Expand Up @@ -34,11 +34,11 @@ public class SectionRendererImpl implements ChronographSectionRenderer {
public void draw(GC gc, String label, Rectangle bounds, Display display, int width, int hintY) {
gc.setAntialias(SWT.ON);
int fontHeight = gc.getFontMetrics().getHeight();

final Rectangle sectionRectangle = new Rectangle(bounds.x, bounds.y, width, bounds.height);
gc.setForeground(SectionStyler.SECTION_BTM_COLOR);
gc.setBackground(SectionStyler.SECTION_TOP_COLOR);
gc.fillRoundRectangle(0, bounds.y - hintY, width, bounds.height, width, width);
gc.drawRoundRectangle(0, bounds.y - hintY, width, bounds.height, width, width);
gc.fillRoundRectangle(0, sectionRectangle.y - hintY, width, sectionRectangle.height, width, width);
gc.drawRoundRectangle(0, sectionRectangle.y - hintY, width, sectionRectangle.height, width, width);

gc.setForeground(SectionStyler.SECTION_TOP_COLOR);
Point lblPoint = gc.stringExtent(label);
Expand All @@ -47,12 +47,36 @@ public void draw(GC gc, String label, Rectangle bounds, Display display, int wid
tr.rotate(-90);
gc.setTransform(tr);
gc.setForeground(SectionStyler.SECTION_TEXT_COLOR);
gc.drawString(label, -bounds.y - bounds.height + (bounds.height - lblPoint.x) / 2, fontHeight / 2, true);

Point stringExtent = gc.stringExtent(label);
String msg = calculateLabel(gc, label, sectionRectangle, stringExtent);
stringExtent = gc.stringExtent(msg);
int x = -sectionRectangle.height - sectionRectangle.y;
gc.drawString(msg, x + (sectionRectangle.height - stringExtent.x) / 2, fontHeight / 2, true);
gc.setBackground(SectionStyler.SECTION_BTM_COLOR);
gc.fillOval(-bounds.y - bounds.height, 0, width, width);
// gc.fillOval(-bounds.y - bounds.height, 0, width, width);

tr.dispose();
gc.setTransform(null);
}

private String calculateLabel(GC gc, String label, final Rectangle rectangle, Point stringExtent) {
String msg = ""; //$NON-NLS-1$
String ends = "..."; //$NON-NLS-1$
Point endsExt = gc.stringExtent(ends);
int chWidths = 0;
if (stringExtent.x > rectangle.height) {
for (char ch : label.toCharArray()) {
chWidths += gc.getCharWidth(ch);
if (chWidths < rectangle.height - endsExt.x) {
msg += ch;
}
}
msg += ends;
} else {
msg = label;
}
return msg;
}

}
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.chronograph.internal.swt.stage;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.chronograph.internal.api.representation.Decoration;
import org.eclipse.chronograph.internal.base.AreaImpl;
import org.eclipse.chronograph.internal.base.PlainData;
import org.eclipse.chronograph.internal.base.UnitConverter;
import org.eclipse.chronograph.internal.base.query.ActualBricks;
import org.eclipse.chronograph.internal.base.query.ExpiredBricks;
import org.eclipse.chronograph.internal.swt.AreaRectangle;
Expand Down Expand Up @@ -60,7 +62,7 @@ public final class Stage<D> extends Canvas {
private int pxlHint = 5;
private int pXhint;
private int pYhint;
private int stageScale = 5;
private int scale = 5;

private List<Brick<D>> bricksSelected;

Expand Down Expand Up @@ -110,7 +112,7 @@ private void initCalculator() {
private void initScrollBarHorizontal() {
scrollBarHorizontal = getHorizontalBar();
scrollBarHorizontal.setVisible(true);
scrollBarHorizontal.setPageIncrement(stageScale);
scrollBarHorizontal.setPageIncrement(scale);
scrollBarHorizontal.setMaximum(pXMax);
scrollBarHorizontal.addListener(SWT.Selection, new Listener() {
@Override
Expand Down Expand Up @@ -164,7 +166,7 @@ protected void horizontalScroll(Event event) {
if (!scrollBarHorizontal.isVisible()) {
return;
}
setPositionByX(scrollBarHorizontal.getSelection() * pxlHint * stageScale);
setPositionByX(scrollBarHorizontal.getSelection() * pxlHint * scale);
applyHint();
redraw();
}
Expand Down Expand Up @@ -203,7 +205,7 @@ public void repaint(PaintEvent event) {
INSTANCE.getDrawingStagePainter().draw(gc, stageRectangle);
List<ChronographStageRulerRenderer> list = INSTANCE.getDrawingRulersPainter();
for (ChronographStageRulerRenderer painter : list) {
painter.draw(gc, stageRectangle, stageScale, pxlHint, pXhint, pX);
painter.draw(gc, stageRectangle, scale, pxlHint, pXhint, pX);
}
for (Group section : registry.groups()) {
List<Group> groupsBySection = registry.subGroups(section);
Expand Down Expand Up @@ -294,7 +296,7 @@ private void drawSceneObjects(final GC gc, Area area, final Collection<Brick<D>>
Rectangle rectangleArea = areaRectangle.apply(brickArea);
INSTANCE.getContentPainter().draw(brick, gc, rectangleArea, pYhint);
String label = labelProvider.brickText(brick);
INSTANCE.getLabelPainter().drawLabel(label, brick.position(), gc, rectangleArea, pYhint, pxlHint);
INSTANCE.getLabelPainter().drawLabel(label, brick.position(), gc, rectangleArea, pYhint, pxlHint, zoom);
INSTANCE.getDurationPainter().drawObjectDuration(brick, gc, pYhint);
}
});
Expand All @@ -313,13 +315,13 @@ private void drawSelectedObjects(final GC gc, Area area, final Collection<Brick<
Rectangle rectangleArea = areaRectangle.apply(brickArea);
INSTANCE.getSelectedContentPainter().draw(brick, gc, rectangleArea, pYhint);
String label = labelProvider.brickText(brick);
INSTANCE.getLabelPainter().drawLabel(label, brick.position(), gc, rectangleArea, pYhint, pxlHint);
INSTANCE.getLabelPainter().drawLabel(label, brick.position(), gc, rectangleArea, pYhint, pxlHint, zoom);
INSTANCE.getDurationPainter().drawObjectDuration(brick, gc, pYhint);
}
}

public void navigateToUnit(int hint) {
pX = hint * pxlHint * stageScale;
pX = hint * pxlHint * scale;
applyHint();
redraw();
}
Expand All @@ -346,29 +348,29 @@ public void clearSections() {
}

public int getScale() {
return stageScale;
return scale;
}

private void updateStageScale() {
if (stageScale <= 0) {
stageScale = 1;
if (scale <= 0) {
scale = 1;

}
pxlHint = stageScale;
// if (stageScale == 2 || stageScale == 3) {
// pxlHint = stageScale * 1;
pxlHint = scale;
// if (scale == 2 || scale == 3) {
// pxlHint = scale * 1;
// }
// if (stageScale == 4) {
// pxlHint = stageScale * 2;
// if (scale == 4) {
// pxlHint = scale * 2;
// }
// if (stageScale > 4) {
// pxlHint = stageScale * SCALE_DEFAULT;
// if (scale > 4) {
// pxlHint = scale * SCALE_DEFAULT;
// }
}

public void scaleUp() {
checkWidget();
stageScale--;
scale--;
updateStageScale();
redraw();
updateScrollers();
Expand All @@ -377,7 +379,7 @@ public void scaleUp() {

public void scaleDown() {
checkWidget();
stageScale++;
scale++;
updateStageScale();
redraw();
navigateToUnit(pXhint);
Expand All @@ -402,7 +404,7 @@ public Rectangle getClientArea() {
}

void applyHint() {
pXhint = pX / (pxlHint * stageScale);
pXhint = pX / (pxlHint * scale);
}

public Optional<Brick<D>> brickAt(int x, int y) {
Expand Down Expand Up @@ -465,4 +467,13 @@ public void structure(List<Class<?>> types) {
public void refresh() {
structure(registry.structure());
}

public void reset() {
zoom = 2;
scale = 3;
updateStageScale();
navigateToUnit(UnitConverter.localDatetoUnits(LocalDate.now().minusMonths(5)));
calculateObjectBounds();
redraw();
}
}

0 comments on commit 6f2fe8e

Please sign in to comment.