;+
;remove contour property
;+
PRO Contour_CleanupEvent,prop

	COMPILE_OPT IDL2

	Widget_Control, prop, Get_UValue=prop_value
	FOR i=0,9 DO BEGIN
		Obj_Destroy, (prop_value.ContourlevelComp)[i]
		Obj_Destroy, (prop_value.oComp)[i]
	ENDFOR
END
;+
;remove contour
;+
PRO sesContour::Remove
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	Obj_Destroy,self.oContours
	oSystem.oWindow->Draw,oSystem.oScene
END
;+
;handle event
;+
PRO sesContour::HandleEvent,event
	COMPILE_OPT IDL2
    oSystem = self.oSystem

	UName = Widget_Info(event.id, /UName)
	tagName = Tag_Names(event, /Structure_Name)


	CASE tagName OF

		'WIDGET_KILL_REQUEST':BEGIN
			Widget_Control, event.top, /Destroy
		END

		'WIDGET_SLIDER':BEGIN
			CASE UName OF
				'alphaSet':BEGIN
					Widget_Control,event.id,Get_Value = alphaValue
					self.alpha = alphaValue/10.
				END
				'nLevelSet':BEGIN
					Widget_Control,event.id,Get_Value = nLevelValue
					self.nLevel = nLevelValue
				END
			ELSE:
			ENDCASE
		END

		'':BEGIN
			CASE UName OF
				'SurfFill':BEGIN
					Widget_Control,event.id,Get_Value = tempValue
					IF tempValue EQ 0 THEN BEGIN
						self.fill = 0
					ENDIF

					IF tempValue EQ 1 THEN BEGIN
						self.fill = 1
					ENDIF
				END
				'Hide':BEGIN
					Widget_Control,event.id,Get_Value = tempValue
					IF tempValue EQ 1 THEN BEGIN
						self.Hide = 1
						self.oContours -> SetProperty,Hide=0
					ENDIF

					IF tempValue EQ 0 THEN BEGIN
						self.Hide = 0
						self.oContours -> SetProperty,Hide=1
					ENDIF
					oSystem.oWindow->Draw,oSystem.oScene
				END
				'ShowLabels':BEGIN
					Widget_Control,event.id,Get_Value = tempValue
					IF tempValue EQ 1 THEN BEGIN
						self.labelhide = 1
						c_Label = IntArr(N_Elements(self.nLevel))
						c_Label[*] = 1
						self.oContours -> SetProperty,C_LABEL_show = c_Label
						self.oContours -> SetProperty,C_USE_LABEL_ORIENTATION = c_label*360
					ENDIF

					IF tempValue EQ 0 THEN BEGIN
						self.labelhide = 0
						c_Label = IntArr(N_Elements(self.nLevel))
;						c_Label[*] = 0
						self.oContours -> SetProperty,C_LABEL_show = c_Label

					ENDIF
					oSystem.oWindow->Draw,oSystem.oScene
				END
			ELSE:
			ENDCASE
		END

		'WIDGET_BUTTON':BEGIN
			CASE UName OF
		 		'Sure':BEGIN
					oSystem.oProcess->mapBase , 1
					Widget_Control, event.top, /Destroy
					oSystem.oWindow->Draw,oSystem.oScene
					FOR i = 0 , 20 DO BEGIN
						oSystem.oProcess->DoProcess , i
					ENDFOR
					self.oContours -> setproperty, C_Color=Transpose(self.conColor)
					self.oContours -> SetProperty, C_Linestyle=self.conLineSet
					self.oContours -> SetProperty, C_Thick=self.conThick
					self.oContours -> SetProperty, C_Label_Interval=self.conWidth

					self.oContours -> SetProperty, Alpha_Channel=self.alpha
					self.oContours -> SetProperty, N_Level=self.nLevel
					IF self.fill EQ 1 THEN BEGIN
						self.oContours -> SetProperty, fill = 1
					ENDIF
					IF self.fill EQ 0 THEN BEGIN
						self.oContours -> SetProperty, fill = 0
					ENDIF
					FOR i = 21 , 35 DO BEGIN
						oSystem.oProcess->DoProcess , i
					ENDFOR
					oSystem.oWindow->Draw,oSystem.oScene

					self.rAlpha = self.alpha * 10
					oSystem.oProcess->mapBase , 0

		 		END

				'Cancel':BEGIN
					self.fill = self.rFill
					Widget_Control, event.top, /Destroy
				END

			ELSE:
			ENDCASE
		END

	ElSE:BEGIN

		IF (event.type EQ 0) THEN BEGIN

			contourValue = Widget_Info(self.prop, Component = event.component  , $
								       Property_Value = event.identifier         )

		    event.component -> GetProperty, UValue = levelname

			FOR i = 0 , 9  DO BEGIN
				name = 'level'+Strtrim(String(i+1),2)
				IF levelname EQ name THEN BEGIN
					CASE event.identifier OF

					 	'MY_COLOR' : BEGIN
		                     self.conColor[i,*] = contourValue
						END
						'MY_LINESTYLE' : BEGIN
						 	 self.conLineSet[i] = contourValue
						END
						'MY_THICK' : BEGIN
						 	 self.conThick[i] = contourValue
						END
						'MY_LABEL_INTERVAL' : BEGIN
						 	 self.conWidth[i] = contourValue
						END
					ELSE :
					ENDCASE
				ENDIF
			ENDFOR
		ENDIF
	ENDELSE
	ENDCASE

