NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/ROIPolygonTool/ROIPolygonTool.widget.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * ROIPolygonTool Widget class
4  *
5  * @project     CWC2
6  * @revision    $Id: ROIPolygonTool.widget.php,v 1.10 2005/05/27 16:23:30 jlacroix Exp $
7  * @purpose     ROI Polygon Tool Widget class.  Allows the user to select
8  * a circular region of interest on the map. 
9  * @author      DM Solutions Group (bronsema@dmsolutions.ca)
10  * @copyright
11  * <b>Copyright (c) 2002-2003, DM Solutions Group Inc.</b>
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30
31 include_once(dirname(__FILE__)."/../Widget.php");
32 include_once(dirname(__FILE__)."/../Button.php");
33
34 /**
35  * ROIPolygonTool
36  *
37  * @desc A widget to draw a polagonal Region of Interest
38  */
39 class ROIPolygonTool extends CWCWidget
40 {
41     /* the button that is the interface for this widget */
42     var $moButton;
43     var $mnNumPoints = 12;  // default number of points
44     var $mnPointSpacing = 10; // default the point spacing;
45    
46     /**
47      * construct a new ROI Polygon Tool widget
48      */
49     function ROIPolygonTool()
50     {
51         // set the language resource
52         $this->mszLanguageResource = str_replace("\\","/",dirname(__FILE__))."/ROIPolygonTool.dbf";       
53        
54         // invoke constructor of parent
55         parent::CWCWidget();
56        
57         // set the attributes
58         $this->maAttributes["NUMBEROFPOINTS"] = new IntegerAttribute( "NUMBEROFPOINTS", false, 0 );
59         $this->maAttributes["POINTSPACING"] = new IntegerAttribute( "POINTSPACING", false, 0 );
60        
61         // set the description for this widget
62         $this->szWidgetDescription = <<<EOT
63 The ROIPolygonTool widget allows the user to draw a circular region
64 of interest on the map interface.
65 EOT;
66         $this->moButton = new CWCButton( $this );
67         $this->mnMaturityLevel = MATURITY_BETA;
68     }
69    
70     /**
71      *
72      */
73     function InitDefaults()
74     {
75         parent::InitDefaults();
76        
77         $this->moButton->InitDefaults();
78         $this->moButton->SetOnClick('ROIPolygonTool'.$this->mnId);
79     }
80    
81     /**
82      * pass URL to button
83      */
84     function  ParseURL()
85     {
86         return $this->moButton->ParseURL();
87     }
88
89     function GetHTMLHiddenVariables()
90     {
91         $aReturn = $this->moButton->GetHTMLHiddenVariables();
92                
93         $szCmd = '';
94         if ($this->isVarSet('NAV_CMD'))
95             $szCmd = $this->getVar('NAV_CMD');
96         $szVariable = "NAV_CMD";
97         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"\">\n";
98         $aReturn[$szVariable] = $szValue;
99        
100         $szVariable = "ROI_TOOL_LOCKED";
101         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"0\">\n";
102         $aReturn[$szVariable] = $szValue;
103        
104         return $aReturn;
105     }
106    
107     /**
108      * GetJavascriptInitFunctions
109      *
110      * Functions to be called at the end of the load.
111      */
112     function GetJavascriptInitFunctions()
113     {
114         $aReturn = $this->moButton->GetJavascriptInitFunctions();
115
116         $szJsFunctionName = "ROIPolygonToolInit";
117         $szFunction = "$szJsFunctionName();\n";
118         $aReturn[$szJsFunctionName] = $szFunction;
119
120         $aReturn['InitCWCROIManagerForm'] = 'goCWCROIManager.SetFormObject('.$this->mszHTMLForm.');';
121        
122         return $aReturn;
123     }
124
125     /**
126      * javascript include files
127      */
128     function GetJavascriptIncludeFunctions()
129     {
130         $aReturn = parent::GetJavascriptIncludeFunctions();
131        
132         $szVar = "cwc_roi.js";
133         $aReturn[$szVar] = '<script src="'.$_SESSION['gszCoreWebPath'].
134                             '/widgets/js/cwc_roi.js" type="text/javascript"></script>';
135                            
136         $szVar = "cwc_line.js";
137         $aReturn[$szVar] = '<script src="'.$_SESSION['gszCoreWebPath'].
138                             '/widgets/js/cwc_line.js" type="text/javascript"></script>';                           
139        
140         $szVar = "cwc_dhtml.js";
141         $aReturn[$szVar] = '<script src="'.$_SESSION['gszCoreWebPath'].
142                             '/widgets/js/cwc_dhtml.js" type="text/javascript"></script>';
143         return $aReturn;
144     }
145
146     /**
147      * @desc javascript mousemove functions
148      */
149     function GetJavascriptOnMouseMoveFunctions()
150     {
151         $aReturn = parent::GetJavascriptOnMouseMoveFunctions();
152        
153         $aReturn['ROIPolygonTool'] = 'ROIPolygonToolMouseMove(e);';
154
155         return $aReturn;
156     }
157
158     /**
159      * @desc general javascript functions
160      */
161     function GetJavascriptFunctions()
162     {
163         $aReturn = parent::GetJavascriptFunctions();
164
165         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
166           $bCWCJSAPI = 1;
167         else
168           $bCWCJSAPI = 0;
169          
170         // get the chameleon images url
171         $szImagesURL = trim(str_replace("\\", '/', $_SESSION['gszCoreWebPath']));
172         $szImagesURL = substr( $szImagesURL, -1 ) == '/' ? $szImagesURL.'images/' :
173                                                             $szImagesURL.'/images/';         
174          
175         // determine the number of points to use
176         if ( isset($this->maParams["NUMBEROFPOINTS"] ) )
177             $nPoints = intval( $this->maParams["NUMBEROFPOINTS"] );
178         else
179             $nPoints = $this->mnNumPoints;
180            
181         // add JSAPI mode functions 
182         if ($bCWCJSAPI)
183         {
184             $szJsFunctionName = "ROIPolygonToolWRegisterForEvent";
185             $szFunction = <<<EOT
186 /**
187  * {$szJsFunctionName}
188  * called to register events when the layers change (JSAPI)
189  */
190 function {$szJsFunctionName}()
191 {
192     goCWCJSAPI.RegisterEvent(MAP_EXTENT_CHANGED, "ROIPolygonToolWExtentChanged");
193     goCWCJSAPI.RegisterEvent(MAP_PROJECTION_CHANGED, "ROIPolygonToolWExtentChanged");
194 }
195 EOT;
196
197            $aReturn[$szJsFunctionName] = $szFunction;
198            $szJsFunctionName = "ROIPolygonToolWExtentChanged";
199            $szFunction = <<<EOT
200 /**
201  * {$szJsFunctionName}
202  * called when the extent is changed (JSAPI)
203  */
204 function {$szJsFunctionName}()
205 {
206     // re-draw the ROI
207     goCWCROIManager.SetGeoExtents( goCWCJSAPI.oMap.minx, goCWCJSAPI.oMap.miny,
208                                     goCWCJSAPI.oMap.maxx, goCWCJSAPI.oMap.maxy );
209     goCWCROIManager.Redraw();
210 }
211 EOT;
212             $aReturn[$szJsFunctionName] = $szFunction;
213
214         }
215        
216         $nId = $this->mnId;
217         $szName = 'ROIPolygonTool'.$nId;
218         $szFunction = <<<EOT
219         function {$szName}()
220         {
221             goCWCROIManager.SetROITool('{$nId}');
222         }
223 EOT;
224         $aReturn[$szName] = $szFunction;
225        
226         $szName = 'ROIPolygonToolInit';
227         $szFunction = <<<EOT
228 function {$szName}()
229 {
230     // create new layers to use with line drawing
231     var sId = "";
232     gaszPolygonDivPointNames = new Array();
233     gaszPolygonDivNodeNames = new Array();
234     var content = "<img  src='{$szImagesURL}ruler_pix.gif' width='6' height='6'><br>";
235     var content2 = "<img  src='{$szImagesURL}ruler_node.gif' width='7' height='7'><br>";
236     for (var i=1; i < {$nPoints}; i++)
237     {
238         // create the point layers
239         sId = 'polygonPointLayer'+i;
240         CWCDHTML_CreateLayer(sId, 10, 10, 10, 10, false, content);
241         CWCDHTML_SetLayerZOrder( sId, 20 );
242         gaszPolygonDivPointNames.push( sId );
243
244         // create the node layers
245         sId = 'polygonNodeLayer'+i;
246         CWCDHTML_CreateLayer(sId, 10, 10, 10, 10, false, content2);
247         CWCDHTML_SetLayerZOrder( sId, 20 );
248         gaszPolygonDivNodeNames.push( sId );
249     }
250    
251     // init polygon object
252     goROIPolygonObj = new CWCPolygonROI({$nPoints});
253     goROIPolygonObj.pointImageWidth = 6;
254     goROIPolygonObj.nodeImageWidth = 7;
255     goROIPolygonObj.left = gMapWhspc;
256     goROIPolygonObj.top = gMapWvspc;
257     goROIPolygonObj.right = gMapWhspc + gMapWiWidth;
258     goROIPolygonObj.bottom = gMapWvspc + gMapWiHeight;
259     goROIPolygonObj.edgeColor = goCWCROIManager.szEdgeColor;
260     goROIPolygonObj.edgeWidth = goCWCROIManager.nEdgeWidth;
261     goROIPolygonObj.fillLayer = goCWCROIManager.bUseFill;
262     goROIPolygonObj.fillColor = goCWCROIManager.szFillColor;
263     goROIPolygonObj.fillOpacity = goCWCROIManager.nFillOpacity; 
264    
265     // show the polygon
266     goROIPolygonObj.Show();   
267    
268 }
269 EOT;
270         $aReturn[$szName] = $szFunction;
271        
272         $szObjectJS = "    var jsObj;\n";
273        
274         /**
275          * TODO: this does not reconstitute the points associated with the
276          * ROI tool because this is complicated ;)  In reality, the points may
277          * not be at valid locations.  The only reason to reconstitute the points
278          * would be to enable editing, which can't happen
279          */
280         if (isset($_SESSION['ROIRENDERER']))
281         {
282             $nROI = count($_SESSION['ROIRENDERER']);
283             for ($i=0; $i<$nROI; $i++)
284             {
285                 $aROI = $_SESSION['ROIRENDERER'][$i];
286                 if ($aROI['type'] == 'polygon')
287                 {
288                     $szObjectJS .= "    goCWCROIManager.SetMode(".
289                                    $aROI['mode'] . ");\n";
290                     $szObjectJS .= "    jsObj = new CWCPolygonROI();\n";
291                     $szObjectJS .= "    jsObj.edgeColor = \"".$aROI['edgecolor']."\";\n";
292                     $szObjectJS .= "    jsObj.edgeWidth = ".$aROI['edgewidth'].";\n";
293                     $szObjectJS .= "    jsObj.fillLayer = goCWCROIManager.bUseFill;\n";
294                     $szObjectJS .= "    jsObj.fillColor = \"".$aROI['fillcolor']."\";\n";
295                     $szObjectJS .= "    jsObj.fillOpacity = ".$aROI['fillopacity'].";\n";
296                     $szObjectJS .= "    goCWCROIManager.Insert( jsObj, $i );\n";
297                 }
298             }
299             if (strlen( $szObjectJS ) > 0)
300             {
301                 $szObjectJS .= "    goCWCROIManager.UpdateLayerVisibility();\n";
302             }
303         }
304        
305        
306         $szName = 'ROIPolygonToolInitObjects';
307         $szFunction = <<<EOT
308 function {$szName}()
309 {
310 {$szObjectJS}
311 }
312 EOT;
313         $aReturn[$szName] = $szFunction;
314
315         $szName = 'ROIPolygonToolDrawFloater';
316         $szFunction = <<<EOT
317 function {$szName}()
318 {
319         // set or default the start points
320         var nTmpStartX = 0;
321         var nTmpStartY = 0;
322         nTmpNodeSpacing = 10;
323         if ( goROIPolygonObj.aNodeCoords[0] )
324         {
325             nTmpStartX = goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][0];
326             nTmpStartY = goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][1];
327             nTmpNodeSpacing = gnROIPolygonPointSpacing;
328         }
329        
330         // delete old floater
331         goFloaterLine.deleteLine();
332
333         // create and setup the new floater line
334         goFloaterLine = new Line( nTmpStartX, nTmpStartY,
335             gROIToolmouseX, gROIToolmouseY, false );
336         goFloaterLine.pointSpacing = nTmpNodeSpacing;
337         goFloaterLine.layerNodeList = gaszPolygonDivNodeNames;
338         goFloaterLine.layerPointList = gaszPolygonDivPointNames;
339        
340         // limit points to 3
341         goFloaterLine.numPoints = 3
342        
343         // draw the line
344         goFloaterLine.drawLine();
345 }
346 EOT;
347         $aReturn[$szName] = $szFunction;
348        
349         // get translated text for this section
350         $szTmpNotice = $this->moMLT->get( '2', 'Press `esc` to cancel or any other key to close the polygon.' );
351        
352         $szName = 'ROIPolygonToolMouseDown';
353         $szFunction = <<<EOT
354 function {$szName}(e)
355 {
356     // skip if not on map or locked
357     if ( !MapWinsideMap() || {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value == 1 )
358     {
359         return true;
360     }
361          
362     ROIPolygonToolGetMousePosition(e);
363
364     if (gROIToolmouseX >=0 && gROIToolmouseY >=0)
365     {
366         gROIPolygonToolx1 = gROIToolmouseX;
367         gROIPolygonTooly1 = gROIToolmouseY;
368        
369         // flag that the polygon is open
370         gROIPolygonOpen = true;
371        
372         // draw next node
373         ROIPolygonDrawNode();
374     }
375    
376     // set status message
377     //window.status='{$szTmpNotice}';
378    
379     return false;
380 }
381 EOT;
382         $aReturn[$szName] = $szFunction;
383        
384         $szName = 'ROIPolygonToolMouseUp';
385         $szFunction = <<<EOT
386 function {$szName}(e)
387 {
388     return null;
389 }
390 EOT;
391         $aReturn[$szName] = $szFunction;
392        
393         $szName = 'ROIPolygonToolGetMousePosition';
394         $szFunction = <<<EOT
395 function {$szName}(e)
396 {
397     if (CWCIsNav4 || CWCIsNav6) // Netscape
398     {               
399         gROIToolmouseX = e.pageX;
400         gROIToolmouseY = e.pageY;
401     }
402     else if (CWCIsIE) // IE
403     {           
404         gROIToolmouseX = event.clientX + document.body.scrollLeft;
405         gROIToolmouseY = event.clientY + document.body.scrollTop;
406     }
407     else
408     {                              // Don't know
409         gROIToolmouseX = gROIToolmouseY = 0;
410     }
411     var oImage = CWCDHTML_GetImage('mapimage');
412    
413     if (oImage == null)
414     {
415         gROIToolmouseX = -1;
416         gROIToolmouseY = -1
417         return false;
418     }
419     var left = CWCDHTML_FindObjectPosX( oImage );
420     var top = CWCDHTML_FindObjectPosY( oImage );
421    
422     var width = oImage.width;
423     var height = oImage.height;
424    
425     // subtract offsets from page left and top
426     var nROIToolmouseX = gROIToolmouseX - left;
427     var nROIToolmouseY = gROIToolmouseY - top;
428    
429     if (nROIToolmouseX < 0 || nROIToolmouseX > width ||
430         nROIToolmouseY < 0 || nROIToolmouseY > height)
431     {
432         gROIToolmouseX = -1;
433         gROIToolmouseY = -1
434     }
435     return true;
436 }
437 EOT;
438         $aReturn[$szName] = $szFunction;
439        
440         $szName = 'ROIPolygonToolMouseMove';
441         $szFunction = <<<EOT
442 function {$szName}(e)
443 {
444     ROIPolygonToolGetMousePosition(e);
445    
446     if (gROIToolmouseX >=0 && gROIToolmouseY >=0 &&
447         {$this->mszHTMLForm}.NAV_CMD != null &&
448         {$this->mszHTMLForm}.NAV_CMD.value == "ROI_TOOL" &&
449         {$this->mnId} == goCWCROIManager.GetROITool() )
450     {
451         // capture events
452         if ( CWCIsNav4 || CWCIsNav6 )
453         {
454             document.captureEvents(Event.MOUSEDOWN);
455             document.captureEvents(Event.MOUSEMOVE);
456             document.captureEvents(Event.RESIZE);
457         }
458         if( CWCIsNav4 )
459         {
460             document.onmousedown = ROIPolygonToolMouseDown;
461             document.onmouseup = ROIPolygonToolMouseUp;
462             document.onKeyPress = ROIPolygonToolKeyPress;
463         }
464         else if ( CWCIsIE || CWCIsNav6 )
465         {
466             var layer  = document.getElementById("MapLayerDiv");
467             document.onmousedown = ROIPolygonToolMouseDown;
468             document.onmouseup = ROIPolygonToolMouseUp;           
469             document.onkeypress = ROIPolygonToolKeyPress;
470            
471             if ( CWCIsNav6 )
472               document.addEventListener("keypress", ROIPolygonToolKeyPress, true);
473         }
474
475         if ( gROIPolygonOpen )
476         {
477             // update end point of current line
478             gROIPolygonToolx2 = gROIToolmouseX;
479             gROIPolygonTooly2 = gROIToolmouseY;
480            
481             // continuously update the floater line
482             ROIPolygonToolDrawFloater();
483         }   
484     }
485
486     return false;
487 }
488 EOT;
489         $aReturn[$szName] = $szFunction;
490
491         // get translated text for this function
492         $szTmpNotice1 = $this->moMLT->get( '0', 'A polygon may not overlap itself.  The side you are attempting to draw intersects with an existing side of the polygon.' );
493         $szTmpNotice2 = $this->moMLT->get( '1', 'Unable to close polygon.  Closing the polygon would cause the final side to intersect with an existing side of the polygon.  Click `OK` and either add another line segment so that the polygon will not overlap itself or use the escape key to cancel.' );
494
495         $szName = 'ROIPolygonDrawNode';
496         $szFunction = <<<EOT
497 function {$szName}()
498
499     // calculate coordinates
500     var nPreviousX = gROIPolygonToolx1;
501     var nPreviousY = gROIPolygonTooly1;
502     var bDefaulted = 1;
503     var nNumNodes = 0;
504     if ( goROIPolygonObj.aNodeCoords )
505         nNumNodes = goROIPolygonObj.aNodeCoords.length;
506        
507     if ( nNumNodes > 0 )
508     {
509         nPreviousX = goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][0];
510         nPreviousY = goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][1];
511         bDefaulted = 0;
512     }
513    
514     // skip if coordinates match (i.e. double event triggered)
515     if ( nPreviousX == gROIPolygonToolx1 && nPreviousY == gROIPolygonTooly1 && bDefaulted == 0 )
516         return null;
517        
518     // loop through array of lines an check for overlap
519     if ( {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value != 1 && gaROIPolygonObj )
520     {
521         var oTmpLine = new Line( nPreviousX, nPreviousY, gROIPolygonToolx1, gROIPolygonTooly1, false );
522         for ( var i=0; i<gaROIPolygonObj.length; i++ )
523         {
524             // check for intersection
525             if ( oTmpLine.intersectsWith( gaROIPolygonObj[i] ) )
526             {
527                 // give message
528                 alert('{$szTmpNotice1}');
529                
530                 // lock the tool on a timer
531                 {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value = 1;
532                 setTimeout( "ROIPolygonToolUnlockTool()", 500 );           
533                
534                 // exit function
535                 return null;
536             }
537         }
538     }
539    
540     // delete floater
541     goFloaterLine.deleteLine();   
542    
543     // draw line
544     ROIPolygonToolDrawLine( nPreviousX, nPreviousY, gROIPolygonToolx1, gROIPolygonTooly1 );   
545    
546     // return
547     return null;
548 }
549 EOT;
550         $aReturn[$szName] = $szFunction;       
551        
552         $szName = 'ROIPolygonToolUnlockTool';
553         $szFunction = <<<EOT
554 function {$szName}()
555 {
556     // reset the lock
557     {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value = 0;
558     return null;
559 }
560 EOT;
561         $aReturn[$szName] = $szFunction;
562        
563         $szName = 'ROIPolygonToolKeyPress';
564         $szFunction = <<<EOT
565 function {$szName}(e)
566 {
567     // only process if polygon tool is selected
568     if ( {$this->mszHTMLForm}.NAV_CMD != null &&
569         {$this->mszHTMLForm}.NAV_CMD.value == "ROI_TOOL" &&
570         {$this->mnId} == goCWCROIManager.GetROITool() &&
571         {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value != 1 )
572     {
573         // delete floater
574         goFloaterLine.deleteLine();
575    
576         // check which key was pressed
577         var nKey;
578        
579         // capture based on browser type
580         if ( CWCIsIE )
581             nKey = event.keyCode;
582         else
583             nKey = e.keyCode;
584        
585         switch ( nKey )
586         {
587             case 27:
588                 // esc was pressed so abort       
589                 ROIPolygonToolClearPolygon();
590                 break;
591                
592             default:
593                 // close polygon
594                 ROIPolygonToolClosePolygon();
595         }
596     }
597
598     // return   
599     return null;
600 }
601 EOT;
602         $aReturn[$szName] = $szFunction;
603        
604         $szName = 'ROIPolygonToolClosePolygon';
605         $szFunction = <<<EOT
606 function {$szName}()
607 {
608     // loop through array of lines an check for overlap
609     if ( {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value != 1 )
610     {
611         var oTmpLine = new Line(  goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][0],
612                                 goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][1],
613                                 goROIPolygonObj.aNodeCoords[0][0],
614                                 goROIPolygonObj.aNodeCoords[0][1], false );
615         for ( var i=0; i<gaROIPolygonObj.length; i++ )
616         {
617             // check for intersection
618             if ( oTmpLine.intersectsWith( gaROIPolygonObj[i] ) )
619             {
620                 // give message
621                 alert('{$szTmpNotice2}');
622                
623                 // lock the tool on a timer
624                 {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value = 1;
625                 setTimeout( "ROIPolygonToolUnlockTool()", 500 );           
626                
627                 // exit function
628                 return null;
629             }
630         }
631     }
632
633     // close polygon
634     gROIPolygonOpen = false;
635    
636     // clear statusbar
637     //window.status='';
638
639     // draw line
640     ROIPolygonToolDrawLine( goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][0],
641                             goROIPolygonObj.aNodeCoords[goROIPolygonObj.aNodeCoords.length-1][1],
642                             goROIPolygonObj.aNodeCoords[0][0],
643                             goROIPolygonObj.aNodeCoords[0][1], false );
644     // add aoi
645     goCWCROIManager.Add( goROIPolygonObj );
646
647     // wait to hide the points (need to see the polygon close)
648     setTimeout( 'ROIPolygonToolClearPolygon()', 800 );
649    
650     // lock the tool on a timer
651     {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value = 1;
652     setTimeout( "ROIPolygonToolUnlockTool()", 500 );
653    
654     // return
655     return true;   
656 }
657 EOT;
658         $aReturn[$szName] = $szFunction;       
659
660         $szName = 'ROIPolygonToolDrawLine';
661         $szFunction = <<<EOT
662 function {$szName}( x1, y1, x2, y2, bDrawStartNode )
663 {
664     // draw a new line if necessary
665     var nLineCount = 0;
666     if ( goROIPolygonObj.aNodeCoords )
667         nLineCount = goROIPolygonObj.aNodeCoords.length;
668        
669     if ( nLineCount == 0 )
670     {
671         var oLine = new Line( x1, y1, x2, y2, bDrawStartNode );
672         oLine.layerNodeList = gaszPolygonDivNodeNames;
673         oLine.layerPointList = gaszPolygonDivPointNames;
674        
675         // ensure that no nodes are drawn
676         oLine.numPoints = 0;
677                              
678         // draw the line
679         oLine.drawLine();
680        
681         // add to array of line objects
682         gaROIPolygonObj.push( oLine );       
683     }
684     else if ( nLineCount > 0 )
685     {
686         var oLine = new Line( x1, y1, x2, y2, bDrawStartNode );
687         oLine.pointSpacing = gnROIPolygonPointSpacing;
688         oLine.layerNodeList = gaszPolygonDivNodeNames;
689         oLine.layerPointList = gaszPolygonDivPointNames;
690        
691         // draw the line
692         oLine.drawLine();
693        
694         // add to array of line objects
695         gaROIPolygonObj.push( oLine );
696     }
697    
698     // check if no more nodes then redraw all lines using double spacing
699     if ( gaszPolygonDivPointNames.length == 0 )
700     {
701         // update global point spacing
702         gnROIPolygonPointSpacing = gnROIPolygonPointSpacing * 2;
703        
704         // loop through the list lines, delete, and recreate
705         nLineCount = gaROIPolygonObj.length;       
706         for ( var i=0; i<nLineCount; i++ )
707         {
708             gaROIPolygonObj[i].deleteLine();
709             gaROIPolygonObj[i].pointSpacing = gnROIPolygonPointSpacing;
710             gaROIPolygonObj[i].drawLine();
711         }
712     }
713    
714     // record node click if polygon is open
715     if ( gROIPolygonOpen )
716     {
717         var aTmpArray = new Array( gROIPolygonToolx1, gROIPolygonTooly1 );
718         goROIPolygonObj.aNodeCoords.push( aTmpArray );
719     }
720 }
721 EOT;
722         $aReturn[$szName] = $szFunction;       
723            
724         $szName = 'ROIPolygonToolClearPolygon';
725         $szFunction = <<<EOT
726 function {$szName}()
727 {
728     // delete all lines
729     var nLineCount = gaROIPolygonObj.length;       
730     for ( var i=0; i<nLineCount; i++ )
731         gaROIPolygonObj[i].deleteLine();
732    
733     // reset
734     goROIPolygonObj.aNodeCoords = new Array();
735     gaROIPolygonObj = new Array();
736     gROIPolygonOpen = false;
737     //window.status='';
738     gnROIPolygonPointSpacing = gnInitialROIPolygonPointSpacing;
739     return true;   
740 }
741 EOT;
742         $aReturn[$szName] = $szFunction;       
743        
744         $szName = 'ROIPolygonToolUnlockTool';
745         $szFunction = <<<EOT
746 function {$szName}()
747 {
748     // reset the lock
749     {$this->mszHTMLForm}.ROI_TOOL_LOCKED.value = 0;
750     return null;
751 }
752 EOT;
753         $aReturn[$szName] = $szFunction;       
754        
755         return $aReturn;
756     }
757
758     /**
759      * GetJavascriptVariables
760      *
761      * Return JS global variables and global code.
762      */
763     function GetJavascriptVariables()
764     {
765         $aReturn = array();
766        
767         $szVariable = "gROIToolmouseX";
768         $szValue = " var $szVariable = 0;\n";
769         $aReturn[$szVariable] = $szValue;
770
771         $szVariable = "gROIToolmouseY";
772         $szValue = " var $szVariable = 0;\n";
773         $aReturn[$szVariable] = $szValue;
774
775         $szVariable = "gROIPolygonToolx1";
776         $szValue = " var $szVariable = 0;\n";
777         $aReturn[$szVariable] = $szValue;
778
779         $szVariable = "gROIPolygonTooly1";
780         $szValue = " var $szVariable = 0;\n";
781         $aReturn[$szVariable] = $szValue;
782
783         $szVariable = "gROIPolygonToolx2";
784         $szValue = " var $szVariable = 0;\n";
785         $aReturn[$szVariable] = $szValue;
786
787         $szVariable = "gROIPolygonTooly2";
788         $szValue = " var $szVariable = 0;\n";
789         $aReturn[$szVariable] = $szValue;
790
791         $szVariable = "gROIPolygonOpen";
792         $szValue = " var $szVariable = false;\n";
793         $aReturn[$szVariable] = $szValue;
794
795         $szVariable = "goROIPolygonObj";
796         $szValue = " var $szVariable = null;\n";
797         $aReturn[$szVariable] = $szValue;
798        
799         $szVariable = "gaROIPolygonObj";
800         $szValue = " var $szVariable = new Array();\n";
801         $aReturn[$szVariable] = $szValue;
802        
803         $szVariable = "gaszPolygonDivPointNames";
804         $szValue = " var $szVariable;\n";
805         $aReturn[$szVariable] = $szValue;
806        
807         $szVariable = "gaszPolygonDivNodeNames";
808         $szValue = " var $szVariable;\n";
809         $aReturn[$szVariable] = $szValue;
810        
811         $szVariable = "goFloaterLine";
812         $szValue = " var $szVariable = new Line( 0, 0, 0, 0, false ); ;\n";
813         $aReturn[$szVariable] = $szValue;         
814        
815         // determine point spacing
816         if ( isset($this->maParams["POINTSPACING"] ) )
817             $nPointSpacing = intval( $this->maParams["POINTSPACING"] );
818         else
819             $nPointSpacing = $this->mnPointSpacing;
820         $szVariable = "gnROIPolygonPointSpacing";
821         $szValue = " var $szVariable = $nPointSpacing;\n";
822         $aReturn[$szVariable] = $szValue;           
823            
824         $szVariable = "gnInitialROIPolygonPointSpacing";
825         $szValue = " var $szVariable = $nPointSpacing;\n";
826         $aReturn[$szVariable] = $szValue;
827        
828         return $aReturn;
829     }
830    
831     /**
832      * return an array of javascript functions needed by the Ruler widget
833      * and called when the page is loaded.
834      * @return array of name = function values
835      */
836     function GetJavascriptOnLoadFunctions()
837     {
838         $aReturn = parent::GetJavascriptOnLoadFunctions();
839
840         $aReturn["ROIPolygonToolInitObjects"] = "    ROIPolygonToolInitObjects();\n";
841         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
842         {
843             $szJsFunctionName = "ROIPolygonToolWRegisterForEvent";
844             $szFunction = "$szJsFunctionName();\n";
845             $aReturn[$szJsFunctionName] = $szFunction;
846         }         
847
848         return $aReturn;
849     }
850
851     /**
852      * draw this widget on the page
853      */
854     function DrawPublish($szPostEvent = "")
855     {
856         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
857           $bCWCJSAPI = 1;
858         else
859           $bCWCJSAPI = 0;
860
861         if (!$this->mbVisible)
862             return "<!-- ROIPolygonTool hidden -->";
863
864         $szReturn = $this->moButton->DrawPublish();
865
866         return $szReturn;
867     }
868 }
869 ?>
Note: See TracBrowser for help on using the browser.