[f-spot] Refactor image changing API in ImageView.



commit 74d4e4ae6168ad509807220583d865aabda07486
Author: Ruben Vermeersch <ruben savanne be>
Date:   Thu Jul 23 19:22:30 2009 +0200

    Refactor image changing API in ImageView.
    
    This commit changes the image changing API from property assignments
    (Pixbuf, PixbufOrientation) to two methods ChangeImage and
    ChangeOrientation.
    
    These allow to act differently depending on whether a completely new
    image is loaded, or just a new version (e.g. a higher resolution).

 src/Editors/Editor.cs    |    4 +-
 src/ImportCommand.cs     |    2 +-
 src/PhotoImageView.cs    |   32 +++++++++---------
 src/Widgets/ImageView.cs |   83 ++++++++++++++++++++++++++--------------------
 4 files changed, 66 insertions(+), 55 deletions(-)
---
diff --git a/src/Editors/Editor.cs b/src/Editors/Editor.cs
index 9542419..043d2ce 100644
--- a/src/Editors/Editor.cs
+++ b/src/Editors/Editor.cs
@@ -203,7 +203,7 @@ namespace FSpot.Editors {
 			}
 
 			Pixbuf previewed = ProcessFast (preview, null);
-			State.PhotoImageView.Pixbuf = previewed;
+			State.PhotoImageView.ChangeImage (previewed, State.PhotoImageView.PixbufOrientation, false);
 			State.PhotoImageView.ZoomFit (false);
 			MainWindow.Toplevel.InfoBox.UpdateHistogram (previewed);
 
