2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2009-12-02 10:12:41 +01:00
|
|
|
|
2009-11-25 15:55:45 +01:00
|
|
|
#include "texteditoroverlay.h"
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "texteditor.h"
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QPainter>
|
2020-03-12 09:26:58 +01:00
|
|
|
#include <QPainterPath>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextBlock>
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2018-09-18 18:41:37 +02:00
|
|
|
#include <algorithm>
|
2021-04-16 12:30:47 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2018-09-18 18:41:37 +02:00
|
|
|
|
2009-11-25 15:55:45 +01:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace TextEditor::Internal;
|
|
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
constexpr int borderWidth = 1;
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
TextEditorOverlay::TextEditorOverlay(TextEditorWidget *editor) :
|
2010-11-02 15:35:42 +01:00
|
|
|
QObject(editor),
|
|
|
|
|
m_visible(false),
|
2013-02-18 18:47:54 +01:00
|
|
|
m_alpha(true),
|
2010-11-02 15:35:42 +01:00
|
|
|
m_dropShadowWidth(2),
|
2010-11-29 17:25:01 +01:00
|
|
|
m_firstSelectionOriginalBegin(-1),
|
2010-11-02 15:35:42 +01:00
|
|
|
m_editor(editor),
|
|
|
|
|
m_viewport(editor->viewport())
|
|
|
|
|
{
|
2009-11-25 15:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextEditorOverlay::update()
|
|
|
|
|
{
|
|
|
|
|
if (m_visible)
|
|
|
|
|
m_viewport->update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextEditorOverlay::setVisible(bool b)
|
|
|
|
|
{
|
|
|
|
|
if (m_visible == b)
|
|
|
|
|
return;
|
2010-01-19 12:28:50 +01:00
|
|
|
m_visible = b;
|
2010-08-06 21:55:48 +02:00
|
|
|
if (!m_selections.isEmpty())
|
|
|
|
|
m_viewport->update();
|
2009-11-25 15:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextEditorOverlay::clear()
|
|
|
|
|
{
|
2009-12-02 10:57:05 +01:00
|
|
|
if (m_selections.isEmpty())
|
|
|
|
|
return;
|
2009-11-25 15:55:45 +01:00
|
|
|
m_selections.clear();
|
2010-11-29 17:25:01 +01:00
|
|
|
m_firstSelectionOriginalBegin = -1;
|
2009-11-25 15:55:45 +01:00
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-01 14:47:10 +01:00
|
|
|
void TextEditorOverlay::addOverlaySelection(int begin, int end,
|
2009-12-01 19:44:31 +01:00
|
|
|
const QColor &fg, const QColor &bg,
|
2010-08-05 15:01:20 +02:00
|
|
|
uint overlaySelectionFlags)
|
2009-11-25 15:55:45 +01:00
|
|
|
{
|
2009-11-30 13:56:54 +01:00
|
|
|
if (end < begin)
|
2009-11-25 15:55:45 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QTextDocument *document = m_editor->document();
|
|
|
|
|
|
|
|
|
|
OverlaySelection selection;
|
2009-12-01 14:47:10 +01:00
|
|
|
selection.m_fg = fg;
|
|
|
|
|
selection.m_bg = bg;
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2016-02-01 15:28:38 +01:00
|
|
|
selection.m_cursor_begin = QTextCursor(document);
|
2016-02-02 09:47:03 +01:00
|
|
|
selection.m_cursor_begin.setPosition(begin);
|
2016-02-01 15:28:38 +01:00
|
|
|
selection.m_cursor_end = QTextCursor(document);
|
2016-02-02 09:47:03 +01:00
|
|
|
selection.m_cursor_end.setPosition(end);
|
2010-08-05 15:01:20 +02:00
|
|
|
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (overlaySelectionFlags & ExpandBegin)
|
2012-06-11 17:57:01 +02:00
|
|
|
selection.m_cursor_begin.setKeepPositionOnInsert(true);
|
2010-01-19 12:28:50 +01:00
|
|
|
|
|
|
|
|
if (overlaySelectionFlags & LockSize)
|
2009-11-30 13:56:54 +01:00
|
|
|
selection.m_fixedLength = (end - begin);
|
|
|
|
|
|
2010-01-19 12:28:50 +01:00
|
|
|
selection.m_dropShadow = (overlaySelectionFlags & DropShadow);
|
|
|
|
|
|
2010-11-29 17:25:01 +01:00
|
|
|
if (m_selections.isEmpty())
|
|
|
|
|
m_firstSelectionOriginalBegin = begin;
|
|
|
|
|
else if (begin < m_firstSelectionOriginalBegin)
|
|
|
|
|
qWarning() << "overlay selections not in order";
|
|
|
|
|
|
2010-01-19 12:28:50 +01:00
|
|
|
m_selections.append(selection);
|
2009-11-25 15:55:45 +01:00
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-12-01 14:47:10 +01:00
|
|
|
void TextEditorOverlay::addOverlaySelection(const QTextCursor &cursor,
|
2010-01-19 12:28:50 +01:00
|
|
|
const QColor &fg, const QColor &bg,
|
2010-08-05 15:01:20 +02:00
|
|
|
uint overlaySelectionFlags)
|
2009-11-25 15:55:45 +01:00
|
|
|
{
|
2010-08-05 15:01:20 +02:00
|
|
|
addOverlaySelection(cursor.selectionStart(), cursor.selectionEnd(), fg, bg, overlaySelectionFlags);
|
2009-11-25 15:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QRect TextEditorOverlay::rect() const
|
|
|
|
|
{
|
|
|
|
|
return m_viewport->rect();
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-01 14:47:10 +01:00
|
|
|
QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, const QTextCursor &end,
|
2010-08-05 15:01:20 +02:00
|
|
|
const QRect &clip)
|
2009-11-25 15:55:45 +01:00
|
|
|
{
|
|
|
|
|
if (begin.isNull() || end.isNull() || begin.position() > end.position())
|
2009-12-01 14:47:10 +01:00
|
|
|
return QPainterPath();
|
2009-11-25 15:55:45 +01:00
|
|
|
|
|
|
|
|
QPointF offset = m_editor->contentOffset();
|
|
|
|
|
QRect viewportRect = rect();
|
|
|
|
|
QTextDocument *document = m_editor->document();
|
|
|
|
|
|
2009-12-01 14:47:10 +01:00
|
|
|
if (m_editor->blockBoundingGeometry(begin.block()).translated(offset).top() > clip.bottom() + 10
|
2022-11-23 14:59:59 +01:00
|
|
|
|| m_editor->blockBoundingGeometry(end.block()).translated(offset).bottom()
|
|
|
|
|
< clip.top() - 10) {
|
2009-12-01 14:47:10 +01:00
|
|
|
return QPainterPath(); // nothing of the selection is visible
|
2022-11-23 14:59:59 +01:00
|
|
|
}
|
2010-04-12 20:49:40 +02:00
|
|
|
|
2009-11-25 15:55:45 +01:00
|
|
|
QTextBlock block = begin.block();
|
|
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
if (block.blockNumber() < m_editor->firstVisibleBlock().blockNumber() - 1)
|
|
|
|
|
block = document->findBlockByNumber(m_editor->firstVisibleBlock().blockNumber() - 1);
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2009-12-02 14:12:59 +01:00
|
|
|
if (begin.position() == end.position()) {
|
|
|
|
|
// special case empty selections
|
2009-11-25 15:55:45 +01:00
|
|
|
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
|
|
|
|
|
QTextLayout *blockLayout = block.layout();
|
2009-12-02 14:12:59 +01:00
|
|
|
int pos = begin.position() - begin.block().position();
|
|
|
|
|
QTextLine line = blockLayout->lineForTextPosition(pos);
|
2023-04-27 08:30:19 +02:00
|
|
|
QTC_ASSERT(line.isValid(), return {});
|
2009-12-02 14:12:59 +01:00
|
|
|
QRectF lineRect = line.naturalTextRect();
|
2022-11-23 14:59:59 +01:00
|
|
|
lineRect = lineRect.translated(blockGeometry.topLeft());
|
2009-12-02 14:12:59 +01:00
|
|
|
int x = line.cursorToX(pos);
|
2022-11-23 14:59:59 +01:00
|
|
|
lineRect.setLeft(x - borderWidth);
|
|
|
|
|
lineRect.setRight(x + borderWidth);
|
2023-04-17 10:52:45 +02:00
|
|
|
lineRect.setBottom(lineRect.bottom() + borderWidth);
|
2022-11-23 14:59:59 +01:00
|
|
|
QPainterPath path;
|
|
|
|
|
path.addRect(lineRect);
|
2023-04-17 10:52:45 +02:00
|
|
|
path.translate(offset);
|
2022-11-23 14:59:59 +01:00
|
|
|
return path;
|
2009-11-25 15:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
QPointF top; // *------|
|
|
|
|
|
QPointF left; // *---| |
|
|
|
|
|
QPointF right; // | |---*
|
|
|
|
|
QPointF bottom; // |------*
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
for (; block.isValid() && block.blockNumber() <= end.blockNumber(); block = block.next()) {
|
|
|
|
|
if (!block.isVisible())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
|
|
|
|
|
QTextLayout *blockLayout = block.layout();
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
int firstLine = 0;
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
int beginChar = 0;
|
|
|
|
|
if (block == begin.block()) {
|
|
|
|
|
beginChar = begin.positionInBlock();
|
2023-04-27 08:30:19 +02:00
|
|
|
QTextLine line = blockLayout->lineForTextPosition(beginChar);
|
|
|
|
|
QTC_ASSERT(line.isValid(), return {});
|
2022-11-23 14:59:59 +01:00
|
|
|
firstLine = line.lineNumber();
|
|
|
|
|
const int lineEnd = line.textStart() + line.textLength();
|
|
|
|
|
if (beginChar == lineEnd)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
int lastLine = blockLayout->lineCount() - 1;
|
|
|
|
|
int endChar = -1;
|
|
|
|
|
if (block == end.block()) {
|
|
|
|
|
endChar = end.positionInBlock();
|
2023-04-27 08:30:19 +02:00
|
|
|
QTextLine line = blockLayout->lineForTextPosition(endChar);
|
|
|
|
|
QTC_ASSERT(line.isValid(), return {});
|
|
|
|
|
lastLine = line.lineNumber();
|
2022-11-23 14:59:59 +01:00
|
|
|
if (endChar == beginChar)
|
|
|
|
|
break; // Do not expand overlay to empty selection at end
|
|
|
|
|
} else {
|
|
|
|
|
endChar = block.length();
|
|
|
|
|
while (endChar > beginChar
|
|
|
|
|
&& document->characterAt(block.position() + endChar - 1).isSpace())
|
|
|
|
|
--endChar;
|
|
|
|
|
}
|
2014-05-05 17:18:59 +03:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
for (int i = firstLine; i <= lastLine; ++i) {
|
2023-04-27 08:30:19 +02:00
|
|
|
QTextLine line = blockLayout->lineAt(i);
|
|
|
|
|
QTC_ASSERT(line.isValid(), return {});
|
2022-11-23 14:59:59 +01:00
|
|
|
QRectF lineRect = line.naturalTextRect();
|
|
|
|
|
if (i == firstLine && beginChar > 0)
|
|
|
|
|
lineRect.setLeft(line.cursorToX(beginChar));
|
|
|
|
|
if (line.lineNumber() == lastLine)
|
|
|
|
|
lineRect.setRight(line.cursorToX(endChar));
|
|
|
|
|
lineRect = lineRect.translated(blockGeometry.topLeft());
|
|
|
|
|
if (top.isNull())
|
|
|
|
|
top = lineRect.topLeft();
|
|
|
|
|
else if (left.isNull())
|
|
|
|
|
left = lineRect.topLeft();
|
|
|
|
|
else
|
|
|
|
|
left.setX(std::min(left.x(), lineRect.left()));
|
|
|
|
|
if (i == lastLine && block == end.block() && lineRect.right() <= right.x())
|
|
|
|
|
bottom = lineRect.bottomRight();
|
|
|
|
|
else
|
|
|
|
|
right = {std::max(lineRect.right(), right.x()), lineRect.bottom()};
|
|
|
|
|
}
|
2014-08-08 17:31:03 +03:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
if (blockGeometry.translated(offset).y() > 2 * viewportRect.height())
|
|
|
|
|
break;
|
2009-11-25 15:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
if (top.isNull())
|
|
|
|
|
return {};
|
2014-05-05 17:18:59 +03:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
if (bottom.isNull())
|
|
|
|
|
bottom = right;
|
2014-08-08 17:31:03 +03:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
if (left.isNull())
|
|
|
|
|
left = top;
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
if (right.isNull())
|
|
|
|
|
right = bottom;
|
2009-11-25 15:55:45 +01:00
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
constexpr QPointF marginOffset = {borderWidth, borderWidth};
|
|
|
|
|
right += marginOffset;
|
|
|
|
|
bottom += marginOffset;
|
2009-11-25 15:55:45 +01:00
|
|
|
|
|
|
|
|
QPainterPath path;
|
2022-11-23 14:59:59 +01:00
|
|
|
path.moveTo(top);
|
|
|
|
|
path.lineTo(right.x(), top.y());
|
|
|
|
|
path.lineTo(right);
|
|
|
|
|
path.lineTo(bottom.x(), right.y());
|
|
|
|
|
path.lineTo(bottom);
|
|
|
|
|
path.lineTo(left.x(), bottom.y());
|
|
|
|
|
path.lineTo(left);
|
|
|
|
|
path.lineTo(top.x(), left.y());
|
|
|
|
|
path.lineTo(top);
|
2009-11-25 15:55:45 +01:00
|
|
|
|
|
|
|
|
path.closeSubpath();
|
2009-12-01 14:47:10 +01:00
|
|
|
path.translate(offset);
|
2022-11-23 14:59:59 +01:00
|
|
|
return path;
|
2009-12-01 14:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
2009-12-01 19:44:31 +01:00
|
|
|
void TextEditorOverlay::paintSelection(QPainter *painter,
|
2022-03-17 07:09:51 +01:00
|
|
|
const OverlaySelection &selection,
|
|
|
|
|
const QRect &clip)
|
2009-12-01 14:47:10 +01:00
|
|
|
{
|
2009-12-01 19:44:31 +01:00
|
|
|
|
2010-01-19 12:28:50 +01:00
|
|
|
QTextCursor begin = selection.m_cursor_begin;
|
|
|
|
|
|
2009-12-01 19:44:31 +01:00
|
|
|
const QTextCursor &end= selection.m_cursor_end;
|
|
|
|
|
const QColor &fg = selection.m_fg;
|
|
|
|
|
const QColor &bg = selection.m_bg;
|
|
|
|
|
|
|
|
|
|
|
2019-01-22 14:01:30 +01:00
|
|
|
if (begin.isNull() || end.isNull() || begin.position() > end.position() || !bg.isValid())
|
2009-12-01 14:47:10 +01:00
|
|
|
return;
|
|
|
|
|
|
2022-03-17 07:09:51 +01:00
|
|
|
QPainterPath path = createSelectionPath(begin, end, clip);
|
2023-04-17 10:53:32 +02:00
|
|
|
if (path.isEmpty())
|
|
|
|
|
return;
|
2009-11-25 15:55:45 +01:00
|
|
|
|
|
|
|
|
painter->save();
|
2009-12-01 14:47:10 +01:00
|
|
|
QColor penColor = fg;
|
2009-12-02 14:12:59 +01:00
|
|
|
if (m_alpha)
|
|
|
|
|
penColor.setAlpha(220);
|
2022-11-23 14:59:59 +01:00
|
|
|
QPen pen(penColor, borderWidth);
|
2009-11-25 15:55:45 +01:00
|
|
|
painter->translate(-.5, -.5);
|
2009-11-30 17:23:31 +01:00
|
|
|
|
|
|
|
|
QRectF pathRect = path.controlPointRect();
|
|
|
|
|
|
2019-01-22 14:01:30 +01:00
|
|
|
if (!m_alpha || begin.blockNumber() != end.blockNumber()) {
|
|
|
|
|
// gradients are too slow for larger selections :(
|
|
|
|
|
QColor col = bg;
|
|
|
|
|
if (m_alpha)
|
|
|
|
|
col.setAlpha(50);
|
|
|
|
|
painter->setBrush(col);
|
2009-12-01 14:47:10 +01:00
|
|
|
} else {
|
2019-01-22 14:01:30 +01:00
|
|
|
QLinearGradient linearGrad(pathRect.topLeft(), pathRect.bottomLeft());
|
|
|
|
|
QColor col1 = fg.lighter(150);
|
|
|
|
|
col1.setAlpha(20);
|
|
|
|
|
QColor col2 = fg;
|
|
|
|
|
col2.setAlpha(80);
|
|
|
|
|
linearGrad.setColorAt(0, col1);
|
|
|
|
|
linearGrad.setColorAt(1, col2);
|
|
|
|
|
painter->setBrush(QBrush(linearGrad));
|
2009-12-01 14:47:10 +01:00
|
|
|
}
|
2009-11-30 17:23:31 +01:00
|
|
|
|
2009-12-02 13:24:53 +01:00
|
|
|
if (selection.m_dropShadow) {
|
|
|
|
|
painter->save();
|
2009-12-02 14:12:59 +01:00
|
|
|
QPainterPath shadow = path;
|
|
|
|
|
shadow.translate(m_dropShadowWidth, m_dropShadowWidth);
|
2009-12-04 13:03:22 +01:00
|
|
|
QPainterPath clip;
|
|
|
|
|
clip.addRect(m_editor->viewport()->rect());
|
|
|
|
|
painter->setClipPath(clip - path);
|
2009-12-02 14:12:59 +01:00
|
|
|
painter->fillPath(shadow, QColor(0, 0, 0, 100));
|
2009-12-02 13:24:53 +01:00
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 14:59:59 +01:00
|
|
|
pen.setJoinStyle(Qt::MiterJoin);
|
2009-11-25 15:55:45 +01:00
|
|
|
painter->setPen(pen);
|
|
|
|
|
painter->drawPath(path);
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-01 19:44:31 +01:00
|
|
|
void TextEditorOverlay::fillSelection(QPainter *painter,
|
|
|
|
|
const OverlaySelection &selection,
|
2022-03-17 07:09:51 +01:00
|
|
|
const QColor &color,
|
|
|
|
|
const QRect &clip)
|
2009-12-01 14:47:10 +01:00
|
|
|
{
|
2009-12-01 19:44:31 +01:00
|
|
|
const QTextCursor &begin = selection.m_cursor_begin;
|
|
|
|
|
const QTextCursor &end= selection.m_cursor_end;
|
2009-12-01 14:47:10 +01:00
|
|
|
if (begin.isNull() || end.isNull() || begin.position() > end.position())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-03-17 07:09:51 +01:00
|
|
|
QPainterPath path = createSelectionPath(begin, end, clip);
|
2023-04-17 10:53:32 +02:00
|
|
|
if (path.isEmpty())
|
|
|
|
|
return;
|
2009-12-01 14:47:10 +01:00
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
painter->translate(-.5, -.5);
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
painter->fillPath(path, color);
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
2009-11-25 15:55:45 +01:00
|
|
|
|
|
|
|
|
void TextEditorOverlay::paint(QPainter *painter, const QRect &clip)
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(clip)
|
2023-06-12 11:02:43 +02:00
|
|
|
|
|
|
|
|
const auto firstBlock = m_editor->blockForVerticalOffset(clip.top());
|
|
|
|
|
const auto lastBlock = m_editor->blockForVerticalOffset(clip.bottom());
|
|
|
|
|
|
|
|
|
|
auto overlapsClip = [&](const OverlaySelection &selection) {
|
|
|
|
|
return selection.m_cursor_end.blockNumber() >= firstBlock.blockNumber()
|
|
|
|
|
&& selection.m_cursor_begin.blockNumber() <= lastBlock.blockNumber();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = m_selections.size() - 1; i >= 0; --i) {
|
2009-11-25 15:55:45 +01:00
|
|
|
const OverlaySelection &selection = m_selections.at(i);
|
2023-06-12 11:02:43 +02:00
|
|
|
if (selection.m_dropShadow || !overlapsClip(selection))
|
2010-01-19 12:28:50 +01:00
|
|
|
continue;
|
|
|
|
|
if (selection.m_fixedLength >= 0
|
|
|
|
|
&& selection.m_cursor_end.position() - selection.m_cursor_begin.position()
|
|
|
|
|
!= selection.m_fixedLength)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-03-17 07:09:51 +01:00
|
|
|
paintSelection(painter, selection, clip);
|
2010-01-19 12:28:50 +01:00
|
|
|
}
|
2023-06-12 11:02:43 +02:00
|
|
|
for (int i = m_selections.size() - 1; i >= 0; --i) {
|
2010-01-19 12:28:50 +01:00
|
|
|
const OverlaySelection &selection = m_selections.at(i);
|
2023-06-12 11:02:43 +02:00
|
|
|
if (!selection.m_dropShadow || !overlapsClip(selection))
|
2010-01-19 12:28:50 +01:00
|
|
|
continue;
|
2009-11-30 13:56:54 +01:00
|
|
|
if (selection.m_fixedLength >= 0
|
|
|
|
|
&& selection.m_cursor_end.position() - selection.m_cursor_begin.position()
|
|
|
|
|
!= selection.m_fixedLength)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-03-17 07:09:51 +01:00
|
|
|
paintSelection(painter, selection, clip);
|
2009-12-01 14:47:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 12:30:47 +02:00
|
|
|
QTextCursor TextEditorOverlay::cursorForSelection(const OverlaySelection &selection) const
|
|
|
|
|
{
|
|
|
|
|
QTextCursor cursor = selection.m_cursor_begin;
|
|
|
|
|
cursor.setPosition(selection.m_cursor_begin.position());
|
|
|
|
|
cursor.setKeepPositionOnInsert(false);
|
|
|
|
|
if (!cursor.isNull())
|
|
|
|
|
cursor.setPosition(selection.m_cursor_end.position(), QTextCursor::KeepAnchor);
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextCursor TextEditorOverlay::cursorForIndex(int selectionIndex) const
|
|
|
|
|
{
|
|
|
|
|
return cursorForSelection(m_selections.value(selectionIndex));
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-01 14:47:10 +01:00
|
|
|
void TextEditorOverlay::fill(QPainter *painter, const QColor &color, const QRect &clip)
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(clip)
|
2010-01-19 12:28:50 +01:00
|
|
|
for (int i = m_selections.size()-1; i >= 0; --i) {
|
|
|
|
|
const OverlaySelection &selection = m_selections.at(i);
|
|
|
|
|
if (selection.m_dropShadow)
|
|
|
|
|
continue;
|
|
|
|
|
if (selection.m_fixedLength >= 0
|
|
|
|
|
&& selection.m_cursor_end.position() - selection.m_cursor_begin.position()
|
|
|
|
|
!= selection.m_fixedLength)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-03-17 07:09:51 +01:00
|
|
|
fillSelection(painter, selection, color, clip);
|
2010-01-19 12:28:50 +01:00
|
|
|
}
|
|
|
|
|
for (int i = m_selections.size()-1; i >= 0; --i) {
|
2009-12-01 14:47:10 +01:00
|
|
|
const OverlaySelection &selection = m_selections.at(i);
|
2010-01-19 12:28:50 +01:00
|
|
|
if (!selection.m_dropShadow)
|
|
|
|
|
continue;
|
2009-12-01 14:47:10 +01:00
|
|
|
if (selection.m_fixedLength >= 0
|
|
|
|
|
&& selection.m_cursor_end.position() - selection.m_cursor_begin.position()
|
|
|
|
|
!= selection.m_fixedLength)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-03-17 07:09:51 +01:00
|
|
|
fillSelection(painter, selection, color, clip);
|
2009-11-25 15:55:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 09:35:46 +02:00
|
|
|
bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
|
2010-11-02 15:35:42 +01:00
|
|
|
{
|
2021-04-16 09:35:46 +02:00
|
|
|
if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
|
|
|
|
|
}
|