Action for toggling scrollbars

This commit is contained in:
Rebecca Breu 2021-04-17 23:59:59 +02:00
parent a358d86620
commit a9d360dcb4
5 changed files with 52 additions and 6 deletions

View file

@ -181,4 +181,10 @@ actions = [
'shortcuts': ['Ctrl+H'], 'shortcuts': ['Ctrl+H'],
'callback': 'on_action_help', 'callback': 'on_action_help',
}, },
{
'id': 'show_scrollbars',
'text': 'Show &Scrollbars',
'checkable': True,
'callback': 'on_action_show_scrollbars',
},
] ]

View file

@ -46,6 +46,8 @@ menu_structure = [
'items': [ 'items': [
'fit_scene', 'fit_scene',
'fit_selection', 'fit_selection',
MENU_SEPARATOR,
'show_scrollbars',
], ],
}, },
'insert_images', 'insert_images',

View file

@ -42,9 +42,13 @@ class ActionsMixin:
qaction = QtGui.QAction(action['text'], self) qaction = QtGui.QAction(action['text'], self)
if 'shortcuts' in action: if 'shortcuts' in action:
qaction.setShortcuts(action['shortcuts']) 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) self.addAction(qaction)
qaction.setEnabled(action.get('enabled', True)) qaction.setEnabled(action.get('enabled', True))
qaction.setCheckable(action.get('checkable', False))
self.bee_actions[action['id']] = qaction self.bee_actions[action['id']] = qaction
if 'group' in action: if 'group' in action:
self.bee_actiongroups[action['group']].append(qaction) self.bee_actiongroups[action['group']].append(qaction)

View file

@ -48,7 +48,6 @@ class BeeGraphicsView(QtWidgets.QGraphicsView, ActionsMixin):
self.scene = BeeGraphicsScene(self.undo_stack) self.scene = BeeGraphicsScene(self.undo_stack)
self.filename = None self.filename = None
# TBD: make scrollbar configurable
self.setHorizontalScrollBarPolicy( self.setHorizontalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAlwaysOff) Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy( self.setVerticalScrollBarPolicy(
@ -143,6 +142,18 @@ class BeeGraphicsView(QtWidgets.QGraphicsView, ActionsMixin):
def on_action_fit_selection(self): def on_action_fit_selection(self):
self.fit_rect(self.scene.itemsBoundingRect(selection_only=True)) 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): def on_action_undo(self):
logger.debug('Undo: %s' % self.undo_stack.undoText()) logger.debug('Undo: %s' % self.undo_stack.undoText())
self.undo_stack.undo() self.undo_stack.undo()

View file

@ -23,7 +23,9 @@ class ActionsMixinTestCase(BeeTestCase):
self.addCleanup(actions_patcher.stop) self.addCleanup(actions_patcher.stop)
self.widget = FooWidget() 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 = [{ self.actions_mock.__iter__.return_value = [{
'id': 'foo', 'id': 'foo',
'text': '&Foo', 'text': '&Foo',
@ -31,9 +33,10 @@ class ActionsMixinTestCase(BeeTestCase):
'callback': 'on_foo', 'callback': 'on_foo',
}] }]
with patch('PyQt6.QtGui.QAction.triggered') as trigger_mock: self.widget._create_actions()
self.widget._create_actions() trigger_mock.connect.assert_called_once_with(self.widget.on_foo)
trigger_mock.connect.assert_called_once_with(self.widget.on_foo) toggle_mock.connect.assert_not_called()
assert len(self.widget.actions()) == 1 assert len(self.widget.actions()) == 1
qaction = self.widget.actions()[0] qaction = self.widget.actions()[0]
qaction.text() == '&Foo' qaction.text() == '&Foo'
@ -41,6 +44,26 @@ class ActionsMixinTestCase(BeeTestCase):
qaction.isEnabled() is True qaction.isEnabled() is True
assert self.widget.bee_actions['foo'] == qaction 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): def test_create_actions_enabled_false(self):
self.actions_mock.__iter__.return_value = [{ self.actions_mock.__iter__.return_value = [{
'id': 'foo', 'id': 'foo',