CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ZoomableScrollArea.py
Go to the documentation of this file.
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 
4 from Vispa.Gui.Zoomable import Zoomable
5 from Vispa.Gui.ZoomableWidget import ZoomableWidget
6 
7 class ZoomableScrollArea(Zoomable, QScrollArea):
8  """ Standard QScrollArea extended by zooming capabilties.
9  """
10 
11  def __init__(self, parent=None):
12  """ Constructor.
13  """
14  QScrollArea.__init__(self, parent)
15  Zoomable.__init__(self) # Call after QScrollArea constructor, required by setZoom()
16 
17  self.connect(self.verticalScrollBar(), SIGNAL("valueChanged(int)"), self.scrollBarValueChanged)
18  self.connect(self.horizontalScrollBar(), SIGNAL("valueChanged(int)"), self.scrollBarValueChanged)
19 
20  def wheelEvent(self, event):
21  """ If wheelEvent occurs either zoom window (ctrl-key pressed) or scroll the area.
22  """
23  if event.modifiers() == Qt.ControlModifier:
24  oldZoom = self.zoom()
25  oldLeft = self.widget().mapFrom(self,QPoint(event.x(),0)).x()
26  oldTop = self.widget().mapFrom(self,QPoint(0,event.y())).y()
27 
28  if event.delta() > 0:
29  self.incrementZoom()
30  else:
31  self.decrementZoom()
32 
33  newZoom = self.zoom()
34  zoomFactor = newZoom / oldZoom
35  newLeft = oldLeft * zoomFactor
36  newTop = oldTop * zoomFactor
37 
39  self.ensureVisible(newLeft-event.x()+self.viewport().width()/2.0,newTop-event.y()+self.viewport().height()/2.0,self.viewport().width()/2.0,self.viewport().height()/2.0)
40  self.emit(SIGNAL('wheelZoom()'))
41  else:
42  QScrollArea.wheelEvent(self, event)
43 
44  def setZoom(self, zoom):
45  """ Sets its own zoom factor and passes it to it's child widget if child is Zoomable.
46  """
47  Zoomable.setZoom(self, zoom)
48  if isinstance(self.widget(), ZoomableWidget):
49  self.widget().setZoom(zoom)
51  self.emit(SIGNAL("zoomChanged(float)"), zoom)
52 
53  def resizeEvent(self, event):
54  """Calls autosizeScrollWidget().
55  """
57  QScrollArea.resizeEvent(self, event)
58 
60  """Sets size of child widget to the size needed to fit whole content.
61  """
62  if not self.widget():
63  return
64  childrenRect = self.widget().childrenRect()
65  width = max(self.viewport().width(), childrenRect.bottomRight().x()) - min(0, childrenRect.topLeft().x())
66  height = max(self.viewport().height(), childrenRect.bottomRight().y()) - min(0, childrenRect.topLeft().y())
67  self.widget().resize(width, height)
68 
69  def mousePressEvent(self, event):
70  """ Forward mousePressEvent.
71  """
72  self.widget().mousePressEvent(event)
73 
74  def scrollBarValueChanged(self, value):
75  """ Forward valueChanged(int) signal from scroll bars to viewport widget.
76 
77  If the widget (see QScrollArea.widget()) has a function called "scrollBarValueChanged", it will be called.
78  """
79  if hasattr(self.widget(), "scrollBarValueChanged"):
80  self.widget().scrollBarValueChanged(value)
const T & max(const T &a, const T &b)
Definition: DDAxes.h:10