Newer
Older

mae
committed
if (animatePosition >= 0) {
foreach (QTextEdit::ExtraSelection sel, BaseTextEditor::extraSelections(ParenthesesMatchingSelection)) {
if (sel.cursor.selectionStart() == animatePosition
|| sel.cursor.selectionEnd() - 1 == animatePosition) {
animatePosition = -1;
break;
}
}
}

mae
committed
if (animatePosition >= 0) {
if (d->m_animator)
d->m_animator->finish(); // one animation is enough
d->m_animator = new BaseTextEditorAnimator(this);
d->m_animator->setPosition(animatePosition);
QPalette pal;
pal.setBrush(QPalette::Text, d->m_matchFormat.foreground());
pal.setBrush(QPalette::Base, d->m_rangeFormat.background());
d->m_animator->setData(font(), pal, characterAt(d->m_animator->position()));
connect(d->m_animator, SIGNAL(updateRequest(int,QPointF,QRectF)),
this, SLOT(_q_animateUpdate(int,QPointF,QRectF)));

mae
committed
setExtraSelections(ParenthesesMatchingSelection, extraSelections);
void BaseTextEditor::_q_highlightBlocks()
{
BaseTextEditorPrivateHighlightBlocks highlightBlocksInfo;
if (d->extraAreaHighlightCollapseBlockNumber >= 0) {
QTextBlock block = document()->findBlockByNumber(d->extraAreaHighlightCollapseBlockNumber);
if (block.isValid()) {
QTextCursor cursor(block);
if (d->extraAreaHighlightCollapseColumn >= 0)
cursor.setPosition(cursor.position() + qMin(d->extraAreaHighlightCollapseColumn,
while (TextBlockUserData::findPreviousBlockOpenParenthesis(&cursor, firstRun)) {
highlightBlocksInfo.open.prepend(cursor.blockNumber());
highlightBlocksInfo.visualIndent.prepend(d->visualIndent(cursor.block()));
if (closeCursor.isNull())
closeCursor = cursor;
if (TextBlockUserData::findNextBlockClosingParenthesis(&closeCursor))
highlightBlocksInfo.close.append(closeCursor.blockNumber());
}
}
if (d->m_highlightBlocksInfo != highlightBlocksInfo) {
d->m_highlightBlocksInfo = highlightBlocksInfo;
viewport()->update();
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
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();
}
}
}
void BaseTextEditor::maybeSelectLine()
{
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);
}
}
// shift+del
void BaseTextEditor::cutLine()
{
maybeSelectLine();
void BaseTextEditor::deleteLine()
{
maybeSelectLine();
textCursor().removeSelectedText();
}
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;
bool cleared = false;
bool set = false;
if (rangeNumber < blocks.size()) {
const BlockRange &range = blocks.at(rangeNumber);
if (block.position() >= range.first && ((block.position() + block.length() - 1) <= range.last || !range.last)) {
set = TextEditDocumentLayout::setIfdefedOut(block);
cleared = TextEditDocumentLayout::clearIfdefedOut(block);
}
if (block.contains(range.last))
++rangeNumber;
} else {
cleared = TextEditDocumentLayout::clearIfdefedOut(block);
if (cleared || set) {
needUpdate = true;
int delta = TextEditDocumentLayout::braceDepthDelta(block);
if (cleared)
braceDepthDelta += delta;
else if (set)
braceDepthDelta -= delta;
}
if (braceDepthDelta)
TextEditDocumentLayout::changeBraceDepth(block,braceDepthDelta);
block = block.next();
}
if (needUpdate)
documentLayout->requestUpdate();
}
void BaseTextEditor::format()
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
indent(document(), cursor, QChar::Null);
cursor.endEditBlock();
}
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
void BaseTextEditor::rewrapParagraph()
{
const int paragraphWidth = displaySettings().m_wrapColumn;
const QRegExp anyLettersOrNumbers = QRegExp("\\w");
const int tabSize = tabSettings().m_tabSize;
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
// Find start of paragraph.
while (cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor)) {
QTextBlock block = cursor.block();
QString text = block.text();
// If this block is empty, move marker back to previous and terminate.
if (!text.contains(anyLettersOrNumbers)) {
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor);
break;
}
}
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
// Find indent level of current block.
int indentLevel = 0;
QString text = cursor.block().text();
for (int i = 0; i < text.length(); i++) {
const QChar ch = text.at(i);
if (ch == QLatin1Char(' '))
indentLevel++;
else if (ch == QLatin1Char('\t'))
indentLevel += tabSize - (indentLevel % tabSize);
else
break;
}
// If there is a common prefix, it should be kept and expanded to all lines.
// this allows nice reflowing of doxygen style comments.
QTextCursor nextBlock = cursor;
QString commonPrefix;
if (nextBlock.movePosition(QTextCursor::NextBlock))
{
QString nText = nextBlock.block().text();
int maxLength = qMin(text.length(), nText.length());
for (int i = 0; i < maxLength; ++i) {
const QChar ch = text.at(i);
if (ch != nText[i] || ch.isLetterOrNumber())
break;
commonPrefix.append(ch);
}
}
// Find end of paragraph.
while (cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor)) {
QString text = cursor.block().text();
if (!text.contains(anyLettersOrNumbers))
break;
}
QString selectedText = cursor.selectedText();
// Preserve initial indent level.or common prefix.
QString spacing;
if (commonPrefix.isEmpty()) {
spacing = tabSettings().indentationString(0, indentLevel);
} else {
spacing = commonPrefix;
indentLevel = commonPrefix.length();
}
int currentLength = indentLevel;
QString result;
result.append(spacing);
// Remove existing instances of any common prefix from paragraph to
// reflow.
selectedText.remove(0, commonPrefix.length());
commonPrefix.prepend(QChar::ParagraphSeparator);
selectedText.replace(commonPrefix, QLatin1String("\n"));
// remove any repeated spaces, trim lines to PARAGRAPH_WIDTH width and
// keep the same indentation level as first line in paragraph.
QString currentWord;
for (int i = 0; i < selectedText.length(); ++i) {
QChar ch = selectedText.at(i);
if (ch.isSpace()) {
if (!currentWord.isEmpty()) {
currentLength += currentWord.length() + 1;
if (currentLength > paragraphWidth) {
currentLength = currentWord.length() + 1 + indentLevel;
result.chop(1); // remove trailing space
result.append(QChar::ParagraphSeparator);
result.append(spacing);
}
result.append(currentWord);
result.append(QLatin1Char(' '));
currentWord.clear();
}
continue;
}
currentWord.append(ch);
}
result.chop(1);
result.append(QChar::ParagraphSeparator);
cursor.insertText(result);
cursor.endEditBlock();
}
void BaseTextEditor::unCommentSelection()
void BaseTextEditor::showEvent(QShowEvent* e)
{
if (!d->m_fontSettings.isEmpty()) {
setFontSettings(d->m_fontSettings);
d->m_fontSettings.clear();
}
QPlainTextEdit::showEvent(e);
}
void BaseTextEditor::setFontSettingsIfVisible(const TextEditor::FontSettings &fs)
{
if (!isVisible()) {
d->m_fontSettings = fs;
return;
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));
d->m_searchScopeFormat = fs.toTextCharFormat(QLatin1String(Constants::C_SEARCH_SCOPE));
const QTextCharFormat parenthesesFormat = fs.toTextCharFormat(QLatin1String(Constants::C_PARENTHESES));
d->m_currentLineFormat = fs.toTextCharFormat(QLatin1String(Constants::C_CURRENT_LINE));
d->m_currentLineNumberFormat = fs.toTextCharFormat(QLatin1String(Constants::C_CURRENT_LINE_NUMBER));
d->m_linkFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_LINK));
d->m_ifdefedOutFormat = fs.toTextCharFormat(QLatin1String(Constants::C_DISABLED_CODE));
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
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());
// Matching braces
d->m_matchFormat.setForeground(parenthesesFormat.foreground());
d->m_rangeFormat.setBackground(parenthesesFormat.background());
slotUpdateExtraAreaWidth(); // Adjust to new font width
updateCurrentLineHighlight(); // Make sure it takes the new color
}
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);
setRevisionsVisible(ds.m_markTextChanges);
setMouseNavigationEnabled(ds.m_mouseNavigation);
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;
if (!ds.m_highlightBlocks) {
d->extraAreaHighlightCollapseBlockNumber = d->extraAreaHighlightCollapseColumn = -1;
d->m_highlightBlocksInfo = BaseTextEditorPrivateHighlightBlocks();
}
slotCursorPositionChanged();
}
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());
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
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();
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
}
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();
}
QMimeData *BaseTextEditor::createMimeDataFromSelection() const
{
if (d->m_inBlockSelectionMode) {
QMimeData *mimeData = new QMimeData;
QString text = d->copyBlockSelection();
mimeData->setData(QLatin1String("application/vnd.nokia.qtcreator.vblocktext"), text.toUtf8());
mimeData->setText(text); // for exchangeability
return mimeData;
} else if (textCursor().hasSelection()){
QTextCursor cursor = textCursor();
QMimeData *mimeData = new QMimeData;
QString text = cursor.selectedText();
convertToPlainText(text);
mimeData->setText(text);
/*
Try to figure out whether we are copying an entire block, and store the complete block
including indentation in the qtcreator.blocktext mimetype.
*/
QTextCursor selstart = cursor;
selstart.setPosition(cursor.selectionStart());
QTextCursor selend = cursor;
selend.setPosition(cursor.selectionEnd());
const TabSettings &ts = d->m_document->tabSettings();
bool startOk = ts.cursorIsAtBeginningOfLine(selstart);
bool multipleBlocks = (selend.block() != selstart.block());
selstart.movePosition(QTextCursor::StartOfBlock);
if (ts.cursorIsAtBeginningOfLine(selend))
selend.movePosition(QTextCursor::StartOfBlock);
cursor.setPosition(selstart.position());
cursor.setPosition(selend.position(), QTextCursor::KeepAnchor);
text = cursor.selectedText();
mimeData->setData(QLatin1String("application/vnd.nokia.qtcreator.blocktext"), text.toUtf8());
}
return mimeData;
}
bool BaseTextEditor::canInsertFromMimeData(const QMimeData *source) const
{
return QPlainTextEdit::canInsertFromMimeData(source);
}
void BaseTextEditor::insertFromMimeData(const QMimeData *source)
{
if (isReadOnly())
return;
if (source->hasFormat(QLatin1String("application/vnd.nokia.qtcreator.vblocktext"))) {
QString text = QString::fromUtf8(source->data(QLatin1String("application/vnd.nokia.qtcreator.vblocktext")));
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
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;
}
QString text = source->text();
if (text.isEmpty())
return;
const TabSettings &ts = d->m_document->tabSettings();
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
cursor.insertText(text);
cursor.endEditBlock();
setTextCursor(cursor);
return;
}
cursor.beginEditBlock();
bool insertAtBeginningOfLine = ts.cursorIsAtBeginningOfLine(cursor);
if (insertAtBeginningOfLine
&& source->hasFormat(QLatin1String("application/vnd.nokia.qtcreator.blocktext"))) {
text = QString::fromUtf8(source->data(QLatin1String("application/vnd.nokia.qtcreator.blocktext")));
if (text.isEmpty())
return;
int reindentBlockStart = cursor.blockNumber() + (insertAtBeginningOfLine?0:1);
bool hasFinalNewline = (text.endsWith(QLatin1Char('\n'))
|| text.endsWith(QChar::ParagraphSeparator)
|| text.endsWith(QLatin1Char('\r')));
if (insertAtBeginningOfLine
&& hasFinalNewline) // since we'll add a final newline, preserve current line's indentation
cursor.setPosition(cursor.block().position());
int cursorPosition = cursor.position();
cursor.insertText(text);
int reindentBlockEnd = cursor.blockNumber() - (hasFinalNewline?1:0);
if (reindentBlockStart < reindentBlockEnd
|| (reindentBlockStart == reindentBlockEnd
&& (!insertAtBeginningOfLine || hasFinalNewline))) {
if (insertAtBeginningOfLine && !hasFinalNewline) {
QTextCursor unnecessaryWhitespace = cursor;
unnecessaryWhitespace.setPosition(cursorPosition);
unnecessaryWhitespace.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
unnecessaryWhitespace.removeSelectedText();
QTextCursor c = cursor;
c.setPosition(cursor.document()->findBlockByNumber(reindentBlockStart).position());
c.setPosition(cursor.document()->findBlockByNumber(reindentBlockEnd).position(),
QTextCursor::KeepAnchor);
reindent(document(), c);
}
cursor.endEditBlock();
setTextCursor(cursor);
}
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, Find::IFindSupport::FindFlags)),
editor, SLOT(highlightSearchResults(QString, Find::IFindSupport::FindFlags)));
connect(baseTextFind, SIGNAL(findScopeChanged(QTextCursor)), editor, SLOT(setFindScope(QTextCursor)));
aggregate->add(baseTextFind);
aggregate->add(editor);
#endif
m_cursorPositionLabel = new Utils::LineColumnLabel;
QHBoxLayout *l = new QHBoxLayout;
QWidget *w = new QWidget;
l->setMargin(0);
l->setContentsMargins(5, 0, 5, 0);
l->addStretch(0);
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;
}
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
{
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();
m_cursorPositionLabel->setText(tr("Line: %1, Col: %2").arg(line).arg(e->tabSettings().columnAt(block.text(), column)+1),
tr("Line: %1, Col: 999").arg(e->blockCount()));
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
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();
}
}