[blam/gtk3] Store paths instead of iters



commit 2200312a96e1fd6cd11347b11aa06cd9e94f6c56
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Wed Dec 11 20:45:23 2013 +0100

    Store paths instead of iters
    
    Iters become invalid pretty quickly

 src/Channel.cs      |    4 ++--
 src/ChannelGroup.cs |   10 +---------
 src/ChannelList.cs  |   47 +++++++++++++++++++++++++++++++----------------
 3 files changed, 34 insertions(+), 27 deletions(-)
---
diff --git a/src/Channel.cs b/src/Channel.cs
index 19abb59..ed33c4b 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -31,7 +31,7 @@ namespace Imendio.Blam {
         string Name {get; set; }
         string Url {get; set; }
         IEnumerable<string> ItemList { get; }
-        Gtk.TreeIter Iter {get; set; }
+        Gtk.TreePath Path {get; set; }
         bool MarkAsRead();
         Item GetItem(string id);
         void Setup();
@@ -126,7 +126,7 @@ namespace Imendio.Blam {
                }
 
                [XmlIgnore]
-               public Gtk.TreeIter Iter { get; set; }
+               public Gtk.TreePath Path { get; set; }
 
                public Channel()
                {
diff --git a/src/ChannelGroup.cs b/src/ChannelGroup.cs
index d9ebda1..858e713 100644
--- a/src/ChannelGroup.cs
+++ b/src/ChannelGroup.cs
@@ -22,7 +22,6 @@ namespace Imendio.Blam
         [XmlAttribute("Name")] public string Int_Name = null;
         [XmlAttribute("Url")] public string Int_Url = null;
         [XmlElement("Channel", typeof(Channel))] public List<Channel> Channels;
-        private Gtk.TreeIter mIter;
 
                public event PropertyChangedEventHandler PropertyChanged;
                public event NotifyCollectionChangedEventHandler CollectionChanged;
@@ -69,14 +68,7 @@ namespace Imendio.Blam
         }
 
         [XmlIgnore]
-        public Gtk.TreeIter Iter {
-            get {
-                return mIter;
-            }
-            set {
-                mIter = value;
-            }
-        }
+               public Gtk.TreePath Path { get; set; }
 
                public void OnCollectionChanged(NotifyCollectionChangedAction action, Channel channel)
                {
diff --git a/src/ChannelList.cs b/src/ChannelList.cs
index 12f7eab..82b484a 100644
--- a/src/ChannelList.cs
+++ b/src/ChannelList.cs
@@ -140,11 +140,12 @@ namespace Blam
 
                        additions.Subscribe(chan => {
                                Gtk.TreeIter iter = model.AppendValues(chan);
-                               chan.Iter = iter;
+                               chan.Path = model.GetPath(iter);
                        });
 
                        removals.Subscribe(chan => {
-                               var iter = chan.Iter;
+                               TreeIter iter;
+                               model.GetIter(out iter, chan.Path);
                                model.Remove(ref iter);
                        });
 
@@ -152,21 +153,29 @@ namespace Blam
                                .Merge(channels.Select(c => 
Observable.FromEventPattern<PropertyChangedEventArgs>(c, "PropertyChanged")))
                                .Merge(Observable.Merge(groups.Select(grp => 
Observable.FromEventPattern<PropertyChangedEventArgs>(grp, "PropertyChanged"))))
                                .Merge(Observable.Merge(groups.SelectMany(grp => grp.Channels.Select(c => 
Observable.FromEventPattern<PropertyChangedEventArgs>(c, "PropertyChanged")))))
-                               .Select(x => ((IChannel)x.Sender).Iter);
+                               .Select(x => ((IChannel)x.Sender).Path);
 
-                       chanChanges.Subscribe(iter => model.EmitRowChanged(model.GetPath(iter), iter));
+                       chanChanges.Subscribe(path => {
+                               TreeIter iter;
+                               model.GetIter(out iter, path);
+                               model.EmitRowChanged(path, iter);
+                       });
 
                        foreach (Channel chan in channels) {
-                               chan.Iter = model.AppendValues(chan);
+                               var iter = model.AppendValues(chan);
+                               chan.Path = model.GetPath(iter);
                        }
 
                        foreach (ChannelGroup group in groups) {
                                var iter = model.AppendValues(group);
-                               group.Iter = iter;
+                               group.Path = model.GetPath(iter);
                                //SetDragDestRow(Model.GetPath(iter), TreeViewDropPosition.IntoOrAfter);
 
                                foreach(IChannel chan in group.Channels) {
-                                       chan.Iter = model.AppendValues(group.Iter, chan);
+                                       TreeIter parentIter;
+                                       model.GetIter(out parentIter, group.Path);
+                                       var chanIter = model.AppendValues(parentIter, chan);
+                                       chan.Path = model.GetPath(chanIter);
                                }
                        }
 
@@ -377,7 +386,7 @@ namespace Imendio.Blam {
                        TreeViewDropPosition pos;
                        TreeIter iter, tmp_iter;
                        IChannel src = GetSelected();
-                       tmp_iter = src.Iter;
+                       Model.GetIter(out tmp_iter, src.Path);
                        GetDestRowAtPos(args.X, args.Y, out path, out pos);
                        Model.GetIter(out iter, path);
                        IChannel dst = Model.GetValue(iter, 0) as IChannel;
@@ -388,7 +397,7 @@ namespace Imendio.Blam {
 
                        /* Not directly over a channel or group. */
                        if(pos == TreeViewDropPosition.After || pos == TreeViewDropPosition.Before){
-                               var src_path = Model.GetPath(src.Iter).Copy();
+                               var src_path = src.Path.Copy();
                                // both at the top level, don't do anything
                                if (path.Depth == 1 && src_path.Depth == 1)
                                        return;
@@ -426,7 +435,7 @@ namespace Imendio.Blam {
                                        return;
                                }
                                // on top of a channel
-                               var src_path = Model.GetPath(src.Iter).Copy();
+                               var src_path = src.Path.Copy();
                                if (path.Depth == 1 && src_path.Depth == 1)
                                        return;
                                if (path.Depth == 1 && src_path.Depth > 1) {
@@ -450,13 +459,17 @@ namespace Imendio.Blam {
 
         public void Add (IChannel channel)
         {
-            channel.Iter = (this.Model as TreeStore).AppendValues(channel);
+            var iter = (this.Model as TreeStore).AppendValues(channel);
+                       channel.Path = Model.GetPath(iter);
                        channel.PropertyChanged += ChannelChanged;
         }
 
                public void AddToGroup(IChannel group, IChannel channel)
                {
-                       channel.Iter = (Model as TreeStore).AppendValues(group.Iter, channel);
+                       TreeIter parentIter;
+                       Model.GetIter(out parentIter, group.Path);
+                       var iter = (Model as TreeStore).AppendValues(parentIter, channel);
+                       channel.Path = Model.GetPath(iter);
                        channel.PropertyChanged += ChannelChanged;
                }
 
@@ -464,12 +477,14 @@ namespace Imendio.Blam {
         {
             TreeIter iter = (this.Model as TreeStore).AppendValues(channel);
             ChannelGroup group = channel as ChannelGroup;
-            group.Iter = iter;
-                       SetDragDestRow(Model.GetPath(iter), TreeViewDropPosition.IntoOrAfter);
+                       group.Path = Model.GetPath(iter);
+                       SetDragDestRow(group.Path, TreeViewDropPosition.IntoOrAfter);
 
             foreach(IChannel chan in group.Channels){
-                iter = (this.Model as TreeStore).AppendValues(group.Iter, chan);
-                chan.Iter = iter;
+                               TreeIter parentIter;
+                               Model.GetIter(out parentIter, group.Path);
+                iter = (this.Model as TreeStore).AppendValues(parentIter, chan);
+                               chan.Path = Model.GetPath(iter);
             }
         }
 


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