CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ToolBoxContainer.py
Go to the documentation of this file.
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 
4 import logging
5 
6 class ToolBoxContainer(QWidget):
7  """ Container for widgets which can be shown or hidden by a row of toggle buttons at the bottom of the container
8 
9  The container uses a vertical QSplitter object to show added widgets.
10  """
11 
12  HIDE_LAST_TOGGLE_BUTTON = True # see setHideLastToggleButton()
13 
14  TYPE_SPLITTER = 0
15  TYPE_WIDGETS = 2
16 
17  def __init__(self, parent=None, containerType=None):
18  """ Constructor.
19  """
20  logging.debug(__name__ +": __init__()")
21  QWidget.__init__(self, parent)
22  self._hideLastToggleButton = False
25  self._toolboxList = []
26  self._toggleButtons = []
27  self._splitter = None
28  if containerType:
29  self.setType(containerType)
30 
31  # initialize layout
32  self.setLayout(QVBoxLayout())
33  self.layout().setSpacing(0)
34  self.layout().setContentsMargins(0, 0, 0, 0)
35  if self._containerType == self.TYPE_SPLITTER:
36  self._splitter = QSplitter(Qt.Vertical)
37  self.layout().addWidget(self._splitter, 3)
38 
39  self._buttonLayout = QHBoxLayout()
40  self._buttonLayout.addStretch(2) # push buttons completely to the right
41  self._buttonLayout.setSpacing(0)
42 
43  self.layout().addStretch(0.5) # keep buttons at the bottom even if all tool boxes are invisible
44  self.layout().addLayout(self._buttonLayout)
45 
46  def setType(self, containerType):
47  self._containerType = containerType
48 
49  def setHideLastToggleButton(self, hide):
50  """ Influences visibility of last visible toggle button.
51 
52  If hide is True toggle buttons are only shown if there is more than one widget selectable.
53  In this case the ToolBoxContainer behaves like a normal widget.
54  If hide if False the toggle button is also show if there is only one widget selectable.
55  """
56  self._hideLastToggleButton = hide
57 
58  def splitter(self):
59  """ Returns splitter containing widgets.
60  """
61  return self._splitter
62 
63  def toggleButtons(self):
64  return self._toggleButtons
65 
66  def addWidget(self, widget, stretch=0):
67  """ Adds widget to tool box.
68  """
69  if self._containerType == self.TYPE_SPLITTER:
70  self._splitter.addWidget(widget)
71  else:
72  self.layout().insertWidget(len(self._toolboxList), widget, stretch)
73  self._toolboxList.append(widget)
74  toggleButton = QToolButton()
75  toggleButton.setCheckable(True)
76  toggleButton.setChecked(True)
77  toggleButton.setText("v")
78  toggleButton.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
79  self.connect(toggleButton, SIGNAL('clicked(bool)'), self.toggleButtonPressed)
80  self._buttonLayout.addWidget(toggleButton, 0, Qt.AlignRight)
81  self._toggleButtons.append(toggleButton)
82 
84 
85  def showWidget(self, widget, show=True):
86  if widget in self._toolboxList:
87  widget.setVisible(show)
88  self._toggleButtons[self._toolboxList.index(widget)].setVisible(show)
90 
92  if self._hideLastToggleButton:
93  indices = self.visibleToggleButtonsIdices()
94  if len(indices) == 1:
95  self._toggleButtons[indices[0]].hide()
96 
97  # make sure toggle buttons are shown if there are more than one widget selectable
98  # or if hideLastToggleButton was changed in the meantime
99  indices = self.visibleToolBoxIdices()
100  if len(indices) > 1 or not self._hideLastToggleButton:
101  for i in indices:
102  if not self._toggleButtons[i].isVisible():
103  self._toggleButtons[i].show()
104 
105  def hideWidget(self, widget):
106  self.showWidget(widget, False)
107 
109  """ Returns list of indices of toggle buttons which are visible.
110  """
111  return self._visibleIndices(self._toggleButtons)
112 
114  """ Returns list of indices of tool box widgets which are visible.
115  """
116  return self._visibleIndices(self._toolboxList)
117 
118  def _visibleIndices(self, list):
119  """ Returns list of indices of entries in given list which are visible.
120 
121  It is assumed list entries have boolean function isVisible() (e.g. QWidget).
122  """
123  indices = []
124  for i in range(len(list)):
125  if list[i].isVisible():
126  indices.append(i)
127  return indices
128 
130  """ Slot for showing and hinding widgets when toggle buttons are pressed.
131  """
132  if self.sender() in self._toggleButtons:
133  index = self._toggleButtons.index(self.sender())
134 
135  if self._toolboxList[index].isVisible():
136  self._toolboxList[index].hide()
137  self.sender().setText("^")
138  else:
139  self._toolboxList[index].show()
140  self.sender().setText("v")
141