Esempio n. 1
0
 def testRefcount(self):
     textedit = QTextEdit()
     textedit.setReadOnly(True)
     doc = textedit.document()
     cursor = QTextCursor(doc)
     cursor.insertText("PySide Rocks")
     ud = TestUserData({"Life": 42})
     self.assertEqual(sys.getrefcount(ud), 2)
     cursor.block().setUserData(ud)
     self.assertEqual(sys.getrefcount(ud), 3)
     ud2 = cursor.block().userData()
     self.assertEqual(sys.getrefcount(ud), 4)
     self.udata = weakref.ref(ud, None)
     del ud, ud2
     self.assertEqual(sys.getrefcount(self.udata()), 2)
Esempio n. 2
0
    def getFirstVisibleBlockId(self):
        """
        Detect the first block for which bounding rect - once translated in absolute coordinated -
        is contained by the editor's text area
        :return: the first visible block
        """

        if self.verticalScrollBar().sliderPosition() == 0:
            return 0

        cursor = QTextCursor(self.document())
        cursor.movePosition(QTextCursor.Start)
        for i in range(self.document().blockCount()):
            block = cursor.block()
            r1 = self.viewport().geometry()
            r2 = self.document().documentLayout().blockBoundingRect(block).translated(
                self.viewport().geometry().x(),
                self.viewport().geometry().y() - self.verticalScrollBar().sliderPosition()
            ).toRect()

            if r1.contains(r2, True):
                return i

            cursor.movePosition(QTextCursor.NextBlock)

        return 0
Esempio n. 3
0
    def get_word_under_cursor(self, cursor: QTextCursor):
        """
        A word is ~ r'[A-Za-z\'-]+', where leading / trailing `'` are stripped or blocking
        e.g. "'h'i'" w/ cursor at 0 returns ("", ""), cursor at 1 returns ("", "h'i")
        :param cursor:
        :return: Tuple(front part, back part)
        """
        front_text = cursor.block().text()[:cursor.positionInBlock()]
        back_text = cursor.block().text()[cursor.positionInBlock():]

        # strip leading quotes
        raw_front_match = re.search(
            r'(?P<junk>\'*)(?P<raw_front>[A-Za-z\'-]*?)$', front_text)
        raw_front_word = raw_front_match.group('raw_front')
        # strip trailing quotes
        raw_back_match = re.search(r'^(?P<raw_back>[A-Za-z\'-]*)', back_text)
        raw_back_word = raw_back_match.group('raw_back')
        pre_back_match = re.search(
            r'^(?P<pre_back>[A-Za-z\'-]*?)(?P<junk>\'*)$', raw_back_word)
        pre_back_word = pre_back_match.group('pre_back')

        if len(pre_back_word) == 0:
            end_quotes_match = re.search(r'(?P<end_quotes>\'*)$',
                                         raw_front_word)
            if len(end_quotes_match.group(
                    'end_quotes')) > 0:  # Not inside a word.
                front_word = ''
            else:
                front_word = raw_front_word
        else:
            front_word = raw_front_word
        if len(front_word) == 0:
            lead_quotes_match = re.search(r'^(?P<lead_quotes>\'*)',
                                          pre_back_word)
            if len(lead_quotes_match.group(
                    'lead_quotes')) > 0:  # Not inside a word.
                back_word = ''
            else:
                back_word = pre_back_word
        else:
            back_word = pre_back_word

        return (front_word, back_word)
Esempio n. 4
0
    def on_log_received(self, data):
        time_info = datetime.fromtimestamp((data['time'] / 1000)).isoformat()
        log_message = '%s: %s : %s' % (time_info, data['level'],
                                       data['message'])
        message_document = self.document()
        cursor_to_add = QTextCursor(message_document)
        cursor_to_add.movePosition(cursor_to_add.End)
        cursor_to_add.insertText(log_message + '\n')

        if data['level'] in COLORS:
            fmt = QTextCharFormat()
            fmt.setForeground(COLORS[data['level']])
            cursor_to_add.movePosition(cursor_to_add.PreviousBlock)
            log_lvl_data = LogLevelData(log_levels[data['level'].upper()])
            cursor_to_add.block().setUserData(log_lvl_data)
            cursor_to_add_fmt = message_document.find(data['level'],
                                                      cursor_to_add.position())
            cursor_to_add_fmt.mergeCharFormat(fmt)
            if log_levels[data['level']] > self.log_lvl:
                cursor_to_add.block().setVisible(False)
        self.ensureCursorVisible()
Esempio n. 5
0
 def on_log_filter(self):
     log_lvl_name = str(self.sender().iconText()).upper()
     self.log_lvl = log_levels[log_lvl_name]
     cursor = QTextCursor(self.document())
     current_block = cursor.block()
     while current_block.isValid() and current_block.userData():
         block_log_lvl = current_block.userData().log_lvl
         if block_log_lvl <= self.log_lvl:
             current_block.setVisible(True)
         else:
             current_block.setVisible(False)
         current_block = next(current_block)
     self.viewport().update()