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

View File

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

View File

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

View File

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

View File

@ -117,7 +117,7 @@ void ProjectFilesWidget::disableFileOptions()
void ProjectFilesWidget::loadUnitEncoding(PProjectUnit unit)
{
if (unit->encoding() == ENCODING_AUTO_DETECT
if (unit->encoding() == ENCODING_PROJECT
|| unit->encoding() == ENCODING_SYSTEM_DEFAULT
|| unit->encoding() == ENCODING_UTF8) {
int index =ui->cbEncoding->findData(unit->encoding());
@ -224,7 +224,7 @@ void ProjectFilesWidget::on_txtBuildCommand_textChanged()
void ProjectFilesWidget::on_cbEncoding_currentTextChanged(const QString &)
{
QString userData = ui->cbEncoding->currentData().toString();
if (userData == ENCODING_AUTO_DETECT
if (userData == ENCODING_PROJECT
|| userData == ENCODING_SYSTEM_DEFAULT
|| userData == ENCODING_UTF8) {
PProjectUnit unit = currentUnit();
@ -255,7 +255,7 @@ void ProjectFilesWidget::init()
ui->spinPriority->setMaximum(9999);
ui->cbEncodingDetail->setVisible(false);
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("UTF-8"),ENCODING_UTF8);
foreach (const QString& langName, pCharsetInfoManager->languageNames()) {

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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