175 lines
5.6 KiB
C++
175 lines
5.6 KiB
C++
#include "manager_docs.h"
|
||
#include "DocsManager.h"
|
||
#include "keywordshighlighter.h"
|
||
|
||
using namespace Components;
|
||
using namespace Core;
|
||
using namespace Config;
|
||
using namespace Presents;
|
||
|
||
DocumentsManager::DocumentsManager(Project::ProjectManager *project) : pjtins(project) { initContentViewPlugins(); }
|
||
|
||
void DocumentsManager::appendPresent(Components::PresentHost *container) {
|
||
if (present_ui.contains(container))
|
||
return;
|
||
|
||
present_ui << container;
|
||
}
|
||
|
||
void DocumentsManager::removePresent(Components::PresentHost *container) {}
|
||
|
||
Project::ProjectManager *DocumentsManager::projectManager() const { return pjtins; }
|
||
|
||
#include "srcedit_defaulttext.h"
|
||
#include "srcedit_storyboard.h"
|
||
void DocumentsManager::initContentViewPlugins() {
|
||
registPresentType(new DefaultTextEditFactory);
|
||
registPresentType(new StorySourceEditFactory);
|
||
}
|
||
|
||
QList<QString> DocumentsManager::fileTypes() const {
|
||
QList<QString> all_types;
|
||
for (auto &it : factory_map.keys())
|
||
all_types.append(it);
|
||
return all_types;
|
||
}
|
||
|
||
void DocumentsManager::createPackage(const Core::Route &link) { pjtins->operateAccess()->newPackage(link.links()); }
|
||
|
||
void DocumentsManager::createFile(const Core::Route &group_path, const QString &name, const QString &suffix) {
|
||
auto all_types = fileTypes();
|
||
if (!all_types.contains(suffix))
|
||
throw new SimpleException<QString>("创建文件错误", "指定文件类型无法打开:" + suffix);
|
||
|
||
auto midx = convertPath(group_path);
|
||
auto fidx = projectManager()->operateAccess()->newFile(midx, name, suffix);
|
||
auto fileinfo = projectManager()->queryAccess()->queryInfo(fidx);
|
||
factory_map[suffix]->create(fileinfo);
|
||
}
|
||
|
||
void DocumentsManager::renameNode(const Core::Route &node_path, const QString &name) {
|
||
pjtins->operateAccess()->rename(convertPath(node_path), name);
|
||
|
||
auto activie_file_nodes = actives();
|
||
if (activie_file_nodes.contains(node_path)) {
|
||
auto inst = convertPresent(node_path);
|
||
inst->applySetting(name);
|
||
doc_openning.remove(node_path);
|
||
|
||
auto old_link = node_path.links();
|
||
auto npath = Core::Route::collect(old_link.mid(0, old_link.size() - 1)) | name;
|
||
doc_openning[npath] = inst;
|
||
|
||
active(npath);
|
||
}
|
||
}
|
||
|
||
void DocumentsManager::deleteNode(const Core::Route &node_path) {
|
||
auto node_mindex = convertPath(node_path);
|
||
auto file_nodes = pjtins->queryAccess()->filesGather(node_mindex);
|
||
auto activie_file_nodes = actives();
|
||
|
||
QList<Core::Route> path_buff;
|
||
for (auto &n : file_nodes) {
|
||
auto target = convertPath(n);
|
||
if (activie_file_nodes.contains(target))
|
||
path_buff << target;
|
||
}
|
||
|
||
closeFile(path_buff);
|
||
pjtins->operateAccess()->deleteT(node_mindex);
|
||
}
|
||
|
||
void DocumentsManager::openFile(const QList<Route> &doc_paths) {
|
||
for (auto &it : doc_paths) {
|
||
auto openning = actives();
|
||
if (openning.contains(it)) {
|
||
doc_openning[it]->applySetting(it.links().last());
|
||
active(it);
|
||
continue;
|
||
}
|
||
|
||
auto info = pjtins->queryAccess()->queryInfo(convertPath(it));
|
||
if (!info.isFile())
|
||
throw new SimpleException<QString>("打开文件错误", "指向的节点不是文件节点");
|
||
|
||
auto view = factory_map[info.suffix()]->newInst(this, it);
|
||
doc_openning[it] = view;
|
||
view->load(info);
|
||
view->applySetting(it.links().last());
|
||
|
||
for (auto con : present_ui)
|
||
if (con->avaliable(view))
|
||
con->append(view);
|
||
}
|
||
}
|
||
|
||
void DocumentsManager::closeFile(const QList<Core::Route> &doc_paths) {
|
||
auto actives = this->actives();
|
||
for (auto &idx : doc_paths)
|
||
if (actives.contains(idx)) {
|
||
auto inst = convertPresent(idx);
|
||
inst->saveAs();
|
||
|
||
for (auto vit : present_ui)
|
||
if (present_ui.contains(vit))
|
||
vit->remove(inst);
|
||
|
||
delete inst;
|
||
doc_openning.remove(idx);
|
||
}
|
||
}
|
||
|
||
QModelIndex DocumentsManager::convertPath(const Core::Route &path) {
|
||
auto path_list = path.links();
|
||
auto itor_node = pjtins->model()->item(0);
|
||
while (path_list.size() > 1) {
|
||
auto item = path_list.takeAt(1);
|
||
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->parent()->rowCount())
|
||
throw new SimpleException<QString>("路径转换错误", "指定的path非法:/" + path.links().join("/"));
|
||
}
|
||
return itor_node->index();
|
||
}
|
||
|
||
Route DocumentsManager::convertPath(const QModelIndex &index) {
|
||
Core::Route path;
|
||
auto item = pjtins->model()->itemFromIndex(index);
|
||
while (item != nullptr) {
|
||
path.prepend(item->text().trimmed());
|
||
item = item->parent();
|
||
}
|
||
|
||
return path;
|
||
}
|
||
|
||
Route DocumentsManager::convertPresent(FilePresent *ins) const { return doc_openning.key(ins); }
|
||
|
||
FilePresent *DocumentsManager::convertPresent(const Core::Route &node_path) { return doc_openning[node_path]; }
|
||
|
||
QList<Route> DocumentsManager::actives() const { return doc_openning.keys(); }
|
||
|
||
void DocumentsManager::active(const Core::Route &node_path) {
|
||
if (doc_openning.contains(node_path)) {
|
||
auto inst = convertPresent(node_path);
|
||
for (auto vit : present_ui)
|
||
if (vit->contains(inst))
|
||
vit->active(inst);
|
||
}
|
||
}
|
||
|
||
void DocumentsManager::save() {
|
||
auto actives_paths = this->actives();
|
||
for (auto &p : actives_paths)
|
||
convertPresent(p)->saveAs();
|
||
}
|
||
|
||
QString DocumentsManager::name() const { return NAME(DocumentsManager); }
|