END
;+
;create contour edit menu
;+
PRO sesContour::CreateEditMenu

	COMPILE_OPT IDL2

	Device, Get_Screen_Size = screenSize

	xOff = (screenSize[0]-600)/2
	yOff = (screenSize[1]-10)/2

	;ÊôÐÔ¶Ô»°¿ò¶¥base

	self.cBase = Widget_Base(Group_Leader = self.mainBase		, $
			                UName      = 'ContourEdit' 	        , $
			                UValue	   = self					, $
			                Title      = 'Contour Property'		, $
			                Space      = 2          			, $
			                XOffSet    = xOff					, $
			                YOffSet    = yOff					, $
			                /Modal								, $
			                /Toolbar							, $
			                /Column                 			, $
			                /Floating               			, $
			                /Tlb_Kill_Request_Events 		 	  )

	infoBase = Widget_Base(self.cBase, /Row, xPad=10)

    alphaLabel = Widget_Label(infoBase , Value = 'Transparence: ')

    IF self.rAlpha EQ 0 THEN BEGIN
    	self.rAlpha = 10
    ENDIF

	alphaSlider = Widget_Slider(infoBase                , $
	                            Value=self.rAlpha       , $
	                            Scr_XSize=140	        , $
                                Maximum=10	            , $
                                Minimum=1               , $
                                UName='alphaSet'  		  )

	temcBase = Widget_Base(infoBase,xsize=30)

	nLevelLabel = Widget_Label(infoBase , Value = '   Number of Levels: ')

	nLevelSlider = Widget_Slider(infoBase				, $
								Value=self.nLevel		, $
								Scr_XSize=140			, $
								Maximum=10				, $
								Minimum=2				, $
								UName='nLevelSet'		  )

	fillBase = Widget_Base(self.cBase, /Row, xPad=10)
	fillLabel = Widget_Label(fillBase , Value = 'ContourFill: ')

    sPar = ['Lines','Fill']
    surfPar = Cw_Bgroup(fillBase, sPar,UName='SurfFill', COL=2  , $
                        /Exclusive, set_value=self.fill, $
                        /Return_Index)

	self.rFill = self.fill

	hideLabel = Widget_Label(fillBase , Value = '     Show Contours: ')

    hPar = ['']
    hidePar = Cw_Bgroup(fillBase, hPar,UName='Hide', COL=1  , $
                        /NonExclusive, set_value=self.hide, $
                        /Return_Index)

	hideLabel = Widget_Label(fillBase , Value = '  Show Labels: ')

    hPar = ['']
    hidePar = Cw_Bgroup(fillBase, hPar,UName='ShowLabels', COL=1  , $
                        /NonExclusive, set_value=self.labelhide, $
                        /Return_Index)

	conBase = Widget_Base(self.cBase                   , $
                          /Column                 , $
                          Space = 5              , $
                          xPad=10                   )


    conLabel = Widget_Label(conBase,Value = 'Contour Level Setting',/Align_Left)

    levelSet_bs = Widget_Base(conBase,Frame=1)

    buttonbs = Widget_Base(conBase, /row, space=10, /ALIGN_CENTER)

    oComp = ObjArr(10)
    ContourlevelComp = ObjArr(10)

 	FOR i=0, 9 DO BEGIN
		oComp[i] = Obj_New('IDLitvisContour')
		self.ContourContainer->Add,oComp[i]
	ENDFOR

    arr = Transpose(self.conColor)

 	FOR i=0, 9 DO BEGIN
    	ContourlevelComp[i] = Obj_New('IDLitVisContourlevel'                , $
								 	  name='Level'+Strtrim(String(i+1),2)    , $
								 	  uvalue='level'+Strtrim(String(i+1),2) , $
								      _contour=oComp[i])
	    ContourlevelComp[i]->SetPropertyAttribute, 'MY_LABEL_COLOR', /HIDE
        ContourlevelComp[i]->SetPropertyAttribute, 'MY_LABEL_NOGAPS', /HIDE
        ContourlevelComp[i]->SetPropertyAttribute, 'MY_LABEL_SYMBOL', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'MY_LABEL_TEXT', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'MY_LABELLING', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'MY_SYMBOL_SIZE', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'MY_USE_LABEL_COLOR', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'TICKFORMAT', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'TICK_DEFINEDFORMAT', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'FONT_INDEX', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'FONT_STYLE', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'FONT_SIZE', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'MY_VALUE', /HIDE
    	ContourlevelComp[i]->SetPropertyAttribute, 'MY_COLOR', NAME=' Color'
        ContourlevelComp[i]->SetPropertyAttribute, 'MY_LINESTYLE', NAME=' Line Style'
        ContourlevelComp[i]->SetPropertyAttribute, 'MY_THICK', NAME=' Line Thick'
        ContourlevelComp[i]->SetPropertyAttribute, 'MY_LABEL_INTERVAL', NAME=' Label Interval'
        ContourlevelComp[i]->setproperty, MY_COLOR=arr[*,i]
        ContourlevelComp[i]->setproperty, MY_LINESTYLE=self.conLineSet[i]
        ContourlevelComp[i]->setproperty, MY_THICK=self.conThick[i]
        ContourlevelComp[i]->setproperty, MY_LABEL_INTERVAL= self.conWidth[i]
        self.ContourContainer->Add,ContourlevelComp[i]
 	ENDFOR


    h_laber = Widget_label(LevelSet_bs           , $
                           Value=''  			 , $
                           /Align_Center         , $
                           /Dynamic_Resize       , $
                           yOffSet=5             , $
                           xOffSet=2               )

	ContourLevelValue=ObjArr(self.nLevel)

	For i=0,self.nLevel-1 DO BEGIN
		ContourLevelValue[i] = ContourlevelComp[i]
	ENDFOR

   	prop = WIDGET_PROPERTYSHEET(levelSet_bs                              , $
  								/SUNKEN_FRAME                            , $
  								XSIZE = 85                               , $
  								UNAME = 'LevelSheet'                     , $
			   					VALUE = ContourLevelValue                , $
			   					KILL_NOTIFY='Contour_CleanupEvent'         )

    self.prop = prop
	Widget_Control, prop, set_UValue={ContourlevelComp :  ContourlevelComp , $
	                                  oComp            :  oComp              }


	butBase = Widget_Base(self.cBase, /Row, xPad=10,/Align_Right,Space = 10)

    sureButton = Widget_Button(butBase,XSize = 60 , UName = 'Sure' , Value = 'Ok')
	canButton = Widget_Button(butBase,XSize = 60 , UName = 'Cancel' , Value = 'Cancel')

	Widget_Control,self.cBase,/REALIZE

	XManager, 'SunEle'                              , $
	           self.cBase                           , $
	           /No_Block                            , $
	           Event_Handler = 'sesFrame_Event'   , $
  			   Cleanup = 'sesFrame_Cleanup'
