void FindAndReplace::find( bool next )
{
    QLineEdit * find = ui->findEdit;
    const QString & text = find->text();

    if ( text.isEmpty() )
        return;

    bool backward = !next;
    Shared::FindFlags findFlags = ui->findEdit->findOptions();
    bool caseSensitive = findFlags.testFlag( Shared::CaseSensitive );
    bool wholeWords = findFlags.testFlag( Shared::WholeWords );
    bool regExp = findFlags.testFlag( Shared::RegExp );

    QTextDocument::FindFlags flags;
    if ( backward )
        flags |= QTextDocument::FindBackward;

    if ( caseSensitive )
        flags |= QTextDocument::FindCaseSensitively;

    if ( wholeWords )
        flags |= QTextDocument::FindWholeWords;

    QTextDocument * document = d_editor->document();
    QTextCursor textCursor = d_editor->textCursor();
    do
    {
        if ( regExp )
            textCursor = document->find( QRegExp( text ), textCursor, flags );
        else
            textCursor = document->find( text, textCursor, flags );

        if ( textCursor.isNull() )
        {
            d_editor->moveCursor( next ? QTextCursor::Start : QTextCursor::End, QTextCursor::MoveAnchor );

            if ( regExp )
                textCursor = document->find( QRegExp( text ), textCursor, flags );
            else
                textCursor = document->find( text, textCursor, flags );
        }

        if ( textCursor.hasSelection() )
        {
            d_editor->setTextCursor( textCursor );
            break;
        }

    } while ( !textCursor.isNull() );

    d_editor->ensureCursorVisible();
    d_editor->setFocus();
}
bool TextBrowserHelpViewer::findText(const QString &text, Core::FindFlags flags,
    bool incremental, bool fromSearch, bool *wrapped)
{
    if (wrapped)
        *wrapped = false;
    QTextDocument *doc = m_textBrowser->document();
    QTextCursor cursor = m_textBrowser->textCursor();
    if (!doc || cursor.isNull())
        return false;

    const int position = cursor.selectionStart();
    if (incremental)
        cursor.setPosition(position);

    QTextDocument::FindFlags f = Core::textDocumentFlagsForFindFlags(flags);
    QTextCursor found = doc->find(text, cursor, f);
    if (found.isNull()) {
        if ((flags & Core::FindBackward) == 0)
            cursor.movePosition(QTextCursor::Start);
        else
            cursor.movePosition(QTextCursor::End);
        found = doc->find(text, cursor, f);
        if (!found.isNull() && wrapped)
            *wrapped = true;
    }

    if (fromSearch) {
        cursor.beginEditBlock();
        m_textBrowser->viewport()->setUpdatesEnabled(false);

        QTextCharFormat marker;
        marker.setForeground(Qt::red);
        cursor.movePosition(QTextCursor::Start);
        m_textBrowser->setTextCursor(cursor);

        while (m_textBrowser->find(text)) {
            QTextCursor hit = m_textBrowser->textCursor();
            hit.mergeCharFormat(marker);
        }

        m_textBrowser->viewport()->setUpdatesEnabled(true);
        cursor.endEditBlock();
    }

    bool cursorIsNull = found.isNull();
    if (cursorIsNull) {
        found = m_textBrowser->textCursor();
        found.setPosition(position);
    }
    m_textBrowser->setTextCursor(found);
    return !cursorIsNull;
}
Example #3
0
	void finish()
	{
		for (int i = 0; i < codeSections.size(); ++i)
		{
			QTextCharFormat fmt;
			fmt.setFontFamily("Monospace");
			doc->find(QString("$${{%1}}$$").arg(i)).insertText(codeSections.at(i), fmt);
		}
		for (int i = 0; i < htmlSections.size(); ++i)
		{
			doc->find(QString("$$[[%1]]$$").arg(i)).insertHtml(htmlSections.at(i));
		}
	}
/*!
    \reimp
 */
