####################################################################################################
##     SciSCADA Toolbox
##     Application_bout.tcl
##     Developed by Zhe.Peng & Longhua.Ma
##     Email:pengzhe1113@gmail.com
##     Hangzhou, China
##     Sep.2006
####################################################################################################


set DIRE
set DIRE_main

set proc_temp main

set local_time "00:00:00"
set lastX 300
set lastY 300


set w .application_main
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's application in water treatment:about"
wm state $w zoom 


canvas $w.c -width 1020 -height 758 -bg blue

####################### menu ###########################

	menu $w.menu -tearoff 0

	set mfile $w.menu.file
	menu $mfile -tearoff 0
	$w.menu add cascade -label "File" -menu $mfile -underline 0
	$mfile add command -label "Open" 
	$mfile add separator
	$mfile add command -label "Quit" -command exit

	set minterface $w.menu.interface
	menu $minterface -tearoff 0 
	$w.menu add cascade -label "Interface" -menu $minterface -underline 0
	
	$minterface add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/main.tcl')" 
	}
 
	$minterface add command -label "OPC" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/opc.tcl')" 
	}
 
	$minterface add command -label "Modbus" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/modbus.tcl')" 
	}

	$minterface add command -label "Ethernet" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/ethernet.tcl')" 
	}

	$minterface add command -label "Mysql" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/mysql.tcl')" 
	}

	$minterface add command -label "Serial" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/serial.tcl')" 
	}


	set mapplication $w.menu.application
	menu $mapplication -tearoff 0 
	$w.menu add cascade -label "Application" -menu $mapplication -underline 0
	$mapplication add command -label "Intro to Tong Lu's water supply"  -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_about.tcl')" 
	}

	$mapplication add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/Application_main.tcl')" 
	}

	$mapplication add command -label "Networks" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_net.tcl')" 
	}

	$mapplication add command -label "Data Acquisition" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_acquision.tcl')" 
	}

	$mapplication add command -label "Data Monitor" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_monitor.tcl')" 
	}

	$mapplication add command -label "Data Curve" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_curve.tcl')" 
	}

	

	set mhelp $w.menu.mhelp
	menu $mhelp -tearoff 0
	$w.menu add cascade -label "Help" -menu $mhelp -underline 0
	
	$mhelp add cascade -label "Source codes" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/source.tcl')" 
	}
	


	
	$mhelp add command -label "Help" -command {
        set tmp [file join $DIRE_main index.htm]
        regsub -all "/" $tmp "\\" tmp
        exec $env(COMSPEC) /c $tmp &
	}
	

	
	$mhelp add command -label "About" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/about.tcl')" 
	}
	 
	$w configure -menu $w.menu 

#######################Top frame###########################

frame $w.top -width 1024 -height 600 -bg white
pack $w.top -padx 0 -pady 0
pack propagate $w.top false 
 
set draw_first 1
canvas $w.top.c -width 1024 -height 600 -bg white
scale $w.top.scale -from 1 -to 100 -length 600 -variable scale_x -orient horizontal -showvalue true -bg white

$w.top.c create line 200 450 810 450 -arrow last -width 2
$w.top.c create line 200 450 200 40 -arrow last -width 2
$w.top.c create text 500 20 -text "Realtime curve" -fill brown
$w.top.c create line 200 490 300 490 -fill red -arrow last -width 2
$w.top.c create line 200 520 300 520 -fill blue -arrow last -width 2
$w.top.c create line 200 550 300 550 -fill green -arrow last -width 2
$w.top.c create text 350 490 -text "first tag" -fill red
$w.top.c create text 350 520 -text "second tag" -fill blue
$w.top.c create text 350 550 -text "third tag" -fill green

$w.top.c create line 500 450 500 20 -width 3 -fill red

##############画初始横纵轴

for {set i 0} {$i <= $scale_x} {incr i} {
    set x [expr {200 + ($i*600/($scale_x))}]
    $w.top.c create line $x 450 $x 445 -width 2 -tags line_x
    $w.top.c create text $x 454 -text [expr {$i}] -anchor n  -tags text_x
}
for {set i 0} {$i <= 10} {incr i} {
    set y [expr {450 - ($i*40)}]
    $w.top.c create line 200 $y 205 $y -width 2
    $w.top.c create text 196 $y -text [expr {$i*10}].0 -anchor e 
}


#######################Buttom frame###########################

frame $w.buttom -width 1024 -height 30 -bg white
pack $w.buttom -padx 0 -pady 0
pack propagate $w.buttom false
button $w.buttom.cmd0 -width 27 -height 2 -text "Main" -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_main.tcl')"}
button $w.buttom.cmd1 -width 27 -height 2 -text "Network frame"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_net.tcl')"}
button $w.buttom.cmd2 -width 27 -height 2 -text "Data Acquisition and Storage"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_acquision.tcl')"}
button $w.buttom.cmd3 -width 27 -height 2 -text "Data Monitor"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_monitor.tcl')"}
button $w.buttom.cmd4 -width 27 -height 2 -text "curve"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_curve.tcl')"}
button $w.buttom.cmd5 -width 27 -height 2 -text "About"  -command {ScilabEval "TCL_EvalFile(DIR+'tcl/application_about.tcl')"}


