Skip to content

Commit

Permalink
Fix ghost of 'Recent Files' showing up on Welcome Screen when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
rbreu committed Dec 8, 2023
1 parent 4ae153b commit b176e79
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
25 changes: 15 additions & 10 deletions beeref/widgets/welcome_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ def data(self, index, role):

class RecentFilesView(QtWidgets.QListView):

def __init__(self, parent, files=None):
def __init__(self, parent, view, files=None):
super().__init__(parent)
self.view = view
self.files = files or []
self.clicked.connect(self.on_clicked)
self.setModel(RecentFilesModel(self.files))
self.setMouseTracking(True)

def on_clicked(self, index):
self.parent().parent().open_from_file(self.files[index.row()])
self.view.open_from_file(self.files[index.row()])

def update_files(self, files):
self.files = files
Expand Down Expand Up @@ -96,13 +97,16 @@ def __init__(self, parent):
self.init_main_controls(main_window=parent.parent)

# Recent files
self.files_layout = QtWidgets.QVBoxLayout()
self.files_layout.addStretch(50)
self.files_layout.addWidget(
self.files_widget = QtWidgets.QWidget(self)
files_layout = QtWidgets.QVBoxLayout()
files_layout.addStretch(50)
files_layout.addWidget(
QtWidgets.QLabel('<h3>Recent Files</h3>', self))
self.files_view = RecentFilesView(self)
self.files_layout.addWidget(self.files_view)
self.files_layout.addStretch(50)
self.files_view = RecentFilesView(self, parent)
files_layout.addWidget(self.files_view)
files_layout.addStretch(50)
self.files_widget.setLayout(files_layout)
self.files_widget.hide()

# Help text
self.label = QtWidgets.QLabel(self.txt, self)
Expand All @@ -117,8 +121,9 @@ def __init__(self, parent):
def show(self):
files = BeeSettings().get_recent_files(existing_only=True)
self.files_view.update_files(files)
if files and self.layout.indexOf(self.files_layout) < 0:
self.layout.insertLayout(0, self.files_layout)
if files and self.layout.indexOf(self.files_widget) < 0:
self.layout.insertWidget(0, self.files_widget)
self.files_widget.show()
super().show()

def disable_mouse_events(self):
Expand Down
18 changes: 15 additions & 3 deletions tests/widgets/test_welcome_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,39 @@ def test_welcome_overlay_when_no_recent_files(qapp):
view = BeeGraphicsView(qapp, parent)
overlay = WelcomeOverlay(view)
overlay.show()
assert overlay.layout.indexOf(overlay.files_layout) < 0
assert overlay.layout.indexOf(overlay.files_widget) < 0


def test_recent_files_view_size_hint(qapp):
parent = QtWidgets.QMainWindow()
files_view = RecentFilesView(parent)
files_view = RecentFilesView(parent, None)

files_view.sizeHintForRow = lambda i: 10 + i
files_view.sizeHintForColumn = lambda i: 50 + i
files_view.update_files(['foo.png', 'bar.png'])
assert files_view.sizeHint() == QtCore.QSize(53, 25)


def test_recent_files_view_on_click(qapp):
parent = QtWidgets.QMainWindow()
view = BeeGraphicsView(qapp, parent)
view.open_from_file = MagicMock()
overlay = WelcomeOverlay(view)
overlay.files_view.update_files(['foo.bee', 'bar.bee'])
overlay.files_view.on_clicked(
RecentFilesModel(
['foo.bee', 'bar.bee']).createIndex(1, 0))
view.open_from_file.assert_called_once_with('bar.bee')


@patch('beeref.widgets.welcome_overlay.BeeSettings.get_recent_files',
return_value=['foo.bee', 'bar.bee'])
def test_welcome_overlay_when_recent_files(qapp):
parent = QtWidgets.QMainWindow()
view = BeeGraphicsView(qapp, parent)
overlay = WelcomeOverlay(view)
overlay.show()
assert overlay.layout.indexOf(overlay.files_layout) == 0
assert overlay.layout.indexOf(overlay.files_widget) == 0


@patch('PyQt6.QtWidgets.QGraphicsView.mousePressEvent')
Expand Down

0 comments on commit b176e79

Please sign in to comment.