void TextEditFindWidget::find(const QString &ttf, bool skipCurrent, bool backward, bool *found, bool *wrapped)
{
    if (!m_textEdit)
        return;

    QTextCursor cursor = m_textEdit->textCursor();
    QTextDocument *doc = m_textEdit->document();

    if (!doc || cursor.isNull())
        return;

    if (cursor.hasSelection())
        cursor.setPosition((skipCurrent && !backward) ? cursor.position() : cursor.anchor());

    *found = true;
    QTextCursor newCursor = cursor;

    if (!ttf.isEmpty()) {
        QTextDocument::FindFlags options;

        if (backward)
            options |= QTextDocument::FindBackward;

        if (caseSensitive())
            options |= QTextDocument::FindCaseSensitively;

        if (wholeWords())
            options |= QTextDocument::FindWholeWords;

        newCursor = doc->find(ttf, cursor, options);
        if (newCursor.isNull()) {
            QTextCursor ac(doc);
            ac.movePosition(options & QTextDocument::FindBackward
                    ? QTextCursor::End : QTextCursor::Start);
            newCursor = doc->find(ttf, ac, options);
            if (newCursor.isNull()) {
                *found = false;
                newCursor = cursor;
            } else {
                *wrapped = true;
            }
        }
    }

    if (!isVisible())
        show();

    m_textEdit->setTextCursor(newCursor);
}
Example #5
0
/*!
 *  Find a string within document.
 *
 *  \param  findString  string to be found.
 *  \param  options     find options.
 */