END
;+
;create contour
;+
PRO sesContour::Create
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	self.ContourContainer = Obj_New('IDL_Container')
	(oSystem.CurrentPro).oImage->GetProperty,Data=dataZ
	minv = self.MinMaxXY[0]
	maxv = self.MinMaxXY[1]
	temp = Where(dataZ LT 127 ,count)
	IF count NE 0 THEN BEGIN
		dataZ[temp] = self.MinMaxXY[0]
	ENDIF
	temp = Where(dataZ GE 127 ,count)
	IF count NE 0 THEN BEGIN
		dataZ[temp] = self.MinMaxXY[1]
	ENDIF
	ImageSize = Size(dataZ , /Dimensions)
	dataZ = Smooth(dataZ,5, /Edge_Truncate)

	c_Label = IntArr(N_Elements(self.nLevel))
	c_Label[*] = 0

	self.oContours=Obj_New('IDLgrContour' , dataZ			  		  , $
	                        Fill = 0			                      , $
	                        Geomz=0                                   , $
	                        Alpha_Channel = 1 		                  , $
							C_Label_Interval = self.conWidth          , $
							C_Label_Nogaps = 0 	                      , $
							C_Linestyle =  self.conLineSet            , $
							C_Thick = self.conThick                   , $
							/Register_Properties                      , $
							Min_Value = minv                          , $
            				Max_Value = maxv                          , $
                            Planar = 1                     			  , $
                            N_Levels = self.nLevel					  , $
                            Shading = 1                               , $
                            C_Label_show = c_Label          	  	  , $
                            Label_Font = self.contour_font	          , $
	                        C_Use_Label_Orientation = c_Label*360	  , $
                            C_Color = Transpose(self.conColor)          )

	(oSystem.CurrentPro).oModel->Add,self.oContours

	oSystem.oWindow->Draw,oSystem.oScene
	dataZ = 0
