当前位置: 首页>>代码示例>>Python>>正文


Python QCompleter.setCompletionPrefix方法代码示例

本文整理汇总了Python中PySide.QtGui.QCompleter.setCompletionPrefix方法的典型用法代码示例。如果您正苦于以下问题:Python QCompleter.setCompletionPrefix方法的具体用法?Python QCompleter.setCompletionPrefix怎么用?Python QCompleter.setCompletionPrefix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PySide.QtGui.QCompleter的用法示例。


在下文中一共展示了QCompleter.setCompletionPrefix方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Completer

# 需要导入模块: from PySide.QtGui import QCompleter [as 别名]
# 或者: from PySide.QtGui.QCompleter import setCompletionPrefix [as 别名]
class Completer(QGraphicsProxyWidget, object):
    ''' Class for handling text autocompletion in the SDL scene '''
    def __init__(self, parent):
        ''' Create an autocompletion list popup '''
        widget = QListWidget()
        super(Completer, self).__init__(parent)
        self.setWidget(widget)
        self.string_list = QStringListModel()
        self._completer = QCompleter()
        self.parent = parent
        self._completer.setCaseSensitivity(Qt.CaseInsensitive)
        # For some reason the default minimum size is (61,61)
        # Set it to 0 so that the size of the box is not taken
        # into account when it is hidden.
        self.setMinimumSize(0, 0)
        self.prepareGeometryChange()
        self.resize(0, 0)
        self.hide()

    def set_completer_list(self):
        ''' Set list of items for the autocompleter popup '''
        compl = [item.replace('-', '_') for item in
                 self.parent.parentItem().completion_list]
        self.string_list.setStringList(compl)
        self._completer.setModel(self.string_list)

    def set_completion_prefix(self, completion_prefix):
        '''
            Set the current completion prefix (user-entered text)
            and set the corresponding list of words in the popup widget
        '''
        self._completer.setCompletionPrefix(completion_prefix)
        self.widget().clear()
        count = self._completer.completionCount()
        for i in xrange(count):
            self._completer.setCurrentRow(i)
            self.widget().addItem(self._completer.currentCompletion())
        self.prepareGeometryChange()
        if count:
            self.resize(self.widget().sizeHintForColumn(0) + 40, 70)
        else:
            self.resize(0, 0)
        return count

    # pylint: disable=C0103
    def keyPressEvent(self, e):
        super(Completer, self).keyPressEvent(e)
        if e.key() == Qt.Key_Escape:
            self.parentItem().setFocus()
        # Consume the event so that it is not repeated at EditableText level
        e.accept()

    # pylint: disable=C0103
    def focusOutEvent(self, event):
        ''' When the user leaves the popup, return focus to parent '''
        super(Completer, self).focusOutEvent(event)
        self.hide()
        self.resize(0, 0)
        self.parentItem().setFocus()
开发者ID:Kampbell,项目名称:opengeode,代码行数:61,代码来源:TextInteraction.py

示例2: CommandBox

