bool QFCompleterTextEditWidget::replaceNext(){
    if (!currentlyReplacing) return false;


    QTextDocument::FindFlags flags=0;
    if (matchCase) flags |= QTextDocument::FindCaseSensitively;
    if (wholeWords) flags |= QTextDocument::FindWholeWords;
    if (!find(searchPhrase, flags)) {
        emit findNextAvailable(false);
        return false;
    } else {
        if (askBeforeReplace) {
            QMessageBox::StandardButton ret;
            ret = QMessageBox::question(this, tr("Find & Replace ..."),
                         tr("Replace this occurence?"),
                         QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
            if (ret == QMessageBox::Cancel) {
                emit findNextAvailable(false);
                return false;
            } else if (ret == QMessageBox::Yes) {
                QTextCursor c(textCursor());
                c.insertText(replacePhrase);
                setTextCursor(c);
            }

        } else {
            QTextCursor c(textCursor());
            c.insertText(replacePhrase);
            setTextCursor(c);
        }
    }
    return true;
}
bool QFCompleterTextEditWidget::findFirst(QString phrase, bool searchFromStart, bool matchCase, bool wholeWords){
    this->searchFromStart=searchFromStart;
    this->matchCase=matchCase;
    this->wholeWords=wholeWords;
    this->searchPhrase=phrase;
    currentlySearching=true;
    currentlyReplacing=false;
    // move cursor to home position of first line
    QTextCursor c(textCursor());
    c.clearSelection();
    if (searchFromStart) {
        c.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
        while (!c.atStart()) {
            c.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor);
        }
    }
    setTextCursor(c);

    // find the next occurence
    QTextDocument::FindFlags flags=0;
    if (matchCase) flags |= QTextDocument::FindCaseSensitively;
    if (wholeWords) flags |= QTextDocument::FindWholeWords;
    if (find(phrase, flags)) {
        emit findNextAvailable(true);
        return true;
    }else {
        emit findNextAvailable(false);
        return false;
    }
}
bool QFCompleterTextEditWidget::findNext(){
    if (!currentlySearching) return false;

    // find the next occurence
    QTextDocument::FindFlags flags=0;
    if (matchCase) flags |= QTextDocument::FindCaseSensitively;
    if (wholeWords) flags |= QTextDocument::FindWholeWords;
    if (find(searchPhrase, flags)) {
        emit findNextAvailable(true);
        return true;
    }else {
        emit findNextAvailable(false);
        return false;
    }
}
Example #4
0
void FindDialog::findChanged(const QString& text)
{
	bool enabled = !text.isEmpty();
	m_find_button->setEnabled(enabled);
	m_replace_button->setEnabled(enabled);
	m_replace_all_button->setEnabled(enabled);
	emit findNextAvailable(enabled);
}
bool QFCompleterTextEditWidget::replaceFirst(QString phrase, QString replaceBy, bool searchFromStart, bool matchCase, bool wholeWords, bool replaceAll, bool askBeforeReplace){
    this->searchFromStart=searchFromStart;
    this->matchCase=matchCase;
    this->wholeWords=wholeWords;
    this->searchPhrase=phrase;
    this->replacePhrase=replaceBy;
    this->replaceAll=replaceAll;
    this->askBeforeReplace=askBeforeReplace;
    currentlySearching=false;
    currentlyReplacing=true;

    emit findNextAvailable(true);


    QTextCursor c(textCursor());
    c.clearSelection();
    if (searchFromStart) {
        c.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
        while (!c.atStart()) {
            c.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor);
        }
    }
    setTextCursor(c);

    // find the next occurence
    QTextDocument::FindFlags flags=0;
    if (matchCase) flags |= QTextDocument::FindCaseSensitively;
    if (wholeWords) flags |= QTextDocument::FindWholeWords;
    if (replaceAll) {
        long count=0;
        bool stopped=false;
        while (find(phrase, flags) && !stopped) {
            if (askBeforeReplace) {
                QMessageBox::StandardButton ret;
                ret = QMessageBox::question(this, tr("Find & Replace ..."),
                             tr("Replace this occurence?"),
                             QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
                if (ret == QMessageBox::Cancel) {
                    stopped=true;
                } else if (ret == QMessageBox::Yes) {
                    QTextCursor c(textCursor());
                    c.insertText(replacePhrase);
                    setTextCursor(c);
                    count++;
                }

            } else {
                QTextCursor c(textCursor());
                c.insertText(replacePhrase);
                setTextCursor(c);
                count++;
            }
        }
        if (count>0) QMessageBox::information(this, tr("Find & Replace ..."),
                             tr("Replaced %1 occurences of '%2' ...")
                             .arg(count)
                             .arg(searchPhrase));
        emit findNextAvailable(false);
        return (count>0);
    } else {
        if (!find(searchPhrase, flags)) {
            /*QMessageBox::information(this, tr("Find & Replace ..."),
                             tr("Did not find '%1' ...")
                             .arg(searchPhrase));*/
            emit findNextAvailable(false);
            return false;
        } else {
            if (askBeforeReplace) {
                QMessageBox::StandardButton ret;
                ret = QMessageBox::question(this, tr("Find & Replace ..."),
                             tr("Replace this occurence?"),
                             QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
                if (ret == QMessageBox::Cancel) {
                    emit findNextAvailable(false);
                    return true;
                } else if (ret == QMessageBox::Yes) {
                    QTextCursor c(textCursor());
                    c.insertText(replacePhrase);
                    setTextCursor(c);
                    emit findNextAvailable(true);
                    return true;
                } else if (ret == QMessageBox::No) {
                    emit findNextAvailable(true);
                    return true;
                }

            } else {
                QTextCursor c(textCursor());
                c.insertText(replacePhrase);
                setTextCursor(c);
                emit findNextAvailable(true);
                return true;
            }
        }
    }
    emit findNextAvailable(false);
    return false;
}