void TextEdit::findExpretion(const QString &findString, QTextDocument::FindFlags options)
{
    /*! \todo fix the performance! */
    //highlight(findString, options);

    QTextDocument *document = this->document();
    QTextCursor findCursor(document);
    QTextCursor cursor(document);

    int pos = this->textCursor().position();
    if (textCursor().hasSelection() && options == QTextDocument::FindBackward){
        findCursor.setPosition(pos, QTextCursor::MoveAnchor);
        findCursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
    } else {
        findCursor.setPosition(pos, QTextCursor::MoveAnchor);
    }
    if (textCursor().hasSelection() && options == QTextDocument::FindBackward){
        cursor.setPosition(pos, QTextCursor::MoveAnchor);
        cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
    } else {
        cursor.setPosition(pos, QTextCursor::MoveAnchor);
    }

    if ((m_lastFindString.contains(findString) || findString.contains(m_lastFindString)) &&
        options != QTextDocument::FindBackward && textCursor().hasSelection() &&
        findString != m_lastFindString){
        findCursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
    }

    //if (!(this->textCursor().hasSelection()) && (!findString.isEmpty() || !findString.isNull()))
    findCursor = document->find(findString, findCursor, options);

    if (findCursor.isNull()) {
        if (options == QTextDocument::FindBackward){
            findCursor = cursor;
            findCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        }
        findCursor = document->find(findString, findCursor, options);
    }

    if (findCursor.isNull()) {
        this->setTextCursor(cursor);
    } else {
        this->setTextCursor(findCursor);
        //m_isFirstTime = false;
    }
    m_lastFindString = findString;
}
Example #6
0
QString ChatEdit::textEditToPlainText()
{
	QTextDocument *doc = document();
	QString result;
	result.reserve(doc->characterCount());
	QTextCursor begin(doc);
	QTextCursor end;
	QString specialChar = QChar(QChar::ObjectReplacementCharacter);
	bool first = true;
	while (!begin.atEnd()) {
		end = doc->find(specialChar, begin, QTextDocument::FindCaseSensitively);
		QString postValue;
		bool atEnd = end.isNull();
		if (atEnd) {
			end = QTextCursor(doc);
			QTextBlock block = doc->lastBlock();
			end.setPosition(block.position() + block.length() - 1);
		} else {
			postValue = end.charFormat().toolTip();
		}
		begin.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,
						   end.position() - begin.position() - (atEnd ? 0 : 1));
		QString selectionText = begin.selection().toPlainText();
		if (!first)
			result += selectionText.midRef(1);
		else
			result += selectionText;
		result += postValue;
		begin = end;
		end.clearSelection();
		first = false;
	}
	return result;
}
Example #7
0
QTextCursor QTextDocumentProto::find(const QRegExp &expr, int position, int options) const
{
  QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject());
  if (item)
    return item->find(expr, position, (QTextDocument::FindFlags)options);
  return QTextCursor();
}
Example #8
0
QTextCursor QTextDocumentProto::find(const QString &subString, const QTextCursor &cursor, int options) const
{
  QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject());
  if (item)
    return item->find(subString, cursor, (QTextDocument::FindFlags)options);
  return QTextCursor();
}
Example #9
0
void Journal::on_calendarWidget_activated(const QDate &date)
{
    ui->textEdit->setText(notes);
    QString searchString;
    searchString=date.toString();
    QTextDocument *document = ui->textEdit->document();

    bool found=false;
    QTextCursor highlightCursor(document);
    QTextCursor cursor(document);

    cursor.beginEditBlock();

    QTextCharFormat plainFormat(highlightCursor.charFormat());
    QTextCharFormat colorFormat = plainFormat;
    colorFormat.setForeground(Qt::red);

    while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
        highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);

        if (!highlightCursor.isNull()) {
            found = true;
            highlightCursor.movePosition(QTextCursor::WordRight,
                                         QTextCursor::KeepAnchor);
            highlightCursor.mergeCharFormat(colorFormat);
        }
    }

    cursor.endEditBlock();


}
Example #10
0
void TestFindMatch::testBasic()
{
    KoFindMatch match;

    QTextDocument *doc = new QTextDocument("Test Document", this);
    QVariant docVariant = QVariant::fromValue(doc);
    QVERIFY(docVariant.isValid());
    
    match.setContainer(docVariant);
    QCOMPARE(match.container(), docVariant);
    QCOMPARE(match.container().value<QTextDocument*>(), doc);

    QTextCursor cursor = doc->find("Document");
    QVariant curVariant = QVariant::fromValue(cursor);
    QVERIFY(curVariant.isValid());
    
    match.setLocation(curVariant);
    QCOMPARE(match.location(), curVariant);
    QCOMPARE(match.location().value<QTextCursor>(), cursor);

    QVERIFY(match.isValid());

    KoFindMatch other(docVariant, curVariant);
    QVERIFY(other.isValid());
    QCOMPARE(other.container(), docVariant);
    QCOMPARE(other.location(), curVariant);
}
void	QsvTextOperationsWidget::on_replaceOldText_returnPressed()
{
	if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) ||
	    QApplication::keyboardModifiers().testFlag(Qt::AltModifier) ||
	    QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) ) {
		on_replaceAll_clicked();
		showReplace();
		return;
	}

	QTextCursor c = m_searchCursor;
	QTextDocument *doc = getTextDocument();
	if (!doc){
		qDebug("%s:%d - no document found, using a wrong class? wrong parent?", __FILE__,__LINE__);
		return;
	}
	c = doc->find( replaceFormUi->findText->text(), c, getReplaceFlags() );
	if (c.isNull())
		return;

	int start = c.selectionStart();
	int end   = c.selectionEnd();
	c.beginEditBlock();
	c.deleteChar();
	c.insertText( replaceFormUi->replaceText->text() );
	c.setPosition(start,QTextCursor::KeepAnchor);
	c.setPosition(end  ,QTextCursor::MoveAnchor);
	c.endEditBlock();
	setTextCursor( c );

	// is there any other apperance of this text?
	m_searchCursor = c;
	updateReplaceInput();
}
Example #12
0
void TestFindMatch::testCopyAssign()
{
    KoFindMatch match;

    QTextDocument *doc = new QTextDocument("Test Document", this);
    QVariant docVariant = QVariant::fromValue(doc);
    QTextCursor cursor = doc->find("Document");
    QVariant curVariant = QVariant::fromValue(cursor);

    QVERIFY(docVariant.isValid());
    QVERIFY(curVariant.isValid());
    
    match.setContainer(docVariant);
    match.setLocation(curVariant);

    KoFindMatch other(match);
    QVERIFY(other.isValid());
    QCOMPARE(other.container(), match.container());
    QCOMPARE(other.location(), match.location());

    KoFindMatch third = match;
    QVERIFY(third.isValid());
    QCOMPARE(third.container(), match.container());
    QCOMPARE(third.location(), match.location());

    QVERIFY(other == match);
}
Example #13
0
void PlainTextViewWidget::searchDocument(const QString &text, Qt::CaseSensitivity caseSensitive)
{
   m_currentSearchResult = QTextCursor();
   m_searchResults.clear();

   QTextDocument::FindFlags findFlags;
   if (caseSensitive == Qt::CaseSensitive) {
      findFlags |= QTextDocument::FindCaseSensitively;
   }

   QPlainTextEdit *textEdit = ui->plainTextEdit;
   QTextDocument *textDocument = textEdit->document();
   QTextCursor currentCursor = textEdit->textCursor();
   currentCursor.clearSelection(); // To get QTextDocument::find to work properly

   // Find all results forwards beginning from current cursor
   QTextCursor findCursor = textDocument->find(text, currentCursor, findFlags);
   if (!findCursor.isNull()) {
      // Set to first result after current cursor pos
      m_currentSearchResult = findCursor;
   }

   while (!findCursor.isNull()) {
      m_searchResults.append(findCursor);
      findCursor = textDocument->find(text, findCursor, findFlags);
   }

   // Find all results backwards beginning from current cursor
   findFlags |= QTextDocument::FindBackward;
   findCursor = textDocument->find(text, currentCursor, findFlags);
   while (!findCursor.isNull()) {
      m_searchResults.prepend(findCursor);
      findCursor = textDocument->find(text, findCursor, findFlags);
   }

   if (m_searchResults.isEmpty()) {
      ui->searchBar->setResultNumberAndCount(0, 0);
      return;
   }

   if (m_currentSearchResult.isNull()) {
      // Set to first result because after current cursor pos wasn't a search match
      m_currentSearchResult = m_searchResults.at(0);
   }

   jumpToHighlightResult(m_currentSearchResult);
}
Example #14
0
void CodeEditor::findText(QString text, int dir){
    QTextCursor cursor = textCursor();
    if(dir==0 || dir==-1){
        cursor.movePosition(cursor.PreviousWord);
    }
    QTextDocument* d = document();
    QTextCursor p;
    if(dir==-1){
        p = d->find(text,cursor,d->FindBackward);
    }
    else{
        p = d->find(text,cursor);
    }
    qDebug() << text << "Found...";
    qDebug() << p.blockNumber();
    if(!p.isNull()){
        setTextCursor(p);
    }
}
Example #15
0
void QQTextEdit::replaceIdToName(QString id, QString name)
{
    QTextDocument *doc = this->document();
    QTextCursor cursor = doc->find(id, QTextCursor());

    if (cursor.isNull())
        return;

    cursor.insertText(name);
}
Example #16
0
bool BaseEditor::findAllOccurrance(const QString &text, QTextDocument::FindFlags qff, bool isRE)
{
    QTextDocument *doc = document();

    findTextSelection.clear();
    bool finded=false;

    if(text.isEmpty())
    {
        prevFindCursor = QTextCursor();
        return finded;
    } else {
        QTextEdit::ExtraSelection es;
        QTextCursor highlightCursor(doc);

        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setBackground(Qt::yellow);

        es.format = colorFormat;
        QRegExp re(text);

        while(!highlightCursor.isNull() && !highlightCursor.atEnd())
        {
            highlightCursor = isRE ? doc->find(re, highlightCursor, qff) :
                                     doc->find(text, highlightCursor, qff);

            if(!highlightCursor.isNull())
            {
                finded = true;
                es.cursor = highlightCursor;
                findTextSelection.append(es);
            }
        }
        if(!finded)
        {
            prevFindCursor = highlightCursor;
        }
        return finded;
    }
}
Example #17
0
void BaseEditor::replaceAll(const QString &ft, const QString &rt,
                            QTextDocument::FindFlags qff, bool isRE)
{
    QTextDocument *doc = document();
    QTextCursor tc(doc);
    QRegExp re(ft);
    replacing = true;
    while(!tc.isNull() && !tc.atEnd())
    {
        tc = isRE ? doc->find(re, tc, qff) :
                                 doc->find(ft, tc, qff);
        if(!tc.isNull())
        {
            tc.beginEditBlock();
            tc.insertText(rt);
            tc.endEditBlock();
        }
    }
    replacing = false;
    findAndHighlightText(ft, qff, isRE);
}
Example #18
0
    void highlightMatches(const QString &pattern)
    {
        QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget);
        if (!ed)
            return;

        QTextCursor cur = ed->textCursor();

        QTextEdit::ExtraSelection selection;
        selection.format.setBackground(Qt::yellow);
        selection.format.setForeground(Qt::black);

        // Highlight matches.
        QTextDocument *doc = ed->document();
        QRegExp re(pattern);
        cur = doc->find(re);

        m_searchSelection.clear();

        int a = cur.position();
        while ( !cur.isNull() ) {
            if ( cur.hasSelection() ) {
                selection.cursor = cur;
                m_searchSelection.append(selection);
            } else {
                cur.movePosition(QTextCursor::NextCharacter);
            }
            cur = doc->find(re, cur);
            int b = cur.position();
            if (a == b) {
                cur.movePosition(QTextCursor::NextCharacter);
                cur = doc->find(re, cur);
                b = cur.position();
                if (a == b) break;
            }
            a = b;
        }

        updateExtraSelections();
    }