# 需要导入模块: from PySide.QtGui import QCompleter [as 别名]
# 或者: from PySide.QtGui.QCompleter import setCompletionPrefix [as 别名]
class CommandBox(QtGui.QPlainTextEdit, object):
    newCommand = QtCore.Signal(str)

    def reset_history(self):
        self.history_index = len(self.history)

    def __init__(self, history, commands):
        self.history_index = 0
        self.history = history
        self.reset_history()
        super(CommandBox, self).__init__()

        #Autocompleter
        self.completer = QCompleter([BOT_PREFIX + name for name in commands], self)
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setWidget(self)
        self.completer.activated.connect(self.onAutoComplete)
        self.autocompleteStart = None

    def onAutoComplete(self, text):
        #Select the text from autocompleteStart until the current cursor
        cursor = self.textCursor()
        cursor.setPosition(0, cursor.KeepAnchor)
        #Replace it with the selected text
        cursor.insertText(text)
        self.autocompleteStart = None

    #noinspection PyStringFormat
    def keyPressEvent(self, *args, **kwargs):
        event = args[0]
        key = event.key()
        ctrl = event.modifiers() == QtCore.Qt.ControlModifier

        # don't disturb the completer behavior
        if self.completer.popup().isVisible() and key in (Qt.Key_Enter, Qt.Key_Return, Qt.Key_Tab, Qt.Key_Backtab):
            event.ignore()
            return

        if self.autocompleteStart is not None and not event.text().isalnum() and \
                not (key == Qt.Key_Backspace and self.textCursor().position() > self.autocompleteStart):
            self.completer.popup().hide()
            self.autocompleteStart = None

        if key == Qt.Key_Space and ctrl:
            #Pop-up the autocompleteList
            rect = self.cursorRect(self.textCursor())
            rect.setSize(QtCore.QSize(100, 150))
            self.autocompleteStart = self.textCursor().position()
            self.completer.complete(rect)  # The popup is positioned in the next if block

        if self.autocompleteStart:
            prefix = self.toPlainText()
            cur = self.textCursor()
            cur.setPosition(self.autocompleteStart)

            self.completer.setCompletionPrefix(prefix)
            #Select the first one of the matches
            self.completer.popup().setCurrentIndex(self.completer.completionModel().index(0, 0))

        if key == Qt.Key_Up and ctrl:
            if self.history_index > 0:
                self.history_index -= 1
                self.setPlainText(BOT_PREFIX + '%s %s' % self.history[self.history_index])
                key.ignore()
                return
        elif key == Qt.Key_Down and ctrl:
            if self.history_index < len(self.history) - 1:
                self.history_index += 1
                self.setPlainText(BOT_PREFIX + '%s %s' % self.history[self.history_index])
                key.ignore()
                return
        elif key == QtCore.Qt.Key_Return and ctrl:
            self.newCommand.emit(self.toPlainText())
            self.reset_history()
        super(CommandBox, self).keyPressEvent(*args, **kwargs)
开发者ID:linuxtechie,项目名称:err,代码行数:77,代码来源:graphic.py

示例3: CodeCompletionMode

# 需要导入模块: from PySide.QtGui import QCompleter [as 别名]
# 或者: from PySide.QtGui.QCompleter import setCompletionPrefix [as 别名]

#.........这里部分代码省略.........
        # build the completion model
        cc_model = QStandardItemModel()
        cptSuggestion = 0
        displayedTexts = []
        self.__tooltips.clear()
        for model in self._models:
            for s in model.suggestions:
                # skip redundant completion
                if s.display != completionPrefix and \
                        not s.display in displayedTexts:
                    displayedTexts.append(s.display)
                    items = []
                    item = QStandardItem()
                    items.append(item)
                    item.setData(s.display, Qt.DisplayRole)
                    if s.description is not None:
                        self.__tooltips[s.display] = s.description
                    if s.decoration is not None:
                        item.setData(QIcon(s.decoration), Qt.DecorationRole)
                    cc_model.appendRow(items)
                    cptSuggestion += 1
            # do we need to use more completion model?
            if cptSuggestion >= self.minSuggestions:
                break  # enough suggestions
        return cc_model, cptSuggestion

    def _showCompletions(self, completionPrefix):
        """
        Shows the completion popup

        :param completionPrefix: completion prefix use to set the popup pos
        """
        c = self.__completer
        c.setCompletionPrefix(completionPrefix)
        c.popup().setCurrentIndex(
            self.__completer.completionModel().index(0, 0))
        cr = self.editor.codeEdit.cursorRect()
        charWidth = self.editor.codeEdit.fm.width('A')
        cr.setX(cr.x() - len(completionPrefix) * charWidth)
        cr.setWidth(400)
        c.complete(cr)  # popup it up!
        self._displayHighlightedTooltip(c.currentCompletion())

    def _hideCompletions(self):
        """
        Hides the completion popup
        """
        self.__completer.popup().hide()
        QToolTip.hideText()

    def _requestCompletion(self, completionPrefix, onlyAdapt=False):
        """
        Requests a code completion. The request will be transmitted to the
        background thread and treated by the __applyRequestResults slot when
        __completionResultsAvailable is emitted.

        :param completionPrefix:
        :param onlyAdapt:
        :return:
        """
        # cancel prev running request
        if not onlyAdapt:
            request = self._getCCRequest(completionPrefix)
        else:
            request = CompletionRequest(completionPrefix=completionPrefix,
                                        onlyAdapt=True)
