Package client :: Module ToolBar
[hide private]
[frames] | no frames]

Source Code for Module client.ToolBar

  1  """ 
  2  Overview 
  3  ======== 
  4  This module adds the toolbar, a quicker and easier way for users to access the functions that they need. 
  5  Currently the toolbar is customizeable trhough the Preferences module.  It is allows the user to add 
  6  any combination of packages to the toolbar.  A package added to the toolbar adds all of the actions 
  7  from that package with it.  While there certainly could be a higher degree of flexibility, allowing 
  8  a user to manually edit whether they want every single menu option on their toolbar would be tedious. 
  9  """ 
 10   
 11  from PyQt4.QtGui  import * 
 12  from PyQt4.QtCore import * 
 13   
 14  import Constants 
 15  import re 
 16   
17 -class ToolBar (QToolBar):
18 """ 19 Overview 20 ======== 21 This class is the Basin Client's ToolBar. It has the ability to add and remove entire packages 22 of actions from it at a time. It also stores these settings into a configuration file, so the 23 same settings are persistent from use to use. Currently, all the packages that exist can be 24 placed into the toolbar. They are as follows: 25 - File 26 - Plotting 27 - General Functions 28 - Stellar Dynamics 29 - Linear Algebra 30 - Statistics 31 - Cosmology 32 - Transformations 33 - Help 34 """ 35
36 - def __init__ (self, parent, bridge):
37 """ 38 Overview 39 ======== 40 The initialization of ToolBar requires the parent 41 object to be a QMainWindow, which it will embed itself in, 42 and the bridge argument is the Bridge object which connects 43 all of the interactive objects together. 44 45 For each individual package of actions, this will take a list 46 of references to each action from the bridge._actions object. 47 This makes adding & deleting a group of actions to the toolbar 48 as easy as iterating over a list. 49 """ 50 51 # Define basic toolbar information 52 QToolBar.__init__ (self, parent) 53 self.setIconSize (QSize (22, 22)) 54 self.setToolButtonStyle (Qt.ToolButtonIconOnly) 55 self.setMovable (True) 56 57 # Set the bridge, and have the bridge set ithe toolbar 58 self._bridge = bridge 59 bridge.setToolBar (self) 60 61 # Define `File` button actions, and initialize them to being unset 62 self._file = False 63 self._fileActions = self._bridge._actions._file 64 65 # Define `Plot` button actions, and initialize them to being unset 66 self._plot = False 67 self._plotActions = self._bridge._actions._plot 68 69 # Define `General Functions` button actions, and initialize them to being unset 70 self._generalfunctions = False 71 self._generalfunctionsActions = self._bridge._actions._genf 72 73 # Define `Stellar Dynamics` button actions, and initialize them to being unset 74 self._stellardynamics = False 75 self._stellardynamicsActions = self._bridge._actions._stel 76 77 # Define `Linear Algebra` button actions, and initialize them to being unset 78 self._linearalgebra = False 79 self._linearalgebraActions = self._bridge._actions._linalg 80 81 # Define `Statistics` button actions, and initialize them to being unset 82 self._statistics = False 83 self._statisticsActions = self._bridge._actions._stat 84 85 # Define `Cosmology` button actions, and initialize them to being unset 86 self._cosmology = False 87 self._cosmologyActions = self._bridge._actions._cosm 88 89 # Define `Transformations` button actions, and initialize them to being unset 90 self._transformation = False 91 self._transformationActions = self._bridge._actions._tran 92 93 # Define `Help` button actions, and initialize them to being unset 94 self._help = False 95 self._helpActions = self._bridge._actions._help 96 97 # Set the types you want, then make this the main window's toolbar 98 parent.addToolBar (self)
99
100 - def categories (self):
101 """ 102 Overview 103 ======== 104 This method is used to get the names of all the categories, along with whether or not 105 they are currently on your ToolBar. This is useful for reporting to the Preferences 106 modules which ones are currently selected. 107 108 @return: This method will return a list of 2 element lists. Each 109 inner 2 element list contains a string name, which is the 110 package of actions, and then a boolean which is True if the 111 package is being displayed in the ToolBar, False if not. 112 """ 113 return [["File", self._file], \ 114 ["Plotting", self._plot], \ 115 ["General Functions", self._generalfunctions], \ 116 ["Cosmology", self._cosmology], \ 117 ["Linear Algebra", self._linearalgebra], \ 118 ["Stellar Dynamics", self._stellardynamics], \ 119 ["Statistics", self._statistics], \ 120 ["Transformations", self._transformation], \ 121 ["Help", self._help]]
122
123 - def actions (self, category):
124 """ 125 Overview 126 ======== 127 This method will return the list of actions under a given 128 package taken from the string argument, category. This 129 allows you to gather all of the actions in one fell swoop 130 when you simply know the name of a package. 131 132 @param category: The name of a package you want to retrieve the actions for. 133 @type category: string 134 """ 135 if category == "File": 136 return self._fileActions 137 elif category == "Plotting": 138 return self._plotActions 139 elif category == "General Functions": 140 return self._generalfunctionsActions 141 elif category == "Cosmology": 142 return self._cosmologyActions 143 elif category == "Linear Algebra": 144 return self._linearalgebraActions 145 elif category == "Stellar Dynamics": 146 return self._stellardynamicsActions 147 elif category == "Statistics": 148 return self._statisticsActions 149 elif category == "Transformations": 150 return self._transformationActions 151 elif category == "Help": 152 return self._helpActions 153 else: 154 return []
155
156 - def clearSettings (self):
157 """ 158 Overview 159 ======== 160 THis method quite simply removes all actions from the toolbar, regardless of anything. This is 161 useful for getting hte toolbar back to a default state, especially to avoid adding a package 162 twice to the ToolBar. 163 """ 164 165 self.remFileActions () 166 self.remPlotActions () 167 self.remGeneralFunctionsActions () 168 self.remStellarDynamicsActions () 169 self.remCosmologyActions () 170 self.remStatisticsActions () 171 self.remTransformationActions () 172 self.remLinearAlgebraActions () 173 self.remHelpActions ()
174
175 - def loadSettings (self):
176 """ 177 Overview 178 ======== 179 When this method is called, the configuration file ~/.basin/client/menubar.conf is loaded 180 and parsed. The configuration file should explicitly set or remove all packages from the 181 toolbar. This will make use of the L{_parseSetting<_parseSetting>} method which actually 182 handles the adding or removal of the toolbar buttons. 183 """ 184 185 self.clearSettings () 186 file = QFile (str (QDir.home ().path ()) + "/.basin/client/menubar.conf") 187 if file.exists (): 188 file.open (QIODevice.ReadOnly | QIODevice.Text) 189 stream = QTextStream (file) 190 regexp = re.compile ("\s*.*\s*:\s*.*\s*") 191 while not stream.atEnd (): 192 line = str (stream.readLine ()) 193 if regexp.match (line): 194 splitLine = line.replace (":", " ").split () 195 if len (splitLine) == 2: 196 self._parseSetting (str (splitLine[0]), str (splitLine[1])) 197 file.close () 198 else: 199 # If the menubar.conf isn't even found, default the file actions to the toolbar 200 self._parseSetting ("FILE", "True")
201
202 - def _parseSetting (self, key, value):
203 """ 204 Overview 205 ======== 206 This method is called from loadSettings so that it can parse the ~/.basin/client/menubar.conf 207 configuration file. The format is quite simple: 208 209 C{PACKAGENAME:True|False} 210 211 This method will end up calling the methods to add or remove the actions from the toolbar. No harm 212 will come from removing a package from the toolbar that isn't on it currently. 213 214 @param key: An all caps representation of a package for Basin. I.e. "FILE", "STELLARDYNAMICS", "HELP"... 215 @type key: string 216 217 @param value: Either "True" for a package that should be drawn to the toolbar, or anything else for false 218 @type value: string 219 """ 220 221 if value != "True": 222 return 223 224 if key == "FILE": 225 self.addFileActions () 226 elif key == "PLOT": 227 self.addPlotActions () 228 elif key == "GENERALFUNCTIONS": 229 self.addGeneralFunctionsActions () 230 elif key == "STELLARDYNAMICS": 231 self.addStellarDynamicsActions () 232 elif key == "STATISTICS": 233 self.addStatisticsActions () 234 elif key == "COSMOLOGY": 235 self.addCosmologyActions () 236 elif key == "LINEARALGEBRA": 237 self.addLinearAlgebraActions () 238 elif key == "TRANSFORMATIONS": 239 self.addTransformationActions () 240 elif key == "HELP": 241 self.addHelpActions ()
242
243 - def addFileActions (self):
244 """ 245 Overview 246 ======== 247 Adds all of the I{File} actions from the toolbar. 248 Actions may be added multiple times, though this would never be useful. 249 """ 250 251 for action in self._fileActions: 252 self.addAction (action) 253 self._file = True
254
255 - def remFileActions (self):
256 """ 257 Overview 258 ======== 259 Removes all of the I{File} actions from the toolbar. 260 There is no harm in attempting to remove a package not currently on the toolbar. 261 """ 262 263 for action in self._fileActions: 264 self.removeAction (action) 265 self._file = False
266
267 - def addPlotActions (self):
268 """ 269 Overview 270 ======== 271 Adds all of the I{Plot} actions from the toolbar. 272 Actions may be added multiple times, though this would never be useful. 273 """ 274 275 for action in self._plotActions: 276 self.addAction (action) 277 self._plot = True
278
279 - def remPlotActions (self):
280 """ 281 Overview 282 ======== 283 Removes all of the I{Plot} actions from the toolbar. 284 There is no harm in attempting to remove a package not currently on the toolbar. 285 """ 286 287 for action in self._plotActions: 288 self.removeAction (action) 289 self._plot = False
290
291 - def addCosmologyActions (self):
292 """ 293 Overview 294 ======== 295 Adds all of the I{Cosmology} actions from the toolbar. 296 Actions may be added multiple times, though this would never be useful. 297 """ 298 299 for action in self._cosmologyActions: 300 self.addAction (action) 301 self._cosmology = True
302
303 - def remCosmologyActions (self):
304 """ 305 Overview 306 ======== 307 Removes all of the I{Cosmology} actions from the toolbar. 308 There is no harm in attempting to remove a package not currently on the toolbar. 309 """ 310 311 for action in self._cosmologyActions: 312 self.removeAction (action) 313 self._cosmology = False
314
315 - def addTransformationActions (self):
316 """ 317 Overview 318 ======== 319 Adds all of the I{Transformation} actions from the toolbar. 320 Actions may be added multiple times, though this would never be useful. 321 """ 322 323 for action in self._transformationActions: 324 self.addAction (action) 325 self._transformation = True
326
327 - def remTransformationActions (self):
328 """ 329 Overview 330 ======== 331 Removes all of the I{Transformation} actions from the toolbar. 332 There is no harm in attempting to remove a package not currently on the toolbar. 333 """ 334 335 for action in self._transformationActions: 336 self.removeAction (action) 337 self._transformation = False
338
339 - def addStellarDynamicsActions (self):
340 """ 341 Overview 342 ======== 343 Adds all of the I{Stellar Dynamics} actions from the toolbar. 344 Actions may be added multiple times, though this would never be useful. 345 """ 346 347 for action in self._stellardynamicsActions: 348 self.addAction (action) 349 self._stellardynamics = True
350
351 - def remStellarDynamicsActions (self):
352 """ 353 Overview 354 ======== 355 Removes all of the I{Stellar Dynamics} actions from the toolbar. 356 There is no harm in attempting to remove a package not currently on the toolbar. 357 """ 358 359 for action in self._stellardynamicsActions: 360 self.removeAction (action) 361 self._stellardynamics = False
362
363 - def addStatisticsActions (self):
364 """ 365 Overview 366 ======== 367 Adds all of the I{Statistics} actions from the toolbar. 368 Actions may be added multiple times, though this would never be useful. 369 """ 370 371 for action in self._statisticsActions: 372 self.addAction (action) 373 self._statistics = True
374
375 - def remStatisticsActions (self):
376 """ 377 Overview 378 ======== 379 Removes all of the I{Statistics} actions from the toolbar. 380 There is no harm in attempting to remove a package not currently on the toolbar. 381 """ 382 383 for action in self._statisticsActions: 384 self.removeAction (action) 385 self._statistics = False
386
387 - def addGeneralFunctionsActions (self):
388 """ 389 Overview 390 ======== 391 Adds all of the I{General Functions} actions from the toolbar. 392 Actions may be added multiple times, though this would never be useful. 393 """ 394 395 for action in self._generalfunctionsActions: 396 self.addAction (action) 397 self._generalfunctions = True
398
399 - def remGeneralFunctionsActions (self):
400 """ 401 Overview 402 ======== 403 Removes all of the I{General Functions} actions from the toolbar. 404 There is no harm in attempting to remove a package not currently on the toolbar. 405 """ 406 407 for action in self._generalfunctionsActions: 408 self.removeAction (action) 409 self._generalfunctions = False
410
411 - def addLinearAlgebraActions (self):
412 """ 413 Overview 414 ======== 415 Adds all of the I{Linear Algebra} actions from the toolbar. 416 Actions may be added multiple times, though this would never be useful. 417 """ 418 419 for action in self._linearalgebraActions: 420 self.addAction (action) 421 self._linearalgebra = True
422
423 - def remLinearAlgebraActions (self):
424 """ 425 Overview 426 ======== 427 Removes all of the I{Linear Algebra} actions from the toolbar. 428 There is no harm in attempting to remove a package not currently on the toolbar. 429 """ 430 431 for action in self._linearalgebraActions: 432 self.removeAction (action) 433 self._linearalgebra = False
434
435 - def addHelpActions (self):
436 """ 437 Overview 438 ======== 439 Adds all of the I{Help} actions from the toolbar. 440 Actions may be added multiple times, though this would never be useful. 441 """ 442 443 for action in self._helpActions: 444 self.addAction (action) 445 self._help = True
446
447 - def remHelpActions (self):
448 """ 449 Overview 450 ======== 451 Removes all of the I{Help} actions from the toolbar. 452 There is no harm in attempting to remove a package not currently on the toolbar. 453 """ 454 455 for action in self._helpActions: 456 self.removeAction (action) 457 self._help = False
458