本文整理汇总了C++中Tokens::count方法的典型用法代码示例。如果您正苦于以下问题:C++ Tokens::count方法的具体用法?C++ Tokens::count怎么用?C++ Tokens::count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tokens
的用法示例。
在下文中一共展示了Tokens::count方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: autoComplete
void Editor::autoComplete( const QString& item )
{
if( !d->autoCompleteEnabled || item.isEmpty() )
return;
int para = 0, curPos = 0;
getCursorPosition( ¶, &curPos );
QString subtext = text().left( curPos );
Tokens tokens = Evaluator::scan( subtext );
if( !tokens.valid() || tokens.count() < 1 )
return;
Token lastToken = tokens[ tokens.count()-1 ];
if( !lastToken.isIdentifier() )
return;
QStringList str = QStringList::split( ':', item );
blockSignals( true );
setSelection( 0, lastToken.pos(), 0, lastToken.pos()+lastToken.text().length() );
insert( str[0] );
blockSignals( false );
}
示例2: doMatchingRight
void Editor::doMatchingRight()
{
// Tokenize the expression.
const int currentPosition = textCursor().position();
// Check for left par.
QString subtext = text().right(text().length() - currentPosition);
Tokens tokens = m_evaluator->scan(subtext);
if (!tokens.valid() || tokens.count() < 1)
return;
Token firstToken = tokens.at(0);
// Left par?
if (firstToken.type() == Token::stxOpenPar && firstToken.pos() == 0) {
// Find the matching right par.
unsigned par = 1;
int k = 0;
Token matchToken;
int matchPosition = -1;
for (k = 1; k < tokens.count() && par > 0; ++k) {
const Token matchToken = tokens.at(k);
switch (matchToken.type()) {
case Token::stxOpenPar : ++par; break;
case Token::stxClosePar: --par; break;
default:;
}
matchPosition = matchToken.pos();
}
if (par == 0) {
QTextEdit::ExtraSelection hilite1;
hilite1.cursor = textCursor();
hilite1.cursor.setPosition(currentPosition+matchPosition);
hilite1.cursor.setPosition(currentPosition+matchPosition + 1, QTextCursor::KeepAnchor);
hilite1.format.setBackground(m_highlighter->colorForRole(SyntaxHighlighter::Matched));
QTextEdit::ExtraSelection hilite2;
hilite2.cursor = textCursor();
hilite2.cursor.setPosition(currentPosition+firstToken.pos());
hilite2.cursor.setPosition(currentPosition+firstToken.pos() + 1, QTextCursor::KeepAnchor);
hilite2.format.setBackground(m_highlighter->colorForRole(SyntaxHighlighter::Matched));
QList<QTextEdit::ExtraSelection> extras;
extras << hilite1;
extras << hilite2;
setExtraSelections(extras);
}
}
}
示例3: doMatchingRight
void Editor::doMatchingRight()
{
if( !d->syntaxHighlightEnabled ) return;
// tokenize the expression
int para = 0, curPos = 0;
getCursorPosition( ¶, &curPos );
// check for left par
QString subtext = text().right( text().length() - curPos );
Tokens tokens = Evaluator::scan( subtext );
if( !tokens.valid() ) return;
if( tokens.count()<1 ) return;
Token firstToken = tokens[ 0 ];
// left par ?
if( firstToken.isOperator() )
if( firstToken.asOperator() == Token::LeftPar )
if( firstToken.pos() == 0 )
{
// find the matching right par
unsigned par = 1;
unsigned int k = 0;
Token matchToken;
int matchPos = -1;
for( k = 1; k < tokens.count(); k++ )
{
if( par < 1 ) break;
Token matchToken = tokens[k];
if( matchToken.isOperator() )
{
if( matchToken.asOperator() == Token::LeftPar )
par++;
if( matchToken.asOperator() == Token::RightPar )
par--;
if( par == 0 ) matchPos = matchToken.pos();
}
}
if( matchPos >= 0 )
{
setSelection( 0, curPos+matchPos, 0, curPos+matchPos+1, 2 );
setSelection( 0, curPos+firstToken.pos(), 0, curPos+firstToken.pos()+1, 1 );
setCursorPosition( para, curPos );
}
}
}
示例4: doMatchingLeft
void Editor::doMatchingLeft()
{
// Tokenize the expression.
const int currentPosition = textCursor().position();
// Check for right par.
QString subtext = text().left(currentPosition);
Tokens tokens = m_evaluator->scan(subtext, Evaluator::NoAutoFix);
if (!tokens.valid() || tokens.count() < 1)
return;
Token lastToken = tokens.at(tokens.count() - 1);
// Right par?
if (lastToken.type() == Token::stxClosePar && lastToken.pos() == currentPosition - 1) {
// Find the matching left par.
unsigned par = 1;
int matchPosition = -1;
for (int i = tokens.count() - 2; i >= 0 && par > 0; --i) {
Token matchToken = tokens.at(i);
switch (matchToken.type()) {
case Token::stxOpenPar : --par; break;
case Token::stxClosePar: ++par; break;
default:;
}
matchPosition = matchToken.pos();
}
if (par == 0) {
QTextEdit::ExtraSelection hilite1;
hilite1.cursor = textCursor();
hilite1.cursor.setPosition(matchPosition);
hilite1.cursor.setPosition(matchPosition + 1, QTextCursor::KeepAnchor);
hilite1.format.setBackground(m_highlighter->colorForRole(SyntaxHighlighter::Matched));
QTextEdit::ExtraSelection hilite2;
hilite2.cursor = textCursor();
hilite2.cursor.setPosition(lastToken.pos());
hilite2.cursor.setPosition(lastToken.pos() + 1, QTextCursor::KeepAnchor);
hilite2.format.setBackground(m_highlighter->colorForRole(SyntaxHighlighter::Matched));
QList<QTextEdit::ExtraSelection> extras;
extras << hilite1;
extras << hilite2;
setExtraSelections(extras);
}
}
}
示例5: setActiveSubRegion
// Called by the cell tool when setting the active element with a cell location.
void CellEditor::setActiveSubRegion(int index)
{
index = qBound(0, index, (int)d->highlighter->rangeCount());
int counter = 0;
bool subRegion = false;
const Tokens tokens = d->highlighter->formulaTokens();
for (int i = 0; i < tokens.count(); ++i) {
const Token token = tokens[i];
switch (token.type()) {
case Token::Cell:
case Token::Range:
if (!subRegion) {
d->currentToken = i;
subRegion = true;
}
if (counter == index) {
setCursorPosition(token.pos() + token.text().length() + 1);
return;
}
++counter;
continue;
case Token::Operator:
if (token.asOperator() == Token::Semicolon) {
if (subRegion) {
continue;
}
}
subRegion = false;
continue;
default:
subRegion = false;
continue;
}
}
}
示例6: findMatchingBrace
int FormulaEditorHighlighter::findMatchingBrace(int pos)
{
int depth = 0;
int step = 0;
Tokens tokens = d->tokens;
//If this is a left brace we need to step forwards through the text to find the matching right brace,
//otherwise, it is a right brace so we need to step backwards through the text to find the matching left
//brace.
if (tokens.at(pos).asOperator() == Token::LeftPar)
step = 1;
else
step = -1;
for (int index = pos ; (index >= 0) && (index < (int) tokens.count()) ; index += step) {
if (tokens.at(index).asOperator() == Token::LeftPar)
depth++;
if (tokens.at(index).asOperator() == Token::RightPar)
depth--;
if (depth == 0) {
return index;
}
}
return -1;
}
示例7: highlightBlock
void SyntaxHighlighter::highlightBlock(const QString& text)
{
if (!Settings::instance()->syntaxHighlighting) {
setFormat(0, text.length(), colorForRole(Number));
return;
}
if (text.startsWith(QLatin1String("="))) {
setFormat(0, 1, colorForRole(Operator));
setFormat(1, text.length(), colorForRole(Result));
groupDigits(text, 1, text.length() - 1);
return;
}
Tokens tokens = Evaluator::instance()->scan(text);
for (int i = 0; i < tokens.count(); ++i) {
const Token& token = tokens.at(i);
const QString tokenText = token.text().toLower();
QStringList functionNames = FunctionRepo::instance()->getIdentifiers();
QColor color;
switch (token.type()) {
case Token::stxNumber:
case Token::stxUnknown:
color = colorForRole(Number);
break;
case Token::stxOperator:
color = colorForRole(Operator);
break;
case Token::stxSep:
color = colorForRole(Separator);
break;
case Token::stxOpenPar:
case Token::stxClosePar:
color = colorForRole(Parens);
break;
case Token::stxIdentifier:
color = colorForRole(Variable);
for (int i = 0; i < functionNames.count(); ++i)
if (functionNames.at(i).toLower() == tokenText)
color = colorForRole(Function);
break;
default:
break;
};
if (token.pos() >= 0) {
setFormat(token.pos(), token.text().length(), color);
if (token.type() == Token::stxNumber)
groupDigits(text, token.pos(), token.text().length());
}
}
}
示例8: autoComplete
void Editor::autoComplete(const QString& item)
{
if (!m_isAutoCompletionEnabled || item.isEmpty())
return;
const int currentPosition = textCursor().position();
const QString subtext = text().left(currentPosition);
const Tokens tokens = m_evaluator->scan(subtext);
if (!tokens.valid() || tokens.count() < 1)
return;
const Token lastToken = tokens.at(tokens.count() - 1);
if (!lastToken.isIdentifier())
return;
const QStringList str = item.split(':');
blockSignals(true);
QTextCursor cursor = textCursor();
cursor.setPosition(lastToken.pos());
cursor.setPosition(lastToken.pos() + lastToken.text().length(), QTextCursor::KeepAnchor);
setTextCursor(cursor);
insert(str.at(0));
blockSignals(false);
cursor = textCursor();
bool hasParensAlready;
if ((hasParensAlready = cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor))) {
QString nextChar = cursor.selectedText();
hasParensAlready = (nextChar == "(");
}
bool shouldAutoInsertParens = (FunctionRepo::instance()->find(str.at(0))
|| m_evaluator->hasUserFunction(str.at(0)))
&& !hasParensAlready;
if (shouldAutoInsertParens) {
insert(QString::fromLatin1("()"));
cursor = textCursor();
cursor.movePosition(QTextCursor::PreviousCharacter);
setTextCursor(cursor);
}
}
示例9: region
void CellEditor::Private::rebuildSelection()
{
// Do not react on selection changes, that update the formula's expression,
// because the selection gets already build based on the current formula.
selectionChangedLocked = true;
Sheet *const originSheet = selection->originSheet();
Map *const map = originSheet->map();
// Rebuild the reference selection by using the formula tokens.
Tokens tokens = highlighter->formulaTokens();
selection->update(); // set the old cursor dirty; updates the editors
selection->clear();
//A list of regions which have already been highlighted on the spreadsheet.
//This is so that we don't end up highlighting the same region twice in two different
//colors.
QSet<QString> alreadyUsedRegions;
int counter = 0;
for (int i = 0; i < tokens.count(); ++i) {
const Token token = tokens[i];
const Token::Type type = token.type();
if (type == Token::Cell || type == Token::Range) {
const Region region(token.text(), map, originSheet);
if (!region.isValid() || region.isEmpty()) {
continue;
}
if (alreadyUsedRegions.contains(region.name())) {
continue;
}
alreadyUsedRegions.insert(region.name());
const QRect range = region.firstRange();
Sheet *const sheet = region.firstSheet();
selection->initialize(range, sheet);
// Always append the next range by pointing behind the last item.
selection->setActiveSubRegion(++counter, 0);
}
}
// Set the active sub-region.
// Needs up-to-date tokens; QSyntaxHighlighter::rehighlight() gets called
// automatically on text changes, which does the update.
updateActiveSubRegion(highlighter->formulaTokens());
selectionChangedLocked = false;
}
示例10: autoCalcSelection
void Editor::autoCalcSelection()
{
if (!m_isAutoCalcEnabled)
return;
const QString str = m_evaluator->autoFix(textCursor().selectedText());
if (str.isEmpty())
return;
// Very short (just one token) and still no calculation, then skip.
if (!m_isAnsAvailable) {
const Tokens tokens = m_evaluator->scan(text());
if (tokens.count() < 2)
return;
}
// Too short even after autofix? Don't bother either.
const Tokens tokens = m_evaluator->scan(str);
if (tokens.count() < 2)
return;
// Same reason as above, do not update "ans".
m_evaluator->setExpression(str);
const HNumber num = m_evaluator->evalNoAssign();
if (m_evaluator->error().isEmpty()) {
if (num.isNan() && m_evaluator->isUserFunctionAssign()) {
// Result is not always available when assigning a user function.
const QString message = tr("Selection result: n/a");
emit autoCalcEnabled(message);
} else {
const QString message = tr("Selection result: <b>%1</b>").arg(NumberFormatter::format(num));
emit autoCalcEnabled(message);
}
} else
emit autoCalcEnabled(m_evaluator->error());
}
示例11: highlightParagraph
int EditorHighlighter::highlightParagraph ( const QString & text, int )
{
if( !editor->isSyntaxHighlightEnabled() )
{
setFormat( 0, text.length(), editor->colorGroup().text() );
return 0;
}
QStringList fnames = FunctionManager::instance()->functionList(FunctionManager::All);
fnames.sort(); // Sort list so we can bin search it.
Tokens tokens = Evaluator::scan( text );
for( unsigned i = 0; i < tokens.count(); i++ )
{
Token& token = tokens[i];
QString text = token.text().lower();
QFont font = editor->font();
QColor color = Qt::black;
switch( token.type() )
{
case Token::Number:
color = editor->highlightColor( Editor::Number );
break;
case Token::Identifier:
{
color = editor->highlightColor( Editor::Variable );
// XXX: QT 4: Replace this with qBinaryFind().
if( fnames.contains( text ) ) {
color = editor->highlightColor( Editor::FunctionName );
}
}
break;
case Token::Operator:
break;
default: break;
}
if( token.pos() >= 0 ) {
setFormat( token.pos(), token.text().length(), font, color );
}
}
return 0;
}
示例12: activeArgument
int ClangFunctionHintModel::activeArgument(const QString &prefix) const
{
int argnr = 0;
int parcount = 0;
SimpleLexer tokenize;
Tokens tokens = tokenize(prefix);
for (int i = 0; i < tokens.count(); ++i) {
const CPlusPlus::Token &tk = tokens.at(i);
if (tk.is(T_LPAREN))
++parcount;
else if (tk.is(T_RPAREN))
--parcount;
else if (! parcount && tk.is(T_COMMA))
++argnr;
}
if (parcount < 0)
return -1;
if (argnr != m_currentArg)
m_currentArg = argnr;
return argnr;
}
示例13: highlightBlock
void FormulaEditorHighlighter::highlightBlock(const QString& text)
{
// reset syntax highlighting
setFormat(0, text.length(), QApplication::palette().text().color());
// save the old ones to identify range changes
Tokens oldTokens = d->tokens;
// interpret the text as formula
// we accept invalid/incomplete formulas
Formula f;
d->tokens = f.scan(text);
QFont editorFont = document()->defaultFont();
QFont font;
uint oldRangeCount = d->rangeCount;
d->rangeCount = 0;
QList<QColor> colors = d->selection->colors();
QList<QString> alreadyFoundRanges;
Sheet *const originSheet = d->selection->originSheet();
Map *const map = originSheet->map();
for (int i = 0; i < d->tokens.count(); ++i) {
Token token = d->tokens[i];
Token::Type type = token.type();
switch (type) {
case Token::Cell:
case Token::Range: {
// don't compare, if we have already found a change
if (!d->rangeChanged && i < oldTokens.count() && token.text() != oldTokens[i].text()) {
d->rangeChanged = true;
}
const Region newRange(token.text(), map, originSheet);
if (!newRange.isValid()) {
continue;
}
int index = alreadyFoundRanges.indexOf(newRange.name());
if (index == -1) { /* not found */
alreadyFoundRanges.append(newRange.name());
index = alreadyFoundRanges.count() - 1;
}
const QColor color(colors[index % colors.size()]);
setFormat(token.pos() + 1, token.text().length(), color);
++d->rangeCount;
}
break;
case Token::Boolean: // True, False (also i18n-ized)
/* font = QFont(editorFont);
font.setBold(true);
setFormat(token.pos() + 1, token.text().length(), font);*/
break;
case Token::Identifier: // function name or named area*/
/* font = QFont(editorFont);
font.setBold(true);
setFormat(token.pos() + 1, token.text().length(), font);*/
break;
case Token::Unknown:
case Token::Integer: // 14, 3, 1977
case Token::Float: // 3.141592, 1e10, 5.9e-7
case Token::String: // "Calligra", "The quick brown fox..."
case Token::Error:
break;
case Token::Operator: { // +, *, /, -
switch (token.asOperator()) {
case Token::LeftPar:
case Token::RightPar:
//Check where this brace is in relation to the cursor and highlight it if necessary.
handleBrace(i);
break;
default:
break;
}
}
break;
}
}
if (oldRangeCount != d->rangeCount)
d->rangeChanged = true;
}
示例14: selectionChanged
// Called on selection (and sheet) changes.
void CellEditor::selectionChanged()
{
if (d->selectionChangedLocked) {
return;
}
Selection* choice = selection();
if (choice->isEmpty())
return;
const QString text = toPlainText();
const int textLength = text.length();
// Find the start text cursor position for the active sub-region within
// the formula's expression and determine the length of the sub-region.
Tokens tokens = d->highlighter->formulaTokens();
uint start = 1;
uint length = 0;
if (!tokens.empty()) {
if (d->currentToken < tokens.count()) {
Token token = tokens[d->currentToken];
Token::Type type = token.type();
if (type == Token::Cell || type == Token::Range) {
start = token.pos() + 1; // don't forget the '='!
length = token.text().length();
// Iterate to the end of the sub-region.
for (int i = d->currentToken + 1; i < tokens.count(); ++i) {
token = tokens[i];
type = token.type();
switch (type) {
case Token::Cell:
case Token::Range:
length += token.text().length();
continue;
case Token::Operator:
if (token.asOperator() == Token::Semicolon) {
++length;
continue;
}
default:
break;
}
break;
}
} else {
start = token.pos() + 1; // don't forget the '='!
length = token.text().length();
}
} else {
// sanitize
d->currentToken = tokens.count();
start = textLength;
}
}
// Replace the formula's active sub-region with the selection's one.
const QString address = choice->activeSubRegionName();
const QString newExpression = QString(text).replace(start, length, address);
// The expression highlighting gets updated automatically by the next call,
// even though signals are blocked (must be connected to QTextDocument).
blockSignals(true);
setText(newExpression, start + address.length());
blockSignals(false);
// Ranges have changed.
// Reset the flag, that indicates range changes after text changes.
d->highlighter->resetRangeChanged();
// Mirror the behaviour of slotCursorPositionChanged(), but here the tokens
// are already up-to-date.
d->globalCursorPos = mapToGlobal(cursorRect().bottomLeft());
// Set the active sub-region.
// Needs up-to-date tokens; QSyntaxHighlighter::rehighlight() gets called
// automatically on text changes, which does the update.
d->updateActiveSubRegion(d->highlighter->formulaTokens());
// Always emit, because this editor may be hidden or does not have focus,
// but the external one needs an update.
emit textChanged(toPlainText());
}
示例15: isTrueFormula
bool Conditions::isTrueFormula(const Cell &cell, const QString &formula, const QString &baseCellAddress) const
{
Map* const map = cell.sheet()->map();
ValueCalc *const calc = map->calc();
Formula f(cell.sheet(), cell);
f.setExpression('=' + formula);
Region r(baseCellAddress, map, cell.sheet());
if (r.isValid() && r.isSingular()) {
QPoint basePoint = static_cast<Region::Point*>(*r.constBegin())->pos();
QString newFormula('=');
const Tokens tokens = f.tokens();
for (int t = 0; t < tokens.count(); ++t) {
const Token token = tokens[t];
if (token.type() == Token::Cell || token.type() == Token::Range) {
if (map->namedAreaManager()->contains(token.text())) {
newFormula.append(token.text());
continue;
}
const Region region(token.text(), map, cell.sheet());
if (!region.isValid() || !region.isContiguous()) {
newFormula.append(token.text());
continue;
}
if (region.firstSheet() != r.firstSheet()) {
newFormula.append(token.text());
continue;
}
Region::Element* element = *region.constBegin();
if (element->type() == Region::Element::Point) {
Region::Point* point = static_cast<Region::Point*>(element);
QPoint pos = point->pos();
if (!point->isRowFixed()) {
int delta = pos.y() - basePoint.y();
pos.setY(cell.row() + delta);
}
if (!point->isColumnFixed()) {
int delta = pos.x() - basePoint.x();
pos.setX(cell.column() + delta);
}
newFormula.append(Region(pos, cell.sheet()).name());
} else {
Region::Range* range = static_cast<Region::Range*>(element);
QRect r = range->rect();
if (!range->isTopFixed()) {
int delta = r.top() - basePoint.y();
r.setTop(cell.row() + delta);
}
if (!range->isBottomFixed()) {
int delta = r.bottom() - basePoint.y();
r.setBottom(cell.row() + delta);
}
if (!range->isLeftFixed()) {
int delta = r.left() - basePoint.x();
r.setLeft(cell.column() + delta);
}
if (!range->isRightFixed()) {
int delta = r.right() - basePoint.x();
r.setRight(cell.column() + delta);
}
newFormula.append(Region(r, cell.sheet()).name());
}
} else {
newFormula.append(token.text());
}
}
f.setExpression(newFormula);
}
Value val = f.eval();
return calc->conv()->asBoolean(val).asBoolean();
}