2021-04-06 23:10:57 +08:00
|
|
|
#include "editor.h"
|
|
|
|
|
|
|
|
#include <QtCore/QFileInfo>
|
2021-04-09 17:48:25 +08:00
|
|
|
#include <QFont>
|
2021-04-08 10:29:21 +08:00
|
|
|
#include <QTextCodec>
|
2021-04-07 22:44:08 +08:00
|
|
|
#include <QVariant>
|
2021-04-09 17:48:25 +08:00
|
|
|
#include <QWheelEvent>
|
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-09 17:48:25 +08:00
|
|
|
#include <Qsci/qscilexercpp.h>
|
2021-04-07 21:13:15 +08:00
|
|
|
|
|
|
|
using namespace std;
|
2021-04-06 23:10:57 +08:00
|
|
|
|
|
|
|
|
2021-04-09 17:48:25 +08:00
|
|
|
Editor::Editor(QWidget *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):
|
2021-04-09 17:48:25 +08:00
|
|
|
QsciScintilla(parent),
|
2021-04-06 23:10:57 +08:00
|
|
|
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)
|
|
|
|
{
|
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);
|
2021-04-09 17:48:25 +08:00
|
|
|
if (mParentPageControl!=NULL)
|
|
|
|
mParentPageControl->addTab(this,fileInfo.fileName());
|
2021-04-06 23:10:57 +08:00
|
|
|
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-09 17:48:25 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
QsciLexerCPP *lexer = new QsciLexerCPP();
|
|
|
|
lexer->setHighlightEscapeSequences(true);
|
|
|
|
this->setLexer(lexer);
|
|
|
|
this->setAutoIndent(pSettings->value(EDITOR_AUTO_INDENT).toBool());
|
|
|
|
|
2021-04-07 22:44:08 +08:00
|
|
|
}
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-07 22:44:08 +08:00
|
|
|
Editor::~Editor() {
|
2021-04-09 17:48:25 +08:00
|
|
|
if (mParentPageControl!=NULL) {
|
|
|
|
int index = mParentPageControl->indexOf(this);
|
|
|
|
mParentPageControl->removeTab(index);
|
|
|
|
}
|
|
|
|
this->setParent(0);
|
|
|
|
|
|
|
|
delete this->lexer();
|
|
|
|
this->setLexer(NULL);
|
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) {
|
2021-04-09 17:48:25 +08:00
|
|
|
this->setText(QString::fromUtf8(ba));
|
2021-04-08 10:29:21 +08:00
|
|
|
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
|
2021-04-09 17:48:25 +08:00
|
|
|
this->setText(QString::fromUtf8(ba.mid(3)));
|
2021-04-08 10:29:21 +08:00
|
|
|
} else if (mFileEncoding == ENCODING_ASCII) {
|
2021-04-09 17:48:25 +08:00
|
|
|
this->setText(QString::fromLatin1(ba));
|
2021-04-08 10:29:21 +08:00
|
|
|
}else {
|
|
|
|
QTextCodec*codec = QTextCodec::codecForName(mFileEncoding);
|
2021-04-09 17:48:25 +08:00
|
|
|
this->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-09 17:48:25 +08:00
|
|
|
if (!isTextAllAscii(this->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) {
|
2021-04-09 17:48:25 +08:00
|
|
|
ba = this->text().toUtf8();
|
2021-04-08 10:29:21 +08:00
|
|
|
} 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;
|
2021-04-09 17:48:25 +08:00
|
|
|
ba.append(this->text().toUtf8());
|
2021-04-08 10:29:21 +08:00
|
|
|
} else if (mFileEncoding == ENCODING_ASCII) {
|
2021-04-09 17:48:25 +08:00
|
|
|
ba = this->text().toLatin1();
|
2021-04-08 10:29:21 +08:00
|
|
|
} else {
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName(mFileEncoding);
|
2021-04-09 17:48:25 +08:00
|
|
|
ba = codec->fromUnicode(this->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-09 10:08:05 +08:00
|
|
|
bool Editor::save(bool force, bool reparse) {
|
2021-04-08 10:29:21 +08:00
|
|
|
|
|
|
|
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-09 17:48:25 +08:00
|
|
|
|
|
|
|
QTabWidget* Editor::pageControl() {
|
|
|
|
return mParentPageControl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::wheelEvent(QWheelEvent *event) {
|
|
|
|
if ( (event->modifiers() & Qt::ControlModifier)!=0) {
|
|
|
|
if (event->angleDelta().y()>0) {
|
|
|
|
this->zoomIn();
|
|
|
|
} else {
|
|
|
|
this->zoomOut();
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
}
|