[Commits] r1750 - in sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt: data widgets

commits at geoext.org commits at geoext.org
Fri Jan 15 00:48:18 CET 2010


Author: ahocevar
Date: 2010-01-15 00:48:18 +0100 (Fri, 15 Jan 2010)
New Revision: 1750

Modified:
   sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/data/PrintPage.js
   sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js
Log:
refactored PrintPage to show 4 handles instead of 1.

Modified: sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/data/PrintPage.js
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/data/PrintPage.js	2010-01-14 21:28:40 UTC (rev 1749)
+++ sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/data/PrintPage.js	2010-01-14 23:48:18 UTC (rev 1750)
@@ -41,10 +41,10 @@
     feature: null,
     
     /** api: property[handle]
-     *  ``OpenLayers.Feature.Vector`` Feature providing the scale/rotation
-     *  handle for the page. Read-only.
+     *  ``OpenLayers.Feature.Vector`` Features providing the four
+     *  scale/rotation handles for the page. Read-only.
      */
-    handle: null,
+    handles: null,
     
     /** api: property[scale]
      *  ``Ext.data.Record`` The current scale record of the page. Read-only.
@@ -92,8 +92,20 @@
 
         this.feature = new OpenLayers.Feature.Vector(
             OpenLayers.Geometry.fromWKT("POLYGON((-1 -1,1 -1,1 1,-1 1,-1 -1))"));
-        this.handle = new OpenLayers.Feature.Vector(
-            new OpenLayers.Geometry.Point(0,0));
+        this.handles = [
+            new OpenLayers.Feature.Vector(
+                new OpenLayers.Geometry.Point(-1,-1)
+            ),
+            new OpenLayers.Feature.Vector(
+                new OpenLayers.Geometry.Point(1,-1)
+            ),
+            new OpenLayers.Feature.Vector(
+                new OpenLayers.Geometry.Point(1,1)
+            ),
+            new OpenLayers.Feature.Vector(
+                new OpenLayers.Geometry.Point(-1,1)
+            )
+        ];
         
         this.printProvider.on({
             "layoutchange": this.updateByHandle,
@@ -185,28 +197,50 @@
     },
 
     /** api: method[updateByHandle]
-     *  :param updateHandle: ``Boolean`` If set to false, the handle location
-     *      will not be aligned with the feature after updating. Default is
-     *      true.
+     *  :param handle: ``OpenLayers.Feature.Vector`` the handle to use.
      *  
      *  Updates scale and rotation based on the current handle location. This
      *  method is useful for drag handlers on the handle geometry, when
      *  displayed on a layer.
      */
