From 5789aeee45fb744fd419e283eb341a17e2ce47e8 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sat, 12 Mar 2022 17:37:53 +0800 Subject: [PATCH] - enhancement: auto detect "gbk" encoding when running in zh_CN locale under Linux - enhancement: disable encoding submenu when editor closed - enhancement: clear infos in the status bar when editor closed --- NEWS.md | 4 + RedPandaIDE/cpprefacter.cpp | 2 +- RedPandaIDE/editor.cpp | 12 +- RedPandaIDE/editorlist.cpp | 2 +- RedPandaIDE/main.cpp | 5 +- RedPandaIDE/mainwindow.cpp | 22 +- RedPandaIDE/mainwindow.h | 6 +- RedPandaIDE/platform.cpp | 328 +++++++++--------- RedPandaIDE/platform.h | 8 +- RedPandaIDE/qsynedit/TextBuffer.cpp | 53 ++- RedPandaIDE/qsynedit/TextBuffer.h | 2 + RedPandaIDE/settings.cpp | 31 +- RedPandaIDE/settings.h | 8 +- .../compilersetoptionwidget.cpp | 3 +- .../settingsdialog/editormiscwidget.cpp | 62 +++- RedPandaIDE/settingsdialog/editormiscwidget.h | 6 + .../settingsdialog/editormiscwidget.ui | 32 +- .../settingsdialog/projectfileswidget.cpp | 3 +- 18 files changed, 387 insertions(+), 202 deletions(-) diff --git a/NEWS.md b/NEWS.md index 73164670..31f4e808 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,10 @@ Red Panda C++ Version 1.0.0 - enhancement: highlight hits in the find panel's result list - enhancement: optimize startup time - fix: batch replace in file doesn't respect item check states in the find panel + - enhancement: option for default file encoding in "option" dialog's "editor"->"misc" tab + - enhancement: auto detect "gbk" encoding when running in zh_CN locale under Linux + - enhancement: disable encoding submenu when editor closed + - enhancement: clear infos in the status bar when editor closed Red Panda C++ Version 0.14.5 - fix: the "gnu c++ 20" option in compiler set options is wrong diff --git a/RedPandaIDE/cpprefacter.cpp b/RedPandaIDE/cpprefacter.cpp index 1ba6d5c7..b9d4f5e4 100644 --- a/RedPandaIDE/cpprefacter.cpp +++ b/RedPandaIDE/cpprefacter.cpp @@ -319,7 +319,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement & QByteArray realEncoding; QFile file(filename); editor.lines()->saveToFile(file,ENCODING_AUTO_DETECT, - pSettings->editor().useUTF8ByDefault()? ENCODING_UTF8 : QTextCodec::codecForLocale()->name(), + pSettings->editor().defaultEncoding(), realEncoding); } } diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 12c1c1cc..78d3ec23 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -105,10 +105,7 @@ Editor::Editor(QWidget *parent, const QString& filename, loadFile(); highlighter = highlighterManager.getHighlighter(mFilename); } else { - if (mEncodingOption == ENCODING_AUTO_DETECT) - mFileEncoding = ENCODING_ASCII; - else - mFileEncoding = mEncodingOption; + mFileEncoding = ENCODING_ASCII; highlighter=highlighterManager.getCppHighlighter(); } @@ -226,7 +223,7 @@ void Editor::loadFile(QString filename) { void Editor::saveFile(QString filename) { QFile file(filename); this->lines()->saveToFile(file,mEncodingOption, - pSettings->editor().useUTF8ByDefault()? ENCODING_UTF8 : QTextCodec::codecForLocale()->name(), + pSettings->editor().defaultEncoding(), mFileEncoding); emit fileSaved(filename, mInProject); } @@ -528,6 +525,7 @@ void Editor::focusInEvent(QFocusEvent *event) SynEdit::focusInEvent(event); pMainWindow->updateAppTitle(); pMainWindow->updateEditorActions(); + pMainWindow->updateForEncodingInfo(); pMainWindow->updateStatusbarForLineCol(); pMainWindow->updateForStatusbarModeInfo(); pMainWindow->updateClassBrowserForEditor(this); @@ -537,6 +535,7 @@ void Editor::focusOutEvent(QFocusEvent *event) { SynEdit::focusOutEvent(event); //pMainWindow->updateClassBrowserForEditor(nullptr); + pMainWindow->updateForEncodingInfo(); pMainWindow->updateStatusbarForLineCol(); pMainWindow->updateForStatusbarModeInfo(); pMainWindow->functionTip()->hide(); @@ -1192,6 +1191,9 @@ void Editor::closeEvent(QCloseEvent *) mCompletionPopup->hide(); if (pMainWindow->functionTip()) pMainWindow->functionTip()->hide(); + pMainWindow->updateForEncodingInfo(true); + pMainWindow->updateStatusbarForLineCol(true); + pMainWindow->updateForStatusbarModeInfo(true); } void Editor::showEvent(QShowEvent */*event*/) diff --git a/RedPandaIDE/editorlist.cpp b/RedPandaIDE/editorlist.cpp index 03f4727d..d54ae8a2 100644 --- a/RedPandaIDE/editorlist.cpp +++ b/RedPandaIDE/editorlist.cpp @@ -371,7 +371,7 @@ Editor *EditorList::getEditorByFilename(QString filename) QFileInfo fileInfo(filename); QString fullname = fileInfo.absoluteFilePath(); if (fileInfo.exists() && fileInfo.isFile()) - return newEditor(fullname,ENCODING_AUTO_DETECT,false,false); + return newEditor(fullname,pSettings->editor().autoDetectFileEncoding()?ENCODING_AUTO_DETECT:pSettings->editor().defaultEncoding(),false,false); return nullptr; } diff --git a/RedPandaIDE/main.cpp b/RedPandaIDE/main.cpp index 58a3cd62..154096b8 100644 --- a/RedPandaIDE/main.cpp +++ b/RedPandaIDE/main.cpp @@ -291,10 +291,11 @@ int main(int argc, char *argv[]) tempFile.remove(); return -1; } + QString language; { QSettings languageSetting(settingFilename,QSettings::IniFormat); languageSetting.beginGroup(SETTING_ENVIRONMENT); - QString language = languageSetting.value("language",QLocale::system().name()).toString(); + language = languageSetting.value("language",QLocale::system().name()).toString(); if (trans.load("RedPandaIDE_"+language,":/i18n/")) { app.installTranslator(&trans); @@ -314,7 +315,7 @@ int main(int argc, char *argv[]) SystemConsts systemConsts; pSystemConsts = &systemConsts; - pCharsetInfoManager = new CharsetInfoManager(); + pCharsetInfoManager = new CharsetInfoManager(language); auto charsetInfoManager = std::unique_ptr(pCharsetInfoManager); //load settings pSettings = new Settings(settingFilename); diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 8ce33fd1..77c8136b 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -357,9 +357,9 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::updateForEncodingInfo() { +void MainWindow::updateForEncodingInfo(bool clear) { Editor * editor = mEditorList->getEditor(); - if (editor!=NULL) { + if (!clear && editor!=NULL) { if (editor->encodingOption() != editor->fileEncoding()) { mFileEncodingStatus->setText( QString("%1(%2)") @@ -395,6 +395,7 @@ void MainWindow::updateEditorActions() ui->actionAuto_Detect->setEnabled(false); ui->actionEncode_in_ANSI->setEnabled(false); ui->actionEncode_in_UTF_8->setEnabled(false); + mMenuEncoding->setEnabled(false); ui->actionConvert_to_ANSI->setEnabled(false); ui->actionConvert_to_UTF_8->setEnabled(false); ui->actionCopy->setEnabled(false); @@ -439,6 +440,7 @@ void MainWindow::updateEditorActions() ui->actionAuto_Detect->setEnabled(true); ui->actionEncode_in_ANSI->setEnabled(true); ui->actionEncode_in_UTF_8->setEnabled(true); + mMenuEncoding->setEnabled(true); ui->actionConvert_to_ANSI->setEnabled(e->encodingOption()!=ENCODING_SYSTEM_DEFAULT && e->fileEncoding()!=ENCODING_SYSTEM_DEFAULT); ui->actionConvert_to_UTF_8->setEnabled(e->encodingOption()!=ENCODING_UTF8 && e->fileEncoding()!=ENCODING_UTF8); @@ -1011,10 +1013,10 @@ QMenuBar *MainWindow::menuBar() const return ui->menubar; } -void MainWindow::updateStatusbarForLineCol() +void MainWindow::updateStatusbarForLineCol(bool clear) { Editor* e = mEditorList->getEditor(); - if (e!=nullptr) { + if (!clear && e!=nullptr) { int col = e->charToColumn(e->caretY(),e->caretX()); QString msg = tr("Line:%1 Col:%2 Selected:%3 Lines:%4 Length:%5") .arg(e->caretY()) @@ -1028,10 +1030,10 @@ void MainWindow::updateStatusbarForLineCol() } } -void MainWindow::updateForStatusbarModeInfo() +void MainWindow::updateForStatusbarModeInfo(bool clear) { Editor* e = mEditorList->getEditor(); - if (e!=nullptr) { + if (!clear && e!=nullptr) { QString msg; if (e->readOnly()) { msg = tr("Read Only"); @@ -1087,7 +1089,8 @@ void MainWindow::openFile(const QString &filename, QTabWidget* page) unit = mProject->findUnitByFilename(filename); } bool inProject = (mProject && unit); - QByteArray encoding = unit ? unit->encoding() : ENCODING_AUTO_DETECT; + QByteArray encoding = unit ? unit->encoding() : + (pSettings->editor().autoDetectFileEncoding()? ENCODING_AUTO_DETECT : pSettings->editor().defaultEncoding()); editor = mEditorList->newEditor(filename,encoding, inProject, false, page); // if (mProject) { @@ -2125,7 +2128,8 @@ void MainWindow::loadLastOpens() unit = mProject->findUnitByFilename(editorFilename); } bool inProject = (mProject && unit); - QByteArray encoding = unit ? unit->encoding() : ENCODING_AUTO_DETECT; + QByteArray encoding = unit ? unit->encoding() : + (pSettings->editor().autoDetectFileEncoding()? ENCODING_AUTO_DETECT : pSettings->editor().defaultEncoding()); Editor * editor = mEditorList->newEditor(editorFilename, encoding, inProject,false,page); // if (mProject) { // mProject->associateEditorToUnit(editor,unit); @@ -2213,7 +2217,7 @@ void MainWindow::newEditor() { try { Editor * editor=mEditorList->newEditor("", - pSettings->editor().useUTF8ByDefault()?ENCODING_UTF8:ENCODING_AUTO_DETECT, + pSettings->editor().defaultEncoding(), false,true); editor->activate(); updateForEncodingInfo(); diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index cc52db18..d92d1f1a 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -100,9 +100,9 @@ public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); - void updateForEncodingInfo(); - void updateStatusbarForLineCol(); - void updateForStatusbarModeInfo(); + void updateForEncodingInfo(bool clear=false); + void updateStatusbarForLineCol(bool clear=false); + void updateForStatusbarModeInfo(bool clear=false); void updateStatusbarMessage(const QString& s); void updateEditorSettings(); void updateEditorActions(); diff --git a/RedPandaIDE/platform.cpp b/RedPandaIDE/platform.cpp index 301f3f31..10439f63 100644 --- a/RedPandaIDE/platform.cpp +++ b/RedPandaIDE/platform.cpp @@ -77,6 +77,16 @@ QList CharsetInfoManager::findCharsetsByLanguageName(const QString return result; } +QList CharsetInfoManager::findCharsetByLocale(const QString &localeName) +{ + QList result; + foreach (const PCharsetInfo& info, mCodePages) { + if (info->enabled && info->localeName == localeName) + result.append(info); + } + return result; +} + QString CharsetInfoManager::findLanguageByCharsetName(const QString &encodingName) { @@ -88,168 +98,176 @@ QString CharsetInfoManager::findLanguageByCharsetName(const QString &encodingNam return "Unknown"; } -CharsetInfoManager::CharsetInfoManager():QObject() +const QString &CharsetInfoManager::localeName() const { - mCodePages.append(std::make_shared(37,"IBM037","",false)); - mCodePages.append(std::make_shared(437,"IBM437","",false)); - mCodePages.append(std::make_shared(500,"IBM500","",false)); - mCodePages.append(std::make_shared(708,"ASMO-708","",false)); - mCodePages.append(std::make_shared(709,"","",false)); - mCodePages.append(std::make_shared(710,"","",false)); - mCodePages.append(std::make_shared(720,"DOS-720",tr("Arabic"),false)); - mCodePages.append(std::make_shared(737,"ibm737",tr("Greek"),false)); - mCodePages.append(std::make_shared(775,"ibm775",tr("Baltic"),false)); - mCodePages.append(std::make_shared(850,"ibm850",tr("Western Europe"),false)); - mCodePages.append(std::make_shared(852,"ibm852",tr("Central Europe"),false)); - mCodePages.append(std::make_shared(855,"IBM855",tr("Cyrillic"),false)); - mCodePages.append(std::make_shared(857,"ibm857",tr("Turkish"),false)); - mCodePages.append(std::make_shared(858,"ibm858",tr("Western Europe"),false)); - mCodePages.append(std::make_shared(860,"IBM860",tr("Western Europe"),false)); - mCodePages.append(std::make_shared(861,"ibm861",tr("Northern Europe"),false)); - mCodePages.append(std::make_shared(862,"DOS-862",tr("Hebrew"),false)); - mCodePages.append(std::make_shared(863,"IBM863",tr("Western Europe"),false)); - mCodePages.append(std::make_shared(864,"IBM864","",false)); - mCodePages.append(std::make_shared(865,"IBM865",tr("Northern Europe"),false)); - mCodePages.append(std::make_shared(866,"cp866",tr("Cyrillic"),false)); - mCodePages.append(std::make_shared(869,"ibm869",tr("Greek"),false)); - mCodePages.append(std::make_shared(870,"IBM870","",false)); - mCodePages.append(std::make_shared(874,"tis-620",tr("Thai"),true)); - mCodePages.append(std::make_shared(875,"cp875","",false)); - mCodePages.append(std::make_shared(932,"shift_jis",tr("Japanese"),true)); - mCodePages.append(std::make_shared(936,"gbk",tr("Chinese"),true)); - mCodePages.append(std::make_shared(949,"windows-949",tr("Korean"),true)); - mCodePages.append(std::make_shared(950,"big5",tr("Chinese"),true)); - mCodePages.append(std::make_shared(1026,"IBM1026","",false)); - mCodePages.append(std::make_shared(1047,"IBM01047","",false)); - mCodePages.append(std::make_shared(1140,"IBM01140","",false)); - mCodePages.append(std::make_shared(1141,"IBM01141","",false)); - mCodePages.append(std::make_shared(1142,"IBM01142","",false)); - mCodePages.append(std::make_shared(1143,"IBM01143","",false)); - mCodePages.append(std::make_shared(1144,"IBM01144","",false)); - mCodePages.append(std::make_shared(1145,"IBM01145","",false)); - mCodePages.append(std::make_shared(1146,"IBM01146","",false)); - mCodePages.append(std::make_shared(1147,"IBM01147","",false)); - mCodePages.append(std::make_shared(1148,"IBM01148","",false)); - mCodePages.append(std::make_shared(1149,"IBM01149","",false)); - mCodePages.append(std::make_shared(1200,"utf-16","",false)); - mCodePages.append(std::make_shared(1201,"unicodeFFFE","",false)); - mCodePages.append(std::make_shared(1250,"windows-1250",tr("Central Europe"),true)); - mCodePages.append(std::make_shared(1251,"windows-1251",tr("Cyrillic"),true)); - mCodePages.append(std::make_shared(1252,"windows-1252",tr("Western Europe"),true)); - mCodePages.append(std::make_shared(1253,"windows-1253",tr("Greek"),true)); - mCodePages.append(std::make_shared(1254,"windows-1254",tr("Turkish"),true)); - mCodePages.append(std::make_shared(1255,"windows-1255",tr("Hebrew"),true)); - mCodePages.append(std::make_shared(1256,"windows-1256",tr("Arabic"),true)); - mCodePages.append(std::make_shared(1257,"windows-1257",tr("Baltic"),true)); - mCodePages.append(std::make_shared(1258,"windows-1258",tr("Vietnamese"),true)); - mCodePages.append(std::make_shared(1361,"Johab","",false)); - mCodePages.append(std::make_shared(10000,"macintosh",tr("Cyrillic"),true)); - mCodePages.append(std::make_shared(10001,"x-mac-japanese","",false)); - mCodePages.append(std::make_shared(10002,"x-mac-chinesetrad","",false)); - mCodePages.append(std::make_shared(10003,"x-mac-korean","",false)); - mCodePages.append(std::make_shared(10004,"x-mac-arabic","",false)); - mCodePages.append(std::make_shared(10005,"x-mac-hebrew","",false)); - mCodePages.append(std::make_shared(10006,"x-mac-greek","",false)); - mCodePages.append(std::make_shared(10007,"x-mac-cyrillic","",false)); - mCodePages.append(std::make_shared(10008,"x-mac-chinesesimp","",false)); - mCodePages.append(std::make_shared(10010,"x-mac-romanian","",false)); - mCodePages.append(std::make_shared(10017,"x-mac-ukrainian","",false)); - mCodePages.append(std::make_shared(10021,"x-mac-thai","",false)); - mCodePages.append(std::make_shared(10029,"x-mac-ce","",false)); - mCodePages.append(std::make_shared(10079,"x-mac-icelandic","",false)); - mCodePages.append(std::make_shared(10081,"x-mac-turkish","",false)); - mCodePages.append(std::make_shared(10082,"x-mac-croatian","",false)); - mCodePages.append(std::make_shared(12000,"utf-32","",false)); - mCodePages.append(std::make_shared(12001,"utf-32BE","",false)); - mCodePages.append(std::make_shared(20000,"x-Chinese_CNS","",false)); - mCodePages.append(std::make_shared(20001,"x-cp20001","",false)); - mCodePages.append(std::make_shared(20002,"x_Chinese-Eten","",false)); - mCodePages.append(std::make_shared(20003,"x-cp20003","",false)); - mCodePages.append(std::make_shared(20004,"x-cp20004","",false)); - mCodePages.append(std::make_shared(20005,"x-cp20005","",false)); - mCodePages.append(std::make_shared(20105,"x-IA5","",false)); - mCodePages.append(std::make_shared(20106,"x-IA5-German","",false)); - mCodePages.append(std::make_shared(20107,"x-IA5-Swedish","",false)); - mCodePages.append(std::make_shared(20108,"x-IA5-Norwegian","",false)); - mCodePages.append(std::make_shared(20127,"us-ascii","",false)); - mCodePages.append(std::make_shared(20261,"x-cp20261","",false)); - mCodePages.append(std::make_shared(20269,"x-cp20269","",false)); - mCodePages.append(std::make_shared(20273,"IBM273","",false)); - mCodePages.append(std::make_shared(20277,"IBM277","",false)); - mCodePages.append(std::make_shared(20278,"IBM278","",false)); - mCodePages.append(std::make_shared(20280,"IBM280","",false)); - mCodePages.append(std::make_shared(20284,"IBM284","",false)); - mCodePages.append(std::make_shared(20285,"IBM285","",false)); - mCodePages.append(std::make_shared(20290,"IBM290","",false)); - mCodePages.append(std::make_shared(20297,"IBM297","",false)); - mCodePages.append(std::make_shared(20420,"IBM420","",false)); - mCodePages.append(std::make_shared(20423,"IBM423","",false)); - mCodePages.append(std::make_shared(20424,"IBM424","",false)); - mCodePages.append(std::make_shared(20833,"x-EBCDIC-KoreanExtended","",false)); - mCodePages.append(std::make_shared(20838,"IBM-Thai","",false)); - mCodePages.append(std::make_shared(20866,"koi8-r",tr("Cyrillic"),true)); - mCodePages.append(std::make_shared(20871,"IBM871","",false)); - mCodePages.append(std::make_shared(20880,"IBM880","",false)); - mCodePages.append(std::make_shared(20905,"IBM905","",false)); - mCodePages.append(std::make_shared(20924,"IBM00924","",false)); - mCodePages.append(std::make_shared(20932,"EUC-JP","",false)); - mCodePages.append(std::make_shared(20936,"x-cp20936","",false)); - mCodePages.append(std::make_shared(20949,"x-cp20949","",false)); - mCodePages.append(std::make_shared(21025,"cp1025","",false)); - mCodePages.append(std::make_shared(21027,"","",false)); - mCodePages.append(std::make_shared(21866,"koi8-u",tr("Cyrillic"),true)); - mCodePages.append(std::make_shared(28591,"iso-8859-1",tr("Western Europe"),true)); - mCodePages.append(std::make_shared(28592,"iso-8859-2",tr("Eastern Europe"),true)); - mCodePages.append(std::make_shared(28593,"iso-8859-3",tr("Turkish"),true)); - mCodePages.append(std::make_shared(28594,"iso-8859-4",tr("Baltic"),true)); - mCodePages.append(std::make_shared(28595,"iso-8859-5",tr("Cyrillic"),true)); - mCodePages.append(std::make_shared(28596,"iso-8859-6",tr("Arabic"),true)); - mCodePages.append(std::make_shared(28597,"iso-8859-7",tr("Greek"),true)); - mCodePages.append(std::make_shared(28598,"iso-8859-8",tr("Hebrew"),true)); - mCodePages.append(std::make_shared(28599,"iso-8859-9",tr("Turkish"),true)); - mCodePages.append(std::make_shared(28603,"iso-8859-13",tr("Baltic"),true)); - mCodePages.append(std::make_shared(-1,"iso-8859-14",tr("Celtic"),true)); - mCodePages.append(std::make_shared(28605,"iso-8859-15",tr("Western Europe"),true)); - mCodePages.append(std::make_shared(29001,"x-Europa","",false)); - mCodePages.append(std::make_shared(38598,"iso-8859-8-i","",false)); - mCodePages.append(std::make_shared(50220,"iso-2022-jp","",false)); - mCodePages.append(std::make_shared(50221,"csISO2022JP","",false)); - mCodePages.append(std::make_shared(50222,"iso-2022-jp","",false)); - mCodePages.append(std::make_shared(50225,"iso-2022-kr","",false)); - mCodePages.append(std::make_shared(50227,"x-cp50227","",false)); - mCodePages.append(std::make_shared(50229,"","",false)); - mCodePages.append(std::make_shared(50930,"","",false)); - mCodePages.append(std::make_shared(50931,"","",false)); - mCodePages.append(std::make_shared(50933,"","",false)); - mCodePages.append(std::make_shared(50935,"","",false)); - mCodePages.append(std::make_shared(50936,"","",false)); - mCodePages.append(std::make_shared(50937,"","",false)); - mCodePages.append(std::make_shared(50939,"","",false)); - mCodePages.append(std::make_shared(51932,"euc-jp",tr("Japanese"),true)); - mCodePages.append(std::make_shared(51936,"euc-cn","",false)); - mCodePages.append(std::make_shared(51949,"euc-kr",tr("Korean"),true)); - mCodePages.append(std::make_shared(51950,"","",false)); - mCodePages.append(std::make_shared(52936,"hz-gb-2312","",false)); - mCodePages.append(std::make_shared(54936,"gb18030",tr("Chinese"),true)); - mCodePages.append(std::make_shared(57002,"x-iscii-de","",false)); - mCodePages.append(std::make_shared(57003,"x-iscii-be","",false)); - mCodePages.append(std::make_shared(57004,"x-iscii-ta","",false)); - mCodePages.append(std::make_shared(57005,"x-iscii-te","",false)); - mCodePages.append(std::make_shared(57006,"x-iscii-as","",false)); - mCodePages.append(std::make_shared(57007,"x-iscii-or","",false)); - mCodePages.append(std::make_shared(57008,"x-iscii-ka","",false)); - mCodePages.append(std::make_shared(57009,"x-iscii-ma","",false)); - mCodePages.append(std::make_shared(57010,"x-iscii-gu","",false)); - mCodePages.append(std::make_shared(57011,"x-iscii-pa","",false)); - mCodePages.append(std::make_shared(65000,"utf-7","",false)); - mCodePages.append(std::make_shared(65001,"utf-8","",false)); + return mLocaleName; +} + +CharsetInfoManager::CharsetInfoManager(const QString& localeName): + QObject(), + mLocaleName(localeName) +{ + mCodePages.append(std::make_shared(37,"IBM037","","",false)); + mCodePages.append(std::make_shared(437,"IBM437","","",false)); + mCodePages.append(std::make_shared(500,"IBM500","","",false)); + mCodePages.append(std::make_shared(708,"ASMO-708","","",false)); + mCodePages.append(std::make_shared(709,"","","",false)); + mCodePages.append(std::make_shared(710,"","","",false)); + mCodePages.append(std::make_shared(720,"DOS-720",tr("Arabic"),"",false)); + mCodePages.append(std::make_shared(737,"ibm737",tr("Greek"),"",false)); + mCodePages.append(std::make_shared(775,"ibm775",tr("Baltic"),"",false)); + mCodePages.append(std::make_shared(850,"ibm850",tr("Western Europe"),"",false)); + mCodePages.append(std::make_shared(852,"ibm852",tr("Central Europe"),"",false)); + mCodePages.append(std::make_shared(855,"IBM855",tr("Cyrillic"),"",false)); + mCodePages.append(std::make_shared(857,"ibm857",tr("Turkish"),"",false)); + mCodePages.append(std::make_shared(858,"ibm858",tr("Western Europe"),"",false)); + mCodePages.append(std::make_shared(860,"IBM860",tr("Western Europe"),"",false)); + mCodePages.append(std::make_shared(861,"ibm861",tr("Northern Europe"),"",false)); + mCodePages.append(std::make_shared(862,"DOS-862",tr("Hebrew"),"",false)); + mCodePages.append(std::make_shared(863,"IBM863",tr("Western Europe"),"",false)); + mCodePages.append(std::make_shared(864,"IBM864","","",false)); + mCodePages.append(std::make_shared(865,"IBM865",tr("Northern Europe"),"",false)); + mCodePages.append(std::make_shared(866,"cp866",tr("Cyrillic"),"",false)); + mCodePages.append(std::make_shared(869,"ibm869",tr("Greek"),"",false)); + mCodePages.append(std::make_shared(870,"IBM870","","",false)); + mCodePages.append(std::make_shared(874,"tis-620",tr("Thai"),"",true)); + mCodePages.append(std::make_shared(875,"cp875","","",false)); + mCodePages.append(std::make_shared(932,"shift_jis",tr("Japanese"),"",true)); + mCodePages.append(std::make_shared(936,"gbk",tr("Chinese"),"zh_CN",true)); + mCodePages.append(std::make_shared(949,"windows-949",tr("Korean"),"",true)); + mCodePages.append(std::make_shared(950,"big5",tr("Chinese"),"",true)); + mCodePages.append(std::make_shared(1026,"IBM1026","","",false)); + mCodePages.append(std::make_shared(1047,"IBM01047","","",false)); + mCodePages.append(std::make_shared(1140,"IBM01140","","",false)); + mCodePages.append(std::make_shared(1141,"IBM01141","","",false)); + mCodePages.append(std::make_shared(1142,"IBM01142","","",false)); + mCodePages.append(std::make_shared(1143,"IBM01143","","",false)); + mCodePages.append(std::make_shared(1144,"IBM01144","","",false)); + mCodePages.append(std::make_shared(1145,"IBM01145","","",false)); + mCodePages.append(std::make_shared(1146,"IBM01146","","",false)); + mCodePages.append(std::make_shared(1147,"IBM01147","","",false)); + mCodePages.append(std::make_shared(1148,"IBM01148","","",false)); + mCodePages.append(std::make_shared(1149,"IBM01149","","",false)); + mCodePages.append(std::make_shared(1200,"utf-16","","",false)); + mCodePages.append(std::make_shared(1201,"unicodeFFFE","","",false)); + mCodePages.append(std::make_shared(1250,"windows-1250",tr("Central Europe"),"",true)); + mCodePages.append(std::make_shared(1251,"windows-1251",tr("Cyrillic"),"",true)); + mCodePages.append(std::make_shared(1252,"windows-1252",tr("Western Europe"),"",true)); + mCodePages.append(std::make_shared(1253,"windows-1253",tr("Greek"),"",true)); + mCodePages.append(std::make_shared(1254,"windows-1254",tr("Turkish"),"",true)); + mCodePages.append(std::make_shared(1255,"windows-1255",tr("Hebrew"),"",true)); + mCodePages.append(std::make_shared(1256,"windows-1256",tr("Arabic"),"",true)); + mCodePages.append(std::make_shared(1257,"windows-1257",tr("Baltic"),"",true)); + mCodePages.append(std::make_shared(1258,"windows-1258",tr("Vietnamese"),"",true)); + mCodePages.append(std::make_shared(1361,"Johab","","",false)); + mCodePages.append(std::make_shared(10000,"macintosh",tr("Cyrillic"),"",true)); + mCodePages.append(std::make_shared(10001,"x-mac-japanese","","",false)); + mCodePages.append(std::make_shared(10002,"x-mac-chinesetrad","","",false)); + mCodePages.append(std::make_shared(10003,"x-mac-korean","","",false)); + mCodePages.append(std::make_shared(10004,"x-mac-arabic","","",false)); + mCodePages.append(std::make_shared(10005,"x-mac-hebrew","","",false)); + mCodePages.append(std::make_shared(10006,"x-mac-greek","","",false)); + mCodePages.append(std::make_shared(10007,"x-mac-cyrillic","","",false)); + mCodePages.append(std::make_shared(10008,"x-mac-chinesesimp","","",false)); + mCodePages.append(std::make_shared(10010,"x-mac-romanian","","",false)); + mCodePages.append(std::make_shared(10017,"x-mac-ukrainian","","",false)); + mCodePages.append(std::make_shared(10021,"x-mac-thai","","",false)); + mCodePages.append(std::make_shared(10029,"x-mac-ce","","",false)); + mCodePages.append(std::make_shared(10079,"x-mac-icelandic","","",false)); + mCodePages.append(std::make_shared(10081,"x-mac-turkish","","",false)); + mCodePages.append(std::make_shared(10082,"x-mac-croatian","","",false)); + mCodePages.append(std::make_shared(12000,"utf-32","","",false)); + mCodePages.append(std::make_shared(12001,"utf-32BE","","",false)); + mCodePages.append(std::make_shared(20000,"x-Chinese_CNS","","",false)); + mCodePages.append(std::make_shared(20001,"x-cp20001","","",false)); + mCodePages.append(std::make_shared(20002,"x_Chinese-Eten","","",false)); + mCodePages.append(std::make_shared(20003,"x-cp20003","","",false)); + mCodePages.append(std::make_shared(20004,"x-cp20004","","",false)); + mCodePages.append(std::make_shared(20005,"x-cp20005","","",false)); + mCodePages.append(std::make_shared(20105,"x-IA5","","",false)); + mCodePages.append(std::make_shared(20106,"x-IA5-German","","",false)); + mCodePages.append(std::make_shared(20107,"x-IA5-Swedish","","",false)); + mCodePages.append(std::make_shared(20108,"x-IA5-Norwegian","","",false)); + mCodePages.append(std::make_shared(20127,"us-ascii","","",false)); + mCodePages.append(std::make_shared(20261,"x-cp20261","","",false)); + mCodePages.append(std::make_shared(20269,"x-cp20269","","",false)); + mCodePages.append(std::make_shared(20273,"IBM273","","",false)); + mCodePages.append(std::make_shared(20277,"IBM277","","",false)); + mCodePages.append(std::make_shared(20278,"IBM278","","",false)); + mCodePages.append(std::make_shared(20280,"IBM280","","",false)); + mCodePages.append(std::make_shared(20284,"IBM284","","",false)); + mCodePages.append(std::make_shared(20285,"IBM285","","",false)); + mCodePages.append(std::make_shared(20290,"IBM290","","",false)); + mCodePages.append(std::make_shared(20297,"IBM297","","",false)); + mCodePages.append(std::make_shared(20420,"IBM420","","",false)); + mCodePages.append(std::make_shared(20423,"IBM423","","",false)); + mCodePages.append(std::make_shared(20424,"IBM424","","",false)); + mCodePages.append(std::make_shared(20833,"x-EBCDIC-KoreanExtended","","",false)); + mCodePages.append(std::make_shared(20838,"IBM-Thai","","",false)); + mCodePages.append(std::make_shared(20866,"koi8-r",tr("Cyrillic"),"",true)); + mCodePages.append(std::make_shared(20871,"IBM871","","",false)); + mCodePages.append(std::make_shared(20880,"IBM880","","",false)); + mCodePages.append(std::make_shared(20905,"IBM905","","",false)); + mCodePages.append(std::make_shared(20924,"IBM00924","","",false)); + mCodePages.append(std::make_shared(20932,"EUC-JP","","",false)); + mCodePages.append(std::make_shared(20936,"x-cp20936","","",false)); + mCodePages.append(std::make_shared(20949,"x-cp20949","","",false)); + mCodePages.append(std::make_shared(21025,"cp1025","","",false)); + mCodePages.append(std::make_shared(21027,"","","",false)); + mCodePages.append(std::make_shared(21866,"koi8-u",tr("Cyrillic"),"",true)); + mCodePages.append(std::make_shared(28591,"iso-8859-1",tr("Western Europe"),"",true)); + mCodePages.append(std::make_shared(28592,"iso-8859-2",tr("Eastern Europe"),"",true)); + mCodePages.append(std::make_shared(28593,"iso-8859-3",tr("Turkish"),"",true)); + mCodePages.append(std::make_shared(28594,"iso-8859-4",tr("Baltic"),"",true)); + mCodePages.append(std::make_shared(28595,"iso-8859-5",tr("Cyrillic"),"",true)); + mCodePages.append(std::make_shared(28596,"iso-8859-6",tr("Arabic"),"",true)); + mCodePages.append(std::make_shared(28597,"iso-8859-7",tr("Greek"),"",true)); + mCodePages.append(std::make_shared(28598,"iso-8859-8",tr("Hebrew"),"",true)); + mCodePages.append(std::make_shared(28599,"iso-8859-9",tr("Turkish"),"",true)); + mCodePages.append(std::make_shared(28603,"iso-8859-13",tr("Baltic"),"",true)); + mCodePages.append(std::make_shared(-1,"iso-8859-14",tr("Celtic"),"",true)); + mCodePages.append(std::make_shared(28605,"iso-8859-15",tr("Western Europe"),"",true)); + mCodePages.append(std::make_shared(29001,"x-Europa","","",false)); + mCodePages.append(std::make_shared(38598,"iso-8859-8-i","","",false)); + mCodePages.append(std::make_shared(50220,"iso-2022-jp","","",false)); + mCodePages.append(std::make_shared(50221,"csISO2022JP","","",false)); + mCodePages.append(std::make_shared(50222,"iso-2022-jp","","",false)); + mCodePages.append(std::make_shared(50225,"iso-2022-kr","","",false)); + mCodePages.append(std::make_shared(50227,"x-cp50227","","",false)); + mCodePages.append(std::make_shared(50229,"","","",false)); + mCodePages.append(std::make_shared(50930,"","","",false)); + mCodePages.append(std::make_shared(50931,"","","",false)); + mCodePages.append(std::make_shared(50933,"","","",false)); + mCodePages.append(std::make_shared(50935,"","","",false)); + mCodePages.append(std::make_shared(50936,"","","",false)); + mCodePages.append(std::make_shared(50937,"","","",false)); + mCodePages.append(std::make_shared(50939,"","","",false)); + mCodePages.append(std::make_shared(51932,"euc-jp",tr("Japanese"),"",true)); + mCodePages.append(std::make_shared(51936,"euc-cn","","",false)); + mCodePages.append(std::make_shared(51949,"euc-kr",tr("Korean"),"",true)); + mCodePages.append(std::make_shared(51950,"","","",false)); + mCodePages.append(std::make_shared(52936,"hz-gb-2312","","",false)); + mCodePages.append(std::make_shared(54936,"gb18030",tr("Chinese"),"zh_CN",true)); + mCodePages.append(std::make_shared(57002,"x-iscii-de","","",false)); + mCodePages.append(std::make_shared(57003,"x-iscii-be","","",false)); + mCodePages.append(std::make_shared(57004,"x-iscii-ta","","",false)); + mCodePages.append(std::make_shared(57005,"x-iscii-te","","",false)); + mCodePages.append(std::make_shared(57006,"x-iscii-as","","",false)); + mCodePages.append(std::make_shared(57007,"x-iscii-or","","",false)); + mCodePages.append(std::make_shared(57008,"x-iscii-ka","","",false)); + mCodePages.append(std::make_shared(57009,"x-iscii-ma","","",false)); + mCodePages.append(std::make_shared(57010,"x-iscii-gu","","",false)); + mCodePages.append(std::make_shared(57011,"x-iscii-pa","","",false)); + mCodePages.append(std::make_shared(65000,"utf-7","","",false)); + mCodePages.append(std::make_shared(65001,"utf-8","","",false)); } -CharsetInfo::CharsetInfo(int codepage, const QByteArray &name, const QString &language, bool enabled) +CharsetInfo::CharsetInfo(int codepage, const QByteArray &name, const QString &language,const QString& localeName, bool enabled) { this->codepage = codepage; this->name = name; this->language = language; + this->localeName = localeName; this->enabled = enabled; } diff --git a/RedPandaIDE/platform.h b/RedPandaIDE/platform.h index cacdacd9..63aa2a7a 100644 --- a/RedPandaIDE/platform.h +++ b/RedPandaIDE/platform.h @@ -25,10 +25,12 @@ struct CharsetInfo{ int codepage; QByteArray name; QString language; + QString localeName; bool enabled; explicit CharsetInfo(int codepage, const QByteArray& name, const QString& language, + const QString& locale, bool enabled); }; @@ -37,14 +39,18 @@ using PCharsetInfo = std::shared_ptr; class CharsetInfoManager: public QObject { Q_OBJECT public: - explicit CharsetInfoManager(); + explicit CharsetInfoManager(const QString& localeName); QByteArray getDefaultSystemEncoding(); PCharsetInfo findCharsetByCodepage(int codepage); QStringList languageNames(); QList findCharsetsByLanguageName(const QString& languageName); + QList findCharsetByLocale(const QString& localeName); QString findLanguageByCharsetName(const QString& encodingName); + const QString &localeName() const; + private: QList mCodePages; + QString mLocaleName; }; using PCharsetInfoManager = std::shared_ptr; diff --git a/RedPandaIDE/qsynedit/TextBuffer.cpp b/RedPandaIDE/qsynedit/TextBuffer.cpp index 769a89eb..2d3713dd 100644 --- a/RedPandaIDE/qsynedit/TextBuffer.cpp +++ b/RedPandaIDE/qsynedit/TextBuffer.cpp @@ -517,6 +517,34 @@ void SynEditStringList::insertText(int Index, const QString &NewText) insertStrings(Index,lines); } +bool SynEditStringList::tryLoadFileByEncoding(QByteArray encodingName, QFile& file) { + QTextCodec* codec = QTextCodec::codecForName(encodingName); + if (!codec) + return false; + file.reset(); + internalClear(); + QTextCodec::ConverterState state; + while (true) { + if (file.atEnd()){ + break; + } + QByteArray line = file.readLine(); + if (line.endsWith("\r\n")) { + line.remove(line.length()-2,2); + } else if (line.endsWith("\r")) { + line.remove(line.length()-1,1); + } else if (line.endsWith("\n")){ + line.remove(line.length()-1,1); + } + QString newLine = codec->toUnicode(line.constData(),line.length(),&state); + if (state.invalidChars>0) { + return false; + break; + } + addItem(newLine); + } + return true; +} void SynEditStringList::loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding) { QMutexLocker locker(&mMutex); @@ -587,7 +615,30 @@ void SynEditStringList::loadFromFile(const QString& filename, const QByteArray& realEncoding = ENCODING_ASCII; return; } - realEncoding = ENCODING_SYSTEM_DEFAULT; + realEncoding = pCharsetInfoManager->getDefaultSystemEncoding(); + QList charsets = pCharsetInfoManager->findCharsetByLocale(pCharsetInfoManager->localeName()); + if (!charsets.isEmpty()) { + if (tryLoadFileByEncoding(realEncoding,file)) { + emit inserted(0,mList.count()); + return; + } + + QSet encodingSet; + for (int i=0;iname); + } + encodingSet.remove(realEncoding); + foreach (const QByteArray& encodingName,encodingSet) { + if (encodingName == ENCODING_UTF8) + continue; + if (tryLoadFileByEncoding(encodingName,file)) { + qDebug()<execCharset() == ENCODING_AUTO_DETECT || pSet->execCharset() == ENCODING_SYSTEM_DEFAULT || pSet->execCharset() == ENCODING_UTF8) { - ui->cbEncoding->setCurrentText(pSet->execCharset()); + int index =ui->cbEncoding->findData(pSet->execCharset()); + ui->cbEncoding->setCurrentIndex(index); ui->cbEncodingDetails->clear(); ui->cbEncodingDetails->setVisible(false); } else { diff --git a/RedPandaIDE/settingsdialog/editormiscwidget.cpp b/RedPandaIDE/settingsdialog/editormiscwidget.cpp index 329868a1..c013056b 100644 --- a/RedPandaIDE/settingsdialog/editormiscwidget.cpp +++ b/RedPandaIDE/settingsdialog/editormiscwidget.cpp @@ -17,6 +17,7 @@ #include "editormiscwidget.h" #include "ui_editormiscwidget.h" #include "../settings.h" +#include "../platform.h" EditorMiscWidget::EditorMiscWidget(const QString& name, const QString& group, QWidget *parent) : @@ -40,7 +41,27 @@ void EditorMiscWidget::doLoad() } else { ui->rbCFile->setChecked(true); } - ui->chkUseUTF8ByDefault->setChecked(pSettings->editor().useUTF8ByDefault()); + ui->chkAutoDetectFileEncoding->setChecked(pSettings->editor().autoDetectFileEncoding()); + + QByteArray defaultEncoding = pSettings->editor().defaultEncoding(); + if (defaultEncoding == ENCODING_AUTO_DETECT + || defaultEncoding == ENCODING_SYSTEM_DEFAULT + || defaultEncoding == ENCODING_UTF8) { + int index =ui->cbEncoding->findData(defaultEncoding); + ui->cbEncoding->setCurrentIndex(index); + ui->cbEncodingDetail->clear(); + ui->cbEncodingDetail->setVisible(false); + } else { + QString language = pCharsetInfoManager->findLanguageByCharsetName(defaultEncoding); + ui->cbEncoding->setCurrentText(language); + ui->cbEncodingDetail->setVisible(true); + ui->cbEncodingDetail->clear(); + QList infos = pCharsetInfoManager->findCharsetsByLanguageName(language); + foreach (const PCharsetInfo& info, infos) { + ui->cbEncodingDetail->addItem(info->name); + } + ui->cbEncodingDetail->setCurrentText(defaultEncoding); + } } void EditorMiscWidget::doSave() @@ -48,6 +69,43 @@ void EditorMiscWidget::doSave() pSettings->editor().setReadOnlySytemHeader(ui->chkReadonlySystemHeaders->isChecked()); pSettings->editor().setAutoLoadLastFiles(ui->chkLoadLastFiles->isChecked()); pSettings->editor().setDefaultFileCpp(ui->rbCppFile->isChecked()); - pSettings->editor().setUseUTF8ByDefault(ui->chkUseUTF8ByDefault->isChecked()); + pSettings->editor().setAutoDetectFileEncoding(ui->chkAutoDetectFileEncoding->isChecked()); + + if (ui->cbEncodingDetail->isVisible()) { + pSettings->editor().setDefaultEncoding(ui->cbEncodingDetail->currentData().toByteArray()); + } else { + pSettings->editor().setDefaultEncoding(ui->cbEncoding->currentData().toByteArray()); + } pSettings->editor().save(); } + +void EditorMiscWidget::init() +{ + ui->cbEncodingDetail->setVisible(false); + ui->cbEncoding->clear(); + ui->cbEncoding->addItem(tr("ANSI"),ENCODING_SYSTEM_DEFAULT); + ui->cbEncoding->addItem(tr("UTF-8"),ENCODING_UTF8); + foreach (const QString& langName, pCharsetInfoManager->languageNames()) { + ui->cbEncoding->addItem(langName,langName); + } + SettingsWidget::init(); +} + +void EditorMiscWidget::on_cbEncoding_currentTextChanged(const QString &/*arg1*/) +{ + QString userData = ui->cbEncoding->currentData().toString(); + if (userData == ENCODING_AUTO_DETECT + || userData == ENCODING_SYSTEM_DEFAULT + || userData == ENCODING_UTF8) { + ui->cbEncodingDetail->setVisible(false); + ui->cbEncodingDetail->clear(); + } else { + ui->cbEncodingDetail->setVisible(true); + ui->cbEncodingDetail->clear(); + QList infos = pCharsetInfoManager->findCharsetsByLanguageName(userData); + foreach (const PCharsetInfo& info, infos) { + ui->cbEncodingDetail->addItem(info->name); + } + } +} + diff --git a/RedPandaIDE/settingsdialog/editormiscwidget.h b/RedPandaIDE/settingsdialog/editormiscwidget.h index 2f851f73..ba4e7e82 100644 --- a/RedPandaIDE/settingsdialog/editormiscwidget.h +++ b/RedPandaIDE/settingsdialog/editormiscwidget.h @@ -39,6 +39,12 @@ private: protected: void doLoad() override; void doSave() override; + + // SettingsWidget interface +public: + void init() override; +private slots: + void on_cbEncoding_currentTextChanged(const QString &arg1); }; #endif // EDITORMISCWIDGET_H diff --git a/RedPandaIDE/settingsdialog/editormiscwidget.ui b/RedPandaIDE/settingsdialog/editormiscwidget.ui index b10e27bd..b36420f2 100644 --- a/RedPandaIDE/settingsdialog/editormiscwidget.ui +++ b/RedPandaIDE/settingsdialog/editormiscwidget.ui @@ -29,12 +29,40 @@ - + - Use UTF-8 as the default encoding for new file + Auto detect encoding when openning files + + + + Default file encoding + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + diff --git a/RedPandaIDE/settingsdialog/projectfileswidget.cpp b/RedPandaIDE/settingsdialog/projectfileswidget.cpp index d160bbc4..2eca269f 100644 --- a/RedPandaIDE/settingsdialog/projectfileswidget.cpp +++ b/RedPandaIDE/settingsdialog/projectfileswidget.cpp @@ -112,7 +112,8 @@ void ProjectFilesWidget::loadUnitEncoding(PProjectUnit unit) if (unit->encoding() == ENCODING_AUTO_DETECT || unit->encoding() == ENCODING_SYSTEM_DEFAULT || unit->encoding() == ENCODING_UTF8) { - ui->cbEncoding->setCurrentText(unit->encoding()); + int index =ui->cbEncoding->findData(unit->encoding()); + ui->cbEncoding->setCurrentIndex(index); ui->cbEncodingDetail->clear(); ui->cbEncodingDetail->setVisible(false); } else {