Ignore:
Timestamp:
2019-11-02T15:11:34+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #16796 - Rework of GPX track colors / layer preferences (patch by Bjoeni)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

    r15292 r15496  
    2828import java.util.LinkedList;
    2929import java.util.List;
     30import java.util.Objects;
    3031import java.util.Random;
    3132
     
    3334
    3435import org.openstreetmap.josm.data.Bounds;
    35 import org.openstreetmap.josm.data.PreferencesUtils;
    3636import org.openstreetmap.josm.data.SystemOfMeasurement;
    3737import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
     
    5252import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent;
    5353import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener;
     54import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
    5455import org.openstreetmap.josm.io.CachedFile;
    5556import org.openstreetmap.josm.spi.preferences.Config;
     
    6667
    6768    /**
    68      * The color that is used for drawing GPX points.
    69      * @since 10824
    70      */
    71     public static final NamedColorProperty DEFAULT_COLOR = new NamedColorProperty(marktr("gps point"), Color.magenta);
     69     * The default color property that is used for drawing GPX points.
     70     * @since 15496
     71     */
     72    public static final NamedColorProperty DEFAULT_COLOR_PROPERTY = new NamedColorProperty(marktr("gps point"), Color.magenta);
    7273
    7374    private final GpxData data;
     
    7980    private boolean alphaLines;
    8081    // draw direction arrows on the lines
    81     private boolean direction;
     82    private boolean arrows;
    8283    /** width of line for paint **/
    8384    private int lineWidth;
     
    9192    private boolean hdopCircle;
    9293    /** paint direction arrow with alternate math. may be faster **/
    93     private boolean alternateDirection;
     94    private boolean arrowsFast;
    9495    /** don't draw arrows nearer to each other than this **/
    95     private int delta;
     96    private int arrowsDelta;
    9697    private double minTrackDurationForTimeColoring;
    9798
     
    107108    private boolean computeCacheColorDynamic;
    108109    private ColorMode computeCacheColored;
    109     private int computeCacheColorTracksTune;
     110    private int computeCacheVelocityTune;
    110111    private int computeCacheHeatMapDrawColorTableIdx;
    111112    private boolean computeCacheHeatMapDrawPointMode;
     
    113114    private int computeCacheHeatMapDrawLowerLimit;
    114115
     116    private Color colorCache;
     117    private Color colorCacheTransparent;
     118
    115119    //// Color-related fields
    116120    /** Mode of the line coloring **/
    117121    private ColorMode colored;
    118122    /** max speed for coloring - allows to tweak line coloring for different speed levels. **/
    119     private int colorTracksTune;
     123    private int velocityTune;
    120124    private boolean colorModeDynamic;
    121125    private Color neutralColor;
     
    146150    /** heat map parameters **/
    147151
    148     // enabled or not (override by settings)
    149     private boolean heatMapEnabled;
    150152    // draw small extra line
    151153    private boolean heatMapDrawExtraLine;
     
    269271    }
    270272
    271     private static String specName(String layerName) {
    272         return "layer " + layerName;
    273     }
    274 
    275     /**
    276      * Get the default color for gps tracks for specified layer
    277      * @param layerName name of the GpxLayer
    278      * @param ignoreCustom do not use preferences
    279      * @return the color or null if the color is not constant
    280      */
    281     public Color getColor(String layerName, boolean ignoreCustom) {
    282         if (ignoreCustom || getColorMode(layerName) == ColorMode.NONE) {
    283             return DEFAULT_COLOR.getChildColor(
    284                     NamedColorProperty.COLOR_CATEGORY_LAYER,
    285                     layerName,
    286                     DEFAULT_COLOR.getName()).get();
    287         } else {
    288             return null;
    289         }
    290     }
    291 
    292273    /**
    293274     * Read coloring mode for specified layer from preferences
    294      * @param layerName name of the GpxLayer
    295275     * @return coloring mode
    296276     */
    297     public ColorMode getColorMode(String layerName) {
     277    public ColorMode getColorMode() {
    298278        try {
    299             int i = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.colors", specName(layerName), 0);
     279            int i = optInt("colormode");
     280            if (i == -1) i = 0; //global
    300281            return ColorMode.fromIndex(i);
    301282        } catch (IndexOutOfBoundsException e) {
     
    305286    }
    306287
    307     /** Reads generic color from preferences (usually gray)
    308      * @return the color
     288    private String opt(String key) {
     289        return GPXSettingsPanel.getLayerPref(layer, key);
     290    }
     291
     292    private boolean optBool(String key) {
     293        return Boolean.parseBoolean(opt(key));
     294    }
     295
     296    private int optInt(String key) {
     297        return GPXSettingsPanel.getLayerPrefInt(layer, key);
     298    }
     299
     300    /**
     301     * Read all drawing-related settings from preferences
    309302     **/
    310     public static Color getGenericColor() {
    311         return DEFAULT_COLOR.get();
    312     }
    313 
    314     /**
    315      * Read all drawing-related settings from preferences
    316      * @param layerName layer name used to access its specific preferences
    317      **/
    318     public void readPreferences(String layerName) {
    319         String spec = specName(layerName);
    320         forceLines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.force", spec, false);
    321         direction = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.direction", spec, false);
    322         lineWidth = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.linewidth", spec, 0);
    323         alphaLines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.alpha-blend", spec, false);
    324 
     303    public void readPreferences() {
     304        forceLines = optBool("lines.force");
     305        arrows = optBool("lines.arrows");
     306        arrowsFast = optBool("lines.arrows.fast");
     307        arrowsDelta = optInt("lines.arrows.min-distance");
     308        lineWidth = optInt("lines.width");
     309        alphaLines = optBool("lines.alpha-blend");
     310
     311        int l = optInt("lines");
    325312        if (!data.fromServer) {
    326             maxLineLength = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.max-line-length.local", spec, -1);
    327             lines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.local", spec, true);
     313            maxLineLength = optInt("lines.max-length.local");
     314            lines = l != 0; //draw for -1 (default), 1 (local) and 2 (all)
    328315        } else {
    329             maxLineLength = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.max-line-length", spec, 200);
    330             lines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines", spec, true);
    331         }
    332         large = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.large", spec, false);
    333         largesize = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.large.size", spec, 3);
    334         hdopCircle = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.hdopcircle", spec, false);
    335         colored = getColorMode(layerName);
    336         alternateDirection = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.alternatedirection", spec, false);
    337         delta = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.min-arrow-distance", spec, 40);
    338         colorTracksTune = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.colorTracksTune", spec, 45);
    339         colorModeDynamic = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.colors.dynamic", spec, false);
     316            maxLineLength = optInt("lines.max-length");
     317            lines = l == 2; //draw only for 2 (all)
     318        }
     319        large = optBool("points.large");
     320        largesize = optInt("points.large.size");
     321        hdopCircle = optBool("points.hdopcircle");
     322        colored = getColorMode();
     323        velocityTune = optInt("colormode.velocity.tune");
     324        colorModeDynamic = optBool("colormode.dynamic-range");
    340325        /* good HDOP's are between 1 and 3, very bad HDOP's go into 3 digit values */
    341326        hdoprange = Config.getPref().getInt("hdop.range", 7);
    342         minTrackDurationForTimeColoring = Config.getPref().getInt("draw.rawgps.date-coloring-min-dt", 60);
    343         largePointAlpha = Config.getPref().getInt("draw.rawgps.large.alpha", -1) & 0xFF;
     327        minTrackDurationForTimeColoring = optInt("colormode.time.min-distance");
     328        largePointAlpha = optInt("points.large.alpha") & 0xFF;
    344329
    345330        // get heatmap parameters
    346         heatMapEnabled = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.heatmap.enabled", spec, false);
    347         heatMapDrawExtraLine = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.heatmap.line-extra", spec, false);
    348         heatMapDrawColorTableIdx = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.heatmap.colormap", spec, 0);
    349         heatMapDrawPointMode = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.heatmap.use-points", spec, false);
    350         heatMapDrawGain = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.heatmap.gain", spec, 0);
    351         heatMapDrawLowerLimit = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.heatmap.lower-limit", spec, 0);
     331        heatMapDrawExtraLine = optBool("colormode.heatmap.line-extra");
     332        heatMapDrawColorTableIdx = optInt("colormode.heatmap.colormap");
     333        heatMapDrawPointMode = optBool("colormode.heatmap.use-points");
     334        heatMapDrawGain = optInt("colormode.heatmap.gain");
     335        heatMapDrawLowerLimit = optInt("colormode.heatmap.lower-limit");
    352336
    353337        // shrink to range
    354338        heatMapDrawGain = Utils.clamp(heatMapDrawGain, -10, 10);
    355 
    356         neutralColor = getColor(layerName, true);
     339        neutralColor = DEFAULT_COLOR_PROPERTY.get();
    357340        velocityScale.setNoDataColor(neutralColor);
    358341        dateScale.setNoDataColor(neutralColor);
     
    369352        List<WayPoint> visibleSegments = listVisibleSegments(clipBounds);
    370353        if (!visibleSegments.isEmpty()) {
    371             readPreferences(layer.getName());
     354            readPreferences();
    372355            drawAll(graphics.getDefaultGraphics(), graphics.getMapView(), visibleSegments, clipBounds);
    373356            if (graphics.getMapView().getLayerManager().getActiveLayer() == layer) {
     
    431414     * @since 14748 : new parameter clipBounds
    432415     */
    433 
    434416    public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, Bounds clipBounds) {
    435417
     
    463445
    464446        // global enabled or select via color
    465         boolean useHeatMap = heatMapEnabled || ColorMode.HEATMAP == colored;
     447        boolean useHeatMap = ColorMode.HEATMAP == colored;
    466448
    467449        // default global alpha level
     
    571553            oldWp = null;
    572554        } else { // color mode not dynamic
    573             velocityScale.setRange(0, colorTracksTune);
     555            velocityScale.setRange(0, velocityTune);
    574556            hdopScale.setRange(0, hdoprange);
    575557            qualityScale.setRange(1, rtkLibQualityColors.length);
     
    595577            for (WayPoint trkPnt : segment) {
    596578                LatLon c = trkPnt.getCoor();
    597                 trkPnt.customColoring = neutralColor;
     579                trkPnt.customColoring = segment.getColor();
    598580                if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    599581                    continue;
     
    643625                } else { // make sure we reset outdated data
    644626                    trkPnt.drawLine = false;
    645                     color = neutralColor;
     627                    color = segment.getColor();
    646628                }
    647629                if (color != null) {
     
    701683         ********** STEP 3b - DRAW NICE ARROWS **************************
    702684         ****************************************************************/
    703         if (lines && direction && !alternateDirection) {
     685        if (lines && arrows && !arrowsFast) {
    704686            Point old = null;
    705687            Point oldA = null; // last arrow painted
     
    713695                    // skip points that are on the same screenposition
    714696                    if (old != null
    715                             && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
    716                             || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
     697                            && (oldA == null || screen.x < oldA.x - arrowsDelta || screen.x > oldA.x + arrowsDelta
     698                            || screen.y < oldA.y - arrowsDelta || screen.y > oldA.y + arrowsDelta)) {
    717699                        g.setColor(trkPnt.customColoring);
    718700                        double t = Math.atan2((double) screen.y - old.y, (double) screen.x - old.x) + Math.PI;
     
    731713         ********** STEP 3c - DRAW FAST ARROWS **************************
    732714         ****************************************************************/
    733         if (lines && direction && alternateDirection) {
     715        if (lines && arrows && arrowsFast) {
    734716            Point old = null;
    735717            Point oldA = null; // last arrow painted
     
    743725                    // skip points that are on the same screenposition
    744726                    if (old != null
    745                             && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
    746                             || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
     727                            && (oldA == null || screen.x < oldA.x - arrowsDelta || screen.x > oldA.x + arrowsDelta
     728                            || screen.y < oldA.y - arrowsDelta || screen.y > oldA.y + arrowsDelta)) {
    747729                        g.setColor(trkPnt.customColoring);
    748730                        g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
     
    795777                    // color the large GPS points like the gps lines
    796778                    if (trkPnt.customColoring != null) {
    797                         Color customColoringTransparent = largePointAlpha < 0 ? trkPnt.customColoring :
    798                             new Color((trkPnt.customColoring.getRGB() & 0x00ffffff) | (largePointAlpha << 24), true);
    799 
    800                         g.setColor(customColoringTransparent);
     779                        if (trkPnt.customColoring.equals(colorCache) && colorCacheTransparent != null) {
     780                            g.setColor(colorCacheTransparent);
     781                        } else {
     782                            Color customColoringTransparent = largePointAlpha < 0 ? trkPnt.customColoring :
     783                                new Color((trkPnt.customColoring.getRGB() & 0x00ffffff) | (largePointAlpha << 24), true);
     784
     785                            g.setColor(customColoringTransparent);
     786                            colorCache = trkPnt.customColoring;
     787                            colorCacheTransparent = customColoringTransparent;
     788                        }
    801789                    }
    802790                    g.fillRect(screen.x-halfSize, screen.y-halfSize, largesize, largesize);
     
    816804                }
    817805                if (!trkPnt.drawLine) {
     806                    g.setColor(trkPnt.customColoring);
    818807                    Point screen = mv.getPoint(trkPnt);
    819808                    g.drawRect(screen.x, screen.y, 0, 0);
     
    14811470        if ((computeCacheMaxLineLengthUsed != maxLineLength)
    14821471                || (computeCacheColored != colored)
    1483                 || (computeCacheColorTracksTune != colorTracksTune)
     1472                || (computeCacheVelocityTune != velocityTune)
    14841473                || (computeCacheColorDynamic != colorModeDynamic)
    14851474                || (computeCacheHeatMapDrawColorTableIdx != heatMapDrawColorTableIdx)
    1486                 || (!neutralColor.equals(computeCacheColorUsed)
     1475                || !Objects.equals(neutralColor, computeCacheColorUsed)
    14871476                || (computeCacheHeatMapDrawPointMode != heatMapDrawPointMode)
    1488                 || (computeCacheHeatMapDrawGain != heatMapDrawGain))
     1477                || (computeCacheHeatMapDrawGain != heatMapDrawGain)
    14891478                || (computeCacheHeatMapDrawLowerLimit != heatMapDrawLowerLimit)
    14901479        ) {
     
    14941483            computeCacheColorUsed = neutralColor;
    14951484            computeCacheColored = colored;
    1496             computeCacheColorTracksTune = colorTracksTune;
     1485            computeCacheVelocityTune = velocityTune;
    14971486            computeCacheColorDynamic = colorModeDynamic;
    14981487            computeCacheHeatMapDrawColorTableIdx = heatMapDrawColorTableIdx;
Note: See TracChangeset for help on using the changeset viewer.