OSDN Git Service

Ruby-2.0.0 is now included in the repository.
[molby/Molby.git] / Scripts / view.rb
1 #
2 #  view.rb
3 #
4 #  Created by Toshi Nagata.
5 #  Copyright 2014 Toshi Nagata. All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation version 2 of the License.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 class Molecule
17
18 def cmd_show_unit_cell
19   self.show_unitcell = !self.show_unitcell
20 end
21
22 def cmd_show_hydrogens
23   self.show_hydrogens = !self.show_hydrogens
24 end
25
26 def cmd_show_dummy_atoms
27   self.show_dummy_atoms = !self.show_dummy_atoms
28 end
29
30 def cmd_show_expanded
31   self.show_expanded = !self.show_expanded
32 end
33
34 def cmd_show_ellipsoids
35   self.show_ellipsoids = !self.show_ellipsoids
36 end
37
38 def cmd_show_rotation_center
39   self.show_rotation_center = !self.show_rotation_center
40 end
41
42 def cmd_show_graphite
43   n = self.show_graphite
44   flag = self.show_graphite?
45   hash = Dialog.run("Show Graphite") {
46         layout(1,
47           item(:checkbox, :title=>"Show graphite", :tag=>"show_graphite", :value=>(flag ? 1 : 0),
48                 :action=>lambda { |it| set_attr("graphite", :enabled=>(it[:value] == 1)) } ),
49           item(:text, :title=>"Number of graphite rings for each direction:"),
50           item(:textfield, :width=>120, :tag=>"graphite", :value=>n.to_s, :enabled=>flag))
51   }
52   if hash[:status] == 0
53         self.show_graphite(hash["graphite"])
54         self.show_graphite(hash["show_graphite"] == 1 ? true : false)
55   end
56 end
57
58 def cmd_line_mode
59   self.line_mode = !self.line_mode
60 end
61
62 def cmd_ball_and_stick_mode
63   mol = self
64   rad = mol.atom_radius
65   res = mol.atom_resolution
66   brad = mol.bond_radius
67   bres = mol.bond_resolution
68   if mol.line_mode == true || mol.atom_radius >= 0.5
69     rad = 0.2
70         res = 12
71         brad = 0.1
72         bres = 8
73   end
74   hash = Dialog.run("Ball and Stick") {
75     layout(2,
76           item(:text, :title=>"Atom Radius (0..0.5, default 0.2)"),
77           item(:textfield, :width=>100, :tag=>"atom_radius", :value=>rad.to_s),
78           item(:text, :title=>"Atom Resolution (default 12)"),
79           item(:textfield, :width=>100, :tag=>"atom_resolution", :value=>res.to_s),
80           item(:text, :title=>"Bond Radius (default 0.1)"),
81           item(:textfield, :width=>100, :tag=>"bond_radius", :value=>brad.to_s),
82           item(:text, :title=>"Atom Resolution (default 8)"),
83           item(:textfield, :width=>100, :tag=>"bond_resolution", :value=>bres.to_s))
84   }
85   if hash[:status] == 0
86     f = hash["atom_radius"].to_f
87         self.atom_radius = f if f > 0.0 && f < 0.5
88     n = hash["atom_resolution"].to_i
89         self.atom_resolution = n if n >= 6 && n < 100
90     f = hash["bond_radius"].to_f
91         self.bond_radius = f if f > 0.0
92     n = hash["bond_resolution"].to_i
93         self.bond_resolution = n if n >= 4 && n < 100
94         self.line_mode = false
95   end
96 end
97
98 def cmd_space_filling_mode
99   mol = self
100   rad = mol.atom_radius
101   res = mol.atom_resolution
102   if mol.line_mode == true || mol.atom_radius < 0.5
103     rad = 1.0
104         res = 18
105   end
106   hash = Dialog.run("Space Filling") {
107     layout(2,
108           item(:text, :title=>"Atom Radius (0.5..2.0, default 1.0)"),
109           item(:textfield, :width=>100, :tag=>"atom_radius", :value=>rad.to_s),
110           item(:text, :title=>"Atom Resolution (default 18)"),
111           item(:textfield, :width=>100, :tag=>"atom_resolution", :value=>res.to_s))
112   }
113   if hash[:status] == 0
114     f = hash["atom_radius"].to_f
115         self.atom_radius = f if f >= 0.5 && f <= 2.0
116     n = hash["atom_resolution"].to_i
117         self.atom_resolution = n if n >= 6 && n < 100
118         self.line_mode = false
119   end
120 end
121
122 end
123
124 register_menu("View\t-", nil)
125 register_menu("View\t^Show Unit Cell", :cmd_show_unit_cell,
126   lambda { |m| [m != nil, m.show_unitcell] } )
127 register_menu("View\t^Show Hydrogen Atoms", :cmd_show_hydrogens,
128   lambda { |m| [m != nil, m.show_hydrogens] } )
129 register_menu("View\t^Show Dummy Atoms", :cmd_show_dummy_atoms,
130   lambda { |m| [m != nil, m.show_dummy_atoms] } )
131 register_menu("View\t^Show Expanded Atoms", :cmd_show_expanded,
132   lambda { |m| [m != nil, m.show_expanded] } )
133 register_menu("View\t^Show Ellipsoids", :cmd_show_ellipsoids,
134   lambda { |m| [m != nil, m.show_ellipsoids] } )
135 register_menu("View\t^Show Rotation Center", :cmd_show_rotation_center,
136   lambda { |m| [m != nil, m.show_rotation_center] } )
137 register_menu("View\t-", nil)
138 register_menu("View\t^Show Graphite...", :cmd_show_graphite,
139   lambda { |m| [m != nil, m.show_graphite?] } )
140 register_menu("View\t-", nil)
141 register_menu("View\tAppearance\t^Line", :cmd_line_mode,
142   lambda { |m| [m != nil, m.line_mode] } )
143 register_menu("View\tAppearance\t^Ball and Stick", :cmd_ball_and_stick_mode,
144   lambda { |m| [m != nil, !m.line_mode && m.atom_radius < 0.5] } )
145 register_menu("View\tAppearance\t^Space Filling", :cmd_space_filling_mode,
146   lambda { |m| [m != nil, !m.line_mode && m.atom_radius >= 0.5] } )