From a5a29f762d9513cad7373f19db86385a3d883ddc Mon Sep 17 00:00:00 2001 From: Mike Haben Date: Thu, 7 Apr 2022 14:09:32 +0100 Subject: [PATCH 1/4] Add Cairo dependency, and include Statistics plugin in the build --- meson.build | 1 + src/meson.build | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index c0c9929..b43a94d 100644 --- a/meson.build +++ b/meson.build @@ -10,6 +10,7 @@ add_project_arguments('-d:ENABLE_UNIX_SPECIFIC', language: 'cs') gtk_sharp_2_dep = dependency('gtk-sharp-2.0') glib_sharp_2_dep = dependency('glib-sharp-2.0') +cairo_dep = dependency('mono-cairo') subdir('src') subdir('data') diff --git a/src/meson.build b/src/meson.build index 5e51b67..34373e5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -232,8 +232,9 @@ gui_plugins_lib = library( 'gui/plugins/ProgressDisplayPlugin.cs', 'gui/plugins/SelectLayoutPlugin.cs', 'gui/plugins/SelectRangePlugin.cs', + 'gui/plugins/StatisticsPlugin.cs', ), - dependencies: gtk_sharp_2_dep, + dependencies: [gtk_sharp_2_dep, cairo_dep], cs_args: ['-r:Mono.Posix', '-nowarn:0169'], link_with: [ buffers_lib, From 85ca4afcfba0ca6273d9e3843441f84dfc1c1d6f Mon Sep 17 00:00:00 2001 From: Mike Haben Date: Thu, 7 Apr 2022 14:10:34 +0100 Subject: [PATCH 2/4] Various updates to the bar-chart drawing code --- src/gui/plugins/StatisticsPlugin.cs | 74 +++++++++++++++++++---------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/gui/plugins/StatisticsPlugin.cs b/src/gui/plugins/StatisticsPlugin.cs index 65428c6..e037f4c 100644 --- a/src/gui/plugins/StatisticsPlugin.cs +++ b/src/gui/plugins/StatisticsPlugin.cs @@ -20,8 +20,8 @@ */ using System; using System.Collections; -using Cairo; using Gtk; +using Cairo; using Bless.Tools; using Bless.Util; using Bless.Gui; @@ -139,10 +139,9 @@ public StatisticsWidget(DataBook db) info[dvd.View] = new StatisticsInfo(); } - sdw = new StatisticsDrawWidget(); - Preferences.Proxy.Subscribe("Tools.Statistics.Show", "stats2", new PreferencesChangedHandler(OnPreferencesChanged)); + sdw = new StatisticsDrawWidget(); this.Add(sdw); this.ShowAll(); } @@ -276,11 +275,31 @@ public class StatisticsDrawWidget: Gtk.DrawingArea int previousHighlight; int currentHighlight; + const double BARS_X_OFFSET = 0.1; + const double BARS_X_RHS_SPACE = 0.02; + const double BAR_WIDTH_FRACTIONAL = 1.0; + const double BAR_HEIGHT_SCALE_FRACTIONAL = 0.95; + + readonly Color BAR_NORMAL_COLOR = new Color(0.0, 0.0, 1.0); // blue + readonly Color BAR_HIGHLIGHT_COLOR = new Color(1.0, 0.0, 0.0); // red + readonly Color INTER_BAR_COLOR = new Color(1.0, 1.0, 1.0); // white + void DrawBar(Cairo.Context gr, int b) { - gr.MoveTo(barStart[b]); - gr.LineTo (barEnd[b]); - gr.Stroke(); + if ((b >= 0) && (b <= 255)) // prevent exception if user moves mouse-pointer to right of rightmost bar + { + //gr.MoveTo(barStart[b]); + //gr.LineTo (barEnd[b]); + //gr.Stroke(); + + gr.LineWidth = 0.1 / freqs.Length; + double width = 1.0 / freqs.Length * BAR_WIDTH_FRACTIONAL; + double height = (barEnd[b].Y - barStart[b].Y) * BAR_HEIGHT_SCALE_FRACTIONAL; + gr.Rectangle(barStart[b].X - ((0.5 / freqs.Length) * BAR_WIDTH_FRACTIONAL), barStart[b].Y, + width, height); + gr.FillRule = FillRule.Winding; // or FillRule.EvenOdd, makes no difference to red stripes issue + gr.Fill(); + } } void UpdateHighlight() @@ -293,20 +312,25 @@ void UpdateHighlight() win.GetGeometry(out x, out y, out w, out h, out d); g.Scale (w, h); - g.LineWidth = (1.0 / freqs.Length) * 0.6; + //g.LineWidth = (1.0 / freqs.Length) * BAR_WIDTH_FRACTIONAL; if (previousHighlight != -1) { - /*int start=previousHighlight-1; - int end=previousHighlight+1; - if (start<0) start=0; - if (end>=barStart.Length) end=barStart.Length-1;*/ - g.Color = new Color(0.0, 0.0, 0.0); - //for(int i=start; i<=end; i++) + g.SetSourceColor(BAR_NORMAL_COLOR); DrawBar(g, previousHighlight); } + // Brute-force fix for red-lines issue: redraw bars either side of previously-highlighted bar + if (previousHighlight > 0) { + g.SetSourceColor(BAR_NORMAL_COLOR); + DrawBar(g, (previousHighlight - 1)); + } + if (previousHighlight < 255) { + g.SetSourceColor(BAR_NORMAL_COLOR); + DrawBar(g, (previousHighlight + 1)); + } + if (currentHighlight != -1) { - g.Color = new Color(1.0, 0.0, 0.0); + g.SetSourceColor(BAR_HIGHLIGHT_COLOR); DrawBar(g, currentHighlight); } @@ -315,19 +339,19 @@ void UpdateHighlight() void Draw (Cairo.Context gr, int width, int height) { gr.Scale (width, height); - gr.Color = new Color(1.0, 1.0, 1.0); + gr.SetSourceColor(INTER_BAR_COLOR); gr.Rectangle(0.0, 0.0, 1.0, 1.0); gr.Stroke(); - gr.Color = new Color(0.0, 0.0, 0.0); + gr.SetSourceColor(BAR_NORMAL_COLOR); - gr.LineWidth = (1.0 / freqs.Length) * 0.6; + //gr.LineWidth = (1.0 / freqs.Length) * BAR_WIDTH_FRACTIONAL; for (int i = 0; i < freqs.Length; i++) { if (previousHighlight == i) - gr.Color = new Color(1.0, 0.0, 0.0); + gr.SetSourceColor(BAR_HIGHLIGHT_COLOR); DrawBar(gr, i); if (previousHighlight == i) - gr.Color = new Color(0.0, 0.0, 0.0); + gr.SetSourceColor(BAR_NORMAL_COLOR); } } @@ -383,7 +407,7 @@ public void Update(int[] freqs) void DoDrawingCalculations() { - freqWidth = (1.0 / freqs.Length); + freqWidth = ((1.0 - BARS_X_OFFSET - BARS_X_RHS_SPACE) / freqs.Length); int max = 0; @@ -392,11 +416,10 @@ void DoDrawingCalculations() } for (int i = 0; i < barStart.Length; i++) { - barStart[i].X = i * freqWidth; + barStart[i].X = (i * freqWidth) + BARS_X_OFFSET; barStart[i].Y = 1.0; barEnd[i].X = barStart[i].X; - barEnd[i].Y = 1.0 - ((double)freqs[i]) / max; - + barEnd[i].Y = 1.0 - ((double)freqs[i] / max); } } @@ -416,8 +439,9 @@ void OnMotionNotify(object o, MotionNotifyEventArgs args) } Gdk.Rectangle alloc = this.Allocation; //Console.WriteLine("x {0} freq {1} width{2}", x, freqWidth, alloc.Width); - currentHighlight = (int)((x / (freqWidth * alloc.Width))) + 1; - Console.WriteLine(currentHighlight); + currentHighlight = (int)((x / (freqWidth * alloc.Width)) - (BARS_X_OFFSET / freqWidth)) + 1; + //currentHighlight = (int)(((x / alloc.Width) - BARS_X_OFFSET) / freqWidth) + 1; + Console.WriteLine(currentHighlight); // debug output to console, TEMPORARY if (previousHighlight != currentHighlight) { UpdateHighlight(); From 4eedd6f5aaa87628acebeaf9393880f28a92dacb Mon Sep 17 00:00:00 2001 From: Mike Haben Date: Thu, 7 Apr 2022 16:38:36 +0100 Subject: [PATCH 3/4] Removed +1 selected-bin offset from OnMotionNotify() --- src/gui/plugins/StatisticsPlugin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/plugins/StatisticsPlugin.cs b/src/gui/plugins/StatisticsPlugin.cs index e037f4c..48c351a 100644 --- a/src/gui/plugins/StatisticsPlugin.cs +++ b/src/gui/plugins/StatisticsPlugin.cs @@ -439,7 +439,7 @@ void OnMotionNotify(object o, MotionNotifyEventArgs args) } Gdk.Rectangle alloc = this.Allocation; //Console.WriteLine("x {0} freq {1} width{2}", x, freqWidth, alloc.Width); - currentHighlight = (int)((x / (freqWidth * alloc.Width)) - (BARS_X_OFFSET / freqWidth)) + 1; + currentHighlight = (int)((x / (freqWidth * alloc.Width)) - (BARS_X_OFFSET / freqWidth)); //currentHighlight = (int)(((x / alloc.Width) - BARS_X_OFFSET) / freqWidth) + 1; Console.WriteLine(currentHighlight); // debug output to console, TEMPORARY From cdd6b5eb1f84c107d1cb38fd59ed56f84b244b8c Mon Sep 17 00:00:00 2001 From: Mike Haben Date: Thu, 7 Apr 2022 16:48:50 +0100 Subject: [PATCH 4/4] Added +0.5 offset in OnMotionNotify() - selects the bar that the mouse-pointer tip lies within the width of. --- src/gui/plugins/StatisticsPlugin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/plugins/StatisticsPlugin.cs b/src/gui/plugins/StatisticsPlugin.cs index 48c351a..c33b183 100644 --- a/src/gui/plugins/StatisticsPlugin.cs +++ b/src/gui/plugins/StatisticsPlugin.cs @@ -439,7 +439,7 @@ void OnMotionNotify(object o, MotionNotifyEventArgs args) } Gdk.Rectangle alloc = this.Allocation; //Console.WriteLine("x {0} freq {1} width{2}", x, freqWidth, alloc.Width); - currentHighlight = (int)((x / (freqWidth * alloc.Width)) - (BARS_X_OFFSET / freqWidth)); + currentHighlight = (int)((x / (freqWidth * alloc.Width)) - (BARS_X_OFFSET / freqWidth) + 0.5); //currentHighlight = (int)(((x / alloc.Width) - BARS_X_OFFSET) / freqWidth) + 1; Console.WriteLine(currentHighlight); // debug output to console, TEMPORARY