Newer
Older
--i;
}
}
if (i < 0) {
for (;;) {
openParenParag = openParenParag.previous();
if (!openParenParag.isValid())
return NoMatch;
if (TextEditDocumentLayout::hasParentheses(openParenParag)
&& !TextEditDocumentLayout::ifdefedOut(openParenParag)) {
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
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;
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
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::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)
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;
}
}
}
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))
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
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))
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
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, QTextDocument::FindFlags findFlags)
{
if (d->m_searchExpr.pattern() == txt)
return;
d->m_searchExpr.setPattern(txt);
d->m_searchExpr.setPatternSyntax(QRegExp::FixedString);
d->m_searchExpr.setCaseSensitivity((findFlags & QTextDocument::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_matchParentheses()
{
if (isReadOnly())
return;
QTextCursor backwardMatch = textCursor();
QTextCursor forwardMatch = textCursor();
const TextBlockUserData::MatchType backwardMatchType = TextBlockUserData::matchCursorBackward(&backwardMatch);
const TextBlockUserData::MatchType forwardMatchType = TextBlockUserData::matchCursorForward(&forwardMatch);
if (backwardMatchType == TextBlockUserData::NoMatch && forwardMatchType == TextBlockUserData::NoMatch)
return;
QList<QTextEdit::ExtraSelection> extraSelections;
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
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
if (backwardMatch.hasSelection()) {
QTextEdit::ExtraSelection sel;
if (backwardMatchType == TextBlockUserData::Mismatch) {
sel.cursor = backwardMatch;
sel.format = d->m_mismatchFormat;
} else {
if (d->m_formatRange) {
sel.cursor = backwardMatch;
sel.format = d->m_rangeFormat;
extraSelections.append(sel);
}
sel.cursor = backwardMatch;
sel.format = d->m_matchFormat;
sel.cursor.setPosition(backwardMatch.selectionStart());
sel.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
extraSelections.append(sel);
sel.cursor.setPosition(backwardMatch.selectionEnd());
sel.cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
}
extraSelections.append(sel);
}
if (forwardMatch.hasSelection()) {
QTextEdit::ExtraSelection sel;
if (forwardMatchType == TextBlockUserData::Mismatch) {
sel.cursor = forwardMatch;
sel.format = d->m_mismatchFormat;
} else {
if (d->m_formatRange) {
sel.cursor = forwardMatch;
sel.format = d->m_rangeFormat;
extraSelections.append(sel);
}
sel.cursor = forwardMatch;
sel.format = d->m_matchFormat;
sel.cursor.setPosition(forwardMatch.selectionStart());
sel.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
extraSelections.append(sel);
sel.cursor.setPosition(forwardMatch.selectionEnd());
sel.cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
}
extraSelections.append(sel);
}
setExtraSelections(ParenthesesMatchingSelection, extraSelections);
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
}
void BaseTextEditor::setActionHack(QObject *hack)
{
d->m_actionHack = hack;
}
QObject *BaseTextEditor::actionHack() const
{
return d->m_actionHack;
}
void BaseTextEditor::changeEvent(QEvent *e)
{
QPlainTextEdit::changeEvent(e);
if (e->type() == QEvent::ApplicationFontChange
|| e->type() == QEvent::FontChange) {
if (d->m_extraArea) {
QFont f = d->m_extraArea->font();
f.setPointSize(font().pointSize());
d->m_extraArea->setFont(f);
slotUpdateExtraAreaWidth();
d->m_extraArea->update();
}
}
}
// shift+del
void BaseTextEditor::deleteLine()
{
QTextCursor cursor = textCursor();
if (!cursor.hasSelection()) {
const QTextBlock &block = cursor.block();
if (block.next().isValid()) {
cursor.setPosition(block.position());
cursor.setPosition(block.next().position(), QTextCursor::KeepAnchor);
} else {
cursor.movePosition(QTextCursor::EndOfBlock);
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
}
setTextCursor(cursor);
}
cut();
}
void BaseTextEditor::setExtraSelections(ExtraSelectionKind kind, const QList<QTextEdit::ExtraSelection> &selections)
if (selections.isEmpty() && d->m_extraSelections[kind].isEmpty())
return;
d->m_extraSelections[kind] = selections;
QList<QTextEdit::ExtraSelection> all;
for (int i = 0; i < NExtraSelectionKinds; ++i)
all += d->m_extraSelections[i];
QPlainTextEdit::setExtraSelections(all);
QList<QTextEdit::ExtraSelection> BaseTextEditor::extraSelections(ExtraSelectionKind kind) const
return d->m_extraSelections[kind];
}
// the blocks list must be sorted
void BaseTextEditor::setIfdefedOutBlocks(const QList<BaseTextEditor::BlockRange> &blocks)
{
QTextDocument *doc = document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
bool needUpdate = false;
QTextBlock block = doc->firstBlock();
int rangeNumber = 0;
while (block.isValid()) {
if (rangeNumber < blocks.size()) {
const BlockRange &range = blocks.at(rangeNumber);
if (block.position() >= range.first && (block.position() <= range.last || !range.last)) {
needUpdate |= TextEditDocumentLayout::setIfdefedOut(block);
needUpdate |= TextEditDocumentLayout::clearIfdefedOut(block);
}
if (block.contains(range.last))
++rangeNumber;
} else {
needUpdate |= TextEditDocumentLayout::clearIfdefedOut(block);
}
block = block.next();
}
if (needUpdate)
documentLayout->requestUpdate();
}
void BaseTextEditor::format()
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
indent(document(), cursor, QChar::Null);
cursor.endEditBlock();
}
void BaseTextEditor::unCommentSelection()
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
}
void BaseTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
{
const QTextCharFormat textFormat = fs.toTextCharFormat(QLatin1String(Constants::C_TEXT));
const QTextCharFormat selectionFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SELECTION));
const QTextCharFormat lineNumberFormat = fs.toTextCharFormat(QLatin1String(Constants::C_LINE_NUMBER));
const QTextCharFormat searchResultFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_RESULT));
const QTextCharFormat searchScopeFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_SCOPE));
const QTextCharFormat parenthesesFormat = fs.toTextCharFormat(QLatin1String(Constants::C_PARENTHESES));
const QTextCharFormat currentLineFormat = fs.toTextCharFormat(QLatin1String(Constants::C_CURRENT_LINE));
const QTextCharFormat ifdefedOutFormat = fs.toTextCharFormat(QLatin1String(Constants::C_DISABLED_CODE));
QFont font(textFormat.font());
const QColor foreground = textFormat.foreground().color();
const QColor background = textFormat.background().color();
QPalette p = palette();
p.setColor(QPalette::Text, foreground);
p.setColor(QPalette::Foreground, foreground);
p.setColor(QPalette::Base, background);
p.setColor(QPalette::Highlight, (selectionFormat.background().style() != Qt::NoBrush) ?
selectionFormat.background().color() :
QApplication::palette().color(QPalette::Highlight));
p.setColor(QPalette::HighlightedText, selectionFormat.foreground().color());
p.setBrush(QPalette::Inactive, QPalette::Highlight, p.highlight());
p.setBrush(QPalette::Inactive, QPalette::HighlightedText, p.highlightedText());
setPalette(p);
setFont(font);
setTabSettings(d->m_document->tabSettings()); // update tabs, they depend on the font
// Line numbers
QPalette ep = d->m_extraArea->palette();
ep.setColor(QPalette::Dark, lineNumberFormat.foreground().color());
ep.setColor(QPalette::Background, lineNumberFormat.background().style() != Qt::NoBrush ?
lineNumberFormat.background().color() : background);
d->m_extraArea->setPalette(ep);
// Search results
d->m_searchResultFormat.setBackground(searchResultFormat.background());
d->m_searchScopeFormat.setBackground(searchScopeFormat.background());
d->m_currentLineFormat.setBackground(currentLineFormat.background());
// Matching braces
d->m_matchFormat.setForeground(parenthesesFormat.foreground());
d->m_rangeFormat.setBackground(parenthesesFormat.background());
// Disabled code
d->m_ifdefedOutFormat.setForeground(ifdefedOutFormat.foreground());
slotUpdateExtraAreaWidth();
}
void BaseTextEditor::setTabSettings(const TabSettings &ts)
{
d->m_document->setTabSettings(ts);
int charWidth = QFontMetrics(font()).width(QChar(' '));
setTabStopWidth(charWidth * ts.m_tabSize);
}
void BaseTextEditor::setDisplaySettings(const DisplaySettings &ds)
{
setLineWrapMode(ds.m_textWrapping ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap);
setLineNumbersVisible(ds.m_displayLineNumbers);
setVisibleWrapColumn(ds.m_showWrapColumn ? ds.m_wrapColumn : 0);
setCodeFoldingVisible(ds.m_displayFoldingMarkers);
setHighlightCurrentLine(ds.m_highlightCurrentLine);
if (d->m_displaySettings.m_visualizeWhitespace != ds.m_visualizeWhitespace) {
if (QSyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter())
highlighter->rehighlight();
QTextOption option = document()->defaultTextOption();
if (ds.m_visualizeWhitespace)
option.setFlags(option.flags() | QTextOption::ShowTabsAndSpaces);
else
option.setFlags(option.flags() & ~QTextOption::ShowTabsAndSpaces);
option.setFlags(option.flags() | QTextOption::AddSpaceForLineAndParagraphSeparators);
document()->setDefaultTextOption(option);
d->m_displaySettings = ds;
}
void BaseTextEditor::setStorageSettings(const StorageSettings &storageSettings)
{
d->m_document->setStorageSettings(storageSettings);
}
void BaseTextEditor::collapse()
{
QTextDocument *doc = document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
QTextBlock curBlock = block;
if (TextBlockUserData::canCollapse(block) && block.next().isVisible()) {
break;
if ((block.next().userState()) >> 8 <= (curBlock.previous().userState() >> 8))
break;
}
block = block.previous();
}
if (block.isValid()) {
TextBlockUserData::doCollapse(block, false);
d->moveCursorVisible();
documentLayout->requestUpdate();
documentLayout->emitDocumentSizeChanged();
}
}
void BaseTextEditor::expand()
{
QTextDocument *doc = document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
QTextBlock block = textCursor().block();
while (block.isValid() && !block.isVisible())
block = block.previous();
TextBlockUserData::doCollapse(block, true);
d->moveCursorVisible();
documentLayout->requestUpdate();
documentLayout->emitDocumentSizeChanged();
}
void BaseTextEditor::unCollapseAll()
{
QTextDocument *doc = document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
QTextBlock block = doc->firstBlock();
bool makeVisible = true;
while (block.isValid()) {
if (block.isVisible() && TextBlockUserData::canCollapse(block) && block.next().isVisible()) {
makeVisible = false;
break;
}
block = block.next();
}
block = doc->firstBlock();
while (block.isValid()) {
if (TextBlockUserData::canCollapse(block))
TextBlockUserData::doCollapse(block, makeVisible);
block = block.next();
}
d->moveCursorVisible();
documentLayout->requestUpdate();
documentLayout->emitDocumentSizeChanged();
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
}
void BaseTextEditor::setTextCodec(QTextCodec *codec)
{
baseTextDocument()->setCodec(codec);
}
QTextCodec *BaseTextEditor::textCodec() const
{
return baseTextDocument()->codec();
}
void BaseTextEditor::setReadOnly(bool b)
{
QPlainTextEdit::setReadOnly(b);
if (b)
setTextInteractionFlags(textInteractionFlags() | Qt::TextSelectableByKeyboard);
}
void BaseTextEditor::cut()
{
if (d->m_inBlockSelectionMode) {
copy();
d->removeBlockSelection();
return;
}
QPlainTextEdit::cut();
}
void BaseTextEditor::paste()
{
if (d->m_inBlockSelectionMode) {
d->removeBlockSelection();
}
QPlainTextEdit::paste();
}
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
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
3650
3651
3652
3653
3654
3655
3656
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
3682
3683
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
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
QMimeData *BaseTextEditor::createMimeDataFromSelection() const
{
if (d->m_inBlockSelectionMode) {
QMimeData *mimeData = new QMimeData;
QString text = d->copyBlockSelection();
mimeData->setData(QLatin1String("application/vnd.nokia.qtcreator.blocktext"), text.toUtf8());
mimeData->setText(text); // for exchangeability
return mimeData;
}
return QPlainTextEdit::createMimeDataFromSelection();
}
bool BaseTextEditor::canInsertFromMimeData(const QMimeData *source) const
{
return QPlainTextEdit::canInsertFromMimeData(source);
}
void BaseTextEditor::insertFromMimeData(const QMimeData *source)
{
if (!isReadOnly() && source->hasFormat(QLatin1String("application/vnd.nokia.qtcreator.blocktext"))) {
QString text = QString::fromUtf8(source->data(QLatin1String("application/vnd.nokia.qtcreator.blocktext")));
if (text.isEmpty())
return;
QStringList lines = text.split(QLatin1Char('\n'));
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
int initialCursorPosition = cursor.position();
int column = cursor.position() - cursor.block().position();
cursor.insertText(lines.first());
for (int i = 1; i < lines.count(); ++i) {
QTextBlock next = cursor.block().next();
if (next.isValid()) {
cursor.setPosition(next.position() + qMin(column, next.length()-1));
} else {
cursor.movePosition(QTextCursor::EndOfBlock);
cursor.insertBlock();
}
int actualColumn = cursor.position() - cursor.block().position();
if (actualColumn < column)
cursor.insertText(QString(column - actualColumn, QLatin1Char(' ')));
cursor.insertText(lines.at(i));
}
cursor.setPosition(initialCursorPosition);
cursor.endEditBlock();
setTextCursor(cursor);
ensureCursorVisible();
return;
}
QPlainTextEdit::insertFromMimeData(source);
}
BaseTextEditorEditable::BaseTextEditorEditable(BaseTextEditor *editor)
: e(editor)
{
#ifndef TEXTEDITOR_STANDALONE
using namespace Find;
Aggregation::Aggregate *aggregate = new Aggregation::Aggregate;
BaseTextFind *baseTextFind = new BaseTextFind(editor);
connect(baseTextFind, SIGNAL(highlightAll(QString, QTextDocument::FindFlags)),
editor, SLOT(highlightSearchResults(QString, QTextDocument::FindFlags)));
connect(baseTextFind, SIGNAL(findScopeChanged(QTextCursor)), editor, SLOT(setFindScope(QTextCursor)));
aggregate->add(baseTextFind);
aggregate->add(editor);
#endif
m_cursorPositionLabel = new Core::Utils::LineColumnLabel;
QHBoxLayout *l = new QHBoxLayout;
QWidget *w = new QWidget;
l->setMargin(0);
l->setContentsMargins(0, 0, 5, 0);
l->addStretch(1);
l->addWidget(m_cursorPositionLabel);
w->setLayout(l);
m_toolBar = new QToolBar;
m_toolBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
m_toolBar->addWidget(w);
connect(editor, SIGNAL(cursorPositionChanged()), this, SLOT(updateCursorPosition()));
}
BaseTextEditorEditable::~BaseTextEditorEditable()
{
delete m_toolBar;
delete e;
}
QToolBar *BaseTextEditorEditable::toolBar()
{
return m_toolBar;
}
int BaseTextEditorEditable::find(const QString &) const
{
return 0;
}
int BaseTextEditorEditable::currentLine() const
{
return e->textCursor().blockNumber() + 1;
}
int BaseTextEditorEditable::currentColumn() const
{
QTextCursor cursor = e->textCursor();
return cursor.position() - cursor.block().position() + 1;
}
QRect BaseTextEditorEditable::cursorRect(int pos) const
{
QTextCursor tc = e->textCursor();
if (pos >= 0)
tc.setPosition(pos);
QRect result = e->cursorRect(tc);
result.moveTo(e->viewport()->mapToGlobal(result.topLeft()));
return result;
}
QString BaseTextEditorEditable::contents() const
{
return e->toPlainText();
}
QString BaseTextEditorEditable::selectedText() const
{
if (e->textCursor().hasSelection())
return e->textCursor().selectedText();
return QString();
}
QString BaseTextEditorEditable::textAt(int pos, int length) const
{
QTextCursor c = e->textCursor();
if (pos < 0)
pos = 0;
c.movePosition(QTextCursor::End);
if (pos + length > c.position())
length = c.position() - pos;
c.setPosition(pos);
c.setPosition(pos + length, QTextCursor::KeepAnchor);
return c.selectedText();
}
void BaseTextEditorEditable::remove(int length)
{
QTextCursor tc = e->textCursor();
tc.setPosition(tc.position() + length, QTextCursor::KeepAnchor);
tc.removeSelectedText();
}
void BaseTextEditorEditable::insert(const QString &string)
{
QTextCursor tc = e->textCursor();
tc.insertText(string);
}
void BaseTextEditorEditable::replace(int length, const QString &string)
{
QTextCursor tc = e->textCursor();
tc.setPosition(tc.position() + length, QTextCursor::KeepAnchor);
tc.insertText(string);
}
void BaseTextEditorEditable::setCurPos(int pos)
{
QTextCursor tc = e->textCursor();
tc.setPosition(pos);
e->setTextCursor(tc);
}
void BaseTextEditorEditable::select(int toPos)
{
QTextCursor tc = e->textCursor();
tc.setPosition(toPos, QTextCursor::KeepAnchor);
e->setTextCursor(tc);
}
void BaseTextEditorEditable::updateCursorPosition()
{
const QTextCursor cursor = e->textCursor();
const QTextBlock block = cursor.block();
const int line = block.blockNumber() + 1;
const int column = cursor.position() - block.position() + 1;
m_cursorPositionLabel->setText(QString("Line: %1, Col: %2").arg(line).arg(column),
QString("Line: %1, Col: 999").arg(e->blockCount()));
m_contextHelpId.clear();
if (!block.isVisible())
e->ensureCursorVisible();
}
QString BaseTextEditorEditable::contextHelpId() const
{
if (m_contextHelpId.isEmpty())
emit const_cast<BaseTextEditorEditable*>(this)->contextHelpIdRequested(e->editableInterface(),
e->textCursor().position());
return m_contextHelpId;
}
TextBlockUserData::~TextBlockUserData()
{
TextMarks marks = m_marks;
m_marks.clear();
foreach (ITextMark *mrk, marks) {
mrk->removedFromEditor();
}
}