From a9d360dcb43836ff408d1392fa8a6745818ab71d Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Sat, 17 Apr 2021 23:59:59 +0200 Subject: [PATCH] Action for toggling scrollbars --- beeref/actions/actions.py | 6 ++++++ beeref/actions/menu_structure.py | 2 ++ beeref/actions/mixin.py | 6 +++++- beeref/view.py | 13 ++++++++++++- tests/actions/test_mixin.py | 31 +++++++++++++++++++++++++++---- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/beeref/actions/actions.py b/beeref/actions/actions.py index f03e015..03357ab 100644 --- a/beeref/actions/actions.py +++ b/beeref/actions/actions.py @@ -181,4 +181,10 @@ actions = [ 'shortcuts': ['Ctrl+H'], 'callback': 'on_action_help', }, + { + 'id': 'show_scrollbars', + 'text': 'Show &Scrollbars', + 'checkable': True, + 'callback': 'on_action_show_scrollbars', + }, ] diff --git a/beeref/actions/menu_structure.py b/beeref/actions/menu_structure.py index 5a7019d..41298ca 100644 --- a/beeref/actions/menu_structure.py +++ b/beeref/actions/menu_structure.py @@ -46,6 +46,8 @@ menu_structure = [ 'items': [ 'fit_scene', 'fit_selection', + MENU_SEPARATOR, + 'show_scrollbars', ], }, 'insert_images', diff --git a/beeref/actions/mixin.py b/beeref/actions/mixin.py index 25c2dc8..01448bc 100644 --- a/beeref/actions/mixin.py +++ b/beeref/actions/mixin.py @@ -42,9 +42,13 @@ class ActionsMixin: qaction = QtGui.QAction(action['text'], self) if 'shortcuts' in action: qaction.setShortcuts(action['shortcuts']) - qaction.triggered.connect(getattr(self, action['callback'])) + if action.get('checkable', False): + qaction.toggled.connect(getattr(self, action['callback'])) + else: + qaction.triggered.connect(getattr(self, action['callback'])) self.addAction(qaction) qaction.setEnabled(action.get('enabled', True)) + qaction.setCheckable(action.get('checkable', False)) self.bee_actions[action['id']] = qaction if 'group' in action: self.bee_actiongroups[action['group']].append(qaction) diff --git a/beeref/view.py b/beeref/view.py index 6987da4..f45a5e5 100644 --- a/beeref/view.py +++ b/beeref/view.py @@ -48,7 +48,6 @@ class BeeGraphicsView(QtWidgets.QGraphicsView, ActionsMixin): self.scene = BeeGraphicsScene(self.undo_stack) self.filename = None - # TBD: make scrollbar configurable self.setHorizontalScrollBarPolicy( Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy( @@ -143,6 +142,18 @@ class BeeGraphicsView(QtWidgets.QGraphicsView, ActionsMixin): def on_action_fit_selection(self): self.fit_rect(self.scene.itemsBoundingRect(selection_only=True)) + def on_action_show_scrollbars(self, checked): + if checked: + self.setHorizontalScrollBarPolicy( + Qt.ScrollBarPolicy.ScrollBarAsNeeded) + self.setVerticalScrollBarPolicy( + Qt.ScrollBarPolicy.ScrollBarAsNeeded) + else: + self.setHorizontalScrollBarPolicy( + Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + self.setVerticalScrollBarPolicy( + Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + def on_action_undo(self): logger.debug('Undo: %s' % self.undo_stack.undoText()) self.undo_stack.undo() diff --git a/tests/actions/test_mixin.py b/tests/actions/test_mixin.py index 8417fc2..f511bdd 100644 --- a/tests/actions/test_mixin.py +++ b/tests/actions/test_mixin.py @@ -23,7 +23,9 @@ class ActionsMixinTestCase(BeeTestCase): self.addCleanup(actions_patcher.stop) self.widget = FooWidget() - def test_create_actions(self): + @patch('PyQt6.QtGui.QAction.triggered') + @patch('PyQt6.QtGui.QAction.toggled') + def test_create_actions(self, toggle_mock, trigger_mock): self.actions_mock.__iter__.return_value = [{ 'id': 'foo', 'text': '&Foo', @@ -31,9 +33,10 @@ class ActionsMixinTestCase(BeeTestCase): 'callback': 'on_foo', }] - with patch('PyQt6.QtGui.QAction.triggered') as trigger_mock: - self.widget._create_actions() - trigger_mock.connect.assert_called_once_with(self.widget.on_foo) + self.widget._create_actions() + trigger_mock.connect.assert_called_once_with(self.widget.on_foo) + toggle_mock.connect.assert_not_called() + assert len(self.widget.actions()) == 1 qaction = self.widget.actions()[0] qaction.text() == '&Foo' @@ -41,6 +44,26 @@ class ActionsMixinTestCase(BeeTestCase): qaction.isEnabled() is True assert self.widget.bee_actions['foo'] == qaction + @patch('PyQt6.QtGui.QAction.triggered') + @patch('PyQt6.QtGui.QAction.toggled') + def test_create_actions_checkable(self, toggle_mock, trigger_mock): + self.actions_mock.__iter__.return_value = [{ + 'id': 'foo', + 'text': '&Foo', + 'checkable': True, + 'callback': 'on_foo', + }] + + self.widget._create_actions() + trigger_mock.connect.assert_not_called() + toggle_mock.connect.assert_called_once_with(self.widget.on_foo) + + assert len(self.widget.actions()) == 1 + qaction = self.widget.actions()[0] + qaction.text() == '&Foo' + qaction.isEnabled() is True + assert self.widget.bee_actions['foo'] == qaction + def test_create_actions_enabled_false(self): self.actions_mock.__iter__.return_value = [{ 'id': 'foo',