@@ -235,7 +235,7 @@ namespace FSpot.Editors {
 
 		public void Restore () {
 			if (original != null && State.PhotoImageView != null) {
-				State.PhotoImageView.Pixbuf = original;
+				State.PhotoImageView.ChangeImage (original, state.PhotoImageView.PixbufOrientation, false);
 				State.PhotoImageView.ZoomFit (false);
 
 				MainWindow.Toplevel.InfoBox.UpdateHistogram (null);
diff --git a/src/ImportCommand.cs b/src/ImportCommand.cs
index 47f84a6..67be5a6 100644
--- a/src/ImportCommand.cs
+++ b/src/ImportCommand.cs
@@ -638,7 +638,7 @@ public class ImportCommand : GladeDialog
 		GtkUtil.ModifyColors (photo_scrolled);
 		GtkUtil.ModifyColors (photo_view);
 
-		photo_view.Pixbuf = GtkUtil.TryLoadIcon (FSpot.Global.IconTheme, "f-spot", 128, (Gtk.IconLookupFlags)0);
+		photo_view.ChangeImage (GtkUtil.TryLoadIcon (FSpot.Global.IconTheme, "f-spot", 128, (Gtk.IconLookupFlags)0), PixbufOrientation.TopLeft, true);
 		photo_view.ZoomFit (false);
 			
 		tag_entry = new FSpot.Widgets.TagEntry (MainWindow.Toplevel.Database.Tags, false);
diff --git a/src/PhotoImageView.cs b/src/PhotoImageView.cs
index 84d48b2..527d53d 100644
--- a/src/PhotoImageView.cs
+++ b/src/PhotoImageView.cs
@@ -195,12 +195,15 @@ namespace FSpot.Widgets {
 #region loader		
 		uint timer;
 		IImageLoader loader;
+		bool prepared_is_new;
+
 		void Load (Uri uri)
 		{
 			timer = Log.DebugTimerStart ();
 			if (loader != null)
 				loader.Dispose ();
 
+			prepared_is_new = true;
 			loader = ImageLoader.Create (uri);
 			loader.AreaPrepared += HandlePixbufPrepared;
 			loader.AreaUpdated += HandlePixbufAreaUpdated;
@@ -217,13 +220,15 @@ namespace FSpot.Widgets {
 			if (!ShowProgress)
 				return;
 
-			Gdk.Pixbuf prev = this.Pixbuf;
-			this.Pixbuf = loader.Pixbuf;
-			PixbufOrientation = Accelerometer.GetViewOrientation (loader.PixbufOrientation);
-			if (prev != null)
-				prev.Dispose ();
+			Gdk.Pixbuf prev = Pixbuf;
+			PixbufOrientation orientation = Accelerometer.GetViewOrientation (loader.PixbufOrientation);
+			ChangeImage (loader.Pixbuf, orientation, prepared_is_new);
+			prepared_is_new = false;
 
 			this.ZoomFit (args.ReducedResolution);
+
+			if (prev != null)
+				prev.Dispose ();
 		}
 
 		void HandlePixbufAreaUpdated (object sender, AreaUpdatedEventArgs args)
@@ -248,7 +253,7 @@ namespace FSpot.Widgets {
 
 			Pixbuf prev = this.Pixbuf;
 			if (Pixbuf != loader.Pixbuf)
-				Pixbuf = loader.Pixbuf;
+				ChangeImage (loader.Pixbuf, Accelerometer.GetViewOrientation (loader.PixbufOrientation), false);
 
 			if (Pixbuf == null) {
 				// FIXME: Do we have test cases for this ???
@@ -258,17 +263,12 @@ namespace FSpot.Widgets {
 				// than try to load the image one last time.
 				try {
 					Log.Warning ("Falling back to file loader");
-					Pixbuf = PhotoLoader.Load (item.Collection, item.Index);
+					ChangeImage (PhotoLoader.Load (item.Collection, item.Index), PixbufOrientation.TopLeft, true);
 				} catch (Exception e) {
 					LoadErrorImage (e);
 				}
 			}
 
-			if (loader.Pixbuf != null) //FIXME: this test in case the photo was loaded with the direct loader
-				PixbufOrientation = Accelerometer.GetViewOrientation (loader.PixbufOrientation);
-			else
-				PixbufOrientation = PixbufOrientation.TopLeft;
-
 			if (Pixbuf == null)
 				LoadErrorImage (null);
 			else
@@ -302,13 +302,13 @@ namespace FSpot.Widgets {
 			// like offer the user a chance to locate the moved file and
 			// update the db entry, but for now just set the error pixbuf	
 			Pixbuf old = Pixbuf;
-			Pixbuf = new Pixbuf (PixbufUtils.ErrorPixbuf, 0, 0, 
-					     PixbufUtils.ErrorPixbuf.Width, 
-					     PixbufUtils.ErrorPixbuf.Height);
+			Pixbuf err = new Pixbuf (PixbufUtils.ErrorPixbuf, 0, 0,
+									 PixbufUtils.ErrorPixbuf.Width,
+									 PixbufUtils.ErrorPixbuf.Height);
+			ChangeImage (err, PixbufOrientation.TopLeft, true);
 			if (old != null)
 				old.Dispose ();
 
-			PixbufOrientation = PixbufOrientation.TopLeft;
 			ZoomFit (false);
 		}
 
diff --git a/src/Widgets/ImageView.cs b/src/Widgets/ImageView.cs
index 2ba323c..7879191 100644
--- a/src/Widgets/ImageView.cs
+++ b/src/Widgets/ImageView.cs
@@ -40,38 +40,49 @@ namespace FSpot.Widgets
 		{
 		}
 
-		Pixbuf pixbuf;
-		public Pixbuf Pixbuf {
-			get { return pixbuf; } 
-			set {
-				if (pixbuf == value)
-					return;
+		// The is_new parameter denotes whether it's a newly loaded image
+		// (true, in that case the scrolling can be reset), or a new version of
+		// the currently loaded image (false, e.g. a higher resolution
+		// version).
+		public void ChangeImage (Pixbuf pixbuf, PixbufOrientation orientation, bool is_new)
+		{
+			if (Pixbuf == pixbuf)
+				return;
 
-				pixbuf = value;
-				min_zoom = ComputeMinZoom (upscale);
+			Pixbuf = pixbuf;
+			PixbufOrientation = orientation;
+
+			min_zoom = ComputeMinZoom (upscale);
+			ComputeScaledSize ();
 
-				ComputeScaledSize ();
+			if (is_new) {
 				AdjustmentsChanged -= ScrollToAdjustments;
 				Hadjustment.Value = Vadjustment.Value = 0;
 				XOffset = YOffset = 0;
 				AdjustmentsChanged += ScrollToAdjustments;
-				QueueDraw ();
-			} 
+			} else {
+				// TODO: Recalculate the adjustments and offsets such that the
+				// view on the image is maintained.
+			}
+
+			QueueDraw ();
 		}
 
-		PixbufOrientation pixbuf_orientation;
-		public PixbufOrientation PixbufOrientation {
-			get { return pixbuf_orientation; }
-			set {
-				if (value == pixbuf_orientation)
-					return;
-				pixbuf_orientation = value;
-				min_zoom = ComputeMinZoom (upscale);
-				ComputeScaledSize ();
-				QueueDraw ();
-			}
+		public void ChangeOrientation (PixbufOrientation orientation)
+		{
+			if (orientation == PixbufOrientation)
+				return;
+
+			PixbufOrientation = orientation;
+			min_zoom = ComputeMinZoom (upscale);
+			ComputeScaledSize ();
+			QueueDraw ();
 		}
 
+		public Pixbuf Pixbuf { get; private set; }
+
+		public PixbufOrientation PixbufOrientation { get; private set; }
+
 		CheckPattern check_pattern = CheckPattern.Dark;
 		public CheckPattern CheckPattern {
 			get { return check_pattern; } 
@@ -218,7 +229,7 @@ namespace FSpot.Widgets
 			win.X = Clamp (win.X - x_offset, 0, (int)scaled_width - 1);
 			win.Y = Clamp (win.Y - y_offset, 0, (int)scaled_height - 1);
 
-			win = PixbufUtils.TransformOrientation ((int)scaled_width, (int)scaled_height, win, PixbufUtils.ReverseTransformation (pixbuf_orientation));
+			win = PixbufUtils.TransformOrientation ((int)scaled_width, (int)scaled_height, win, PixbufUtils.ReverseTransformation (PixbufOrientation));
 
 			return  new Point ((int) Math.Floor (win.X * (double)(((int)PixbufOrientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) / (double)(scaled_width - 1) + .5),
 					   (int) Math.Floor (win.Y * (double)(((int)PixbufOrientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) / (double)(scaled_height - 1) + .5));
@@ -276,12 +287,12 @@ namespace FSpot.Widgets
 			if (this.Pixbuf == null)
 				return Point.Zero;
 
-			image = PixbufUtils.TransformOrientation (Pixbuf.Width, Pixbuf.Height, image, pixbuf_orientation);
+			image = PixbufUtils.TransformOrientation (Pixbuf.Width, Pixbuf.Height, image, PixbufOrientation);
 			int x_offset = scaled_width < Allocation.Width ? (int)(Allocation.Width - scaled_width) / 2 : -XOffset;
 			int y_offset = scaled_height < Allocation.Height ? (int)(Allocation.Height - scaled_height) / 2 : -YOffset;
 
-			return new Point ((int) Math.Floor (image.X * (double) (scaled_width - 1) / (((int)pixbuf_orientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) + 0.5) + x_offset,
-					  (int) Math.Floor (image.Y * (double) (scaled_height - 1) / (((int)pixbuf_orientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) + 0.5) + y_offset);
+			return new Point ((int) Math.Floor (image.X * (double) (scaled_width - 1) / (((int)PixbufOrientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) + 0.5) + x_offset,
+					  (int) Math.Floor (image.Y * (double) (scaled_height - 1) / (((int)PixbufOrientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) + 0.5) + y_offset);
 		}
 
 		protected Rectangle ImageCoordsToWindow (Rectangle image)
@@ -289,15 +300,15 @@ namespace FSpot.Widgets
 			if (this.Pixbuf == null)
 				return Gdk.Rectangle.Zero;
 
-			image = PixbufUtils.TransformOrientation (Pixbuf.Width, Pixbuf.Height, image, pixbuf_orientation);
+			image = PixbufUtils.TransformOrientation (Pixbuf.Width, Pixbuf.Height, image, PixbufOrientation);
 			int x_offset = scaled_width < Allocation.Width ? (int)(Allocation.Width - scaled_width) / 2 : -XOffset;
 			int y_offset = scaled_height < Allocation.Height ? (int)(Allocation.Height - scaled_height) / 2 : -YOffset;
 
 			Gdk.Rectangle win = Gdk.Rectangle.Zero;
-			win.X = (int) Math.Floor (image.X * (double) (scaled_width - 1) / (((int)pixbuf_orientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) + 0.5) + x_offset;
-			win.Y = (int) Math.Floor (image.Y * (double) (scaled_height - 1) / (((int)pixbuf_orientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) + 0.5) + y_offset;
-			win.Width = (int) Math.Floor ((image.X + image.Width) * (double) (scaled_width - 1) / (((int)pixbuf_orientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) + 0.5) - win.X + x_offset;
-			win.Height = (int) Math.Floor ((image.Y + image.Height) * (double) (scaled_height - 1) / (((int)pixbuf_orientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) + 0.5) - win.Y + y_offset;
+			win.X = (int) Math.Floor (image.X * (double) (scaled_width - 1) / (((int)PixbufOrientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) + 0.5) + x_offset;
+			win.Y = (int) Math.Floor (image.Y * (double) (scaled_height - 1) / (((int)PixbufOrientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) + 0.5) + y_offset;
+			win.Width = (int) Math.Floor ((image.X + image.Width) * (double) (scaled_width - 1) / (((int)PixbufOrientation <= 4 ? Pixbuf.Width : Pixbuf.Height) - 1) + 0.5) - win.X + x_offset;
+			win.Height = (int) Math.Floor ((image.Y + image.Height) * (double) (scaled_height - 1) / (((int)PixbufOrientation <= 4 ? Pixbuf.Height : Pixbuf.Width) - 1) + 0.5) - win.Y + y_offset;
 
 			return win;
 		}
@@ -707,7 +718,7 @@ namespace FSpot.Widgets
 			if (zoom == 1.0 &&
 			    !Pixbuf.HasAlpha &&
 			    Pixbuf.BitsPerSample == 8 &&
-			    pixbuf_orientation == PixbufOrientation.TopLeft) {
+			    PixbufOrientation == PixbufOrientation.TopLeft) {
 				GdkWindow.DrawPixbuf (Style.BlackGC,
 						      Pixbuf,
 						      area.X - x_offset, area.Y - y_offset,
@@ -724,7 +735,7 @@ namespace FSpot.Widgets
 												 (area.Y - y_offset),
 												 area.Width,
 												 area.Height),
-										  PixbufUtils.ReverseTransformation (pixbuf_orientation));
+										  PixbufUtils.ReverseTransformation (PixbufOrientation));
 			using (Pixbuf temp_pixbuf = new Pixbuf (Colorspace.Rgb, false, 8, pixbuf_area.Width, pixbuf_area.Height)) {
 				if (Pixbuf.HasAlpha)
 					temp_pixbuf.Fill (0x00000000);
@@ -741,7 +752,7 @@ namespace FSpot.Widgets
 
 				ApplyColorTransform (temp_pixbuf);
 
-				using (var dest_pixbuf = PixbufUtils.TransformOrientation (temp_pixbuf, pixbuf_orientation)) {
+				using (var dest_pixbuf = PixbufUtils.TransformOrientation (temp_pixbuf, PixbufOrientation)) {
 					GdkWindow.DrawPixbuf (Style.BlackGC,
 							      dest_pixbuf,
 							      0, 0,
@@ -761,7 +772,7 @@ namespace FSpot.Widgets
 			else {
 				double width;
 				double height;
-				if ((int)pixbuf_orientation <= 4 ) { //TopLeft, TopRight, BottomRight, BottomLeft
+				if ((int)PixbufOrientation <= 4 ) { //TopLeft, TopRight, BottomRight, BottomLeft
 					width = Pixbuf.Width;
 					height = Pixbuf.Height;
 				} else {			//LeftTop, RightTop, RightBottom, LeftBottom
@@ -829,7 +840,7 @@ namespace FSpot.Widgets
 
 			double width;
 			double height;
-			if ((int)pixbuf_orientation <= 4 ) { //TopLeft, TopRight, BottomRight, BottomLeft
+			if ((int)PixbufOrientation <= 4 ) { //TopLeft, TopRight, BottomRight, BottomLeft
 				width = Pixbuf.Width;
 				height = Pixbuf.Height;
 			} else {			//LeftTop, RightTop, RightBottom, LeftBottom



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