Newer
Older
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
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
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();
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
}
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
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
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
1304
1305
1306
1307
1308
1309
1310
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();
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
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();
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
1388
1389
1390
1391
1392
1393
}
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;
}
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
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_showingLink(false),
m_editable(0),
m_actionHack(0),
m_inBlockSelectionMode(false),
m_lastEventWasBlockSelectionEvent(false),
m_blockSelectionExtraX(0),
m_moveLineUndoHack(false)
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
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
{
}
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() const
{
return Parenthesis::collapseAtPos(m_parentheses);
}
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;
}
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
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;
}
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
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);
}
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
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
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
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);
QRect cr = viewport()->rect();
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()),
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
}
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,
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)
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
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
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
&& ((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())) {
QTextLayout::FormatRange selection;
selection.start = idx;
selection.length = l;
selection.format = m_searchResultFormat;
selections->append(selection);
}
}
}
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);
cursor.movePosition(QTextCursor::Up);
q->setTextCursor(cursor);
}
if (ensureVisible)
q->ensureCursorVisible();
}
static QColor calcBlendColor(const QColor &baseColor, int level, int count)
{
QColor color80;
QColor color90;
if (baseColor.value() > 128) {
const int f90 = 15;
const int f80 = 30;
color80.setRgb(qMax(0, baseColor.red() - f80),
qMax(0, baseColor.green() - f80),
qMax(0, baseColor.blue() - f80));
color90.setRgb(qMax(0, baseColor.red() - f90),
qMax(0, baseColor.green() - f90),
qMax(0, baseColor.blue() - f90));
const int f90 = 20;
const int f80 = 40;
color80.setRgb(qMin(255, baseColor.red() + f80),
qMin(255, baseColor.green() + f80),
qMin(255, baseColor.blue() + f80));
color90.setRgb(qMin(255, baseColor.red() + f90),
qMin(255, baseColor.green() + f90),
qMin(255, baseColor.blue() + f90));
}
if (level == count)
return baseColor;
if (level == 0)
return color80;
if (level == count - 1)
return color90;
const int blendFactor = level * (256 / (count - 2));
return QColor(
(color90.red() * blendFactor + color80.red() * (256 - blendFactor)) / 256,
(color90.green() * blendFactor + color80.green() * (256 - blendFactor)) / 256,
(color90.blue() * blendFactor + color80.blue() * (256 - blendFactor)) / 256);
}
void BaseTextEditor::paintEvent(QPaintEvent *e)
{
/*
Here comes an almost verbatim copy of
QPlainTextEdit::paintEvent() so we can adjust the extra
selections dynamically to indicate all search results.
*/
//begin QPlainTextEdit::paintEvent()
QPainter painter(viewport());
QTextDocument *doc = document();
TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout());
bool hasMainSelection = textCursor().hasSelection();
QRect er = e->rect();
QRect viewportRect = viewport()->rect();
const QColor baseColor = palette().base().color();
qreal lineX = 0;
if (d->m_visibleWrapColumn > 0) {
lineX = fontMetrics().averageCharWidth() * d->m_visibleWrapColumn + offset.x() + 4;
painter.fillRect(QRectF(lineX, 0, viewportRect.width() - lineX, viewportRect.height()),
d->m_ifdefedOutFormat.background());
// // keep right margin clean from full-width selection
// int maxX = offset.x() + qMax((qreal)viewportRect.width(), documentLayout->documentSize().width())
// - doc->documentMargin();
// er.setRight(qMin(er.right(), maxX));
// painter.setClipRect(er);