2017-11-14 12:46:28 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @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
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2017-11-14 12:46:28 +01:00
|
|
|
**
|
|
|
|
** 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
|
2019-10-29 07:26:05 +01:00
|
|
|
|
2017-11-14 12:46:28 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-07-17 15:33:57 +02:00
|
|
|
VPlainTextEdit::VPlainTextEdit(QWidget *parent)
|
|
|
|
: QPlainTextEdit(parent),
|
|
|
|
m_highlighter(document())
|
2017-11-14 12:46:28 +01:00
|
|
|
{
|
|
|
|
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())
|
2017-11-14 12:46:28 +01:00
|
|
|
{
|
|
|
|
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &VPlainTextEdit::MatchParentheses);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPlainTextEdit::~VPlainTextEdit()
|
|
|
|
{
|
|
|
|
document()->blockSignals(true); // prevent crash
|
|
|
|
}
|
|
|
|
|
2019-10-28 16:44:31 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPlainTextEdit::SetFilter(const QString &filter)
|
|
|
|
{
|
2023-07-17 15:33:57 +02:00
|
|
|
if (m_filter.isEmpty() && not filter.isEmpty())
|
2019-10-28 16:44:31 +01:00
|
|
|
{
|
|
|
|
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)
|
2019-10-28 16:44:31 +01:00
|
|
|
{
|
|
|
|
m_allLines.append(doc->findBlockByNumber(i).text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_filter = filter;
|
|
|
|
|
|
|
|
Filter();
|
|
|
|
|
2023-07-17 15:33:57 +02:00
|
|
|
if (m_filter.isEmpty())
|
2019-10-28 16:44:31 +01:00
|
|
|
{
|
|
|
|
m_allLines.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPlainTextEdit::appendPlainText(const QString &text)
|
|
|
|
{
|
|
|
|
if (m_filter.isEmpty())
|
|
|
|
{
|
|
|
|
QPlainTextEdit::appendPlainText(text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_allLines.append(text);
|
2023-02-09 16:23:11 +01:00
|
|
|
const vsizetype diff = m_allLines.size() - maximumBlockCount();
|
2019-10-28 16:44:31 +01:00
|
|
|
if (diff > 0)
|
|
|
|
{
|
|
|
|
m_allLines = m_allLines.mid(diff);
|
|
|
|
}
|
|
|
|
Filter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 12:46:28 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPlainTextEdit::MatchParentheses()
|
|
|
|
{
|
2024-02-19 17:09:56 +01:00
|
|
|
QList<QTextEdit::ExtraSelection> const selections;
|
2017-11-14 12:46:28 +01:00
|
|
|
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();
|
2017-11-14 12:46:28 +01:00
|
|
|
|
2024-02-19 17:09:56 +01:00
|
|
|
int const pos = textCursor().block().position();
|
2017-11-14 12:46:28 +01:00
|
|
|
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();
|
2017-11-14 12:46:28 +01:00
|
|
|
if ((info->position == curPos - 1 || info->position == curPos) && info->character == '(')
|
|
|
|
{
|
2023-02-09 16:23:11 +01:00
|
|
|
CreateParenthesisSelection(pos + static_cast<int>(info->position),
|
|
|
|
MatchLeftParenthesis(textCursor().block(), i + 1, 0));
|
2017-11-14 12:46:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-02-09 16:23:11 +01:00
|
|
|
|
|
|
|
if ((info->position == curPos - 1 || info->position == curPos) && info->character == ')')
|
2017-11-14 12:46:28 +01:00
|
|
|
{
|
2023-02-09 16:23:11 +01:00
|
|
|
CreateParenthesisSelection(pos + static_cast<int>(info->position),
|
|
|
|
MatchRightParenthesis(textCursor().block(), i - 1, 0));
|
2017-11-14 12:46:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VPlainTextEdit::MatchLeftParenthesis(QTextBlock currentBlock, int i, int numLeftParentheses) -> bool
|
2017-11-14 12:46:28 +01:00
|
|
|
{
|
2024-02-19 17:15:42 +01:00
|
|
|
auto *data = static_cast<VTextBlockData *>(currentBlock.userData());
|
2024-02-19 17:09:56 +01:00
|
|
|
QVector<ParenthesisInfo *> const infos = data->Parentheses();
|
2017-11-14 12:46:28 +01:00
|
|
|
|
2024-02-19 17:09:56 +01:00
|
|
|
int const docPos = currentBlock.position();
|
2017-11-14 12:46:28 +01:00
|
|
|
for (; i < infos.size(); ++i)
|
|
|
|
{
|
|
|
|
ParenthesisInfo *info = infos.at(i);
|
|
|
|
|
|
|
|
if (info->character == '(')
|
|
|
|
{
|
|
|
|
++numLeftParentheses;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->character == ')' && numLeftParentheses == 0)
|
|
|
|
{
|
2023-02-09 16:23:11 +01:00
|
|
|
CreateParenthesisSelection(docPos + static_cast<int>(info->position));
|
2017-11-14 12:46:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-03 13:07:02 +02:00
|
|
|
|
|
|
|
--numLeftParentheses;
|
2017-11-14 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-11-14 12:46:28 +01:00
|
|
|
{
|
2024-02-19 17:15:42 +01:00
|
|
|
auto *data = static_cast<VTextBlockData *>(currentBlock.userData());
|
2024-02-19 17:09:56 +01:00
|
|
|
QVector<ParenthesisInfo *> const parentheses = data->Parentheses();
|
2017-11-14 12:46:28 +01:00
|
|
|
|
2024-02-19 17:09:56 +01:00
|
|
|
int const docPos = currentBlock.position();
|
2017-11-14 12:46:28 +01:00
|
|
|
for (; i > -1 && parentheses.size() > 0; --i)
|
|
|
|
{
|
|
|
|
ParenthesisInfo *info = parentheses.at(i);
|
|
|
|
if (info->character == ')')
|
|
|
|
{
|
|
|
|
++numRightParentheses;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->character == '(' && numRightParentheses == 0)
|
|
|
|
{
|
2023-02-09 16:23:11 +01:00
|
|
|
CreateParenthesisSelection(docPos + static_cast<int>(info->position));
|
2017-11-14 12:46:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
2023-02-09 14:48:28 +01:00
|
|
|
|
|
|
|
--numRightParentheses;
|
2017-11-14 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-10-28 16:44:31 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPlainTextEdit::Filter()
|
|
|
|
{
|
|
|
|
clear();
|
2023-07-17 15:33:57 +02:00
|
|
|
if (not m_filter.isEmpty())
|
2019-10-28 16:44:31 +01:00
|
|
|
{
|
2023-07-17 15:33:57 +02:00
|
|
|
for (auto &line : m_allLines)
|
2019-10-28 16:44:31 +01:00
|
|
|
{
|
|
|
|
if (line.contains(m_filter))
|
|
|
|
{
|
|
|
|
QPlainTextEdit::appendPlainText(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-17 15:33:57 +02:00
|
|
|
for (auto &line : m_allLines)
|
2019-10-28 16:44:31 +01:00
|
|
|
{
|
|
|
|
QPlainTextEdit::appendPlainText(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|