RedPanda-CPP/RedPandaIDE/editor.cpp

150 lines
4.0 KiB
C++
Raw Normal View History

2021-04-06 23:10:57 +08:00
#include "editor.h"
#include <QtCore/QFileInfo>
2021-04-09 17:48:25 +08:00
#include <QFont>
#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>
#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,
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),
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 {
if (mEncodingOption == ENCODING_AUTO_DETECT)
mFileEncoding = ENCODING_ASCII;
2021-04-07 21:13:15 +08:00
else
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());
if (mEncodingOption == ENCODING_AUTO_DETECT) {
2021-04-06 23:10:57 +08:00
mFileEncoding = GetFileEncodingType(ba);
} else {
mFileEncoding = mEncodingOption;
2021-04-06 23:10:57 +08:00
}
if (mFileEncoding == ENCODING_UTF8) {
2021-04-09 17:48:25 +08:00
this->setText(QString::fromUtf8(ba));
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
2021-04-09 17:48:25 +08:00
this->setText(QString::fromUtf8(ba.mid(3)));
} else if (mFileEncoding == ENCODING_ASCII) {
2021-04-09 17:48:25 +08:00
this->setText(QString::fromLatin1(ba));
}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) {
if (mEncodingOption!=ENCODING_AUTO_DETECT && mEncodingOption!=mFileEncoding) {
mFileEncoding = mEncodingOption;
2021-04-07 21:13:15 +08:00
}
if (mEncodingOption == ENCODING_AUTO_DETECT && mFileEncoding == ENCODING_ASCII) {
2021-04-09 17:48:25 +08:00
if (!isTextAllAscii(this->text())) {
mFileEncoding = pSettings->value(EDITOR_DEFAULT_ENCODING).toByteArray();
2021-04-07 21:13:15 +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;
if (mFileEncoding == ENCODING_UTF8) {
2021-04-09 17:48:25 +08:00
ba = this->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;
2021-04-09 17:48:25 +08:00
ba.append(this->text().toUtf8());
} else if (mFileEncoding == ENCODING_ASCII) {
2021-04-09 17:48:25 +08:00
ba = this->text().toLatin1();
} 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) {
return true;
2021-04-06 23:10:57 +08:00
}
const QByteArray& Editor::encodingOption() const {
return mEncodingOption;
2021-04-06 23:10:57 +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();
}
}
}