[Commits] r1787 - in sandbox/ahocevar/playground: trunk/geoext/examples trunk/geoext/lib/GeoExt/widgets trunk/geoext/tests trunk/geoext/tests/lib/GeoExt/widgets ux/Printing/ux/widgets

commits at geoext.org commits at geoext.org
Wed Jan 20 15:38:33 CET 2010


Author: ahocevar
Date: 2010-01-20 15:38:33 +0100 (Wed, 20 Jan 2010)
New Revision: 1787

Added:
   sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.html
   sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.js
   sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintMapPanel.html
Modified:
   sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintMapPanel.js
   sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html
   sandbox/ahocevar/playground/ux/Printing/ux/widgets/PrintPreview.js
Log:
PrintMapPanel tests and example; also a fix to fitSize (which was renamed from fitMap).

Added: sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.html
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.html	                        (rev 0)
+++ sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.html	2010-01-20 14:38:33 UTC (rev 1787)
@@ -0,0 +1,31 @@
+<html>
+    <head>
+        <title>GeoExt PrintMapPanel Example</title>
+
+        <script type="text/javascript" src="http://extjs.cachefly.net/ext-2.2.1/adapter/ext/ext-base.js"></script>
+        <script type="text/javascript" src="http://extjs.cachefly.net/ext-2.2.1/ext-all.js"></script>
+        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/resources/css/ext-all.css" />
+        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/examples/shared/examples.css" />
+        <script src="http://openlayers.org/api/2.8/OpenLayers.js"></script>
+        <script type="text/javascript" src="../lib/GeoExt.js"></script>
+
+        <script type="text/javascript" src="print-preview.js"></script>
+        
+        <!-- The script below will load the capabilities of the print service
+             and save them into the global printCapabilities variable. Instead
+             of this, the PrintProvider can be configured with a url and take
+             care of fetching the capabilities. -->
+        <script type="text/javascript" src="http://demo.opengeo.org/geoserver/pdf/info.json?var=printCapabilities"></script>
+
+    </head>
+    <body>
+        <h1>Using a Print Preview</h1>
+        <p>This example shows the how to create a printable PDF with
+        GeoExt.data.PrintProvider and GeoExt.PrintMapPanel, using the
+        <a href="http://trac.mapfish.org/trac/mapfish/wiki/PrintModuleInstallation">MapFish</a>
+        or <a href="http://geoserver.org/display/GEOS/Printing+2.0+HOWTO">GeoServer</a>
+        print module.</p>
+        <p>The js is not minified so it is readable. See <a href="print-preview.js">print-preview.js</a>.</p>
+        <div id="content"></div>
+    </body>
+</html>

