OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
traj.py
Go to the documentation of this file.
1 #-------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #-------------------------------------------------------------------------------
19 from PyQt4.QtCore import *
20 from PyQt4.QtGui import *
21 from ost import *
22 
23 class TrajWidget(QWidget):
24  def __init__(self, traj=None, render_mode=gfx.SIMPLE, sel='', parent=None):
25  QWidget.__init__(self, parent, Qt.Tool)
26  self.render_mode=render_mode
27  vb=QVBoxLayout()
28  hb=QHBoxLayout()
29  hb2=QHBoxLayout()
30  self.selection=sel
31  self.callback=None
32  self._slider=QSlider(self)
33  self._slider.setOrientation(Qt.Horizontal)
34  self._speed_slider=QSlider(self)
35  self._speed_slider.setOrientation(Qt.Horizontal)
36  self._play=QToolButton(self)
37  self._repeat=QCheckBox(self)
38  self._frame=QLabel(self)
39  self._frameNo=QLabel(self)
40  self._frameEnd=QLabel(self)
41  self._repeat.setText('Repeat')
42  self._slider.setTracking(True)
43  self._play.setText('Play')
44  self._play.setCheckable(True)
45  self._frame.setText('Frame: ')
46  self._frameNo.setNum(0)
47  self._frameNo.setAlignment(Qt.AlignRight)
48  self._frameEnd.setText('/ '+ str(traj.GetFrameCount()-1))
49  self._frameEnd.setAlignment(Qt.AlignLeft)
50  self._speed_slider.setTracking(True)
51  self._speed_slider.setRange(-1000,-1)
52  self._speed_slider.value=-100
53  hb.addWidget(self._play)
54  hb.addWidget(self._repeat)
55  hb2.addWidget(self._frame)
56  hb2.addWidget(self._frameNo)
57  hb2.addWidget(self._frameEnd)
58  hb2.addWidget(self._speed_slider)
59  self.setLayout(vb)
60  vb.addLayout(hb)
61  vb.addWidget(self._slider)
62  vb.addLayout(hb2)
63  self.traj=traj
64  self.time=1
65  QObject.connect(self._play, SIGNAL('toggled(bool)'),
66  self._TogglePlay)
67  QObject.connect(self._slider, SIGNAL('valueChanged(int)'),
69  QObject.connect(self._speed_slider,SIGNAL('valueChanged(int)'),self._SpeedSliderValChanged)
70 
71  def _SpeedSliderValChanged(self,speed_pos):
72  self.time=-speed_pos
73  if self._play.isChecked():
74  self._TogglePlay(False)
75  self._TogglePlay(True)
76 
77  def _SetTime(self,t):
78  self.time=t
79  self._speed_slider.setSliderPosition(-t)
80  if self._play.isChecked():
81  self._TogglePlay(False)
82  self._TogglePlay(True)
83 
84  def _SliderValueChanged(self, pos):
85  self.current_frame=pos
86  self._traj.CopyFrame(self.current_frame)
87  self.gfx_entity.UpdatePositions()
88 
89  def _GetCurrentFrame(self):
90  return self._slider.sliderPosition()
91 
92  def _SetCurrentFrame(self, pos):
93  if self._slider.maximum()<pos:
94  if self._repeat.isChecked():
95  pos=0
96  else:
97  pos=self._slider.maximum()
98  self._slider.setSliderPosition(pos)
99  self._frameNo.setNum(pos)
100 
101  current_frame=property(_GetCurrentFrame, _SetCurrentFrame)
102 
103  def timerEvent(self, event):
104  self.gfx_entity.BlurSnapshot()
105  self.current_frame+=1
106  if self.callback:
107  self.callback(self.gfx_entity)
108  self.gfx_entity.UpdatePositions()
109 
110  def _TogglePlay(self, playing):
111  if playing:
112  self.timer_id_=self.startTimer(self.time)
113  else:
114  self.killTimer(self.timer_id_)
115 
116  def _SetTraj(self, traj):
117  self._traj=traj
118  if self._traj:
119  ev=traj.GetEntity()
121  ev.Select(self.selection))
122  gfx.Scene().Add(self.gfx_entity)
123  gfx.Scene().CenterOn(self.gfx_entity)
124  # enable the blur effect
125  #self.gfx_entity.SetBlur(True)
126  self._slider.setMinimum(0)
127  self._slider.setMaximum(self._traj.GetFrameCount()-1)
128  self._slider.setSliderPosition(0)
129  else:
130  if self._traj:
131  del self._anim
132  self._traj=None
133  def _SetBlur(self, blur):
134  self.gfx_entity.SetBlur(blur)
135 
136  def _GetBlur(self):
137  return self.gfx_entity.GetBlur()
138 
139  def _GetTraj(self):
140  return self._traj
141 
142  blur=property(_GetBlur, _SetBlur)
143  traj=property(_GetTraj, _SetTraj)