END
;+
;clean up
;+
PRO sesContour::CleanUp
	COMPILE_OPT IDL2
	Obj_Destroy,self.contour_font
	Obj_Destroy,self.ContourContainer
	self->IDLitComponent::Cleanup
END
;+
;initialize
;+
FUNCTION sesContour::Init , oSystem = oSystem , MinMaxXY = MinMaxXY
	COMPILE_OPT IDL2
	self.oSystem = oSystem
	self.mainBase = oSystem.mBase
	self.rootDir = oSystem.rootDir
	self.MinMaxXY = MinMaxXY
	self.contour_font=Obj_New('IDLgrfont','hershey*4',size=8)
	self.conLineSet = 0
	self.alpha = 1
	self.conThick = 1
	self.nLevel = 4
	self.conWidth = 0.7
	self.conColor = [[255,250,255,255,100,0,160,0,0,180] , $
	                 [0,120,200,255,255,255,250,120,0,0] , $
					 [0,0,64,0,160,0,225,250,255,250]]
	self.hide = 1
	self->Create

	Return,1

END
;+
;Contour class name
;+
PRO sesContour__Define
	COMPILE_OPT IDL2
	void = { sesContour                     , $
			 INHERITS IDLitComponent	    , $
			 ;system parameter
			 mainBase      :    0L          , $
			 cBase         :    0L          , $
			 rootDir       :    ''          , $
			 conLineSet	   :	IntArr(10)	, $
			 conThick	   :	IntArr(10)	, $
		 	 conWidth	   :	FltArr(10)	, $
			 conColor	   :	IntArr(10,3), $
			 ralpha		   :	0.			, $
			 nLevel		   :	0			, $
			 alpha         :    0.          , $
			 hide          :    0           , $
			 fill          :    0           , $
			 rFill         :    0           , $
			 labelhide     :    0           , $
			 prop          :    0L          , $
			 MinMaxXY      :    [0.0,0.0]   , $
			 ;system objct
	         oContours     :    Obj_New()   , $
	         contour_font  :	Obj_New()	, $
	         ContourContainer:  Obj_New()   , $
	         oSystem       :    Obj_New()     $
	         }

END