Coverage for ase/gui/movie.py: 79.87%

149 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import tkinter as tk 

2import tkinter.ttk as ttk 

3from functools import partial 

4 

5import numpy as np 

6 

7import ase.gui.ui as ui 

8from ase.gui.i18n import _ 

9 

10 

11class Movie: 

12 def __init__(self, gui): 

13 self.win = win = ui.Window(_('Movie'), close=self.close) 

14 win.add(_('Image number:')) 

15 self.frame_number = ui.Scale( 

16 gui.frame, 0, len(gui.images) - 1, callback=self.new_frame 

17 ) 

18 win.add(self.frame_number) 

19 

20 win.add( 

21 [ 

22 ui.Button(_('First'), self.click, -1, True), 

23 ui.Button(_('Back'), self.click, -1), 

24 ui.Button(_('Forward'), self.click, 1), 

25 ui.Button(_('Last'), self.click, 1, True), 

26 ] 

27 ) 

28 

29 play = ui.Button(_('Play'), self.play) 

30 stop = ui.Button(_('Stop'), self.stop) 

31 

32 # TRANSLATORS: This function plays an animation forwards and backwards 

33 # alternatingly, e.g. for displaying vibrational movement 

34 self.rock = ui.CheckButton( 

35 _('Rock'), value=gui.config.getboolean('movie_rock') 

36 ) 

37 

38 win.add([play, stop, self.rock]) 

39 

40 if len(gui.images) > 150: 

41 skipdefault = len(gui.images) // 150 

42 tdefault = min(max(len(gui.images) / (skipdefault * 5.0), 1.0), 30) 

43 else: 

44 skipdefault = 0 

45 tdefault = min(max(len(gui.images) / 5.0, 1.0), 30) 

46 if gui.config.getliteral('movie_rate') != 'auto': 

47 tdefault = gui.config.getliteral('movie_rate') 

48 if gui.config.getliteral('movie_skip') != 'auto': 

49 skipdefault = gui.config.getliteral('movie_skip') 

50 self.time = ui.SpinBox(tdefault, 1.0, 99, 0.1) 

51 self.skip = ui.SpinBox(skipdefault, 0, 99, 1) 

52 win.add([_(' Frame rate: '), self.time, _(' Skip frames: '), self.skip]) 

53 

54 self.gui = gui 

55 self.direction = 1 

56 gui.obs.new_atoms.register(self.close) 

57 

58 def close(self): 

59 self.stop() 

60 self.win.close() 

61 

62 def click(self, step, firstlast=False): 

63 if firstlast and step < 0: 

64 i = 0 

65 elif firstlast: 

66 i = len(self.gui.images) 

67 else: 

68 i = max(0, min(len(self.gui.images) - 1, self.gui.frame + step)) 

69 

70 self.frame_number.value = i 

71 if firstlast: 

72 self.direction = np.sign(-step) 

73 else: 

74 self.direction = np.sign(step) 

75 

76 def new_frame(self, value): 

77 self.gui.set_frame(value) 

78 

79 def play(self): 

80 self.stop() 

81 t = 1 / self.time.value 

82 self.gui.movie_timer = self.gui.window.after(t, self.step) 

83 

84 def stop(self): 

85 if self.gui.movie_timer is not None: 

86 self.gui.movie_timer.cancel() 

87 

88 def step(self): 

89 i = self.gui.frame 

90 nimages = len(self.gui.images) 

91 delta = int(self.skip.value) + 1 

92 

93 if self.rock.value: 

94 if i <= self.skip.value: 

95 self.direction = 1 

96 elif i >= nimages - delta: 

97 self.direction = -1 

98 i += self.direction * delta 

99 else: 

100 i = (i + self.direction * delta + nimages) % nimages 

101 

102 self.frame_number.value = i 

103 self.play() 

104 

105 

106class MovieToolbar: 

107 def __init__(self, parent, gui): 

108 from itertools import count 

109 

110 self.gui = gui 

111 self.direction = 1 

112 

113 tkframe = ttk.Frame(parent, relief='raised', borderwidth=1) 

114 self.tkframe = tkframe 

115 self.controls_with_state = [] 

116 

117 columncounter = count() 

118 

119 def add(widget, **kwargs): 

120 widget.grid(row=0, column=next(columncounter), **kwargs) 

121 return widget 

122 

123 self.graphbutton_icon = gui.icons['graph'] 

124 self.graphbutton = add( 

125 tk.Button( 

126 tkframe, 

127 image=self.graphbutton_icon, 

128 bd=0, 

129 command=self.gui.plot_graph_standard, 

130 ) 

131 ) 

132 self.controls_with_state.append(self.graphbutton) 

133 

134 add(ttk.Separator(tkframe, orient='vertical'), sticky='ns') 

135 

136 self.moviebutton_icon = gui.icons['movie'] 

