Commit 3c9ccc30 authored by Leander Schulten's avatar Leander Schulten
Browse files

Make generation of default code for the modules in the codeeditorhelper...

Make generation of default code for the modules in the codeeditorhelper "intelligent", it only insert new methods if necessary and removes old code when not modified
parent b79453fc
...@@ -523,13 +523,56 @@ QString generateFilterCode(){ ...@@ -523,13 +523,56 @@ QString generateFilterCode(){
return code; return code;
} }
void CodeEditorHelper::setModule( Modules::Module* _module){
if(_module != module){
module = _module;
QObject::disconnect(typeConnection);
if(module){
typeConnection = QObject::connect(module,&Modules::Module::typeChanged,this,&CodeEditorHelper::typeChanged);
if(module->getCode().isEmpty()){
//create default code
if(module->getType()==Modules::Module::Program){
module->setCode(generateProgrammCode());
}else if(module->getType()==Modules::Module::LoopProgram){
module->setCode(generateLoopProgrammCode());
}else if(module->getType()==Modules::Module::Filter){
module->setCode(generateFilterCode());
}
}
}
emit moduleChanged();
}
}
void CodeEditorHelper::typeChanged(){ void CodeEditorHelper::typeChanged(){
QString text = document->toPlainText().trimmed();
// check if we can replace the whole text(when it is empty or a default code)
bool replace = text.trimmed().isEmpty() ? true : text == generateFilterCode().trimmed() || text == generateLoopProgrammCode().trimmed() || text == generateProgrammCode().trimmed();
if(module->getType()==Modules::Module::Program){ if(module->getType()==Modules::Module::Program){
emit insertText(generateProgrammCode(),0); // we only have to generate code if the current code dont have the right functions
if(replace)
module->setCode(generateProgrammCode());
else if(text.indexOf("int getProgrammLengthInMS()") <0 || text.indexOf("void start()") <0 || text.indexOf("ProgramState doStep(time_diff_t") <0){
//emit insertText(generateProgrammCode(),0);
module->setCode(generateProgrammCode() + module->getCode());
qDebug () << "insert ProgrammCode" << generateProgrammCode();
}
}else if(module->getType()==Modules::Module::LoopProgram){ }else if(module->getType()==Modules::Module::LoopProgram){
// we only have to generate code if the current code dont have the right functions
if(replace)
module->setCode(generateLoopProgrammCode());
else if(text.indexOf("int getProgrammLengthInMS()") <0 || text.indexOf("void start()") <0 || text.indexOf("void loopProgram()") <0){
emit insertText(generateLoopProgrammCode(),0); emit insertText(generateLoopProgrammCode(),0);
qDebug () << "insert LoopProgrammCode" << generateLoopProgrammCode();
}
}else if(module->getType()==Modules::Module::Filter){ }else if(module->getType()==Modules::Module::Filter){
// we only have to generate code if the current code dont have the right functions
if(replace)
module->setCode(generateFilterCode());
else if(text.indexOf("unsigned int computeOutputLength(unsigned int inputLength)") <0 || text.indexOf("void filter()") <0 || text.indexOf("bool doStep(time_diff_t ") <0){
emit insertText(generateFilterCode(),0); emit insertText(generateFilterCode(),0);
qDebug () << "insert FilterCode " << generateFilterCode();
}
} }
} }
......
...@@ -94,15 +94,7 @@ public: ...@@ -94,15 +94,7 @@ public:
return &codeCompletions; return &codeCompletions;
} }
void setModule( Modules::Module* _module){ void setModule( Modules::Module* _module);
if(_module != module){
module = _module;
QObject::disconnect(typeConnection);
if(module)
typeConnection = QObject::connect(module,&Modules::Module::typeChanged,this,&CodeEditorHelper::typeChanged);
emit moduleChanged();
}
}
Modules::Module *getModule() const { Modules::Module *getModule() const {
return module; return module;
} }
...@@ -135,6 +127,11 @@ signals: ...@@ -135,6 +127,11 @@ signals:
void information(QString text); void information(QString text);
void moduleChanged(); void moduleChanged();
void documentChanged(); void documentChanged();
/**
* @brief insertText send a signal to the qml editor component to inform the component, that text should be inserted at the current cursor pos
* @param newText the text that should be inserted
* @param pos the new cursor after text inserting.
*/
void insertText(QString newText, int pos); void insertText(QString newText, int pos);
protected: protected:
void typeChanged(); void typeChanged();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment