#include "DocsManager.h" #include "mainwindow.h" #include #include #include using namespace MakeTools; using namespace Core; DocsManager::DocsManager(StoryTool *tool, AppCore *host, MainWindow *views) : host_core(host), views_holder(views), make_core(tool) { } void DocsManager::saveAll() const { for(auto &it : sourcecode_map) it->save(); for(auto &it : plaintext_map) it->save(); } void DocsManager::closeAll() { for(auto &it : sourcecode_map.keys()) closeTextComponent(QFileInfo(it)); for(auto &it : plaintext_map.keys()) closeTextComponent(QFileInfo(it)); host_core->getMakeCore()->getCore()->clear(); } bool DocsManager::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; } TextView *DocsManager::queryTextComponent(const QWidget *child_view) const { for(auto ins : sourcecode_map) if(ins->textView() == child_view) return ins; for(auto ins : plaintext_map) if(ins->textView() == child_view) return ins; return nullptr; } TextView *DocsManager::queryTextComponent(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 DocsManager::closeTextComponent(const QFileInfo &target) { auto key = target.absoluteFilePath(); if(sourcecode_map.contains(key)){ sourcecode_map[key]->save(); delete sourcecode_map[key]; sourcecode_map.remove(key); } else if(plaintext_map.contains(key)){ plaintext_map[key]->save(); delete plaintext_map[key]; plaintext_map.remove(key); } } void DocsManager::openTextDocument(const QString &src, const QString &name) { auto xfactorys = host_core->extensions(QFileInfo(src).suffix()); if(contains(src)){ auto ins = queryTextComponent(QFileInfo(src)); dynamic_cast(ins)->reloadConfigrations(host_core->getConfigs(xfactorys[0]->configs())); views_holder->contentViewAppend(ins->textView(), name); return; } make_core->compile(QFileInfo(src), name); TextView *tview = dynamic_cast(xfactorys[0]->newInstance(host_core)); tview->initSource(host_core, QFileInfo(src), name, views_holder); addPerceptionList(tview); dynamic_cast(tview)->resetProcsType(QFileInfo(src).suffix()); dynamic_cast(tview)->reloadConfigrations(host_core->getConfigs(xfactorys[0]->configs())); QFile fin(src); if(!fin.open(QIODevice::ReadOnly | QIODevice::Text)){ QMessageBox::critical(views_holder, "系统错误", QString("无法打开指定文件:%1(%2)").arg(name, src)); return; } QTextStream tin(&fin); tview->textContentReset(tin.readAll()); views_holder->contentViewAppend(tview->textView(), name); } void DocsManager::addPerceptionList(TextView *ins, SensitiveType type) { if(type == SensitiveType::CompileAtChanged){ connect(ins, &TextView::dataChanged, [ins, this](const QString &path){ this->recompile(path, ins->docName()); }); this->sourcecode_map[ins->absoluteFilePath()] = ins; } else{ this->plaintext_map[ins->absoluteFilePath()] = ins; } } void DocsManager::addProcTrigger(std::function exc) { this->trigger_list << exc; } void DocsManager::recompile(const QString &file_path, const QString &doc_name) { if(!sourcecode_map.contains(file_path)) return; auto view = this->sourcecode_map[file_path]; make_core->compileSource(QFileInfo(file_path), view->textContent(), doc_name); for(auto &ex : trigger_list) ex(); } TextView::TextView() : QObject(nullptr){} void TextView::initSource(Core::AppCore *core, const QFileInfo &src, const QString &name, QWidget *parent) { this->doc_name = name; this->initSource(core, src, parent); } QString TextView::absoluteFilePath() const { return source_x; } void TextView::save() const { QFile bout(absoluteFilePath()); if(!bout.open(QIODevice::WriteOnly|QIODevice::Text)){ QMessageBox::critical(nullptr, "系统错误", absoluteFilePath()+"文件无法打开"); return; } QTextStream tout(&bout); tout << textContent(); tout.flush(); } QString TextView::docName() const { return doc_name; } void TextView::initSource(Core::AppCore *core, const QFileInfo &src, QWidget *parent) { this->source_x = src.absoluteFilePath(); if(parent) this->setParent(parent); }