137 self.moviebutton = add( 

138 tk.Button( 

139 tkframe, 

140 image=self.moviebutton_icon, 

141 bd=0, 

142 command=self.gui.movie, 

143 ) 

144 ) 

145 

146 self.slider = add( 

147 tk.Scale( 

148 tkframe, 

149 from_=0, 

150 orient='horizontal', 

151 command=self.slidercommand, 

152 showvalue=False, 

153 ), 

154 padx=(4, 2), 

155 ) 

156 self.controls_with_state.append(self.slider) 

157 add(tk.Label(tkframe, text=_('Image:'))) 

158 nlwidth = len(str(len(self.gui.images) - 1)) 

159 self.numlabel = add( 

160 tk.Label(tkframe, text='0', width=nlwidth, anchor='w'), padx=(0, 4) 

161 ) 

162 self._update_number_of_images() 

163 

164 self.firstbutton_icon = gui.icons['first'] 

165 self.firstbutton = add( 

166 tk.Button( 

167 tkframe, 

168 image=self.firstbutton_icon, 

169 bd=0, 

170 command=partial(self.click, -1, True), 

171 ) 

172 ) 

173 self.controls_with_state.append(self.firstbutton) 

174 self.backbutton_icon = gui.icons['back'] 

175 self.backbutton = add( 

176 tk.Button( 

177 tkframe, 

178 image=self.backbutton_icon, 

179 bd=0, 

180 command=partial(self.click, -1), 

181 ) 

182 ) 

183 self.controls_with_state.append(self.backbutton) 

184 self.playbutton_icon = gui.icons['play'] 

185 self.playbutton = add( 

186 tk.Button( 

187 tkframe, image=self.playbutton_icon, bd=0, command=self.play 

188 ) 

189 ) 

190 self.controls_with_state.append(self.playbutton) 

191 self.pausebutton_icon = gui.icons['pause'] 

192 self.pausebutton = add( 

193 tk.Button( 

194 tkframe, image=self.pausebutton_icon, bd=0, command=self.stop 

195 ) 

196 ) 

197 self.controls_with_state.append(self.pausebutton) 

198 self.forwardbutton_icon = gui.icons['forward'] 

199 self.forwardbutton = add( 

200 tk.Button( 

201 tkframe, 

202 image=self.forwardbutton_icon, 

203 bd=0, 

204 command=partial(self.click, 1), 

205 ) 

206 ) 

207 self.controls_with_state.append(self.forwardbutton) 

208 self.lastbutton_icon = gui.icons['last'] 

209 self.lastbutton = add( 

210 tk.Button( 

211 tkframe, 

212 image=self.lastbutton_icon, 

213 bd=0, 

214 command=partial(self.click, 1, True), 

215 ) 

216 ) 

217 self.controls_with_state.append(self.lastbutton) 

218 

219 # "Set" atoms means something (anything) changed including 

220 # which frame number we are displaying: 

221 gui.obs.set_atoms.register(self._update_atoms) 

222 gui.obs.set_atoms.register(self._update_button_states) 

223 

224 # "New" atoms may change the number of images altogether: 

225 gui.obs.new_images.register(self._update_number_of_images) 

226 gui.obs.new_images.register(self._update_button_states) 

227 

228 def click(self, step, firstlast=False): 

229 if firstlast and step < 0: 

230 framenum = 0 

231 elif firstlast: 

232 framenum = len(self.gui.images) 

233 else: 

234 framenum = max( 

235 0, min(len(self.gui.images) - 1, self.gui.frame + step) 

236 ) 

237 

238 self.slider.set(framenum) 

239 

240 def play(self): 

241 self.stop() 

242 t = 1 / 24 

243 self.gui.movie_timer = self.gui.window.after(t, self.step) 

244 

245 def stop(self): 

246 if self.gui.movie_timer is not None: 

247 self.gui.movie_timer.cancel() 

248 

249 def step(self): 

250 framenum = self.gui.frame 

251 nimages = len(self.gui.images) 

252 

253 framenum = (framenum + self.direction + nimages) % nimages 

254 

255 self.slider.set(framenum) 

256 self.play() 

257 

258 def _update_number_of_images(self): 

259 self.slider['to'] = len(self.gui.images) - 1 

260 self.numlabel['width'] = len(str(len(self.gui.images) - 1)) 

261 self._update_atoms() 

262 

263 def _update_atoms(self): 

264 if self.gui.frame is not None: 

265 self.slider.set(self.gui.frame) 

266 

267 def slidercommand(self, sliderval: str): 

268 framenum = int(sliderval) 

269 self.numlabel['text'] = sliderval 

270 if framenum != self.gui.frame: 

271 self.gui.set_frame(framenum) 

272 

273 def _update_button_states(self): 

274 movie_state = tk.NORMAL if len(self.gui.images) > 1 else tk.DISABLED 

275 for ctrl in self.controls_with_state: 

276 ctrl['state'] = movie_state