RedPanda-CPP/RedPandaIDE/editor.cpp

112 lines
2.9 KiB
C++
Raw Normal View History

2021-04-06 23:10:57 +08:00
#include "editor.h"
#include <QtCore/QFileInfo>
2021-04-07 22:44:08 +08:00
#include <QVariant>
2021-04-07 21:13:15 +08:00
#include <memory>
using namespace std;
2021-04-06 23:10:57 +08:00
Editor::Editor(QObject *parent, const QString& filename,
FileEncodingType encodingType,
bool inProject, bool isNew,
QTabWidget* parentPageControl):
QObject(parent),
mFilename(filename),
mEncodingType(encodingType),
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-07 21:13:15 +08:00
if (mEncodingType == etAuto)
mFileEncoding = etAscii;
else
mFileEncoding = mEncodingType;
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());
if (mEncodingType == etAuto) {
mFileEncoding = GetFileEncodingType(ba);
} else {
mFileEncoding = mEncodingType;
}
switch(mFileEncoding) {
case etUTF8:
2021-04-07 21:13:15 +08:00
mTextEdit->setText(QString::fromUtf8(ba));
break;
case etUTF8Bom:
mTextEdit->setText(QString::fromUtf8(ba.mid(3)));
break;
default:
mTextEdit->setText(QString::fromLocal8Bit(ba));
}
}
void Editor::saveFile(const QString &filename) {
if (mEncodingType!=etAuto && mEncodingType!=mFileEncoding) {
mFileEncoding = mEncodingType;
}
if (mEncodingType ==etAuto && mFileEncoding == etAscii) {
if (!isTextAllAscii(mTextEdit->text())) {
mFileEncoding = etAnsi;
}
//todo: update status bar, and set fileencoding using configurations
}
QFile file(filename);
QByteArray ba;
switch(mFileEncoding) {
case etUTF8:
ba = mTextEdit->text().toUtf8();
2021-04-06 23:10:57 +08:00
break;
case etUTF8Bom:
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-06 23:10:57 +08:00
break;
default:
2021-04-07 21:13:15 +08:00
ba = mTextEdit->text().toLocal8Bit();
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
}
FileEncodingType Editor::encodingType() const {
return mEncodingType;
}
void Editor::setFileEncodingType(FileEncodingType type) {
mEncodingType = type;
}
FileEncodingType Editor::fileEncoding() const {
return mFileEncoding;
}
const QString& Editor::filename() {
return mFilename;
}
bool Editor::inProject() const {
return mInProject;
}
bool Editor::isNew() const {
return mIsNew;
}