#include "SensitiveCore.h" #include #include using namespace MakeTools; SensitiveCore::SensitiveCore(StoryTool *tool) : make_core(tool) { } void SensitiveCore::saveAll() const { for(auto &it : sourcecode_map) it->save(); for(auto &it : plaintext_map) it->save(); } bool SensitiveCore::contains(const QFileInfo &target) const { for(auto &it : sourcecode_map.keys()) if(it == target.absoluteFilePath()) return true; for(auto &it : plaintext_map.keys()) if(it == target.absoluteFilePath()) return true; return false; } VariedTextView *SensitiveCore::queryComponent(const QFileInfo &target) const { for(auto &it : sourcecode_map.keys()) if(it == target.absoluteFilePath()) return sourcecode_map[it]; for(auto &it : plaintext_map.keys()) if(it == target.absoluteFilePath()) return plaintext_map[it]; return nullptr; } void SensitiveCore::addPerceptionList(VariedTextView *ins, SensitiveType type) { if(type == SensitiveType::CompileAtChanged){ connect(ins, &VariedTextView::dataChanged, this, &SensitiveCore::recompile); this->sourcecode_map[ins->filePath()] = ins; } else{ this->plaintext_map[ins->filePath()] = ins; } } void SensitiveCore::addProcTrigger(std::function exc) { this->trigger_list << exc; } void SensitiveCore::recompile(const QString &file_path) { if(!sourcecode_map.contains(file_path)) return; auto view = this->sourcecode_map[file_path]; make_core->compileSource(QFileInfo(file_path), view->textContent()); for(auto &ex : trigger_list) ex(); } VariedTextView::VariedTextView(const QFileInfo &file, QObject *parent) : QObject(parent), source_x(file){} QString VariedTextView::filePath() const { return source_x.absoluteFilePath(); } void VariedTextView::save() const { QFile bout(filePath()); if(!bout.open(QIODevice::WriteOnly|QIODevice::Text)){ QMessageBox::critical(nullptr, "系统错误", filePath()+"文件无法打开"); return; } QTextStream tout(&bout); tout << textContent(); tout.flush(); }