Example #1
0
    def addLabelClass(self, label_config):
        """
        添加标签
        :param label_config: 标签的json
        """
        # Check label configuration
        if 'attributes' not in label_config:
            raise ImproperlyConfigured("Label with no 'attributes' dict found")
        attrs = label_config['attributes']
        if 'class' not in attrs:
            raise ImproperlyConfigured("Labels must have an attribute 'class'")
        label_class = attrs['class']
        if label_class in self._class_config:
            raise ImproperlyConfigured(
                "Label with class '%s' defined more than once" % label_class)

        # Store config
        self._class_config[label_class] = label_config

        # Parse configuration and create handlers and item
        self.parseConfiguration(label_class, label_config)

        # Add label class button
        button_text = label_config['text']
        button = QPushButton(button_text)
        button.setCheckable(True)
        button.setFlat(True)
        button.clicked.connect(bind(self.onClassButtonPressed, label_class))
        self._class_buttons[label_class] = button
        self._parea.setGeometry(0, 0, 200,
                                self._parea.geometry().height() + 40)
        self._classbox_layout.addWidget(button)

        # 添加右键菜单
        self.label_menu[label_class] = QtGui.QMenu(self)
        self.label_action[label_class] = self.label_menu[
            label_class].addAction('删除')
        self.label_action[label_class].triggered.connect(
            bind(self.remove_item, label_class))
        self._class_buttons[label_class].setContextMenuPolicy(
            QtCore.Qt.CustomContextMenu)
        self._class_buttons[label_class].customContextMenuRequested.connect(
            bind(self.showContextMenu, label_class))

        # Add hotkey
        if 'hotkey' in label_config:
            # 快捷键
            hotkey = label_config['hotkey']
            # 快捷键已经存在,那就去掉原来的
            if hotkey in self.shortcut2button and self.shortcut2button[
                    hotkey] is not None:
                self.shortcut2button[hotkey].setShortcut(QKeySequence())
            # 设置快捷键
            button.setShortcut(QKeySequence(hotkey))
            self.shortcut2button[hotkey] = button
            self.label2shortcut[label_class] = hotkey
Example #2
0
    def addLabelClass(self, label_config):
        # Check label configuration
        if 'attributes' not in label_config:
            raise ImproperlyConfigured("Label with no 'attributes' dict found")
        attrs = label_config['attributes']
        if 'class' not in attrs:
            raise ImproperlyConfigured("Labels must have an attribute 'class'")
        label_class = attrs['class']
        if label_class in self._class_config:
            raise ImproperlyConfigured(
                "Label with class '%s' defined more than once" % label_class)

        # Store config
        self._class_config[label_class] = label_config

        # Parse configuration and create handlers and item
        self.parseConfiguration(label_class, label_config)

        # Add label class button
        button_text = label_config['text']
        button = QPushButton(button_text, self)
        button.setCheckable(True)
        button.setFlat(True)
        button.clicked.connect(bind(self.onClassButtonPressed, label_class))
        self._class_buttons[label_class] = button
        self._classbox_layout.addWidget(button)

        # Add hotkey
        if 'hotkey' in label_config:
            hotkey = QShortcut(QKeySequence(label_config['hotkey']), self)
            hotkey.activated.connect(button.click)
            self._class_shortcuts[label_class] = hotkey
Example #3
0
    def addLabelClass(self, label_config):
        # Check label configuration
        if 'attributes' not in label_config:
            raise ImproperlyConfigured("Label with no 'attributes' dict found")
        attrs = label_config['attributes']
        if 'class' not in attrs:
            raise ImproperlyConfigured("Labels must have an attribute 'class'")
        label_class = attrs['class']
        if label_class in self._class_config:
            raise ImproperlyConfigured("Label with class '%s' defined more than once" % label_class)

        # Store config
        self._class_config[label_class] = label_config

        # Parse configuration and create handlers and item
        self.parseConfiguration(label_class, label_config)

        # Add label class button
        button = QPushButton(label_class, self)
        button.setCheckable(True)
        button.setFlat(True)
        button.clicked.connect(bind(self.onClassButtonPressed, label_class))
        self._class_buttons[label_class] = button
        self._classbox_layout.addWidget(button)

        # Add hotkey
        if 'hotkey' in label_config:
            hotkey = QShortcut(QKeySequence(label_config['hotkey']), self)
            hotkey.activated.connect(button.click)
            self._class_shortcuts[label_class] = hotkey
