/** Automatically indents the QTextDocument doc by using indent/outdent regular expressions. @todo The expressions are currently hardcoded and used to indent XQuery, XML and XSLT documents. */ void AutoIndent::indentDocument(QTextDocument *doc) { int level = 0; QList<QRegExp> indentRules; indentRules.append( QRegExp( "<\\w+[^>]*[^/]>" ) ); indentRules.append( QRegExp( "\\{" ) ); //indentRules.append( QRegExp( "(for|return)" ) ); QRegExp outdentRule( "(\\}|</[^>])" ); QString tNew; for ( QTextBlock block = doc->firstBlock(); block.isValid(); block = block.next() ) { QString t = block.text().trimmed(); const int indentCount = matchCount( indentRules, t ); level += indentCount; const int outdentCount = matchCount( outdentRule, t ); level -= outdentCount; if (level < 0) level = 0; if ( !t.isEmpty() ) // don't indent empty lines { const bool foundIndent = (indentCount > 0); // const bool foundOutdent = (outdentCount > 0); if ( foundIndent ) indent(t, level - indentCount + outdentCount); // don't indent start tag (stays on current level) else indent(t, level); // we are in an indent block } // line break when not the last line if ( block.next().isValid() ) t += "\n"; // add to result tNew += t; //tNew.prepend( QString("[%1 , %2] : ").arg(indentCount, 2).arg(outdentCount, 2); } doc->setPlainText(tNew); }
/** Counts the occurences of a list of regular expressions. Each expression is evaluated independent of other expressions. @return The total match count is returned. */ int AutoIndent::matchCount( const QList<QRegExp> &expressions, const QString &text ) const { int count = 0; for (int i=0; i < expressions.count(); ++i) { count += matchCount(expressions.at(i), text); } return count; }
void QUnsortedModelEngine::filterOnDemand(int n) { Q_ASSERT(matchCount()); if (!curMatch.partial) return; Q_ASSERT(n >= -1); const QAbstractItemModel *model = c->proxy->sourceModel(); int lastRow = model->rowCount(curParent) - 1; QIndexMapper im(curMatch.indices.last() + 1, lastRow); int lastIndex = buildIndices(curParts.last(), curParent, n, im, &curMatch); curMatch.partial = (lastRow != lastIndex); saveInCache(curParts.last(), curParent, curMatch); }