void MainWindow::insertCustomer(const QString &customer){
    if (customer.isEmpty())
        return;
    QStringList customerList = customer.split(", ");
    QTextDocument *document = chatbox->document();
    QTextCursor cursor = document->find("NAME");
    if (!cursor.isNull()) {
        cursor.beginEditBlock();
        cursor.insertText(customerList.at(0));
        QTextCursor oldcursor = cursor;
        cursor = document->find("ADDRESS");
        if (!cursor.isNull()) {
            for (int i = 1; i < customerList.size(); ++i) {
                cursor.insertBlock();
                cursor.insertText(customerList.at(i));
            }
            cursor.endEditBlock();
        }
        else
            oldcursor.endEditBlock();
    }
}
Example #20
0
    void highlightMatches(const QString &pattern)
    {
        QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget);
        if (!ed)
            return;

        // Clear previous highlights.
        ed->selectAll();
        QTextCursor cur = ed->textCursor();
        QTextCharFormat fmt = cur.charFormat();
        fmt.setBackground(Qt::transparent);
        cur.setCharFormat(fmt);

        // Highlight matches.
        QTextDocument *doc = ed->document();
        QRegExp re(pattern);
        cur = doc->find(re);

        int a = cur.position();
        while ( !cur.isNull() ) {
            if ( cur.hasSelection() ) {
                fmt.setBackground(Qt::yellow);
                cur.setCharFormat(fmt);
            } else {
                cur.movePosition(QTextCursor::NextCharacter);
            }
            cur = doc->find(re, cur);
            int b = cur.position();
            if (a == b) {
                cur.movePosition(QTextCursor::NextCharacter);
                cur = doc->find(re, cur);
                b = cur.position();
                if (a == b) break;
            }
            a = b;
        }
    }
