RedPanda-CPP/RedPandaIDE/editor.cpp

319 lines
9.6 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-11 21:33:08 +08:00
#include "systemconsts.h"
2021-04-09 17:48:25 +08:00
#include <Qsci/qscilexercpp.h>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
2021-04-07 21:13:15 +08:00
using namespace std;
2021-04-06 23:10:57 +08:00
2021-04-11 21:33:08 +08:00
SaveException::SaveException(const QString& reason) {
mReason = reason;
}
SaveException::SaveException(const QString&& reason) {
mReason = reason;
}
const QString& SaveException::reason() const noexcept{
return mReason;
}
const char *SaveException::what() const noexcept {
return mReason.toLocal8Bit();
}
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);
if (mParentPageControl!=NULL) {
mParentPageControl->addTab(this,QString());
updateCaption();
}
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);
2021-04-11 13:55:31 +08:00
lexer->setFoldComments(true);
lexer->setDefaultFont(QFont("Consolas",12));
2021-04-09 17:48:25 +08:00
this->setLexer(lexer);
2021-04-13 11:23:37 +08:00
this->setAutoIndent(pSettings->editor().autoIndent());
2021-04-11 13:55:31 +08:00
this->setFolding(FoldStyle::BoxedTreeFoldStyle,3);
this->setTabWidth(4);
2021-04-11 13:55:31 +08:00
//行号显示区域
setMarginType(0, QsciScintilla::NumberMargin);
setMarginLineNumbers(0, true);
setMarginWidth(0,"10");
this->onLinesChanged();
2021-04-11 13:55:31 +08:00
//断点设置区域
setMarginType(1, QsciScintilla::SymbolMargin);
setMarginLineNumbers(1, false);
setMarginWidth(1,20);
setMarginSensitivity(1, true); //设置是否可以显示断点
setMarginsBackgroundColor(QColor("#bbfaae"));
setMarginMarkerMask(1, 0x02);
// connect(textEdit, SIGNAL(marginClicked(int, int, Qt::KeyboardModifiers)),this,
// SLOT(on_margin_clicked(int, int, Qt::KeyboardModifiers)));
markerDefine(QsciScintilla::Circle, 1);
setMarkerBackgroundColor(QColor("#ee1111"), 1);
//单步执行显示区域
setMarginType(2, QsciScintilla::SymbolMargin);
setMarginLineNumbers(2, false);
setMarginWidth(2, 20);
setMarginSensitivity(2, false);
setMarginMarkerMask(2, 0x04);
markerDefine(QsciScintilla::RightArrow, 2);
setMarkerBackgroundColor(QColor("#eaf593"), 2);
//自动折叠区域
setMarginType(3, QsciScintilla::SymbolMargin);
setMarginLineNumbers(3, false);
setMarginWidth(3, 15);
setMarginSensitivity(3, true);
2021-04-09 17:48:25 +08:00
// connect will fail if use new function pointer syntax
2021-04-11 13:55:31 +08:00
// connect(this, &QsciScintilla::modificationChanged,
// this, &Editor::onModificationChanged);
// connect(this , &QsciScintilla::cursorPositionChanged,
// this, &Editor::onCursorPositionChanged);
connect(this,SIGNAL(modificationChanged(bool)),
this,SLOT(onModificationChanged(bool)));
2021-04-11 13:55:31 +08:00
connect(this , SIGNAL(cursorPositionChanged(int,int)),
this, SLOT(onCursorPositionChanged(int,int)));
connect(this, SIGNAL(linesChanged()),
this,SLOT(onLinesChanged()));
2021-04-11 21:33:08 +08:00
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() {
QFile file(mFilename);
2021-04-11 21:33:08 +08:00
if (!file.open(QFile::ReadOnly)) {
QMessageBox::information(pMainWindow,
tr("Error"),
QString(tr("Can't Open File %1:%2")).arg(mFilename).arg(file.errorString()));
}
QByteArray content=file.readAll();
file.close();
if (mEncodingOption == ENCODING_AUTO_DETECT) {
mFileEncoding = GuessTextEncoding(content);
2021-04-06 23:10:57 +08:00
} else {
mFileEncoding = mEncodingOption;
2021-04-06 23:10:57 +08:00
}
if (mFileEncoding == ENCODING_UTF8) {
this->setText(QString::fromUtf8(content));
} else if (mFileEncoding == ENCODING_UTF8_BOM) {
this->setText(QString::fromUtf8(content.mid(3)));
} else if (mFileEncoding == ENCODING_ASCII) {
this->setText(QString::fromLatin1(content));
2021-04-11 21:33:08 +08:00
} else if (mFileEncoding == ENCODING_SYSTEM_DEFAULT) {
this->setText(QString::fromLocal8Bit(content));
}else {
QTextCodec*codec = QTextCodec::codecForName(mFileEncoding);
this->setText(codec->toUnicode(content));
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())) {
2021-04-13 11:23:37 +08:00
mFileEncoding = pSettings->editor().defaultEncoding();
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();
2021-04-11 21:33:08 +08:00
} else if (mFileEncoding == ENCODING_SYSTEM_DEFAULT) {
ba = this->text().toLocal8Bit();
} 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-11 21:33:08 +08:00
if (file.open(QFile::WriteOnly)) {
if (file.write(ba)<0) {
throw SaveException(QString(tr("Failed to Save file %1: %2")).arg(filename).arg(file.errorString()));
}
file.close();
} else {
throw SaveException(QString(tr("Failed to Open file %1: %2")).arg(filename).arg(file.errorString()));
}
2021-04-06 23:10:57 +08:00
}
2021-04-09 10:08:05 +08:00
bool Editor::save(bool force, bool reparse) {
if (this->mIsNew) {
return saveAs();
}
QFileInfo info(mFilename);
2021-04-11 21:33:08 +08:00
//is this file writable;
if (!force && !info.isWritable()) {
QMessageBox::information(pMainWindow,tr("Fail"),
QString(QObject::tr("File %s is not writable!")));
return false;
}
if (this->isModified() || force) {
2021-04-11 21:33:08 +08:00
try {
saveFile(mFilename);
setModified(false);
} catch (SaveException& exception) {
QMessageBox::information(pMainWindow,tr("Fail"),
exception.reason());
return false;
}
}
if (reparse) {
//todo: reparse the file
}
return true;
}
bool Editor::saveAs(){
2021-04-11 21:33:08 +08:00
QString selectedFileFilter = pSystemConsts->defaultFileFilter();
QString newName = QFileDialog::getSaveFileName(pMainWindow,
2021-04-11 21:33:08 +08:00
tr("Save As"), QString(), pSystemConsts->defaultFileFilters().join(";;"),
&selectedFileFilter);
if (newName.isEmpty()) {
return false;
}
2021-04-11 21:33:08 +08:00
try {
saveFile(mFilename);
mFilename = newName;
mIsNew = false;
setModified(false);
} catch (SaveException& exception) {
QMessageBox::information(pMainWindow,tr("Fail"),
exception.reason());
return false;
}
//todo: update (reassign highlighter)
//todo: remove old file from parser and reparse file
//todo: unmoniter/ monitor file
//todo: update windows caption
//todo: update class browser;
return true;
2021-04-06 23:10:57 +08:00
}
void Editor::activate()
{
this->mParentPageControl->setCurrentWidget(this);
}
2021-04-11 21:33:08 +08:00
const QByteArray& Editor::encodingOption() const noexcept{
return mEncodingOption;
2021-04-06 23:10:57 +08:00
}
2021-04-11 21:33:08 +08:00
void Editor::setEncodingOption(const QByteArray& encoding) noexcept{
mEncodingOption = encoding;
}
2021-04-11 21:33:08 +08:00
const QByteArray& Editor::fileEncoding() const noexcept{
2021-04-06 23:10:57 +08:00
return mFileEncoding;
}
2021-04-11 21:33:08 +08:00
const QString& Editor::filename() const noexcept{
2021-04-06 23:10:57 +08:00
return mFilename;
}
2021-04-11 21:33:08 +08:00
bool Editor::inProject() const noexcept{
2021-04-06 23:10:57 +08:00
return mInProject;
}
2021-04-11 21:33:08 +08:00
bool Editor::isNew() const noexcept {
2021-04-06 23:10:57 +08:00
return mIsNew;
}
2021-04-09 17:48:25 +08:00
2021-04-11 21:33:08 +08:00
QTabWidget* Editor::pageControl() noexcept{
2021-04-09 17:48:25 +08:00
return mParentPageControl;
}
void Editor::wheelEvent(QWheelEvent *event) {
if ( (event->modifiers() & Qt::ControlModifier)!=0) {
if (event->angleDelta().y()>0) {
this->zoomIn();
} else {
this->zoomOut();
}
onLinesChanged();
2021-04-09 17:48:25 +08:00
}
}
2021-04-11 21:33:08 +08:00
void Editor::onModificationChanged(bool) {
updateCaption();
}
2021-04-11 21:33:08 +08:00
void Editor::onCursorPositionChanged(int line, int index) {
2021-04-11 13:55:31 +08:00
pMainWindow->updateStatusBarForEditingInfo(line,index+1,lines(),text().length());
}
2021-04-11 21:33:08 +08:00
void Editor::onLinesChanged() {
this->setMarginWidth(0,QString("0%1").arg(lines()));
qDebug()<<marginWidth(0);
2021-04-20 22:24:33 +08:00
qDebug()<<"Editor lines changed"<<lines();
2021-04-11 13:55:31 +08:00
}
void Editor::updateCaption(const QString& newCaption) {
if (mParentPageControl==NULL) {
return;
}
int index = mParentPageControl->indexOf(this);
if (index==-1)
return;
if (newCaption.isEmpty()) {
QString caption = QFileInfo(mFilename).fileName();
if (this->isModified()) {
caption.append("[*]");
}
mParentPageControl->setTabText(index,caption);
} else {
mParentPageControl->setTabText(index,newCaption);
}
}