valentina/src/libs/vwidgets/vplaintextedit.cpp

256 lines
8.0 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vplaintextedit.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 14 11, 2017
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2017 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "vplaintextedit.h"
2023-07-17 15:33:57 +02:00
// Header <ciso646> is removed in C++20.
#if __cplusplus <= 201703L
#include <ciso646> // and, not, or
#endif
//---------------------------------------------------------------------------------------------------------------------
2023-07-17 15:33:57 +02:00
VPlainTextEdit::VPlainTextEdit(QWidget *parent)
: QPlainTextEdit(parent),
m_highlighter(document())
{
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &VPlainTextEdit::MatchParentheses);
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-17 15:33:57 +02:00
VPlainTextEdit::VPlainTextEdit(const QString &text, QWidget *parent)
: QPlainTextEdit(text, parent),
m_highlighter(document())
{
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &VPlainTextEdit::MatchParentheses);
}
//---------------------------------------------------------------------------------------------------------------------
VPlainTextEdit::~VPlainTextEdit()
{
document()->blockSignals(true); // prevent crash
}
//---------------------------------------------------------------------------------------------------------------------
void VPlainTextEdit::SetFilter(const QString &filter)
{
2023-07-17 15:33:57 +02:00
if (m_filter.isEmpty() && not filter.isEmpty())
{
QTextDocument *doc = document();
m_allLines.clear();
m_allLines.reserve(doc->lineCount());
2023-07-17 15:33:57 +02:00
for (int i = 0; i < doc->blockCount(); ++i)
{
m_allLines.append(doc->findBlockByNumber(i).text());
}
}
m_filter = filter;
Filter();
2023-07-17 15:33:57 +02:00
if (m_filter.isEmpty())
{
m_allLines.clear();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPlainTextEdit::appendPlainText(const QString &text)
{
if (m_filter.isEmpty())
{
QPlainTextEdit::appendPlainText(text);
}
else
{
m_allLines.append(text);
const vsizetype diff = m_allLines.size() - maximumBlockCount();
if (diff > 0)
{
m_allLines = m_allLines.mid(diff);
}
Filter();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPlainTextEdit::MatchParentheses()
{
2024-02-19 17:09:56 +01:00
QList<QTextEdit::ExtraSelection> const selections;
setExtraSelections(selections);
VTextBlockData *data = static_cast<VTextBlockData *>(textCursor().block().userData());
if (data)
{
2024-02-19 17:09:56 +01:00
QVector<ParenthesisInfo *> const infos = data->Parentheses();
2024-02-19 17:09:56 +01:00
int const pos = textCursor().block().position();
for (int i = 0; i < infos.size(); ++i)
{
ParenthesisInfo *info = infos.at(i);
2024-02-19 17:09:56 +01:00
int const curPos = textCursor().position() - textCursor().block().position();
if ((info->position == curPos - 1 || info->position == curPos) && info->character == '(')
{
CreateParenthesisSelection(pos + static_cast<int>(info->position),
MatchLeftParenthesis(textCursor().block(), i + 1, 0));
return;
}
if ((info->position == curPos - 1 || info->position == curPos) && info->character == ')')
{
CreateParenthesisSelection(pos + static_cast<int>(info->position),
MatchRightParenthesis(textCursor().block(), i - 1, 0));
return;
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VPlainTextEdit::MatchLeftParenthesis(QTextBlock currentBlock, int i, int numLeftParentheses) -> bool
{
auto *data = static_cast<VTextBlockData *>(currentBlock.userData());
2024-02-19 17:09:56 +01:00
QVector<ParenthesisInfo *> const infos = data->Parentheses();
2024-02-19 17:09:56 +01:00
int const docPos = currentBlock.position();
for (; i < infos.size(); ++i)
{
ParenthesisInfo *info = infos.at(i);
if (info->character == '(')
{
++numLeftParentheses;
continue;
}
if (info->character == ')' && numLeftParentheses == 0)
{
CreateParenthesisSelection(docPos + static_cast<int>(info->position));
return true;
}
2023-05-03 13:07:02 +02:00
--numLeftParentheses;
}
currentBlock = currentBlock.next();
if (currentBlock.isValid())
{
return MatchLeftParenthesis(currentBlock, 0, numLeftParentheses);
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
2023-05-03 13:07:02 +02:00
auto VPlainTextEdit::MatchRightParenthesis(QTextBlock currentBlock, int i, int numRightParentheses) -> bool
{
auto *data = static_cast<VTextBlockData *>(currentBlock.userData());
2024-02-19 17:09:56 +01:00
QVector<ParenthesisInfo *> const parentheses = data->Parentheses();
2024-02-19 17:09:56 +01:00
int const docPos = currentBlock.position();
for (; i > -1 && parentheses.size() > 0; --i)
{
ParenthesisInfo *info = parentheses.at(i);
if (info->character == ')')
{
++numRightParentheses;
continue;
}
if (info->character == '(' && numRightParentheses == 0)
{
CreateParenthesisSelection(docPos + static_cast<int>(info->position));
return true;
}
2023-02-09 14:48:28 +01:00
--numRightParentheses;
}
currentBlock = currentBlock.previous();
if (currentBlock.isValid())
{
return MatchRightParenthesis(currentBlock, 0, numRightParentheses);
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
void VPlainTextEdit::CreateParenthesisSelection(int pos, bool match)
{
QList<QTextEdit::ExtraSelection> selections = extraSelections();
QTextEdit::ExtraSelection selection;
QTextCharFormat format = selection.format;
if (match)
{
format.setBackground(Qt::lightGray);
format.setForeground(Qt::yellow);
}
else
{
format.setForeground(Qt::red);
}
selection.format = format;
QTextCursor cursor = textCursor();
cursor.setPosition(pos);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
selection.cursor = cursor;
selections.append(selection);
setExtraSelections(selections);
}
//---------------------------------------------------------------------------------------------------------------------
void VPlainTextEdit::Filter()
{
clear();
2023-07-17 15:33:57 +02:00
if (not m_filter.isEmpty())
{
2023-07-17 15:33:57 +02:00
for (auto &line : m_allLines)
{
if (line.contains(m_filter))
{
QPlainTextEdit::appendPlainText(line);
}
}
}
else
{
2023-07-17 15:33:57 +02:00
for (auto &line : m_allLines)
{
QPlainTextEdit::appendPlainText(line);
}
}
}