void MainWindow::addParagraph(const QString &paragraph){
    if (paragraph.isEmpty())
        return;
    QTextDocument *document = chatbox->document();
    QTextCursor cursor = document->find(tr("Yours sincerely,"));
    if (cursor.isNull())
        return;
    cursor.beginEditBlock();
    cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2);
    cursor.insertBlock();
    cursor.insertText(paragraph);
    cursor.insertBlock();
    cursor.endEditBlock();

}
Example #22
0
void BaseEditor::findFirstOccurrance(const QString &text, QTextDocument::FindFlags qff,
                                     bool isRE, bool init, bool isSetTextCusor)
{
    if (!finded)
        return;
    QRegExp re(text);
    QTextDocument *doc = document();
    QTextCursor currentCursor = textCursor();
    QTextCursor firstCursor;
    QTextEdit::ExtraSelection es;
    if(!init || prevFindCursor.isNull())
    {
        QTextCursor startCursor;
        if(qff&QTextDocument::FindBackward && !prevFindCursor.isNull())
        {
            prevFindCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor,
                      abs(prevFindCursor.selectionStart()-prevFindCursor.selectionEnd()));
        }
        if(prevFindCursor.isNull())
            startCursor = currentCursor;
        else
            startCursor = prevFindCursor;
        firstCursor = isRE ? doc->find(re, startCursor, qff):
                             doc->find(text, startCursor, qff);
    } else {
        firstCursor = isRE ? doc->find(re, prevFindCursor.selectionStart(), qff):
                             doc->find(text, prevFindCursor.selectionStart(), qff);
    }
    if(firstCursor.isNull())
    {
        QTextCursor wholeCursor(doc);
        if(qff & QTextDocument::FindBackward)
            wholeCursor.movePosition(QTextCursor::End);
        firstCursor = isRE ? doc->find(re, wholeCursor, qff):
                             doc->find(text, wholeCursor, qff);
    }
    if(firstCursor.isNull())
    {
        prevFindCursor = firstCursor;
        return;
    }
    es.cursor = firstCursor;
    QTextCharFormat f;
    f.setBackground(Qt::blue);
    f.setForeground(Qt::white);
    es.format = f;
    currentFindSelection.clear();
    currentFindSelection.append(es);
    prevFindCursor = firstCursor;
    firstCursor.clearSelection();
    if(isSetTextCusor)
        setTextCursor(firstCursor);
    ensureCursorVisible();
    updateExtraSelection();
}
//! [6] //! [7]
void TextFinder::on_findButton_clicked()
{
    QString searchString = ui_lineEdit->text();
    QTextDocument *document = ui_textEdit->document();

    bool found = false;

    if (isFirstTime == false)
        document->undo();

    if (searchString.isEmpty()) {
        QMessageBox::information(this, tr("Empty Search Field"),
                "The search field is empty. Please enter a word and click Find.");
    } else {

        QTextCursor highlightCursor(document);
        QTextCursor cursor(document);

        cursor.beginEditBlock();
//! [6]

        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setForeground(Qt::red);

        while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
            highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);

            if (!highlightCursor.isNull()) {
                found = true;
                highlightCursor.movePosition(QTextCursor::WordRight,
                                       QTextCursor::KeepAnchor);
                highlightCursor.mergeCharFormat(colorFormat);
            }
        }

