Пример #1
0
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;
}
Пример #2
0
QValidator::State OrganiseFormat::Validator::validate(QString& input,
        int&) const {
    QRegExp tag_regexp(kTagPattern);

    // Make sure all the blocks match up
    int block_level = 0;
    for (int i = 0; i < input.length(); ++i) {
        if (input[i] == '{')
            block_level++;
        else if (input[i] == '}')
            block_level--;

        if (block_level < 0 || block_level > 1) return QValidator::Invalid;
    }

    if (block_level != 0) return QValidator::Invalid;

    // Make sure the tags are valid
    int pos = 0;
    while ((pos = tag_regexp.indexIn(input, pos)) != -1) {
        if (!OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1)))
            return QValidator::Invalid;

        pos += tag_regexp.matchedLength();
    }

    return QValidator::Acceptable;
}
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();
  }
}