QtNovelUI/WordsIDE/SensitiveCore.cpp

145 lines
3.4 KiB
C++

#include "SensitiveCore.h"
#include <QMessageBox>
#include <QTextStream>
#include <QDebug>
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;
}
TextView *SensitiveCore::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 *SensitiveCore::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 SensitiveCore::closeTextComponent(const QFileInfo &target)
{
for(auto &it : sourcecode_map.keys()){
if(it == target.absoluteFilePath()){
sourcecode_map[it]->save();
delete sourcecode_map[it];
sourcecode_map.remove(it);
}
}
for(auto &it : plaintext_map.keys())
if(it == target.absoluteFilePath()){
sourcecode_map[it]->save();
delete sourcecode_map[it];
sourcecode_map.remove(it);
}
}
void SensitiveCore::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->filePath()] = ins;
}
else{
this->plaintext_map[ins->filePath()] = ins;
}
}
void SensitiveCore::addProcTrigger(std::function<void ()> exc)
{
this->trigger_list << exc;
}
void SensitiveCore::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::filePath() const
{
return source_x;
}
void TextView::save() const
{
QFile bout(filePath());
if(!bout.open(QIODevice::WriteOnly|QIODevice::Text)){
QMessageBox::critical(nullptr, "系统错误", filePath()+"文件无法打开");
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);
}