CMS 3D CMS Logo

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