def testInstance(self): """ test retrieving global instance """ self.assertTrue(QgsShortcutsManager.instance()) # register an action to the singleton action = QAction('test', None) QgsShortcutsManager.instance().registerAction(action) # check that the same instance is returned self.assertEqual(QgsShortcutsManager.instance().listActions(), [action]) s2 = QgsShortcutsManager() self.assertEqual(s2.listActions(), [])
def testUnregister(self): """ test unregistering from manager """ QSettings().clear() s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setKey('x') shortcut1.setObjectName('shortcut1') shortcut2 = QShortcut(None) shortcut2.setKey('y') shortcut2.setObjectName('shortcut2') action1 = QAction('action1', None) action1.setShortcut('x') action2 = QAction('action2', None) action2.setShortcut('y') # try unregistering objects not registered in manager self.assertFalse(s.unregisterShortcut(shortcut1)) self.assertFalse(s.unregisterAction(action1)) # try unregistering objects from manager s.registerShortcut(shortcut1) s.registerShortcut(shortcut2) s.registerAction(action1) s.registerAction(action2) self.assertEqual(set(s.listActions()), set([action1, action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) self.assertTrue(s.unregisterAction(action1)) self.assertTrue(s.unregisterShortcut(shortcut1)) self.assertEqual(set(s.listActions()), set([action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut2])) self.assertTrue(s.unregisterAction(action2)) self.assertTrue(s.unregisterShortcut(shortcut2))
def testRegisterAction(self): """ test registering actions """ QgsSettings().clear() s = QgsShortcutsManager(None) action1 = QAction('action1', None) action1.setShortcut('x') self.assertTrue(s.registerAction(action1, 'A')) action2 = QAction('action2', None) action2.setShortcut('y') self.assertTrue(s.registerAction(action2, 'B')) self.assertCountEqual(s.listActions(), [action1, action2]) # try re-registering an existing action - should fail, but leave action registered self.assertFalse(s.registerAction(action2, 'B')) self.assertCountEqual(s.listActions(), [action1, action2]) # actions should have been set to default sequences self.assertEqual(action1.shortcut().toString(), 'A') self.assertEqual(action2.shortcut().toString(), 'B') # test that adding an action should set its shortcut automatically s.setKeySequence('action1', 'C') s.setKeySequence('action2', 'D') s = QgsShortcutsManager(None) self.assertTrue(s.registerAction(action1, 'A')) self.assertTrue(s.registerAction(action2, 'B')) # actions should have been set to previous shortcuts self.assertEqual(action1.shortcut().toString(), 'C') self.assertEqual(action2.shortcut().toString(), 'D') # test registering an action containing '&' in name s = QgsShortcutsManager(None) action = QAction('&action1', None) self.assertTrue(s.registerAction(action)) self.assertEqual(action1.shortcut().toString(), 'C')
def testList(self): """ test listing registered objects """ QSettings().clear() s = QgsShortcutsManager(None) self.assertEqual(s.listActions(), []) self.assertEqual(s.listShortcuts(), []) self.assertEqual(s.listAll(), []) shortcut1 = QShortcut(None) shortcut2 = QShortcut(None) action1 = QAction('action1', None) action2 = QAction('action2', None) s.registerShortcut(shortcut1) s.registerShortcut(shortcut2) s.registerAction(action1) s.registerAction(action2) self.assertEqual(set(s.listActions()), set([action1, action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) self.assertEqual(set(s.listAll()), set([action1, action2, shortcut1, shortcut2]))
def testRegisterAll(self): """ test registering all children """ w = QWidget() action1 = QAction('action1', w) shortcut1 = QShortcut(w) shortcut1.setObjectName('shortcut1') w2 = QWidget(w) action2 = QAction('action2', w2) shortcut2 = QShortcut(w2) shortcut2.setObjectName('shortcut2') # recursive s = QgsShortcutsManager() s.registerAllChildActions(w, True) self.assertEqual(set(s.listActions()), set([action1, action2])) s.registerAllChildShortcuts(w, True) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) # non recursive s = QgsShortcutsManager() s.registerAllChildActions(w, False) self.assertEqual(set(s.listActions()), set([action1])) s.registerAllChildShortcuts(w, False) self.assertEqual(set(s.listShortcuts()), set([shortcut1])) # recursive s = QgsShortcutsManager() s.registerAllChildren(w, True) self.assertEqual(set(s.listActions()), set([action1, action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) # non recursive s = QgsShortcutsManager() s.registerAllChildren(w, False) self.assertEqual(set(s.listActions()), set([action1])) self.assertEqual(set(s.listShortcuts()), set([shortcut1]))
def testRegisterAll(self): """ test registering all children """ w = QWidget() action1 = QAction('action1', w) shortcut1 = QShortcut(w) shortcut1.setObjectName('shortcut1') w2 = QWidget(w) action2 = QAction('action2', w2) shortcut2 = QShortcut(w2) shortcut2.setObjectName('shortcut2') # recursive s = QgsShortcutsManager() s.registerAllChildActions(w, True) self.assertEqual(set(s.listActions()), set([action1, action2])) s.registerAllChildShortcuts(w, True) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) # non recursive s = QgsShortcutsManager() s.registerAllChildActions(w, False) self.assertEqual(set(s.listActions()), set([action1])) s.registerAllChildShortcuts(w, False) self.assertEqual(set(s.listShortcuts()), set([shortcut1])) # recursive s = QgsShortcutsManager() s.registerAllChildren(w, True) self.assertEqual(set(s.listActions()), set([action1, action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) # non recursive s = QgsShortcutsManager() s.registerAllChildren(w, False) self.assertEqual(set(s.listActions()), set([action1])) self.assertEqual(set(s.listShortcuts()), set([shortcut1]))
def testList(self): """ test listing registered objects """ QgsSettings().clear() s = QgsShortcutsManager(None) self.assertEqual(s.listActions(), []) self.assertEqual(s.listShortcuts(), []) self.assertEqual(s.listAll(), []) shortcut1 = QShortcut(None) shortcut2 = QShortcut(None) action1 = QAction('action1', None) action2 = QAction('action2', None) s.registerShortcut(shortcut1) s.registerShortcut(shortcut2) s.registerAction(action1) s.registerAction(action2) self.assertEqual(set(s.listActions()), set([action1, action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) self.assertEqual(set(s.listAll()), set([action1, action2, shortcut1, shortcut2]))