[longomatch] Handle multiple groups in the event types buttons



commit 8fb9beb92dcab67cf3d441b047ce34077b949ef2
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sun Oct 5 22:35:25 2014 +0200

    Handle multiple groups in the event types buttons

 .../Interfaces/Drawing/IDrawingToolkit.cs          |    1 +
 LongoMatch.Core/Store/EventType.cs                 |    7 +
 LongoMatch.Drawing.Cairo/CairoBackend.cs           |   18 +-
 LongoMatch.Drawing/CanvasObjects/CategoryObject.cs |  244 ++++++++++++-------
 4 files changed, 173 insertions(+), 97 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs 
b/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
index 052926c..1e15403 100644
--- a/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
+++ b/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
@@ -73,6 +73,7 @@ namespace LongoMatch.Core.Interfaces.Drawing
                void Save (ICanvas canvas, double width, double height, string filename);
                Image Copy (ICanvas canvas, double width, double height);
                Area UserToDevice (Area area);
+               void Invoke (EventHandler handler);
        }
 }
 
diff --git a/LongoMatch.Core/Store/EventType.cs b/LongoMatch.Core/Store/EventType.cs
index d937e23..c838912 100644
--- a/LongoMatch.Core/Store/EventType.cs
+++ b/LongoMatch.Core/Store/EventType.cs
@@ -16,6 +16,7 @@
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 //
 using System;
+using System.Linq;
 using System.Collections.Generic;
 using LongoMatch.Core.Common;
 using Mono.Unix;
@@ -119,6 +120,12 @@ namespace LongoMatch.Core.Store
                        get;
                        set;
                }
+
+               public Dictionary<string, List<Tag>> TagsByGroup {
+                       get {
+                               return Tags.GroupBy (t => t.Group).ToDictionary (g => g.Key, g => g.ToList());
+                       }
+               }
        }
 
        [Serializable]
diff --git a/LongoMatch.Drawing.Cairo/CairoBackend.cs b/LongoMatch.Drawing.Cairo/CairoBackend.cs
index fe67556..650180c 100644
--- a/LongoMatch.Drawing.Cairo/CairoBackend.cs
+++ b/LongoMatch.Drawing.Cairo/CairoBackend.cs
@@ -17,17 +17,17 @@
 //
 using System;
 using Cairo;
+using Gdk;
 using LongoMatch.Core.Common;
 using LongoMatch.Core.Interfaces.Drawing;
+using Pango;
+using Color = LongoMatch.Core.Common.Color;
+using FontAlignment = LongoMatch.Core.Common.FontAlignment;
 using FontSlant = LongoMatch.Core.Common.FontSlant;
 using FontWeight = LongoMatch.Core.Common.FontWeight;
-using FontAlignment = LongoMatch.Core.Common.FontAlignment;
-using Color = LongoMatch.Core.Common.Color;
 using Image = LongoMatch.Core.Common.Image;
 using LineStyle = LongoMatch.Core.Common.LineStyle;
 using Point = LongoMatch.Core.Common.Point;
-using Gdk;
-using Pango;
 
 namespace LongoMatch.Drawing.Cairo
 {
@@ -118,7 +118,7 @@ namespace LongoMatch.Drawing.Cairo
                                }
                        }
                }
-               
+
                public FontAlignment FontAlignment {
                        set {
                                switch (value) {
@@ -378,7 +378,7 @@ namespace LongoMatch.Drawing.Cairo
                                String.Format ("{0} {1}px", FontFamily, FontSize));
                        layout.FontDescription.Weight = fWeight;
                        layout.FontDescription.Style = fSlant;
-                       layout.Width = Pango.Units.FromPixels ((int) width);
+                       layout.Width = Pango.Units.FromPixels ((int)width);
                        layout.Alignment = fAlignment;
                        layout.SetMarkup (GLib.Markup.EscapeText (text));
                        SetColor (StrokeColor);
@@ -386,7 +386,7 @@ namespace LongoMatch.Drawing.Cairo
                        layout.GetPixelExtents (out inkRect, out logRect);
                        CContext.MoveTo (point.X, point.Y + height / 2 - (double)logRect.Height / 2);
                        Pango.CairoHelper.ShowLayout (CContext, layout);
-                       CContext.NewPath();
+                       CContext.NewPath ();
                }
 
                public void DrawImage (Image image)
@@ -543,6 +543,10 @@ namespace LongoMatch.Drawing.Cairo
                                CContext.SetSourceRGBA (0, 0, 0, 0);
                        }
                }
