2021-06-10 09:34:59 +08:00
|
|
|
#include "synexporter.h"
|
|
|
|
|
|
|
|
#include <QClipboard>
|
2021-06-12 22:36:23 +08:00
|
|
|
#include <QFile>
|
2021-06-10 09:34:59 +08:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QTextCodec>
|
2021-09-03 20:55:14 +08:00
|
|
|
#include "../../platform.h"
|
2021-06-10 09:34:59 +08:00
|
|
|
|
|
|
|
SynExporter::SynExporter()
|
|
|
|
{
|
|
|
|
mClipboardFormat = "text/plain";
|
|
|
|
mFont = QGuiApplication::font();
|
|
|
|
mBackgroundColor = QGuiApplication::palette().color(QPalette::Base);
|
|
|
|
mForegroundColor = QGuiApplication::palette().color(QPalette::Text);
|
|
|
|
mUseBackground = false;
|
|
|
|
mExportAsText = false;
|
2021-09-28 17:17:33 +08:00
|
|
|
mCharset = pCharsetInfoManager->getDefaultSystemEncoding();
|
2021-06-12 22:36:23 +08:00
|
|
|
mFileEndingType = FileEndingType::Windows;
|
2021-06-10 09:34:59 +08:00
|
|
|
clear();
|
|
|
|
setTitle("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::clear()
|
|
|
|
{
|
|
|
|
mBuffer.clear();
|
|
|
|
mLastStyle = SynFontStyle::fsNone;
|
|
|
|
mLastBG = QGuiApplication::palette().color(QPalette::Base);
|
|
|
|
mLastFG = QGuiApplication::palette().color(QPalette::Text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::CopyToClipboard()
|
|
|
|
{
|
|
|
|
if (mExportAsText) {
|
2021-06-12 22:36:23 +08:00
|
|
|
CopyToClipboardFormat("text/plain");
|
2021-06-10 09:34:59 +08:00
|
|
|
} else
|
2021-06-12 22:36:23 +08:00
|
|
|
CopyToClipboardFormat(clipboardFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::ExportAll(PSynEditStringList ALines)
|
|
|
|
{
|
|
|
|
ExportRange(ALines, BufferCoord{1, 1}, BufferCoord{INT_MAX, INT_MAX});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::ExportRange(PSynEditStringList ALines, BufferCoord Start, BufferCoord Stop)
|
|
|
|
{
|
|
|
|
// abort if not all necessary conditions are met
|
|
|
|
if (!ALines || !mHighlighter || (ALines->count() == 0))
|
|
|
|
return;
|
|
|
|
Stop.Line = std::max(1, std::min(Stop.Line, ALines->count()));
|
|
|
|
Stop.Char = std::max(1, std::min(Stop.Char, ALines->getString(Stop.Line - 1).length() + 1));
|
|
|
|
Start.Line = std::max(1, std::min(Start.Line, ALines->count()));
|
|
|
|
Start.Char = std::max(1, std::min(Start.Char, ALines->getString(Start.Line - 1).length() + 1));
|
|
|
|
if ( (Start.Line > ALines->count()) || (Start.Line > Stop.Line) )
|
|
|
|
return;
|
|
|
|
if ((Start.Line == Stop.Line) && (Start.Char >= Stop.Char))
|
|
|
|
return;
|
|
|
|
// initialization
|
|
|
|
mBuffer.clear();
|
|
|
|
// export all the lines into fBuffer
|
|
|
|
mFirstAttribute = true;
|
|
|
|
|
|
|
|
if (Start.Line == 1)
|
|
|
|
mHighlighter->resetState();
|
|
|
|
else
|
2021-09-23 12:06:26 +08:00
|
|
|
mHighlighter->setState(ALines->ranges(Start.Line-2));
|
2021-06-12 22:36:23 +08:00
|
|
|
for (int i = Start.Line; i<=Stop.Line; i++) {
|
|
|
|
QString Line = ALines->getString(i-1);
|
|
|
|
// order is important, since Start.Y might be equal to Stop.Y
|
|
|
|
// if (i == Stop.Line)
|
|
|
|
// Line.remove(Stop.Char-1, INT_MAX);
|
|
|
|
// if ( (i = Start.Line) && (Start.Char > 1))
|
|
|
|
// Line.remove(0, Start.Char - 1);
|
|
|
|
// export the line
|
|
|
|
mHighlighter->setLine(Line, i);
|
|
|
|
while (!mHighlighter->eol()) {
|
|
|
|
PSynHighlighterAttribute attri = mHighlighter->getTokenAttribute();
|
|
|
|
int startPos = mHighlighter->getTokenPos();
|
|
|
|
QString token = mHighlighter->getToken();
|
|
|
|
if (i==Start.Line && (startPos+token.length() < Start.Char)) {
|
|
|
|
mHighlighter->next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i==Stop.Line && (startPos >= Stop.Char-1)) {
|
|
|
|
mHighlighter->next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i==Stop.Line && (startPos+token.length() > Stop.Char)) {
|
|
|
|
token = token.remove(Stop.Char - startPos - 1);
|
|
|
|
}
|
|
|
|
if (i==Start.Line && startPos < Start.Char-1) {
|
|
|
|
token = token.mid(Start.Char-1-startPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Token = ReplaceReservedChars(token);
|
|
|
|
if (mOnFormatToken)
|
|
|
|
mOnFormatToken(i, mHighlighter->getTokenPos(), mHighlighter->getToken(),attri);
|
|
|
|
SetTokenAttribute(attri);
|
|
|
|
FormatToken(Token);
|
|
|
|
mHighlighter->next();
|
|
|
|
}
|
2021-10-10 22:19:48 +08:00
|
|
|
if (i!=Stop.Line)
|
|
|
|
FormatNewLine();
|
2021-06-12 22:36:23 +08:00
|
|
|
}
|
|
|
|
if (!mFirstAttribute)
|
|
|
|
FormatAfterLastAttribute();
|
|
|
|
// insert header
|
|
|
|
InsertData(0, GetHeader());
|
|
|
|
// add footer
|
|
|
|
AddData(GetFooter());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::SaveToFile(const QString &AFileName)
|
|
|
|
{
|
|
|
|
QFile file(AFileName);
|
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
|
|
|
SaveToStream(file);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::SaveToStream(QIODevice &AStream)
|
|
|
|
{
|
|
|
|
AStream.write(mBuffer);
|
2021-06-10 09:34:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SynExporter::exportAsText() const
|
|
|
|
{
|
|
|
|
return mExportAsText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setExportAsText(bool Value)
|
|
|
|
{
|
|
|
|
if (mExportAsText != Value) {
|
|
|
|
mExportAsText = Value;
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont SynExporter::font() const
|
|
|
|
{
|
|
|
|
return mFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setFont(const QFont &font)
|
|
|
|
{
|
|
|
|
mFont = font;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynHighlighter SynExporter::highlighter() const
|
|
|
|
{
|
|
|
|
return mHighlighter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setHighlighter(PSynHighlighter Value)
|
|
|
|
{
|
|
|
|
if (mHighlighter != Value) {
|
|
|
|
mHighlighter = Value;
|
|
|
|
clear();
|
|
|
|
if ((mHighlighter) && (mHighlighter->whitespaceAttribute()) && mUseBackground)
|
|
|
|
mBackgroundColor = mHighlighter->whitespaceAttribute()->background();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynExporter::title() const
|
|
|
|
{
|
|
|
|
return mTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setTitle(const QString &Value)
|
|
|
|
{
|
|
|
|
if (mTitle != Value) {
|
|
|
|
if (!Value.isEmpty())
|
|
|
|
mTitle = Value;
|
|
|
|
else
|
|
|
|
mTitle = QObject::tr("Untitled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SynExporter::useBackground() const
|
|
|
|
{
|
|
|
|
return mUseBackground;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setUseBackground(bool Value)
|
|
|
|
{
|
|
|
|
if (mUseBackground != Value) {
|
|
|
|
mUseBackground = Value;
|
|
|
|
clear();
|
|
|
|
if ((mHighlighter) && (mHighlighter->whitespaceAttribute()) && mUseBackground)
|
|
|
|
mBackgroundColor = mHighlighter->whitespaceAttribute()->background();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileEndingType SynExporter::fileEndingType() const
|
|
|
|
{
|
|
|
|
return mFileEndingType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setFileEndingType(const FileEndingType &fileEndingType)
|
|
|
|
{
|
|
|
|
mFileEndingType = fileEndingType;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor SynExporter::foregroundColor() const
|
|
|
|
{
|
|
|
|
return mForegroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setForegroundColor(const QColor &value)
|
|
|
|
{
|
|
|
|
if (mForegroundColor != value) {
|
|
|
|
mForegroundColor = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor SynExporter::backgroundColor() const
|
|
|
|
{
|
|
|
|
return mBackgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setBackgroundColor(const QColor &value)
|
|
|
|
{
|
|
|
|
if (mBackgroundColor != value) {
|
|
|
|
mBackgroundColor = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray SynExporter::charset() const
|
|
|
|
{
|
|
|
|
return mCharset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setCharset(const QByteArray &charset)
|
|
|
|
{
|
|
|
|
mCharset = charset;
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
QString SynExporter::defaultFilter() const
|
|
|
|
{
|
|
|
|
return mDefaultFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setDefaultFilter(const QString &defaultFilter)
|
|
|
|
{
|
|
|
|
mDefaultFilter = defaultFilter;
|
|
|
|
}
|
|
|
|
|
2021-06-10 09:34:59 +08:00
|
|
|
void SynExporter::AddData(const QString &AText)
|
|
|
|
{
|
|
|
|
if (!AText.isEmpty()) {
|
2021-06-12 22:36:23 +08:00
|
|
|
QTextCodec* codec = getCodec();
|
2021-06-10 09:34:59 +08:00
|
|
|
mBuffer.append(codec->fromUnicode(AText));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::AddDataNewLine(const QString &AText)
|
|
|
|
{
|
|
|
|
AddData(AText);
|
|
|
|
AddNewLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::AddNewLine()
|
|
|
|
{
|
2021-06-12 22:36:23 +08:00
|
|
|
AddData(lineBreak());
|
2021-06-10 09:34:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::CopyToClipboardFormat(QByteArray AFormat)
|
|
|
|
{
|
|
|
|
QClipboard* clipboard = QGuiApplication::clipboard();
|
|
|
|
QMimeData * mimeData = new QMimeData();
|
|
|
|
mimeData->setData(AFormat,mBuffer);
|
2021-06-12 22:36:23 +08:00
|
|
|
clipboard->clear();
|
2021-06-10 09:34:59 +08:00
|
|
|
clipboard->setMimeData(mimeData);
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
void SynExporter::FormatToken(const QString &Token)
|
2021-06-10 09:34:59 +08:00
|
|
|
{
|
|
|
|
AddData(Token);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynExporter::GetBufferSize()
|
|
|
|
{
|
|
|
|
return mBuffer.size();
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
QTextCodec * SynExporter::getCodec() {
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName(mCharset);
|
|
|
|
if (codec == nullptr)
|
|
|
|
codec = QTextCodec::codecForLocale();
|
|
|
|
return codec;
|
|
|
|
}
|
2021-06-10 09:34:59 +08:00
|
|
|
void SynExporter::InsertData(int APos, const QString &AText)
|
|
|
|
{
|
|
|
|
if (!AText.isEmpty()) {
|
2021-06-12 22:36:23 +08:00
|
|
|
QTextCodec* codec = getCodec();
|
2021-06-10 09:34:59 +08:00
|
|
|
mBuffer.insert(APos,codec->fromUnicode(AText));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
QString SynExporter::ReplaceReservedChars(const QString &AToken)
|
2021-06-10 09:34:59 +08:00
|
|
|
{
|
|
|
|
if (AToken.isEmpty())
|
|
|
|
return "";
|
|
|
|
QString result;
|
|
|
|
for (QChar ch:AToken) {
|
|
|
|
if (mReplaceReserved.contains(ch)) {
|
|
|
|
result += mReplaceReserved[ch];
|
|
|
|
} else {
|
|
|
|
result += ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QColor ValidatedColor(const QColor& color, const QColor& defaultColor) {
|
|
|
|
if (color.isValid())
|
|
|
|
return color;
|
|
|
|
else
|
|
|
|
return defaultColor;
|
|
|
|
}
|
|
|
|
void SynExporter::SetTokenAttribute(PSynHighlighterAttribute Attri)
|
|
|
|
{
|
|
|
|
if (mFirstAttribute) {
|
|
|
|
mFirstAttribute = false;
|
|
|
|
mLastBG = ValidatedColor(Attri->background(), mBackgroundColor);
|
|
|
|
mLastFG = ValidatedColor(Attri->foreground(), mForegroundColor);
|
|
|
|
mLastStyle = Attri->styles();
|
|
|
|
FormatBeforeFirstAttribute(
|
|
|
|
mUseBackground && (mLastBG != mBackgroundColor),
|
|
|
|
mLastFG != mForegroundColor, Attri->styles());
|
|
|
|
} else {
|
|
|
|
bool ChangedBG = mUseBackground &&
|
|
|
|
(mLastBG != ValidatedColor(Attri->background(), mBackgroundColor));
|
|
|
|
bool ChangedFG = (mLastFG != ValidatedColor(Attri->foreground(), mForegroundColor));
|
|
|
|
if (ChangedBG || ChangedFG || (mLastStyle != Attri->styles())) {
|
|
|
|
// which font style bits are to reset?
|
|
|
|
SynFontStyles ChangedStyles = mLastStyle & ~(Attri->styles());
|
|
|
|
FormatAttributeDone(ChangedBG, ChangedFG, ChangedStyles);
|
|
|
|
// which font style bits are to set?
|
|
|
|
ChangedStyles = Attri->styles() & ~(mLastStyle);
|
|
|
|
mLastBG = ValidatedColor(Attri->background(), mBackgroundColor);
|
|
|
|
mLastFG = ValidatedColor(Attri->foreground(), mForegroundColor);
|
|
|
|
mLastStyle = Attri->styles();
|
|
|
|
FormatAttributeInit(ChangedBG, ChangedFG, ChangedStyles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
const QByteArray &SynExporter::buffer() const
|
|
|
|
{
|
|
|
|
return mBuffer;
|
|
|
|
}
|
|
|
|
|
2021-06-10 09:34:59 +08:00
|
|
|
QByteArray SynExporter::clipboardFormat()
|
|
|
|
{
|
|
|
|
return this->mClipboardFormat;
|
|
|
|
}
|
2021-06-12 22:36:23 +08:00
|
|
|
|
|
|
|
FormatTokenHandler SynExporter::onFormatToken() const
|
|
|
|
{
|
|
|
|
return mOnFormatToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynExporter::setOnFormatToken(const FormatTokenHandler &onFormatToken)
|
|
|
|
{
|
|
|
|
mOnFormatToken = onFormatToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynExporter::lineBreak()
|
|
|
|
{
|
|
|
|
switch(mFileEndingType) {
|
|
|
|
case FileEndingType::Linux:
|
|
|
|
return "\n";
|
|
|
|
case FileEndingType::Windows:
|
|
|
|
return "\r\n";
|
|
|
|
case FileEndingType::Mac:
|
|
|
|
return "\r";
|
|
|
|
}
|
2021-08-27 23:51:42 +08:00
|
|
|
return "\n";
|
2021-06-12 22:36:23 +08:00
|
|
|
}
|