src/eric7/EricWidgets/EricToolBarManager.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
15 15
16 class EricToolBarManager(QObject): 16 class EricToolBarManager(QObject):
17 """ 17 """
18 Class implementing a toolbar manager. 18 Class implementing a toolbar manager.
19 """ 19 """
20 VersionMarker = 0xffff 20
21 ToolBarMarker = 0xfefe 21 VersionMarker = 0xFFFF
22 CustomToolBarMarker = 0xfdfd 22 ToolBarMarker = 0xFEFE
23 23 CustomToolBarMarker = 0xFDFD
24
24 def __init__(self, ui=None, parent=None): 25 def __init__(self, ui=None, parent=None):
25 """ 26 """
26 Constructor 27 Constructor
27 28
28 @param ui reference to the user interface object (UI.UserInterface) 29 @param ui reference to the user interface object (UI.UserInterface)
29 @param parent reference to the parent object (QObject) 30 @param parent reference to the parent object (QObject)
30 """ 31 """
31 super().__init__(parent) 32 super().__init__(parent)
32 33
33 self.__mainWindow = None 34 self.__mainWindow = None
34 self.__ui = ui 35 self.__ui = ui
35 36
36 self.__toolBars = {} 37 self.__toolBars = {}
37 # maps toolbar IDs to actions 38 # maps toolbar IDs to actions
38 self.__toolBarsWithSeparators = {} 39 self.__toolBarsWithSeparators = {}
39 # maps toolbar IDs to actions incl. separators 40 # maps toolbar IDs to actions incl. separators
40 self.__defaultToolBars = {} 41 self.__defaultToolBars = {}
41 # maps default toolbar IDs to actions 42 # maps default toolbar IDs to actions
42 self.__customToolBars = [] 43 self.__customToolBars = []
43 # list of custom toolbars 44 # list of custom toolbars
44 self.__allToolBars = {} 45 self.__allToolBars = {}
45 # maps toolbar IDs to toolbars 46 # maps toolbar IDs to toolbars
46 47
47 self.__categoryToActions = {} 48 self.__categoryToActions = {}
48 # maps categories to actions 49 # maps categories to actions
49 self.__actionToCategory = {} 50 self.__actionToCategory = {}
50 # maps action IDs to categories 51 # maps action IDs to categories
51 self.__allActions = {} 52 self.__allActions = {}
52 # maps action IDs to actions 53 # maps action IDs to actions
53 self.__actionToToolBars = {} 54 self.__actionToToolBars = {}
54 # maps action IDs to toolbars 55 # maps action IDs to toolbars
55 56
56 self.__widgetActions = {} 57 self.__widgetActions = {}
57 # maps widget action IDs to toolbars 58 # maps widget action IDs to toolbars
58 self.__allWidgetActions = {} 59 self.__allWidgetActions = {}
59 # maps widget action IDs to widget actions 60 # maps widget action IDs to widget actions
60 61
61 ###################################################### 62 ######################################################
62 ## Private methods 63 ## Private methods
63 ###################################################### 64 ######################################################
64 65
65 def __toolBarByName(self, name): 66 def __toolBarByName(self, name):
66 """ 67 """
67 Private slot to get a toolbar by its object name. 68 Private slot to get a toolbar by its object name.
68 69
69 @param name object name of the toolbar (string) 70 @param name object name of the toolbar (string)
70 @return reference to the toolbar (QToolBar) 71 @return reference to the toolbar (QToolBar)
71 """ 72 """
72 for toolBar in list(self.__allToolBars.values()): 73 for toolBar in list(self.__allToolBars.values()):
73 if toolBar.objectName() == name: 74 if toolBar.objectName() == name:
74 return toolBar 75 return toolBar
75 return None 76 return None
76 77
77 def __findAction(self, name): 78 def __findAction(self, name):
78 """ 79 """
79 Private method to find an action by name. 80 Private method to find an action by name.
80 81
81 @param name name of the action to search for (string) 82 @param name name of the action to search for (string)
82 @return reference to the action (QAction) 83 @return reference to the action (QAction)
83 """ 84 """
84 # check objectName() first 85 # check objectName() first
85 for action in list(self.__allActions.values()): 86 for action in list(self.__allActions.values()):
86 if action.objectName() == name: 87 if action.objectName() == name:
87 return action 88 return action
88 89
89 # check text() next 90 # check text() next
90 for action in list(self.__allActions.values()): 91 for action in list(self.__allActions.values()):
91 if action.text() == name: 92 if action.text() == name:
92 return action 93 return action
93 94
94 return None 95 return None
95 96
96 def __findDefaultToolBar(self, name): 97 def __findDefaultToolBar(self, name):
97 """ 98 """
98 Private method to find a default toolbar by name. 99 Private method to find a default toolbar by name.
99 100
100 @param name name of the default toolbar to search for (string) 101 @param name name of the default toolbar to search for (string)
101 @return reference to the default toolbar (QToolBar) 102 @return reference to the default toolbar (QToolBar)
102 """ 103 """
103 # check objectName() first 104 # check objectName() first
104 for tbID in self.__defaultToolBars: 105 for tbID in self.__defaultToolBars:
105 tb = self.__allToolBars[tbID] 106 tb = self.__allToolBars[tbID]
106 if tb.objectName() == name: 107 if tb.objectName() == name:
107 return tb 108 return tb
108 109
109 # check windowTitle() next 110 # check windowTitle() next
110 for tbID in self.__defaultToolBars: 111 for tbID in self.__defaultToolBars:
111 tb = self.__allToolBars[tbID] 112 tb = self.__allToolBars[tbID]
112 if tb.windowTitle() == name: 113 if tb.windowTitle() == name:
113 return tb 114 return tb
114 115
115 return None 116 return None
116 117
117 ###################################################### 118 ######################################################
118 ## Public methods 119 ## Public methods
119 ###################################################### 120 ######################################################
120 121
121 def setMainWindow(self, mainWindow): 122 def setMainWindow(self, mainWindow):
122 """ 123 """
123 Public method to set the reference to the main window. 124 Public method to set the reference to the main window.
124 125
125 @param mainWindow reference to the main window (QMainWindow) 126 @param mainWindow reference to the main window (QMainWindow)
126 """ 127 """
127 self.__mainWindow = mainWindow 128 self.__mainWindow = mainWindow
128 129
129 def mainWindow(self): 130 def mainWindow(self):
130 """ 131 """
131 Public method to get the reference to the main window. 132 Public method to get the reference to the main window.
132 133
133 @return reference to the main window (QMainWindow) 134 @return reference to the main window (QMainWindow)
134 """ 135 """
135 return self.__mainWindow 136 return self.__mainWindow
136 137
137 def addToolBar(self, toolBar, category): 138 def addToolBar(self, toolBar, category):
138 """ 139 """
139 Public method to add a toolbar to be managed. 140 Public method to add a toolbar to be managed.
140 141
141 @param toolBar reference to the toolbar to be managed (QToolBar) 142 @param toolBar reference to the toolbar to be managed (QToolBar)
142 @param category category for the toolbar (string) 143 @param category category for the toolbar (string)
143 """ 144 """
144 if toolBar is None: 145 if toolBar is None:
145 return 146 return
146 147
147 newActions = [] 148 newActions = []
148 newActionsWithSeparators = [] 149 newActionsWithSeparators = []
149 actions = toolBar.actions() 150 actions = toolBar.actions()
150 for action in actions: 151 for action in actions:
151 actID = id(action) 152 actID = id(action)
162 tbID = id(toolBar) 163 tbID = id(toolBar)
163 self.__defaultToolBars[tbID] = newActions 164 self.__defaultToolBars[tbID] = newActions
164 self.__toolBars[tbID] = newActions 165 self.__toolBars[tbID] = newActions
165 self.__toolBarsWithSeparators[tbID] = newActionsWithSeparators 166 self.__toolBarsWithSeparators[tbID] = newActionsWithSeparators
166 self.__allToolBars[tbID] = toolBar 167 self.__allToolBars[tbID] = toolBar
167 168
168 def removeToolBar(self, toolBar): 169 def removeToolBar(self, toolBar):
169 """ 170 """
170 Public method to remove a toolbar added with addToolBar(). 171 Public method to remove a toolbar added with addToolBar().
171 172
172 @param toolBar reference to the toolbar to be removed (QToolBar) 173 @param toolBar reference to the toolbar to be removed (QToolBar)
173 """ 174 """
174 if toolBar is None: 175 if toolBar is None:
175 return 176 return
176 177
177 tbID = id(toolBar) 178 tbID = id(toolBar)
178 179
179 if tbID not in self.__defaultToolBars: 180 if tbID not in self.__defaultToolBars:
180 return 181 return
181 182
182 defaultActions = self.__defaultToolBars[tbID][:] 183 defaultActions = self.__defaultToolBars[tbID][:]
183 self.setToolBar(toolBar, []) 184 self.setToolBar(toolBar, [])
184 for action in defaultActions: 185 for action in defaultActions:
185 self.removeAction(action) 186 self.removeAction(action)
186 187
187 del self.__defaultToolBars[tbID] 188 del self.__defaultToolBars[tbID]
188 del self.__toolBars[tbID] 189 del self.__toolBars[tbID]
189 del self.__toolBarsWithSeparators[tbID] 190 del self.__toolBarsWithSeparators[tbID]
190 del self.__allToolBars[tbID] 191 del self.__allToolBars[tbID]
191 192
192 for action in defaultActions: 193 for action in defaultActions:
193 if action is None: 194 if action is None:
194 toolBar.addSeparator() 195 toolBar.addSeparator()
195 else: 196 else:
196 toolBar.addAction(action) 197 toolBar.addAction(action)
197 198
198 def setToolBars(self, toolBars): 199 def setToolBars(self, toolBars):
199 """ 200 """
200 Public method to set the actions of several toolbars. 201 Public method to set the actions of several toolbars.
201 202
202 @param toolBars dictionary with toolbar id as key and 203 @param toolBars dictionary with toolbar id as key and
203 a list of actions as value 204 a list of actions as value
204 """ 205 """
205 for key, actions in list(toolBars.items()): 206 for key, actions in list(toolBars.items()):
206 tb = self.__allToolBars[key] 207 tb = self.__allToolBars[key]
207 self.setToolBar(tb, actions) 208 self.setToolBar(tb, actions)
208 209
209 def setToolBar(self, toolBar, actions): 210 def setToolBar(self, toolBar, actions):
210 """ 211 """
211 Public method to set the actions of a toolbar. 212 Public method to set the actions of a toolbar.
212 213
213 @param toolBar reference to the toolbar to configure (QToolBar) 214 @param toolBar reference to the toolbar to configure (QToolBar)
214 @param actions list of actions to be set (list of QAction) 215 @param actions list of actions to be set (list of QAction)
215 """ 216 """
216 if toolBar is None: 217 if toolBar is None:
217 return 218 return
218 219
219 tbID = id(toolBar) 220 tbID = id(toolBar)
220 if tbID not in self.__toolBars: 221 if tbID not in self.__toolBars:
221 return 222 return
222 if self.__toolBars[tbID] == actions: 223 if self.__toolBars[tbID] == actions:
223 return 224 return
224 225
225 # step 1: check list of actions 226 # step 1: check list of actions
226 toRemove = {} 227 toRemove = {}
227 newActions = [] 228 newActions = []
228 for action in actions: 229 for action in actions:
229 if ( 230 if action is None or (
230 action is None or 231 action not in newActions and id(action) in self.__allActions
231 (action not in newActions and
232 id(action) in self.__allActions)
233 ): 232 ):
234 newActions.append(action) 233 newActions.append(action)
235 oldTB = self.toolBarWidgetAction(action) 234 oldTB = self.toolBarWidgetAction(action)
236 if oldTB is not None and oldTB != toolBar: 235 if oldTB is not None and oldTB != toolBar:
237 if id(oldTB) not in toRemove: 236 if id(oldTB) not in toRemove:
238 toRemove[id(oldTB)] = [] 237 toRemove[id(oldTB)] = []
239 toRemove[id(oldTB)].append(action) 238 toRemove[id(oldTB)].append(action)
240 self.removeWidgetActions(toRemove) 239 self.removeWidgetActions(toRemove)
241 240
242 # step 2: remove all toolbar actions 241 # step 2: remove all toolbar actions
243 for action in self.__toolBarsWithSeparators[tbID]: 242 for action in self.__toolBarsWithSeparators[tbID]:
244 if self.toolBarWidgetAction(action) == tbID: 243 if self.toolBarWidgetAction(action) == tbID:
245 self.__widgetActions[id(action)] = None 244 self.__widgetActions[id(action)] = None
246 toolBar.removeAction(action) 245 toolBar.removeAction(action)
247 if action.isSeparator(): 246 if action.isSeparator():
248 del action 247 del action
249 else: 248 else:
250 self.__actionToToolBars[id(action)].remove(toolBar) 249 self.__actionToToolBars[id(action)].remove(toolBar)
251 250
252 # step 3: set the actions as requested 251 # step 3: set the actions as requested
253 newActionsWithSeparators = [] 252 newActionsWithSeparators = []
254 for action in newActions: 253 for action in newActions:
255 newAction = None 254 newAction = None
256 if action is None: 255 if action is None:
262 if id(action) in self.__widgetActions: 261 if id(action) in self.__widgetActions:
263 self.__widgetActions[id(action)] = toolBar 262 self.__widgetActions[id(action)] = toolBar
264 else: 263 else:
265 continue 264 continue
266 newActionsWithSeparators.append(newAction) 265 newActionsWithSeparators.append(newAction)
267 266
268 if toolBar.isVisible(): 267 if toolBar.isVisible():
269 toolBar.hide() 268 toolBar.hide()
270 toolBar.show() 269 toolBar.show()
271 self.__toolBars[tbID] = newActions 270 self.__toolBars[tbID] = newActions
272 self.__toolBarsWithSeparators[tbID] = newActionsWithSeparators 271 self.__toolBarsWithSeparators[tbID] = newActionsWithSeparators
273 272
274 def resetToolBar(self, toolBar): 273 def resetToolBar(self, toolBar):
275 """ 274 """
276 Public method to reset a toolbar to its default state. 275 Public method to reset a toolbar to its default state.
277 276
278 @param toolBar reference to the toolbar to configure (QToolBar) 277 @param toolBar reference to the toolbar to configure (QToolBar)
279 """ 278 """
280 if not self.isDefaultToolBar(): 279 if not self.isDefaultToolBar():
281 return 280 return
282 self.setToolBar(toolBar, self.__defaultToolBars[id(toolBar)]) 281 self.setToolBar(toolBar, self.__defaultToolBars[id(toolBar)])
283 282
284 def resetAllToolBars(self): 283 def resetAllToolBars(self):
285 """ 284 """
286 Public method to reset all toolbars to their default state. 285 Public method to reset all toolbars to their default state.
287 """ 286 """
288 self.setToolBars(self.__defaultToolBars) 287 self.setToolBars(self.__defaultToolBars)
289 for toolBar in self.__customToolBars[:]: 288 for toolBar in self.__customToolBars[:]:
290 self.deleteToolBar(toolBar) 289 self.deleteToolBar(toolBar)
291 290
292 def defaultToolBars(self): 291 def defaultToolBars(self):
293 """ 292 """
294 Public method to get all toolbars added with addToolBar(). 293 Public method to get all toolbars added with addToolBar().
295 294
296 @return list of all default toolbars (list of QToolBar) 295 @return list of all default toolbars (list of QToolBar)
297 """ 296 """
298 return list(self.__defaultToolBars.values()) 297 return list(self.__defaultToolBars.values())
299 298
300 def isDefaultToolBar(self, toolBar): 299 def isDefaultToolBar(self, toolBar):
301 """ 300 """
302 Public method to check, if a toolbar was added with addToolBar(). 301 Public method to check, if a toolbar was added with addToolBar().
303 302
304 @param toolBar reference to the toolbar to be checked (QToolBar) 303 @param toolBar reference to the toolbar to be checked (QToolBar)
305 @return flag indicating an added toolbar (boolean) 304 @return flag indicating an added toolbar (boolean)
306 """ 305 """
307 return ( 306 return toolBar is not None and id(toolBar) in self.__defaultToolBars
308 toolBar is not None and 307
309 id(toolBar) in self.__defaultToolBars
310 )
311
312 def createToolBar(self, title, name=""): 308 def createToolBar(self, title, name=""):
313 """ 309 """
314 Public method to create a custom toolbar. 310 Public method to create a custom toolbar.
315 311
316 @param title title to be used for the toolbar (string) 312 @param title title to be used for the toolbar (string)
317 @param name optional name for the new toolbar (string) 313 @param name optional name for the new toolbar (string)
318 @return reference to the created toolbar (QToolBar) 314 @return reference to the created toolbar (QToolBar)
319 """ 315 """
320 if self.__mainWindow is None: 316 if self.__mainWindow is None:
321 return None 317 return None
322 318
323 toolBar = QToolBar(title, self.__mainWindow) 319 toolBar = QToolBar(title, self.__mainWindow)
324 toolBar.setToolTip(title) 320 toolBar.setToolTip(title)
325 if not name: 321 if not name:
326 index = 1 322 index = 1
327 customPrefix = "__CustomPrefix__" 323 customPrefix = "__CustomPrefix__"
329 while self.__toolBarByName(name) is not None: 325 while self.__toolBarByName(name) is not None:
330 index += 1 326 index += 1
331 name = "{0}{1:d}".format(customPrefix, index) 327 name = "{0}{1:d}".format(customPrefix, index)
332 toolBar.setObjectName(name) 328 toolBar.setObjectName(name)
333 self.__mainWindow.addToolBar(toolBar) 329 self.__mainWindow.addToolBar(toolBar)
334 330
335 tbID = id(toolBar) 331 tbID = id(toolBar)
336 self.__customToolBars.append(toolBar) 332 self.__customToolBars.append(toolBar)
337 self.__allToolBars[tbID] = toolBar 333 self.__allToolBars[tbID] = toolBar
338 self.__toolBars[tbID] = [] 334 self.__toolBars[tbID] = []
339 self.__toolBarsWithSeparators[tbID] = [] 335 self.__toolBarsWithSeparators[tbID] = []
340 336
341 if self.__ui is not None: 337 if self.__ui is not None:
342 toolBar.setIconSize(self.__ui.getToolBarIconSize()) 338 toolBar.setIconSize(self.__ui.getToolBarIconSize())
343 self.__ui.registerToolbar(name, title, toolBar) 339 self.__ui.registerToolbar(name, title, toolBar)
344 340
345 return toolBar 341 return toolBar
346 342
347 def deleteToolBar(self, toolBar): 343 def deleteToolBar(self, toolBar):
348 """ 344 """
349 Public method to remove a custom toolbar created with createToolBar(). 345 Public method to remove a custom toolbar created with createToolBar().
350 346
351 @param toolBar reference to the toolbar to be managed (QToolBar) 347 @param toolBar reference to the toolbar to be managed (QToolBar)
352 """ 348 """
353 if toolBar is None: 349 if toolBar is None:
354 return 350 return
355 351
356 tbID = id(toolBar) 352 tbID = id(toolBar)
357 if tbID not in self.__toolBars: 353 if tbID not in self.__toolBars:
358 return 354 return
359 if tbID in self.__defaultToolBars: 355 if tbID in self.__defaultToolBars:
360 return 356 return
361 357
362 if self.__ui is not None: 358 if self.__ui is not None:
363 self.__ui.unregisterToolbar(toolBar.objectName()) 359 self.__ui.unregisterToolbar(toolBar.objectName())
364 360
365 self.setToolBar(toolBar, []) 361 self.setToolBar(toolBar, [])
366 362
367 del self.__allToolBars[tbID] 363 del self.__allToolBars[tbID]
368 del self.__toolBars[tbID] 364 del self.__toolBars[tbID]
369 del self.__toolBarsWithSeparators[tbID] 365 del self.__toolBarsWithSeparators[tbID]
370 self.__customToolBars.remove(toolBar) 366 self.__customToolBars.remove(toolBar)
371 self.__mainWindow.removeToolBar(toolBar) 367 self.__mainWindow.removeToolBar(toolBar)
372 del toolBar 368 del toolBar
373 369
374 def renameToolBar(self, toolBar, title): 370 def renameToolBar(self, toolBar, title):
375 """ 371 """
376 Public method to give a toolbar a new title. 372 Public method to give a toolbar a new title.
377 373
378 @param toolBar reference to the toolbar to be managed (QToolBar) 374 @param toolBar reference to the toolbar to be managed (QToolBar)
379 @param title title to be used for the toolbar (string) 375 @param title title to be used for the toolbar (string)
380 """ 376 """
381 if toolBar is None: 377 if toolBar is None:
382 return 378 return
383 379
384 toolBar.setWindowTitle(title) 380 toolBar.setWindowTitle(title)
385 381
386 if self.__ui is not None: 382 if self.__ui is not None:
387 self.__ui.reregisterToolbar(toolBar.objectName(), title) 383 self.__ui.reregisterToolbar(toolBar.objectName(), title)
388 384
389 def toolBars(self): 385 def toolBars(self):
390 """ 386 """
391 Public method to get all toolbars. 387 Public method to get all toolbars.
392 388
393 @return list of all toolbars (list of QToolBar) 389 @return list of all toolbars (list of QToolBar)
394 """ 390 """
395 return list(self.__allToolBars.values()) 391 return list(self.__allToolBars.values())
396 392
397 def addAction(self, action, category): 393 def addAction(self, action, category):
398 """ 394 """
399 Public method to add an action to be managed. 395 Public method to add an action to be managed.
400 396
401 @param action reference to the action to be managed (QAction) 397 @param action reference to the action to be managed (QAction)
402 @param category category for the toolbar (string) 398 @param category category for the toolbar (string)
403 """ 399 """
404 if action is None: 400 if action is None:
405 return 401 return
406 if action.isSeparator(): 402 if action.isSeparator():
407 return 403 return
408 if id(action) in self.__allActions: 404 if id(action) in self.__allActions:
409 return 405 return
410 406
411 if action.metaObject().className() == "QWidgetAction": 407 if action.metaObject().className() == "QWidgetAction":
412 self.__widgetActions[id(action)] = None 408 self.__widgetActions[id(action)] = None
413 self.__allWidgetActions[id(action)] = action 409 self.__allWidgetActions[id(action)] = action
414 self.__allActions[id(action)] = action 410 self.__allActions[id(action)] = action
415 if category not in self.__categoryToActions: 411 if category not in self.__categoryToActions:
416 self.__categoryToActions[category] = [] 412 self.__categoryToActions[category] = []
417 self.__categoryToActions[category].append(action) 413 self.__categoryToActions[category].append(action)
418 self.__actionToCategory[id(action)] = category 414 self.__actionToCategory[id(action)] = category
419 self.__actionToToolBars[id(action)] = [] 415 self.__actionToToolBars[id(action)] = []
420 416
421 def addActions(self, actions, category): 417 def addActions(self, actions, category):
422 """ 418 """
423 Public method to add actions to be managed. 419 Public method to add actions to be managed.
424 420
425 @param actions list of actions to be managed 421 @param actions list of actions to be managed
426 @type list of QAction 422 @type list of QAction
427 @param category category for the toolbar 423 @param category category for the toolbar
428 @type str 424 @type str
429 """ 425 """
430 for action in actions: 426 for action in actions:
431 self.addAction(action, category) 427 self.addAction(action, category)
432 428
433 def removeAction(self, action): 429 def removeAction(self, action):
434 """ 430 """
435 Public method to remove an action from the manager. 431 Public method to remove an action from the manager.
436 432
437 @param action reference to the action to be removed (QAction) 433 @param action reference to the action to be removed (QAction)
438 """ 434 """
439 aID = id(action) 435 aID = id(action)
440 436
441 if aID not in self.__allActions: 437 if aID not in self.__allActions:
442 return 438 return
443 439
444 toolBars = self.__actionToToolBars[aID] 440 toolBars = self.__actionToToolBars[aID]
445 for toolBar in toolBars: 441 for toolBar in toolBars:
446 tbID = id(toolBar) 442 tbID = id(toolBar)
447 self.__toolBars[tbID].remove(action) 443 self.__toolBars[tbID].remove(action)
448 self.__toolBarsWithSeparators[tbID].remove(action) 444 self.__toolBarsWithSeparators[tbID].remove(action)
449 toolBar.removeAction(action) 445 toolBar.removeAction(action)
450 if toolBar.isVisible(): 446 if toolBar.isVisible():
451 toolBar.hide() 447 toolBar.hide()
452 toolBar.show() 448 toolBar.show()
453 449
454 for tbID in self.__defaultToolBars: 450 for tbID in self.__defaultToolBars:
455 if action in self.__defaultToolBars[tbID]: 451 if action in self.__defaultToolBars[tbID]:
456 self.__defaultToolBars[tbID].remove(action) 452 self.__defaultToolBars[tbID].remove(action)
457 453
458 del self.__allActions[aID] 454 del self.__allActions[aID]
459 if aID in self.__widgetActions: 455 if aID in self.__widgetActions:
460 del self.__widgetActions[aID] 456 del self.__widgetActions[aID]
461 del self.__allWidgetActions[aID] 457 del self.__allWidgetActions[aID]
462 del self.__actionToCategory[aID] 458 del self.__actionToCategory[aID]
463 del self.__actionToToolBars[aID] 459 del self.__actionToToolBars[aID]
464 460
465 for category in self.__categoryToActions: 461 for category in self.__categoryToActions:
466 if action in self.__categoryToActions[category]: 462 if action in self.__categoryToActions[category]:
467 self.__categoryToActions[category].remove(action) 463 self.__categoryToActions[category].remove(action)
468 464
469 def removeCategoryActions(self, category): 465 def removeCategoryActions(self, category):
470 """ 466 """
471 Public method to remove the actions belonging to a category. 467 Public method to remove the actions belonging to a category.
472 468
473 @param category category for the actions (string) 469 @param category category for the actions (string)
474 """ 470 """
475 for action in self.categoryActions(category): 471 for action in self.categoryActions(category):
476 self.removeAction(action) 472 self.removeAction(action)
477 473
478 def saveState(self, version=0): 474 def saveState(self, version=0):
479 """ 475 """
480 Public method to save the state of the toolbar manager. 476 Public method to save the state of the toolbar manager.
481 477
482 @param version version number stored with the data (integer) 478 @param version version number stored with the data (integer)
483 @return saved state as a byte array (QByteArray) 479 @return saved state as a byte array (QByteArray)
484 """ 480 """
485 data = QByteArray() 481 data = QByteArray()
486 stream = QDataStream(data, QIODevice.OpenModeFlag.WriteOnly) 482 stream = QDataStream(data, QIODevice.OpenModeFlag.WriteOnly)
487 stream.setVersion(QDataStream.Version.Qt_4_6) 483 stream.setVersion(QDataStream.Version.Qt_4_6)
488 stream.writeUInt16(EricToolBarManager.VersionMarker) 484 stream.writeUInt16(EricToolBarManager.VersionMarker)
489 stream.writeUInt16(version) 485 stream.writeUInt16(version)
490 486
491 # save default toolbars 487 # save default toolbars
492 stream.writeUInt16(EricToolBarManager.ToolBarMarker) 488 stream.writeUInt16(EricToolBarManager.ToolBarMarker)
493 stream.writeUInt16(len(self.__defaultToolBars)) 489 stream.writeUInt16(len(self.__defaultToolBars))
494 for tbID in self.__defaultToolBars: 490 for tbID in self.__defaultToolBars:
495 tb = self.__allToolBars[tbID] 491 tb = self.__allToolBars[tbID]
504 stream.writeString(action.objectName().encode("utf-8")) 500 stream.writeString(action.objectName().encode("utf-8"))
505 else: 501 else:
506 stream.writeString(action.text().encode("utf-8")) 502 stream.writeString(action.text().encode("utf-8"))
507 else: 503 else:
508 stream.writeString("".encode("utf-8")) 504 stream.writeString("".encode("utf-8"))
509 505
510 # save the custom toolbars 506 # save the custom toolbars
511 stream.writeUInt16(EricToolBarManager.CustomToolBarMarker) 507 stream.writeUInt16(EricToolBarManager.CustomToolBarMarker)
512 stream.writeUInt16(len(self.__toolBars) - len(self.__defaultToolBars)) 508 stream.writeUInt16(len(self.__toolBars) - len(self.__defaultToolBars))
513 for tbID in self.__toolBars: 509 for tbID in self.__toolBars:
514 if tbID not in self.__defaultToolBars: 510 if tbID not in self.__defaultToolBars:
517 stream.writeString(tb.windowTitle().encode("utf-8")) 513 stream.writeString(tb.windowTitle().encode("utf-8"))
518 stream.writeUInt16(len(self.__toolBars[tbID])) 514 stream.writeUInt16(len(self.__toolBars[tbID]))
519 for action in self.__toolBars[tbID]: 515 for action in self.__toolBars[tbID]:
520 if action is not None: 516 if action is not None:
521 if action.objectName(): 517 if action.objectName():
522 stream.writeString(action.objectName() 518 stream.writeString(action.objectName().encode("utf-8"))
523 .encode("utf-8"))
524 else: 519 else:
525 stream.writeString(action.text().encode("utf-8")) 520 stream.writeString(action.text().encode("utf-8"))
526 else: 521 else:
527 stream.writeString("".encode("utf-8")) 522 stream.writeString("".encode("utf-8"))
528 523
529 return data 524 return data
530 525
531 def restoreState(self, state, version=0): 526 def restoreState(self, state, version=0):
532 """ 527 """
533 Public method to restore the state of the toolbar manager. 528 Public method to restore the state of the toolbar manager.
534 529
535 @param state byte array containing the saved state (QByteArray) 530 @param state byte array containing the saved state (QByteArray)
536 @param version version number stored with the data (integer) 531 @param version version number stored with the data (integer)
537 @return flag indicating success (boolean) 532 @return flag indicating success (boolean)
538 """ 533 """
539 if state.isEmpty(): 534 if state.isEmpty():
540 return False 535 return False
541 536
542 data = QByteArray(state) 537 data = QByteArray(state)
543 stream = QDataStream(data, QIODevice.OpenModeFlag.ReadOnly) 538 stream = QDataStream(data, QIODevice.OpenModeFlag.ReadOnly)
544 stream.setVersion(QDataStream.Version.Qt_4_6) 539 stream.setVersion(QDataStream.Version.Qt_4_6)
545 marker = stream.readUInt16() 540 marker = stream.readUInt16()
546 vers = stream.readUInt16() 541 vers = stream.readUInt16()
547 if marker != EricToolBarManager.VersionMarker or vers != version: 542 if marker != EricToolBarManager.VersionMarker or vers != version:
548 return False 543 return False
549 544
550 tmarker = stream.readUInt16() 545 tmarker = stream.readUInt16()
551 if tmarker != EricToolBarManager.ToolBarMarker: 546 if tmarker != EricToolBarManager.ToolBarMarker:
552 return False 547 return False
553 548
554 toolBarCount = stream.readUInt16() 549 toolBarCount = stream.readUInt16()
555 for _i in range(toolBarCount): 550 for _i in range(toolBarCount):
556 objectName = Utilities.readStringFromStream(stream) 551 objectName = Utilities.readStringFromStream(stream)
557 actionCount = stream.readUInt16() 552 actionCount = stream.readUInt16()
558 actions = [] 553 actions = []
565 else: 560 else:
566 actions.append(None) 561 actions.append(None)
567 toolBar = self.__findDefaultToolBar(objectName) 562 toolBar = self.__findDefaultToolBar(objectName)
568 if toolBar is not None: 563 if toolBar is not None:
569 self.setToolBar(toolBar, actions) 564 self.setToolBar(toolBar, actions)
570 565
571 cmarker = stream.readUInt16() 566 cmarker = stream.readUInt16()
572 if cmarker != EricToolBarManager.CustomToolBarMarker: 567 if cmarker != EricToolBarManager.CustomToolBarMarker:
573 return False 568 return False
574 569
575 oldCustomToolBars = self.__customToolBars[:] 570 oldCustomToolBars = self.__customToolBars[:]
576 571
577 toolBarCount = stream.readUInt16() 572 toolBarCount = stream.readUInt16()
578 for _i in range(toolBarCount): 573 for _i in range(toolBarCount):
579 objectName = Utilities.readStringFromStream(stream) 574 objectName = Utilities.readStringFromStream(stream)
580 toolBarTitle = Utilities.readStringFromStream(stream) 575 toolBarTitle = Utilities.readStringFromStream(stream)
581 actionCount = stream.readUInt16() 576 actionCount = stream.readUInt16()
595 else: 590 else:
596 toolBar = self.createToolBar(toolBarTitle, objectName) 591 toolBar = self.createToolBar(toolBarTitle, objectName)
597 if toolBar is not None: 592 if toolBar is not None:
598 toolBar.setObjectName(objectName) 593 toolBar.setObjectName(objectName)
599 self.setToolBar(toolBar, actions) 594 self.setToolBar(toolBar, actions)
600 595
601 for tb in oldCustomToolBars: 596 for tb in oldCustomToolBars:
602 self.deleteToolBar(tb) 597 self.deleteToolBar(tb)
603 598
604 return True 599 return True
605 600
606 def toolBarWidgetAction(self, action): 601 def toolBarWidgetAction(self, action):
607 """ 602 """
608 Public method to get the toolbar for a widget action. 603 Public method to get the toolbar for a widget action.
609 604
610 @param action widget action to check for (QAction) 605 @param action widget action to check for (QAction)
611 @return reference to the toolbar containing action (QToolBar) 606 @return reference to the toolbar containing action (QToolBar)
612 """ 607 """
613 aID = id(action) 608 aID = id(action)
614 if aID in self.__widgetActions: 609 if aID in self.__widgetActions:
615 return self.__widgetActions[aID] 610 return self.__widgetActions[aID]
616 return None 611 return None
617 612
618 def removeWidgetActions(self, actions): 613 def removeWidgetActions(self, actions):
619 """ 614 """
620 Public method to remove widget actions. 615 Public method to remove widget actions.
621 616
622 @param actions dictionary with toolbar id as key and 617 @param actions dictionary with toolbar id as key and
623 a list of widget actions as value 618 a list of widget actions as value
624 """ 619 """
625 for tbID in list(actions.keys())[:]: 620 for tbID in list(actions.keys())[:]:
626 toolBar = self.__allToolBars[tbID] 621 toolBar = self.__allToolBars[tbID]
627 newActions = self.__toolBars[tbID][:] 622 newActions = self.__toolBars[tbID][:]
628 newActionsWithSeparators = self.__toolBarsWithSeparators[tbID][:] 623 newActionsWithSeparators = self.__toolBarsWithSeparators[tbID][:]
629 624
630 removedActions = [] 625 removedActions = []
631 for action in actions[tbID]: 626 for action in actions[tbID]:
632 if ( 627 if action in newActions and self.toolBarWidgetAction(action) == toolBar:
633 action in newActions and
634 self.toolBarWidgetAction(action) == toolBar
635 ):
636 newActions.remove(action) 628 newActions.remove(action)
637 newActionsWithSeparators.remove(action) 629 newActionsWithSeparators.remove(action)
638 removedActions.append(action) 630 removedActions.append(action)
639 631
640 self.__toolBars[tbID] = newActions 632 self.__toolBars[tbID] = newActions
641 self.__toolBarsWithSeparators[tbID] = newActionsWithSeparators 633 self.__toolBarsWithSeparators[tbID] = newActionsWithSeparators
642 634
643 for action in removedActions: 635 for action in removedActions:
644 self.__widgetActions[id(action)] = None 636 self.__widgetActions[id(action)] = None
645 self.__actionToToolBars[id(action)].remove(toolBar) 637 self.__actionToToolBars[id(action)].remove(toolBar)
646 toolBar.removeAction(action) 638 toolBar.removeAction(action)
647 639
648 def isWidgetAction(self, action): 640 def isWidgetAction(self, action):
649 """ 641 """
650 Public method to check, if action is a widget action. 642 Public method to check, if action is a widget action.
651 643
652 @param action reference to the action to be checked (QAction) 644 @param action reference to the action to be checked (QAction)
653 @return flag indicating a widget action (boolean) 645 @return flag indicating a widget action (boolean)
654 """ 646 """
655 return id(action) in self.__allWidgetActions 647 return id(action) in self.__allWidgetActions
656 648
657 def categories(self): 649 def categories(self):
658 """ 650 """
659 Public method to get the list of categories. 651 Public method to get the list of categories.
660 652
661 @return list of categories (list of string) 653 @return list of categories (list of string)
662 """ 654 """
663 return list(self.__categoryToActions.keys()) 655 return list(self.__categoryToActions.keys())
664 656
665 def categoryActions(self, category): 657 def categoryActions(self, category):
666 """ 658 """
667 Public method to get the actions belonging to a category. 659 Public method to get the actions belonging to a category.
668 660
669 @param category category for the actions (string) 661 @param category category for the actions (string)
670 @return list of actions (list of QAction) 662 @return list of actions (list of QAction)
671 """ 663 """
672 if category not in self.__categoryToActions: 664 if category not in self.__categoryToActions:
673 return [] 665 return []
674 666
675 return self.__categoryToActions[category][:] 667 return self.__categoryToActions[category][:]
676 668
677 def actionById(self, aID): 669 def actionById(self, aID):
678 """ 670 """
679 Public method to get an action given its id. 671 Public method to get an action given its id.
680 672
681 @param aID id of the action object (integer) 673 @param aID id of the action object (integer)
682 @return reference to the action (QAction) 674 @return reference to the action (QAction)
683 """ 675 """
684 if aID not in self.__allActions: 676 if aID not in self.__allActions:
685 return None 677 return None
686 return self.__allActions[aID] 678 return self.__allActions[aID]
687 679
688 def toolBarById(self, tbID): 680 def toolBarById(self, tbID):
689 """ 681 """
690 Public method to get a toolbar given its id. 682 Public method to get a toolbar given its id.
691 683
692 @param tbID id of the toolbar object (integer) 684 @param tbID id of the toolbar object (integer)
693 @return reference to the toolbar (QToolBar) 685 @return reference to the toolbar (QToolBar)
694 """ 686 """
695 if tbID not in self.__allToolBars: 687 if tbID not in self.__allToolBars:
696 return None 688 return None
697 return self.__allToolBars[tbID] 689 return self.__allToolBars[tbID]
698 690
699 def toolBarActions(self, tbID): 691 def toolBarActions(self, tbID):
700 """ 692 """
701 Public method to get a toolbar's actions given its id. 693 Public method to get a toolbar's actions given its id.
702 694
703 @param tbID id of the toolbar object (integer) 695 @param tbID id of the toolbar object (integer)
704 @return list of actions (list of QAction) 696 @return list of actions (list of QAction)
705 """ 697 """
706 if tbID not in self.__toolBars: 698 if tbID not in self.__toolBars:
707 return [] 699 return []
708 return self.__toolBars[tbID][:] 700 return self.__toolBars[tbID][:]
709 701
710 def toolBarsActions(self): 702 def toolBarsActions(self):
711 """ 703 """
712 Public method to get all toolbars and their actions. 704 Public method to get all toolbars and their actions.
713 705
714 @return reference to dictionary of toolbar IDs as key and list 706 @return reference to dictionary of toolbar IDs as key and list
715 of actions as values 707 of actions as values
716 """ 708 """
717 return self.__toolBars 709 return self.__toolBars
718 710
719 def defaultToolBarActions(self, tbID): 711 def defaultToolBarActions(self, tbID):
720 """ 712 """
721 Public method to get a default toolbar's actions given its id. 713 Public method to get a default toolbar's actions given its id.
722 714
723 @param tbID id of the default toolbar object (integer) 715 @param tbID id of the default toolbar object (integer)
724 @return list of actions (list of QAction) 716 @return list of actions (list of QAction)
725 """ 717 """
726 if tbID not in self.__defaultToolBars: 718 if tbID not in self.__defaultToolBars:
727 return [] 719 return []

eric ide

mercurial