#######################State frame###########################

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.l1 -text "Local time: "
label $w.statusbar.time -textvar local_time -fg red
label $w.statusbar.l2 -text " | "
label $w.statusbar.author -text "Designed by Zhe.Peng Longhua.Ma ZheJiang Univ. china" -fg green
label $w.statusbar.l3 -text " | "
label $w.statusbar.email -text "pengzhe1113@gmail.com" -fg blue
pack  $w.statusbar.email $w.statusbar.l3 $w.statusbar.author $w.statusbar.l2 $w.statusbar.time $w.statusbar.l1  -side right

proc time_loop {} {
global local_time
set m [expr { [clock seconds] - [clock scan 00:00:00] }]
set local_time [format %2.2d:%2.2d:%2.2d [expr {$m/3600}] [expr {($m%3600)/60}] [expr {($m%3600)%60}]]
after 1000 time_loop
}

time_loop 


##############画横轴
proc drawx {} {
global w
global scale_x
catch { $w.top.c delete withtag line_x }
catch { $w.top.c delete withtag text_x }
for {set i 0} {$i <= $scale_x} {incr i} {
    set x [expr {200 + (($i*600)/($scale_x))}]
    $w.top.c create line $x 450 $x 445 -width 2 -tags line_x
    $w.top.c create text $x 454 -text [expr {$i}] -anchor n -tags text_x
}
}

bind $w.top.scale <ButtonRelease-1> "drawx"


###############滑动曲线响应


bind $w.top.c <1> "itemStartDrag $w.top.c %x %y"
bind $w.top.c <B1-Motion> "itemDrag $w.top.c %x %y"

proc itemStartDrag {c x y} {
    global lastX lastY
    set lastX_temp [$c canvasx $x]
    set lastY_temp [$c canvasy $y]
    if {$lastX_temp>200} {
    if {$lastY_temp<800} {
    set lastX $lastX_temp
    set lastY $lastY_temp
    }
    }
}

proc itemDrag {c x y} {
    global lastX lastY
    set x [$c canvasx $x]
    set y [$c canvasy $y]
    if {$x>200} {
    if {$x<800} {
    $c move current [expr {$x-$lastX}] [expr {$y-$lastY}]
    set lastX $x
    set lastY $y
	displaydata
    }
    }
}

proc displaydata {} {
    global lastX 
	global w
    global scale_x
    global arry1 arry2 arry3
    set display_temp [expr {int((($lastX-200)*($scale_x))/600)}]
	catch { $w.top.c delete withtag dispaly }
	$w.top.c create text 500 490 -text [expr {100-int(($arry1($display_temp)-50)/4)}] -tags dispaly -fill red
	$w.top.c create text 500 520 -text [expr {100-int(($arry2($display_temp)-50)/4)}] -tags dispaly -fill blue
	$w.top.c create text 500 550 -text [expr {100-int(($arry3($display_temp)-50)/4)}] -tags dispaly -fill green
}


#######################画实时曲线###########################

proc draw_curve {} {
global arry1
global arry2
global arry3
global w
global scale_x

global draw_first
catch { $w.top.c delete withtag hands }
catch { $w.top.c delete withtag triangle1 }
catch { $w.top.c delete withtag triangle2 }
catch { $w.top.c delete withtag triangle3 }

if {$draw_first} {
for {set i 0} {$i<=100} {incr i} {
set arry1($i) [expr {50+int(400*rand())}]
set arry2($i) [expr {50+int(400*rand())}]
set arry3($i) [expr {50+int(400*rand())}]
}
set draw_first 0
}

for {set i 1} {$i<=$scale_x} {incr i} {
set arry1([expr $i-1]) $arry1($i)
set arry2([expr $i-1]) $arry2($i)
set arry3([expr $i-1]) $arry3($i)
}

for {set i $scale_x} {$i<=$scale_x} {incr i} {
set arry1($i) [expr {50+int(400*rand())}]
set arry2($i) [expr {50+int(400*rand())}]
set arry3($i) [expr {50+int(400*rand())}]
}

for {set i 1} {$i<=$scale_x} {incr i} {
set j [expr {$i+1}]
$w.top.c create line [expr {200+(($i-1)*600/($scale_x))}] $arry1([expr $i-1]) [expr {200+($i*600/($scale_x))}] $arry1($i) -fill red -width 2 -tags hands
$w.top.c create line [expr {200+(($i-1)*600/($scale_x))}] $arry2([expr $i-1]) [expr {200+($i*600/($scale_x))}] $arry2($i) -fill blue -width 2 -tags hands
$w.top.c create line [expr {200+(($i-1)*600/($scale_x))}] $arry3([expr $i-1]) [expr {200+($i*600/($scale_x))}] $arry3($i) -fill green -width 2 -tags hands
}


$w.top.c create poly -20 0 10 -13 10 13 -width 2 -tags triangle1 -fill red
$w.top.c move triangle1 820 $arry1($scale_x) 

$w.top.c create poly -20 0 10 -13 10 13 -width 2 -tags triangle2 -fill blue
$w.top.c move triangle2 820 $arry2($scale_x) 

$w.top.c create poly -20 0 10 -13 10 13 -width 2 -tags triangle3 -fill green
$w.top.c move triangle3 820 $arry3($scale_x) 

displaydata

after 1000 draw_curve

}

draw_curve






#######################packframe###########################

pack $w.top $w.buttom  -side top
pack $w.buttom.cmd0 $w.buttom.cmd1 $w.buttom.cmd2 $w.buttom.cmd3 $w.buttom.cmd4 $w.buttom.cmd5  -side left
grid $w.top.c 
grid $w.top.scale

