Beispiel #1
0
    def fetchResults(self, string, context, feedback):
        # collect results in main thread, since this method is inexpensive and
        # accessing the processing registry is not thread safe
        for a in QgsApplication.processingRegistry().algorithms():
            if a.flags() & QgsProcessingAlgorithm.FlagHideFromToolbox:
                continue
            if not ProcessingConfig.getSetting(ProcessingConfig.SHOW_ALGORITHMS_KNOWN_ISSUES) and \
                    a.flags() & QgsProcessingAlgorithm.FlagKnownIssues:
                continue

            result = QgsLocatorResult()
            result.filter = self
            result.displayString = a.displayName()
            result.icon = a.icon()
            result.userData = a.id()
            result.score = 0

            if (context.usingPrefix and not string):
                self.resultFetched.emit(result)

            string = string.lower()
            tagScore = 0
            tags = [*a.tags(), a.provider().name(), a.group()]

            for t in tags:
                if string in t.lower():
                    tagScore = 1
                    break

            result.score = QgsStringUtils.fuzzyScore(
                result.displayString, string) * 0.5 + tagScore * 0.5

            if result.score > 0:
                self.resultFetched.emit(result)
    def fetchResults(self, string, context, feedback):
        # collect results in main thread, since this method is inexpensive and
        # accessing the processing registry/current layer is not thread safe

        if iface.activeLayer() is None or iface.activeLayer().type(
        ) != QgsMapLayerType.VectorLayer:
            return

        for a in QgsApplication.processingRegistry().algorithms():
            if not a.flags() & QgsProcessingAlgorithm.FlagSupportsInPlaceEdits:
                continue

            if not a.supportInPlaceEdit(iface.activeLayer()):
                continue

            result = QgsLocatorResult()
            result.filter = self
            result.displayString = a.displayName()
            result.icon = a.icon()
            result.userData = a.id()
            result.score = 0

            if (context.usingPrefix and not string):
                self.resultFetched.emit(result)

            if not string:
                return

            string = string.lower()
            tagScore = 0
            tags = [*a.tags(), a.provider().name()]
            if a.group():
                tags.append(a.group())

            for t in tags:
                if string in t.lower():
                    tagScore = 1
                    break

            result.score = QgsStringUtils.fuzzyScore(
                result.displayString, string) * 0.5 + tagScore * 0.5

            if result.score > 0:
                self.resultFetched.emit(result)
Beispiel #3
0
 def testfuzzyScore(self):
     self.assertEqual(QgsStringUtils.fuzzyScore('', ''), 0)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo', ''), 0)
     self.assertEqual(QgsStringUtils.fuzzyScore('', 'foo'), 0)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo', 'foo'), 1)
     self.assertEqual(QgsStringUtils.fuzzyScore('bar', 'foo'), 0)
     self.assertEqual(QgsStringUtils.fuzzyScore('FOO', 'foo'), 1)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo', 'FOO'), 1)
     self.assertEqual(QgsStringUtils.fuzzyScore('   foo   ', 'foo'), 1)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo', '   foo   '), 1)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo', '   foo   '), 1)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo_bar', 'foo bar'), 1)
     self.assertGreater(QgsStringUtils.fuzzyScore('foo bar', 'foo'), 0)
     self.assertGreater(QgsStringUtils.fuzzyScore('foo bar', 'fooba'), 0)
     self.assertGreater(QgsStringUtils.fuzzyScore('foo_bar', 'ob'), 0)
     self.assertGreater(QgsStringUtils.fuzzyScore('foo bar', 'foobar'), 0)
     self.assertGreater(QgsStringUtils.fuzzyScore('foo bar', 'foo_bar'), 0)
     self.assertGreater(QgsStringUtils.fuzzyScore('foo_bar', 'foo bar'), 0)
     self.assertEqual(QgsStringUtils.fuzzyScore('foo bar', 'foobar'),
                      QgsStringUtils.fuzzyScore('foo_bar', 'foobar'))
     self.assertEqual(QgsStringUtils.fuzzyScore('foo bar', 'foo_bar'),
                      QgsStringUtils.fuzzyScore('foo_bar', 'foo_bar'))
     self.assertEqual(QgsStringUtils.fuzzyScore('foo bar', 'foo bar'),
                      QgsStringUtils.fuzzyScore('foo_bar', 'foo bar'))
     # note the accent
     self.assertEqual(QgsStringUtils.fuzzyScore('foo_bér', 'foober'),
                      QgsStringUtils.fuzzyScore('foo_ber', 'foobér'))
     self.assertGreater(
         QgsStringUtils.fuzzyScore('abcd efg hig', 'abcd hig'),
         QgsStringUtils.fuzzyScore('abcd efg hig', 'abcd e h'))
     #  full words are preferred, even though the same number of characters used
     self.assertGreater(
         QgsStringUtils.fuzzyScore('abcd efg hig', 'abcd hig'),
         QgsStringUtils.fuzzyScore('abcd efg hig', 'abcd e hi'))