Newer
Older
&& e->type() == QEvent::MouseMove && e->buttons() == 0) { // mouse tracking
// Update which folder marker is highlighted
const int highlightBlockNumber = d->extraAreaHighlightCollapseBlockNumber;
const int highlightColumn = d->extraAreaHighlightCollapseColumn;
d->extraAreaHighlightCollapseColumn = -1;
if (e->pos().x() > extraArea()->width() - collapseBoxWidth(fontMetrics())) {
d->extraAreaHighlightCollapseBlockNumber = cursor.blockNumber();
if (TextBlockUserData::canCollapse(cursor.block())
|| !TextBlockUserData::hasClosingCollapse(cursor.block()))
d->extraAreaHighlightCollapseColumn = cursor.block().length()-1;
if (TextBlockUserData::hasCollapseAfter(cursor.block())) {
d->extraAreaHighlightCollapseBlockNumber++;
d->extraAreaHighlightCollapseColumn = -1;
if (TextBlockUserData::canCollapse(cursor.block().next())
|| !TextBlockUserData::hasClosingCollapse(cursor.block().next()))
d->extraAreaHighlightCollapseColumn = cursor.block().next().length()-1;
}
} else if (d->m_displaySettings.m_highlightBlocks) {
QTextCursor cursor = textCursor();
d->extraAreaHighlightCollapseBlockNumber = cursor.blockNumber();
d->extraAreaHighlightCollapseColumn = cursor.position() - cursor.block().position();
if (highlightBlockNumber != d->extraAreaHighlightCollapseBlockNumber
|| highlightColumn != d->extraAreaHighlightCollapseColumn) {
d->m_highlightBlocksTimer->start(d->m_highlightBlocksInfo.isEmpty() ? 120 : 0);
}

Thorbjørn Lindeijer
committed
// Set whether the mouse cursor is a hand or normal arrow
if (e->type() == QEvent::MouseMove) {
bool hand = (e->pos().x() <= markWidth);
if (hand != (d->m_extraArea->cursor().shape() == Qt::PointingHandCursor))
d->m_extraArea->setCursor(hand ? Qt::PointingHandCursor : Qt::ArrowCursor);
}
if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonDblClick) {
if (e->button() == Qt::LeftButton) {
int boxWidth = collapseBoxWidth(fontMetrics());
if (d->m_codeFoldingVisible && e->pos().x() > extraArea()->width() - boxWidth) {
if (!cursor.block().next().isVisible()) {
toggleBlockVisible(cursor.block());
d->moveCursorVisible(false);
} else if (collapseBox().contains(e->pos())) {
cursor.setPosition(
document()->findBlockByNumber(d->m_highlightBlocksInfo.open.last()).position()
);
QTextBlock c = cursor.block();
if (TextBlockUserData::hasCollapseAfter(c.previous()))
c = c.previous();
toggleBlockVisible(c);
d->moveCursorVisible(false);
}
} else if (d->m_marksVisible && e->pos().x() > markWidth) {
QTextCursor selection = cursor;
selection.setVisualNavigation(true);
d->extraAreaSelectionAnchorBlockNumber = selection.blockNumber();
selection.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
selection.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
setTextCursor(selection);
} else {
d->extraAreaToggleMarkBlockNumber = cursor.blockNumber();
}
} else if (d->m_marksVisible && e->button() == Qt::RightButton) {
QMenu * contextMenu = new QMenu(this);
emit d->m_editable->markContextMenuRequested(editableInterface(), cursor.blockNumber() + 1, contextMenu);
if (!contextMenu->isEmpty())
contextMenu->exec(e->globalPos());
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
}
} else if (d->extraAreaSelectionAnchorBlockNumber >= 0) {
QTextCursor selection = cursor;
selection.setVisualNavigation(true);
if (e->type() == QEvent::MouseMove) {
QTextBlock anchorBlock = document()->findBlockByNumber(d->extraAreaSelectionAnchorBlockNumber);
selection.setPosition(anchorBlock.position());
if (cursor.blockNumber() < d->extraAreaSelectionAnchorBlockNumber) {
selection.movePosition(QTextCursor::EndOfBlock);
selection.movePosition(QTextCursor::Right);
}
selection.setPosition(cursor.block().position(), QTextCursor::KeepAnchor);
if (cursor.blockNumber() >= d->extraAreaSelectionAnchorBlockNumber) {
selection.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
selection.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
}
if (e->pos().y() >= 0 && e->pos().y() <= d->m_extraArea->height())
d->autoScrollTimer.stop();
else if (!d->autoScrollTimer.isActive())
d->autoScrollTimer.start(100, this);
} else {
d->autoScrollTimer.stop();
d->extraAreaSelectionAnchorBlockNumber = -1;
return;
}
setTextCursor(selection);
} else if (d->extraAreaToggleMarkBlockNumber >= 0 && d->m_marksVisible && d->m_requestMarkEnabled) {
if (e->type() == QEvent::MouseButtonRelease && e->button() == Qt::LeftButton) {
int n = d->extraAreaToggleMarkBlockNumber;
d->extraAreaToggleMarkBlockNumber = -1;
if (cursor.blockNumber() == n) {
int line = n + 1;
emit d->m_editable->markRequested(editableInterface(), line);
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
}
}
}
}
QTextBlock TextBlockUserData::testCollapse(const QTextBlock& block)
{
QTextBlock info = block;
if (block.userData() && static_cast<TextBlockUserData*>(block.userData())->collapseMode() == CollapseAfter)
;
else if (block.next().userData()
&& static_cast<TextBlockUserData*>(block.next().userData())->collapseMode()
== TextBlockUserData::CollapseThis)
info = block.next();
else
return QTextBlock();
int pos = static_cast<TextBlockUserData*>(info.userData())->collapseAtPos();
if (pos < 0)
return QTextBlock();
QTextCursor cursor(info);
cursor.setPosition(cursor.position() + pos);
matchCursorForward(&cursor);
return cursor.block();
}
void TextBlockUserData::doCollapse(const QTextBlock& block, bool visible)
{
QTextBlock info = block;
if (block.userData() && static_cast<TextBlockUserData*>(block.userData())->collapseMode() == CollapseAfter)
;
else if (block.next().userData()
&& static_cast<TextBlockUserData*>(block.next().userData())->collapseMode()
== TextBlockUserData::CollapseThis)
info = block.next();
else {
if (visible && !block.next().isVisible()) {
// no match, at least unfold!
QTextBlock b = block.next();
while (b.isValid() && !b.isVisible()) {
b.setVisible(true);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
b = b.next();
}
}
return;
}
int pos = static_cast<TextBlockUserData*>(info.userData())->collapseAtPos();
if (pos < 0)
return;
QTextCursor cursor(info);
cursor.setPosition(cursor.position() + pos);
if (matchCursorForward(&cursor) != Match) {
if (visible) {
// no match, at least unfold!
QTextBlock b = block.next();
while (b.isValid() && !b.isVisible()) {
b.setVisible(true);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
b = b.next();
}
}
return;
}
QTextBlock b = block.next();
while (b < cursor.block()) {
b.setVisible(visible);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
if (visible) {
TextBlockUserData *data = canCollapse(b);
if (data && data->collapsed()) {
QTextBlock end = testCollapse(b);
if (data->collapseIncludesClosure())
end = end.next();
if (end.isValid()) {
b = end;
continue;
}
}
}
b = b.next();
}
bool collapseIncludesClosure = hasClosingCollapseAtEnd(b);
if (collapseIncludesClosure) {
b.setVisible(visible);
b.setLineCount(visible ? qMax(1, b.layout()->lineCount()) : 0);
}
static_cast<TextBlockUserData*>(info.userData())->setCollapseIncludesClosure(collapseIncludesClosure);
static_cast<TextBlockUserData*>(info.userData())->setCollapsed(!block.next().isVisible());
}
void BaseTextEditor::ensureCursorVisible()
{
QTextBlock block = textCursor().block();
if (!block.isVisible()) {
while (!block.isVisible() && block.previous().isValid())
block = block.previous();
toggleBlockVisible(block);
}
QPlainTextEdit::ensureCursorVisible();
}
void BaseTextEditor::toggleBlockVisible(const QTextBlock &block)
{
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(document()->documentLayout());
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
bool visible = block.next().isVisible();
TextBlockUserData::doCollapse(block, !visible);
documentLayout->requestUpdate();
documentLayout->emitDocumentSizeChanged();
}
const TabSettings &BaseTextEditor::tabSettings() const
{
return d->m_document->tabSettings();
}
const DisplaySettings &BaseTextEditor::displaySettings() const
{
return d->m_displaySettings;
}
void BaseTextEditor::indentOrUnindent(bool doIndent)
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
int pos = cursor.position();
const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
QTextDocument *doc = document();
if (!cursor.hasSelection()
|| (doc->findBlock(cursor.selectionStart()) == doc->findBlock(cursor.selectionEnd()) )) {
cursor.removeSelectedText();
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = (cursor.position() - block.position());;
int spaces = tabSettings.spacesLeftFromPosition(text, indentPosition);
int startColumn = tabSettings.columnAt(text, indentPosition - spaces);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(tabSettings.indentationString(startColumn, targetColumn));
} else {
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end-1).next();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = tabSettings.lineIndentPosition(text);
if (!doIndent && !indentPosition)
indentPosition = tabSettings.firstNonSpace(text);
int targetColumn = tabSettings.indentedColumn(tabSettings.columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(tabSettings.indentationString(0, targetColumn));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
}
cursor.endEditBlock();
}
void BaseTextEditor::handleHomeKey(bool anchor)
{
QTextCursor cursor = textCursor();
QTextCursor::MoveMode mode = QTextCursor::MoveAnchor;
if (anchor)
mode = QTextCursor::KeepAnchor;
const int initpos = cursor.position();
int pos = cursor.block().position();
QChar character = characterAt(pos);
const QLatin1Char tab = QLatin1Char('\t');
while (character == tab || character.category() == QChar::Separator_Space) {
++pos;
if (pos == initpos)
break;
character = characterAt(pos);
}
// Go to the start of the block when we're already at the start of the text
if (pos == initpos)
pos = cursor.block().position();
cursor.setPosition(pos, mode);
setTextCursor(cursor);
}
void BaseTextEditor::handleBackspaceKey()
{
QTextCursor cursor = textCursor();
const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
if (tabSettings.m_autoIndent && autoBackspace(cursor))
if (!tabSettings.m_smartBackspace) {
cursor.deletePreviousChar();
return;
}
int positionInBlock = pos - currentBlock.position();
const QString blockText = currentBlock.text();
if (cursor.atBlockStart() || tabSettings.firstNonSpace(blockText) < positionInBlock) {
cursor.deletePreviousChar();
return;
}
int previousIndent = 0;
const int indent = tabSettings.columnAt(blockText, positionInBlock);
for (QTextBlock previousNonEmptyBlock = currentBlock.previous();
previousNonEmptyBlock.isValid();
previousNonEmptyBlock = previousNonEmptyBlock.previous()) {
QString previousNonEmptyBlockText = previousNonEmptyBlock.text();
if (previousNonEmptyBlockText.trimmed().isEmpty())
continue;
previousIndent = tabSettings.columnAt(previousNonEmptyBlockText,
tabSettings.firstNonSpace(previousNonEmptyBlockText));
if (previousIndent < indent) {
cursor.beginEditBlock();
cursor.setPosition(currentBlock.position(), QTextCursor::KeepAnchor);
cursor.insertText(tabSettings.indentationString(previousNonEmptyBlockText));
cursor.endEditBlock();
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
}
}
void BaseTextEditor::wheelEvent(QWheelEvent *e)
{
d->clearVisibleCollapsedBlock();
if (e->modifiers() & Qt::ControlModifier) {
const int delta = e->delta();
if (delta < 0)
zoomOut();
else if (delta > 0)
zoomIn();
return;
}
QPlainTextEdit::wheelEvent(e);
}
void BaseTextEditor::zoomIn(int range)
{
d->clearVisibleCollapsedBlock();
QFont f = font();
const int newSize = f.pointSize() + range;
if (newSize <= 0)
return;
emit requestFontSize(newSize);
// f.setPointSize(newSize);
// setFont(f);
}
void BaseTextEditor::zoomOut(int range)
{
zoomIn(-range);
}
bool BaseTextEditor::isElectricCharacter(const QChar &) const
{
return false;
}
QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &text) const
Q_UNUSED(cursor);
Q_UNUSED(text);
return QString();
}
bool BaseTextEditor::autoBackspace(QTextCursor &cursor)
{
Q_UNUSED(cursor);
return false;
}
int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
Q_UNUSED(cursor);
return 0;
}
void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
{
}
void BaseTextEditor::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
do {
indentBlock(doc, block, typedChar);
block = block.next();
} while (block.isValid() && block != end);
} else {
indentBlock(doc, cursor.block(), typedChar);
}
}
void BaseTextEditor::reindent(QTextDocument *doc, const QTextCursor &cursor)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(cursor.selectionStart());
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
const TabSettings &ts = d->m_document->tabSettings();
// skip empty blocks
while (block.isValid() && block != end) {
QString bt = block.text();
if (ts.firstNonSpace(bt) < bt.size())
break;
indentBlock(doc, block, QChar::Null);
block = block.next();
}
int previousIndentation = ts.indentationColumn(block.text());
indentBlock(doc, block, QChar::Null);
int currentIndentation = ts.indentationColumn(block.text());
int delta = currentIndentation - previousIndentation;
block = block.next();
while (block.isValid() && block != end) {
ts.reindentLine(block, delta);
block = block.next();
}
} else {
indentBlock(doc, cursor.block(), QChar::Null);
}
}
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
BaseTextEditor::Link BaseTextEditor::findLinkAt(const QTextCursor &, bool)
{
return Link();
}
bool BaseTextEditor::openLink(const Link &link)
{
if (link.fileName.isEmpty())
return false;
if (baseTextDocument()->fileName() == link.fileName) {
Core::EditorManager *editorManager = Core::EditorManager::instance();
editorManager->addCurrentPositionToNavigationHistory();
gotoLine(link.line, link.column);
setFocus();
return true;
}
return openEditorAt(link.fileName, link.line, link.column);
}
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
void BaseTextEditor::updateLink(QMouseEvent *e)
{
bool linkFound = false;
if (d->m_mouseNavigationEnabled && e->modifiers() & Qt::ControlModifier) {
// Link emulation behaviour for 'go to definition'
const QTextCursor cursor = cursorForPosition(e->pos());
// Check that the mouse was actually on the text somewhere
bool onText = cursorRect(cursor).right() >= e->x();
if (!onText) {
QTextCursor nextPos = cursor;
nextPos.movePosition(QTextCursor::Right);
onText = cursorRect(nextPos).right() >= e->x();
}
const Link link = findLinkAt(cursor, false);
if (onText && link.isValid()) {
showLink(link);
linkFound = true;
}
}
if (!linkFound)
clearLink();
}
void BaseTextEditor::showLink(const Link &link)
{
if (d->m_currentLink == link)
return;
QTextEdit::ExtraSelection sel;
sel.cursor = textCursor();
sel.cursor.setPosition(link.pos);
sel.cursor.setPosition(link.pos + link.length, QTextCursor::KeepAnchor);
sel.format = d->m_linkFormat;
sel.format.setFontUnderline(true);
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel);
viewport()->setCursor(Qt::PointingHandCursor);
d->m_currentLink = link;
d->m_linkPressed = false;
}
void BaseTextEditor::clearLink()
{
if (!d->m_currentLink.isValid())
return;
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
viewport()->setCursor(Qt::IBeamCursor);
d->m_currentLink = Link();
d->m_linkPressed = false;
}
void BaseTextEditorPrivate::updateMarksBlock(const QTextBlock &block)
{
if (const TextBlockUserData *userData = TextEditDocumentLayout::testUserData(block))
foreach (ITextMark *mrk, userData->marks())
mrk->updateBlock(block);
}
void BaseTextEditorPrivate::updateMarksLineNumber()
{
QTextDocument *doc = q->document();
QTextBlock block = doc->begin();
int blockNumber = 0;
while (block.isValid()) {
if (const TextBlockUserData *userData = TextEditDocumentLayout::testUserData(block))
foreach (ITextMark *mrk, userData->marks()) {
mrk->updateLineNumber(blockNumber + 1);
}
block = block.next();
++blockNumber;
}
}
void BaseTextEditor::markBlocksAsChanged(QList<int> blockNumbers)
{
QTextBlock block = document()->begin();
while (block.isValid()) {
if (block.revision() < 0)
block.setRevision(-block.revision() - 1);
block = block.next();
}
foreach (const int blockNumber, blockNumbers) {
QTextBlock block = document()->findBlockByNumber(blockNumber);
if (block.isValid())
block.setRevision(-block.revision() - 1);
}
}
TextBlockUserData::MatchType TextBlockUserData::checkOpenParenthesis(QTextCursor *cursor, QChar c)
{
QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
QTextBlock closedParenParag = block;
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
const int cursorPos = cursor->position() - closedParenParag.position();
int i = 0;
int ignore = 0;
bool foundOpen = false;
for (;;) {
if (!foundOpen) {
if (i >= parenList.count())
return NoMatch;
openParen = parenList.at(i);
if (openParen.pos != cursorPos) {
++i;
continue;
} else {
foundOpen = true;
++i;
}
}
if (i >= parenList.count()) {
for (;;) {
closedParenParag = closedParenParag.next();
if (!closedParenParag.isValid())
return NoMatch;
if (TextEditDocumentLayout::hasParentheses(closedParenParag)
&& !TextEditDocumentLayout::ifdefedOut(closedParenParag)) {
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
parenList = TextEditDocumentLayout::parentheses(closedParenParag);
break;
}
}
i = 0;
}
closedParen = parenList.at(i);
if (closedParen.type == Parenthesis::Opened) {
ignore++;
++i;
continue;
} else {
if (ignore > 0) {
ignore--;
++i;
continue;
}
cursor->clearSelection();
cursor->setPosition(closedParenParag.position() + closedParen.pos + 1, QTextCursor::KeepAnchor);
if ((c == QLatin1Char('{') && closedParen.chr != QLatin1Char('}'))
|| (c == QLatin1Char('(') && closedParen.chr != QLatin1Char(')'))
|| (c == QLatin1Char('[') && closedParen.chr != QLatin1Char(']'))
|| (c == QLatin1Char('+') && closedParen.chr != QLatin1Char('-'))
)
return Mismatch;
return Match;
}
}
}
TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCursor *cursor, QChar c)
{
QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
QTextBlock openParenParag = block;
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
const int cursorPos = cursor->position() - openParenParag.position();
int i = parenList.count() - 1;
int ignore = 0;
bool foundClosed = false;
for (;;) {
if (!foundClosed) {
if (i < 0)
return NoMatch;
closedParen = parenList.at(i);
if (closedParen.pos != cursorPos - 1) {
--i;
continue;
} else {
foundClosed = true;
--i;
}
}
if (i < 0) {
for (;;) {
openParenParag = openParenParag.previous();
if (!openParenParag.isValid())
return NoMatch;
if (TextEditDocumentLayout::hasParentheses(openParenParag)
&& !TextEditDocumentLayout::ifdefedOut(openParenParag)) {
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
parenList = TextEditDocumentLayout::parentheses(openParenParag);
break;
}
}
i = parenList.count() - 1;
}
openParen = parenList.at(i);
if (openParen.type == Parenthesis::Closed) {
ignore++;
--i;
continue;
} else {
if (ignore > 0) {
ignore--;
--i;
continue;
}
cursor->clearSelection();
cursor->setPosition(openParenParag.position() + openParen.pos, QTextCursor::KeepAnchor);
if ((c == '}' && openParen.chr != '{') ||
(c == ')' && openParen.chr != '(') ||
(c == ']' && openParen.chr != '[') ||
(c == '-' && openParen.chr != '+'))
return Mismatch;
return Match;
}
}
}
bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = parenList.count()-1; i >= 0; --i) {
Parenthesis paren = parenList.at(i);
if (block == cursor->block() &&
(position - block.position() <= paren.pos + (paren.type == Parenthesis::Closed ? 1 : 0)))
continue;
if (paren.type == Parenthesis::Closed) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos, select ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
return true;
}
}
}
block = block.previous();
}
return false;
}
bool TextBlockUserData::findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = parenList.count()-1; i >= 0; --i) {
Parenthesis paren = parenList.at(i);
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-')
&& paren.chr != QLatin1Char('[') && paren.chr != QLatin1Char(']'))
if (block == cursor->block()) {
if (position - block.position() <= paren.pos + (paren.type == Parenthesis::Closed ? 1 : 0))
if (checkStartPosition && paren.type == Parenthesis::Opened && paren.pos== cursor->position()) {
if (paren.type == Parenthesis::Closed) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos);
return true;
}
}
}
}
return false;
}
bool TextBlockUserData::findNextClosingParenthesis(QTextCursor *cursor, bool select)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = 0; i < parenList.count(); ++i) {
Parenthesis paren = parenList.at(i);
if (block == cursor->block() &&
(position - block.position() > paren.pos - (paren.type == Parenthesis::Opened ? 1 : 0)))
continue;
if (paren.type == Parenthesis::Opened) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos+1, select ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
return true;
}
}
}
}
return false;
}
bool TextBlockUserData::findNextBlockClosingParenthesis(QTextCursor *cursor)
{
QTextBlock block = cursor->block();
int position = cursor->position();
int ignore = 0;
while (block.isValid()) {
Parentheses parenList = TextEditDocumentLayout::parentheses(block);
if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
for (int i = 0; i < parenList.count(); ++i) {
Parenthesis paren = parenList.at(i);
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-')
&& paren.chr != QLatin1Char('[') && paren.chr != QLatin1Char(']'))
if (block == cursor->block() &&
(position - block.position() > paren.pos - (paren.type == Parenthesis::Opened ? 1 : 0)))
continue;
if (paren.type == Parenthesis::Opened) {
++ignore;
} else if (ignore > 0) {
--ignore;
} else {
cursor->setPosition(block.position() + paren.pos+1);
return true;
}
}
}
block = block.next();
}
return false;
}
TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor *cursor)
{
cursor->clearSelection();
const QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
return NoMatch;
const int relPos = cursor->position() - block.position();
Parentheses parentheses = TextEditDocumentLayout::parentheses(block);
const Parentheses::const_iterator cend = parentheses.constEnd();
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
const Parenthesis &paren = *it;
if (paren.pos == relPos - 1
&& paren.type == Parenthesis::Closed) {
return checkClosedParenthesis(cursor, paren.chr);
}
}
return NoMatch;
}
TextBlockUserData::MatchType TextBlockUserData::matchCursorForward(QTextCursor *cursor)
{
cursor->clearSelection();
const QTextBlock block = cursor->block();
if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
return NoMatch;
const int relPos = cursor->position() - block.position();
Parentheses parentheses = TextEditDocumentLayout::parentheses(block);
const Parentheses::const_iterator cend = parentheses.constEnd();
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
const Parenthesis &paren = *it;
if (paren.pos == relPos
&& paren.type == Parenthesis::Opened) {
return checkOpenParenthesis(cursor, paren.chr);
}
}
return NoMatch;
}
void BaseTextEditor::highlightSearchResults(const QString &txt, Find::IFindSupport::FindFlags findFlags)
{
if (d->m_searchExpr.pattern() == txt)
return;
d->m_searchExpr.setPattern(txt);
d->m_searchExpr.setPatternSyntax((findFlags & Find::IFindSupport::FindRegularExpression) ?
QRegExp::RegExp : QRegExp::FixedString);
d->m_searchExpr.setCaseSensitivity((findFlags & Find::IFindSupport::FindCaseSensitively) ?
Qt::CaseSensitive : Qt::CaseInsensitive);
d->m_findFlags = findFlags;
viewport()->update();
}
void BaseTextEditor::setFindScope(const QTextCursor &scope)
{
if (scope.isNull() != d->m_findScope.isNull()) {
d->m_findScope = scope;
viewport()->update();
}
}
void BaseTextEditor::_q_animateUpdate(int position, QPointF lastPos, QRectF rect)

