[mapguide-commits] r6894 - in branches/2.4/MgDev/Desktop: MapViewer MapViewer.Desktop SampleExtension

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Wed Jul 11 08:39:01 PDT 2012


Author: jng
Date: 2012-07-11 08:39:01 -0700 (Wed, 11 Jul 2012)
New Revision: 6894

Added:
   branches/2.4/MgDev/Desktop/MapViewer/MgTransientMapState.cs
Modified:
   branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgDesktopMapViewerProvider.cs
   branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgdTransientMapState.cs
   branches/2.4/MgDev/Desktop/MapViewer/MapViewer.csproj
   branches/2.4/MgDev/Desktop/MapViewer/MgMapViewerProvider.cs
   branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.Designer.cs
   branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.cs
   branches/2.4/MgDev/Desktop/SampleExtension/CustomOutput.cs
Log:
mg-desktop updates:
 - Refactor MgdTransientMapState to be an abstract base class created by MgMapViewerProvider, with a MapViewer.Desktop specific concrete subclass.
 - Add a convenience RenderMap method to MgMapViewerProvider
 - This allows us to use the new transient state class on the MgPrintComponent so that we can (print/print preview) off of the current map with custom display parameters instead of using a clipboard copy of the current map view.
 - This also allows us to modify the Print UI to allow the user to specify a custom DPI

Modified: branches/2.4/MgDev/Desktop/MapViewer/MapViewer.csproj
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer/MapViewer.csproj	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/MapViewer/MapViewer.csproj	2012-07-11 15:39:01 UTC (rev 6894)
@@ -182,6 +182,7 @@
     <Compile Include="MgThemeControlImpl.Designer.cs">
       <DependentUpon>MgThemeControlImpl.cs</DependentUpon>
     </Compile>
+    <Compile Include="MgTransientMapState.cs" />
     <Compile Include="MgViewerOptionsComponent.cs">
       <SubType>Component</SubType>
     </Compile>

Modified: branches/2.4/MgDev/Desktop/MapViewer/MgMapViewerProvider.cs
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer/MgMapViewerProvider.cs	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/MapViewer/MgMapViewerProvider.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -282,6 +282,7 @@
         protected abstract MgSpatialContextReader GetSpatialContexts(MgLayerBase layer, bool activeOnly);
 
         public abstract MgMapBase CreateMap(MgResourceIdentifier mapDefinitionId, string name);
+        public abstract MgByteReader RenderMap(MgSelectionBase selection, string format);
         public abstract MgByteReader RenderDynamicOverlay(MgSelectionBase selection, MgViewerRenderingOptions args);
         public abstract void SetDisplaySize(int width, int height);
         public abstract MgSelectionBase CreateSelectionForMap();
@@ -303,6 +304,13 @@
 
         public abstract bool IsLayerPotentiallyVisibleAtScale(MgLayerBase layer, bool bConsiderParentGroupVisibility);
 
