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-11 21:33:08 +08:00
|
|
|
#include "systemconsts.h"
|
2021-04-11 12:39:22 +08:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDebug>
|
2021-05-24 00:41:00 +08:00
|
|
|
#include "qsynedit/highlighter/cpp.h"
|
|
|
|
|
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,
|
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-05-24 00:41:00 +08:00
|
|
|
SynEdit(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-11 12:39:22 +08:00
|
|
|
if (mParentPageControl!=NULL) {
|
|
|
|
mParentPageControl->addTab(this,QString());
|
|
|
|
updateCaption();
|
|
|
|
}
|
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
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
//SynEditCppHighlighter highlighter;
|
2021-04-29 20:54:44 +08:00
|
|
|
|
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);
|
|
|
|
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::loadFile() {
|
|
|
|
QFile file(mFilename);
|
2021-05-24 00:41:00 +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()));
|
|
|
|
// }
|
|
|
|
this->lines()->LoadFromFile(file,mEncodingOption,mFileEncoding);
|
2021-05-24 18:11:07 +08:00
|
|
|
this->setModified(false);
|
|
|
|
updateCaption();
|
|
|
|
pMainWindow->updateStatusBarForEncoding();
|
2021-04-07 21:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::saveFile(const QString &filename) {
|
2021-05-24 00:41:00 +08:00
|
|
|
// if (mEncodingOption!=ENCODING_AUTO_DETECT && mEncodingOption!=mFileEncoding) {
|
|
|
|
// mFileEncoding = mEncodingOption;
|
|
|
|
// }
|
|
|
|
// if (mEncodingOption == ENCODING_AUTO_DETECT && mFileEncoding == ENCODING_ASCII) {
|
|
|
|
// if (!isTextAllAscii(this->text())) {
|
|
|
|
// mFileEncoding = pSettings->editor().defaultEncoding();
|
|
|
|
// }
|
|
|
|
// pMainWindow->updateStatusBarForEncoding();
|
|
|
|
// //todo: update status bar, and set fileencoding using configurations
|
|
|
|
// }
|
2021-04-07 21:13:15 +08:00
|
|
|
QFile file(filename);
|
2021-05-24 00:41:00 +08:00
|
|
|
this->lines()->SaveToFile(file,mEncodingOption,mFileEncoding);
|
|
|
|
pMainWindow->updateStatusBarForEncoding();
|
|
|
|
// QByteArray ba;
|
|
|
|
// if (mFileEncoding == ENCODING_UTF8) {
|
|
|
|
// ba = this->text().toUtf8();
|
|
|
|
// } else if (mFileEncoding == ENCODING_UTF8_BOM) {
|
|
|
|
// ba.resize(3);
|
|
|
|
// ba[0]=0xEF;
|
|
|
|
// ba[1]=0xBB;
|
|
|
|
// ba[2]=0xBF;
|
|
|
|
// ba.append(this->text().toUtf8());
|
|
|
|
// } else if (mFileEncoding == ENCODING_ASCII) {
|
|
|
|
// ba = this->text().toLatin1();
|
|
|
|
// } else if (mFileEncoding == ENCODING_SYSTEM_DEFAULT) {
|
|
|
|
// ba = this->text().toLocal8Bit();
|
|
|
|
// } else {
|
|
|
|
// QTextCodec* codec = QTextCodec::codecForName(mFileEncoding);
|
|
|
|
// ba = codec->fromUnicode(this->text());
|
|
|
|
// }
|
|
|
|
// 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) {
|
2021-04-11 12:39:22 +08:00
|
|
|
if (this->mIsNew) {
|
|
|
|
return saveAs();
|
|
|
|
}
|
|
|
|
QFileInfo info(mFilename);
|
2021-04-11 21:33:08 +08:00
|
|
|
//is this file writable;
|
2021-04-11 12:39:22 +08:00
|
|
|
if (!force && !info.isWritable()) {
|
|
|
|
QMessageBox::information(pMainWindow,tr("Fail"),
|
|
|
|
QString(QObject::tr("File %s is not writable!")));
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-24 00:41:00 +08:00
|
|
|
if (this->modified()|| 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;
|
|
|
|
}
|
2021-04-11 12:39:22 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
|
2021-04-11 12:39:22 +08:00
|
|
|
if (reparse) {
|
|
|
|
//todo: reparse the file
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Editor::saveAs(){
|
2021-04-11 21:33:08 +08:00
|
|
|
QString selectedFileFilter = pSystemConsts->defaultFileFilter();
|
2021-04-11 12:39:22 +08:00
|
|
|
QString newName = QFileDialog::getSaveFileName(pMainWindow,
|
2021-04-11 21:33:08 +08:00
|
|
|
tr("Save As"), QString(), pSystemConsts->defaultFileFilters().join(";;"),
|
|
|
|
&selectedFileFilter);
|
2021-04-11 12:39:22 +08:00
|
|
|
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;
|
|
|
|
}
|
2021-04-11 12:39:22 +08:00
|
|
|
|
|
|
|
//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;
|
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
|
|
|
|
2021-04-12 00:00:29 +08:00
|
|
|
void Editor::activate()
|
|
|
|
{
|
|
|
|
this->mParentPageControl->setCurrentWidget(this);
|
2021-04-29 20:54:44 +08:00
|
|
|
this->setFocus();
|
2021-04-12 00:00:29 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:33:08 +08:00
|
|
|
const QByteArray& Editor::encodingOption() const noexcept{
|
2021-04-08 10:29:21 +08:00
|
|
|
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{
|
2021-04-08 10:29:21 +08:00
|
|
|
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) {
|
2021-05-24 00:41:00 +08:00
|
|
|
QFont oldFont = font();
|
|
|
|
int size = oldFont.pointSize();
|
2021-04-09 17:48:25 +08:00
|
|
|
if (event->angleDelta().y()>0) {
|
2021-05-24 00:41:00 +08:00
|
|
|
size = std::max(5,size-1);
|
|
|
|
oldFont.setPointSize(oldFont.pointSize());
|
|
|
|
this->setFont(oldFont);
|
|
|
|
//this->zoomIn();
|
2021-04-09 17:48:25 +08:00
|
|
|
} else {
|
2021-05-24 00:41:00 +08:00
|
|
|
size = std::min(size+1,50);
|
|
|
|
oldFont.setPointSize(oldFont.pointSize());
|
|
|
|
this->setFont(oldFont);
|
|
|
|
//this->zoomOut();
|
2021-04-09 17:48:25 +08:00
|
|
|
}
|
2021-04-24 15:57:45 +08:00
|
|
|
onLinesChanged(0,0);
|
2021-04-09 17:48:25 +08:00
|
|
|
}
|
2021-04-08 10:29:21 +08:00
|
|
|
}
|
2021-04-11 12:39:22 +08:00
|
|
|
|
2021-04-11 21:33:08 +08:00
|
|
|
void Editor::onModificationChanged(bool) {
|
2021-04-11 12:39:22 +08:00
|
|
|
updateCaption();
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:33:08 +08:00
|
|
|
void Editor::onCursorPositionChanged(int line, int index) {
|
2021-05-24 00:41:00 +08:00
|
|
|
pMainWindow->updateStatusBarForEditingInfo(line,index+1,lines()->count(),lines()->getTextLength());
|
2021-04-29 20:54:44 +08:00
|
|
|
|
2021-04-11 13:55:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-24 15:57:45 +08:00
|
|
|
void Editor::onLinesChanged(int startLine, int count) {
|
2021-05-24 00:41:00 +08:00
|
|
|
qDebug()<<"Editor lines changed"<<lines()->count();
|
2021-04-24 15:57:45 +08:00
|
|
|
qDebug()<<startLine<<count;
|
2021-04-11 13:55:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 12:39:22 +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();
|
2021-05-24 00:41:00 +08:00
|
|
|
if (this->modified()) {
|
2021-04-11 12:39:22 +08:00
|
|
|
caption.append("[*]");
|
|
|
|
}
|
|
|
|
mParentPageControl->setTabText(index,caption);
|
|
|
|
} else {
|
|
|
|
mParentPageControl->setTabText(index,newCaption);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|