def enforce_dtype(arr, dtype, msg=""): """Force the dtype of a Numpy array.""" if isinstance(arr, np.ndarray): if arr.dtype is not np.dtype(dtype): log_debug("enforcing dtype for array %s %s" % (str(arr.dtype), msg)) return np.array(arr, dtype) return arr
def get_application(): """Get the current QApplication, or create a new one.""" app_created = False app = QtCore.QCoreApplication.instance() if app is None: log_debug("creating a new QApplication in order to show the window") app = QtGui.QApplication(sys.argv) app_created = True return app, app_created
def process_interaction(self, event=None, args=None, do_update=None): """Process user interaction. This method is called after each user action (mouse, keyboard...). It finds the right action associated to the command, then the event associated to that action. Arguments: * event=None: if None, the current event associated to the current user action is retrieved. Otherwise, an event can be directly passed here to force the trigger of any interaction event. * args=None: the arguments of the event if event is not None. """ if event is None: # get current event from current user action event, args = self.get_current_event() if event == 'Animate' and self.block_refresh: return prev_event = self.interaction_manager.prev_event # handle interaction mode change if event == 'SwitchInteractionMode': binding = self.switch_interaction_mode() log_debug("Switching interaction mode to %s." % \ binding.__class__.__name__) # process the interaction event self.interaction_manager.process_event(event, args) # raise a signal if there is one associated to the current event if event in self.events_to_signals: self.events_to_signals[event].emit(*args) # set cursor self.set_current_cursor() # clean current action (unique usage) self.user_action_generator.clean_action() # update the OpenGL view if do_update is None: do_update = ( # (not isinstance(self, GalryTimerWidget)) and (event is not None or prev_event is not None)) if do_update: self.updateGL()
def initialize_companion_classes(self): """Initialize companion classes.""" # default companion classes if not getattr(self, "companion_classes", None): self.set_companion_classes() # create the managers for key, val in self.companion_classes.iteritems(): log_debug("Initializing '%s'" % key) obj = val(self) setattr(self, key, obj) # link all managers for key, val in self.companion_classes.iteritems(): for child_key, child_val in self.companion_classes.iteritems(): # no self-reference if child_key == key: continue obj = getattr(self, key) setattr(obj, child_key, getattr(self, child_key)) self.interaction_manager.constrain_navigation = self.constrain_navigation self.companion_classes_initialized = True
def find_best_size(font="segoe", size=32): """Find the closest font size in the existing font files.""" filename = font + str(size) path = os.path.dirname(os.path.realpath(__file__)) fnt = os.path.join(path, "%s.fnt" % filename) if os.path.exists(fnt): return size else: # we go through all .fnt files files = [file for file in os.listdir(path) if file.startswith(font) \ and file.endswith('.fnt')] files.sort() # we take the largest existing size that is smaller to the requested # size newsize = 0 for file in files: m = re.match(font + "([0-9]+)", file) if m: if int(m.group(1)) > size: break newsize = int(m.group(1)) log_debug("font %s %d does not exist, loading size %d instead" % \ (font, size, newsize)) return newsize