+        public abstract MgTransientMapState CreateTransientState(MgMapBase map);
+
+        public MgTransientMapState CreateTransientState()
+        {
+            return CreateTransientState(_map);
+        }
+
         public string GetGeometryProperty(string objId)
         {
             return _layerGeomProps[objId];

Modified: branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.Designer.cs
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.Designer.cs	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.Designer.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -37,6 +37,9 @@
             this.btnPrintPreview = new System.Windows.Forms.Button();
             this.btnPrint = new System.Windows.Forms.Button();
             this.printDialog = new System.Windows.Forms.PrintDialog();
+            this.numDPI = new System.Windows.Forms.NumericUpDown();
+            this.label2 = new System.Windows.Forms.Label();
+            ((System.ComponentModel.ISupportInitialize)(this.numDPI)).BeginInit();
             this.SuspendLayout();
             // 
             // label1
@@ -60,7 +63,7 @@
             // chkScale
             // 
             this.chkScale.AutoSize = true;
-            this.chkScale.Location = new System.Drawing.Point(17, 58);
+            this.chkScale.Location = new System.Drawing.Point(17, 103);
             this.chkScale.Name = "chkScale";
             this.chkScale.Size = new System.Drawing.Size(83, 17);
             this.chkScale.TabIndex = 2;
@@ -87,7 +90,7 @@
             // 
             // btnPrintPreview
             // 
-            this.btnPrintPreview.Location = new System.Drawing.Point(17, 92);
+            this.btnPrintPreview.Location = new System.Drawing.Point(17, 126);
             this.btnPrintPreview.Name = "btnPrintPreview";
             this.btnPrintPreview.Size = new System.Drawing.Size(83, 23);
             this.btnPrintPreview.TabIndex = 3;
@@ -97,7 +100,7 @@
             // 
             // btnPrint
             // 
-            this.btnPrint.Location = new System.Drawing.Point(106, 92);
+            this.btnPrint.Location = new System.Drawing.Point(106, 126);
             this.btnPrint.Name = "btnPrint";
             this.btnPrint.Size = new System.Drawing.Size(75, 23);
             this.btnPrint.TabIndex = 4;
@@ -112,17 +115,46 @@
             this.printDialog.ShowHelp = true;
             this.printDialog.UseEXDialog = true;
             // 
+            // numDPI
+            // 
+            this.numDPI.Location = new System.Drawing.Point(17, 77);
+            this.numDPI.Maximum = new decimal(new int[] {
+            1410065407,
+            2,
+            0,
+            0});
+            this.numDPI.Name = "numDPI";
+            this.numDPI.Size = new System.Drawing.Size(120, 20);
+            this.numDPI.TabIndex = 5;
+            this.numDPI.Value = new decimal(new int[] {
+            96,
+            0,
+            0,
+            0});
+            // 
+            // label2
+            // 
+            this.label2.AutoSize = true;
+            this.label2.Location = new System.Drawing.Point(14, 61);
+            this.label2.Name = "label2";
+            this.label2.Size = new System.Drawing.Size(25, 13);
+            this.label2.TabIndex = 6;
+            this.label2.Text = "DPI";
+            // 
             // MgPrintControlImpl
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.Controls.Add(this.label2);
+            this.Controls.Add(this.numDPI);
             this.Controls.Add(this.btnPrint);
             this.Controls.Add(this.btnPrintPreview);
             this.Controls.Add(this.chkScale);
             this.Controls.Add(this.txtTitle);
             this.Controls.Add(this.label1);
             this.Name = "MgPrintControlImpl";
-            this.Size = new System.Drawing.Size(236, 133);
+            this.Size = new System.Drawing.Size(236, 164);
+            ((System.ComponentModel.ISupportInitialize)(this.numDPI)).EndInit();
             this.ResumeLayout(false);
             this.PerformLayout();
 
@@ -138,5 +170,7 @@
         private System.Windows.Forms.Button btnPrintPreview;
         private System.Windows.Forms.Button btnPrint;
         private System.Windows.Forms.PrintDialog printDialog;
+        private System.Windows.Forms.NumericUpDown numDPI;
+        private System.Windows.Forms.Label label2;
     }
 }