Added: sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.js
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.js	                        (rev 0)
+++ sandbox/ahocevar/playground/trunk/geoext/examples/print-preview.js	2010-01-20 14:38:33 UTC (rev 1787)
@@ -0,0 +1,60 @@
+ /**
+ * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
+ * 
+ * Published under the BSD license.
+ * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
+ * of the license.
+ */
+
+/** api: example[print-preview]
+ *  Printing with a Preview
+ *  ----------------------------------------
+ *  Using PrintMapPanel for an interactive print preview.
+ */
+
+var mapPanel, printDialog;
+
+Ext.onReady(function() {
+    // The PrintProvider that connects us to the print service
+    var printProvider = new GeoExt.data.PrintProvider({
+        method: "GET", // "POST" recommended for production use
+        capabilities: printCapabilities, // provide url instead for lazy loading
+        customParams: {
+            mapTitle: "GeoExt Printing Demo",
+            comment: "This demo shows how to use GeoExt.PrintMapPanel"
+        }
+    });
+    
+    // A MapPanel with a "Print..." button
+    mapPanel = new GeoExt.MapPanel({
+        renderTo: "content",
+        width: 500,
+        height: 350,
+        layers: [new OpenLayers.Layer.WMS("Tasmania State Boundaries",
+            "http://demo.opengeo.org/geoserver/wms",
+            {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
+        center: [146.56, -41.56],
+        zoom: 6,
+        bbar: [{
+            text: "Print...",
+            handler: function(){
+                // A window with the PrintMapPanel, which we can use to adjust
+                // the print extent before creating the pdf.
+                printDialog = new Ext.Window({
+                    title: "Print Preview",
+                    items: [{
+                        xtype: "gx_printmappanel",
+                        sourceMap: mapPanel,
+                        printProvider: printProvider
+                    }],
+                    bbar: [{
+                        text: "Create PDF",
+                        handler: function(){ printDialog.items.get(0).print(); }
+                    }]
+                });
+                printDialog.show();
+            }
+        }]
+    });
+
+});
\ No newline at end of file

Modified: sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintMapPanel.js
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintMapPanel.js	2010-01-19 22:04:34 UTC (rev 1786)
+++ sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintMapPanel.js	2010-01-20 14:38:33 UTC (rev 1787)
@@ -21,18 +21,60 @@
  * GeoExt/widgets/MapPanel.js
  */
 
+/** api: example
+ *  A map with a "Print..." button. If clicked, a dialog containing a
+ *  PrintMapPanel will open, with a "Create PDF" button.
+ * 
+ *  .. code-block:: javascript
+ *     
+ *      var mapPanel = new GeoExt.MapPanel({
+ *          renderTo: "map",
+ *          layers: [new OpenLayers.Layer.WMS("Tasmania State Boundaries",
+ *              "http://demo.opengeo.org/geoserver/wms",
+ *              {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
+ *          center: [146.56, -41.56],
+ *          zoom: 6,
+ *          bbar: [{
+ *              text: "Print...",
+ *              handler: function() {
+ *                  var printDialog = new Ext.Window({
+ *                      items: [new GeoExt.PrintMapPanel({
+ *                          sourceMap: mapPanel,
+ *                          printProvider: {
+ *                              capabilities: printCapabilities
+ *                          }
+ *                      })],
+ *                      bbar: [{
+ *                          text: "Create PDF",
+ *                          handler: function() {
+ *                              printDialog.items.get(0).print();
+ *                          }
+ *                      }]
+ *                  });
+ *                  printDialog.show();
+ *              }
+ *          }]
+ *      });
+ */
+
 /** api: constructor
  *  .. class:: PrintMapPanel
  * 
  *  A map panel that controls scale and center of a print page, based the
- *  current view of a source map.
+ *  current view of a source map. This panel will contain the same layers as
+ *  the source map, and it will be sized to exactly match the print extent at
+ *  the smallest available scale.
+ *  
+ *  .. note:: The ``zoom``, ``center`` and ``extent`` config options will have
+ *      no affect, as they will be determined by the ``sourceMap``.
  */
 GeoExt.PrintMapPanel = Ext.extend(GeoExt.MapPanel, {
     
     /** api: config[map]
      *  ``Object`` Optional configuration for the ``OpenLayers.Map`` object
-     *  that this PrintMapPanel uses. Useful e.g. to configure a map with a
-     *  custom set of controls.
+     *  that this PrintMapPanel creates. Useful e.g. to configure a map with a
+     *  custom set of controls, or to add a ``preaddlayer`` listener for
+     *  filtering out layer types that cannot be printed.
      *  
      *  .. note:: ``numZoomLevels`` and ``resolutions`` of the map will be set
      *      by this PrintMapPanel, and the layers will be copied from
@@ -40,7 +82,7 @@
      */
     
     /** api: config[sourceMap]
-     *  :class:`GeoExt.MapPanel`|``OpenLayers.Layer`` The map that is to be
+     *  :class:`GeoExt.MapPanel`|``OpenLayers.Map`` The map that is to be
      *  printed.
      */
     
@@ -49,7 +91,7 @@
      */
     sourceMap: null,
     
-    /** config: property[printProvider]
+    /** api: config[printProvider]
      *  :class:`GeoExt.data.PrintProvider`|``Object`` PrintProvider to use
      *  for printing. If an ``Object`` is provided, a new PrintProvider will
      *  be created and configured with the object.
@@ -65,13 +107,13 @@
      *  ``capabilities`` is provided in the printProvider's configuration
      *  object. If ``printProvider`` is provided as an instance of
      *  :class:`GeoExt.data.PrintProvider`, the capabilities must be loaded
-     *  upon PrintMapPanel initialization.
+     *  before PrintMapPanel initialization.
      */
     printProvider: null,
     
     /** api: property[printPage]
      *  :class:`GeoExt.data.PrintPage` PrintPage for this PrintMapPanel.
-     *  read-only.
+     *  Read-only.
      */
     printPage: null,
     
@@ -101,6 +143,7 @@
         
         this.zoom = resolutions.indexOf(this.sourceMap.getResolution());
         this.center = this.sourceMap.getCenter();
+        this.extent = null;
 
         this.layers = [];
         var layer;
@@ -115,7 +158,7 @@
         });
 
         // set an initial size with the same aspect ratio as the print page.
-        // This is crucial for the first fitMap call to determine the correct
+        // This is crucial for the first fitSize call to determine the correct
         // resolution, otherwise we may be one zoom level off.
         var size = this.printProvider.layout.get("size");
         this.width = size.width;
@@ -123,24 +166,23 @@
         
         GeoExt.PrintMapPanel.superclass.initComponent.call(this);
         
-        this.fitMap();
-        this.printProvider.on("layoutchange", this.fitMap, this);
+        this.fitSize();
+        this.printProvider.on("layoutchange", this.fitSize, this);
         this.printPage.on("change", this.fitZoom, this);
         this.map.events.register("moveend", this, this.updatePage);
     },
     
-    /** private: method[fitMap]
+    /** private: method[fitSize]
      *  Fits this PrintMapPanel's width and height to the print extent. This
      *  calculation is based on the print extent for the first available scale,
      *  which means that the print view extent is only guaranteed to be
      *  accurate at that scale. The aspect ratio, however, can be taken for
-     *  granted at any scale.
+     *  granted at any scale, because it depends on the layout.
      */
-    fitMap: function() {
-        this.printPage.setScale(this.printProvider.scales.getAt(0),
+    fitSize: function() {
+        var extent = this.printPage.calculatePageBounds(
+            this.printProvider.scales.getAt(0),
             this.sourceMap.units);
-        this.printPage.setCenter(this.sourceMap.getCenter());
-        var extent = this.printPage.feature.geometry.getBounds();
         var zoom = this.map.getZoomForExtent(extent);
         var resolution = this.map.getResolutionForZoom(zoom);
         this.setSize(
@@ -174,8 +216,8 @@
     /** api: method[print]
      *  :param options: ``Object`` options for the printProvider's
      *  :ref:`GeoExt.data.PrintProvider.print` method.
-     *  Convenience methods for printing the map, without the need to interact
-     *  with the printProvider.
+     *  Convenience method for printing the map, without the need to interact
+     *  with the printProvider and printPage.
      */
     print: function(options) {
         this.printProvider.print(this.map, [this.printPage], options);
@@ -186,7 +228,7 @@
     beforeDestroy: function() {
         this.map.events.unregister("moveend", this, this.updatePage);
         this.printPage.un("change", this.fitZoom, this);
-        this.printProvider.un("layoutchange", this.fitMap, this);
+        this.printProvider.un("layoutchange", this.fitSize, this);
         GeoExt.PrintMapPanel.superclass.beforeDestroy.apply(this, arguments);
     }
 });

Added: sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintMapPanel.html
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintMapPanel.html	                        (rev 0)
+++ sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintMapPanel.html	2010-01-20 14:38:33 UTC (rev 1787)
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
+    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
+    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+
+    <script type="text/javascript">
+        var printCapabilities = {"scales":[{"name":"1:25,000","value":"25000"},{"name":"1:50,000","value":"50000"},{"name":"1:100,000","value":"100000"},{"name":"1:200,000","value":"200000"},{"name":"1:500,000","value":"500000"},{"name":"1:1,000,000","value":"1000000"},{"name":"1:2,000,000","value":"2000000"},{"name":"1:4,000,000","value":"4000000"}],"dpis":[{"name":"75","value":"75"},{"name":"150","value":"150"},{"name":"300","value":"300"}],"layouts":[{"name":"A4 portrait","map":{"width":440,"height":483},"rotation":true},{"name":"Legal","map":{"width":440,"height":483},"rotation":false}],"printURL":"http://demo.opengeo.org/geoserver/pdf/print.pdf","createURL":"http://demo.opengeo.org/geoserver/pdf/create.json"};
+       
+        function test_initComponent(t) {
+            t.plan(9);
+            
+            var mapPanel = new GeoExt.MapPanel({
+                renderTo: "mappanel",
+                width: 256,
+                height: 256,
+                layers: [new OpenLayers.Layer.WMS("Tasmania State Boundaries",
+                    "http://demo.opengeo.org/geoserver/wms",
+                    {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
+                center: [146.56, -41.56],
+                zoom: 7
+            });
+            
+            var log = {};
+            var printMapPanel = new GeoExt.PrintMapPanel({
+                renderTo: "printmappanel",
+                map: {
+                    controls: [new OpenLayers.Control.PanPanel()],
+                    eventListeners: {
+                        preaddlayer: function(e) {
+                            log.preaddlayer = e.layer;
+                        }
+                    }
+                },
+                printProvider: {
+                    capabilities: printCapabilities
+                },
+                sourceMap: mapPanel
+            });
+            
+            var size = printMapPanel.printProvider.layout.get("size");
+            var center = mapPanel.map.getCenter();
+            
+            t.eq(printMapPanel.map.controls.length, printMapPanel.map.controls[0].controls.length+1, "Preview map has only the control we added,");
+            t.ok(printMapPanel.map.controls[0] instanceof OpenLayers.Control.PanPanel, "which is a PanPanel.");
+            t.eq(printMapPanel.map.getResolution(), mapPanel.map.getResolution(), "Preview map has the same resolution as the source map.");
+            t.ok(printMapPanel.map.getCenter().equals(center), "Preview map has the same center as the source map.");
+            t.eq(printMapPanel.map.layers[0].name, mapPanel.map.layers[0].name, "Preview map has the same visible layer as the source map.");
+            t.eq(log.preaddlayer.name, printMapPanel.map.layers[0].name, "preaddlayer listener noticed addition of the layer.");
+            t.eq((printMapPanel.width/printMapPanel.height).toPrecision(8), (size.width/size.height).toPrecision(8), "Aspect ratio of the preview map is the same as of the print layout.");
+            t.ok(printMapPanel.printPage.getCenter().equals(center), "Print page has the same center as the source map.");
+            t.eq(printMapPanel.printPage.scale.get("value"), 2000000, "Print page has the correct scale to fit the source map.");
+            
+            printMapPanel.destroy();
+            mapPanel.destroy();
+        }
+
+    </script>
+  <body>
+    <div id="mappanel"></div>
+    <div id="printmappanel"></div>
+  </body>
+</html>

Modified: sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html	2010-01-19 22:04:34 UTC (rev 1786)
+++ sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html	2010-01-20 14:38:33 UTC (rev 1787)
@@ -22,6 +22,7 @@
   <li>lib/GeoExt/widgets/LayerOpacitySlider.html</li>
   <li>lib/GeoExt/widgets/MapPanel.html</li>
   <li>lib/GeoExt/widgets/Popup.html</li>
+  <li>lib/GeoExt/widgets/PrintMapPanel.html</li>
   <li>lib/GeoExt/widgets/form.html</li>
   <li>lib/GeoExt/widgets/form/SearchAction.html</li>
   <li>lib/GeoExt/widgets/form/BasicForm.html</li>

Modified: sandbox/ahocevar/playground/ux/Printing/ux/widgets/PrintPreview.js
===================================================================
--- sandbox/ahocevar/playground/ux/Printing/ux/widgets/PrintPreview.js	2010-01-19 22:04:34 UTC (rev 1786)
+++ sandbox/ahocevar/playground/ux/Printing/ux/widgets/PrintPreview.js	2010-01-20 14:38:33 UTC (rev 1787)
@@ -95,7 +95,8 @@
     
     /** api: config[mapOptions]
      *  ``Object`` Configuration object for the preview map. Useful e.g. for
-     *  configuring custom controls.
+     *  configuring custom controls, or filtering out unsupported layers with
+     *  a preaddlayer listener.
      */
     mapOptions: null,
     



More information about the Commits mailing list