Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics plugin bar chart April 2022 #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
74 changes: 49 additions & 25 deletions src/gui/plugins/StatisticsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
using System;
using System.Collections;
using Cairo;
using Gtk;
using Cairo;
using Bless.Tools;
using Bless.Util;
using Bless.Gui;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}

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

}
Expand Down Expand Up @@ -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;

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

}
Expand All @@ -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) + 0.5);
//currentHighlight = (int)(((x / alloc.Width) - BARS_X_OFFSET) / freqWidth) + 1;
Console.WriteLine(currentHighlight); // debug output to console, TEMPORARY

if (previousHighlight != currentHighlight) {
UpdateHighlight();
Expand Down
3 changes: 2 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down