Example #4
0
    def initShortcuts(self, HOTKEYS):
        self.shortcuts = []

        for hotkey in HOTKEYS:
            assert len(hotkey) >= 2
            key = hotkey[0]
            fun = hotkey[1]
            desc = ""
            if len(hotkey) > 2:
                desc = hotkey[2]
            if type(fun) == str:
                fun = import_callable(fun)

            hk = QAction(desc, self)
            hk.setShortcut(QKeySequence(key))
            hk.setEnabled(True)
            if hasattr(fun, '__call__'):
                hk.triggered.connect(bind(fun, self.labeltool))
            else:
                hk.triggered.connect(compose_noargs([bind(f, self.labeltool) for f in fun]))
            self.ui.menuShortcuts.addAction(hk)
            self.shortcuts.append(hk)
Example #5
0
    def initShortcuts(self, HOTKEYS):
        self.shortcuts = []

        for hotkey in HOTKEYS:
            assert len(hotkey) >= 2
            key = hotkey[0]
            fun = hotkey[1]
            desc = ""
            if len(hotkey) > 2:
                desc = hotkey[2]
            if type(fun) == str:
                fun = import_callable(fun)

            hk = QAction(desc, self)
            hk.setShortcut(QKeySequence(key))
            hk.setEnabled(True)
            if hasattr(fun, '__call__'):
                hk.triggered.connect(bind(fun, self.labeltool))
            else:
                hk.triggered.connect(compose_noargs([bind(f, self.labeltool) for f in fun]))
            self.ui.menuShortcuts.addAction(hk)
            self.shortcuts.append(hk)
Example #6
0
 def addShortcut(self, shortcut, widget, value):
     if widget is not None:
         if shortcut not in self._shortcuts:
             sc = QShortcut(QKeySequence(shortcut), self)
             self._shortcuts[shortcut] = sc
             if isinstance(widget, QPushButton):
                 sc.activated.connect(bind(lambda w: w.click() if not w.isChecked() else None, widget))
             elif isinstance(widget, QLineEdit):
                 sc.activated.connect(self.focusInputField)
         else:
             raise ImproperlyConfigured("Shortcut '%s' defined more than once" % shortcut)
     else:
         raise ImproperlyConfigured("Shortcut '%s' defined for value '%s' which is hidden" % (shortcut, value))
Example #7
0
 def addValue(self, v, autoAddValue=False):
     if v in self._buttons: return
     if autoAddValue and self._insertIndex < 0: return
     button = QPushButton(v, self)
     button.setFlat(True)
     button.setCheckable(True)
     self._buttons[v] = button
     if autoAddValue:
         self._layout.insertWidget(self._insertIndex, button)
         if self._insertAtEnd:
             self._insertIndex += 1
     else:
         self._layout.addWidget(button)
     button.clicked.connect(bind(self.onButtonClicked, v))
Example #8
0
 def addValue(self, v, autoAddValue=False):
     if v in self._buttons: return
     if autoAddValue and self._insertIndex < 0: return
     button = QPushButton(v, self)
     button.setFlat(True)
     button.setCheckable(True)
     self._buttons[v] = button
     if autoAddValue:
         self._layout.insertWidget(self._insertIndex, button)
         if self._insertAtEnd:
             self._insertIndex += 1
     else:
         self._layout.addWidget(button)
     button.clicked.connect(bind(self.onButtonClicked, v))