//! [8]
        cursor.endEditBlock();
//! [7] //! [9]
        isFirstTime = false;

        if (found == false) {
            QMessageBox::information(this, tr("Word Not Found"),
                "Sorry, the word cannot be found.");
        }
    }
}
Example #24
0
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit();

    QTextCursor cursor(editor->textCursor());
    cursor.movePosition(QTextCursor::Start);

    QTextCharFormat plainFormat(cursor.charFormat());
    QTextCharFormat colorFormat = plainFormat;
    colorFormat.setForeground(Qt::red);

    cursor.insertText(tr("Text can be displayed in a variety of "
                                  "different character "
                                  "formats. "), plainFormat);
    cursor.insertText(tr("We can emphasize text by making it "));
    cursor.insertText(tr("italic, give it a different color "));
    cursor.insertText(tr("to the default text color, underline it, "));
    cursor.insertText(tr("and use many other effects."));

    QString searchString = tr("text");

    QTextDocument *document = editor->document();
//! [0]
    QTextCursor newCursor(document);

    while (!newCursor.isNull() && !newCursor.atEnd()) {
        newCursor = document->find(searchString, newCursor);

        if (!newCursor.isNull()) {
            newCursor.movePosition(QTextCursor::WordRight,
                                   QTextCursor::KeepAnchor);

            newCursor.mergeCharFormat(colorFormat);
        }
//! [0] //! [1]
    }
//! [1]

    editor->setWindowTitle(tr("Text Document Find"));
    editor->resize(320, 480);
    editor->show();
    return app.exec();
}
Example #25
0
void TextEdit::findButton(const QRegExp searchString)
{
    QTextDocument *document = textEdit->document();
    if (isFirstTime == false)
        document->undo();
    highLightRows.clear();
    curHighlightRow = -1;
    if (searchString.isEmpty())
    {
        return;
    }
    else
    {
        QTextCursor highlightCursor(document);
        QTextCursor cursor(document);
        cursor.beginEditBlock();
        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setBackground(Qt::yellow);

        //qDebug() << searchString.pattern().size();
        while (!highlightCursor.isNull() && !highlightCursor.atEnd())
        {
            //highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);
            highlightCursor = document->find(searchString, highlightCursor);

            if (!highlightCursor.isNull())
            {
                //highlightCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
                //highlightCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, searchString.pattern().size());
                //highlightCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
                highlightCursor.movePosition(QTextCursor::NoMove, QTextCursor::KeepAnchor);
                highlightCursor.mergeCharFormat(colorFormat);
                if(!highLightRows.contains(highlightCursor.blockNumber()))
                       highLightRows.append(highlightCursor.blockNumber());
            }
        }
        cursor.endEditBlock();
        isFirstTime = false;
        if(!highLightRows.isEmpty())
            curHighlightRow = highLightRows[0];
        select_cur_line();
    }
}
Example #26
0
void Notepad::on_searchEdit_returnPressed()
{
    QString searchString = ui->searchEdit->text();
    QTextDocument *document = ui->notepadTextEdit->document();

    bool found = false;

    if (isFirstTime == false)
        document->undo();

    if (!searchString.isEmpty()) {

        QTextCursor highlightCursor(document);
        QTextCursor cursor(document);

        cursor.beginEditBlock();

        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setForeground(Qt::red);

        while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
            highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);

            if (!highlightCursor.isNull()) {
                found = true;
                highlightCursor.movePosition(QTextCursor::WordRight,
                                       QTextCursor::KeepAnchor);
                highlightCursor.mergeCharFormat(colorFormat);
            }
        }

        cursor.endEditBlock();
        isFirstTime = false;

        /*
        if (found == false) {
            QMessageBox::information(this, tr("Word Not Found"),
                "Sorry, the word cannot be found.");
        }
        */
    }
}
Example #27
0
/*!
 *  \fn void TextEdit::highlight(const QString &findString, QTextDocument::FindFlags options)
 *  Highlight texts that match the find query in document.
 *
 *  \param &findString  text to be find in document.
 *  \param options      QTextDocument::FindFlags find options.
 */
