Skip to content

Commit

Permalink
Add NoBorderScrollArea and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanicaSTFC committed Aug 6, 2024
1 parent 1a182f7 commit 4e5b0fe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
33 changes: 33 additions & 0 deletions eqt/ui/NoBorderScrollArea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from PySide2.QtWidgets import QPushButton, QScrollArea, QWidget
import qdarkstyle, re

class NoBorderScrollArea(QScrollArea):
"""Note: move this class to eqt."""

def __init__(self, widget, parent=None):
"""Creates an instance of a QScrollArea and sets its border to none.
Sets its widget to resizable."""
super().__init__(parent)
self.setStyleSheet("QScrollArea { border: none; }")
self.setWidgetResizable(True)
self.setWidget(widget)
self.apply_qdarkstyle_to_buttons(widget)

def apply_qdarkstyle_to_buttons(self, widget):
"""Applies the qdarkstyle to all the buttons in the widget explicitly.
This ensures that the style is consistent with the rest of the app."""
if isinstance(widget, QPushButton):

button_style = self._extract_qdarkstyle_button_style()
widget.setStyleSheet(button_style)
for child in widget.findChildren(QWidget):
self.apply_qdarkstyle_to_buttons(child)

def _extract_qdarkstyle_button_style(self):
"""Returns the QPushButton styles from qdarkstyle, including the different button styles."""
style = qdarkstyle.load_stylesheet(qt_api='pyside2')
pattern = re.compile(r"(QPushButton\s*{[^}]*}|QPushButton\s*:[^}]*{[^}]*})", re.DOTALL)
matches = pattern.findall(style)
if matches:
return ''.join(matches)
return ""
26 changes: 26 additions & 0 deletions test/test_NoBorderScrollArea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from eqt.ui.NoBorderScrollArea import NoBorderScrollArea
from PySide2.QtWidgets import QApplication, QHBoxLayout, QPushButton, QWidget

class TestNoBorderScrollArea(unittest.TestCase):

@classmethod
def setUpClass(cls):
'''If not present already, creates a QApplication.'''
if not QApplication.instance():
app = QApplication([])
else:
app = QApplication.instance()

def setUp(self):
'''Initialises a NoBorderScrollArea widget and adds it to a layout.'''
self.main_widget = QWidget()
self.layout = QHBoxLayout(self.main_widget)
self.scroll_area_widget = NoBorderScrollArea(QPushButton())
self.layout.addWidget(self.scroll_area_widget)

def test_scroll_area_creation(self):
'''
Tests the init method of the NoBorderScrollArea class.
'''
self.assertIsNotNone(self.scroll_area_widget, "NoBorderScrollArea widget should be created")

0 comments on commit 4e5b0fe

Please sign in to comment.