xNotepad

Posted 一只蚂蚁2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xNotepad相关的知识,希望对你有一定的参考价值。

xNotepad

分享一个自己写的记事本xNotepad,MDI方式,支持语法高亮、自动补全,语法高亮,批量注释,缩进、文件批量转码等功能。
点击这里下载

效果图:

文件编码

bool TextEdit::saveFileEvent(const QString &fileName)

    QFile file(fileName);
    bool rs = file.open(QIODevice::WriteOnly);
    if(rs)
        QTextStream out(&file);
        out.setCodec(m_codec.textCodec());
        out.setGenerateByteOrderMark(m_codec.bom());
        out<<m_textEdit->toPlainText();
        m_textEdit->document()->setModified(false);
    
    return rs;

语法高亮

SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
    :QSyntaxHighlighter(parent)

    HighlightingRule rule;

    QStringList keys;
    keys<< "char" << "class" << "const"
        << "double" << "enum" << "explicit"
        << "friend" << "inline" << "int"
        << "long" << "namespace" << "operator"
        << "private" << "protected" << "public"
        << "short" << "signals" << "signed"
        << "slots" << "static" << "struct"
        << "template" << "typedef" << "typename"
        << "union" << "unsigned" << "virtual"
        << "void" << "volatile" << "bool" << "nullptr";
    rule.format.setForeground(Qt::blue);
    for(auto& key:keys)
        rule.pattern.setPattern("\\\\b"+key+"\\\\b");
        m_highlightingRules.append(rule);
    

    // class
    rule.pattern.setPattern("\\\\b(Q[A-Za-z]+|X[A-Za-z]+)\\\\b");
    rule.format.setForeground(Qt::darkBlue);
    m_highlightingRules.append(rule);

    // singale commet
    rule.pattern.setPattern("//[^\\n]*");
    rule.format.setForeground(Qt::gray);
    m_highlightingRules.append(rule);

    rule.pattern.setPattern("#[^\\n]*");
    rule.format.setForeground(Qt::gray);
    m_highlightingRules.append(rule);

    // quotation
    rule.pattern.setPattern("\\".*?\\"");
    rule.format.setForeground(Qt::darkGray);
    m_highlightingRules.append(rule);

    rule.pattern.setPattern("<.*?>");
    rule.format.setForeground(Qt::blue);
    m_highlightingRules.append(rule);

    // function
    rule.pattern.setPattern("\\\\b[A-Za-z0-9_]+(?=\\\\()");
    rule.format.setForeground(Qt::black);
    m_highlightingRules.append(rule);

    m_commentStartExpression.setPattern("/\\\\*");
    m_commentEndExpression.setPattern("\\\\*/");
    m_commentFormat.setForeground(Qt::gray);


void SyntaxHighlighter::highlightBlock(const QString &text)

    for(auto& rule:m_highlightingRules)
        QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
        while (matchIterator.hasNext()) 
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), rule.format);
        
    

    setCurrentBlockState(0);
    int startIndex = 0;
    if (previousBlockState() != 1)
        startIndex = text.indexOf(m_commentStartExpression);
    while (startIndex >= 0) 
        QRegularExpressionMatch match = m_commentEndExpression.match(text, startIndex);
        int endIndex = match.capturedStart();
        int commentLength = 0;
        if (endIndex == -1) 
            setCurrentBlockState(1);
            commentLength = text.length() - startIndex;
         else 
            commentLength = endIndex - startIndex
                            + match.capturedLength();
        
        setFormat(startIndex, commentLength, m_commentFormat);
        startIndex = text.indexOf(m_commentStartExpression, startIndex + commentLength);
    

自动补全

    auto completer = new QCompleter(this);
    auto completerModel = new QStringListModel(completer);
    completerModel->setStringList(keys);
    completer->setModel(completerModel);
    //completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    completer->setWrapAround(false);
    m_textEdit->setCompleter(completer);

以上是关于xNotepad的主要内容,如果未能解决你的问题,请参考以下文章

如何批量在多个文件前面添加名称或者数字?

SVN 批量删除或添加文件

批量修改JS文件名称.

Linux下批量删除文件(急)

怎么批量修改部分文件名?

bat批量删除特定开头同一类型文件?