#=============================================== # GNUPlotView #=============================================== package GNUPlotView; use TkFittingCommon; @ISA = qw(TkFittingCommon); #use TkPlotModule; #@ISA = qw(Rietan TkPlotModule); #公開したいサブルーチン #@EXPORT = qw(DelSpace Reduce01 MakePath RegExpQuote); use strict; use Utils; use MyTk::GraphFrameArray; use GraphData; #============================================================ # Global変数 #============================================================ #my $material = new Material; my $Program = "GNUPlot2010"; my $EntryWidth = 12; my $TextBoxWidth = 30; my $TextBoxHeight = 10; #============================================================ # コンストラクタ、デストラクタ #============================================================ sub new { my ($class, $app, @a) = @_; # my ($module, $app, $canvas) = @_; my $self = TkFittingCommon->new($app, @a); my $this = bless $self, $class; # my $this = {}; # bless $this; $this->SetApplication($app) if($app); # $this->SetCanvas($canvas) if($canvas); return $this; } sub DESTROY { my $this = shift; $this->SUPER::DESTROY(@_); } #============================================================ # 継承クラスで定義しなおす関数 #============================================================ sub CreateWidgets { my ($this) = @_; my $App = $this->App(); my $args = $App->Args(); $this->{ini} = new IniFile; $this->{dini} = new IniFile; $this->InitializeParameters($this->{ini}, $this->{dini}); # $this->SUPER::CreateWidgets(0, 1); # $this->SetTitle("$Program / TkPlot"); # $this->App()->SetProgram("$Program / TkPlot"); #=================================================== # ペインを作製 #=================================================== my $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeChooseFileEntry($Frame, 'Param', 'Param:', $this->{ini}->pVariable("ParameterFile", ""), '&Choose', sub { $this->ChooseFile(@_); }, "ChooseParameterFile"); $this->{SaveParamButton} = $Frame->MyButton( -text => '&Save', -takefocus => 1, -command => [\&ButtonPressed, $this, 'RButtonDown', 'SaveParam'], )->pack(-side => 'left'); $this->{EditParamButton} = $Frame->MyButton( -text => 'ed', -takefocus => 1, -command => [\&ButtonPressed, $this, 'RButtonDown', 'EditParam'], )->pack(-side => 'left'); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeChooseFileEntry($Frame, 'TempPath', 'Temp:', $this->{ini}->pVariable("TempPath", "d:/temp/Temp.txt"), '&Choose', sub { $this->ChooseFile(@_); }, "ChooseTempFile"); $this->{EditTempButton} = $Frame->MyButton( -text => 'ed', -takefocus => 1, -command => [\&ButtonPressed, $this, 'RButtonDown', 'EditTemp'], )->pack(-side => 'left'); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeChooseFileEntry($Frame, 'OutputPath', 'Output:', $this->{ini}->pVariable("OutputPath", "d:/temp/output.pdf"), '&Choose', sub { $this->ChooseFile(@_); }, "ChooseOutputFile"); $this->{EditTempButton} = $Frame->MyButton( -text => 'ed', -takefocus => 1, -command => [\&ButtonPressed, $this, 'RButtonDown', 'EditOutput'], )->pack(-side => 'left'); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); my $lsb1 = $this->{ChooseTerminalListBox} = $Frame->MyBrowseEntry( -label => "Terminal:", -state => "readonly", -takefocus => 1, -width => 5, -Selections => [], -SelIndex => 0, )->pack(-side => 'left', -expand => 'yes', -fill => 'x'); $lsb1->configure(-browsecmd => [\&SelChangeListBox, $this, "X", $this->{ChooseXListBox}]); $lsb1->AddItem("wxt"); $lsb1->AddItem("pdf"); $lsb1->AddItem("postscript"); $lsb1->AddItem("jpeg"); $lsb1->AddItem("emf"); $lsb1->AddItem("gif"); $lsb1->AddItem("cgm"); $lsb1->AddItem("dxf"); $lsb1->AddItem("eepic"); $lsb1->AddItem("fig"); $lsb1->AddItem("hpgl"); $lsb1->AddItem("latex"); $lsb1->AddItem("bm"); $lsb1->AddItem("png"); $lsb1->AddItem("tgif"); $lsb1->AddItem("tpic"); $lsb1->SetText("wxt"); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "Title", "Graph title:", $this->{ini}->pVariable('Title', 'Graph title'), "%s", $EntryWidth*5, "", 0, 0); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "Function", "Function:", $this->{ini}->pVariable('Function', 'sin(x)'), "%s", $EntryWidth*5, "", 0, 0); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "PlotCommand", "Plot command:", $this->{ini}->pVariable('PlotCommand', 'plot'), "%s", $EntryWidth*5, "", 0, 0); $Frame = $this->MyFrame()->pack(-anchor => 'nw'); $Frame->MyCheckbutton( -text => 'Plot line', -takefocus => 1, -variable => $this->{ini}->pVariable('PlotLine', 1), -onvalue => 1, -offvalue => 0, -indicatoron => 1, )->pack(-side => 'left', -fill => 'x'); # $this->MakeLabelEntry($Frame, "LineWidth", "Width:", # $this->{ini}->pVariable('LineWidth', 1), "%d", $EntryWidth, "", 0, 0); $Frame->MyCheckbutton( -text => 'Plot symbol', -takefocus => 1, -variable => $this->{ini}->pVariable('PlotSymbol', 1), -onvalue => 1, -offvalue => 0, -indicatoron => 1, )->pack(-side => 'left', -fill => 'x'); my $lsb1 = $this->{ChooseSymbolTypeBox} = $Frame->MyBrowseEntry( -label => "Symbol:", -state => "readonly", -takefocus => 1, -width => 5, -Selections => ['+', 'X', '*', 'square', 'solid square', 'circle', 'solid circlse', 'triangle', 'solid triangle', 'rev triangle', 'solid rev triangle', 'diamond', 'solid diamon'], -SelIndex => 4, )->pack(-side => 'left', -expand => 'yes', -fill => 'x'); $lsb1->configure(-browsecmd => [\&SelChangeListBox, $this, "X", $this->{ChooseXListBox}]); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "PlotOption", "Plot option:", $this->{ini}->pVariable('PlotOption', ''), "%s", $EntryWidth*5, "", 0, 0); #my $PlotOption = "with p ps 3 pt 4"; # line|dot|impulse|w p p[oint]s[ize] 3 p[oint]t[ype] 4 l[ine]w[idth] 3 $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "XMin", "XMin:", $this->{ini}->pVariable('XMin', ''), "%s", $EntryWidth, "", 0, 0); $this->MakeLabelEntry($Frame, "XMin", "XMax:", $this->{ini}->pVariable('XMax', ''), "%s", $EntryWidth, "", 0, 1); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "YMin", "YMin:", $this->{ini}->pVariable('YMin', ''), "%s", $EntryWidth, "", 0, 0); $this->MakeLabelEntry($Frame, "YMin", "YMax:", $this->{ini}->pVariable('YMax', ''), "%s", $EntryWidth, "", 0, 1); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->MakeLabelEntry($Frame, "ZMin", "ZMin:", $this->{ini}->pVariable('ZMin', ''), "%s", $EntryWidth, "", 0, 0); $this->MakeLabelEntry($Frame, "ZMin", "ZMax:", $this->{ini}->pVariable('ZMax', ''), "%s", $EntryWidth, "", 0, 1); $Frame = $this->MyFrame()->pack(-anchor => 'nw'); $Frame->MyCheckbutton( -text => 'Logplot X axis', -takefocus => 1, -variable => $this->{ini}->pVariable('LogplotX', 0), -onvalue => 1, -offvalue => 0, -indicatoron => 1, )->pack(-side => 'left', -fill => 'x'); $Frame->MyCheckbutton( -text => 'Logplot Y axis', -takefocus => 1, -variable => $this->{ini}->pVariable('LogplotY', 0), -onvalue => 1, -offvalue => 0, -indicatoron => 1, )->pack(-side => 'left', -fill => 'x'); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->{PlotButton} = $Frame->MyButton( -text => '&gnuplot', -takefocus => 1, -command => [\&ButtonPressed, $this, 'RButtonDown', 'ExecGNUPlot'], )->pack(-side => 'left'); $this->{PlotButton} = $Frame->MyButton( -text => '&Plot', -takefocus => 1, -command => [\&ButtonPressed, $this, 'RButtonDown', 'Plot'], )->pack(-side => 'left'); $Frame = $this->MyFrame()->pack(-anchor => 'nw', -fill => 'x'); $this->{TextBox} = $Frame->Scrolled( 'MyText', # -readonly => 1, -width => $TextBoxWidth, -height => $TextBoxHeight, -scrollbars => 'e', -takefocus => 1, )->pack(-side => 'top', -expand => 'yes', -fill => 'both'); # if($App->{FileContentFontName}) { # $this->{FileContentText}->configure( # -font => [ $App->{FileContentFontName}, # $App->{FileContentFontSize}, # $App->{FileContentFontStyle} ], # ); # } $this->UpdateParameters(); #ツールバーのOpenボタンをファイル読み込みにバインドする $this->{OpenButton}->configure( -command => [ \&ChooseFile, $this, $this->{SPEFileFileEntry} ], ) if($this->{OpenButton}); $this->InitWindowPosition($App->{geometory}); } sub CheckFileType { my ($path) = @_; my $ret = Rietan::CheckFileType($path); return $ret; } sub Read { my ($this, $filename) = @_; return Rietan::Read($this, $filename); } sub ButtonPressed { my ($this, $event, $type) = @_; my $App = $this->App(); if($type eq 'ExecGNUPlot') { &ExecGNUPlot($this); return; } elsif($type eq 'Plot') { &Plot($this); return; } elsif($type eq 'EditTemp') { my $Editor = $this->App()->{EditorPath}; my $File = $this->{ini}->{TempPath}; my $command = "$Editor $File"; system($command); return; } elsif($type eq 'EditOutput') { my $Editor = '';#$this->App()->{EditorPath}; my $File = $this->{ini}->{OutputPath}; my $command = "$Editor $File"; system($command); return; } } sub ExecGNUPlot { my ($this) = @_; my $App = $this->App(); #my $GNUPLOTPath = 'D:\Programs\gnu\gnuplot4.4\binary\pgnuplot.exe'; my $GNUPLOTPath = $App->{GNUPlotPath}; #'D:\Programs\gnu\gnuplot4.6\binary\pgnuplot.exe'; $this->{TextBox}->SetText("cmd: [$GNUPLOTPath]"); system($GNUPLOTPath); } sub Plot { my ($this) = @_; my $App = $this->App(); my $ini = $this->{ini}; my $GNUPLOTPath = $App->{GNUPlotPath}; my $TempPath = $ini->{TempPath}; my $Terminal = $this->{ChooseTerminalListBox}->GetText(); #$ini->{Terminal}; my $OutputPath = $ini->{OutputPath}; my $PlotFunction = $ini->{PlotCommand}; my $Function = $ini->{Function}; my $PlotOption = $ini->{PlotOption}; #"with p ps 3 pt 4"; # line|dot|impulse|w p p[oint]s[ize] 3 p[oint]t[ype] 4 l[ine]w[idth] 3 my $PlotStyle = 'w l'; if($ini->{PlotLine}) { if($ini->{PlotSymbol}) { $PlotStyle = 'w lp ps 2 pt ' . $this->{ChooseSymbolTypeBox}->GetCurSel()+1; } else { $PlotStyle = 'w l'; } } elsif($ini->{PlotSymbol}) { $PlotStyle = 'w p ps 2 pt ' . $this->{ChooseSymbolTypeBox}->GetCurSel(); } my $Title = $ini->{Title}; #'Test Graph'; my ($XMin, $XMax) = ($ini->{XMin}, $ini->{XMax}); my ($YMin, $YMax) = ($ini->{YMin}, $ini->{YMax}); my ($ZMin, $ZMax) = ($ini->{ZMin}, $ini->{ZMax}); my $XPlotType = ($ini->{LogplotX})? 'log' : 'linear'; my $YPlotType = ($ini->{LogplotY})? 'log' : 'linear'; if($Terminal ne 'wxt') { if($OutputPath =~ /\.pdf$/i) { $Terminal = 'pdf'; } elsif($OutputPath =~ /\.ps$/i) { $Terminal = 'postscript'; } elsif($OutputPath =~ /\.(jpeg|jpg)$/i) { $Terminal = 'jpeg'; } elsif($OutputPath =~ /\.(emf|wmf)$/i) { $Terminal = 'emf'; } elsif($OutputPath =~ /\.(gif|cgm|dxf|eepic|fig|hpgl|latex|bm|png|tgif|tpic)$/i) { $Terminal = $1; } } print "OutputPath: $OutputPath\n"; print "Terminal: $Terminal\n"; open(OUT, ">$TempPath"); print OUT "reset\n"; if($Terminal eq 'wxt') { print OUT "set terminal $Terminal\n"; } else { print OUT "set terminal $Terminal\n"; print OUT "set output \"$OutputPath\"\n"; } print OUT "set title '$Title'\n"; if($XPlotType eq 'log') { print OUT "set logscale x\n"; } if($YPlotType eq 'log') { print OUT "set logscale y\n"; } #print OUT "set xrange [$XMin:$XMax]\n"; #print OUT "set yrange [$YMin:$YMax]\n"; #print OUT "set zrange [$ZMin:$ZMax]\n"; print OUT "$PlotFunction [$XMin:$XMax] [$YMin:$YMax] [$ZMin:$ZMax] $Function $PlotStyle $PlotOption\n"; #print OUT "$PlotFunction $Function $PlotOption\n"; if($OutputPath eq '') { } else { print OUT "exit\n"; } close(OUT); #gnuplot -persist -e "set title 'Sine curve'; plot sin(x)" #ファイルのコマンドを実行する前に、ユーザ定義変数 a と s をセットする: #gnuplot -e "a=2; s='file.png'" input.gpl $TempPath =~ s/\//\\/g; my $cmd; if($Terminal eq 'wxt') { # $cmd = "$GNUPLOTPath \"$TempPath\""; $cmd = "$GNUPLOTPath -persist \"$TempPath\""; } else { $cmd = "$GNUPLOTPath \"$TempPath\""; } #print "cmd: [$cmd]\n"; my $content = JFile->new()->ReadFile($TempPath); $this->{TextBox}->SetText("cmd: [$cmd]\n\n$content"); system($cmd); } sub CreateGraphFrame { my ($this, $canvas) = @_; } sub AssignGraphData { my ($this) = @_; } sub AdjustViewRange { my ($this) = @_; } sub Draw { my ($this, $canvas) = @_; } sub ConfigureIniFileVariables { my ($this) = @_; my $App = $this->App(); $App->ConfigureIniFileVariables(); my $Style = $App->Args()->GetGetArg("style"); $Style = "General" unless($Style); $App->AddIniFileVariable("\\Preferences\\GNUPlotPath", 'GNUPlotPath', 'D:\Programs\gnu\gnuplot4.4\binary\pgnuplot.exe'); $App->AddIniFileVariable("\\$Style\\GraphFrame_Position", "GraphFramePosition", "r0.20,0.80,0.80,0.10"); $App->ReadSetting(); #print "GNUPlotPath: $App->{GNUPlotPath}\n"; } #============================================================ # bind応答関数 #============================================================ sub HoverItem { my ($canvas, $this, $event, $str, $x) = @_; } sub SelChangeListBox { my ($this, $type, $lb) = @_; #print "this:$this lb=$lb\n"; # my $s = $lb->GetText(); # if($s =~ /Solar/i) { # } # $this->CreateGraphFrame(); # $this->AssignGraphData(); # $this->Draw(); } sub ChooseFile { my ($this, $type, $widget, $Option) = @_; my $App = $this->App(); my $canvas = $this->Canvas(); my $this = $this->mw(); my $mode = 'open'; my $fmask = '*.csv'; if($Option eq 'ChooseParameterFile') { # $mode = 'save'; $fmask = '*.prm'; } my $dir = ''; unless($widget) { $dir = $this->App()->{WorkDir}; chdir($dir); } my $defstr = ''; my $message = 'Choose file'; my $filepath = Dialog::OpenFileDialog($this, $mode, $fmask, $defstr, $message, $dir); if($filepath) { my $DirPath = $this->SetWorkDir($filepath); if($widget) { $widget->SetText($filepath); } } else { return undef; } my $ret; if($Option eq 'ChooseParameterFile') { $this->{ini}->{DataFile} = ''; $this->{ini}->ReadAll($filepath); $this->{ini}->{ParameterFile} = $filepath; $this->{ChooseModelListBox}->SetText($this->{ini}->{Model}) if($this->{ChooseModelListBox} and $this->{ini}->{Model}); $this->{ChooseXListBox}->SetText($this->{ini}->{X}) if($this->{ChooseXListBox} and $this->{ini}->{X}); $this->{ChooseY1ListBox}->SetText($this->{ini}->{Y1}) if($this->{ChooseY1ListBox} and $this->{ini}->{Y1}); if($this->{ini}->{DataFile}) { $ret = $this->ReadFile($this->{ini}->{DataFile}, 1, 0); return undef unless($ret); $this->Draw(); } } elsif($Option eq 'ChooseCSVFile') { $ret = $this->ReadFile($filepath); return undef unless($ret); $this->Draw(); } return $ret; } sub ChooseSaveFile { my ($this, $path, $fmask, $widget) = @_; $path = 'save.csv' if(!defined $path); $fmask = '*.csv' if(!defined $fmask); $path = $widget->GetText() if($widget); $path = 'out.csv' if(!$path); my $App = $this->App(); my $this = $this->mw(); my $dir = $this->App()->{WorkDir}; chdir($dir) if($dir); my $defstr = $path; my $message = 'Choose file'; my $filepath = Dialog::OpenFileDialog($this, 'save', $fmask, $defstr, $message, $dir); if($filepath) { my $DirPath = $this->SetWorkDir($filepath); if($widget) { $widget->SetText($filepath); } } return $filepath; } sub InitializeParameters { my ($this, $ini, $dini) = @_; # $this->AddParameters($ini, $dini, "EF", 0.0, 0, 1.0, '', ''); } 1;