def viewSelectionChanged(self, view, context): safecall(self.variableController).detach() self.variableController = None if self.annotationChoiceView is not None: self.annotationChoiceView.anchor = None self.annotationChoiceView = None if self.view.selected: self.variableController = VariableController( self, self.view.transcriptionLabel) self.annotationChoiceView = ChoiceView(itemsPerLine=1, anchor=self.view, anchorPosition='above', controller=self, hotkeysEnabled=False) self.annotationChoiceView.choices = [ 'new agreement relation', 'new constituent', 'new reference label' ] self.annotationChoiceView.getViewForChoice( 'new agreement relation' ).choiceAction = self.createDependencyAnnotation self.annotationChoiceView.getViewForChoice( 'new constituent' ).choiceAction = self.createConstituentAnnotation self.annotationChoiceView.getViewForChoice( 'new reference label').choiceAction = self.createReferenceLabel
def quit(self): # close all windows for window in self.windows: window.close() # quit the application safecall(self.controller).applicationQuitting() self.__app._quit()
def yieldedFirstResponder(self): # stop editing and take over the new value safecall(self.__editController).endEdit() self.__value = self.__editController.value # reset the editing state self.__editController = None self.__cursor = None self.presentationDelegate.endEdit()
def createDependencyAnnotation(self, sender): # create a new dependency relation transaction = transaction = self.representedObject.__db__.transaction() document = self.representedObject.__db__.document with transaction: dependency = document.persistenceSchema.classes.DependencyRelation( self.representedObject, self.representedObject) # start editing the newly created dependency relation depController = self.spanController.getControllerForObject(dependency) safecall(depController).edit('target')
def createConstituentAnnotation(self, sender): # create a new dependency relation transaction = transaction = self.representedObject.__db__.transaction() document = self.representedObject.__db__.document with transaction: constituent = document.persistenceSchema.classes.Constituent() constituent.add(self.representedObject) # start editing the newly created dependency relation conController = self.spanController.getControllerForObject(constituent) safecall(conController).edit()
def __init__(self, **kwargs): self.__window = self.www = GUI_Window(size=(900, 700)) self.__window.windowWrapper = self self.__contents = ScrollView(self, self.__window.scrollview) self.__firstResponder = self self.__defaultFirstResponder = None self.__controller = kwargs.get('controller') if self.__controller is not None: self.__controller.window = self safecall(self.controller).windowLoaded()
def keyDown(self, event): # let the controller handle this if safecall(self.__editController).keyEvent(event) == True: return # move cursor to the left if event.key == 'left_arrow': self.moveCursorLeft() # move the cursor to the right elif event.key == 'right_arrow': self.moveCursorRight() # delete text elif event.key == 'delete': willChange(self, 'presentationState', {}) self.moveCursorToCharacterAtPos( self.__editController.deleteTextAtPosition( self.cursorPosition)) didChange(self, 'presentationState', {}) elif event.key in ('enter', 'return', 'esc'): # resign the first responder status self.window.firstResponder = self.superview # insert text elif event.unicodePoint is not None: willChange(self, 'presentationState', {}) try: self.moveCursorToCharacterAtPos( self.__editController.insertTextAtPosition( event.unicodePoint, self.cursorPosition)) except EditorOperationError: self.operationError() didChange(self, 'presentationState', {})
def firstResponder(self, responder): if responder is None: responder = self # find the item in the hierarchy that is willing to become first responder for responder in getattr(responder, 'responderChain', ()): if responder.canBecomeFirstResponder: break # check if the item is actually willing to become first responder if not responder.canBecomeFirstResponder: return # change the first responder safecall(self.firstResponder).yieldedFirstResponder() self.__firstResponder = responder safecall(self.firstResponder).becameFirstResponder()
def becameFirstResponder(self): self.__cursor = 0 editController = safecall( self.controller).startEditAndGetEditControllerForView(self) if editController is None: editController = StrEditController(self) self.__editController = editController self.presentationDelegate.startEdit()
def moveCursorRight(self): p = self.cursorPosition if p is None: return # guard agains being on the left if p == len(self.value): safecall(self.__editController).cursorMovedAfterText() return # try to find the next grapheme # we do it by moving backwards until the string width changes font = Font.defaultFont value = self.value[0:p] x = font.getWidthForString(value) p = p + 1 while font.getWidthForString(value[0:p]) == x and p < len(value): p = p + 1 self.__cursor = p self.dirty()
def setup_menus(self, menu_config): super(GUI.Application, self).setup_menus(menu_config) for cmd_name, item in self.gui_menu_map.iteritems(): #print "%s.enabled=%s" % (item, item.action.hasResponder) invokable = item.action.canBeInvoked() if not invokable: menu_config[cmd_name].enabled = False else: # get the state of the menu item responder = item.action.getCurrentTarget() state = safecall(responder).getStateForMenuItem(item) if state is None: state = True # set the state accordingly menu_config[cmd_name].enabled = state is not False menu_config[cmd_name].checked = state is 1
def computeLayout(self): """ Arrange the subviews """ # if model is not loaded, basically do nothing if not self.modelLoaded: self.size = (500, 50) self.dirty() self.needsLayout = False return # ============= 1. Arrange the tokens # this happens deterministically, so if the tokens did not change, their # positions will be identical between runs # also, we do not move tokens around! x = self.tokenInitialOffset for tokenView in self.tokenViews: # x = x + tokenView.size.width/2 tokenView.position = (x, 0) x = tokenView.rect.x1 + self.tokenSeparator # ============= 2. Determine the x coordinates and x zones for the nodes # The middle of the node should be at the middle of its x zone nodeXZones = dict() for nodeView in sorted( self.nodeViews, key=lambda v: getattr(v, 'positionPriority', 10)): nodeView.position = ( (min(v.rect.center.x for v in nodeView.dependentViews) + max(v.rect.center.x for v in nodeView.dependentViews) - nodeView.size.width) / 2, 0) x0 = min(min(v.rect.x0 for v in nodeView.dependentViews), nodeView.rect.x0) x1 = max(max(v.rect.x1 for v in nodeView.dependentViews), nodeView.rect.x1) nodeXZones[nodeView] = (x0, x1) # ============= 3. Group the nodes based on their x zones nodeLayoutGroups = self.__groupNodesBasedOnX(nodeXZones) for layoutGroup in nodeLayoutGroups: self.__layoutNodesVertically(layoutGroup, nodeXZones) # ============= 3. Resize the container view to fit all the annotations etc. # place the label views so taht they don't mess stuff up self.headerLabel.position = (15, 0) self.auxiliaryLabel.position = (0, self.headerLabel.size.height + 15) # adjust the node views # we want the highest element be 25 pixels under the header ymin = min(v.rect.y0 for v in self.views) - 25 - self.headerLabel.size.height # move all the view vertical coordinates so that y0 becomes 0 for view in self.viewsWithoutSegments: (x, y) = view.position view.position = (x, y - ymin) ymax = max(v.rect.y1 for v in self.viewsWithoutSegments) # position the labels self.headerLabel.position = (15, 0) self.auxiliaryLabel.position = (self.tokenInitialOffset, ymax) # adjust the size of the span self.size = (max(500, max(v.rect.x1 for v in self.viewsWithoutSegments)), self.auxiliaryLabel.rect.y1 + 25) # ============= 4. Reset all the segments for nodeView in self.nodeViews: safecall(nodeView).setupSegments() for s in self.segments: willChange(view, 'globalRect', {}) willChange(s, 'superview', {}) s._View__superview = self willChange(s, 'superview', {}) didChange(view, 'globalRect', {}) # ============= Layouting is finished self.dirty() self.needsLayout = False
def keyDown(self, event): safecall(self.nextResponder).keyDown(event)
def keyUp(self, event): safecall(self.nextResponder).keyUp(event)
def open_app(self): safecall(self.app.controller).applicationLoaded()
def close_cmd(self): safecall(self.windowWrapper.controller).windowClosing() super(GUI.Window, self).close_cmd() if len(GUI.application().windows) == 0: Application().quit()
def editableActivated(self, sender): safecall(sender.controller).editableActivated(sender)