如何在QScintilla编辑器窗口小部件中取消选择/取消突出显示所选和突出显示的文本?

时间:2019-01-22 10:05:33

标签: c++ qt5 qscintilla

我正试图编写一个搜索框,其中包含查找/查找上一个/下一个以与QScintilla textEditor小部件进行交互。因此,我写了一些方法,一个用于突出显示所有匹配的单词,然后选择第一个出现的单词,一个用于取消选择/取消突出显示它们的方法。

首先执行接缝操作,但是如果我用另一个关键字重复搜索,则会同时显示旧关键字和新关键字。

enter image description here

这是我的代码片段:

//
// search & replace:
// do_search_and_replace() - search for matching word
//
void MainWindow::do_search_and_replace(QString action_str)
{
    int line, index;
    qDebug() <<  "do_search_and_replace()";
    // just to be sure...
    if(action_str.isEmpty())
        action_str == "0";

    int action_nr = action_str.toInt();    // convert argument to int, so we can switch() on it...
    text = ui->lineEdit_find->text();
    docText = ui->textEdit->text();
    qDebug() <<  "action_nr: " << action_nr;

    //
    // first part: Mark all occurances of search term
    //
    if (!( text.isEmpty() ))
    {
        qDebug() << text;
        ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_FULLBOX);
        ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::darkBlue));


        int end = docText.lastIndexOf(text);
        int cur = -1;

        if(end != -1)
        {
            ui->textEdit->getCursorPosition(&line, &index);
            qDebug() << "line: " << line << " index: " << index;
            while(cur != end)
            {
                cur = docText.indexOf(text,cur+1);
                ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE,cur,
                    text.length());
            }
        }
    } // END text.isEmpty(), END mark ALL

    //
    // second part: Find firs occurance of search term
    //
    bool use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward;
    use_regular_expression = false;
    is_case_sensitive = ui->checkBox_CaseSensitive->isChecked();
    match_whole_word_only = ui->checkBox_WholeWords->isChecked();
    use_wrap = true;
    search_forward = ui->radioButton_Forward->isChecked();

    ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_FULLBOX);
    //ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::darkBlue));

    bool found = ui->textEdit->findFirst(text, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);
    qDebug() <<  "START: found = " << found;
    while(found)
    {
        ui->textEdit->getCursorPosition(&line, &index);

        qDebug() << "line: " << line << " index: " << index;
        qDebug() << text;

        // pattern: found = findFirst(pattern, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward)
        //found = ui->textEdit->findFirst(text, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);

        if(found)
        {
            ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE, line, text.length());
            int start = ui->textEdit->positionFromLineIndex(line, index);
            int end = ui->textEdit->positionFromLineIndex(line, index + text.length());
            qDebug() << "line: " << line << " start: " << start << " end: " << end;

//            found = ui->textEdit->findNext();
//            ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE, line, text.length());

        }

        found = false;
    }
}


//
// unselect selected stuff
//
void MainWindow::reset_searchResult()
{
    int line, index;
    qDebug() <<  "in: reset_searchResult()";

    //QString text = ui->lineEdit_find->text();
    text.clear();
    docText.clear();

    //
    // first part: Mark all occurances of search term
    //
    if (( text.isEmpty() ))
    {
        qDebug() << text;
        ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_PLAIN);
        ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::white));

        //docText = text;
        docText = ui->lineEdit_find->text();
        int end = docText.lastIndexOf(text);
        int cur = -1;

        if(end != -1)
        {
            ui->textEdit->getCursorPosition(&line, &index);
            qDebug() << "line: " << line << " index: " << index;
            while(cur != end)
            {
                cur = docText.indexOf(text,cur+1);
                ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORALLONFOR,cur, text.length());
                ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORCLEARRANGE,cur, text.length());
            }
        }
    } // END text.isEmpty(), END mark ALL

    //
    // second part: Find firs occurance of search term
    //
    bool use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward;
    use_regular_expression = false;
    is_case_sensitive = ui->checkBox_CaseSensitive->isChecked();
    match_whole_word_only = ui->checkBox_WholeWords->isChecked();
    use_wrap = true;
    search_forward = ui->radioButton_Forward->isChecked();

    ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_PLAIN);
    //ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::darkBlue));

    bool found = ui->textEdit->findFirst(text, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);
    qDebug() <<  "START: found = " << found;
    while(found)
    {
        ui->textEdit->getCursorPosition(&line, &index);

        if(found)
        {
            ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORCLEARRANGE, line, text.length());
            int start = ui->textEdit->positionFromLineIndex(line, index);
            int end = ui->textEdit->positionFromLineIndex(line, index + text.length());
            qDebug() << "line: " << line << " start: " << start << " end: " << end;

        }

        found = false;
    }
}

我很确定这不是有史以来最好的代码,但是我所能负担的...;)

也许适合QScintilla的人可以纠正我的尝试或为我提供一个可以多次调用而又不保留旧结果的有效搜索和突出显示示例?

先谢谢大家!

1 个答案:

答案 0 :(得分:0)

假设您在ui-> textEdit中使用QsciScintilla实例,也许比您想象的要容易:

#define MY_MARKER_ID 0

void MainWindow::clearMarkers()
{
    int lastLine = ui->textEdit->lines() - 1;
    ui->textEdit->clearIndicatorRange( 0, 0, lastLine, ui->textEdit->text( lastLine ).length() - 1, MY_MARKER_ID );
}