Example #9
0
    def addButton(self, buttonConfig):
        # LOG.info("addLabelClass with buttonConfig {} ...".format(buttonConfig))
        # Check label configuration
        if 'attributes' not in buttonConfig:
            raise ImproperlyConfigured("Label with no 'attributes' dict found")

        inserter_creator_method = buttonConfig['inserter']
        inserted_item_creator_method = buttonConfig['item']

        attrs = buttonConfig['attributes']
        # LOG.info("buttonConfig['attributes'] {} type {} ...".format(buttonConfig['attributes'], type(buttonConfig['attributes'])))
        if config.METADATA_LABELCLASS_TOKEN not in attrs:
            raise ImproperlyConfigured(
                "Labels must have an attribute config.METADATA_LABELCLASS_TOKEN"
            )
        label_class = attrs[config.METADATA_LABELCLASS_TOKEN]
        # LOG.info("buttonConfig['attributes'][config.METADATA_LABELCLASS_TOKEN] {} type {} ...".format(attrs[config.METADATA_LABELCLASS_TOKEN], type(attrs[config.METADATA_LABELCLASS_TOKEN])))
        if label_class in self._inserters_modelitems:
            raise ImproperlyConfigured(
                "Label with class '%s' defined more than once" % label_class)

        # Add INSERTER button
        displaytext = attrs['displaytext']
        buttonName = label_class
        button = QPushButton(displaytext, self)

        optionInfo = attrs.get('optioninfo', None)
        # print "button {}: option {}".format(buttonName, optionInfo)
        buttonOptionsWidget = None
        buttonDisplayColor = None
        tmp = [
            o.get(default_config.METADATA_DISPLAYCOLOR_TOKEN, None)
            for o in optionInfo['option']
            if o.get(config.METADATA_IS_DEFAULT_TOKEN, False)
        ][0] if optionInfo else None
        buttonDisplayColor = tmp if tmp else optionInfo['option'][0].get(
            default_config.METADATA_DISPLAYCOLOR_TOKEN,
            None) if optionInfo else attrs.get(
                default_config.METADATA_DISPLAYCOLOR_TOKEN, None)

        # LOG.info(u"buttonConfig['attributes'] = {}, displaytext = {}, displayColor = {}".format(attrs, displaytext, buttonDisplayColor))

        # ==== zx add @ 20161114 to display button with color configured by user ====
        txtColor = None
        if buttonDisplayColor is not None:
            qtColor, hexColor = utils.getColorDesc(buttonDisplayColor)
            rgba = utils.hexColorStrToRGBA(hexColor)
            distance = math.sqrt((rgba[0] - 255)**2 + (rgba[1] - 255)**2 +
                                 (rgba[2] - 255)**2)
            txtColor = '#000000' if distance > config.GUI_COLOR_TAG_TEXT_BLACKWHITE_TOGGLE_THRESHOLD else '#ffffff'
            buttonDisplayColor = hexColor[0:8]
            # LOG.info(u"buttonDisplayColor = {} txtColor = {}, qtColor = {} hexColor = {}".format(buttonDisplayColor, txtColor, qtColor, hexColor ))
            # print (u"buttonDisplayColor = {} txtColor = {}, qtColor = {} hexColor = {}".format(buttonDisplayColor, txtColor, qtColor, hexColor ))

        # print "button {} buttonDisplayColor {} ...".format(buttonName, buttonDisplayColor)
        utils.set_qobj_stylesheet(
            button,
            'QPushButton',
            widgetBackgroundColor=None,
            widgetTextColor=None,
            widgetBackgroundColorWhenChecked=buttonDisplayColor,
            widgetTextColorWhenChecked=txtColor)
        # ========================== zx add end ============================

        button.clicked.connect(bind(self.onClassButtonPressed, label_class))
        # Add hotkey
        if 'hotkey' in buttonConfig:
            hotkey = QShortcut(QKeySequence(buttonConfig['hotkey']), self)
            hotkey.activated.connect(button.click)
            self._class_shortcuts[label_class] = hotkey
            # print "{} is set hotkey {} {}".format(label_class, buttonConfig['hotkey'], hotkey)

        if optionInfo:
            optionListName = optionInfo['name']
            optionListText = optionInfo['displaytext']
            option = optionInfo['option']
            buttonOptionsWidget = AttachedCheckboxGroupWidget(
                buttonName,
                optionListName,
                optionListText,
                True,
                option,
                self._optionsCheckboxStateChangedSignal,
                parent=None)

            isDefaultOption = False
            for o in option:
                new_class = o.get('tag', None)
                if new_class:
                    # Add prototype mdoelItem for insertion
                    mi = {config.METADATA_LABELCLASS_TOKEN: new_class}
                    mi['displaytext'] = o.get('displaytext', new_class)

                    self._inserters_modelitems[
                        new_class] = AnnotationModelItem(mi)
                    self._inserters_guiitems[new_class] = (
                        inserter_creator_method, inserted_item_creator_method)
                    # print "addButton.....self._inserters_guiitems[{}] = {}".format(new_class, (inserter_creator_method, inserted_item_creator_method))
                    for key, val in o.iteritems():
                        if key != 'tag':
                            self._inserters_modelitems[new_class][key] = val

        else:
            attrs = buttonConfig['attributes']

            # Add prototype mdoelItem for insertion
            mi = {config.METADATA_LABELCLASS_TOKEN: label_class}
            mi['displaytext'] = attrs.get('displaytext', label_class)

            self._inserters_modelitems[label_class] = AnnotationModelItem(mi)
            self._inserters_guiitems[label_class] = (
                inserter_creator_method, inserted_item_creator_method)

            # update their values
            for key, val in attrs.iteritems():
                self._inserters_modelitems[label_class][key] = val
                # LOG.info("self._inserters_modelitems[{}][{}] = {}".format(label_class, key, val))

        self._widgets_dict[label_class] = [button, buttonOptionsWidget]
        # print "self._widgets_dict [ {} ] = {}".format(label_class, button)

        button.setCheckable(True)

        utils.set_qobj_stylesheet(
            self._widgets_dict[label_class][0],
            'QPushButton',
            widgetBackgroundColor=None,
            widgetTextColor=None,
            widgetBackgroundColorWhenChecked=buttonDisplayColor,
            widgetTextColorWhenChecked=txtColor)

        if buttonOptionsWidget:
            # self._layout.addWidget(button)
            self._inserterButtonGroup_layout.addWidget(button)
            self._layout.addWidget(buttonOptionsWidget)
        else:
            self._inserterButtonGroup_layout.addWidget(button)