From e52cc5d7d8ec6e6567c7dc79fb06d1480aec58d4 Mon Sep 17 00:00:00 2001 From: Lynne Date: Thu, 3 Sep 2020 16:02:56 +1000 Subject: [PATCH] much nicer way of rendering the list view, with easier updates and such --- Listing.cs | 2 +- MainWindow.cs | 59 +++++++++++++++++++++++++++++++----- ui/main.glade | 82 +++++++++++++++++++++++++-------------------------- 3 files changed, 94 insertions(+), 49 deletions(-) diff --git a/Listing.cs b/Listing.cs index 652f758..0fffd15 100644 --- a/Listing.cs +++ b/Listing.cs @@ -6,7 +6,7 @@ namespace Buypeeb { public string url { get; set; } public string id { get; set; } public string name { get; set; } - public int price; + public int price = 0; public int win_price; public string original_name; public bool favourite { get; set; } = false; diff --git a/MainWindow.cs b/MainWindow.cs index 71c5ce2..9d2e81e 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -37,6 +37,7 @@ namespace Buypeeb { private ListStore items; private Settings settings; + private TreeView itemTreeView; static SemaphoreSlim tasklimit = new SemaphoreSlim(4); @@ -48,12 +49,23 @@ namespace Buypeeb { this.Title = "Buypeeb"; builder.Autoconnect(this); - this.items = (ListStore)builder.GetObject("ListItems"); + + // bind treeview columns to watchlist instead of needing to manually sync its liststore + this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems"); + this.items = new ListStore(typeof(Listing)); this.RenderList(); + this.itemTreeView.Model = this.items; + //TODO: make this less verbose + this.itemTreeView.Columns[ItemColumns.Name].SetCellDataFunc(this.itemTreeView.Columns[ItemColumns.Name].Cells[0], new Gtk.TreeCellDataFunc(this.RenderColumnName)); + this.itemTreeView.Columns[ItemColumns.PriceYen].SetCellDataFunc(this.itemTreeView.Columns[ItemColumns.PriceYen].Cells[0], new Gtk.TreeCellDataFunc(this.RenderColumnPriceYen)); + this.itemTreeView.Columns[ItemColumns.PriceAUD].SetCellDataFunc(this.itemTreeView.Columns[ItemColumns.PriceAUD].Cells[0], new Gtk.TreeCellDataFunc(this.RenderColumnPriceAUD)); + this.itemTreeView.Columns[ItemColumns.Ending].SetCellDataFunc(this.itemTreeView.Columns[ItemColumns.Ending].Cells[0], new Gtk.TreeCellDataFunc(this.RenderColumnEnding)); + foreach (object[] row in this.items) { - Console.WriteLine(row[ItemColumns.Name]); + Console.WriteLine(row[0]); } + DeleteEvent += Window_Shutdown; } @@ -73,8 +85,20 @@ namespace Buypeeb { item.ready = true; Console.WriteLine($"{id} updated."); Gtk.Application.Invoke(delegate { - // this.RenderListItem(id);\ - this.RenderList(); + //TODO: surely there's a better way to do this + TreeIter iter; + this.itemTreeView.Model.GetIterFirst(out iter); + + for (int i = 0; i < this.itemTreeView.Model.IterNChildren(); i++) { + var x = (Listing)this.itemTreeView.Model.GetValue(iter, 0); + if (x.id == id) { + this.items.EmitRowChanged(this.itemTreeView.Model.GetPath(iter), iter); + break; + } + else { + this.itemTreeView.Model.IterNext(ref iter); + } + } }); } @@ -114,9 +138,8 @@ namespace Buypeeb { private void RenderList() { this.items.Clear(); - foreach (KeyValuePair entry in settings.watchlist) { - string[] values = new[] { entry.Value.name, entry.Value.PriceJPY(), entry.Value.PriceAUD(), "whenever", entry.Value.id }; - this.items.AppendValues(values); + foreach (var item in this.settings.watchlist) { + items.AppendValues(item.Value); } } @@ -188,5 +211,27 @@ namespace Buypeeb { Console.WriteLine("ButtonSelectedRenameClicked"); } + // column renderers + + private void RenderColumnName(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { + Listing item = (Listing)model.GetValue(iter, 0); + (cell as Gtk.CellRendererText).Text = item.name ?? "Loading..."; + } + + private void RenderColumnPriceYen(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { + Listing item = (Listing)model.GetValue(iter, 0); + (cell as Gtk.CellRendererText).Text = item.PriceJPY(); + } + + private void RenderColumnPriceAUD(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { + Listing item = (Listing)model.GetValue(iter, 0); + (cell as Gtk.CellRendererText).Text = item.PriceAUD(); + } + + private void RenderColumnEnding(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { + Listing item = (Listing)model.GetValue(iter, 0); + (cell as Gtk.CellRendererText).Text = "whatever"; + } + } } diff --git a/ui/main.glade b/ui/main.glade index 989f681..96bcef8 100644 --- a/ui/main.glade +++ b/ui/main.glade @@ -1,19 +1,19 @@ - + - + - + - + - + - + @@ -55,8 +55,8 @@ Add new True gtk-add - - + + False @@ -71,8 +71,8 @@ Update all True gtk-refresh - - + + False @@ -97,7 +97,7 @@ Undo True gtk-undo - + False @@ -112,7 +112,7 @@ Redo True gtk-redo - + False @@ -137,7 +137,7 @@ Clear ended True gtk-clear - + False @@ -152,7 +152,7 @@ Clear all True gtk-delete - + False @@ -177,7 +177,7 @@ Open True gtk-open - + False @@ -192,8 +192,8 @@ Save True gtk-save - - + + False @@ -208,7 +208,7 @@ Export as... True gtk-save-as - + False @@ -233,7 +233,7 @@ Help True gtk-help - + False @@ -248,7 +248,7 @@ Settings True gtk-preferences - + False @@ -263,8 +263,8 @@ Quit True gtk-quit - - + + False @@ -287,7 +287,7 @@ in 200 - + True True ListItems @@ -296,7 +296,7 @@ True - + @@ -308,7 +308,7 @@ True True - + 0 @@ -320,7 +320,7 @@ True Price (¥) - + 1 @@ -332,7 +332,7 @@ True Price (AUD) - + 2 @@ -345,7 +345,7 @@ Ending at True - + 3 @@ -385,8 +385,8 @@ 40 3 - - + + @@ -597,22 +597,22 @@ - + - + - + - + - + - + @@ -647,7 +647,7 @@ True True True - + False @@ -661,7 +661,7 @@ True True True - + False @@ -682,7 +682,7 @@ True True Remove - + True @@ -704,7 +704,7 @@ True True Rename - + True @@ -829,7 +829,7 @@ - +