- enhancement: autolink add "force utf8" property (mainly for raylib)

This commit is contained in:
Roy Qu 2022-06-23 19:07:48 +08:00
parent 895a927ee0
commit b2585f567a
19 changed files with 118 additions and 46 deletions

View File

@ -5,6 +5,7 @@ Red Panda C++ Version 1.1.2
- fix: can't correctly compile when link params are seperated by line breaks - fix: can't correctly compile when link params are seperated by line breaks
- fix: select all shouldn't set file's modified flag - fix: select all shouldn't set file's modified flag
- enhancement: add (return)type info for functions/varaibles/typedefs in the class browser panel - enhancement: add (return)type info for functions/varaibles/typedefs in the class browser panel
- enhancement: autolink add "force utf8" property (mainly for raylib)
Red Panda C++ Version 1.1.1 Red Panda C++ Version 1.1.1
- enhancement: adjust the appearance of problem case's input/output/expected control - enhancement: adjust the appearance of problem case's input/output/expected control

View File

@ -107,15 +107,17 @@ void AutolinkManager::save()
} }
} }
void AutolinkManager::setLink(const QString &header, const QString &linkOption) void AutolinkManager::setLink(const QString &header, const QString &linkOption, bool execUseUTF8)
{ {
PAutolink link = mLinks.value(header,PAutolink()); PAutolink link = mLinks.value(header,PAutolink());
if (link) { if (link) {
link->linkOption = linkOption; link->linkOption = linkOption;
link->execUseUTF8 = execUseUTF8;
} else { } else {
link = std::make_shared<Autolink>(); link = std::make_shared<Autolink>();
link->header = header; link->header = header;
link->linkOption = linkOption; link->linkOption = linkOption;
link->execUseUTF8 = execUseUTF8;
mLinks.insert(header,link); mLinks.insert(header,link);
} }
} }
@ -137,6 +139,7 @@ QJsonArray AutolinkManager::toJson()
QJsonObject autolink; QJsonObject autolink;
autolink["header"]=header; autolink["header"]=header;
autolink["links"]=mLinks[header]->linkOption; autolink["links"]=mLinks[header]->linkOption;
autolink["execUseUTF8"]=mLinks[header]->execUseUTF8;
result.append(autolink); result.append(autolink);
} }
return result; return result;
@ -147,6 +150,6 @@ void AutolinkManager::fromJson(QJsonArray json)
clear(); clear();
for (int i=0;i<json.size();i++) { for (int i=0;i<json.size();i++) {
QJsonObject obj = json[i].toObject(); QJsonObject obj = json[i].toObject();
setLink(obj["header"].toString(),obj["links"].toString()); setLink(obj["header"].toString(),obj["links"].toString(),obj["execUseUTF8"].toBool());
} }
} }

View File

@ -26,6 +26,7 @@
struct Autolink { struct Autolink {
QString header; QString header;
QString linkOption; QString linkOption;
bool execUseUTF8;
}; };
using PAutolink = std::shared_ptr<Autolink>; using PAutolink = std::shared_ptr<Autolink>;
@ -37,7 +38,8 @@ public:
void load(); void load();
void save(); void save();
void setLink(const QString& header, void setLink(const QString& header,
const QString& linkOption); const QString& linkOption,
bool execUseUTF8);
void removeLink(const QString& header); void removeLink(const QString& header);
const QMap<QString,PAutolink>& links() const; const QMap<QString,PAutolink>& links() const;
void clear(); void clear();

View File

