Skip to content

Commit

Permalink
CollapseWidget: restores widget sizes, add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ihumphrey committed Nov 4, 2019
1 parent 1bf5a42 commit d727f70
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions xicam/gui/widgets/collapsiblewidget.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import QLabel, QGridLayout, QSplitter, QToolBar, QVBoxLayout, QWidget

from xicam.core.msg import logError
from qtpy.QtWidgets import QGridLayout, QSplitter, QToolBar, QWidget


# TODO: this could be more generic, defining a collapsible interface/mixin type class
class CollapsibleWidget(QWidget):
"""
Creates a widget that can be collapsed when a button is clicked.
"""

toggled = Signal(bool)

def __init__(self, widget: QWidget, buttonText: str, parent=None):
"""
Constructs a widget that lets the passed ``widget`` keep an internal collapsed state that can be triggered when
a button is clicked.
Internally, when the button is clicked, a toggled signal is emitted, indicating what the collapse state has
been toggled to. Additionally, this signal is connected to the collapse() slot, which will collapse the passed
widget if another widget has been added via addWidget(). The widget added via addWidget() is not collapsible.
Parameters
----------
widget
The widget to make collapsible.
buttonText
The text of the button that will be used to collapse.
parent
The parent widget.
"""
super(CollapsibleWidget, self).__init__(parent)
self.widget = widget
self.buttonText = buttonText
self.collapsed = False

toolBar = QToolBar()
action = toolBar.addAction(self.name, self.toggle)
action = toolBar.addAction(self.buttonText, self.toggle)
action.setIconText("&" + action.text())
self.collapseButton = toolBar.widgetForAction(action)
self.collapseButton.setCheckable(True)
Expand All @@ -24,6 +43,8 @@ def __init__(self, widget: QWidget, buttonText: str, parent=None):
self.splitter = QSplitter(Qt.Horizontal)
self.splitter.addWidget(self.widget)
self.splitter.setCollapsible(0, self.collapsed)
# Keep track of the collapsed widget's size to restore properly when un-collapsed
self.widgetSize = self.splitter.sizes()[0]

layout = QGridLayout()
layout.addWidget(self.splitter, 0, 0)
Expand All @@ -34,6 +55,15 @@ def __init__(self, widget: QWidget, buttonText: str, parent=None):
self.toggled.connect(self.collapse)

def addWidget(self, widget):
"""
Adds a non-collapsible widget to the internal splitter.
Parameters
----------
widget
Non-collapsible widget to add.
"""
# TODO -- what happens when more than one widget is added?
self.splitter.addWidget(widget)
self.splitter.setCollapsible(1, False)

Expand All @@ -44,18 +74,12 @@ def toggle(self):
def collapse(self, collapsed):
self.collapseButton.setChecked(not collapsed)
self.splitter.setCollapsible(0, collapsed)
try:
# Only do something for now if there is more than one widget added.
if len(self.splitter.sizes()) > 1:
if collapsed:
sizes = []
for i in range(self.splitter.count()):
sizes.append(self.splitter.widget(i).minimumSizeHint().width())
sizes[0] = 0
self.widgetSize = self.splitter.sizes()[0]
sizes = [0, self.splitter.sizes()[1]]
self.splitter.setSizes(sizes)
else:
sizes = []
for i in range(self.splitter.count()):
sizes.append(self.splitter.sizes()[i])
sizes[0] = self.splitter.widget(i).minimumSizeHint().width()
sizes = [self.widgetSize, self.splitter.sizes()[1] - self.widgetSize]
self.splitter.setSizes(sizes)
except Exception as e:
logError(e)

0 comments on commit d727f70

Please sign in to comment.