[76] | 1 | /** |
---|
| 2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
| 3 | * |
---|
| 4 | * Published under the BSD license. |
---|
| 5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
| 6 | * of the license. |
---|
| 7 | */ |
---|
| 8 | Ext.namespace("GeoExt.data"); |
---|
| 9 | |
---|
| 10 | /** api: (define) |
---|
| 11 | * module = GeoExt.data |
---|
| 12 | * class = PrintPage |
---|
| 13 | * base_link = `Ext.util.Observable <http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.Observable>`_ |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /** api: constructor |
---|
| 17 | * .. class:: PrintPage |
---|
| 18 | * |
---|
| 19 | * Provides a representation of a print page for |
---|
| 20 | * :class:`GeoExt.data.PrintProvider`. The extent of the page is stored as |
---|
| 21 | * ``OpenLayers.Feature.Vector``. Widgets can use this to display the print |
---|
| 22 | * extent on the map. |
---|
| 23 | */ |
---|
| 24 | GeoExt.data.PrintPage = Ext.extend(Ext.util.Observable, { |
---|
| 25 | |
---|
| 26 | /** api:config[printProvider] |
---|
| 27 | * :class:`GeoExt.data.PrintProvider` The print provider to use with |
---|
| 28 | * this page. |
---|
| 29 | */ |
---|
| 30 | |
---|
| 31 | /** private: property[printProvider] |
---|
| 32 | * :class:`GeoExt.data.PrintProvider` |
---|
| 33 | */ |
---|
| 34 | printProvider: null, |
---|
| 35 | |
---|
| 36 | /** api: property[feature] |
---|
| 37 | * ``OpenLayers.Feature.Vector`` Feature representing the page extent. To |
---|
| 38 | * get the extent of the print page for a specific map, use |
---|
| 39 | * ``getPrintExtent``. |
---|
| 40 | * Read-only. |
---|
| 41 | */ |
---|
| 42 | feature: null, |
---|
| 43 | |
---|
| 44 | /** api: property[center] |
---|
| 45 | * ``OpenLayers.LonLat`` The current center of the page. Read-only. |
---|
| 46 | */ |
---|
| 47 | center: null, |
---|
| 48 | |
---|
| 49 | /** api: property[scale] |
---|
| 50 | * ``Ext.data.Record`` The current scale record of the page. Read-only. |
---|
| 51 | */ |
---|
| 52 | scale: null, |
---|
| 53 | |
---|
| 54 | /** api: property[rotation] |
---|
| 55 | * ``Float`` The current rotation of the page. Read-only. |
---|
| 56 | */ |
---|
| 57 | rotation: 0, |
---|
| 58 | |
---|
| 59 | /** api:config[customParams] |
---|
| 60 | * ``Object`` Key-value pairs of additional parameters that the |
---|
| 61 | * printProvider will send to the print service for this page. |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | /** api: property[customParams] |
---|
| 65 | * ``Object`` Key-value pairs of additional parameters that the |
---|
| 66 | * printProvider will send to the print service for this page. |
---|
| 67 | */ |
---|
| 68 | customParams: null, |
---|
| 69 | |
---|
| 70 | /** private: method[constructor] |
---|
| 71 | * Private constructor override. |
---|
| 72 | */ |
---|
| 73 | constructor: function(config) { |
---|
| 74 | this.initialConfig = config; |
---|
| 75 | Ext.apply(this, config); |
---|
| 76 | |
---|
| 77 | if(!this.customParams) { |
---|
| 78 | this.customParams = {}; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | this.addEvents( |
---|
| 82 | /** api: event[change] |
---|
| 83 | * Triggered when any of the page properties have changed |
---|
| 84 | * |
---|
| 85 | * Listener arguments: |
---|
| 86 | * |
---|
| 87 | * * printPage - :class:`GeoExt.data.PrintPage` this printPage |
---|
| 88 | * * modifications - ``Object`` Object with one or more of |
---|
| 89 | * ``scale``, ``center`` and ``rotation``, notifying |
---|
| 90 | * listeners of the changed properties. |
---|
| 91 | */ |
---|
| 92 | "change" |
---|
| 93 | ); |
---|
| 94 | |
---|
| 95 | GeoExt.data.PrintPage.superclass.constructor.apply(this, arguments); |
---|
| 96 | |
---|
| 97 | this.feature = new OpenLayers.Feature.Vector( |
---|
| 98 | OpenLayers.Geometry.fromWKT("POLYGON((-1 -1,1 -1,1 1,-1 1,-1 -1))")); |
---|
| 99 | |
---|
| 100 | if(this.printProvider.capabilities) { |
---|
| 101 | this.setScale(this.printProvider.scales.getAt(0)); |
---|
| 102 | } else { |
---|
| 103 | this.printProvider.on({ |
---|
| 104 | "loadcapabilities": function() { |
---|
| 105 | this.setScale(this.printProvider.scales.getAt(0)); |
---|
| 106 | }, |
---|
| 107 | scope: this, |
---|
| 108 | single: true |
---|
| 109 | }); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | this.printProvider.on({ |
---|
| 113 | "layoutchange": this.onLayoutChange, |
---|
| 114 | scope: this |
---|
| 115 | }); |
---|
| 116 | }, |
---|
| 117 | |
---|
| 118 | /** api: method[getPrintExtent] |
---|
| 119 | * :param map: ``OpenLayers.Map`` or :class:`GeoExt.MapPanel` the map to |
---|
| 120 | * get the print extent for. |
---|
| 121 | * :returns: ``OpenLayers.Bounds`` |
---|
| 122 | * |
---|
| 123 | * Gets this page's print extent for the provided map. |
---|
| 124 | */ |
---|
| 125 | getPrintExtent: function(map) { |
---|
| 126 | map = map instanceof GeoExt.MapPanel ? map.map : map; |
---|
| 127 | return this.calculatePageBounds(this.scale, map.getUnits()); |
---|
| 128 | }, |
---|
| 129 | |
---|
| 130 | /** api: method[setScale] |
---|
| 131 | * :param scale: ``Ext.data.Record`` The new scale record. |
---|
| 132 | * :param units: ``String`` map units to use for the scale calculation. |
---|
| 133 | * Optional if the ``feature`` is on a layer which is added to a map. |
---|
| 134 | * If not found, "dd" will be assumed. |
---|
| 135 | * |
---|
| 136 | * Updates the page geometry to match a given scale. Since this takes the |
---|
| 137 | * current layout of the printProvider into account, this can be used to |
---|
| 138 | * update the page geometry feature when the layout has changed. |
---|
| 139 | */ |
---|
| 140 | setScale: function(scale, units) { |
---|
| 141 | var bounds = this.calculatePageBounds(scale, units); |
---|
| 142 | var geom = bounds.toGeometry(); |
---|
| 143 | var rotation = this.rotation; |
---|
| 144 | if(rotation != 0) { |
---|
| 145 | geom.rotate(-rotation, geom.getCentroid()); |
---|
| 146 | } |
---|
| 147 | this.updateFeature(geom, {scale: scale}); |
---|
| 148 | }, |
---|
| 149 | |
---|
| 150 | /** api: method[setCenter] |
---|
| 151 | * :param center: ``OpenLayers.LonLat`` The new center. |
---|
| 152 | * |
---|
| 153 | * Moves the page extent to a new center. |
---|
| 154 | */ |
---|
| 155 | setCenter: function(center) { |
---|
| 156 | var geom = this.feature.geometry; |
---|
| 157 | var oldCenter = geom.getBounds().getCenterLonLat(); |
---|
| 158 | var dx = center.lon - oldCenter.lon; |
---|
| 159 | var dy = center.lat - oldCenter.lat; |
---|
| 160 | geom.move(dx, dy); |
---|
| 161 | this.updateFeature(geom, {center: center}); |
---|
| 162 | }, |
---|
| 163 | |
---|
| 164 | /** api: method[setRotation] |
---|
| 165 | * :param rotation: ``Float`` The new rotation. |
---|
| 166 | * :param force: ``Boolean`` If set to true, the rotation will also be |
---|
| 167 | * set when the layout does not support it. Default is false. |
---|
| 168 | * |
---|
| 169 | * Sets a new rotation for the page geometry. |
---|
| 170 | */ |
---|
| 171 | setRotation: function(rotation, force) { |
---|
| 172 | if(force || this.printProvider.layout.get("rotation") === true) { |
---|
| 173 | var geom = this.feature.geometry; |
---|
| 174 | geom.rotate(this.rotation - rotation, geom.getCentroid()); |
---|
| 175 | this.updateFeature(geom, {rotation: rotation}); |
---|
| 176 | } |
---|
| 177 | }, |
---|
| 178 | |
---|
| 179 | /** api: method[fit] |
---|
| 180 | * :param fitTo: :class:`GeoExt.MapPanel` or ``OpenLayers.Map`` or ``OpenLayers.Feature.Vector`` |
---|
| 181 | * The map or feature to fit the page to. |
---|
| 182 | * :param options: ``Object`` Additional options to determine how to fit |
---|
| 183 | * |
---|
| 184 | * Fits the page layout to a map or feature extent. If the map extent has |
---|
| 185 | * not been centered yet, this will do nothing. |
---|
| 186 | * |
---|
| 187 | * Available options: |
---|
| 188 | * |
---|
| 189 | * * mode - ``String`` How to calculate the print extent? If "closest", |
---|
| 190 | * the closest matching print extent will be chosen. If "printer", the |
---|
| 191 | * chosen print extent will be the closest one that can show the entire |
---|
| 192 | * ``fitTo`` extent on the printer. If "screen", it will be the closest |
---|
| 193 | * one that is entirely visible inside the ``fitTo`` extent. Default is |
---|
| 194 | * "printer". |
---|
| 195 | * |
---|
| 196 | */ |
---|
| 197 | fit: function(fitTo, options) { |
---|
| 198 | options = options || {}; |
---|
| 199 | var map = fitTo, extent; |
---|
| 200 | if(fitTo instanceof GeoExt.MapPanel) { |
---|
| 201 | map = fitTo.map; |
---|
| 202 | } else if(fitTo instanceof OpenLayers.Feature.Vector) { |
---|
| 203 | map = fitTo.layer.map; |
---|
| 204 | extent = fitTo.geometry.getBounds(); |
---|
| 205 | } |
---|
| 206 | if(!extent) { |
---|
| 207 | extent = map.getExtent(); |
---|
| 208 | if(!extent) { |
---|
| 209 | return; |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | this._updating = true; |
---|
| 213 | var center = extent.getCenterLonLat(); |
---|
| 214 | this.setCenter(center); |
---|
| 215 | var units = map.getUnits(); |
---|
| 216 | var scale = this.printProvider.scales.getAt(0); |
---|
| 217 | var closest = Number.POSITIVE_INFINITY; |
---|
| 218 | var mapWidth = extent.getWidth(); |
---|
| 219 | var mapHeight = extent.getHeight(); |
---|
| 220 | this.printProvider.scales.each(function(rec) { |
---|
| 221 | var bounds = this.calculatePageBounds(rec, units); |
---|
| 222 | if (options.mode == "closest") { |
---|
| 223 | var diff = |
---|
| 224 | Math.abs(bounds.getWidth() - mapWidth) + |
---|
| 225 | Math.abs(bounds.getHeight() - mapHeight); |
---|
| 226 | if (diff < closest) { |
---|
| 227 | closest = diff; |
---|
| 228 | scale = rec; |
---|
| 229 | } |
---|
| 230 | } else { |
---|
| 231 | var contains = options.mode == "screen" ? |
---|
| 232 | !extent.containsBounds(bounds) : |
---|
| 233 | bounds.containsBounds(extent); |
---|
| 234 | if (contains || (options.mode == "screen" && !contains)) { |
---|
| 235 | scale = rec; |
---|
| 236 | } |
---|
| 237 | return contains; |
---|
| 238 | } |
---|
| 239 | }, this); |
---|
| 240 | this.setScale(scale, units); |
---|
| 241 | delete this._updating; |
---|
| 242 | this.updateFeature(this.feature.geometry, { |
---|
| 243 | center: center, |
---|
| 244 | scale: scale |
---|
| 245 | }); |
---|
| 246 | }, |
---|
| 247 | |
---|
| 248 | /** private: method[updateFeature] |
---|
| 249 | * :param geometry: ``OpenLayers.Geometry`` New geometry for the feature. |
---|
| 250 | * If not provided, the existing geometry will be left unchanged. |
---|
| 251 | * :param mods: ``Object`` An object with one or more of ``scale``, |
---|
| 252 | * ``center`` and ``rotation``, reflecting the page properties to |
---|
| 253 | * update. |
---|
| 254 | * |
---|
| 255 | * Updates the page feature with a new geometry and notifies listeners |
---|
| 256 | * of changed page properties. |
---|
| 257 | */ |
---|
| 258 | updateFeature: function(geometry, mods) { |
---|
| 259 | var f = this.feature; |
---|
| 260 | var modified = f.geometry !== geometry; |
---|
| 261 | geometry.id = f.geometry.id; |
---|
| 262 | f.geometry = geometry; |
---|
| 263 | |
---|
| 264 | if(!this._updating) { |
---|
| 265 | for(var property in mods) { |
---|
| 266 | if(mods[property] === this[property]) { |
---|
| 267 | delete mods[property]; |
---|
| 268 | } else { |
---|
| 269 | this[property] = mods[property]; |
---|
| 270 | modified = true; |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | Ext.apply(this, mods); |
---|
| 274 | |
---|
| 275 | f.layer && f.layer.drawFeature(f); |
---|
| 276 | modified && this.fireEvent("change", this, mods); |
---|
| 277 | } |
---|
| 278 | }, |
---|
| 279 | |
---|
| 280 | /** private: method[calculatePageBounds] |
---|
| 281 | * :param scale: ``Ext.data.Record`` Scale record to calculate the page |
---|
| 282 | * bounds for. |
---|
| 283 | * :param units: ``String`` Map units to use for the scale calculation. |
---|
| 284 | * Optional if ``feature`` is added to a layer which is added to a |
---|
| 285 | * map. If not provided, "dd" will be assumed. |
---|
| 286 | * :return: ``OpenLayers.Bounds`` |
---|
| 287 | * |
---|
| 288 | * Calculates the page bounds for a given scale. |
---|
| 289 | */ |
---|
| 290 | calculatePageBounds: function(scale, units) { |
---|
| 291 | var s = scale.get("value"); |
---|
| 292 | var f = this.feature; |
---|
| 293 | var geom = this.feature.geometry; |
---|
| 294 | var center = geom.getBounds().getCenterLonLat(); |
---|
| 295 | |
---|
| 296 | var size = this.printProvider.layout.get("size"); |
---|
| 297 | var units = units || |
---|
| 298 | (f.layer && f.layer.map && f.layer.map.getUnits()) || |
---|
| 299 | "dd"; |
---|
| 300 | var unitsRatio = OpenLayers.INCHES_PER_UNIT[units]; |
---|
| 301 | var w = size.width / 72 / unitsRatio * s / 2; |
---|
| 302 | var h = size.height / 72 / unitsRatio * s / 2; |
---|
| 303 | |
---|
| 304 | return new OpenLayers.Bounds(center.lon - w, center.lat - h, |
---|
| 305 | center.lon + w, center.lat + h); |
---|
| 306 | }, |
---|
| 307 | |
---|
| 308 | /** private: method[onLayoutChange] |
---|
| 309 | * Handler for the printProvider's layoutchange event. |
---|
| 310 | */ |
---|
| 311 | onLayoutChange: function() { |
---|
| 312 | if(this.printProvider.layout.get("rotation") === false) { |
---|
| 313 | this.setRotation(0, true); |
---|
| 314 | } |
---|
| 315 | // at init time the print provider triggers layoutchange |
---|
| 316 | // before loadcapabilities, i.e. before we set this.scale |
---|
| 317 | // to the first scale in the scales store, we need to |
---|
| 318 | // guard against that |
---|
| 319 | this.scale && this.setScale(this.scale); |
---|
| 320 | }, |
---|
| 321 | |
---|
| 322 | /** private: method[destroy] |
---|
| 323 | */ |
---|
| 324 | destroy: function() { |
---|
| 325 | this.printProvider.un("layoutchange", this.onLayoutChange, this); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | }); |
---|