Re: view sorted chronologically, ascending



Hi list,

In attachment you'll find an improved version of my previous sort
order patch.  This one stores the current "view by" mode (ie by
directory or by month), the sort order and the position of the "glass"
on the bar and reads them when starting up f-spot.

That way f-spot starts up showing pictures from the same month or
directory as when you last quit f-spot.

Comments welcome,
Thomas
Index: src/DirectoryAdaptor.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/DirectoryAdaptor.cs,v
retrieving revision 1.14
diff -u -w -r1.14 DirectoryAdaptor.cs
--- src/DirectoryAdaptor.cs	24 Oct 2005 22:11:27 -0000	1.14
+++ src/DirectoryAdaptor.cs	14 Jan 2006 16:04:58 -0000
@@ -6,6 +6,19 @@
 		public PhotoQuery query;
 		System.Collections.DictionaryEntry [] dirs;
 
+		private bool order_ascending = true;
+		public override bool OrderAscending {
+			get {
+				return order_ascending;
+			}
+			set {
+				if (order_ascending != value) {
+					order_ascending = value;
+					Reload();
+				}
+			}
+		}
+
 		// FIXME store the Photo.Id list here not just the count
 		private class Group : IComparer {
 			public int Count = 1;
@@ -98,6 +111,11 @@
 			Array.Sort (dirs, new DirectoryAdaptor.Group ());
 			Array.Sort (query.Photos, new Photo.CompareDirectory ());
 			
+			if (!order_ascending) {
+				Array.Reverse (dirs);
+				Array.Reverse (query.Photos);
+			}
+			
 			if (Changed != null)
 				Changed (this);
 		}
@@ -114,6 +132,23 @@
 			}
 			
 			// FIXME not truly implemented
+			return 0;
+		}
+
+		public override FSpot.IBrowsableItem PhotoFromIndex (int item) 
+		{
+			return query.Items [LookupItem (item)];
+		}
+
+		private int LookupItem (int group)
+		{
+			int i = 0;
+			while (i < query.Count) {
+				if (((Photo)(query [i])).DirectoryPath == (string)dirs [group].Key) {
+					return i;
+				}
+				i++;
+			}
 			return 0;
 		}
 
Index: src/GroupAdaptor.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/GroupAdaptor.cs,v
retrieving revision 1.8
diff -u -w -r1.8 GroupAdaptor.cs
--- src/GroupAdaptor.cs	24 Oct 2005 22:11:27 -0000	1.8
+++ src/GroupAdaptor.cs	14 Jan 2006 16:04:59 -0000
@@ -6,6 +6,8 @@
 	}
 	
 	public abstract class GroupAdaptor {
+		public abstract bool OrderAscending {get; set;}
+		
 		public abstract int Value (int item) ;
 		public abstract int Count ();
 		public abstract string TickLabel (int item);
@@ -15,6 +17,7 @@
 
 		public abstract void SetGlass (int item);
 		public abstract int IndexFromPhoto (FSpot.IBrowsableItem photo);
+		public abstract FSpot.IBrowsableItem PhotoFromIndex (int item);
 
 		public delegate void GlassSetHandler (GroupAdaptor adaptor, int index);
 		public virtual event GlassSetHandler GlassSet;
Index: src/GroupSelector.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/GroupSelector.cs,v
retrieving revision 1.57
diff -u -w -r1.57 GroupSelector.cs
--- src/GroupSelector.cs	19 Dec 2005 04:44:49 -0000	1.57
+++ src/GroupSelector.cs	14 Jan 2006 16:05:01 -0000
@@ -69,6 +69,12 @@
 			}
 		}
 
