Example #1
0
    def __init__(self, window, widget_prefix):
        self.window = window
        self.model = self.MODEL_CLASS(self)
        self.widget_prefix = widget_prefix + "_"
        self.ui = window
        self.ruleset = window.ruleset
        self.container = None
        self.menu = None
        self.toolbox = None
        self.toolbox_index = None
        self.url_prefix = self.URL_FORMAT[:-2]
        self.drag_format = self.URL_FORMAT
        self.highlight_format = u"highlight:%s" % self.URL_FORMAT
        self.filter = Filter()
        self.create_button = self.getWidget("create_button")
        self.modify_button = self.getWidget("modify_button")
        self.delete_button = self.getWidget("delete_button")
        self.create_grp_button = self.getWidget("creategrp_button", True)
        self.edit_mode = False

        # abstract attributes
        self.dialog = None

        # disable drag/drop on filter
        filter_text = self.getWidget('filter_text')
        filter_text.setAcceptDrops(False)

        index = window.LIBRARIES.index(type(self))
        self.setToolbox(window.object_library, index)
Example #2
0
 def match(self, object):
     if Filter.match(self, object):
         return True
     if ('group' in object) \
     and self.matchGroup(object['group']):
         return True
     return False
Example #3
0
class Library(object):
    REFRESH_DOMAIN = u"object"
    URL_FORMAT = u"object:%s"
    RULESET_ATTRIBUTE = "object"
    ACTIONS = LibraryActions
    GROUP_CLASS = Group
    MODEL_CLASS = LibraryModel

    # Abstract attributes
    CHILD_CLASS = None

    def __init__(self, window, widget_prefix):
        self.window = window
        self.model = self.MODEL_CLASS(self)
        self.widget_prefix = widget_prefix + "_"
        self.ui = window
        self.ruleset = window.ruleset
        self.container = None
        self.menu = None
        self.toolbox = None
        self.toolbox_index = None
        self.url_prefix = self.URL_FORMAT[:-2]
        self.drag_format = self.URL_FORMAT
        self.highlight_format = u"highlight:%s" % self.URL_FORMAT
        self.filter = Filter()
        self.create_button = self.getWidget("create_button")
        self.modify_button = self.getWidget("modify_button")
        self.delete_button = self.getWidget("delete_button")
        self.create_grp_button = self.getWidget("creategrp_button", True)
        self.edit_mode = False

        # abstract attributes
        self.dialog = None

        # disable drag/drop on filter
        filter_text = self.getWidget('filter_text')
        filter_text.setAcceptDrops(False)

        index = window.LIBRARIES.index(type(self))
        self.setToolbox(window.object_library, index)

    def setToolbox(self, toolbox, index):
        self.toolbox = toolbox
        self.toolbox_index = index

    def setButtons(self):
        window = self.window
        window.connect(self.create_button, SIGNAL("clicked()"), self.create)
        window.connect(self.modify_button, SIGNAL("clicked()"), self.modifyEvent)
        window.connect(self.delete_button, SIGNAL("clicked()"), self.deleteEvent)
        if self.create_grp_button:
            window.connect(self.create_grp_button, SIGNAL("clicked()"), self.createGroup)
        window.connect(self.getWidget('filter_text'), SIGNAL('textChanged(const QString&)'), self.filterChanged)

    def getWidget(self, name, use_none=False):
        try:
            return getattr(self.ui, self.widget_prefix + name)
        except AttributeError:
            if use_none:
                return None
            else:
                raise

    def filterChanged(self, text):
        text = unicode(text)
        self.filter.setText(text)
        self.display(Updates())

    def setContainer(self, container):
        self.container = container
        self.container.header().hide()
        container.setDragEnabled(True)
        container.startDrag = self.startDrag
        container.setAlternatingRowColors(True)
        self.window.selection_widgets.append(container)

        self.window.connect(
            container.selectionModel(),
            SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
            self.selectEvent)
        signal = SIGNAL("itemActivated(QTreeWidgetItem*,int)")
        self.window.connect(container, signal, self.modifyEvent)
        container.keyPressEvent = self.keyHandler
        self.selectEvent(None, None)

    def setMenu(self, menu):
        self.menu = menu
        self.container.contextMenuEvent = self.contextMenuEvent

    def keyHandler(self, event):
        if not self.edit_mode \
        and event.key() == Qt.Key_Delete:
            self.deleteEvent()
            event.accept()
            return
        QTreeWidget.keyPressEvent(self.container, event)

    def currentItem(self):
        return self.container.currentItem()

    def currentIdentifier(self):
        item = self.currentItem()
        if not item:
            return None
        if not bool(item.flags() & Qt.ItemIsSelectable):
            return
        text = item.data(0, Qt.UserRole)
        text = unicode(text.toString())
        return text

    def __getitem__(self, identifier):
        return self.model[identifier]

    def __iter__(self):
        return iter(self.model)

    def currentObject(self):
        identifier = self.currentIdentifier()
        if identifier:
            return self[identifier]
        else:
            return None

    def modifyEvent(self, *unused):
        if self.ruleset.read_only or self.edit_mode:
            return
        identifier = self.currentIdentifier()
        if not identifier:
            return
        object = self[identifier]
        if not object.isEditable():
            return
        if isinstance(object, Group):
            self.window.objgroup.editGroup(object, self)
        else:
            self.modify(object)

    def deleteEvent(self):
        identifier = self.currentIdentifier()
        if not identifier:
            return
        object = self[identifier]
        if not object.isEditable():
            return
        if object['references']:
            references = object.createReferencesHTML(glue=', ', icon=False)
            self.window.error(
                tr("Unable to delete the object: it is used by %s") % references,
                escape=False)
            return
        try:
            updates = self.model.delete(object)
        except Exception, err:
            self.window.exception(err)
            return
        self.window.refresh(updates)
        self.window.setInfo('')