开发者ID:hofoen,项目名称:pcef-core,代码行数:70,代码来源:cc.py

示例4: EditorCodeCompletion

# 需要导入模块: from PySide.QtGui import QCompleter [as 别名]
# 或者: from PySide.QtGui.QCompleter import setCompletionPrefix [as 别名]

#.........这里部分代码省略.........
        cursor.insertText(completion[extra:])
        self.setTextCursor(cursor)
        cursor.endEditBlock()

    def __insertTag(self):
        '''
        inserts the corresponding closing tag to an opening xml tag
        '''
        self.find('<', QTextDocument.FindBackward)
        tc = self.textCursor()        
        tc.select(QTextCursor.WordUnderCursor)
        txt = '' if self.__stringHasBracket(tc.selectedText().replace(' ', '')) else tc.selectedText()
        txt = '</' + txt + '>'
        
        self.find('>')    
        tc = self.textCursor()
        tc.clearSelection()
        
        tc.insertText(txt) 
        tc.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, len(txt))
        self.setTextCursor(tc)
    
    def __stringHasBracket(self, s):
        return '<' in s or '>' in s

    def __insertClosingTag(self, event):
        operation_sys = sys.platform 
        flag = "linux" in operation_sys        
        if flag :
            self.__insertClosingTag_Unix(event)
        else:
            self.__insertClosingTag_Win(event)
        
    def __insertClosingTag_Unix(self, event):
        '''
        inserts a closing tag after the closing bracket of open tag
        @param key: keyboard input value as int
        '''
        if self.flag_open_angle_bracket :
            if event.key() == 47 : # /
                print ("/")
                self.flag_open_angle_bracket = False 
            elif event.key() == 62  :  # >
                print (">")
                self.__insertTag()
                self.flag_open_angle_bracket = False  
        elif event.key() == 60  :  # <
            print ("<")
            self.flag_open_angle_bracket = True
 
    def __insertClosingTag_Win(self, event) :
        if self.flag_open_angle_bracket :
            if event.modifiers() & Qt.ShiftModifier :
                if event.key() == 55 : # /
                    print ("/")
                    self.flag_open_angle_bracket = False             
                elif event.key() == 60 : # > 
                    print (">")
                    self.__insertTag()
                    self.flag_open_angle_bracket = False  
        elif event.key() == 60  :  # <
            print ("<")
            self.flag_open_angle_bracket = True    
 
    def keyPressEvent(self, event):
        '''
        checks keyboard input to set closing tag or start code completion 
        '''        
        if self.m_completer.popup().isVisible() :
            if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return or event.key() == Qt.Key_Tab or event.key() == Qt.Key_Escape :
                event.ignore()
                return
        
        # open popup
        isShortcut = (event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_Space)                
                
        if not isShortcut:        
            super(EditorCodeCompletion, self).keyPressEvent(event)
            # ============================================================================================= 
            # begin tag inline insertion         
            self.__insertClosingTag(event)
            # end tag inline insertion 
            # ============================================================================================= 
        
        cursor = self.textCursor()
        cursor.select(QTextCursor.WordUnderCursor)        
        completionPrefix = cursor.selectedText()

        if completionPrefix != self.m_completer.completionPrefix() :
            self.m_completer.setCompletionPrefix(completionPrefix)
            self.m_completer.popup().setCurrentIndex(self.m_completer.completionModel().index(0, 0))

        # if not event.text() != "" and len(completionPrefix) > 2 :
        if len(completionPrefix) > 2 and isShortcut :
            print ("hier")
            cr = self.cursorRect()
            cr.setWidth(2 * (self.m_completer.popup().sizeHintForColumn(0)
                         + self.m_completer.popup().verticalScrollBar().sizeHint().width()))

            self.m_completer.complete(cr)  # # popup it up!
开发者ID:frankfx,项目名称:master_thesis,代码行数:104,代码来源:editor_comp.py


注:本文中的PySide.QtGui.QCompleter.setCompletionPrefix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。