263 lines
7.2 KiB
C++
263 lines
7.2 KiB
C++
// This file is part of Friendiqa
|
|
// https://github.com/lubuwest/Friendiqa
|
|
// Copyright (C) 2020 Marco R. <thomasschmidt45@gmx.net>
|
|
//
|
|
// This program 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.
|
|
//
|
|
// In addition, as a special exception, the copyright holders give
|
|
// permission to link the code of portions of this program with the
|
|
// OpenSSL library under certain conditions as described in each
|
|
// individual source file, and distribute linked combinations including
|
|
// the two.
|
|
//
|
|
// You must obey the GNU General Public License in all respects for all
|
|
// of the code used other than OpenSSL. If you modify file(s) with this
|
|
// exception, you may extend this exception to your version of the
|
|
// file(s), but you are not obligated to do so. If you do not wish to do
|
|
// so, delete this exception statement from your version. If you delete
|
|
// this exception statement from all source files in the program, then
|
|
// also delete it here.
|
|
//
|
|
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#include <QQuickTextDocument>
|
|
#include <QTextCharFormat>
|
|
#include <QStringDecoder>
|
|
#include <QTextDocument>
|
|
#include <QTextDocumentFragment>
|
|
#include <QTextList>
|
|
#include <QDebug>
|
|
#include "documenthandler.h"
|
|
|
|
DocumentHandler::DocumentHandler(QObject *parent)
|
|
: QObject(parent)
|
|
, m_document(nullptr)
|
|
, m_cursorPosition(-1)
|
|
, m_selectionStart(0)
|
|
, m_selectionEnd(0)
|
|
{
|
|
}
|
|
|
|
QQuickTextDocument *DocumentHandler::document() const
|
|
{
|
|
return m_document;
|
|
}
|
|
|
|
void DocumentHandler::setDocument(QQuickTextDocument *document)
|
|
{
|
|
if (document == m_document)
|
|
return;
|
|
|
|
if (m_document)
|
|
disconnect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged);
|
|
m_document = document;
|
|
if (m_document)
|
|
connect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged);
|
|
emit documentChanged();
|
|
}
|
|
|
|
int DocumentHandler::cursorPosition() const
|
|
{
|
|
return m_cursorPosition;
|
|
}
|
|
|
|
void DocumentHandler::setCursorPosition(int position)
|
|
{
|
|
if (position == m_cursorPosition)
|
|
return;
|
|
|
|
m_cursorPosition = position;
|
|
emit cursorPositionChanged();
|
|
}
|
|
|
|
int DocumentHandler::selectionStart() const
|
|
{
|
|
return m_selectionStart;
|
|
}
|
|
|
|
void DocumentHandler::setSelectionStart(int position)
|
|
{
|
|
if (position == m_selectionStart)
|
|
return;
|
|
|
|
m_selectionStart = position;
|
|
emit selectionStartChanged();
|
|
}
|
|
|
|
int DocumentHandler::selectionEnd() const
|
|
{
|
|
return m_selectionEnd;
|
|
}
|
|
|
|
void DocumentHandler::setSelectionEnd(int position)
|
|
{
|
|
if (position == m_selectionEnd)
|
|
return;
|
|
|
|
m_selectionEnd = position;
|
|
emit selectionEndChanged();
|
|
}
|
|
|
|
QTextCursor DocumentHandler::textCursor() const
|
|
{
|
|
QTextDocument *doc = textDocument();
|
|
if (!doc)
|
|
return QTextCursor();
|
|
QTextCursor cursor = QTextCursor(doc);
|
|
if (m_selectionStart != m_selectionEnd) {
|
|
cursor.setPosition(m_selectionStart);
|
|
cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor);
|
|
} else {
|
|
cursor.setPosition(m_cursorPosition);
|
|
}
|
|
return cursor;
|
|
}
|
|
|
|
QTextDocument *DocumentHandler::textDocument() const
|
|
{
|
|
if (!m_document)
|
|
return nullptr;
|
|
|
|
return m_document->textDocument();
|
|
}
|
|
|
|
void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
|
|
{
|
|
QTextCursor cursor = textCursor();
|
|
if (!cursor.hasSelection())
|
|
cursor.select(QTextCursor::WordUnderCursor);
|
|
cursor.mergeCharFormat(format);
|
|
}
|
|
|
|
bool DocumentHandler::modified() const
|
|
{
|
|
return m_document && m_document->textDocument()->isModified();
|
|
}
|
|
|
|
void DocumentHandler::setModified(bool m)
|
|
{
|
|
if (m_document)
|
|
m_document->textDocument()->setModified(m);
|
|
}
|
|
|
|
QFont DocumentHandler::font() const
|
|
{
|
|
QTextCursor cursor = textCursor();
|
|
if (cursor.isNull())
|
|
return m_document->textDocument()->defaultFont();
|
|
QTextCharFormat format = cursor.charFormat();
|
|
return format.font();
|
|
}
|
|
|
|
void DocumentHandler::setFont(const QFont & font){
|
|
|
|
QTextCursor cursor = textCursor();
|
|
if (!cursor.isNull() && cursor.charFormat().font() == font)
|
|
return;
|
|
|
|
QTextCharFormat format;
|
|
format.setFont(font);
|
|
mergeFormatOnWordOrSelection(format);
|
|
|
|
emit fontChanged();
|
|
}
|
|
|
|
bool DocumentHandler::bold() const
|
|
{
|
|
const QTextCursor cursor = textCursor();
|
|
if (cursor.isNull())
|
|
return m_document->textDocument()->defaultFont().bold();
|
|
return cursor.charFormat().font().bold();
|
|
}
|
|
|
|
void DocumentHandler::setBold(bool bold)
|
|
{
|
|
const QTextCursor cursor = textCursor();
|
|
if (!cursor.isNull() && cursor.charFormat().font().bold() == bold)
|
|
return;
|
|
|
|
QFont font = cursor.charFormat().font();
|
|
font.setBold(bold);
|
|
QTextCharFormat format;
|
|
format.setFont(font);
|
|
mergeFormatOnWordOrSelection(format);
|
|
|
|
emit boldChanged();
|
|
}
|
|
|
|
bool DocumentHandler::italic() const
|
|
{
|
|
const QTextCursor cursor = textCursor();
|
|
if (cursor.isNull())
|
|
return m_document->textDocument()->defaultFont().italic();
|
|
return cursor.charFormat().font().italic();
|
|
}
|
|
|
|
void DocumentHandler::setItalic(bool italic)
|
|
{
|
|
const QTextCursor cursor = textCursor();
|
|
if (!cursor.isNull() && cursor.charFormat().font().italic() == italic)
|
|
return;
|
|
|
|
QFont font = cursor.charFormat().font();
|
|
font.setItalic(italic);
|
|
QTextCharFormat format;
|
|
format.setFont(font);
|
|
mergeFormatOnWordOrSelection(format);
|
|
|
|
emit italicChanged();
|
|
}
|
|
|
|
bool DocumentHandler::liststyle() const
|
|
{
|
|
const QTextCursor cursor = textCursor();
|
|
if (cursor.isNull())
|
|
return false;
|
|
return bool(cursor.currentList());
|
|
}
|
|
|
|
void DocumentHandler::setListstyle(bool liststyle)
|
|
{
|
|
QTextCursor cursor = textCursor();
|
|
if (!cursor.isNull() && !liststyle){
|
|
cursor.currentList()->remove(cursor.block());
|
|
emit liststyleChanged();
|
|
}else{
|
|
cursor.createList(QTextListFormat::ListDisc);
|
|
emit liststyleChanged();
|
|
}
|
|
}
|
|
|
|
bool DocumentHandler::codeblock() const
|
|
{
|
|
const QTextCursor cursor = textCursor();
|
|
if (cursor.isNull())
|
|
return false;
|
|
qDebug()<< QTextDocumentFragment(cursor).toPlainText();
|
|
return bool(QTextDocumentFragment(cursor).toMarkdown().contains("```"));
|
|
}
|
|
|
|
void DocumentHandler::setCodeblock(bool codeblock)
|
|
{
|
|
QTextCursor cursor = textCursor();
|
|
if (!cursor.isNull() && !codeblock){
|
|
qDebug()<< "!codeblock ```\n" + QTextDocumentFragment(cursor).toMarkdown() + "\n```";
|
|
cursor.insertMarkdown("```\n" + QTextDocumentFragment(cursor).toMarkdown() + "\n```");
|
|
emit codeblockChanged();
|
|
}
|
|
else{
|
|
qDebug()<< "```\n" + QTextDocumentFragment(cursor).toMarkdown() + "\n```";
|
|
cursor.insertMarkdown(QTextDocumentFragment(cursor).toMarkdown().remove("```"));
|
|
emit codeblockChanged();
|
|
}
|
|
}
|