QtNovelUI/WordsIDE/DocsManager.cpp

149 lines
3.7 KiB
C++

#include "DocsManager.h"
#include "mainwindow.h"
#include <QMessageBox>
#include <QTextStream>
#include <QDebug>
#include <QTextCodec>
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->saveAs();
for(auto &it : plaintext_map)
it->saveAs();
}
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::activedContains(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;
}
MakeTools::ContentPresent *DocsManager::queryTextComponent(const QWidget *child_view) const
{
for(auto ins : sourcecode_map)
if(ins->widget() == child_view)
return ins;
for(auto ins : plaintext_map)
if(ins->widget() == child_view)
return ins;
return nullptr;
}
MakeTools::ContentPresent *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]->saveAs();
delete sourcecode_map[key];
sourcecode_map.remove(key);
}
else if(plaintext_map.contains(key)){
plaintext_map[key]->saveAs();
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(activedContains(src)){
auto ins = queryTextComponent(QFileInfo(src));
if(ins == nullptr)
throw new SimpleException("逻辑错误,不应出现空指针");
views_holder->contentViewAppend(ins->widget(), name);
return;
}
make_core->compile(QFileInfo(src), name);
auto tins = xfactorys[0]->newInst();
tins->applySetting(name, host_core);
tins->load(QFileInfo(src));
addPerceptionList(tins);
views_holder->contentViewAppend(tins->widget(), name);
}
void DocsManager::addPerceptionList(ContentPresent *ins, SensitiveType type)
{
if(type == SensitiveType::CompileAtChanged){
connect(ins, &ContentPresent::dataChanged, [ins, this](const QString &path){
this->recompile(path, ins->name());
});
this->sourcecode_map[ins->absoluteTargetPath()] = ins;
}
else{
this->plaintext_map[ins->absoluteTargetPath()] = ins;
}
}
void DocsManager::addProcTrigger(std::function<void ()> 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->getText(), doc_name);
for(auto &ex : trigger_list)
ex();
}
MakeTools::ContentPresent::ContentPresent(QObject *parent)
: QObject(parent)
{
}