QtNovelUI/WordsIDE/DocsManager.cpp

149 lines
3.7 KiB
C++
Raw Normal View History

2022-12-31 13:05:58 +00:00
#include "DocsManager.h"
#include "mainwindow.h"
#include <QMessageBox>
#include <QTextStream>
#include <QDebug>
2023-02-26 14:44:00 +00:00
#include <QTextCodec>
2022-12-31 13:05:58 +00:00
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)
2023-02-26 14:44:00 +00:00
it->saveAs();
2022-12-31 13:05:58 +00:00
for(auto &it : plaintext_map)
2023-02-26 14:44:00 +00:00
it->saveAs();
2022-12-31 13:05:58 +00:00
}
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();
}
2023-02-26 14:44:00 +00:00
bool DocsManager::activedContains(const QFileInfo &target) const
2022-12-31 13:05:58 +00:00
{
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;
}
2023-02-26 14:44:00 +00:00
MakeTools::ContentPresent *DocsManager::queryTextComponent(const QWidget *child_view) const
2022-12-31 13:05:58 +00:00
{
for(auto ins : sourcecode_map)
2023-02-26 14:44:00 +00:00
if(ins->widget() == child_view)
2022-12-31 13:05:58 +00:00
return ins;
for(auto ins : plaintext_map)
2023-02-26 14:44:00 +00:00
if(ins->widget() == child_view)
2022-12-31 13:05:58 +00:00
return ins;
return nullptr;
}
2023-02-26 14:44:00 +00:00
MakeTools::ContentPresent *DocsManager::queryTextComponent(const QFileInfo &target) const
2022-12-31 13:05:58 +00:00
{
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)){
2023-02-26 14:44:00 +00:00
sourcecode_map[key]->saveAs();
2022-12-31 13:05:58 +00:00
delete sourcecode_map[key];
sourcecode_map.remove(key);
}
else if(plaintext_map.contains(key)){
2023-02-26 14:44:00 +00:00
plaintext_map[key]->saveAs();
2022-12-31 13:05:58 +00:00
delete plaintext_map[key];
plaintext_map.remove(key);
}
}
void DocsManager::openTextDocument(const QString &src, const QString &name)
{
2023-02-26 14:44:00 +00:00
// 获取匹配的处理类型
2022-12-31 13:05:58 +00:00
auto xfactorys = host_core->extensions(QFileInfo(src).suffix());
2023-02-26 14:44:00 +00:00
// 如果已经打开
if(activedContains(src)){
auto ins = queryTextComponent(QFileInfo(src));
if(ins == nullptr)
throw new SimpleException("逻辑错误,不应出现空指针");
views_holder->contentViewAppend(ins->widget(), name);
2022-12-31 13:05:58 +00:00
return;
}
make_core->compile(QFileInfo(src), name);
2023-02-26 14:44:00 +00:00
auto tins = xfactorys[0]->newInst();
tins->applySetting(name, host_core);
tins->load(QFileInfo(src));
addPerceptionList(tins);
2022-12-31 13:05:58 +00:00
2023-02-26 14:44:00 +00:00
views_holder->contentViewAppend(tins->widget(), name);
2022-12-31 13:05:58 +00:00
}
2023-02-26 14:44:00 +00:00
void DocsManager::addPerceptionList(ContentPresent *ins, SensitiveType type)
2022-12-31 13:05:58 +00:00
{
if(type == SensitiveType::CompileAtChanged){
2023-02-26 14:44:00 +00:00
connect(ins, &ContentPresent::dataChanged, [ins, this](const QString &path){
this->recompile(path, ins->name());
2022-12-31 13:05:58 +00:00
});
2023-02-26 14:44:00 +00:00
this->sourcecode_map[ins->absulateTargetPath()] = ins;
2022-12-31 13:05:58 +00:00
}
else{
2023-02-26 14:44:00 +00:00
this->plaintext_map[ins->absulateTargetPath()] = ins;
2022-12-31 13:05:58 +00:00
}
}
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];
2023-02-26 14:44:00 +00:00
make_core->compileSource(QFileInfo(file_path), view->getText(), doc_name);
2022-12-31 13:05:58 +00:00
for(auto &ex : trigger_list)
ex();
}
2023-02-26 14:44:00 +00:00
MakeTools::ContentPresent::ContentPresent(QObject *parent)
: QObject(parent)
2022-12-31 13:05:58 +00:00
{
}