@ -301,10 +301,38 @@ void Compiler::stopCompile()
mStop = true; mStop = true;
} }
QString Compiler::getCharsetArgument(const QByteArray& encoding, bool checkSyntax) QString Compiler::getCharsetArgument(const QByteArray& encoding,FileType fileType, bool checkSyntax)
{ {
QString result; QString result;
if (compilerSet()->autoAddCharsetParams() && encoding != ENCODING_ASCII bool forceExecUTF8=false;
// test if force utf8 from autolink infos
if ((fileType == FileType::CSource ||
fileType == FileType::CppSource) && pSettings->editor().enableAutolink() ){
Editor* editor = pMainWindow->editorList()->getEditor();
if (editor) {
PCppParser parser = editor->parser();
if (parser) {
int waitCount = 0;
//wait parsing ends, at most 1 second
while(parser->parsing()) {
if (waitCount>10)
break;
waitCount++;
QThread::msleep(100);
QApplication *app=dynamic_cast<QApplication*>(
QApplication::instance());
app->processEvents();
}
QSet<QString> parsedFiles;
forceExecUTF8 = parseForceUTF8ForAutolink(
editor->filename(),
parsedFiles,
parser);
}
}
}
if ((forceExecUTF8 || compilerSet()->autoAddCharsetParams()) && encoding != ENCODING_ASCII
&& compilerSet()->compilerType()!=COMPILER_CLANG) { && compilerSet()->compilerType()!=COMPILER_CLANG) {
QString encodingName; QString encodingName;
QString execEncodingName; QString execEncodingName;
@ -317,7 +345,9 @@ QString Compiler::getCharsetArgument(const QByteArray& encoding, bool checkSynta
} else { } else {
encodingName = encoding; encodingName = encoding;
} }
if (compilerSetExecCharset == ENCODING_SYSTEM_DEFAULT || compilerSetExecCharset.isEmpty()) { if (forceExecUTF8) {
execEncodingName = "UTF-8";
} else if (compilerSetExecCharset == ENCODING_SYSTEM_DEFAULT || compilerSetExecCharset.isEmpty()) {
execEncodingName = systemEncodingName; execEncodingName = systemEncodingName;
} else { } else {
execEncodingName = compilerSetExecCharset; execEncodingName = compilerSetExecCharset;
@ -576,6 +606,35 @@ QString Compiler::parseFileIncludesForAutolink(
return result; return result;
} }
bool Compiler::parseForceUTF8ForAutolink(const QString &filename, QSet<QString> &parsedFiles, PCppParser &parser)
{
bool result;
if (parsedFiles.contains(filename))
return false;
parsedFiles.insert(filename);
PAutolink autolink = pAutolinkManager->getLink(filename);
if (autolink && autolink->execUseUTF8) {
return true;
}
QStringList includedFiles = parser->getFileDirectIncludes(filename);
// log(QString("File %1 included:").arg(filename));
// for (int i=includedFiles.size()-1;i>=0;i--) {
// QString includeFilename = includedFiles[i];
// log(includeFilename);
// }
for (int i=includedFiles.size()-1;i>=0;i--) {
QString includeFilename = includedFiles[i];
result = parseForceUTF8ForAutolink(
includeFilename,
parsedFiles,
parser);
if (result)
return true;
}
return false;
}
void Compiler::runCommand(const QString &cmd, const QString &arguments, const QString &workingDir, const QByteArray& inputText) void Compiler::runCommand(const QString &cmd, const QString &arguments, const QString &workingDir, const QByteArray& inputText)
{ {
QProcess process; QProcess process;

View File

@ -64,7 +64,7 @@ protected:
virtual bool prepareForCompile() = 0; virtual bool prepareForCompile() = 0;
virtual QByteArray pipedText(); virtual QByteArray pipedText();
virtual bool prepareForRebuild() = 0; virtual bool prepareForRebuild() = 0;
virtual QString getCharsetArgument(const QByteArray& encoding,bool onlyCheckSyntax); virtual QString getCharsetArgument(const QByteArray& encoding, FileType fileType, bool onlyCheckSyntax);
virtual QString getCCompileArguments(bool checkSyntax); virtual QString getCCompileArguments(bool checkSyntax);
virtual QString getCppCompileArguments(bool checkSyntax); virtual QString getCppCompileArguments(bool checkSyntax);
virtual QString getCIncludeArguments(); virtual QString getCIncludeArguments();
@ -75,6 +75,10 @@ protected:
const QString& filename, const QString& filename,
QSet<QString>& parsedFiles, QSet<QString>& parsedFiles,
PCppParser& parser); PCppParser& parser);
virtual bool parseForceUTF8ForAutolink(
const QString& filename,
QSet<QString>& parsedFiles,
PCppParser& parser);
void log(const QString& msg); void log(const QString& msg);
void error(const QString& msg); void error(const QString& msg);
void runCommand(const QString& cmd, const QString& arguments, const QString& workingDir, const QByteArray& inputText=QByteArray()); void runCommand(const QString& cmd, const QString& arguments, const QString& workingDir, const QByteArray& inputText=QByteArray());

View File

@ -59,7 +59,7 @@ bool FileCompiler::prepareForCompile()
} }
} }
mArguments += getCharsetArgument(mEncoding, mOnlyCheckSyntax); mArguments += getCharsetArgument(mEncoding, fileType, mOnlyCheckSyntax);
QString strFileType; QString strFileType;
switch(fileType) { switch(fileType) {
case FileType::CSource: case FileType::CSource:

View File

@ -41,7 +41,7 @@ bool StdinCompiler::prepareForCompile()
fileType = FileType::CppSource; fileType = FileType::CppSource;
QString strFileType; QString strFileType;
if (mEncoding!=ENCODING_ASCII) { if (mEncoding!=ENCODING_ASCII) {
mArguments += getCharsetArgument(mEncoding, mOnlyCheckSyntax); mArguments += getCharsetArgument(mEncoding,fileType, mOnlyCheckSyntax);
} }
switch(fileType) { switch(fileType) {
case FileType::CSource: case FileType::CSource:

View File

@ -646,31 +646,6 @@ void Project::saveAsTemplate(const QString &filename,
ini.SetValue("Template", "Description", toByteArray(description)); ini.SetValue("Template", "Description", toByteArray(description));
ini.SetValue("Project", "Icon", toByteArray(options().icon)); ini.SetValue("Project", "Icon", toByteArray(options().icon));
//todo: save to template //todo: save to template
// mOptions.type = static_cast<ProjectType>(mIni->GetLongValue("Project", "Type", 0)); // default = gui
// mOptions.objFiles = fromByteArray(mIni->GetValue("Project", "ObjFiles", "")).split(";",QString::SkipEmptyParts);
// mOptions.includes = fromByteArray(mIni->GetValue("Project", "Includes", "")).split(";",QString::SkipEmptyParts);
// mOptions.libs = fromByteArray(mIni->GetValue("Project", "Libs", "")).split(";",QString::SkipEmptyParts);
// mOptions.resourceIncludes = fromByteArray(mIni->GetValue("Project", "ResourceIncludes", "")).split(";",QString::SkipEmptyParts);
// mOptions.compilerCmd = fromByteArray(mIni->GetValue("Project", "Compiler", ""));
// mOptions.cppCompilerCmd = fromByteArray(mIni->GetValue("Project", "CppCompiler", ""));
// mOptions.linkerCmd = fromByteArray(mIni->GetValue("Project", "Linker",""));
// mOptions.isCpp = mIni->GetBoolValue("Project", "IsCpp", false);
// mOptions.includeVersionInfo = mIni->GetBoolValue("Project", "IncludeVersionInfo", false);
// mOptions.supportXPThemes = mIni->GetBoolValue("Project", "SupportXPThemes", false);
// mOptions.exeOutput = fromByteArray(mIni->GetValue("Project", "ExeOutput", ""));
// mOptions.objectOutput = fromByteArray(mIni->GetValue("Project", "ObjectOutput", ""));
// mOptions.logOutput = fromByteArray(mIni->GetValue("Project", "LogOutput", ""));
// mOptions.staticLink = mIni->GetBoolValue("Project", "StaticLink",true);
// mOptions.addCharset = mIni->GetBoolValue("Project", "AddCharset",true);
// bool useUTF8 = mIni->GetBoolValue("Project", "UseUTF8", false);
// if (useUTF8) {
// mOptions.encoding = fromByteArray(mIni->GetValue("Project","Encoding", ENCODING_UTF8));
// } else {
// mOptions.encoding = fromByteArray(mIni->GetValue("Project","Encoding", ENCODING_AUTO_DETECT));
// }
// mOptions.modelType = (ProjectModelType)mIni->GetLongValue("Project", "ModelType", (int)ProjectModelType::FileSystem);
} }
PProjectUnit Project::findUnitByFilename(const QString &filename) PProjectUnit Project::findUnitByFilename(const QString &filename)

View File

@ -143,6 +143,8 @@ void ProjectTemplate::readTemplateFile(const QString &fileName)
mOptions.exeOutput = fromByteArray(mIni->GetValue("Project", "ExeOutput", "")); mOptions.exeOutput = fromByteArray(mIni->GetValue("Project", "ExeOutput", ""));
mOptions.objectOutput = fromByteArray(mIni->GetValue("Project", "ObjectOutput", "")); mOptions.objectOutput = fromByteArray(mIni->GetValue("Project", "ObjectOutput", ""));
mOptions.logOutput = fromByteArray(mIni->GetValue("Project", "LogOutput", "")); mOptions.logOutput = fromByteArray(mIni->GetValue("Project", "LogOutput", ""));
mOptions.execEncoding = mIni->GetValue("Project","ExecEncoding", ENCODING_SYSTEM_DEFAULT);
mOptions.staticLink = mIni->GetBoolValue("Project", "StaticLink",true); mOptions.staticLink = mIni->GetBoolValue("Project", "StaticLink",true);
mOptions.addCharset = mIni->GetBoolValue("Project", "AddCharset",true); mOptions.addCharset = mIni->GetBoolValue("Project", "AddCharset",true);
bool useUTF8 = mIni->GetBoolValue("Project", "UseUTF8", false); bool useUTF8 = mIni->GetBoolValue("Project", "UseUTF8", false);

View File

@ -1,5 +1,6 @@
[ [
{ {
"execUseUTF8": true,
"header": "raylib.h", "header": "raylib.h",
"links": "-lraylib -lGL -lm -lpthread -ldl -lrt -lX11" "links": "-lraylib -lGL -lm -lpthread -ldl -lrt -lX11"
}, },

View File

@ -28,6 +28,7 @@
"links": "-lgraphics -luuid -lmsimg32 -lgdi32 -limm32 -lole32 -loleaut32 -lwinmm -lgdiplus" "links": "-lgraphics -luuid -lmsimg32 -lgdi32 -limm32 -lole32 -loleaut32 -lwinmm -lgdiplus"
}, },
{ {
"execUseUTF8": true,
"header": "raylib.h", "header": "raylib.h",
"links": "-lraylib -lopengl32 -lgdi32 -lwinmm" "links": "-lraylib -lopengl32 -lgdi32 -lwinmm"
}, },

View File

@ -51,7 +51,8 @@ void CompilerAutolinkWidget::doSave()
if (!link->header.isEmpty()) { if (!link->header.isEmpty()) {
pAutolinkManager->setLink( pAutolinkManager->setLink(
link->header, link->header,
link->linkOption link->linkOption,
link->execUseUTF8
); );
} }
} }
@ -79,7 +80,7 @@ int AutolinkModel::rowCount(const QModelIndex &) const
int AutolinkModel::columnCount(const QModelIndex &) const int AutolinkModel::columnCount(const QModelIndex &) const
{ {
return 2; return 3;
} }
QVariant AutolinkModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant AutolinkModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -89,6 +90,8 @@ QVariant AutolinkModel::headerData(int section, Qt::Orientation orientation, int
case 0: case 0:
return tr("Header"); return tr("Header");
case 1: case 1:
return tr("UTF-8");
case 2:
return tr("Link options"); return tr("Link options");
} }
} }
@ -107,9 +110,17 @@ QVariant AutolinkModel::data(const QModelIndex &index, int role) const
switch(index.column()) { switch(index.column()) {
case 0: case 0:
return link->header; return link->header;
case 1: case 2:
return link->linkOption; return link->linkOption;
} }
} else if (role == Qt::CheckStateRole && index.column()==1) {
int row = index.row();
if (row<0 || row>=mLinks.count())
return QVariant();
PAutolink link = mLinks[row];
return link->execUseUTF8 ? Qt::Checked : Qt::Unchecked;
} else if (role == Qt::TextAlignmentRole && index.column()==1) {
return Qt::AlignCenter;
} }
return QVariant(); return QVariant();
} }
@ -118,7 +129,16 @@ bool AutolinkModel::setData(const QModelIndex &index, const QVariant &value, int
{ {
if (!index.isValid()) if (!index.isValid())
return false; return false;
if (role == Qt::EditRole) { if (role == Qt::CheckStateRole && index.column()==1) {
int row = index.row();
if (row<0 || row>=mLinks.count())
return false;
PAutolink link = mLinks[row];
link->execUseUTF8 = (value == Qt::Checked);
mWidget->setSettingsChanged();
return true;
}
else if (role == Qt::EditRole) {
int row = index.row(); int row = index.row();
if (row<0 || row>=mLinks.count()) if (row<0 || row>=mLinks.count())
return false; return false;
@ -143,7 +163,7 @@ bool AutolinkModel::setData(const QModelIndex &index, const QVariant &value, int
mLinks[row]=newLink; mLinks[row]=newLink;
mWidget->setSettingsChanged(); mWidget->setSettingsChanged();
return true; return true;
} else if (index.column() == 1) { } else if (index.column() == 2) {
//we must create a new link, becasue mList may share link pointer with the autolink manger //we must create a new link, becasue mList may share link pointer with the autolink manger
PAutolink newLink = std::make_shared<Autolink>(); PAutolink newLink = std::make_shared<Autolink>();
newLink->header = link->header; newLink->header = link->header;
@ -160,6 +180,9 @@ Qt::ItemFlags AutolinkModel::flags(const QModelIndex &index) const
{ {
Qt::ItemFlags flags = Qt::NoItemFlags; Qt::ItemFlags flags = Qt::NoItemFlags;
if (index.isValid()) { if (index.isValid()) {
if (index.column()==1)
flags = Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
else
flags = Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable ; flags = Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable ;
} }
return flags; return flags;

View File

@ -25,4 +25,5 @@ UnitCount=3
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lraylib -lopengl32 -lgdi32 -lwinmm linker=-lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8

View File

@ -17,4 +17,4 @@ UnitCount=1
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lraylib -lopengl32 -lgdi32 -lwinmm linker=-lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8

View File

@ -25,4 +25,4 @@ UnitCount=3
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lraylib -lopengl32 -lgdi32 -lwinmm linker=-lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8

View File

@ -17,4 +17,4 @@ UnitCount=1
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lraylib -lopengl32 -lgdi32 -lwinmm linker=-lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8

View File

@ -17,4 +17,4 @@ UnitCount=1
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lraylib -lopengl32 -lgdi32 -lwinmm linker=-lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8

View File

@ -17,4 +17,4 @@ UnitCount=1
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lrdrawing -lraylib -lopengl32 -lgdi32 -lwinmm linker=-lrdrawing -lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8

View File

@ -17,4 +17,4 @@ UnitCount=1
Type=1 Type=1
IsCpp=0 IsCpp=0
linker=-lrdrawing -lraylib -lopengl32 -lgdi32 -lwinmm linker=-lrdrawing -lraylib -lopengl32 -lgdi32 -lwinmm
ExecEncoding=UTF-8