mae
committed
{
QTextCursor cursor(textCursor());
cursor.setPosition(position);
viewport()->update(QRectF(cursorRect(cursor).topLeft() + rect.topLeft(), rect.size()).toAlignedRect());
if (!lastPos.isNull())
viewport()->update(QRectF(lastPos + rect.topLeft(), rect.size()).toAlignedRect());

mae
committed
}
BaseTextEditorAnimator::BaseTextEditorAnimator(QObject *parent)
:QObject(parent)
{
m_value = 0;
m_timeline = new QTimeLine(256, this);

mae
committed
m_timeline->setCurveShape(QTimeLine::SineCurve);
connect(m_timeline, SIGNAL(valueChanged(qreal)), this, SLOT(step(qreal)));
connect(m_timeline, SIGNAL(finished()), this, SLOT(deleteLater()));
m_timeline->start();
}
void BaseTextEditorAnimator::setData(QFont f, QPalette pal, const QString &text)
{
m_font = f;
m_palette = pal;
m_text = text;
QFontMetrics fm(m_font);
m_size = QSizeF(fm.width(m_text), fm.height());
}

mae
committed
void BaseTextEditorAnimator::draw(QPainter *p, const QPointF &pos)
{
m_lastDrawPos = pos;

mae
committed
p->setPen(m_palette.text().color());
QFont f = m_font;
f.setPointSizeF(f.pointSizeF() * (1.0 + m_value/2));

mae
committed
QFontMetrics fm(f);
int width = fm.width(m_text);
QRectF r((m_size.width()-width)/2, (m_size.height() - fm.height())/2, width, fm.height());
r.translate(pos);
p->fillRect(r, m_palette.base());
p->setFont(f);
p->drawText(r, m_text);
}
bool BaseTextEditorAnimator::isRunning() const
{
return m_timeline->state() == QTimeLine::Running;
}

