add project encoding

This commit is contained in:
Roy Qu 2023-01-24 11:31:30 +08:00
parent 1f491fccfd
commit 0ccec06f33
10 changed files with 460 additions and 439 deletions

View File

@ -427,6 +427,8 @@ void ProjectCompiler::writeMakeObjFilesRules(QFile &file)
} else { } else {
sourceEncoding = targetEncoding; sourceEncoding = targetEncoding;
} }
} else if (unit->encoding()==ENCODING_PROJECT) {
sourceEncoding=mProject->options().encoding;
} else if (unit->encoding()==ENCODING_SYSTEM_DEFAULT) { } else if (unit->encoding()==ENCODING_SYSTEM_DEFAULT) {
sourceEncoding = defaultSystemEncoding; sourceEncoding = defaultSystemEncoding;
} else if (unit->encoding()!=ENCODING_ASCII && !unit->encoding().isEmpty()) { } else if (unit->encoding()!=ENCODING_ASCII && !unit->encoding().isEmpty()) {

View File

@ -94,6 +94,9 @@ Editor::Editor(QWidget *parent, const QString& filename,
} }
QFileInfo fileInfo(mFilename); QFileInfo fileInfo(mFilename);
QSynedit::PSyntaxer syntaxer; QSynedit::PSyntaxer syntaxer;
if (mProject && mEncodingOption==ENCODING_PROJECT) {
mEncodingOption=mProject->options().encoding;
}
mFileEncoding = ENCODING_ASCII; mFileEncoding = ENCODING_ASCII;
if (!isNew) { if (!isNew) {
try { try {
@ -107,12 +110,7 @@ Editor::Editor(QWidget *parent, const QString& filename,
} else { } else {
syntaxer=syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP); syntaxer=syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
} }
if (mEncodingOption==ENCODING_AUTO_DETECT) { resolveAutoDetectEncodingOption();
if (mFileEncoding==ENCODING_ASCII)
mEncodingOption=pSettings->editor().defaultEncoding();
else
mEncodingOption=mFileEncoding;
}
if (syntaxer) { if (syntaxer) {
setSyntaxer(syntaxer); setSyntaxer(syntaxer);
setUseCodeFolding(true); setUseCodeFolding(true);
@ -239,15 +237,17 @@ void Editor::loadFile(QString filename) {
if (mProject) { if (mProject) {
PProjectUnit unit = mProject->findUnit(this); PProjectUnit unit = mProject->findUnit(this);
if (unit) { if (unit) {
if (mEncodingOption==ENCODING_AUTO_DETECT) { // if (mEncodingOption==ENCODING_AUTO_DETECT) {
if (mFileEncoding==ENCODING_ASCII) // if (mFileEncoding==ENCODING_ASCII)
unit->setEncoding(mProject->options().encoding); // unit->setEncoding(mProject->options().encoding);
else // else
unit->setEncoding(mFileEncoding); // unit->setEncoding(mFileEncoding);
mEncodingOption=unit->encoding(); // mEncodingOption=unit->encoding();
} else { // } else {
unit->setEncoding(mEncodingOption); // unit->setEncoding(mEncodingOption);
} // }
// unit->setRealEncoding(mFileEncoding);
//unit->setEncoding(mEncodingOption);
unit->setRealEncoding(mFileEncoding); unit->setRealEncoding(mFileEncoding);
} }
} }
@ -278,9 +278,10 @@ void Editor::loadFile(QString filename) {
void Editor::saveFile(QString filename) { void Editor::saveFile(QString filename) {
QFile file(filename); QFile file(filename);
QByteArray encoding = mFileEncoding; // QByteArray encoding = mFileEncoding;
if (mEncodingOption!=ENCODING_AUTO_DETECT || mFileEncoding==ENCODING_ASCII) // if (mEncodingOption != ENCODING_AUTO_DETECT || mFileEncoding==ENCODING_ASCII)
encoding = mEncodingOption; // encoding = mEncodingOption;
QByteArray encoding = mEncodingOption;
// save backup // save backup
// QString backupFilename=filename+".savebak"; // QString backupFilename=filename+".savebak";
// int count=1; // int count=1;
@ -541,6 +542,7 @@ void Editor::setEncodingOption(const QByteArray& encoding) noexcept{
} }
} else if (mParentPageControl) } else if (mParentPageControl)
pMainWindow->updateForEncodingInfo(this); pMainWindow->updateForEncodingInfo(this);
resolveAutoDetectEncodingOption();
if (mProject) { if (mProject) {
PProjectUnit unit = mProject->findUnit(this); PProjectUnit unit = mProject->findUnit(this);
if (unit) { if (unit) {
@ -1958,6 +1960,16 @@ void Editor::onAutoBackupTimer()
saveAutoBackup(); saveAutoBackup();
} }
void Editor::resolveAutoDetectEncodingOption()
{
if (mEncodingOption==ENCODING_AUTO_DETECT) {
if (mFileEncoding==ENCODING_ASCII)
mEncodingOption=pSettings->editor().defaultEncoding();
else
mEncodingOption=mFileEncoding;
}
}
bool Editor::isBraceChar(QChar ch) bool Editor::isBraceChar(QChar ch)
{ {
switch( ch.unicode()) { switch( ch.unicode()) {

View File

@ -236,6 +236,7 @@ private slots:
void onAutoBackupTimer(); void onAutoBackupTimer();
private: private:
void resolveAutoDetectEncodingOption();
bool isBraceChar(QChar ch); bool isBraceChar(QChar ch);
bool shouldOpenInReadonly(); bool shouldOpenInReadonly();
QChar getCurrentChar(); QChar getCurrentChar();

View File

@ -469,14 +469,12 @@ void MainWindow::updateForEncodingInfo(const Editor* editor, bool clear) {
.arg(QString(editor->encodingOption())) .arg(QString(editor->encodingOption()))
); );
} }
//ui->actionAuto_Detect->setEnabled(editor->inProject());
//ui->actionAuto_Detect->setChecked(editor->encodingOption() == ENCODING_AUTO_DETECT); //ui->actionAuto_Detect->setChecked(editor->encodingOption() == ENCODING_AUTO_DETECT);
ui->actionEncode_in_ANSI->setChecked(editor->encodingOption() == ENCODING_SYSTEM_DEFAULT); ui->actionEncode_in_ANSI->setChecked(editor->encodingOption() == ENCODING_SYSTEM_DEFAULT);
ui->actionEncode_in_UTF_8->setChecked(editor->encodingOption() == ENCODING_UTF8); ui->actionEncode_in_UTF_8->setChecked(editor->encodingOption() == ENCODING_UTF8);
ui->actionEncode_in_UTF_8_BOM->setChecked(editor->encodingOption() == ENCODING_UTF8_BOM); ui->actionEncode_in_UTF_8_BOM->setChecked(editor->encodingOption() == ENCODING_UTF8_BOM);
} else { } else {
mFileEncodingStatus->setText(""); mFileEncodingStatus->setText("");
//ui->actionAuto_Detect->setEnabled(true);
//ui->actionAuto_Detect->setChecked(false); //ui->actionAuto_Detect->setChecked(false);
ui->actionEncode_in_ANSI->setChecked(false); ui->actionEncode_in_ANSI->setChecked(false);
ui->actionEncode_in_UTF_8->setChecked(false); ui->actionEncode_in_UTF_8->setChecked(false);
@ -3335,7 +3333,7 @@ void MainWindow::buildEncodingMenu()
mMenuEncoding = new QMenu(); mMenuEncoding = new QMenu();
mMenuEncoding->setTitle(tr("File Encoding")); mMenuEncoding->setTitle(tr("File Encoding"));
//mMenuEncoding->addAction(ui->actionAuto_Detect); mMenuEncoding->addAction(ui->actionAuto_Detect);
mMenuEncoding->addAction(ui->actionEncode_in_ANSI); mMenuEncoding->addAction(ui->actionEncode_in_ANSI);
mMenuEncoding->addAction(ui->actionEncode_in_UTF_8); mMenuEncoding->addAction(ui->actionEncode_in_UTF_8);
mMenuEncoding->addAction(ui->actionEncode_in_UTF_8_BOM); mMenuEncoding->addAction(ui->actionEncode_in_UTF_8_BOM);
@ -4593,7 +4591,6 @@ void MainWindow::onClassBrowserRefreshEnd()
void MainWindow::onProjectSwitchCustomViewMode() void MainWindow::onProjectSwitchCustomViewMode()
{ {
mProject->setModelType(ProjectModelType::Custom); mProject->setModelType(ProjectModelType::Custom);
qDebug()<<"3";
ui->projectView->expand( ui->projectView->expand(
mProjectProxyModel->mapFromSource( mProjectProxyModel->mapFromSource(
mProject->model()->rootIndex())); mProject->model()->rootIndex()));

View File

@ -165,7 +165,7 @@ bool Project::unitsModifiedSince(const QDateTime& time)
foreach(const PProjectUnit& unit, mUnits) { foreach(const PProjectUnit& unit, mUnits) {
QFileInfo info(unit->fileName()); QFileInfo info(unit->fileName());
if (info.lastModified()>time) { if (info.lastModified()>time) {
qDebug()<<info.lastModified()<<time; //qDebug()<<info.lastModified()<<time;
return true; return true;
} }
Editor * e=unitEditor(unit); Editor * e=unitEditor(unit);
@ -236,15 +236,9 @@ void Project::open()
newUnit->setPriority(ini.GetLongValue(groupName,"Priority", 1000)); newUnit->setPriority(ini.GetLongValue(groupName,"Priority", 1000));
newUnit->setOverrideBuildCmd(ini.GetBoolValue(groupName,"OverrideBuildCmd", false)); newUnit->setOverrideBuildCmd(ini.GetBoolValue(groupName,"OverrideBuildCmd", false));
newUnit->setBuildCmd(fromByteArray(ini.GetValue(groupName,"BuildCmd", ""))); newUnit->setBuildCmd(fromByteArray(ini.GetValue(groupName,"BuildCmd", "")));
QByteArray defaultEncoding = mOptions.encoding; newUnit->setEncoding(ini.GetValue(groupName, "FileEncoding",ENCODING_PROJECT));
//Compatibility
if (ini.GetBoolValue(groupName,"DetectEncoding",false)){
defaultEncoding = mOptions.encoding;
}
newUnit->setEncoding(ini.GetValue(groupName, "FileEncoding",defaultEncoding));
if (QTextCodec::codecForName(newUnit->encoding())==nullptr) { if (QTextCodec::codecForName(newUnit->encoding())==nullptr) {
newUnit->setEncoding(mOptions.encoding); newUnit->setEncoding(ENCODING_PROJECT);
} }
newUnit->setRealEncoding(ini.GetValue(groupName, "RealEncoding",ENCODING_ASCII)); newUnit->setRealEncoding(ini.GetValue(groupName, "RealEncoding",ENCODING_ASCII));
@ -764,11 +758,7 @@ bool Project::saveUnits()
ini.SetValue(groupName,"BuildCmd", toByteArray(unit->buildCmd())); ini.SetValue(groupName,"BuildCmd", toByteArray(unit->buildCmd()));
//ini.SetLongValue(groupName,"DetectEncoding", unit->encoding()==ENCODING_AUTO_DETECT); //ini.SetLongValue(groupName,"DetectEncoding", unit->encoding()==ENCODING_AUTO_DETECT);
ini.Delete(groupName,"DetectEncoding"); ini.Delete(groupName,"DetectEncoding");
if (unit->encoding() != options().encoding ini.SetValue(groupName,"FileEncoding", unit->encoding());
&& unit->encoding()!=ENCODING_AUTO_DETECT)
ini.SetValue(groupName,"FileEncoding", unit->encoding());
else
ini.Delete(groupName,"FileEncoding");
ini.SetValue(groupName,"RealEncoding",unit->realEncoding()); ini.SetValue(groupName,"RealEncoding",unit->realEncoding());
} }
ini.SetLongValue("Project","UnitCount",count); ini.SetLongValue("Project","UnitCount",count);
@ -802,15 +792,25 @@ void Project::associateEditorToUnit(Editor *editor, PProjectUnit unit)
return; return;
} }
if (editor) { if (editor) {
editor->setProject(this); Editor * e= unitEditor(unit);
if (editor->encodingOption()==ENCODING_AUTO_DETECT) { if (e) {
if (editor->fileEncoding()==ENCODING_ASCII) { if (editor==e)
editor->setEncodingOption(mOptions.encoding); return;
} else { e->setProject(nullptr);
editor->setEncodingOption(editor->fileEncoding()); e->close();
}
} }
unit->setEncoding(editor->encodingOption()); editor->setProject(this);
// if (editor->encodingOption()==ENCODING_AUTO_DETECT) {
// if (editor->fileEncoding()==ENCODING_ASCII) {
// editor->setEncodingOption(mOptions.encoding);
// } else {
// editor->setEncodingOption(editor->fileEncoding());
// }
// }
if (editor->encodingOption()!=mOptions.encoding)
unit->setEncoding(editor->encodingOption());
else if (editor->encodingOption()!=unit->encoding())
unit->setEncoding(editor->encodingOption());
unit->setRealEncoding(editor->fileEncoding()); unit->setRealEncoding(editor->fileEncoding());
} }
} }
@ -1234,7 +1234,7 @@ PProjectUnit Project::internalAddUnit(const QString &inFileName, PProjectModelNo
// newUnit->setRealEncoding(e->fileEncoding()); // newUnit->setRealEncoding(e->fileEncoding());
// e->setProject(this); // e->setProject(this);
} else { } else {
newUnit->setEncoding(options().encoding); newUnit->setEncoding(ENCODING_PROJECT);
} }
// Determine compilation flags // Determine compilation flags
@ -2307,7 +2307,7 @@ ProjectUnit::ProjectUnit(Project* parent)
mFileMissing = false; mFileMissing = false;
mPriority=0; mPriority=0;
mNew = true; mNew = true;
mEncoding=ENCODING_SYSTEM_DEFAULT; mEncoding=ENCODING_PROJECT;
mRealEncoding=""; mRealEncoding="";
} }

View File

@ -117,7 +117,7 @@ void ProjectFilesWidget::disableFileOptions()
void ProjectFilesWidget::loadUnitEncoding(PProjectUnit unit) void ProjectFilesWidget::loadUnitEncoding(PProjectUnit unit)
{ {
if (unit->encoding() == ENCODING_AUTO_DETECT if (unit->encoding() == ENCODING_PROJECT
|| unit->encoding() == ENCODING_SYSTEM_DEFAULT || unit->encoding() == ENCODING_SYSTEM_DEFAULT
|| unit->encoding() == ENCODING_UTF8) { || unit->encoding() == ENCODING_UTF8) {
int index =ui->cbEncoding->findData(unit->encoding()); int index =ui->cbEncoding->findData(unit->encoding());
@ -224,7 +224,7 @@ void ProjectFilesWidget::on_txtBuildCommand_textChanged()
void ProjectFilesWidget::on_cbEncoding_currentTextChanged(const QString &) void ProjectFilesWidget::on_cbEncoding_currentTextChanged(const QString &)
{ {
QString userData = ui->cbEncoding->currentData().toString(); QString userData = ui->cbEncoding->currentData().toString();
if (userData == ENCODING_AUTO_DETECT if (userData == ENCODING_PROJECT
|| userData == ENCODING_SYSTEM_DEFAULT || userData == ENCODING_SYSTEM_DEFAULT
|| userData == ENCODING_UTF8) { || userData == ENCODING_UTF8) {
PProjectUnit unit = currentUnit(); PProjectUnit unit = currentUnit();
@ -255,7 +255,7 @@ void ProjectFilesWidget::init()
ui->spinPriority->setMaximum(9999); ui->spinPriority->setMaximum(9999);
ui->cbEncodingDetail->setVisible(false); ui->cbEncodingDetail->setVisible(false);
ui->cbEncoding->clear(); ui->cbEncoding->clear();
ui->cbEncoding->addItem(tr("Auto detect"),ENCODING_AUTO_DETECT); ui->cbEncoding->addItem(tr("Project(%1)").arg(QString(pMainWindow->project()->options().encoding)),ENCODING_PROJECT);
ui->cbEncoding->addItem(tr("ANSI"),ENCODING_SYSTEM_DEFAULT); ui->cbEncoding->addItem(tr("ANSI"),ENCODING_SYSTEM_DEFAULT);
ui->cbEncoding->addItem(tr("UTF-8"),ENCODING_UTF8); ui->cbEncoding->addItem(tr("UTF-8"),ENCODING_UTF8);
foreach (const QString& langName, pCharsetInfoManager->languageNames()) { foreach (const QString& langName, pCharsetInfoManager->languageNames()) {

View File

@ -5578,7 +5578,7 @@
</message> </message>
<message> <message>
<source>Auto detect</source> <source>Auto detect</source>
<translation>Detecção automática</translation> <translation type="vanished">Detecção automática</translation>
</message> </message>
<message> <message>
<source>ANSI</source> <source>ANSI</source>
@ -5588,6 +5588,10 @@
<source>UTF-8</source> <source>UTF-8</source>
<translation>UTF-8</translation> <translation>UTF-8</translation>
</message> </message>
<message>
<source>Project(%1)</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>ProjectGeneralWidget</name> <name>ProjectGeneralWidget</name>

File diff suppressed because it is too large Load Diff

View File

@ -5353,10 +5353,6 @@
<source>Override build command:</source> <source>Override build command:</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Auto detect</source>
<translation type="unfinished"></translation>
</message>
<message> <message>
<source>ANSI</source> <source>ANSI</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
@ -5365,6 +5361,10 @@
<source>UTF-8</source> <source>UTF-8</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Project(%1)</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>ProjectGeneralWidget</name> <name>ProjectGeneralWidget</name>

View File

@ -39,6 +39,7 @@ class QTextCodec;
#define ENCODING_UTF32_BOM "UTF-32 BOM" #define ENCODING_UTF32_BOM "UTF-32 BOM"
#define ENCODING_SYSTEM_DEFAULT "SYSTEM" #define ENCODING_SYSTEM_DEFAULT "SYSTEM"
#define ENCODING_ASCII "ASCII" #define ENCODING_ASCII "ASCII"
#define ENCODING_PROJECT "PROJECT"
enum class NewlineType { enum class NewlineType {
Windows, Windows,