Modified: branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.cs
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.cs	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/MapViewer/MgPrintControlImpl.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -49,6 +49,11 @@
 
         private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
         {
+            MgMapViewerProvider provider = _viewer.GetProvider();
+            MgSelectionBase selection = _viewer.GetSelection();
+            MgMapBase map = _viewer.GetMap();
+            MgPoint pt = map.ViewCenter;
+            MgCoordinate coord = pt.Coordinate;
             float leftMargin = e.MarginBounds.Left;
             float topMargin = e.MarginBounds.Top;
             float yPos = topMargin;
@@ -65,27 +70,23 @@
             var scaleStr = string.Format("{0} 1 : {1}", Properties.Resources.TextScale, _viewer.GetMap().ViewScale);
             var sizeScale = e.Graphics.MeasureString(scaleStr, scaleFont);
 
-            //Draw map
-            var img = _viewer.GetCurrentImage();
-            var imgWidth = (float)img.Width;
-            var imgHeight = (float)img.Height;
-
             //Scale to fit within this page
-            //TODO: We should probably re-render at the desired size 
-            var workableImgHeight = pageHeight - (sizeTitle.Height * 1.1f) - (sizeScale.Height * 1.1f);
-            var workableImgWidth = e.MarginBounds.Width;
-            var hratio = 1.0f;
-            var wratio = 1.0f;
-            if (imgHeight > workableImgHeight)
-                hratio = imgHeight / workableImgHeight;
-            if (imgWidth > workableImgWidth)
-                wratio = imgWidth / workableImgWidth;
+            var imgHeight = (int)(pageHeight - (sizeTitle.Height * 1.1f) - (sizeScale.Height * 1.1f));
+            var imgWidth = e.MarginBounds.Width;
 
-            //Multiply by this ratio
-            imgWidth *= (1.0f / wratio);
-            imgHeight *= (1.0f / hratio);
+            using (var state = provider.CreateTransientState())
+            {
+                var tempState = new MgMapDisplayParameters(coord.X, coord.Y, map.ViewScale, imgWidth, imgHeight, (int)numDPI.Value);
+                state.PushState(tempState);
+                MgByteReader br = provider.RenderMap(selection, "PNG");
+                using (MgReadOnlyStream stream = new MgReadOnlyStream(br))
+                {
+                    Image img = Image.FromStream(stream);
+                    e.Graphics.DrawImage(img, leftMargin, yPos, imgWidth, imgHeight);
+                    img.Dispose();
+                }
+            }
 
-            e.Graphics.DrawImage(img, leftMargin, yPos, imgWidth, imgHeight);
             yPos += imgHeight;
 
             //Render scale

Added: branches/2.4/MgDev/Desktop/MapViewer/MgTransientMapState.cs
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer/MgTransientMapState.cs	                        (rev 0)
+++ branches/2.4/MgDev/Desktop/MapViewer/MgTransientMapState.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -0,0 +1,129 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace OSGeo.MapGuide.Viewer
+{
+    public abstract class MgTransientMapState : IDisposable
+    {
+        private Stack<MgdMapStateTransition> _states;
+
+        protected MgMapDisplayParameters _origState;
+
+        protected MgTransientMapState()
+        {
+            _states = new Stack<MgdMapStateTransition>();
+        }
+
+        protected abstract MgMapDisplayParameters GetCurrentState();
+
+        protected abstract void ApplyViewCenter(double x, double y);
+        protected abstract void ApplyViewScale(double scale);
+        protected abstract void ApplyDisplaySize(int width, int height);
+        protected abstract void ApplyDPI(int dpi);
+
+        private void ApplyState(MgMapDisplayParameters state)
+        {
+            ApplyViewCenter(state.X, state.Y);
+            ApplyViewScale(state.Scale);
+            ApplyDisplaySize(state.Width, state.Height);
+            if (state.DPI.HasValue)
+                ApplyDPI(state.DPI.Value);
+        }
+
+        public int Depth { get { return _states.Count; } }
+
+        /// <summary>
+        /// Pushes the given state onto the map state stack. The map takes on the display parameters specified
+        /// in this given state.
+        /// </summary>
+        /// <param name="state"></param>
+        public void PushState(MgMapDisplayParameters state)
+        {
+            if (state == null)
+                throw new ArgumentNullException("state");
+
+            var oldState = GetCurrentState();
+            ApplyState(state);
+
+            _states.Push(new MgdMapStateTransition() { OldState = oldState, NewState = state });
+        }
+
+        /// <summary>
+        /// Pops the latest state from the map state stack. The map state is restored to
+        /// the previously applied state.
+        /// </summary>
+        /// <returns>The state that was previously applied</returns>
+        public MgMapDisplayParameters PopState()
+        {
+            if (_states.Count == 0)
+                return null;
+
+            var trans = _states.Pop();
+            ApplyState(trans.OldState);
+            return trans.NewState;
+        }
+
+        public void Dispose()
+        {
+            while (_states.Count > 0)
+            {
+                this.PopState();
+            }
+            ApplyState(_origState);
+        }
+    }
+
+    public abstract class MgTransientMapState<T> : MgTransientMapState where T : MgMapBase
+    {
+        protected T _map;
+
+        protected MgTransientMapState(T map)
+        {
+            _map = map;
+            _origState = GetCurrentState();
+        }
+
+        protected override MgMapDisplayParameters GetCurrentState()
+        {
+            var pt = _map.ViewCenter;
+            var coord = pt.Coordinate;
+            return new MgMapDisplayParameters(coord.X, coord.Y, _map.ViewScale, _map.DisplayWidth, _map.DisplayHeight, _map.DisplayDpi);
+        }
+    }
+
+    internal class MgdMapStateTransition
+    {
+        public MgMapDisplayParameters NewState { get; set; }
+
+        public MgMapDisplayParameters OldState { get; set; }
+    }
+
+    /// <summary>
+    /// Represents display parameters for a map
+    /// </summary>
+    public class MgMapDisplayParameters
+    {
+        public double X { get; private set; }
+        public double Y { get; private set; }
+        public double Scale { get; private set; }
+        public int Width { get; private set; }
+        public int Height { get; private set; }
+        public int? DPI { get; private set; }
+
+        public MgMapDisplayParameters(double x, double y, double scale, int width, int height)
+        {
+            this.X = x;
+            this.Y = y;
+            this.Scale = scale;
+            this.Width = width;
+            this.Height = height;
+        }
+
+        public MgMapDisplayParameters(double x, double y, double scale, int width, int height, int dpi)
+            : this(x, y, scale, width, height)
+        {
+            this.DPI = dpi;
+        }
+    }
+}

Modified: branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgDesktopMapViewerProvider.cs
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgDesktopMapViewerProvider.cs	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgDesktopMapViewerProvider.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -149,5 +149,15 @@
         {
             return ((MgdLayer)layer).GetSpatialContexts(activeOnly);
         }
+
+        public override MgTransientMapState CreateTransientState(MgMapBase map)
+        {
+            return new MgdTransientMapState((MgdMap)map);
+        }
+
+        public override MgByteReader RenderMap(MgSelectionBase selection, string format)
+        {
+            return _renderSvc.RenderMap(_implMap, (MgdSelection)selection, format);
+        }
     }
 }

