From 6cd1d7c80408a2dea0f9e87317803d38673a2dc5 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Wed, 2 Jan 2019 12:00:31 +0100 Subject: [PATCH] Extract rules from renderInterval - Simplify interval coloring (extract rules from helper::intervalColor) - Add a default color for intervals without tags --- src/commands/CmdChart.cpp | 11 +++++------ src/helper.cpp | 29 +++++++++++++---------------- src/timew.h | 4 ++-- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/commands/CmdChart.cpp b/src/commands/CmdChart.cpp index 6d4efeb4..1ddad8a5 100644 --- a/src/commands/CmdChart.cpp +++ b/src/commands/CmdChart.cpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2016 - 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez. +// Copyright 2016 - 2019, Thomas Lauf, Paul Beckingham, Federico Hernandez. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -45,8 +45,8 @@ static std::string renderDay (Datetime&, const Color&); static std::string renderTotal (time_t); static std::string renderSubTotal (time_t, unsigned long); static void renderExclusionBlocks (std::vector&, const Datetime&, int, int, const std::vector&, int, int, const std::string&, const Color&, const Color&); -static void renderInterval (const Rules&, std::vector&, const Datetime&, const Interval&, std::map&, int, time_t&, bool, int, int); -std::string renderHolidays (const std::map &); +static void renderInterval (std::vector&, const Datetime&, const Interval&, std::map&, int, time_t&, bool, int, int); + std::string renderHolidays (const std::map &); static std::string renderSummary (const std::string&, const Interval&, const std::vector &, const std::vector &, bool); unsigned long getIndentSize (const std::string &type, const Rules &rules); @@ -220,7 +220,7 @@ int renderChart ( for (auto& track : tracked) { time_t interval_work = 0; - renderInterval (rules, lines, day, track, tag_colors, first_hour, interval_work, ids, minutes_per_char, spacing); + renderInterval (lines, day, track, tag_colors, first_hour, interval_work, ids, minutes_per_char, spacing); work += interval_work; } } @@ -572,7 +572,6 @@ static void renderExclusionBlocks ( //////////////////////////////////////////////////////////////////////////////// static void renderInterval ( - const Rules& rules, std::vector & lines, const Datetime& day, const Interval& track, @@ -626,7 +625,7 @@ static void renderInterval ( if (end_offset > start_offset) { // Determine color of interval. - Color colorTrack = intervalColor (track, rules, tag_colors); + Color colorTrack = intervalColor (track.tags (), tag_colors); // Properly format the tags within the space. std::string label; diff --git a/src/helper.cpp b/src/helper.cpp index 51ab2712..27f75c3a 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2016 - 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez. +// Copyright 2016 - 2019, Thomas Lauf, Paul Beckingham, Federico Hernandez. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -39,27 +39,20 @@ //////////////////////////////////////////////////////////////////////////////// // Select a color to represent the interval. Color intervalColor ( - const Interval& interval, - const Rules& rules, + const std::set & tags, std::map & tag_colors) { - Color c; - std::string first_tag; - for (auto& tag : interval.tags ()) + if (tags.empty ()) { - if (first_tag.empty ()) - first_tag = tag; - - std::string name = std::string ("tags.") + tag + ".color"; - if (rules.has (name)) - c.blend (Color (rules.get (name))); + return tag_colors[""]; } - if (c.nontrivial ()) - return c; + Color c; - if (! interval.tags ().empty ()) - return tag_colors[first_tag]; + for (auto& tag : tags) + { + c.blend (tag_colors[tag]); + } return c; } @@ -368,6 +361,10 @@ std::map createTagColorMap ( const std::vector & intervals) { std::map mapping; + + // Add a color for intervals without tags + mapping[""] = palette.next (); + for (auto& interval : intervals) { for (auto& tag : interval.tags ()) diff --git a/src/timew.h b/src/timew.h index dad9de6c..86770dd9 100644 --- a/src/timew.h +++ b/src/timew.h @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez. +// Copyright 2016 - 2019, Paul Beckingham, Federico Hernandez. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -68,7 +68,7 @@ void initializeExtensions (CLI&, const Rules&, Extensions&); int dispatchCommand (const CLI&, Database&, Rules&, const Extensions&); // helper.cpp -Color intervalColor (const Interval&, const Rules&, std::map &); +Color intervalColor (const std::set &, std::map &); Color tagColor (const Rules&, const std::string&); std::string intervalSummarize (Database&, const Rules&, const Interval&); bool expandIntervalHint (const std::string&, Range&);