2021-04-06 23:10:57 +08:00
|
|
|
#include "editor.h"
|
|
|
|
|
|
|
|
#include <QtCore/QFileInfo>
|
2021-04-08 10:29:21 +08:00
|
|
|
#include <QTextCodec>
|
2021-04-07 22:44:08 +08:00
|
|
|
#include <QVariant>
|
2021-04-07 21:13:15 +08:00
|
|
|
#include <memory>
|
2021-04-08 10:29:21 +08:00
|
|
|
#include "settings.h"
|
|
|
|
#include "mainwindow.h"
|
2021-04-07 21:13:15 +08:00
|
|
|
|
|
|
|
using namespace std;
|
2021-04-06 23:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
Editor::Editor(QObject *parent, const QString& filename,
|
2021-04-08 10:29:21 +08:00
|
|
|
const QByteArray& encoding,
|
2021-04-06 23:10:57 +08:00
|
|
|
bool inProject, bool isNew,
|
|
|
|
QTabWidget* parentPageControl):
|
|
|
|
QObject(parent),
|
|
|
|
mFilename(filename),
|
2021-04-08 10:29:21 +08:00
|
|
|
mEncodingOption(encoding),
|
2021-04-06 23:10:57 +08:00
|
|
|
mInProject(inProject),
|
|
|
|
mIsNew(isNew),
|
|
|
|
mParentPageControl(parentPageControl)
|
|
|
|
{
|
|
|
|
mTextEdit = new QsciScintilla();
|
2021-04-07 21:13:15 +08:00
|
|
|
if (mFilename.isEmpty()) {
|
|
|
|
mFilename = tr("untitled") + "1";
|
|
|
|
}
|
2021-04-06 23:10:57 +08:00
|
|
|
QFileInfo fileInfo(mFilename);
|
|
|
|
mParentPageControl->addTab(mTextEdit,fileInfo.fileName());
|
|
|
|
if (!isNew) {
|
|
|
|
loadFile();
|
|
|
|
} else {
|
2021-04-08 10:29:21 +08:00
|
|
|
if (mEncodingOption == ENCODING_AUTO_DETECT)
|
|
|
|
mFileEncoding = ENCODING_ASCII;
|
2021-04-07 21:13:15 +08:00
|
|
|
else
|
2021-04-08 10:29:21 +08:00
|
|
|
mFileEncoding = mEncodingOption;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
2021-04-07 22:44:08 +08:00
|
|
|
mTextEdit->setProperty("editor",QVariant::fromValue<intptr_t>((intptr_t)this));
|
|
|
|
}
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-07 22:44:08 +08:00
|
|
|
Editor::~Editor() {
|
|
|
|
int index = mParentPageControl->indexOf(mTextEdit);
|
|
|
|
mParentPageControl->removeTab(index);
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::loadFile() {
|
|
|
|
QStringList strs;
|
|
|
|
QFile file(mFilename);
|
|
|
|
QByteArray ba=file.read(file.bytesAvailable());
|
2021-04-08 10:29:21 +08:00
|
|
|
if (mEncodingOption == ENCODING_AUTO_DETECT) {
|
2021-04-06 23:10:57 +08:00
|
|
|
mFileEncoding = GetFileEncodingType(ba);
|
|
|
|
} else {
|
2021-04-08 10:29:21 +08:00
|
|
|
mFileEncoding = mEncodingOption;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
if (mFileEncoding == ENCODING_UTF8) {
|
|
|
|
mTextEdit->setText(QString::fromUtf8(ba));
|
|
|
|
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
|
|
|
|
mTextEdit->setText(QString::fromUtf8(ba.mid(3)));
|
|
|
|
} else if (mFileEncoding == ENCODING_ASCII) {
|
|
|
|
mTextEdit->setText(QString::fromLatin1(ba));
|
|
|
|
}else {
|
|
|
|
QTextCodec*codec = QTextCodec::codecForName(mFileEncoding);
|
|
|
|
mTextEdit->setText(codec->toUnicode(ba));
|
2021-04-07 21:13:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::saveFile(const QString &filename) {
|
2021-04-08 10:29:21 +08:00
|
|
|
if (mEncodingOption!=ENCODING_AUTO_DETECT && mEncodingOption!=mFileEncoding) {
|
|
|
|
mFileEncoding = mEncodingOption;
|
2021-04-07 21:13:15 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
if (mEncodingOption == ENCODING_AUTO_DETECT && mFileEncoding == ENCODING_ASCII) {
|
2021-04-07 21:13:15 +08:00
|
|
|
if (!isTextAllAscii(mTextEdit->text())) {
|
2021-04-08 10:29:21 +08:00
|
|
|
mFileEncoding = pSettings->value(EDITOR_DEFAULT_ENCODING).toByteArray();
|
2021-04-07 21:13:15 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
pMainWindow->updateStatusBarForEncoding();
|
2021-04-07 21:13:15 +08:00
|
|
|
//todo: update status bar, and set fileencoding using configurations
|
|
|
|
}
|
|
|
|
QFile file(filename);
|
|
|
|
QByteArray ba;
|
2021-04-08 10:29:21 +08:00
|
|
|
if (mFileEncoding == ENCODING_UTF8) {
|
|
|
|
ba = mTextEdit->text().toUtf8();
|
|
|
|
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
|
2021-04-07 21:13:15 +08:00
|
|
|
ba.resize(3);
|
|
|
|
ba[0]=0xEF;
|
|
|
|
ba[1]=0xBB;
|
|
|
|
ba[2]=0xBF;
|
|
|
|
ba.append(mTextEdit->text().toUtf8());
|
2021-04-08 10:29:21 +08:00
|
|
|
} else if (mFileEncoding == ENCODING_ASCII) {
|
|
|
|
ba = mTextEdit->text().toLatin1();
|
|
|
|
} else {
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName(mFileEncoding);
|
|
|
|
ba = codec->fromUnicode(mTextEdit->text());
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
2021-04-07 21:13:15 +08:00
|
|
|
file.write(ba);
|
|
|
|
file.close();
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
bool Editor::save() {
|
|
|
|
|
|
|
|
return true;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
|
|
|
|
const QByteArray& Editor::encodingOption() const {
|
|
|
|
return mEncodingOption;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
void Editor::setEncodingOption(const QByteArray& encoding) {
|
|
|
|
mEncodingOption = encoding;
|
|
|
|
}
|
|
|
|
const QByteArray& Editor::fileEncoding() const {
|
2021-04-06 23:10:57 +08:00
|
|
|
return mFileEncoding;
|
|
|
|
}
|
|
|
|
const QString& Editor::filename() {
|
|
|
|
return mFilename;
|
|
|
|
}
|
|
|
|
bool Editor::inProject() const {
|
|
|
|
return mInProject;
|
|
|
|
}
|
|
|
|
bool Editor::isNew() const {
|
|
|
|
return mIsNew;
|
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
QsciScintilla* Editor::textEdit() {
|
|
|
|
return mTextEdit;
|
|
|
|
}
|