本文整理汇总了C++中QDocumentCursor::replaceSelectedText方法的典型用法代码示例。如果您正苦于以下问题:C++ QDocumentCursor::replaceSelectedText方法的具体用法?C++ QDocumentCursor::replaceSelectedText怎么用?C++ QDocumentCursor::replaceSelectedText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QDocumentCursor
的用法示例。
在下文中一共展示了QDocumentCursor::replaceSelectedText方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: alignTableCols
void LatexTables::alignTableCols(QDocumentCursor &cur){
QString text = getTableText(cur);
if (!cur.hasSelection()) return;
QString indentation = cur.selectionStart().line().indentation();
// split off \begin and \end parts
int index = text.indexOf("\\begin{")+6;
int cellsStart;
QList<CommandArgument> args = getCommandOptions(text, index, &cellsStart);
if (args.count() < 2) return;
QString tableType = args.at(0).value;
// assume alignment in second arg except for the following environments (which have it in the third one)
QString alignment;
if (tabularNames.contains(tableType)) {
alignment = args.at(1).value;
} else if (tabularNamesWithOneOption.contains(tableType)) {
if (args.count()<3) alignment = ""; // incomplete definition -> fall back to defaults
else alignment = args.at(2).value;
} else return; // not a registered table environment
int cellsEnd = text.indexOf("\\end{"+tableType);
if (cellsEnd<0) return;
QString beginPart = text.left(cellsStart);
QString endPart = text.mid(cellsEnd);
LatexTableModel ltm;
ltm.setContent(text.mid(cellsStart, cellsEnd-cellsStart));
QStringList l_defs=splitColDef(alignment);
simplifyColDefs(l_defs);
QStringList content(ltm.getAlignedLines(l_defs));
QString result = beginPart + '\n';
for (int i=0; i<content.count(); i++) {
result.append(indentation + content.at(i));
}
result.append(indentation + endPart);
cur.replaceSelectedText(result);
}
示例2: searchReplaceFunction
QScriptValue searchReplaceFunction(QScriptContext *context, QScriptEngine *engine, bool replace){
QEditor *editor = qobject_cast<QEditor*>(context->thisObject().toQObject());
//read arguments
SCRIPT_REQUIRE(editor, "invalid object");
SCRIPT_REQUIRE(!replace || context->argumentCount()>=2, "at least two arguments are required");
SCRIPT_REQUIRE(context->argumentCount()>=1, "at least one argument is required");
SCRIPT_REQUIRE(context->argumentCount()<=4, "too many arguments");
SCRIPT_REQUIRE(context->argument(0).isString()||context->argument(0).isRegExp(), "first argument must be a string or regexp");
QDocumentSearch::Options flags = QDocumentSearch::Silent;
bool global = false, caseInsensitive = false;
QString searchFor;
if (context->argument(0).isRegExp()) {
flags |= QDocumentSearch::RegExp;
QRegExp r = context->argument(0).toRegExp();
searchFor = r.pattern();
caseInsensitive = r.caseSensitivity() == Qt::CaseInsensitive;
Q_ASSERT(caseInsensitive == context->argument(0).property("ignoreCase").toBool()); //check assumption about javascript core
global = context->argument(0).property("global").toBool();
} else searchFor = context->argument(0).toString();
QScriptValue handler;
QDocumentCursor scope = editor->document()->cursor(0,0,editor->document()->lineCount(),0);
int handlerCount = 0;
for (int i=1; i<context->argumentCount();i++)
if (context->argument(i).isString() || context->argument(i).isFunction()) handlerCount++;
SCRIPT_REQUIRE(handlerCount <= (replace?2:1), "too many string or function arguments");
for (int i=1; i<context->argumentCount();i++) {
QScriptValue a = context->argument(i);
if (a.isFunction()) {
SCRIPT_REQUIRE(!handler.isValid(), "Multiple callbacks");
handler = a;
} else if (a.isString()) {
if (!replace || handlerCount > 1) {
QString s = a.toString().toLower();
global = s.contains("g");
caseInsensitive = s.contains("i");
if (s.contains("w")) flags |= QDocumentSearch::WholeWords;
} else {
SCRIPT_REQUIRE(!handler.isValid(), "Multiple callbacks");
handler = a;
}
handlerCount--;
} else if (a.isNumber()) flags |= QDocumentSearch::Options((int)a.toNumber());
else if (a.isObject()) scope = cursorFromValue(a);
else SCRIPT_REQUIRE(false, "Invalid argument");
}
SCRIPT_REQUIRE(handler.isValid() || !replace, "No callback given");
if (!caseInsensitive) flags |= QDocumentSearch::CaseSensitive;
//search/replace
QDocumentSearch search(editor, searchFor, flags);
search.setScope(scope);
if (replace && handler.isString()) {
search.setReplaceText(handler.toString());
search.setOption(QDocumentSearch::Replace,true);
return search.next(false, global, false, false);
}
if (!handler.isValid())
return search.next(false,global,true,false);
int count=0;
while (search.next(false, false, true, false) && search.cursor().isValid()) {
count++;
QDocumentCursor temp = search.cursor();
QScriptValue cb = handler.call(QScriptValue(), QScriptValueList() << engine->newQObject(&temp));
if (replace && cb.isValid()){
QDocumentCursor tmp = search.cursor();
tmp.replaceSelectedText(cb.toString());
search.setCursor(tmp.selectionEnd());
}
if (!global) break;
}
return count;
}