本文整理汇总了C++中QPlainTextEdit::textCursor方法的典型用法代码示例。如果您正苦于以下问题:C++ QPlainTextEdit::textCursor方法的具体用法?C++ QPlainTextEdit::textCursor怎么用?C++ QPlainTextEdit::textCursor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QPlainTextEdit
的用法示例。
在下文中一共展示了QPlainTextEdit::textCursor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: updateBlockSelection
void FakeVimProxy::updateBlockSelection() {
QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(m_widget);
if (!ed)
return;
requestSetBlockSelection(ed->textCursor());
}
示例2: find
int QScriptDebuggerCodeView::find(const QString &exp, int options)
{
Q_D(QScriptDebuggerCodeView);
QPlainTextEdit *ed = (QPlainTextEdit*)d->editor;
QTextCursor cursor = ed->textCursor();
if (options & 0x100) {
// start searching from the beginning of selection
if (cursor.hasSelection()) {
int len = cursor.selectedText().length();
cursor.clearSelection();
cursor.setPosition(cursor.position() - len);
ed->setTextCursor(cursor);
}
options &= ~0x100;
}
int ret = 0;
if (ed->find(exp, QTextDocument::FindFlags(options))) {
ret |= 0x1;
} else {
QTextCursor curse = cursor;
curse.movePosition(QTextCursor::Start);
ed->setTextCursor(curse);
if (ed->find(exp, QTextDocument::FindFlags(options)))
ret |= 0x1 | 0x2;
else
ed->setTextCursor(cursor);
}
return ret;
}
示例3: file
QTextDocument *ReplaceDocument::fileDocument(const QString &fileName, QTextCursor &cursor, bool &crlf)
{
LiteApi::IEditor *editor = m_liteApp->editorManager()->findEditor(fileName,true);
if (editor) {
QPlainTextEdit *ed = LiteApi::getPlainTextEdit(editor);
if (ed) {
cursor = ed->textCursor();
return ed->document();
}
}
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
QString text = QString::fromUtf8(data);
int lf = text.indexOf('\n');
if (lf <= 0) {
crlf = false;
} else {
lf = text.indexOf(QRegExp("[^\r]\n"),lf-1);
if (lf >= 0) {
crlf = false;
} else {
crlf = true;
}
}
m_document = new QTextDocument(text);
cursor = QTextCursor(m_document);
return m_document;
}
return 0;
}
示例4: sinkMessage
void MessagesDialog::sinkMessage( const MsgEvent *msg )
{
QMutexLocker locker( &messageLocker );
QPlainTextEdit *messages = ui.messages;
/* Only scroll if the viewport is at the end.
Don't bug user by auto-changing/losing viewport on insert(). */
bool b_autoscroll = ( messages->verticalScrollBar()->value()
+ messages->verticalScrollBar()->pageStep()
>= messages->verticalScrollBar()->maximum() );
/* Copy selected text to the clipboard */
if( messages->textCursor().hasSelection() )
messages->copy();
/* Fix selected text bug */
if( !messages->textCursor().atEnd() ||
messages->textCursor().anchor() != messages->textCursor().position() )
messages->moveCursor( QTextCursor::End );
/* Start a new logic block so we can hide it on-demand */
messages->textCursor().insertBlock();
QString buf = QString( "<i><font color='darkblue'>%1</font>" ).arg( msg->module );
switch ( msg->priority )
{
case VLC_MSG_INFO:
buf += "<font color='blue'> info: </font>";
break;
case VLC_MSG_ERR:
buf += "<font color='red'> error: </font>";
break;
case VLC_MSG_WARN:
buf += "<font color='green'> warning: </font>";
break;
case VLC_MSG_DBG:
default:
buf += "<font color='grey'> debug: </font>";
break;
}
/* Insert the prefix */
messages->textCursor().insertHtml( buf /* + "</i>" */ );
/* Insert the message */
messages->textCursor().insertHtml( msg->text );
/* Pass the new message thru the filter */
QTextBlock b = messages->document()->lastBlock();
b.setVisible( matchFilter( b.text() ) );
/* Tell the QTextDocument to recompute the size of the given area */
messages->document()->markContentsDirty( b.position(), b.length() );
if ( b_autoscroll ) messages->ensureCursorVisible();
}
示例5: checkSelection
//-------------------------------------------------------------------------
void MainWindow::checkSelection()
{
QPlainTextEdit *sourceCode = getCurrentText();
bool bHasSelect = sourceCode->textCursor().hasSelection();
ui.actionCopy->setEnabled(bHasSelect);
ui.actionCut->setEnabled(bHasSelect);
ui.actionDelete->setEnabled(bHasSelect);
ui.actionUndo->setEnabled(sourceCode->document()->isUndoAvailable());
ui.actionRedo->setEnabled(sourceCode->document()->isRedoAvailable());
}
示例6: openUrlFile
void LiteDoc::openUrlFile(const QUrl &url)
{
QFileInfo info(url.toLocalFile());
if (!info.exists()) {
info.setFile(url.path());
}
QString ext = info.suffix().toLower();
if (ext == "html") {
QFile file(info.filePath());
if (file.open(QIODevice::ReadOnly)) {
QByteArray ba = file.readAll();
file.close();
if (info.fileName().compare("docs.html",Qt::CaseInsensitive) == 0) {
updateHtmlDoc(url,ba,QString(),false);
} else {
updateHtmlDoc(url,ba);
}
}
} else if (ext == "md") {
QFile file(info.filePath());
if (file.open(QIODevice::ReadOnly)) {
QByteArray ba = mdtohtml(file.readAll());
updateHtmlDoc(url,ba);
}
} else if (ext == "go") {
LiteApi::IEditor *editor = m_liteApp->fileManager()->openEditor(info.filePath());
if (editor) {
editor->setReadOnly(true);
QPlainTextEdit *ed = LiteApi::findExtensionObject<QPlainTextEdit*>(editor,"LiteApi.QPlainTextEdit");
if (ed && url.hasQueryItem("s")) {
QStringList pos = url.queryItemValue("s").split(":");
if (pos.length() == 2) {
bool ok = false;
int begin = pos.at(0).toInt(&ok);
if (ok) {
QTextCursor cur = ed->textCursor();
cur.setPosition(begin);
ed->setTextCursor(cur);
ed->centerCursor();
}
}
}
}
} else if (ext == "pdf") {
QDesktopServices::openUrl(info.filePath());
} else {
QFile file(info.filePath());
if (file.open(QIODevice::ReadOnly)) {
QByteArray ba = file.readAll();
updateTextDoc(url,ba,info.fileName());
}
}
}
示例7:
QTextCursor QsvTextOperationsWidget::getTextCursor()
{
QTextCursor searchCursor;
QTextEdit *t = qobject_cast<QTextEdit*>(parent());
if (t) {
searchCursor = t->textCursor();
} else {
QPlainTextEdit *pt = qobject_cast<QPlainTextEdit*>(parent());
if (pt) {
searchCursor = pt->textCursor();
}
}
return searchCursor;
}
示例8: insertTextAtLine
//-------------------------------------------------------------------------
void MainWindow::insertTextAtLine(int lineNr, const char* pText)
{
QPlainTextEdit *sourceCode = getCurrentSourceCode();
if (lineNr >= 0)
{
QTextCursor sourceCursor = sourceCode->textCursor();
sourceCursor.setPosition(sourceCode->document()->findBlockByLineNumber(lineNr).position());
sourceCode->setTextCursor(sourceCursor);
}
else
{
sourceCode->moveCursor(QTextCursor::StartOfLine);
}
sourceCode->insertPlainText(QString::fromUtf8(pText));
}
示例9: searchDocument
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);
}
示例10: moveCursorToEndOfName
// TODO: Can this be improved? This code is ripped from CppEditor, especially CppElementEvaluater
// We cannot depend on this since CppEditor plugin code is internal and requires building the implementation files ourselves
CPlusPlus::Symbol *AnalyzerUtils::findSymbolUnderCursor()
{
EditorManager *editorManager = EditorManager::instance();
if (!editorManager)
return 0;
IEditor *editor = editorManager->currentEditor();
if (!editor)
return 0;
TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor *>(editor);
if (!textEditor)
return 0;
TextEditor::BaseTextEditorWidget *editorWidget = qobject_cast<TextEditor::BaseTextEditorWidget *>(editor->widget());
if (!editorWidget)
return 0;
QPlainTextEdit *ptEdit = qobject_cast<QPlainTextEdit *>(editor->widget());
if (!ptEdit)
return 0;
QTextCursor tc;
tc = ptEdit->textCursor();
int line = 0;
int column = 0;
const int pos = tc.position();
editorWidget->convertPosition(pos, &line, &column);
const CPlusPlus::Snapshot &snapshot = CPlusPlus::CppModelManagerInterface::instance()->snapshot();
CPlusPlus::Document::Ptr doc = snapshot.document(editor->document()->fileName());
QTC_ASSERT(doc, return 0)
// fetch the expression's code
CPlusPlus::ExpressionUnderCursor expressionUnderCursor;
moveCursorToEndOfName(&tc);
const QString &expression = expressionUnderCursor(tc);
CPlusPlus::Scope *scope = doc->scopeAt(line, column);
CPlusPlus::TypeOfExpression typeOfExpression;
typeOfExpression.init(doc, snapshot);
const QList<CPlusPlus::LookupItem> &lookupItems = typeOfExpression(expression.toUtf8(), scope);
if (lookupItems.isEmpty())
return 0;
const CPlusPlus::LookupItem &lookupItem = lookupItems.first(); // ### TODO: select best candidate.
return lookupItem.declaration();
}
示例11: setVisible
void FindEditor::setVisible(bool b)
{
this->m_widget->setVisible(b);
if (b) {
LiteApi::IEditor *editor = m_liteApp->editorManager()->currentEditor();
if (editor) {
QString text;
QPlainTextEdit *ed = LiteApi::findExtensionObject<QPlainTextEdit*>(editor,"LiteApi.QPlainTextEdit");
if (ed) {
text = ed->textCursor().selectedText();
}
if (!text.isEmpty()) {
this->m_findEdit->setText(text);
}
this->m_findEdit->setFocus();
this->m_findEdit->selectAll();
}
}
updateCurrentEditor(m_liteApp->editorManager()->currentEditor());
}
示例12: findHelper
void FindEditor::findHelper(FindOption *opt)
{
bool bFocus = m_findEdit->hasFocus();
LiteApi::IEditor *editor = m_liteApp->editorManager()->currentEditor();
if (!editor) {
return;
}
LiteApi::ITextEditor *textEditor = LiteApi::getTextEditor(editor);
QTextCursor find;
if (textEditor) {
QPlainTextEdit *ed = LiteApi::getPlainTextEdit(editor);
if (ed) {
find = findEditor(ed->document(),ed->textCursor(),opt);
if (!find.isNull()) {
ed->setTextCursor(find);
}
}
} else {
QTextBrowser *ed = LiteApi::findExtensionObject<QTextBrowser*>(editor,"LiteApi.QTextBrowser");
if (ed) {
find = findEditor(ed->document(),ed->textCursor(),opt);
if (!find.isNull()) {
ed->setTextCursor(find);
}
}
}
if (find.isNull()) {
m_status->setText(tr("Not find"));
} else {
m_status->setText(QString("Ln:%1 Col:%2").
arg(find.blockNumber()+1).
arg(find.columnNumber()+1));
}
if (bFocus) {
m_findEdit->setFocus();
} else if (textEditor) {
textEditor->onActive();
}
}
示例13: highlightMatches
void FakeVimProxy::highlightMatches(const QString &pattern) {
QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(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();
}
示例14: replaceHelper
void FindEditor::replaceHelper(LiteApi::ITextEditor *editor, FindOption *opt, int replaceCount)
{
bool bFocus = m_replaceEdit->hasFocus();
QPlainTextEdit *ed = LiteApi::getPlainTextEdit(editor);
if (!ed) {
return;
}
QTextCursor find;
QTextCursor cursor = ed->textCursor();
int line = cursor.blockNumber();
int col = cursor.columnNumber();
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
if (opt->matchCase) {
cs = Qt::CaseSensitive;
}
if ( cursor.hasSelection() ) {
QString text = cursor.selectedText();
if (opt->useRegexp) {
if (text.indexOf(QRegExp(opt->findText,cs),0) != -1) {
find = cursor;
}
} else {
if (text.indexOf(opt->findText,0,cs) != -1) {
find = cursor;
}
}
}
int number = 0;
bool wrap = opt->wrapAround;
do {
if (!find.isNull()) {
number++;
find.beginEditBlock();
QString text = find.selectedText();
if (opt->useRegexp) {
text.replace(QRegExp(opt->findText,cs),opt->replaceText);
} else {
text.replace(opt->findText,opt->replaceText);
}
find.removeSelectedText();
find.insertText(text);
find.endEditBlock();
ed->setTextCursor(find);
}
cursor = ed->textCursor();
find = findEditor(ed->document(),cursor,opt,false);
if (find.isNull() && wrap) {
wrap = false;
find = findEditor(ed->document(),cursor,opt,true);
}
if (opt->wrapAround && !wrap) {
if (find.blockNumber() > line ||
(find.blockNumber() >= line && find.columnNumber() > col) ) {
break;
}
}
if (replaceCount != -1 && number >= replaceCount) {
if (!find.isNull()) {
ed->setTextCursor(find);
m_status->setText(QString("Ln:%1 Col:%2").
arg(find.blockNumber()+1).
arg(find.columnNumber()+1));
} else {
m_status->setText(tr("Not find"));
}
break;
}
} while(!find.isNull());
if (replaceCount == -1) {
m_status->setText(QString("Replace:%1").arg(number));
}
if (bFocus) {
m_replaceEdit->setFocus();
} else if (editor) {
editor->onActive();
}
}
示例15: replaceHelper
void FindEditor::replaceHelper(LiteApi::ITextEditor *editor, FindOption *opt, int replaceCount)
{
bool bFocus = m_replaceEdit->hasFocus();
QPlainTextEdit *ed = LiteApi::getPlainTextEdit(editor);
if (!ed) {
return;
}
QTextCursor find;
QTextCursor cursor = ed->textCursor();
int startLine = cursor.blockNumber();
int startColumn = cursor.columnNumber();
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
if (opt->matchCase) {
cs = Qt::CaseSensitive;
}
int from = cursor.position();
if ( cursor.hasSelection() ) {
// QString text = cursor.selectedText();
// if (opt->useRegexp) {
// if (text.indexOf(QRegExp(opt->findText,cs),0) != -1) {
// find = cursor;
// }
// } else {
// if (text.indexOf(opt->findText,0,cs) != -1) {
// find = cursor;
// }
// }
from = cursor.selectionStart();
startColumn -= cursor.selectedText().length();
}
int number = 0;
bool wrap = opt->wrapAround;
ed->textCursor().beginEditBlock();
do {
if (!find.isNull()) {
number++;
//find.beginEditBlock();
QString text = find.selectedText();
bool changed = find.blockNumber() == startLine && find.columnNumber() < startColumn;
if (changed) {
startColumn -= text.length();
}
if (opt->useRegexp) {
text.replace(QRegExp(opt->findText,cs),opt->replaceText);
} else {
text.replace(QRegExp(opt->findText,cs,QRegExp::FixedString),opt->replaceText);
}
find.removeSelectedText();
from = find.position()+ text.length();
find.insertText(text);
//find.endEditBlock();
//ed->setTextCursor(find);
if (changed) {
startColumn += text.length();
}
}
//cursor = ed->textCursor();
find = findEditorHelper(ed->document(),from,opt,false);
if (find.isNull() && wrap) {
wrap = false;
find = findEditorHelper(ed->document(),0,opt,true);
}
if (!find.isNull() && opt->wrapAround && !wrap) {
if (find.blockNumber() > startLine ||
(find.blockNumber() == startLine && (find.columnNumber()-find.selectedText().length()) >= startColumn) ) {
break;
}
}
if (replaceCount != -1 && number >= replaceCount) {
if (!find.isNull()) {
ed->setTextCursor(find);
m_status->setText(QString("Ln:%1 Col:%2").
arg(find.blockNumber()+1).
arg(find.columnNumber()+1));
} else {
m_status->setText(tr("Not found"));
}
break;
}
} while(!find.isNull());
ed->textCursor().endEditBlock();
if (replaceCount == -1) {
m_status->setText(QString("Replace:%1").arg(number));
}
if (bFocus) {
m_replaceEdit->setFocus();
} else if (editor) {
editor->onActive();
}
}