mae
committed
QRectF BaseTextEditorAnimator::rect() const
{
QFont f = m_font;
f.setPointSizeF(f.pointSizeF() * (1.0 + m_value/2));

mae
committed
QFontMetrics fm(f);
int width = fm.width(m_text);
return QRectF((m_size.width()-width)/2, (m_size.height() - fm.height())/2, width, fm.height());
}
void BaseTextEditorAnimator::step(qreal v)
{
QRectF before = rect();
m_value = v;
QRectF after = rect();
emit updateRequest(m_position, m_lastDrawPos, before.united(after));

mae
committed
}
void BaseTextEditorAnimator::finish()
{
m_timeline->stop();

mae
committed
step(0);
deleteLater();
}
void BaseTextEditor::_q_matchParentheses()
{
if (isReadOnly())
return;
QTextCursor backwardMatch = textCursor();
QTextCursor forwardMatch = textCursor();
const TextBlockUserData::MatchType backwardMatchType = TextBlockUserData::matchCursorBackward(&backwardMatch);
const TextBlockUserData::MatchType forwardMatchType = TextBlockUserData::matchCursorForward(&forwardMatch);
QList<QTextEdit::ExtraSelection> extraSelections;
if (backwardMatchType == TextBlockUserData::NoMatch && forwardMatchType == TextBlockUserData::NoMatch) {
setExtraSelections(ParenthesesMatchingSelection, extraSelections); // clear
return;
}