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(); }
/*! * Clear highlight of the found text. */ void TextEdit::clearFind() { QTextDocument *document = this->document(); QTextCursor highlightCursor(document); QTextCharFormat plainFormat(highlightCursor.charFormat()); plainFormat.setBackground(Qt::white); plainFormat.setForeground(Qt::black); highlightCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor); highlightCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); highlightCursor.setCharFormat(plainFormat); }
void CodeEdit::searchText(const QString &text, bool caseSenstive) { m_searchText = text; //ui->results->setVisible(!m_searchText.isEmpty()); // TODO is this correct? document->undo(); m_startPositions.clear(); if (m_searchText.isEmpty()) { updateResultLabel(-1); return; } 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(m_searchText, highlightCursor, caseSenstive ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlags()); if (!highlightCursor.isNull()) { highlightCursor.mergeCharFormat(colorFormat); m_startPositions.append(highlightCursor.position() - m_searchText.length()); } } cursor.endEditBlock(); if (!m_startPositions.isEmpty()) { markSearchResult(0); } else { updateResultLabel(-1); } //ui->results->setVisible(!m_startPositions.isEmpty()); }
//! [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."); } } }
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(); } }
//search function void DocumentWidget::s_search() { QString searchString=m_searchLine->text(); QTextDocument *document=m_txt->document(); bool found = false; if (isFirstTime == false) document->undo(); if (searchString.isEmpty()) { QMessageBox::information(this, tr("Empty Search Field"), "Please enter a word."); isFirstTime=true; } else { 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."); isFirstTime = true; } } m_searchLine->clear(); }
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."); } */ } }
/*! * \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); } } }
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; } }
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(); //***************结束*************** } }
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; } }
void TextRoom::spellCheck() { textEdit->document()->blockSignals(true); QString textVar = textEdit->document()->toPlainText(); textVar.replace(" ", "+"); textVar.replace("\n", "+"); textVar.replace("\t", "+"); textVar.replace("\r", "+"); textVar.replace(".", "+"); textVar.replace(",", "+"); textVar.replace(";", "+"); textVar.replace("!", "+"); textVar.replace("?", "+"); textVar.replace("(", "+"); textVar.replace(")", "+"); textVar.replace(":", "+"); textVar.replace("@", "+"); textVar.replace("&", "+"); textVar.replace("$", "+"); textVar.replace("%", "+"); textVar.replace("\"", "+"); QStringList wordList = textVar.split("+", QString::SkipEmptyParts); QString affFileName; QString dicFileName; #ifdef Q_OS_WIN32 affFileName = language+".aff"; dicFileName = language+".dic"; #elif defined(Q_OS_MACX) affFileName = resourcesDir+"/dict/"+language+".aff"; dicFileName = resourcesDir+"/dict/"+language+".dic"; #else affFileName = "/usr/share/hunspell/"+language+".aff"; dicFileName = "/usr/share/hunspell/"+language+".dic"; #endif pMS= new Hunspell(affFileName.toLocal8Bit(), dicFileName.toLocal8Bit()); QTextCharFormat highlightFormat; highlightFormat.setUnderlineColor(Qt::red); highlightFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline); QTextCharFormat defaultFormat; defaultFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); if ( !isHighlighted ) isHighlighted = true; else { isHighlighted = false; } QTextCursor findCursor(textEdit->document()); findCursor.movePosition(QTextCursor::Start); foreach (QString word, wordList) { QTextCursor highlightCursor(textEdit->document()->find(word, findCursor)); findCursor.setPosition(highlightCursor.position()); findCursor.movePosition(QTextCursor::EndOfWord); QByteArray ba = word.toUtf8(); char * wordChar = ba.data(); int correct = pMS->spell(wordChar); if ( !correct && isHighlighted ) highlightCursor.mergeCharFormat(highlightFormat); else { QTextCursor notUnderlined(textEdit->document()->find(" ", findCursor)); notUnderlined.mergeCharFormat(defaultFormat); highlightCursor.mergeCharFormat(defaultFormat); } }