+
+               public void Invoke (EventHandler handler) {
+                       Gtk.Application.Invoke (handler);
+               }
        }
 }
 
diff --git a/LongoMatch.Drawing/CanvasObjects/CategoryObject.cs 
b/LongoMatch.Drawing/CanvasObjects/CategoryObject.cs
index 7a297be..97cdb27 100644
--- a/LongoMatch.Drawing/CanvasObjects/CategoryObject.cs
+++ b/LongoMatch.Drawing/CanvasObjects/CategoryObject.cs
@@ -21,6 +21,7 @@ using LongoMatch.Core.Common;
 using LongoMatch.Core.Interfaces.Drawing;
 using LongoMatch.Core.Store;
 using LongoMatch.Core.Store.Drawables;
+using Mono.Unix;
 
 namespace LongoMatch.Drawing.CanvasObjects
 {
@@ -28,7 +29,11 @@ namespace LongoMatch.Drawing.CanvasObjects
        {
 
                Dictionary <Rectangle, object> rects;
-               bool catSelected;
+               Dictionary <string, List<Tag>> tagsByGroup;
+               bool catSelected, tagSelected;
+               int nrows;
+               const int TIMEOUT_MS = 800;
+               System.Threading.Timer timer;
 
                public CategoryObject (AnalysisEventButton category): base (category)
                {
@@ -37,6 +42,15 @@ namespace LongoMatch.Drawing.CanvasObjects
                        SelectedTags = new List<Tag> ();
                }
 
+               protected override void Dispose (bool disposing)
+               {
+                       if (timer != null) {
+                               timer.Dispose ();
+                               timer = null;
+                       }
+                       base.Dispose (disposing);
+               }
+
                public AnalysisEventButton Button {
                        get;
                        set;
@@ -53,31 +67,76 @@ namespace LongoMatch.Drawing.CanvasObjects
                        }
                }
 
-               public override int NRows {
-                       get {
-                               /* Header */
-                               int rows = 1;
-                               int tagsPerRow = Math.Max (1, Button.TagsPerRow);
+               void UpdateRows ()
+               {
+                       /* Header */
+                       int tagsPerRow = Math.Max (1, Button.TagsPerRow);
+                       nrows = 1;
 
-                               /* Recorder */
-                               if (Button.TagMode == TagMode.Free) {
-                                       rows ++;
-                               }
-                               rows += (int)Math.Ceiling ((float)TagsCount / tagsPerRow);
-                               return rows;
+                       /* Recorder */
+                       if (Button.TagMode == TagMode.Free) {
+                               nrows ++;
+                       }
+                       foreach (List<Tag> tags in tagsByGroup.Values) {
+                               nrows += (int)Math.Ceiling ((float)tags.Count / tagsPerRow);
+                       }
+                       if (Mode == TagMode.Edit) {
+                               nrows ++;
                        }
                }
 
-               int TagsCount {
-                       get {
-                               int tagsCount = Button.AnalysisEventType.Tags.Count;
-                               if (Mode == TagMode.Edit) {
-                                       tagsCount ++;
+               void TimerCallback (Object state)
+               {
+                       Config.DrawingToolkit.Invoke (delegate {
+                               EmitClickEvent ();
+                               tagSelected = false;
+                               catSelected = false;
+                               SelectedTags.Clear ();
+                       });
+               }
+
+               void DelayTagClicked ()
+               {
+                       if (tagsByGroup.Keys.Count == 1) {
+                               TimerCallback (null);
+                       }
+                       if (timer == null) {
+                               timer = new System.Threading.Timer (TimerCallback, null, TIMEOUT_MS, 0);
+                       } else {
+                               timer.Change (TIMEOUT_MS, 0);
+                       } 
+               }
+
+               void CategoryClicked (AnalysisEventButton category)
+               {
+                       if (Button.TagMode == TagMode.Predefined) {
+                               catSelected = true;
+                       }
+               }
+
+               void TagClicked (Tag tag)
+               {
+                       if (SelectedTags.Contains (tag)) {
+                               SelectedTags.Remove (tag);
+                       } else {
+                               SelectedTags.Add (tag);
+                               if (Button.TagMode == TagMode.Predefined) {
+                                       catSelected = true;
+                                       tagSelected = true;
                                }
-                               return tagsCount;
                        }
                }
 
+               void RecordClicked ()
+               {
+               }
+
+               void UpdateGroups ()
+               {
+                       tagsByGroup = Button.AnalysisEventType.TagsByGroup;
+                       
+               }
+
                public List<Tag> SelectedTags {
                        get;
                        set;
@@ -109,7 +168,7 @@ namespace LongoMatch.Drawing.CanvasObjects
                                Selection subsel = rect.GetSelection (p, 0);
                                if (subsel != null) {
                                        if (rects [rect] is AnalysisEventButton) {
-                                               CategoryClicked (rects [rect] as AnalysisEventButton);
+                                               CategoryClicked (Button);
                                        } else if (rects [rect] is Tag) {
                                                TagClicked (rects [rect] as Tag);
                                        } else {
@@ -122,66 +181,40 @@ namespace LongoMatch.Drawing.CanvasObjects
 
                public override void ClickReleased ()
                {
-                       if (catSelected) {
+                       if (catSelected && !tagSelected) {
                                EmitClickEvent ();
                                SelectedTags.Clear ();
                                catSelected = false;
+                       } else if (tagSelected) {
+                               DelayTagClicked ();
                        }
                }
 
-               public override void Draw (IDrawingToolkit tk, Area area)
+               void DrawTagsGroup (IDrawingToolkit tk, double catWidth, double heightPerRow, List<Tag> tags, 
ref double yptr)
                {
-                       Point position;
-                       double heightPerRow, catWidth, rowwidth, yptr = 0;
-                       int tagsPerRow, tagsCount, row = 0;
+                       double rowwidth;
+                       int tagsPerRow, row = 0;
 
-                       rects.Clear ();
-                       position = Button.Position;
-                       heightPerRow = Button.Height / NRows;
-                       catWidth = Button.Width;
-                       tagsCount = TagsCount;
                        tagsPerRow = Math.Max (1, Button.TagsPerRow);
                        rowwidth = catWidth / tagsPerRow;
 
-                       if (!UpdateDrawArea (tk, area, new Area (Position, Width, Height))) {
-                               return;
-                       }
-
-                       tk.Begin ();
-                       tk.FontWeight = FontWeight.Bold;
-
-                       /* Draw Rectangle */
-                       DrawButton (tk);
-                       DrawImage (tk);
-
-                       /* Draw header */
-                       tk.FillColor = LongoMatch.Core.Common.Color.Grey2;
-                       tk.LineWidth = 2;
-                       if (catSelected) {
-                               tk.StrokeColor = Button.DarkColor;
-                       } else {
-                               tk.StrokeColor = Button.TextColor;
-                       }
-                       tk.DrawText (position, catWidth, heightPerRow, Button.EventType.Name);
-                       rects.Add (new Rectangle (position, catWidth, heightPerRow), Button);
-                       yptr += heightPerRow;
-
                        /* Draw tags */
-                       for (int i=0; i < tagsCount; i++) {
+                       for (int i=0; i < tags.Count; i++) {
                                Point pos;
                                int col;
                                Tag tag;
 
                                row = i / tagsPerRow;
                                col = i % tagsPerRow;
-                               pos = new Point (position.X + col * rowwidth,
-                                                            position.Y + yptr + row * heightPerRow);
+                               pos = new Point (Button.Position.X + col * rowwidth,
+                                                Button.Position.Y + yptr + row * heightPerRow);
 
                                if (col == 0) {
-                                       if (i + tagsPerRow > tagsCount) {
-                                               rowwidth = catWidth / (tagsCount - i);
+                                       if (i + tagsPerRow > tags.Count) {
+                                               rowwidth = catWidth / (tags.Count - i);
                                        }
                                }
+
                                tk.StrokeColor = Button.DarkColor;
                                tk.LineWidth = 1;
                                if (col == 0) {
@@ -192,54 +225,85 @@ namespace LongoMatch.Drawing.CanvasObjects
                                        tk.DrawLine (pos, new Point (pos.X, pos.Y + heightPerRow));
                                }
                                tk.StrokeColor = Button.TextColor;
-                               if (i < Button.AnalysisEventType.Tags.Count) {
-                                       tag = Button.AnalysisEventType.Tags [i];
-                                       if (Mode == TagMode.Edit || !SelectedTags.Contains (tag)) {
-                                               tk.DrawText (pos, rowwidth, heightPerRow, tag.Value);
-                                       } else {
-                                               tk.StrokeColor = Button.DarkColor;
-                                               tk.DrawText (pos, rowwidth, heightPerRow, tag.Value);
-                                       }
+                               tag = tags [i];
+                               if (Mode == TagMode.Edit || !SelectedTags.Contains (tag)) {
+                                       tk.DrawText (pos, rowwidth, heightPerRow, tag.Value);
                                } else {
-                                       tag = AddTag;
-                                       tk.DrawText (pos, rowwidth, heightPerRow, "Add");
+                                       tk.StrokeColor = Button.DarkColor;
+                                       tk.DrawText (pos, rowwidth, heightPerRow, tag.Value);
                                }
                                rects.Add (new Rectangle (pos, rowwidth, heightPerRow), tag);
                        }
                        yptr += heightPerRow * (row + 1);
-
-                       if (Button.TagMode == TagMode.Free) {
-                               /* Draw Tagger */
-                               tk.DrawLine (new Point (position.X, position.Y + yptr),
-                                                        new Point (position.X + catWidth, position.Y + 
yptr));
-                               tk.DrawText (new Point (position.X, position.Y + yptr), catWidth, 
heightPerRow, "Record");
-                       }
-                       DrawSelectionArea (tk);
-                       tk.End ();
                }
 
-               void CategoryClicked (AnalysisEventButton category)
+               void DrawEditButton (IDrawingToolkit tk, double catWidth, double heightPerRow, ref double 
yptr)
                {
-                       if (Button.TagMode == TagMode.Predefined) {
-                               catSelected = true;
+                       Point start;
+                       Tag tag = AddTag;
+
+                       if (Mode != TagMode.Edit) {
+                               return;
                        }
+                       tk.StrokeColor = Button.DarkColor;
+                       tk.LineWidth = 1;
+                       start = new Point (Button.Position.X, Button.Position.Y + yptr);
+                       tk.DrawLine (start, new Point (start.X + catWidth, start.Y));
+                       tk.StrokeColor = Button.TextColor;
+                       tk.DrawText (start, catWidth, heightPerRow, Catalog.GetString ("Edit"));
+                       rects.Add (new Rectangle (start, catWidth, heightPerRow), tag);
+                       yptr += heightPerRow;
                }
 
-               void TagClicked (Tag tag)
+               public override void Draw (IDrawingToolkit tk, Area area)
                {
-                       if (SelectedTags.Contains (tag)) {
-                               SelectedTags.Remove (tag);
+                       Point pos;
+                       double catWidth, heightPerRow, yptr = 0;
+
+                       rects.Clear ();
+                       UpdateGroups ();
+                       UpdateRows ();
+                       heightPerRow = Button.Height / nrows;
+                       catWidth = Button.Width;
+                       pos = Button.Position;
+
+                       if (!UpdateDrawArea (tk, area, new Area (Position, Width, Height))) {
+                               return;
+                       }
+
+                       tk.Begin ();
+                       tk.FontWeight = FontWeight.Bold;
+
+                       /* Draw Rectangle */
+                       DrawButton (tk);
+                       DrawImage (tk);
+
+                       /* Draw header */
+                       tk.FillColor = LongoMatch.Core.Common.Color.Grey2;
+                       tk.LineWidth = 2;
+                       if (catSelected) {
+                               tk.StrokeColor = Button.DarkColor;
                        } else {
-                               SelectedTags.Clear ();
-                               SelectedTags.Add (tag);
-                               if (Button.TagMode == TagMode.Predefined) {
-                                       catSelected = true;
-                               }
+                               tk.StrokeColor = Button.TextColor;
                        }
-               }
+                       tk.DrawText (pos, catWidth, heightPerRow, Button.EventType.Name);
+                       rects.Add (new Rectangle (pos, catWidth, heightPerRow), Button);
+                       yptr += heightPerRow;
 
-               void RecordClicked ()
-               {
+                       foreach (List<Tag> tags in tagsByGroup.Values) {
+                               DrawTagsGroup (tk, catWidth, heightPerRow, tags, ref yptr);
+                       }
+
+                       DrawEditButton (tk, catWidth, heightPerRow, ref yptr);
+
+                       if (Button.TagMode == TagMode.Free) {
+                               /* Draw Tagger */
+                               tk.DrawLine (new Point (pos.X, pos.Y + yptr),
+                                            new Point (pos.X + catWidth, pos.Y + yptr));
+                               tk.DrawText (new Point (pos.X, pos.Y + yptr), catWidth, heightPerRow, 
"Record");
+                       }
+                       DrawSelectionArea (tk);
+                       tk.End ();
                }
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]