def ui(self): super(AttributeEditor, self).ui() self._main_group = QGroupBox() self._main_group.setProperty('class', 'attr_main_group') self._main_group.setFlat(False) self._main_group.setTitle('') self._main_group_layout = self.get_attributes_layout() self._main_group.setLayout(self._main_group_layout) scroll_area = QScrollArea() scroll_area.setWidgetResizable(True) scroll_area.setStyleSheet( 'QScrollArea { background-color: rgb(57,57,57);}') scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) scroll_area.setWidget(self._main_group) self.main_layout.addWidget(scroll_area) self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self._on_create_context_menu)
class ExpandableLine(QWidget, object): def __init__(self, title='', animation_duration=300, parent=None): super(ExpandableLine, self).__init__(parent=parent) self._animation_duration = animation_duration base_layout = layouts.GridLayout(margins=(0, 0, 0, 0)) base_layout.setVerticalSpacing(0) self.setLayout(base_layout) self.expand_btn = QToolButton() self.expand_btn.setText(str(title)) self.expand_btn.setStyleSheet('QToolButton { border : none; }') self.expand_btn.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) self.expand_btn.setArrowType(Qt.ArrowType.RightArrow) self.expand_btn.setCheckable(True) self.expand_btn.setChecked(True) header_line = QFrame() header_line.setFrameShape(QFrame.HLine) header_line.setFrameShadow(QFrame.Sunken) header_line.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) self.content_area = QScrollArea() self.content_area.setStyleSheet('QScrollArea { border: none;}') self.content_area.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.content_area.setMaximumHeight(0) self.content_area.setMinimumHeight(0) self.toggle_anim = QParallelAnimationGroup() self.toggle_anim.addAnimation(QPropertyAnimation(self, 'minimumHeight')) self.toggle_anim.addAnimation(QPropertyAnimation(self, 'maximumHeight')) self.toggle_anim.addAnimation(QPropertyAnimation(self.content_area, 'maximumHeight')) row = 0 base_layout.addWidget(self.expand_btn, row, 0, 1, 1, Qt.AlignLeft) base_layout.addWidget(header_line, row, 2, 1, 1) row += 1 base_layout.addWidget(self.content_area, row, 0, 1, 3) def expand_view(checked): arrow_type = Qt.DownArrow if checked else Qt.RightArrow direction = QAbstractAnimation.Forward if checked else QAbstractAnimation.Backward self.expand_btn.setArrowType(arrow_type) self.toggle_anim.setDirection(direction) self.toggle_anim.start() # === SIGNALS === # self.expand_btn.toggled.connect(expand_view) expand_view(True) def set_content_layout(self, content_layout): self.content_area.destroy() self.content_area.setLayout(content_layout) collapsed_height = self.sizeHint().height() - self.content_area.maximumHeight() content_height = content_layout.sizeHint().height() + 300 for i in range(self.toggle_anim.animationCount() - 1): expand_anim = self.toggle_anim.animationAt(i) expand_anim.setDuration(self._animation_duration) expand_anim.setStartValue(collapsed_height) expand_anim.setEndValue(collapsed_height + content_height) content_anim = self.toggle_anim.animationAt(self.toggle_anim.animationCount() - 1) content_anim.setDuration(self._animation_duration) content_anim.setStartValue(0) content_anim.setEndValue(content_height)