def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.parent = parent sqlKeyword = QTextCharFormat() sqlOperator = QTextCharFormat() self.highlightingRules = [] # Keywords sqlKeyword.setFontWeight(QFont.Bold) sqlKeyword.setForeground(Qt.blue) sqlKeywords = ["AND", "OR", "LIKE"] for word in sqlKeywords: regExp = QRegExp("\\b" + word + "\\b", Qt.CaseInsensitive) rule = HighlightingRule(regExp, sqlKeyword) self.highlightingRules.append(rule) # Comparison Operators sqlOperator.setForeground(Qt.magenta) sqlOperators = ["<", ">", "="] for operator in sqlOperators: regExp = QRegExp("\\W" + operator + "\\W", Qt.CaseInsensitive) rule = HighlightingRule(regExp, sqlOperator) self.highlightingRules.append(rule)
def format(color, style=''): """Return a QTextCharFormat with the given attributes. """ _color = QColor() _color.setNamedColor(color) _format = QTextCharFormat() _format.setForeground(_color) if 'bold' in style: _format.setFontWeight(QFont.Bold) if 'italic' in style: _format.setFontItalic(True) return _format
def _insert_metadata_info(self): # Insert version and build numbers respectively. if not self._metadata is None: installed_version = self._metadata.get('version_installed', None) else: installed_version = version_from_metadata() if installed_version is None: return cursor = self.txtAbout.textCursor() cursor.movePosition(QTextCursor.End) cursor.insertBlock() cursor.insertBlock() # Insert installed version text version_msg = QApplication.translate('AboutSTDMDialog', 'STDM version') version_text = '{0} {1}'.format(version_msg, installed_version) char_format = cursor.blockCharFormat() text_format = QTextCharFormat(char_format) text_format.setFontWeight(75) cursor.insertText(version_text, text_format)
def set_text_bold(widget, pattern): """ Set bold text when word match with pattern :param widget: QTextEdit :param pattern: Text to find used as pattern for QRegExp (String) :return: """ cursor = widget.textCursor() format_ = QTextCharFormat() format_.setFontWeight(QFont.Bold) regex = QRegExp(pattern) pos = 0 index = regex.indexIn(widget.toPlainText(), pos) while index != -1: # Set cursor at begin of match cursor.setPosition(index, 0) pos = index + regex.matchedLength() # Set cursor at end of match cursor.setPosition(pos, 1) # Select the matched text and apply the desired format cursor.mergeCharFormat(format_) # Move to the next match index = regex.indexIn(widget.toPlainText(), pos)
def __init__(self, parent=None): super(XMLHighlighter, self).__init__(parent) keyword_format = QTextCharFormat() keyword_format.setForeground(Qt.darkMagenta) keyword_patterns = ["\\b?xml\\b", "/>", ">", "<"] self.highlightingRules = [(QRegExp(pattern), keyword_format) for pattern in keyword_patterns] xml_element_format = QTextCharFormat() xml_element_format.setForeground(QColor("#117700")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_\-]+(?=[\s/>])"), xml_element_format)) nominatim_area_format = QTextCharFormat() nominatim_area_format.setFontItalic(True) nominatim_area_format.setFontWeight(QFont.Bold) nominatim_area_format.setForeground(QColor("#FF7C00")) self.highlightingRules.append( (QRegExp("\{\{[A-Za-z0-9:, ]*\}\}"), nominatim_area_format)) xml_attribute_format = QTextCharFormat() xml_attribute_format.setFontItalic(True) xml_attribute_format.setForeground(QColor("#2020D2")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_]+(?=\\=)"), xml_attribute_format)) self.value_format = QTextCharFormat() self.value_format.setForeground(Qt.red) self.value_start_expression = QRegExp("\"") self.value_end_expression = QRegExp("\"(?=[\s></])") single_line_comment_format = QTextCharFormat() single_line_comment_format.setForeground(Qt.gray) self.highlightingRules.append( (QRegExp("<!--[^\n]*-->"), single_line_comment_format))
class XMLHighlighter(QSyntaxHighlighter): def __init__(self, parent=None): super(XMLHighlighter, self).__init__(parent) keyword_format = QTextCharFormat() keyword_format.setForeground(Qt.darkMagenta) keyword_patterns = ["\\b?xml\\b", "/>", ">", "<"] self.highlightingRules = [(QRegExp(pattern), keyword_format) for pattern in keyword_patterns] xml_element_format = QTextCharFormat() xml_element_format.setForeground(QColor("#117700")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_\-]+(?=[\s/>])"), xml_element_format)) nominatim_area_format = QTextCharFormat() nominatim_area_format.setFontItalic(True) nominatim_area_format.setFontWeight(QFont.Bold) nominatim_area_format.setForeground(QColor("#FF7C00")) self.highlightingRules.append( (QRegExp("\{\{[A-Za-z0-9:, ]*\}\}"), nominatim_area_format)) xml_attribute_format = QTextCharFormat() xml_attribute_format.setFontItalic(True) xml_attribute_format.setForeground(QColor("#2020D2")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_]+(?=\\=)"), xml_attribute_format)) self.value_format = QTextCharFormat() self.value_format.setForeground(Qt.red) self.value_start_expression = QRegExp("\"") self.value_end_expression = QRegExp("\"(?=[\s></])") single_line_comment_format = QTextCharFormat() single_line_comment_format.setForeground(Qt.gray) self.highlightingRules.append( (QRegExp("<!--[^\n]*-->"), single_line_comment_format)) def highlightBlock(self, text): # for every pattern for pattern, char_format in self.highlightingRules: # Create a regular expression from the retrieved pattern expression = QRegExp(pattern) # Check what index that expression occurs at with the ENTIRE text index = expression.indexIn(text) # While the index is greater than 0 while index >= 0: # Get the length of how long the expression is true, # set the format from the start to the length with # the text format length = expression.matchedLength() self.setFormat(index, length, char_format) # Set index to where the expression ends in the text index = expression.indexIn(text, index + length) self.setCurrentBlockState(0) start_index = 0 if self.previousBlockState() != 1: start_index = self.value_start_expression.indexIn(text) while start_index >= 0: end_index = self.value_end_expression.indexIn(text, start_index) if end_index == -1: self.setCurrentBlockState(1) comment_length = len(text) - start_index else: comment_length = \ end_index - start_index + \ self.value_end_expression.matchedLength() self.setFormat(start_index, comment_length, self.value_format) start_index = self.value_start_expression.indexIn( text, start_index + comment_length)
def CreatePDF(self, task, out, timestamp, data, frame, rows, columns, fileName, VManager): ''' Create PDF QgsTask ''' font_normal = QFont("Helvetica", 8, QFont.Normal) font_bold = QFont("Helvetica", 9, QFont.Bold) printer = QPrinter(QPrinter.HighResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPageSize(QPrinter.A4) printer.setOutputFileName(out) printer.setFullPage(True) document = QTextDocument() document.setDefaultFont(font_normal) document.setPageSize(printer.paperSize(QPrinter.Point)); cursor = QTextCursor(document) video_t = QCoreApplication.translate("QgsFmvMetadata", "Video : ") time_t = QCoreApplication.translate("QgsFmvMetadata", "TimeStamp : ") cursor.insertHtml( """ <p style='text-align: center;'> <img style='display: block; margin-left: auto; margin-right: auto;' src=\':/imgFMV/images/header_logo.png\' width='200' height='25' /> <p style='text-align: center;'> <strong>%s</strong>%s<strong> <p style='text-align: center;'> <strong>%s</strong>%s <p></p> """ % (video_t, fileName, time_t, timestamp)) tableFormat = QTextTableFormat() tableFormat.setBorderBrush(QBrush(Qt.black)) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setHeaderRowCount(1) tableFormat.setCellPadding(2) tableFormat.setCellSpacing(2) cursor.insertTable(rows + 1, columns, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setFont(font_bold) tableHeaderFormat.setBackground(QColor("#67b03a")) tableHeaderFormat.setForeground(Qt.white) tableHeaderFormat.setVerticalAlignment(QTextCharFormat.AlignMiddle) alternate_background = QTextCharFormat() alternate_background.setBackground(QColor("#DDE9ED")) for column in range(columns): cursor.mergeBlockCharFormat(tableHeaderFormat) cursor.insertText(VManager.horizontalHeaderItem( column).text()) cursor.movePosition(QTextCursor.NextCell) row = 1 for key in sorted(data.keys()): values = [str(key), str(data[key][0]),str(data[key][1])] for column in range(columns): cursor.insertText(values[column]) if (row) % 2 == 0: cursor.mergeBlockCharFormat(alternate_background) cursor.movePosition(QTextCursor.NextCell) row += 1 cursor.movePosition(QTextCursor.End) current_t = QCoreApplication.translate("QgsFmvMetadata", "Current Frame") self.TextBlockCenter(cursor, TextFormat= QTextFormat.PageBreak_AlwaysBefore) cursor.insertHtml(""" <br><p style='text-align: center;'><strong>""" + current_t +"""</strong></p><br> """) self.TextBlockCenter(cursor) cursor.insertImage(frame.scaledToWidth(500, Qt.SmoothTransformation)) document.print_(printer) if task.isCanceled(): return None return {'task': task.description()}
def __init__(self, parent=None): super().__init__(parent) keyword_format = QTextCharFormat() keyword_format.setForeground(Qt.darkMagenta) keyword_patterns = [ "\\b?xml\\b", "/>", ">", "<", ";", "\[", "\]", "\(", "\)" ] self.highlightingRules = [(QRegExp(pattern), keyword_format) for pattern in keyword_patterns] element_format = QTextCharFormat() element_format.setForeground(QColor("#117700")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z_\-]+(?=[\s\/>:;])"), element_format)) nominatim_area_format = QTextCharFormat() nominatim_area_format.setFontItalic(True) nominatim_area_format.setFontWeight(QFont.Bold) nominatim_area_format.setForeground(QColor("#FF7C00")) self.highlightingRules.append( (QRegExp("\{\{[A-Za-z0-9:, ]*\}\}"), nominatim_area_format)) attribute_format = QTextCharFormat() attribute_format.setFontItalic(True) attribute_format.setForeground(QColor("#2020D2")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_-]+(?=\\=|\\[|\\(|$|\\.)"), attribute_format)) value_format = QTextCharFormat() value_format.setForeground(Qt.red) self.highlightingRules.append( (QRegExp("(\"[A-Za-z0-9:, _.]*\"|\:([0-9]+)(?=\,|\]))"), value_format)) area_format = QTextCharFormat() area_format.setForeground(QColor("#11CC00")) area_pattern = [ "\.([A-Za-z0-9_]{2,})(?=\\)|\\;)", "\(([0-9]{2,})\)", "[0-9]+[.]+[0-9]+" ] for pattern in area_pattern: self.highlightingRules.append((QRegExp(pattern), area_format)) single_line_comment_format = QTextCharFormat() single_line_comment_format.setForeground(Qt.gray) self.highlightingRules.append( (QRegExp("(<!--[^\n]*-->|//[^\n]*)"), single_line_comment_format)) # Multi lines comment self.oql_start_comment = QRegExp("\/\*") self.oql_end_comment = QRegExp('\*\/')