def show_context_menu(self, pos): """ desc: Shows a context menu for the element. arguments: pos: type: QPoint """ from libqtopensesame._input.popup_menu import popup_menu pm = popup_menu(self.main_window, [ (0, _(u'Edit script'), u'utilities-terminal'), (1, _(u'Raise to front'), u'go-top'), (2, _(u'Lower to bottom'), u'go-bottom'), (3, _(u'Delete'), u'edit-delete'), ]) resp = pm.show() if resp is None: return if resp == 0: self.show_script_edit_dialog() elif resp == 1: self.properties[u'z_index'] = self.sketchpad.min_z_index() - 1 elif resp == 2: self.properties[u'z_index'] = self.sketchpad.max_z_index() + 1 elif resp == 3: self.sketchpad.remove_elements([self]) self.sketchpad.draw()
def contextMenuEvent(self, event): """ desc: Presents a context menu. arguments: event: type: QMouseClickEvent """ item = self.ui.list_pool.itemAt(self.ui.list_pool.mapFromGlobal( event.globalPos())) if item is None: return path = item.path pm = popup_menu(self, [ (0, _(u'Open'), item.icon), (1, _(u'Remove from pool'), u'delete'), (2, _(u'Rename'), u'rename'), ]) action = pm.show() if action == 0: self.open_file(path) elif action == 1: self.delete_selected_files() elif action == 2: self.ui.list_pool.editItem(item)
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u'item-existing', u'item-new']): if e != None: e.ignore() self.main_window.set_busy(False) return # Ignore drops on non-droppable tree items. if target_treeitem == None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e != None: e.ignore() self.main_window.set_busy(False) return # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = unicode(target_treeitem.text(0)) if target_item_name not in self.experiment.items: debug.msg(u'Don\'t know how to drop on %s' % target_item_name) if e != None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return target_item = self.experiment.items[target_item_name] # Get the item to be inserted. If the drop type is item-new, we need # to create a new item, otherwise we get an existin item. Also, if # the drop doesn't originate from this application, we create a new # item. if data[u'type'] == u'item-new' \ or data[u'application-id'] != self.main_window._id(): try: item = self.experiment.items.new(data[u'item-type'], data[u'item-name'], data[u'script']) except Exception as ex: if not isinstance(e, osexception): ex = osexception(msg=u'Plug-in error', exception=ex) self.notify( u'Failed to load plug-in %s (see debug window for stack trace)' \ % data[u'item-type']) self.main_window.print_debug_window(ex) e.accept() self.main_window.set_busy(False) return self.extension_manager.fire(u'new_item', name=data[u'item-name'], _type=data[u'item-type']) else: item = self.experiment.items[data[u'item-name']] inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() == None or \ target_item.name == self.experiment.start: target_item.insert_child_item(item.name) inserted = True else: if target_item.item_type in (u'loop', u'sequence'): self.main_window.set_busy(False) # Choose appropriate option if target_item.item_type == u'loop': question = _('Set as item to run for %s') % target_item.name icon = u'os-loop' else: question = _('Insert into %s') % target_item.name icon = u'os-sequence' resp = popup_menu(self, [(0, question, icon), (1, _('Insert after %s' % target_item.name), 'list-add') ]).show() # If the popup was cancelled if resp == None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem == None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return parent_item_name = unicode(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem)+1 parent_item.insert_child_item(item.name, index=index) if e != None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False)
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] returns: desc: True if the drop was successful, False otherwise. type: bool """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u"item-snippet", u"item-existing"]): if e is not None: e.ignore() self.main_window.set_busy(False) return False if data[u"type"] == u"item-existing" and data[u"item-name"] not in self.experiment.items: self.experiment.notify( _(u'Cannot create linked copy of "%s". Has ' u"the item been permanently deleted?") % data[u"item-name"] ) if e is not None: e.ignore() self.main_window.set_busy(False) return False # Ignore drops on non-droppable tree items. if target_treeitem is None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e is not None: e.ignore() self.main_window.set_busy(False) return False # Accept drops on the unused items bin and unused items (i.e. items # in the bin) if target_treeitem.name == u"__unused__" or ( target_treeitem.parent() is not None and target_treeitem.parent().name == u"__unused__" ): e.accept() self.structure_change.emit() self.main_window.set_busy(False) return True # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = target_treeitem.text(0) if target_item_name not in self.experiment.items: debug.msg(u"Don't know how to drop on %s" % target_item_name) if e is not None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return False target_item = self.experiment.items[target_item_name] if data[u"type"] == u"item-existing": item, new_items = self.drop_get_item_existing(data) else: # Creating an item may fail, and we therefore need to clean up when # this happens. But we don't catch the Exception itself, because it # will be handled with higher up, for example by the bug_report # extension. try: item = None item, new_items = self.drop_get_item_snippet(data) finally: if item is None: if e is not None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) self.end_drag() inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() is None or target_item.name == self.experiment.var.start: target_item.insert_child_item(item.name) inserted = True else: if isinstance(target_item, qtstructure_item): self.main_window.set_busy(False) resp = popup_menu( self, [ (0, _("Insert into %s") % target_item.name, u"go-next"), (1, _("Insert after %s") % target_item.name, u"go-down"), ], ).show() # If the popup was cancelled if resp is None: if e is not None: e.accept() self.main_window.set_busy(False) for item in new_items: del self.experiment.items[item] return False # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem is None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return False parent_item_name = str(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem) + 1 parent_item.insert_child_item(item.name, index=index) if e is not None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False) return True
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] returns: desc: True if the drop was successful, False otherwise. type: bool """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u'item-snippet', u'item-existing']): if e is not None: e.ignore() self.main_window.set_busy(False) return False if data[u'type'] == u'item-existing' and \ data[u'item-name'] not in self.experiment.items: self.experiment.notify( _(u'Cannot create linked copy of "%s". Has ' u'the item been permanently deleted?') % data[u'item-name']) if e is not None: e.ignore() self.main_window.set_busy(False) return False # Ignore drops on non-droppable tree items. if target_treeitem is None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e is not None: e.ignore() self.main_window.set_busy(False) return False # Accept drops on the unused items bin and unused items (i.e. items # in the bin) if target_treeitem.name == u'__unused__' or \ (target_treeitem.parent() is not None and \ target_treeitem.parent().name == u'__unused__'): e.accept() self.structure_change.emit() self.main_window.set_busy(False) return True # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = target_treeitem.text(0) if target_item_name not in self.experiment.items: debug.msg(u'Don\'t know how to drop on %s' % target_item_name) if e is not None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return False target_item = self.experiment.items[target_item_name] if data[u'type'] == u'item-existing': item, new_items = self.drop_get_item_existing(data) else: # Creating an item may fail, and we therefore need to clean up when # this happens. But we don't catch the Exception itself, because it # will be handled with higher up, for example by the bug_report # extension. try: item = None item, new_items = self.drop_get_item_snippet(data) finally: if item is None: if e is not None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) self.end_drag() inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() is None or \ target_item.name == self.experiment.var.start: target_item.insert_child_item(item.name) inserted = True else: if isinstance(target_item, qtstructure_item): self.main_window.set_busy(False) resp = popup_menu(self, [ (0, _('Insert into %s') % target_item.name, u'go-next'), (1, _('Insert after %s') % target_item.name, u'go-down') ]).show() # If the popup was cancelled if resp is None: if e is not None: e.accept() self.main_window.set_busy(False) for item in new_items: del self.experiment.items[item] return False # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem is None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return False parent_item_name = str(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem) + 1 parent_item.insert_child_item(item.name, index=index) if e is not None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False) return True
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] returns: desc: True if the drop was successful, False otherwise. type: bool """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u'item-existing', u'item-new']): if e != None: e.ignore() self.main_window.set_busy(False) return False # Ignore drops on non-droppable tree items. if target_treeitem == None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e != None: e.ignore() self.main_window.set_busy(False) return False # Accept drops on the unused items bin and unused items (i.e. items # in the bin) if target_treeitem.name == u'__unused__' or \ (target_treeitem.parent() is not None and \ target_treeitem.parent().name == u'__unused__'): e.accept() self.structure_change.emit() self.main_window.set_busy(False) return True # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = unicode(target_treeitem.text(0)) if target_item_name not in self.experiment.items: debug.msg(u'Don\'t know how to drop on %s' % target_item_name) if e != None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return False target_item = self.experiment.items[target_item_name] # Get the item to be inserted. If the drop type is item-new, we need # to create a new item, otherwise we get an existin item. Also, if # the drop doesn't originate from this application, we create a new # item. if data[u'type'] == u'item-new' \ or data[u'application-id'] != self.main_window._id(): try: item = self.experiment.items.new(data[u'item-type'], data[u'item-name'], data[u'script']) except Exception as ex: if not isinstance(e, osexception): ex = osexception(msg=u'Plug-in error', exception=ex) self.notify( u'Failed to load plug-in %s (see debug window for stack trace)' \ % data[u'item-type']) self.main_window.print_debug_window(ex) e.accept() self.main_window.set_busy(False) return False self.extension_manager.fire(u'new_item', name=data[u'item-name'], _type=data[u'item-type']) else: item = self.experiment.items[data[u'item-name']] inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() == None or \ target_item.name == self.experiment.start: target_item.insert_child_item(item.name) inserted = True else: if target_item.item_type in (u'loop', u'sequence'): self.main_window.set_busy(False) # Choose appropriate option if target_item.item_type == u'loop': question = _('Set as item to run for %s') % target_item.name icon = u'os-loop' else: question = _('Insert into %s') % target_item.name icon = u'os-sequence' resp = popup_menu(self, [(0, question, icon), (1, _('Insert after %s' % target_item.name), 'list-add') ]).show() # Confirmation if resp == 0 and target_item.item_type == u'loop' and \ target_item.item in self.experiment.items: resp = popup_menu(self, [(0, _(u'I know, do it!'), icon)], title=_(u'This will replace %s' % (target_item.item)) ).show() # If the popup was cancelled if resp == None: e.accept() # Delete the item if it was new or didn't originate from # this application. if data[u'type'] == u'item-new' \ or data[u'application-id'] != self.main_window._id(): del self.experiment.items[item.name] self.main_window.set_busy(False) return False # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem == None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return False parent_item_name = unicode(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem)+1 parent_item.insert_child_item(item.name, index=index) if e != None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False) return True
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] returns: desc: True if the drop was successful, False otherwise. type: bool """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u'item-snippet', u'item-existing']): if e is not None: e.ignore() self.main_window.set_busy(False) return False if data[u'type'] == u'item-existing' and \ data[u'item-name'] not in self.experiment.items: self.experiment.notify(_(u'Cannot create linked copy of "%s". Has ' u'the item been permanently deleted?') % data[u'item-name']) if e is not None: e.ignore() self.main_window.set_busy(False) return False # Ignore drops on non-droppable tree items. if target_treeitem is None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e is not None: e.ignore() self.main_window.set_busy(False) return False # Accept drops on the unused items bin and unused items (i.e. items # in the bin) if target_treeitem.name == u'__unused__' or \ (target_treeitem.parent() is not None and \ target_treeitem.parent().name == u'__unused__'): e.accept() self.structure_change.emit() self.main_window.set_busy(False) return True # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = target_treeitem.text(0) if target_item_name not in self.experiment.items: debug.msg(u'Don\'t know how to drop on %s' % target_item_name) if e is not None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return False target_item = self.experiment.items[target_item_name] if data[u'type'] == u'item-existing': item, new_items = self.drop_get_item_existing(data) else: item, new_items = self.drop_get_item_snippet(data) inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() is None or \ target_item.name == self.experiment.var.start: target_item.insert_child_item(item.name) inserted = True else: if target_item.item_type in (u'loop', u'sequence'): self.main_window.set_busy(False) # Choose appropriate option if target_item.item_type == u'loop': question = _('Set as item to run for %s') % target_item.name icon = u'os-loop' else: question = _('Insert into %s') % target_item.name icon = u'os-sequence' resp = popup_menu(self, [(0, question, icon), (1, _('Insert after %s' % target_item.name), 'list-add') ]).show() # Confirmation if resp == 0 and target_item.item_type == u'loop' and \ target_item.var.item in self.experiment.items: resp = popup_menu(self, [(0, _(u'I know, do it!'), icon)], title=_(u'This will replace %s' % (target_item.item)) ).show() # If the popup was cancelled if resp is None: if e is not None: e.accept() self.main_window.set_busy(False) for item in new_items: del self.experiment.items[item] return False # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem is None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return False parent_item_name = str(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem)+1 parent_item.insert_child_item(item.name, index=index) if e is not None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False) return True
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] returns: desc: True if the drop was successful, False otherwise. type: bool """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u"item-existing", u"item-new"]): if e != None: e.ignore() self.main_window.set_busy(False) return False # Ignore drops on non-droppable tree items. if target_treeitem == None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e != None: e.ignore() self.main_window.set_busy(False) return False # Accept drops on the unused items bin and unused items (i.e. items # in the bin) if target_treeitem.name == u"__unused__" or ( target_treeitem.parent() is not None and target_treeitem.parent().name == u"__unused__" ): e.accept() self.structure_change.emit() self.main_window.set_busy(False) return True # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = unicode(target_treeitem.text(0)) if target_item_name not in self.experiment.items: debug.msg(u"Don't know how to drop on %s" % target_item_name) if e != None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return False target_item = self.experiment.items[target_item_name] # Get the item to be inserted. If the drop type is item-new, we need # to create a new item, otherwise we get an existin item. Also, if # the drop doesn't originate from this application, we create a new # item. if data[u"type"] == u"item-new" or data[u"application-id"] != self.main_window._id(): try: item = self.experiment.items.new(data[u"item-type"], data[u"item-name"], data[u"script"]) except Exception as ex: if not isinstance(e, osexception): ex = osexception(msg=u"Plug-in error", exception=ex) self.notify(u"Failed to load plug-in %s (see debug window for stack trace)" % data[u"item-type"]) self.main_window.print_debug_window(ex) e.accept() self.main_window.set_busy(False) return False self.extension_manager.fire(u"new_item", name=data[u"item-name"], _type=data[u"item-type"]) else: item = self.experiment.items[data[u"item-name"]] inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() == None or target_item.name == self.experiment.start: target_item.insert_child_item(item.name) inserted = True else: if target_item.item_type in (u"loop", u"sequence"): self.main_window.set_busy(False) # Choose appropriate option if target_item.item_type == u"loop": question = _("Set as item to run for %s") % target_item.name icon = u"os-loop" else: question = _("Insert into %s") % target_item.name icon = u"os-sequence" resp = popup_menu( self, [(0, question, icon), (1, _("Insert after %s" % target_item.name), "list-add")] ).show() # Confirmation if resp == 0 and target_item.item_type == u"loop" and target_item.item in self.experiment.items: resp = popup_menu( self, [(0, _(u"I know, do it!"), icon)], title=_(u"This will replace %s" % (target_item.item)) ).show() # If the popup was cancelled if resp == None: e.accept() # Delete the item if it was new or didn't originate from # this application. if data[u"type"] == u"item-new" or data[u"application-id"] != self.main_window._id(): del self.experiment.items[item.name] self.main_window.set_busy(False) return False # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem == None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return False parent_item_name = unicode(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem) + 1 parent_item.insert_child_item(item.name, index=index) if e != None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False) return True
def drop_event_item_new(self, data, e=None, target_treeitem=None): """ desc: Handles drop events for item creation. arguments: data: desc: A drop-data dictionary. type: dict: keywords: e: desc: A drop event or None if a target treeitem is provided. type: [QDropEvent, NoneType] target_treeitem: desc: A target tree item or None in a drop event is specified. type: [tree_base_item, NoneType] returns: desc: True if the drop was successful, False otherwise. type: bool """ self.main_window.set_busy(True) if not drag_and_drop.matches(data, [u'item-snippet', u'item-existing']): if e is not None: e.ignore() self.main_window.set_busy(False) return False # Ignore drops on non-droppable tree items. if target_treeitem is None: target_treeitem = self.itemAt(e.pos()) if not self.droppable(target_treeitem, data): if e is not None: e.ignore() self.main_window.set_busy(False) return False # Accept drops on the unused items bin and unused items (i.e. items # in the bin) if target_treeitem.name == u'__unused__' or \ (target_treeitem.parent() is not None and \ target_treeitem.parent().name == u'__unused__'): e.accept() self.structure_change.emit() self.main_window.set_busy(False) return True # Get the target item, check if it exists, and, if so, drop the source # item on it. target_item_name = target_treeitem.text(0) if target_item_name not in self.experiment.items: debug.msg(u'Don\'t know how to drop on %s' % target_item_name) if e is not None: e.ignore() self.structure_change.emit() self.main_window.set_busy(False) return False target_item = self.experiment.items[target_item_name] if data[u'type'] == u'item-existing': item, new_items = self.drop_get_item_existing(data) else: item, new_items = self.drop_get_item_snippet(data) inserted = False # If the item has no parent or if it is the experiment starting point, # we insert into it directly. if target_treeitem.parent() is None or \ target_item.name == self.experiment.var.start: target_item.insert_child_item(item.name) inserted = True else: if target_item.item_type in (u'loop', u'sequence'): self.main_window.set_busy(False) # Choose appropriate option if target_item.item_type == u'loop': question = _('Set as item to run for %s') % target_item.name icon = u'os-loop' else: question = _('Insert into %s') % target_item.name icon = u'os-sequence' resp = popup_menu(self, [(0, question, icon), (1, _('Insert after %s' % target_item.name), 'list-add') ]).show() # Confirmation if resp == 0 and target_item.item_type == u'loop' and \ target_item.var.item in self.experiment.items: resp = popup_menu(self, [(0, _(u'I know, do it!'), icon)], title=_(u'This will replace %s' % (target_item.item)) ).show() # If the popup was cancelled if resp is None: if e is not None: e.accept() self.main_window.set_busy(False) for item in new_items: del self.experiment.items[item] return False # If the user chose to insert into the target item if resp == 0: target_item.insert_child_item(item.name) inserted = True # Otherwise, we find the parent of the target item, and insert the # new item at the correct position. if not inserted: while True: try: parent_treeitem = target_treeitem.parent() except: # A race condition can occur in which the tree_overview has # been rebuild, thus destroying target_treeitem. If this # happens, we re-take target_treeitem based on the mouse # coordinates. target_treeitem = self.itemAt(e.pos()) parent_treeitem = target_treeitem.parent() if parent_treeitem is None: e.accept() del self.experiment.items[item.name] self.main_window.set_busy(False) return False parent_item_name = str(parent_treeitem.text(0)) parent_item = self.experiment.items[parent_item_name] if isinstance(parent_item, sequence): break target_treeitem = parent_treeitem index = parent_treeitem.indexOfChild(target_treeitem)+1 parent_item.insert_child_item(item.name, index=index) if e is not None: e.accept() self.structure_change.emit() if self.overview_mode: item.open_tab() self.main_window.set_busy(False) return True