QString OrganiseFormat::ParseBlock(QString block, const Song& song, bool* any_empty) const { QRegExp tag_regexp(kTagPattern); QRegExp block_regexp(kBlockPattern); // Find any blocks first int pos = 0; while ((pos = block_regexp.indexIn(block, pos)) != -1) { // Recursively parse the block bool empty = false; QString value = ParseBlock(block_regexp.cap(1), song, &empty); if (empty) value = ""; // Replace the block's value block.replace(pos, block_regexp.matchedLength(), value); pos += value.length(); } // Now look for tags bool empty = false; pos = 0; while ((pos = tag_regexp.indexIn(block, pos)) != -1) { QString value = TagValue(tag_regexp.cap(1), song); if (value.isEmpty()) empty = true; block.replace(pos, tag_regexp.matchedLength(), value); pos += value.length(); } if (any_empty) *any_empty = empty; return block; }
void OrganiseFormat::SyntaxHighlighter::highlightBlock(const QString& text) { const bool light = QApplication::palette().color(QPalette::Base).value() > 128; const QRgb block_color = light ? kBlockColorLight : kBlockColorDark; const QRgb valid_tag_color = light ? kValidTagColorLight : kValidTagColorDark; const QRgb invalid_tag_color = light ? kInvalidTagColorLight : kInvalidTagColorDark; QRegExp tag_regexp(kTagPattern); QRegExp block_regexp(kBlockPattern); QTextCharFormat block_format; block_format.setBackground(QColor(block_color)); // Reset formatting setFormat(0, text.length(), QTextCharFormat()); // Blocks int pos = 0; while ((pos = block_regexp.indexIn(text, pos)) != -1) { setFormat(pos, block_regexp.matchedLength(), block_format); pos += block_regexp.matchedLength(); } // Tags pos = 0; while ((pos = tag_regexp.indexIn(text, pos)) != -1) { QTextCharFormat f = format(pos); f.setForeground(QColor(OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1)) ? valid_tag_color : invalid_tag_color)); setFormat(pos, tag_regexp.matchedLength(), f); pos += tag_regexp.matchedLength(); } }