Modified: branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgdTransientMapState.cs
===================================================================
--- branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgdTransientMapState.cs	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/MapViewer.Desktop/MgdTransientMapState.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -10,112 +10,28 @@
     /// of this stack, allowing your map to be used for <see cref="T:OSGeo.MapGuide.MgdRenderingService.RenderMap()"/>
     /// calls without permanently altering state.
     /// </summary>
-    public class MgdTransientMapState : IDisposable
+    public class MgdTransientMapState : MgTransientMapState<MgdMap>
     {
-        private MgdMap _map;
+        internal MgdTransientMapState(MgdMap map) : base(map) { } 
 
-        private Stack<MgdMapStateTransition> _states;
-
-        private MgdMapDisplayParameters _origState;
-
-        public MgdTransientMapState(MgdMap map)
+        protected override void ApplyViewCenter(double x, double y)
         {
-            _map = map;
-            _origState = GetCurrentState();
-            _states = new Stack<MgdMapStateTransition>();
+            _map.SetViewCenterXY(x, y);
         }
 
-        private MgdMapDisplayParameters GetCurrentState()
+        protected override void ApplyViewScale(double scale)
         {
-            var pt = _map.ViewCenter;
-            var coord = pt.Coordinate;
-            return new MgdMapDisplayParameters(coord.X, coord.Y, _map.ViewScale, _map.DisplayWidth, _map.DisplayHeight, _map.DisplayDpi);
+            _map.SetViewScale(scale);
         }
 
-        private void ApplyState(MgdMapDisplayParameters state)
+        protected override void ApplyDisplaySize(int width, int height)
         {
-            _map.SetViewCenterXY(state.X, state.Y);
-            _map.SetViewScale(state.Scale);
-            _map.SetDisplaySize(state.Width, state.Height);
-            if (state.DPI.HasValue)
-                _map.DisplayDpi = state.DPI.Value;
+            _map.SetDisplaySize(width, height);
         }
 
-        public int Depth { get { return _states.Count; } }
-
-        /// <summary>
-        /// Pushes the given state onto the map state stack. The map takes on the display parameters specified
-        /// in this given state.
-        /// </summary>
-        /// <param name="state"></param>
-        public void PushState(MgdMapDisplayParameters state)
+        protected override void ApplyDPI(int dpi)
         {
-            if (state == null)
-                throw new ArgumentNullException("state");
-
-            var oldState = GetCurrentState();
-            ApplyState(state);
-
-            _states.Push(new MgdMapStateTransition() { OldState = oldState, NewState = state });
+            _map.DisplayDpi = dpi;
         }
-
-        /// <summary>
-        /// Pops the latest state from the map state stack. The map state is restored to
-        /// the previously applied state.
-        /// </summary>
-        /// <returns>The state that was previously applied</returns>
-        public MgdMapDisplayParameters PopState()
-        {
-            if (_states.Count == 0)
-                return null;
-
-            var trans = _states.Pop();
-            ApplyState(trans.OldState);
-            return trans.NewState;
-        }
-
-        public void Dispose()
-        {
-            while (_states.Count > 0)
-            {
-                this.PopState();
-            }
-            ApplyState(_origState);
-        }
     }