+		public int GlassPosition {
+			get {
+				return glass.Position;
+			}
+		}
+
 		private void HandleAdaptorChanged (GroupAdaptor adaptor)
 		{
 			bool size_changed = box_counts.Length != adaptor.Count ();
@@ -274,6 +280,9 @@
 
 		protected override bool OnButtonPressEvent (Gdk.EventButton args)
 		{
+			if (args.Button == 3)
+				return DrawOrderMenu (args); 
+
 			double x = args.X + action.X;
 			double y = args.Y + action.Y;
 
@@ -484,6 +493,31 @@
 			if (tick.Intersect (area, out area)) {
 				GdkWindow.DrawRectangle (Style.ForegroundGC (State), true, area);
 			}
+		}
+
+		private bool DrawOrderMenu (Gdk.EventButton args)
+		{
+			Gtk.Menu order_menu = new Gtk.Menu();
+
+			GtkUtil.MakeCheckMenuItem (order_menu, Mono.Posix.Catalog.GetString ("Order Ascending"),
+					      new EventHandler (HandleSetAscending), true, adaptor.OrderAscending);
+
+			GtkUtil.MakeCheckMenuItem (order_menu, Mono.Posix.Catalog.GetString ("Order Descending"),
+					      new EventHandler (HandleSetDescending), true, !adaptor.OrderAscending);
+
+			order_menu.Popup (null, null, null, args.Button, args.Time);
+
+			return base.OnButtonPressEvent (args);
+		}
+
+		private void HandleSetAscending (object sender, EventArgs args)
+		{
+			adaptor.OrderAscending = true;
+		}
+
+		private void HandleSetDescending (object sender, EventArgs args)
+		{
+			adaptor.OrderAscending = false;
 		}
 
 		public abstract class Manipulator {
Index: src/MainWindow.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/MainWindow.cs,v
retrieving revision 1.262
diff -u -w -r1.262 MainWindow.cs
--- src/MainWindow.cs	11 Jan 2006 09:14:40 -0000	1.262
+++ src/MainWindow.cs	14 Jan 2006 16:05:04 -0000
@@ -75,6 +75,9 @@
 	[Glade.Widget] MenuItem zoom_in;
 	[Glade.Widget] MenuItem zoom_out;
 
+	[Glade.Widget] RadioMenuItem month;
+	[Glade.Widget] RadioMenuItem directory;
+
 	// Find
 	[Glade.Widget] MenuItem find_tag;
 	[Glade.Widget] CheckMenuItem find_untagged;
@@ -111,6 +114,8 @@
 	FSpot.FullScreenView fsview;
 	FSpot.PhotoQuery query;
 	FSpot.GroupSelector group_selector;
+	FSpot.TimeAdaptor time_adaptor;
+	FSpot.DirectoryAdaptor directory_adaptor;
 	MainSelection selection;
 	
 	FSpot.Delay slide_delay;
@@ -240,9 +245,10 @@
 #endif
 
 		group_selector = new FSpot.GroupSelector ();
-		FSpot.GroupAdaptor adaptor = new FSpot.TimeAdaptor (query);
+		directory_adaptor = new FSpot.DirectoryAdaptor (query);
+		time_adaptor = new FSpot.TimeAdaptor (query);
+		group_selector.Adaptor = time_adaptor;
 
-		group_selector.Adaptor  = adaptor;
 		group_selector.ShowAll ();
 		
 		if (zoom_scale != null) {
@@ -320,7 +326,10 @@
 		photo_view.DragDataReceived += HandlePhotoViewDragDataReceived;
 
 		view_notebook.SwitchPage += HandleViewNotebookSwitchPage;
-		adaptor.GlassSet += HandleAdaptorGlassSet;
+		group_selector.Adaptor.GlassSet += HandleAdaptorGlassSet;
+		group_selector.Adaptor.Changed += HandleAdaptorChanged;
+		LoadPreference (Preferences.GROUP_ADAPTOR);
+		LoadPreference (Preferences.GROUP_ADAPTOR_ORDER_ASC);
 
 		this.selection = new MainSelection (this);
 		this.selection.Changed += HandleSelectionChanged;
@@ -852,6 +861,11 @@
 		JumpTo (index);
 	}
 
+	void HandleAdaptorChanged (FSpot.GroupAdaptor sender)
+	{
+		UpdateGlass ();
+	}
+
 	/*
 	 * Keep the glass temporal slider in sync with the user's scrolling in the icon_view
 	 */
@@ -862,7 +876,7 @@
 			return;
 
 		int cell_num = icon_view.TopLeftVisibleCell();
-		if (cell_num == -1 || cell_num == lastTopLeftCell)
+		if (cell_num == -1 /*|| cell_num == lastTopLeftCell*/)
 			return;
 
 		lastTopLeftCell = cell_num;
@@ -888,7 +902,7 @@
 
 	void HandleIconViewReady (object sender, EventArgs args)
 	{
-		LoadPreference (Preferences.ICON_VIEW_POSITION);
+		LoadPreference (Preferences.GLASS_POSITION);
 
 		// We only want to set the position the first time
 		// the icon_view is ready (eg on startup)
@@ -1402,19 +1416,23 @@
 	void HandleArrangeByTime (object sender, EventArgs args)
 	{
 		group_selector.Adaptor.GlassSet -= HandleAdaptorGlassSet;
-		FSpot.GroupAdaptor adaptor = new FSpot.TimeAdaptor (query);
-		group_selector.Adaptor = adaptor;
+		group_selector.Adaptor.Changed -= HandleAdaptorChanged;
+		group_selector.Adaptor = time_adaptor;
+		time_adaptor.Reload ();
 		group_selector.Mode = FSpot.GroupSelector.RangeType.Min;
-		adaptor.GlassSet += HandleAdaptorGlassSet;
+		group_selector.Adaptor.GlassSet += HandleAdaptorGlassSet;
+		group_selector.Adaptor.Changed += HandleAdaptorChanged;
 	}
 
 	void HandleArrangeByDirectory (object sender, EventArgs args)
 	{
 		group_selector.Adaptor.GlassSet -= HandleAdaptorGlassSet;
-		FSpot.GroupAdaptor adaptor = new FSpot.DirectoryAdaptor (query);		
-		group_selector.Adaptor = adaptor;
+		group_selector.Adaptor.Changed -= HandleAdaptorChanged;
+		group_selector.Adaptor = directory_adaptor;
+		directory_adaptor.Reload ();
 		group_selector.Mode = FSpot.GroupSelector.RangeType.Min;
-		adaptor.GlassSet += HandleAdaptorGlassSet;
+		group_selector.Adaptor.GlassSet += HandleAdaptorGlassSet;
+		group_selector.Adaptor.Changed += HandleAdaptorChanged;
 	}
 
 	// Called when the user clicks the X button	
@@ -1451,11 +1469,13 @@
 		Preferences.Set (Preferences.SHOW_TAGS,			icon_view.DisplayTags);
 		Preferences.Set (Preferences.SHOW_DATES,		icon_view.DisplayDates);
 
+		Preferences.Set (Preferences.GROUP_ADAPTOR,		(group_selector.Adaptor is DirectoryAdaptor) ? 1 : 0);
+		Preferences.Set (Preferences.GROUP_ADAPTOR_ORDER_ASC,   group_selector.Adaptor.OrderAscending);
+		Preferences.Set (Preferences.GLASS_POSITION,		group_selector.GlassPosition);
+		
 		Preferences.Set (Preferences.SIDEBAR_POSITION,		main_hpaned.Position);
 		Preferences.Set (Preferences.ZOOM,			icon_view.Zoom);
 	
-		Preferences.Set (Preferences.ICON_VIEW_POSITION, icon_view.TopLeftVisibleCell ());
-		
 		tag_selection_widget.SaveExpandDefaults ();
 
 		this.Window.Destroy ();
@@ -2219,6 +2239,21 @@
 				//display_dates_menu_item.Toggle ();
 			break;
 		
+		case Preferences.GROUP_ADAPTOR:
+			if ((int) val == 1) {				
+				directory.Active = true;
+			}
+			break;
+
+		case Preferences.GROUP_ADAPTOR_ORDER_ASC:
+			group_selector.Adaptor.OrderAscending = (bool) val;
+			break;
+
+		case Preferences.GLASS_POSITION:
+			IBrowsableItem photo = group_selector.Adaptor.PhotoFromIndex ((int) val);
+			JumpTo (query.IndexOf (photo));
+			break;
+			
 		case Preferences.SIDEBAR_POSITION:
 			if (main_hpaned.Position != (int) val)
 				main_hpaned.Position = (int) val;
@@ -2228,13 +2263,6 @@
 			icon_view.Zoom = (double) val;
 			break;
 		
-		case Preferences.ICON_VIEW_POSITION:
-			if (icon_view.TopLeftVisibleCell () != (int) val) {
-				icon_view.FocusCell = (int) val;
-				photo_view.Item.Index = (int) val;
-				icon_view.ScrollTo ((int) val, false);
-			}
-			break;
 		case Preferences.METADATA_EMBED_IN_IMAGE:
 			write_metadata = (bool) val;
 			break;
Index: src/Preferences.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/Preferences.cs,v
retrieving revision 1.4
diff -u -w -r1.4 Preferences.cs
--- src/Preferences.cs	11 Dec 2005 01:48:48 -0000	1.4
+++ src/Preferences.cs	14 Jan 2006 16:05:04 -0000
@@ -20,9 +20,12 @@
 		public const string SHOW_DATES = "/apps/f-spot/ui/show_dates";
 		public const string EXPANDED_TAGS = "/apps/f-spot/ui/expanded_tags";
 		
+		public const string GLASS_POSITION = "/apps/f-spot/ui/glass_position";
+		public const string GROUP_ADAPTOR = "/apps/f-spot/ui/group_adaptor";
+		public const string GROUP_ADAPTOR_ORDER_ASC = "/apps/f-spot/ui/group_adaptor_sort_asc";
+		
 		public const string SIDEBAR_POSITION = "/apps/f-spot/ui/sidebar_size";
 		public const string ZOOM = "/apps/f-spot/ui/zoom";
-		public const string ICON_VIEW_POSITION = "/apps/f-spot/ui/icon_view_position";
 
 		public const string EXPORT_FLICKR_SCALE = "/apps/f-spot/export/flickr/scale";
 		public const string EXPORT_FLICKR_SIZE = "/apps/f-spot/export/flickr/size";
@@ -68,6 +71,11 @@
 			case MAIN_WINDOW_MAXIMIZED:
 				return false;
 
+			case GROUP_ADAPTOR:
+			case GLASS_POSITION:
+			case GROUP_ADAPTOR_ORDER_ASC:
+				return null;
+
 			case SHOW_TOOLBAR:
 			case SHOW_SIDEBAR:
 			case SHOW_TIMELINE:
@@ -77,7 +85,6 @@
 		
 			case SIDEBAR_POSITION:
 			case ZOOM:
-			case ICON_VIEW_POSITION:
 				return null;
 			}
 
Index: src/TimeAdaptor.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/TimeAdaptor.cs,v
retrieving revision 1.26
diff -u -w -r1.26 TimeAdaptor.cs
--- src/TimeAdaptor.cs	19 Dec 2005 04:35:17 -0000	1.26
+++ src/TimeAdaptor.cs	14 Jan 2006 16:05:05 -0000
@@ -4,6 +4,19 @@
 namespace FSpot {
 	public class TimeAdaptor : GroupAdaptor, FSpot.ILimitable {
 		public PhotoQuery query;
+		private bool order_ascending = false;
+		public override bool OrderAscending {
+			get {
+				return order_ascending;
+			}
+			set {
+				if (value != order_ascending) {
+					order_ascending = value;
+					Reload();
+				}
+			}
+		
+		}
 
 		ArrayList years = new ArrayList ();
 		struct YearData {
@@ -22,7 +35,26 @@
 
 		public int LookupItem (System.DateTime date)
 		{
+			if (order_ascending) 
+				return LookUpItemAscending (date);
+			
+			return LookUpItemDescending (date);
+		}
+
+		private int LookUpItemAscending (System.DateTime date)
+		{
 			int i = 0;
+
+			while (i < query.Count && query [i].Time < date)
+				i++;
+
+			return i;
+		}
+
+		private int LookUpItemDescending (System.DateTime date)
+		{
+			int i = 0;
+
 			while (i < query.Count && query [i].Time > date)
 				i++;
 
@@ -63,7 +95,7 @@
 		{
 			DateTime start = DateFromIndex (item);
 			
-			if (start.Month == 12)
+			if ((start.Month == 12 && !order_ascending) || (start.Month == 1 && order_ascending))
 				return start.Year.ToString ();
 			else 
 				return null;
@@ -81,6 +113,22 @@
 			item = Math.Max (item, 0);
 			item = Math.Min (years.Count * 12 - 1, item);
 			
+			if (order_ascending)
+				return DateFromIndexAscending (item);
+
+			return DateFromIndexDescending (item);
+		}
+
+		private DateTime DateFromIndexAscending (int item)
+		{
+			int year = (int)((YearData)years [item / 12]).Year;
+			int month = 1 + (item % 12);
+
+			return new DateTime(year, month, 1);
+		}
+
+		private DateTime DateFromIndexDescending (int item)
+		{
 			int year =  (int)((YearData)years [item / 12]).Year;
 			int month = 12 - (item % 12);
 			
@@ -89,6 +137,34 @@
 		
 		public override int IndexFromPhoto (FSpot.IBrowsableItem photo) 
 		{
+			if (order_ascending)
+			       return IndexFromPhotoAscending (photo);
+
+			return IndexFromPhotoDescending (photo);	
+		}
+
+		private int IndexFromPhotoAscending (FSpot.IBrowsableItem photo)
+		{
+			int year = photo.Time.Year;
+			int max_year = ((YearData)years [years.Count - 1]).Year;
+			int min_year = ((YearData)years [0]).Year;
+
+			if (year < min_year || year > max_year) {
+				Console.WriteLine("TimeAdaptor.IndexFromPhoto year out of range[{1},{2}]: {0}", year, min_year, max_year);
+				return 0;
+			}
+
+			int index = photo.Time.Month - 1;
+
+			for (int i = 0 ; i < years.Count; i++)
+				if (year > ((YearData)years[i]).Year)
+					index += 12;
+			
+			return index;						
+		}
+
+		private int IndexFromPhotoDescending (FSpot.IBrowsableItem photo)
+		{
 			int year = photo.Time.Year;
 			int max_year = ((YearData)years [0]).Year;
 			int min_year = ((YearData)years [years.Count - 1]).Year;
@@ -109,6 +185,13 @@
 			return index;
 		}
 
+		public override FSpot.IBrowsableItem PhotoFromIndex (int item)
+	       	{
+			DateTime start = DateFromIndex (item);
+			return query.Items [LookupItem (start)];
+		
+		}
+
 		private void HandleChanged (IBrowsableCollection sender)
 		{
 			Console.WriteLine ("Reloading");
@@ -123,8 +206,12 @@
 
 			Photo [] photos = query.Store.Query (null, null);
 			Array.Sort (query.Photos);
+			Array.Sort (photos);
+
+			if (!order_ascending) {				
 			Array.Reverse (query.Photos);
 			Array.Reverse (photos);
+			}
 
 			if (photos.Length > 0) {
 				YearData data = new YearData ();
@@ -139,6 +226,9 @@
 						years.Add (data);
 						//Console.WriteLine ("Found Year {0}", current);
 					}
+					if (order_ascending)
+						data.Months [photo.Time.Month - 1] += 1;
+					else
 					data.Months [12 - photo.Time.Month] += 1;
 				}
 			} else {
Index: src/Util.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/Util.cs,v
retrieving revision 1.20
diff -u -w -r1.20 Util.cs
--- src/Util.cs	4 Dec 2005 19:03:53 -0000	1.20
+++ src/Util.cs	14 Jan 2006 16:05:05 -0000
@@ -217,6 +217,19 @@
 		i.Show ();
 	}
 
+	public static void MakeCheckMenuItem (Gtk.Menu menu, string label, EventHandler e, bool enabled, bool active)
+	{
+		Gtk.CheckMenuItem i = new Gtk.CheckMenuItem (label);
+		i.Activated += e;
+		i.Sensitive = enabled;
+		i.DrawAsRadio = true;
+		i.Active = active;
+
+		menu.Append(i);
+		i.Show ();
+
+	}
+
 	public static void MakeMenuSeparator (Gtk.Menu menu)
 	{
 		Gtk.SeparatorMenuItem i = new Gtk.SeparatorMenuItem ();


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