0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/scripteditor/lrcodeeditor.cpp

359 lines
11 KiB
C++
Raw Normal View History

2017-09-16 16:04:29 +03:00
#include "lrcodeeditor.h"
#include <QAbstractItemView>
#include <QWidget>
#include <QCompleter>
#include <QKeyEvent>
#include <QScrollBar>
#include <QPainter>
#include <QTextBlock>
#include <QDebug>
#include "lrscripthighlighter.h"
#include "lrglobal.h"
2017-09-16 16:04:29 +03:00
namespace LimeReport{
CodeEditor::CodeEditor(QWidget *parent)
2021-11-03 00:40:16 +03:00
: QPlainTextEdit(parent), m_completer(0)
2017-09-16 16:04:29 +03:00
{
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
updateLineNumberAreaWidth(0);
highlightCurrentLine();
2017-09-19 19:05:38 +03:00
new ScriptHighlighter(document());
connect(this, SIGNAL(cursorPositionChanged()),
this, SLOT(matchParentheses()));
2017-09-16 16:04:29 +03:00
}
void CodeEditor::setCompleter(QCompleter *value)
{
if (value) disconnect(value,0,this,0);
2021-11-03 00:40:16 +03:00
m_completer = value;
if (!m_completer) return;
m_completer->setWidget(this);
m_completer->setCompletionMode(QCompleter::PopupCompletion);
m_completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(m_completer,SIGNAL(activated(QString)),this,SLOT(insertCompletion(QString)));
2017-09-16 16:04:29 +03:00
}
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event)
{
QPainter painter(lineNumberArea);
2017-10-05 01:28:24 +03:00
QStyleOption option;
option.initFrom(this);
2021-08-23 08:07:08 +03:00
//painter.fillRect(event->rect(), QPalette().window().color());
QColor bg = option.palette.window().color().darker(150);
2017-10-05 01:28:24 +03:00
painter.fillRect(event->rect(), bg);
2017-09-16 16:04:29 +03:00
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
2017-10-05 01:28:24 +03:00
painter.setPen(option.palette.text().color());
2017-09-16 16:04:29 +03:00
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignCenter, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
2021-08-23 08:07:08 +03:00
int space = fontMetrics().boundingRect(QLatin1Char('9')).width()*2 + fontMetrics().boundingRect(QLatin1Char('9')).width() * digits;
2017-09-16 16:04:29 +03:00
return space;
}
void CodeEditor::keyPressEvent(QKeyEvent *e)
{
2021-11-03 00:40:16 +03:00
if (m_completer && m_completer->popup()->isVisible()) {
2017-09-16 16:04:29 +03:00
switch (e->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
case Qt::Key_Right:
case Qt::Key_Left:
case Qt::Key_Up:
case Qt::Key_Down:
2017-09-16 16:04:29 +03:00
e->ignore();
return;
default:
break;
}
}
bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
2021-11-03 00:40:16 +03:00
if (!m_completer || !isShortcut) QPlainTextEdit::keyPressEvent(e);
2017-09-16 16:04:29 +03:00
const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
2021-11-03 00:40:16 +03:00
if (!m_completer || (ctrlOrShift && e->text().isEmpty()))
2017-09-16 16:04:29 +03:00
return;
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
|| Const::EOW.contains(e->text().right(1)))
)
{
2021-11-03 00:40:16 +03:00
m_completer->popup()->hide();
2017-09-16 16:04:29 +03:00
return;
}
2021-11-03 00:40:16 +03:00
if (completionPrefix != m_completer->completionPrefix()) {
m_completer->setCompletionPrefix(completionPrefix);
m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
2017-09-16 16:04:29 +03:00
}
2021-11-03 00:40:16 +03:00
QModelIndex ci = m_completer->completionModel()->index(0,0);
if (ci.isValid() && m_completer->completionModel()->data(ci).toString().compare(completionPrefix) == 0){
m_completer->popup()->hide();
return;
}
2017-09-16 16:04:29 +03:00
QRect cr = cursorRect();
2021-11-03 00:40:16 +03:00
cr.setWidth(m_completer->popup()->sizeHintForColumn(0)
+ m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_completer->complete(cr);
2017-09-16 16:04:29 +03:00
if (!completionPrefix.isEmpty() &&
completionPrefix.at(completionPrefix.length()-1) == '.')
{
2021-11-03 00:40:16 +03:00
m_completer->popup();
}
2017-09-16 16:04:29 +03:00
}
void CodeEditor::focusInEvent(QFocusEvent *e)
{
2021-11-03 00:40:16 +03:00
if (m_completer) m_completer->setWidget(this);
2017-09-16 16:04:29 +03:00
QPlainTextEdit::focusInEvent(e);
}
void CodeEditor::resizeEvent(QResizeEvent* event)
{
QPlainTextEdit::resizeEvent(event);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
QString CodeEditor::textUnderCursor() const
{
QTextCursor tc = textCursor();
QString currentText;
tc.movePosition(QTextCursor::StartOfBlock,QTextCursor::KeepAnchor);
QString blockText = tc.selectedText();
for(int i = blockText.length(); i>0; --i){
if (!Const::EOW.contains(blockText.at(i-1)))
currentText = blockText.at(i-1) + currentText;
else break;
}
2018-07-09 22:52:47 +03:00
return currentText.trimmed();
2017-09-16 16:04:29 +03:00
}
2017-09-19 19:05:38 +03:00
bool CodeEditor::matchLeftParenthesis(QTextBlock currentBlock, QChar parenthesisType, int i, int numLeftParentheses)
{
TextBlockData *data = static_cast<TextBlockData *>(currentBlock.userData());
2017-10-04 17:14:07 +03:00
if (data){
QVector<ParenthesisInfo *> infos = data->parentheses();
int docPos = currentBlock.position();
for (; i < infos.size(); ++i) {
ParenthesisInfo *info = infos.at(i);
if (info->character == parenthesisType) {
++numLeftParentheses;
continue;
}
if (info->character == getParenthesisReverceChar(parenthesisType)){
if (numLeftParentheses == 0) {
createParenthesisSelection(docPos + info->position);
return true;
} else
--numLeftParentheses;
}
2017-09-19 19:05:38 +03:00
}
}
currentBlock = currentBlock.next();
if (currentBlock.isValid())
return matchLeftParenthesis(currentBlock, parenthesisType, 0, numLeftParentheses);
2017-09-19 19:05:38 +03:00
return false;
}
bool CodeEditor::matchRightParenthesis(QTextBlock currentBlock, QChar parenthesisType, int i, int numRightParentheses)
{
TextBlockData *data = static_cast<TextBlockData *>(currentBlock.userData());
2017-10-04 17:14:07 +03:00
if (data){
2017-11-10 00:47:47 +03:00
QVector<ParenthesisInfo *> parentheses = data->parentheses();
2017-10-04 17:14:07 +03:00
int docPos = currentBlock.position();
2017-12-16 18:02:13 +03:00
if (i == -2) i = parentheses.size()-1;
2017-10-04 17:14:07 +03:00
for (; i > -1 && parentheses.size() > 0; --i) {
ParenthesisInfo *info = parentheses.at(i);
if (info->character == parenthesisType) {
++numRightParentheses;
continue;
}
if (info->character == getParenthesisReverceChar(parenthesisType)){
if (numRightParentheses == 0) {
createParenthesisSelection(docPos + info->position);
return true;
} else
--numRightParentheses;
}
2017-09-19 19:05:38 +03:00
}
}
2017-09-19 19:05:38 +03:00
currentBlock = currentBlock.previous();
if (currentBlock.isValid())
return matchRightParenthesis(currentBlock, parenthesisType, -2, numRightParentheses);
2017-09-19 19:05:38 +03:00
return false;
}
void CodeEditor::createParenthesisSelection(int pos)
{
QList<QTextEdit::ExtraSelection> selections = extraSelections();
QTextEdit::ExtraSelection selection;
QTextCharFormat format = selection.format;
format.setBackground(QColor("#619934"));
format.setForeground(QColor("#ffffff"));
selection.format = format;
QTextCursor cursor = textCursor();
cursor.setPosition(pos);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
selection.cursor = cursor;
selections.append(selection);
setExtraSelections(selections);
}
bool CodeEditor::charIsParenthesis(QChar character, ParenthesisType type)
{
for (int i = 0; i < PARENHEIS_COUNT; ++i){
if (character == parenthesisCharacters[type][i])
return true;
}
return false;
}
QChar CodeEditor::getParenthesisReverceChar(QChar parenthesisChar)
{
for (int i = 0; i < PARENHEIS_COUNT; ++i){
if ( parenthesisCharacters[RightParenthesis][i] == parenthesisChar)
return parenthesisCharacters[LeftParenthesis][i];
if ( parenthesisCharacters[LeftParenthesis][i] == parenthesisChar)
return parenthesisCharacters[RightParenthesis][i];
}
return ' ';
}
2017-09-16 16:04:29 +03:00
void CodeEditor::insertCompletion(const QString &completion)
{
2021-11-03 00:40:16 +03:00
if (m_completer->widget() != this)
2017-09-16 16:04:29 +03:00
return;
QTextCursor tc = textCursor();
// QString prefix = m_completer->completionPrefix();
// int extra = completion.length() - prefix.length();
2021-11-03 00:40:16 +03:00
for (int i=0; i < m_completer->completionPrefix().length(); ++i ) {
tc.deletePreviousChar();
}
tc.insertText(completion);
// tc.insertText(completion.right(extra));
2017-09-16 16:04:29 +03:00
setTextCursor(tc);
}
2017-09-19 19:05:38 +03:00
void CodeEditor::updateLineNumberAreaWidth(int /*newBlockCount*/)
2017-09-16 16:04:29 +03:00
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
2021-08-23 08:07:08 +03:00
QColor lineColor = QColor(QPalette().window().color()).darker(100);
2017-09-16 16:04:29 +03:00
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void CodeEditor::updateLineNumberArea(const QRect& rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
2017-09-19 19:05:38 +03:00
void CodeEditor::matchParentheses()
{
QList<QTextEdit::ExtraSelection> selections;
setExtraSelections(selections);
TextBlockData *data = static_cast<TextBlockData *>(textCursor().block().userData());
if (data) {
QVector<ParenthesisInfo *> infos = data->parentheses();
int pos = textCursor().block().position();
for (int i = 0; i < infos.size(); ++i) {
ParenthesisInfo *info = infos.at(i);
int curPos = textCursor().position() - textCursor().block().position();
if ( (info->position == (curPos - 1)) && charIsParenthesis(info->character, LeftParenthesis))
{
if (matchLeftParenthesis(textCursor().block(), info->character, i + 1, 0))
createParenthesisSelection(pos + info->position);
} else if ( (info->position == (curPos - 1)) && charIsParenthesis(info->character, RightParenthesis)) {
if (matchRightParenthesis(textCursor().block(), info->character, i - 1, 0))
createParenthesisSelection(pos + info->position);
}
}
}
}
2017-09-16 16:04:29 +03:00
} //namespace LimeReport