void TextEdit::highlight(const QString &findString, QTextDocument::FindFlags options)
{
    clearFind();
    QTextDocument *document = this->document();
    QTextCursor highlightCursor(document);
    QTextCharFormat colorFormat(highlightCursor.charFormat());
    colorFormat.setForeground(Qt::white);
    QPalette a;

    QBrush back(a.brush(QPalette::Link).color());//.darker(120));
    colorFormat.setBackground(back);

    while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
        highlightCursor = document->find(findString, highlightCursor, options);

        if (!highlightCursor.isNull()) {
            highlightCursor.setCharFormat(colorFormat);
        }
    }
}
Example #28
0
void MainWindow::on_pb_search_clicked()
{
    QTextDocument *document = textBrowser->document();
    QString filter = m_search->text();

    if (!filter.isEmpty()) {
        QTextCursor highlightCursor(document);
        QTextCursor cursor(document);

        //***************开始***************
        cursor.beginEditBlock();

        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setBackground(m_Pallette->search_bg_color);
        colorFormat.setForeground(m_Pallette->error_fg_color);

        QFlag flag = 0;

        if(cb_search_case->isChecked()){
            flag = flag | QTextDocument::FindCaseSensitively;
        }

        if(cb_wholeWord->isChecked()){
            flag = flag | QTextDocument::FindWholeWords;
        }


        while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
            highlightCursor = document->find(filter, highlightCursor, flag);

            if (!highlightCursor.isNull()) {
                highlightCursor.mergeCharFormat(colorFormat);
            }
        }

        cursor.endEditBlock();
        //***************结束***************
    }

}
bool ReportGenerator::generatePDF(QString templateFilename, QString outputAbsolutePath)
{
    QFile templateFile(":/reports/Reports/"+templateFilename);

    if(!templateFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return false;
    }

    QTextStream templateStream(&templateFile);

    QTextDocument document;
    document.setHtml(templateStream.readAll());

    QList<QString> keys = this->vars.keys();
    QList<QString>::iterator keysIt;

    for(keysIt = keys.begin(); keysIt != keys.end(); ++keysIt)
    {
        QTextCursor cursor(&document);
        while(!cursor.isNull())
        {
            cursor = document.find("%"+(*keysIt)+"%", cursor);
            cursor.insertHtml(this->vars[(*keysIt)]);
        }
    }

    QPrinter* printer = new QPrinter(QPrinter::HighResolution);
    printer->setPageSize(QPrinter::A4);
    printer->setOutputFormat(QPrinter::PdfFormat);

    printer->setOutputFileName(outputAbsolutePath);

    WebViewPrinter* wv = new WebViewPrinter();
    wv->printer = printer;

    wv->setHtml(document.toHtml());

    return true;

}
Example #30
0
void Notepad::on_searchEdit_textChanged(const QString &arg1)
{
    QString searchString = ui->searchEdit->text();
    QTextDocument *document = ui->notepadTextEdit->document();

    bool found = false;

    if (isFirstTime == false)
        document->undo();

    if (!searchString.isEmpty()) {

        QTextCursor highlightCursor(document);
        QTextCursor cursor(document);

        cursor.beginEditBlock();

        QTextCharFormat plainFormat(highlightCursor.charFormat());
        QTextCharFormat colorFormat = plainFormat;
        colorFormat.setForeground(Qt::red);

        while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
            highlightCursor = document->find(searchString, highlightCursor);

            if (!highlightCursor.isNull()) {
                found = true;
                //highlightCursor.movePosition(QTextCursor::WordRight,
                //                       QTextCursor::KeepAnchor);
                highlightCursor.mergeCharFormat(colorFormat);
            }
        }

        cursor.endEditBlock();
        isFirstTime = false;
    }
}