Newer
Older
const TabSettings &ts = d->m_document->tabSettings();
int extraBlocks = paragraphSeparatorAboutToBeInserted(cursor); // virtual
cursor.insertBlock();
QString previousBlockText = cursor.block().text();
cursor.insertBlock();
cursor.insertText(ts.indentationString(previousBlockText));
if (extraBlocks > 0) {
QTextCursor ensureVisible = cursor;
while (extraBlocks > 0) {
--extraBlocks;
ensureVisible.movePosition(QTextCursor::NextBlock);
}
setTextCursor(ensureVisible);
}
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
} else if (!ro
&& (e == QKeySequence::MoveToStartOfBlock
|| e == QKeySequence::SelectStartOfBlock)){
if ((e->modifiers() & (Qt::AltModifier | Qt::ShiftModifier)) == (Qt::AltModifier | Qt::ShiftModifier))
d->m_lastEventWasBlockSelectionEvent = true;
handleHomeKey(e == QKeySequence::SelectStartOfBlock);
e->accept();
return;
} else if (!ro
&& (e == QKeySequence::MoveToStartOfLine
|| e == QKeySequence::SelectStartOfLine)){
if ((e->modifiers() & (Qt::AltModifier | Qt::ShiftModifier)) == (Qt::AltModifier | Qt::ShiftModifier))
d->m_lastEventWasBlockSelectionEvent = true;
QTextCursor cursor = textCursor();
if (QTextLayout *layout = cursor.block().layout()) {
if (layout->lineForTextPosition(cursor.position() - cursor.block().position()).lineNumber() == 0) {
handleHomeKey(e == QKeySequence::SelectStartOfLine);
e->accept();
return;
}
}
} else if (!ro
&& e == QKeySequence::DeleteStartOfWord
&& d->m_document->tabSettings().m_autoIndent
&& !textCursor().hasSelection()){
e->accept();
QTextCursor c = textCursor();
int pos = c.position();
c.movePosition(QTextCursor::PreviousWord);
int targetpos = c.position();
forever {
handleBackspaceKey();
int cpos = textCursor().position();
if (cpos == pos || cpos <= targetpos)
break;
pos = cpos;
}
return;
case Qt::Key_Dollar: {
d->m_overlay->setVisible(!d->m_overlay->isVisible());
d->m_overlay->setCursor(textCursor());
e->accept();
QTextCursor cursor = textCursor();
int newPosition;
if (d->m_document->tabSettings().tabShouldIndent(document(), cursor, &newPosition)) {
if (newPosition != cursor.position() && !cursor.hasSelection()) {
cursor.setPosition(newPosition);
setTextCursor(cursor);
}
indent(document(), cursor, QChar::Null);
} else {
indentOrUnindent(e->key() == Qt::Key_Tab);
}
if ((e->modifiers() & (Qt::ControlModifier
| Qt::ShiftModifier
| Qt::AltModifier
| Qt::MetaModifier)) == Qt::NoModifier
&& !textCursor().hasSelection()) {
handleBackspaceKey();
e->accept();
return;
}
break;
case Qt::Key_Up:
case Qt::Key_Down:
if (e->modifiers() & Qt::ControlModifier) {
verticalScrollBar()->triggerAction(
e->key() == Qt::Key_Up ? QAbstractSlider::SliderSingleStepSub :
QAbstractSlider::SliderSingleStepAdd);
e->accept();
return;
}
// fall through
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
if ((e->modifiers() & (Qt::AltModifier | Qt::ShiftModifier)) == (Qt::AltModifier | Qt::ShiftModifier)) {
d->m_lastEventWasBlockSelectionEvent = true;
if (d->m_inBlockSelectionMode) {
if (e->key() == Qt::Key_Right && textCursor().atBlockEnd()) {
d->m_blockSelectionExtraX++;
viewport()->update();
e->accept();
return;
} else if (e->key() == Qt::Key_Left && d->m_blockSelectionExtraX > 0) {
d->m_blockSelectionExtraX--;
e->accept();
viewport()->update();
return;
}
}
e = new QKeyEvent(
e->type(),
e->key(),
e->modifiers() & ~Qt::AltModifier,
e->text(),
e->isAutoRepeat(),
e->count()
);
}
#endif
break;
case Qt::Key_PageUp:
case Qt::Key_PageDown:
if (e->modifiers() == Qt::ControlModifier) {
verticalScrollBar()->triggerAction(
e->key() == Qt::Key_PageUp ? QAbstractSlider::SliderPageStepSub :
QAbstractSlider::SliderPageStepAdd);
e->accept();
return;
}
break;
default:
break;
}
if (d->m_inBlockSelectionMode) {
QString text = e->text();
if (!text.isEmpty() && (text.at(0).isPrint() || text.at(0) == QLatin1Char('\t'))) {
d->removeBlockSelection(text);
goto skip_event;
}
}
if (ro || e->text().isEmpty() || !e->text().at(0).isPrint()) {
QPlainTextEdit::keyPressEvent(e);
} else {
QTextCursor cursor = textCursor();
QString text = e->text();
QString autoText = autoComplete(cursor, text);
QChar electricChar;
if (d->m_document->tabSettings().m_autoIndent) {
foreach (QChar c, text) {
if (isElectricCharacter(c)) {
electricChar = c;
break;
}
}
}
bool doEditBlock = !(electricChar.isNull() && autoText.isEmpty());
if (doEditBlock)
int pos = cursor.position();
cursor.insertText(autoText);
cursor.setPosition(pos);
}
if (!electricChar.isNull())
if (doEditBlock)
skip_event:
if (!ro && e->key() == Qt::Key_Delete && d->m_parenthesesMatchingEnabled)
d->m_parenthesesMatchingTimer->start(50);
if (!ro && d->m_contentsChanged && !e->text().isEmpty() && e->text().at(0).isPrint())
emit requestAutoCompletion(editableInterface(), false);
if (e != original_e)
delete e;
}
void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
{
// workaround for QTextControl bug
bool selectionChange = cursor.hasSelection() || textCursor().hasSelection();
QPlainTextEdit::setTextCursor(cursor);
if (selectionChange)
slotSelectionChanged();
}
void BaseTextEditor::gotoLine(int line, int column)
d->m_lastCursorChangeWasInteresting = false; // avoid adding the previous position to history
const int blockNumber = line - 1;
const QTextBlock &block = document()->findBlockByNumber(blockNumber);
if (block.isValid()) {
QTextCursor cursor(block);
if (column > 0) {
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, column);
} else {
int pos = cursor.position();
while (characterAt(pos).category() == QChar::Separator_Space) {
++pos;
}
cursor.setPosition(pos);
}
setTextCursor(cursor);
centerCursor();
}
saveCurrentCursorPositionForNavigation();
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
}
int BaseTextEditor::position(ITextEditor::PositionOperation posOp, int at) const
{
QTextCursor tc = textCursor();
if (at != -1)
tc.setPosition(at);
if (posOp == ITextEditor::Current)
return tc.position();
switch (posOp) {
case ITextEditor::EndOfLine:
tc.movePosition(QTextCursor::EndOfLine);
return tc.position();
case ITextEditor::StartOfLine:
tc.movePosition(QTextCursor::StartOfLine);
return tc.position();
case ITextEditor::Anchor:
if (tc.hasSelection())
return tc.anchor();
break;
case ITextEditor::EndOfDoc:
tc.movePosition(QTextCursor::End);
return tc.position();
default:
break;
}
return -1;
}
void BaseTextEditor::convertPosition(int pos, int *line, int *column) const
{
QTextBlock block = document()->findBlock(pos);
if (!block.isValid()) {
(*line) = -1;
(*column) = -1;
} else {
(*line) = block.blockNumber() + 1;
(*column) = pos - block.position();
}
}
QChar BaseTextEditor::characterAt(int pos) const
{
return document()->characterAt(pos);
}
bool BaseTextEditor::event(QEvent *e)
{
e->ignore(); // we are a really nice citizen
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
return true;
default:
break;
}
return QPlainTextEdit::event(e);
}
void BaseTextEditor::duplicateFrom(BaseTextEditor *editor)
{
if (this == editor)
return;
setDisplayName(editor->displayName());
d->m_revisionsVisible = editor->d->m_revisionsVisible;
if (d->m_document == editor->d->m_document)
return;
d->setupDocumentSignals(editor->d->m_document);
d->m_document = editor->d->m_document;
}
QString BaseTextEditor::displayName() const
{
return d->m_displayName;
}
void BaseTextEditor::setDisplayName(const QString &title)
{
d->m_displayName = title;
}
BaseTextDocument *BaseTextEditor::baseTextDocument() const
{
return d->m_document;
}
void BaseTextEditor::setBaseTextDocument(BaseTextDocument *doc)
{
if (doc) {
d->setupDocumentSignals(doc);
d->m_document = doc;
}
}
void BaseTextEditor::memorizeCursorPosition()
{
d->m_tempState = saveState();
}
void BaseTextEditor::restoreCursorPosition()
{
restoreState(d->m_tempState);
}
QByteArray BaseTextEditor::saveState() const
{
QByteArray state;
QDataStream stream(&state, QIODevice::WriteOnly);
stream << 0; // version number
stream << verticalScrollBar()->value();
stream << horizontalScrollBar()->value();
int line, column;
convertPosition(textCursor().position(), &line, &column);
stream << line;
stream << column;
return state;
}
bool BaseTextEditor::restoreState(const QByteArray &state)
{
int version;
int vval;
int hval;
int lval;
int cval;
QDataStream stream(state);
stream >> version;
stream >> vval;
stream >> hval;
stream >> lval;
stream >> cval;
d->m_lastCursorChangeWasInteresting = false; // avoid adding last position to history
gotoLine(lval, cval);
verticalScrollBar()->setValue(vval);
horizontalScrollBar()->setValue(hval);
saveCurrentCursorPositionForNavigation();
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
return true;
}
void BaseTextEditor::setDefaultPath(const QString &defaultPath)
{
baseTextDocument()->setDefaultPath(defaultPath);
}
void BaseTextEditor::setSuggestedFileName(const QString &suggestedFileName)
{
baseTextDocument()->setSuggestedFileName(suggestedFileName);
}
void BaseTextEditor::setParenthesesMatchingEnabled(bool b)
{
d->m_parenthesesMatchingEnabled = b;
}
bool BaseTextEditor::isParenthesesMatchingEnabled() const
{
return d->m_parenthesesMatchingEnabled;
}
void BaseTextEditor::setHighlightCurrentLine(bool b)
{
d->m_highlightCurrentLine = b;
updateCurrentLineHighlight();
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
}
bool BaseTextEditor::highlightCurrentLine() const
{
return d->m_highlightCurrentLine;
}
void BaseTextEditor::setLineNumbersVisible(bool b)
{
d->m_lineNumbersVisible = b;
slotUpdateExtraAreaWidth();
}
bool BaseTextEditor::lineNumbersVisible() const
{
return d->m_lineNumbersVisible;
}
void BaseTextEditor::setMarksVisible(bool b)
{
d->m_marksVisible = b;
slotUpdateExtraAreaWidth();
}
bool BaseTextEditor::marksVisible() const
{
return d->m_marksVisible;
}
void BaseTextEditor::setRequestMarkEnabled(bool b)
{
d->m_requestMarkEnabled = b;
}
bool BaseTextEditor::requestMarkEnabled() const
{
return d->m_requestMarkEnabled;
}
void BaseTextEditor::setLineSeparatorsAllowed(bool b)
{
d->m_lineSeparatorsAllowed = b;
}
bool BaseTextEditor::lineSeparatorsAllowed() const
{
return d->m_lineSeparatorsAllowed;
}
void BaseTextEditor::setCodeFoldingVisible(bool b)
{
d->m_codeFoldingVisible = b && d->m_codeFoldingSupported;
slotUpdateExtraAreaWidth();
}
bool BaseTextEditor::codeFoldingVisible() const
{
return d->m_codeFoldingVisible;
}
/**
* Sets whether code folding is supported by the syntax highlighter. When not
* supported (the default), this makes sure the code folding is not shown.
*
* Needs to be called before calling setCodeFoldingVisible.
*/
void BaseTextEditor::setCodeFoldingSupported(bool b)
{
d->m_codeFoldingSupported = b;
}
bool BaseTextEditor::codeFoldingSupported() const
{
return d->m_codeFoldingSupported;
}
void BaseTextEditor::setMouseNavigationEnabled(bool b)
{
d->m_mouseNavigationEnabled = b;
}
bool BaseTextEditor::mouseNavigationEnabled() const
{
return d->m_mouseNavigationEnabled;
}
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
void BaseTextEditor::setRevisionsVisible(bool b)
{
d->m_revisionsVisible = b;
slotUpdateExtraAreaWidth();
}
bool BaseTextEditor::revisionsVisible() const
{
return d->m_revisionsVisible;
}
void BaseTextEditor::setVisibleWrapColumn(int column)
{
d->m_visibleWrapColumn = column;
viewport()->update();
}
int BaseTextEditor::visibleWrapColumn() const
{
return d->m_visibleWrapColumn;
}
//--------- BaseTextEditorPrivate -----------
BaseTextEditorPrivate::BaseTextEditorPrivate()
:
m_contentsChanged(false),
m_lastCursorChangeWasInteresting(false),
m_document(new BaseTextDocument()),
m_parenthesesMatchingEnabled(false),
m_extraArea(0),
m_mouseOnCollapsedMarker(false),
m_codeFoldingSupported(false),
m_mouseNavigationEnabled(true),
m_revisionsVisible(false),
m_lineNumbersVisible(true),
m_highlightCurrentLine(true),
m_requestMarkEnabled(true),
m_lineSeparatorsAllowed(false),
m_visibleWrapColumn(0),
m_linkPressed(false),
m_editable(0),
m_actionHack(0),
m_inBlockSelectionMode(false),
m_lastEventWasBlockSelectionEvent(false),
m_blockSelectionExtraX(0),
m_moveLineUndoHack(false),
m_cursorBlockNumber(-1)
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
{
}
BaseTextEditorPrivate::~BaseTextEditorPrivate()
{
}
void BaseTextEditorPrivate::setupDocumentSignals(BaseTextDocument *document)
{
BaseTextDocument *oldDocument = q->baseTextDocument();
if (oldDocument) {
q->disconnect(oldDocument->document(), 0, q, 0);
q->disconnect(oldDocument, 0, q, 0);
}
QTextDocument *doc = document->document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
if (!documentLayout) {
QTextOption opt = doc->defaultTextOption();
opt.setTextDirection(Qt::LeftToRight);
opt.setFlags(opt.flags() | QTextOption::IncludeTrailingSpaces
| QTextOption::AddSpaceForLineAndParagraphSeparators
);
doc->setDefaultTextOption(opt);
documentLayout = new TextEditDocumentLayout(doc);
doc->setDocumentLayout(documentLayout);
}
q->setDocument(doc);
QObject::connect(documentLayout, SIGNAL(updateBlock(QTextBlock)), q, SLOT(slotUpdateBlockNotify(QTextBlock)));
QObject::connect(q, SIGNAL(requestBlockUpdate(QTextBlock)), documentLayout, SIGNAL(updateBlock(QTextBlock)));
QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(changed()));
QObject::connect(doc, SIGNAL(contentsChange(int,int,int)), q,
SLOT(editorContentsChange(int,int,int)), Qt::DirectConnection);
QObject::connect(document, SIGNAL(changed()), q, SIGNAL(changed()));
QObject::connect(document, SIGNAL(titleChanged(QString)), q, SLOT(setDisplayName(const QString &)));
QObject::connect(document, SIGNAL(aboutToReload()), q, SLOT(memorizeCursorPosition()));
QObject::connect(document, SIGNAL(reloaded()), q, SLOT(restoreCursorPosition()));
q->slotUpdateExtraAreaWidth();
}
bool Parenthesis::hasClosingCollapse(const Parentheses &parentheses)
{
return closeCollapseAtPos(parentheses) >= 0;
}
int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses)
{
int depth = 0;
for (int i = 0; i < parentheses.size(); ++i) {
const Parenthesis &p = parentheses.at(i);
if (p.chr == QLatin1Char('{')
|| p.chr == QLatin1Char('+')
|| p.chr == QLatin1Char('[')) {
} else if (p.chr == QLatin1Char('}')
|| p.chr == QLatin1Char('-')
|| p.chr == QLatin1Char(']')) {
if (--depth < 0)
return p.pos;
}
}
return -1;
}
int Parenthesis::collapseAtPos(const Parentheses &parentheses, QChar *character)
{
int result = -1;
QChar c;
int depth = 0;
for (int i = 0; i < parentheses.size(); ++i) {
const Parenthesis &p = parentheses.at(i);
if (p.chr == QLatin1Char('{')
|| p.chr == QLatin1Char('+')
|| p.chr == QLatin1Char('[')) {
if (depth == 0) {
result = p.pos;
c = p.chr;
}
++depth;
} else if (p.chr == QLatin1Char('}')
|| p.chr == QLatin1Char('-')
|| p.chr == QLatin1Char(']')) {
if (--depth < 0)
depth = 0;
result = -1;
}
}
if (result >= 0 && character)
*character = c;
return result;
}
int TextBlockUserData::collapseAtPos(QChar *character) const
return Parenthesis::collapseAtPos(m_parentheses, character);
int TextBlockUserData::braceDepthDelta() const
{
int delta = 0;
for (int i = 0; i < m_parentheses.size(); ++i) {
switch (m_parentheses.at(i).chr.unicode()) {
case '{': case '+': case '[': ++delta; break;
case '}': case '-': case ']': --delta; break;
default: break;
}
}
return delta;
}
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
void TextEditDocumentLayout::setParentheses(const QTextBlock &block, const Parentheses &parentheses)
{
if (parentheses.isEmpty()) {
if (TextBlockUserData *userData = testUserData(block))
userData->clearParentheses();
} else {
userData(block)->setParentheses(parentheses);
}
}
Parentheses TextEditDocumentLayout::parentheses(const QTextBlock &block)
{
if (TextBlockUserData *userData = testUserData(block))
return userData->parentheses();
return Parentheses();
}
bool TextEditDocumentLayout::hasParentheses(const QTextBlock &block)
{
if (TextBlockUserData *userData = testUserData(block))
return userData->hasParentheses();
return false;
}
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
int TextEditDocumentLayout::braceDepthDelta(const QTextBlock &block)
{
if (TextBlockUserData *userData = testUserData(block))
return userData->braceDepthDelta();
return 0;
}
int TextEditDocumentLayout::braceDepth(const QTextBlock &block)
{
int state = block.userState();
if (state == -1)
return 0;
return state >> 8;
}
void TextEditDocumentLayout::setBraceDepth(QTextBlock &block, int depth)
{
int state = block.userState();
if (state == -1)
state = 0;
state = state & 0xff;
block.setUserState((depth << 8) | state);
}
void TextEditDocumentLayout::changeBraceDepth(QTextBlock &block, int delta)
{
if (delta)
setBraceDepth(block, braceDepth(block) + delta);
}
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
bool TextEditDocumentLayout::setIfdefedOut(const QTextBlock &block)
{
return userData(block)->setIfdefedOut();
}
bool TextEditDocumentLayout::clearIfdefedOut(const QTextBlock &block)
{
if (TextBlockUserData *userData = testUserData(block))
return userData->clearIfdefedOut();
return false;
}
bool TextEditDocumentLayout::ifdefedOut(const QTextBlock &block)
{
if (TextBlockUserData *userData = testUserData(block))
return userData->ifdefedOut();
return false;
}
TextEditDocumentLayout::TextEditDocumentLayout(QTextDocument *doc)
:QPlainTextDocumentLayout(doc) {
lastSaveRevision = 0;
hasMarks = 0;
}
TextEditDocumentLayout::~TextEditDocumentLayout()
{
}
QRectF TextEditDocumentLayout::blockBoundingRect(const QTextBlock &block) const
{
QRectF r = QPlainTextDocumentLayout::blockBoundingRect(block);
return r;
}
bool BaseTextEditor::viewportEvent(QEvent *event)
{
if (event->type() == QEvent::ContextMenu) {
const QContextMenuEvent *ce = static_cast<QContextMenuEvent*>(event);
if (ce->reason() == QContextMenuEvent::Mouse && !textCursor().hasSelection())
setTextCursor(cursorForPosition(ce->pos()));
} else if (event->type() == QEvent::ToolTip) {
const QHelpEvent *he = static_cast<QHelpEvent*>(event);
if (QApplication::keyboardModifiers() & Qt::ControlModifier)
return true; // eat tooltip event when control is pressed
const QPoint &pos = he->pos();
// Allow plugins to show tooltips
const QTextCursor &c = cursorForPosition(pos);
QPoint cursorPos = mapToGlobal(cursorRect(c).bottomRight() + QPoint(1,1));
cursorPos.setX(cursorPos.x() + d->m_extraArea->width());
editableInterface(); // create if necessary
emit d->m_editable->tooltipRequested(editableInterface(), cursorPos, c.position());
return true;
}
return QPlainTextEdit::viewportEvent(event);
}
void BaseTextEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
d->m_extraArea->setGeometry(
QStyle::visualRect(layoutDirection(), cr,
QRect(cr.left(), cr.top(), extraAreaWidth(), cr.height())));
}
QRect BaseTextEditor::collapseBox()
if (d->m_highlightBlocksInfo.isEmpty() || d->extraAreaHighlightCollapseBlockNumber < 0)
return QRect();
QTextBlock begin = document()->findBlockByNumber(d->m_highlightBlocksInfo.open.last());
if (TextBlockUserData::hasCollapseAfter(begin.previous()))
begin = begin.previous();
QTextBlock end = document()->findBlockByNumber(d->m_highlightBlocksInfo.close.first());
if (!begin.isValid() || !end.isValid())
return QRect();
QRectF br = blockBoundingGeometry(begin).translated(contentOffset());
QRectF er = blockBoundingGeometry(end).translated(contentOffset());
return QRect(d->m_extraArea->width() - collapseBoxWidth(fontMetrics()),
collapseBoxWidth(fontMetrics()),
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
}
QTextBlock BaseTextEditor::collapsedBlockAt(const QPoint &pos, QRect *box) const {
QPointF offset(contentOffset());
QTextBlock block = firstVisibleBlock();
int top = (int)blockBoundingGeometry(block).translated(offset).top();
int bottom = top + (int)blockBoundingRect(block).height();
int viewportHeight = viewport()->height();
while (block.isValid() && top <= viewportHeight) {
QTextBlock nextBlock = block.next();
if (block.isVisible() && bottom >= 0) {
if (nextBlock.isValid() && !nextBlock.isVisible()) {
QTextLayout *layout = block.layout();
QTextLine line = layout->lineAt(layout->lineCount()-1);
QRectF lineRect = line.naturalTextRect().translated(offset.x(), top);
lineRect.adjust(0, 0, -1, -1);
QRectF collapseRect(lineRect.right() + 12,
lineRect.top(),
fontMetrics().width(QLatin1String(" {...}; ")),
lineRect.height());
if (collapseRect.contains(pos)) {
QTextBlock result = block;
if (box)
*box = collapseRect.toAlignedRect();
return result;
} else {
block = nextBlock;
while (nextBlock.isValid() && !nextBlock.isVisible()) {
block = nextBlock;
nextBlock = block.next();
}
}
}
}
block = nextBlock;
top = bottom;
bottom = top + (int)blockBoundingRect(block).height();
}
return QTextBlock();
}
void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block,
TextEditorOverlay *overlay,
QVector<QTextLayout::FormatRange> *selections )
{
if (m_searchExpr.isEmpty())
return;
QString text = block.text();
text.replace(QChar::Nbsp, QLatin1Char(' '));
int idx = -1;
while (idx < text.length()) {
idx = m_searchExpr.indexIn(text, idx + 1);
if (idx < 0)
break;
int l = m_searchExpr.matchedLength();
if ((m_findFlags & Find::IFindSupport::FindWholeWords)
&& ((idx && text.at(idx-1).isLetterOrNumber())
|| (idx + l < text.length() && text.at(idx + l).isLetterOrNumber())))
continue;
if (m_findScope.isNull()
|| (block.position() + idx >= m_findScope.selectionStart()
&& block.position() + idx + l <= m_findScope.selectionEnd())) {
if (selections) {
QTextLayout::FormatRange selection;
selection.start = idx;
selection.length = l;
selection.format = m_searchResultFormat;
selections->append(selection);
}
overlay->addOverlaySelection(block.position() + idx,
block.position() + idx + l,
m_searchResultFormat.background().color().darker(120),
QColor());
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
}
}
}
namespace TextEditor {
namespace Internal {
struct BlockSelectionData {
int selectionIndex;
int selectionStart;
int selectionEnd;
int firstColumn;
int lastColumn;
};
}
}
void BaseTextEditorPrivate::clearBlockSelection()
{
if (m_inBlockSelectionMode) {
m_inBlockSelectionMode = false;
QTextCursor cursor = q->textCursor();
cursor.clearSelection();
q->setTextCursor(cursor);
}
}
QString BaseTextEditorPrivate::copyBlockSelection()
{
QString text;
QTextCursor cursor = q->textCursor();
if (!cursor.hasSelection())
return text;
QTextDocument *doc = q->document();
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
QTextBlock startBlock = doc->findBlock(start);
int columnA = start - startBlock.position();
QTextBlock endBlock = doc->findBlock(end);
int columnB = end - endBlock.position();
int firstColumn = qMin(columnA, columnB);
int lastColumn = qMax(columnA, columnB) + m_blockSelectionExtraX;
QTextBlock block = startBlock;
for (;;) {
cursor.setPosition(block.position() + qMin(block.length()-1, firstColumn));
cursor.setPosition(block.position() + qMin(block.length()-1, lastColumn), QTextCursor::KeepAnchor);
text += cursor.selectedText();
if (block == endBlock)
break;
text += QLatin1Char('\n');
block = block.next();
}
return text;
}
void BaseTextEditorPrivate::removeBlockSelection(const QString &text)
{
QTextCursor cursor = q->textCursor();
if (!cursor.hasSelection())
return;
QTextDocument *doc = q->document();
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
QTextBlock startBlock = doc->findBlock(start);
int columnA = start - startBlock.position();
QTextBlock endBlock = doc->findBlock(end);
int columnB = end - endBlock.position();
int firstColumn = qMin(columnA, columnB);
int lastColumn = qMax(columnA, columnB) + m_blockSelectionExtraX;
cursor.clearSelection();
cursor.beginEditBlock();
QTextBlock block = startBlock;
for (;;) {
cursor.setPosition(block.position() + qMin(block.length()-1, firstColumn));
cursor.setPosition(block.position() + qMin(block.length()-1, lastColumn), QTextCursor::KeepAnchor);
cursor.removeSelectedText();
if (block == endBlock)
break;
block = block.next();
}
cursor.setPosition(start);
if (!text.isEmpty())
cursor.insertText(text);
cursor.endEditBlock();
q->setTextCursor(cursor);
}
void BaseTextEditorPrivate::moveCursorVisible(bool ensureVisible)
{
QTextCursor cursor = q->textCursor();
if (!cursor.block().isVisible()) {
cursor.setVisualNavigation(true);