-
-    internal class MgdMapStateTransition
-    {
-        public MgdMapDisplayParameters NewState { get; set; }
-
-        public MgdMapDisplayParameters OldState { get; set; }
-    }
-
-    /// <summary>
-    /// Represents display parameters for a map
-    /// </summary>
-    public class MgdMapDisplayParameters
-    {
-        public double X { get; private set; }
-        public double Y { get; private set; }
-        public double Scale { get; private set; }
-        public int Width { get; private set; }
-        public int Height { get; private set; }
-        public int? DPI { get; private set; }
-
-        public MgdMapDisplayParameters(double x, double y, double scale, int width, int height)
-        {
-            this.X = x;
-            this.Y = y;
-            this.Scale = scale;
-            this.Width = width;
-            this.Height = height;
-        }
-
-        public MgdMapDisplayParameters(double x, double y, double scale, int width, int height, int dpi)
-            : this(x, y, scale, width, height)
-        {
-            this.DPI = dpi;
-        }
-    }
 }

Modified: branches/2.4/MgDev/Desktop/SampleExtension/CustomOutput.cs
===================================================================
--- branches/2.4/MgDev/Desktop/SampleExtension/CustomOutput.cs	2012-07-11 14:46:38 UTC (rev 6893)
+++ branches/2.4/MgDev/Desktop/SampleExtension/CustomOutput.cs	2012-07-11 15:39:01 UTC (rev 6894)
@@ -36,9 +36,9 @@
             //MgdTransientMapState is a helper class which lets us apply transient state to a map, which is automatically
             //undone on disposal. This is how we can render custom views of a map with specific display parameters without
             //permanently changing the display parameters used by the map viewer
-            using (MgdTransientMapState tempState = new MgdTransientMapState(map))
+            using (MgTransientMapState tempState = provider.CreateTransientState(map))
             {
-                MgdMapDisplayParameters state = new MgdMapDisplayParameters(centerCoord.X, centerCoord.Y, 4000, 640, 480, 96);
+                MgMapDisplayParameters state = new MgMapDisplayParameters(centerCoord.X, centerCoord.Y, 4000, 640, 480, 96);
                 tempState.PushState(state);
 
                 MgByteReader br = renderSvc.RenderMap(map, selection, "PNG");
@@ -75,9 +75,9 @@
             //MgdTransientMapState is a helper class which lets us apply transient state to a map, which is automatically
             //undone on disposal. This is how we can render custom views of a map with specific display parameters without
             //permanently changing the display parameters used by the map viewer
-            using (MgdTransientMapState tempState = new MgdTransientMapState(map))
+            using (MgTransientMapState tempState = provider.CreateTransientState(map))
             {
-                MgdMapDisplayParameters state = new MgdMapDisplayParameters(centerCoord.X, centerCoord.Y, 5000, 1024, 768, 96);
+                MgMapDisplayParameters state = new MgMapDisplayParameters(centerCoord.X, centerCoord.Y, 5000, 1024, 768, 96);
                 tempState.PushState(state);
 
                 MgByteReader br = renderSvc.RenderMap(map, selection, "PNG");
@@ -114,9 +114,9 @@
             //MgdTransientMapState is a helper class which lets us apply transient state to a map, which is automatically
             //undone on disposal. This is how we can render custom views of a map with specific display parameters without
             //permanently changing the display parameters used by the map viewer
-            using (MgdTransientMapState tempState = new MgdTransientMapState(map))
+            using (MgTransientMapState tempState = provider.CreateTransientState(map))
             {
-                MgdMapDisplayParameters state = new MgdMapDisplayParameters(centerCoord.X, centerCoord.Y, 6000, 1920, 1680, 192);
+                MgMapDisplayParameters state = new MgMapDisplayParameters(centerCoord.X, centerCoord.Y, 6000, 1920, 1680, 192);
                 tempState.PushState(state);
 
                 MgByteReader br = renderSvc.RenderMap(map, selection, "PNG");
@@ -153,9 +153,9 @@
             //MgdTransientMapState is a helper class which lets us apply transient state to a map, which is automatically
             //undone on disposal. This is how we can render custom views of a map with specific display parameters without
             //permanently changing the display parameters used by the map viewer
-            using (MgdTransientMapState tempState = new MgdTransientMapState(map))
+            using (MgTransientMapState tempState = provider.CreateTransientState(map))
             {
-                MgdMapDisplayParameters state = new MgdMapDisplayParameters(centerCoord.X, centerCoord.Y, 7000, 2048, 1920, 192);
+                MgMapDisplayParameters state = new MgMapDisplayParameters(centerCoord.X, centerCoord.Y, 7000, 2048, 1920, 192);
                 tempState.PushState(state);
 
                 MgByteReader br = renderSvc.RenderMap(map, selection, "PNG");



More information about the mapguide-commits mailing list