-    updateByHandle: function(updateHandle) {
-        var f = this.feature;
-        
-        var rotation = 0;
-        if(this.printProvider.layout.get("rotation") === true) {
-            var hLoc = this.handle.geometry.getBounds().getCenterLonLat();
-            var center = f.geometry.getBounds().getCenterLonLat();
-            var dx = hLoc.lon - center.lon;
-            var dy = hLoc.lat - center.lat;
-            rotation = Math.round(Math.atan2(dx, dy) * 180 / Math.PI);
+    updateByHandle: function(handle) {
+        var handles = this.handles;
+        var handleIndex = handles.indexOf(handle);
+        if(handleIndex === -1) {
+            return;
         }
 
-        var geom = this.handle.geometry;
-        var dist = f.geometry.getCentroid().distanceTo(geom);
+        var rotate = this.printProvider.layout.get("rotation");
+        var vertices = this.feature.geometry.components[0].components;
+        var fGeom = this.feature.geometry;
+        var hGeom = handle.geometry;
+        var origin = fGeom.getCentroid();
+
+        // resize handles
+        var resizeBy = origin.distanceTo(handle.geometry) /
+            origin.distanceTo(vertices[handleIndex]);
+        var handleRect = fGeom.clone();
+        handleRect.resize(resizeBy, origin);
+
+        // rotate handles
+        if(rotate) {
+            var dxH = hGeom.x - origin.x;
+            var dyH = hGeom.y - origin.y;
+            var dxRef = vertices[handleIndex].x - origin.x;
+            var dyRef = vertices[handleIndex].y - origin.y;
+            var handleRot = Math.round(Math.atan2(dxH, dyH) * 180 / Math.PI) -
+                Math.round(Math.atan2(dxRef, dyRef) * 180 / Math.PI);
+            handleRot && handleRect.rotate(-handleRot, origin);
+        }
+        
+        this.updateHandles(handleRect, handle);
+
+        // fit scale of the page to the handle rectangle
+        var top = new OpenLayers.Geometry.LineString([
+            handles[2].geometry, handles[3].geometry]);
+        var topCenter = top.getBounds().getCenterLonLat();
+        var dist = new OpenLayers.Geometry.Point(
+            topCenter.lon, topCenter.lat).distanceTo(origin);
         var scaleFits = [], distHash = {};
         this.printProvider.scales.each(function(rec){
             var bounds = this.calculatePageBounds(rec);
@@ -216,14 +250,21 @@
         }, this);
         var min = scaleFits.concat().sort(function(a,b){return a<b?-1:1;})[0];
         var scale = distHash[min.toPrecision(8)];
-        
         var bounds = this.calculatePageBounds(scale);
         var geom = bounds.toGeometry();
-        geom.rotate(-rotation, geom.getCentroid());
+
+        // fit rotation of the page to the handle rectangle
+        var rotation = 0;
+        if(rotate) {
+            var dx = topCenter.lon - origin.x;
+            var dy = topCenter.lat - origin.y;
+            rotation = Math.round(Math.atan2(dx, dy) * 180 / Math.PI);
+            geom.rotate(-rotation, geom.getCentroid());
+        }
         
         this.scale = scale;
         this.rotation = rotation;
-        this.updateFeature(geom, updateHandle);
+        this.updateFeature(geom, false);
     },
 
     /** private: method[updateFeature]
@@ -243,37 +284,45 @@
         f.layer && f.layer.drawFeature(f);
         
         if(updateHandle !== false) {
-            this.updateHandle();
+            this.updateHandles();
         }
         this.fireEvent("change", this);
     },
     
-    /** api: method[updateHandle]
-     *  Updates the handle position to align with the page feature.
+    /** api: method[updateHandles]
+     *      
+     *  Updates the handle positions to align with the page feature. Useful
+     *  for drag handlers on the page feature to update the handles while
+     *  dragging, and for drag handlers on the handle features to re-align the
+     *  handles with the page feature after dragging.
      */
-    updateHandle: function() {
+    /** private: method[updateHandles]
+     *  :param geometry: ``OpenLayers.Geometry.Polygon`` Optional. If provided,
+     *      handle geometries will be taken from the vertices of the provided
+     *      polygon instead of this page's feature's geometry.
+     *  :param excludeHandle: ``OpenLayers.Feature``. Optional. If one of the
+     *      features of the handles array is provided, its geometry will not be
+     *      updated.
+     */
+    updateHandles: function(geometry, excludeHandle) {
         var f = this.feature;
-        var h = this.handle;
-        var hLoc = this.calculateHandleLocation();
-        var geom = new OpenLayers.Geometry.Point(hLoc.lon, hLoc.lat);
-        geom.id = h.geometry.id;
-        h.geometry = geom;
-        h.layer && h.layer.drawFeature(h);
+        var h = this.handles;
+        var c = (geometry || f.geometry).components[0].components;
+        var layer = h[0].layer;
+        var id, handle, hGeom;
+        for(var i=0; i<4; ++i) {
+            handle = h[i];
+            if(handle !== excludeHandle) {
+                hGeom = handle.geometry;
+                id = hGeom.id;
+                hGeom = c[i].clone();
+                hGeom.id = id;
+                handle.geometry = hGeom;
+                layer && layer.drawFeature(handle);
+            }
+        }
     },
     
-    /** private: method[calculateHandleLocation]
-     *  :return: ``OpenLayers.LonLat`` The location of the scale/rotation
-     *      handle.
-     *  
-     *  Calculates the position of the scale/rotation handle to align with the
-     *  page feature.
-     */
-    calculateHandleLocation: function() {
-        var c = this.feature.geometry.components[0].components;
-        var top = new OpenLayers.Geometry.LineString([c[2], c[3]]);
-        return top.getBounds().getCenterLonLat();
-    },
-
     /** private: method[calculatePageBounds]
      *  :param scale: ``Ext.data.Record`` Scale record to calculate the page
      *      bounds for.

Modified: sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js	2010-01-14 21:28:40 UTC (rev 1749)
+++ sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js	2010-01-14 23:48:18 UTC (rev 1750)
@@ -144,7 +144,7 @@
         this.map.removeControl(this.control);
         for(var i=0, len=this.pages.length; i<len; ++i) {
             var page = this.pages[i];
-            this.layer.removeFeatures([page.feature, page.handle]);
+            this.layer.removeFeatures([page.feature].concat(page.handles));
         }
         this.initialConfig.layer || this.map.removeLayer(layer);
         this.map = null;
@@ -159,7 +159,7 @@
             });
             for(var i=0, len=this.pages.length; i<len; ++i) {
                 var page = this.pages[i];
-                this.layer.addFeatures([page.feature, page.handle]);
+                this.layer.addFeatures([page.feature].concat(page.handles));
             }
         }
         if(!this.layer.map) {
@@ -175,7 +175,7 @@
         if(!this.control) {
             var updateHandles = function() {
                 for(var i=0, len=pages.length; i<len; ++i) {
-                    pages[i].updateHandle();
+                    pages[i].updateHandles();
                 };
             }
             this.control = new OpenLayers.Control.DragFeature(this.layer, {
@@ -187,7 +187,7 @@
                     } else if(geom instanceof OpenLayers.Geometry.Point) {
                         // a scale/rotation handle was dragged
                         for(var i=0, len=pages.length; i<len; ++i) {
-                            pages[i].updateByHandle(false);
+                            pages[i].updateByHandle(feature);
                         }
                     }
                 },



More information about the Commits mailing list