QtNovelUI/WordsIDE/manager_docs.cpp

247 lines
6.2 KiB
C++
Raw Normal View History

#include "manager_docs.h"
#include "DocsManager.h"
using namespace Components;
2023-03-10 13:01:19 +00:00
using namespace Core;
2023-03-10 13:01:19 +00:00
DocumentsManager::DocumentsManager(AppCore *src, Project::ProjectManager *project, PresentContainer *con)
: rtcore(src), pjtins(project), present_ui(con) {}
2023-03-10 13:01:19 +00:00
Project::ProjectManager *DocumentsManager::projectManager() const
{
2023-03-10 13:01:19 +00:00
return pjtins;
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::newPackage(const Route &link)
{
pjtins->newPackage(link.links());
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::newPackage(const Route &group, const QString &name)
{
2023-03-10 13:01:19 +00:00
newPackage(group|name);
}
2023-03-10 13:01:19 +00:00
QList<QString> DocumentsManager::fileTypes() const
{
QList<QString> all_types;
for(auto &it : rtcore->allViews())
all_types.append(it->suffixes());
return all_types;
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::newFile(const Route &group_path, const QString &name, const QString &suffix)
{
auto list = rtcore->extensions(suffix);
QDir root = pjtins->projectDir();
auto rst = list.first()->create(root, name, suffix);
if(std::get<0>(rst)){
auto rpath = root.relativeFilePath(std::get<1>(rst));
auto idx = pjtins->signFile(converter(group_path), name, rpath);
openFile(converter(idx));
}
else{
throw new SimpleException("无法创建文件:" + name);
}
}
2023-03-10 13:01:19 +00:00
QModelIndex DocumentsManager::converter(const Route &path)
{
2023-03-10 13:01:19 +00:00
auto path_list = path.links();
auto itor_node = pjtins->model()->item(0);
while (path_list.size()) {
auto item = path_list.takeAt(0);
int idx = 0;
for(; idx<itor_node->rowCount(); ++idx){
if(itor_node->child(idx)->text() == item){
itor_node = itor_node->child(idx);
break;
}
}
if(idx == itor_node->rowCount())
2023-03-10 13:01:19 +00:00
throw new SimpleException("指定的path非法/"+ path.links().join("/"));
}
return itor_node->index();
}
2023-03-10 13:01:19 +00:00
Route DocumentsManager::converter(const QModelIndex &index)
{
2023-03-10 13:01:19 +00:00
Route path;
auto item = pjtins->model()->itemFromIndex(index);
auto pjt_node = pjtins->model()->item(0);
while (item != pjt_node) {
2023-03-10 13:01:19 +00:00
path = path | item->text().trimmed();
item = item->parent();
}
return path;
}
2023-03-10 13:01:19 +00:00
//QModelIndex DocumentsManager::converter(const QFileInfo &file)
//{
// for(auto &key : doc_openning.keys()){
// if(file == QFileInfo(doc_openning[key]->absoluteTargetPath()))
// return converter(key);
// }
// return QModelIndex();
//}
void DocumentsManager::removeNode(const Route &node_path)
{
2023-03-10 13:01:19 +00:00
auto node_mindex = converter(node_path);
auto activie_file_nodes = actives();
2023-03-10 13:01:19 +00:00
auto file_nodes = pjtins->filesGather(node_mindex);
2023-03-10 13:01:19 +00:00
QList<Route> path_buff;
for(auto &n : file_nodes){
auto target = converter(n);
if(activie_file_nodes.contains(target))
path_buff << target;
}
2023-03-10 13:01:19 +00:00
close(path_buff);
pjtins->deleteTarget(node_mindex);
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::renameNode(const Route &node_path, const QString &name)
{
pjtins->rename(converter(node_path), name);
auto activie_file_nodes = actives();
if(activie_file_nodes.contains(node_path)){
auto inst = activePresentOf(node_path);
2023-03-10 13:01:19 +00:00
inst->applySetting(name, rtcore);
present_ui->active(inst);
}
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::openFile(const Route &path_node)
{
2023-03-10 13:01:19 +00:00
auto info = pjtins->queryInfo(converter(path_node));
if(!info.isFile())
throw new SimpleException("打开的节点不是文件节点");
2023-03-10 13:01:19 +00:00
auto path_union = path_node.links().join("/");
if(actives().contains(path_node)){
present_ui->active(doc_openning[path_union]);
return;
}
auto list = rtcore->extensions(info.suffix());
auto nview = list.first()->newInst();
nview->load(info);
2023-03-10 13:01:19 +00:00
doc_openning[path_union] = nview;
present_ui->append(nview);
present_ui->active(nview);
}
2023-03-10 13:01:19 +00:00
QList<Route> DocumentsManager::actives() const
{
2023-03-10 13:01:19 +00:00
QList<Route> retv;
for(auto x : doc_openning.keys())
retv << Route::collect(x.split("/"));
return retv;
}
2023-03-10 13:01:19 +00:00
MakeTools::ContentPresent *DocumentsManager::activePresentOf(const Route &node_path)
{
2023-03-10 13:01:19 +00:00
return doc_openning[node_path.links().join("/")];
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::save(const QList<Route> &docs_path)
{
2023-03-10 13:01:19 +00:00
auto actives_paths = this->actives();
for(auto &idx_path : docs_path)
if(actives_paths.contains(idx_path))
activePresentOf(idx_path)->saveAs();
}
2023-03-10 13:01:19 +00:00
void DocumentsManager::close(const QList<Route> &doc_paths)
{
auto actives = this->actives();
for(auto &idx : doc_paths)
if(actives.contains(idx)){
auto inst = activePresentOf(idx);
inst->saveAs();
2023-03-10 13:01:19 +00:00
present_ui->remove(inst);
delete inst;
2023-03-10 13:01:19 +00:00
doc_openning.remove(idx.links().join("/"));
}
}
2023-03-10 13:01:19 +00:00
Route DocumentsManager::pathOf(MakeTools::ContentPresent *ins) const
{
return Route::collect(doc_openning.key(ins).split("/"));
}
Route DocumentsManager::pathOf(const QModelIndex &p) const
{
2023-03-10 13:01:19 +00:00
if(!p.isValid())
throw new SimpleException("传入非法QModelIndex");
auto item = projectManager()->model()->itemFromIndex(p);
auto proot = projectManager()->model()->item(0);
Route retv;
while(proot != item){
retv = retv|item->text();
item = item->parent();
}
return retv;
}
void DocumentsManager::build(bool all_from_disk)
{
if(!all_from_disk)
save();
auto chains = pjtins->filesWithEnds("storychain");
for(auto &it : chains){
rtcore->getMakeCore()->compile(std::get<1>(it), std::get<0>(it));
}
auto units = pjtins->filesWithEnds("storyunit");
for(auto &it : units)
rtcore->getMakeCore()->compile(std::get<1>(it), std::get<0>(it));
auto storys = pjtins->filesWithEnds("storyboard");
for(auto &it : storys)
rtcore->getMakeCore()->compile(std::get<1>(it), std::get<0>(it));
auto volumes = pjtins->filesWithEnds("storyvolume");
for(auto &it : volumes)
rtcore->getMakeCore()->compile(std::get<1>(it), std::get<0>(it));
auto concepts = pjtins->filesWithEnds("storyconcept");
for(auto &it : concepts)
rtcore->getMakeCore()->compile(std::get<1>(it), std::get<0>(it));
}
2023-03-10 13:01:19 +00:00
QString DocumentsManager::name() const
{
2023-03-10 13:01:19 +00:00
return NAME(DocumentsManager);
}