QtNovelUI/WordsIDE/manager_docs.cpp

195 lines
4.6 KiB
C++
Raw Normal View History

#include "manager_docs.h"
#include "DocsManager.h"
using namespace Components;
DocumentManager::DocumentManager(Core::AppCore *src, Project::ProjectManager *project, PresentContainer *con)
: rtcore(src), pjtins(project), front_end(con) {}
void DocumentManager::newPackage(const QString &path_str)
{
QList<QString> path_list;
QRegExp exp("/([^/]+)");
auto offset = -1;
while ((offset = exp.indexIn(path_str, offset + 1)) != -1) {
path_list << exp.capturedTexts()[1].trimmed();
}
pjtins->newPackage(path_list);
}
void DocumentManager::newPackage(const QString &group_path, const QString &name)
{
QString path = group_path + "/" + name;
newPackage(path);
}
QList<QString> DocumentManager::fileTypes() const
{
QList<QString> all_types;
for(auto &it : rtcore->allViews())
all_types.append(it->suffixes());
return all_types;
}
void DocumentManager::newFile(const QString &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);
}
}
QModelIndex DocumentManager::converter(const QString &path)
{
QList<QString> path_list;
QRegExp exp("/([^/]+)");
auto offset = -1;
while ((offset = exp.indexIn(path, offset + 1)) != -1) {
path_list << exp.capturedTexts()[1].trimmed();
}
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())
throw new SimpleException("指定的path非法"+path);
}
return itor_node->index();
}
QString DocumentManager::converter(const QModelIndex &index)
{
QString path = "";
auto item = pjtins->model()->itemFromIndex(index);
auto pjt_node = pjtins->model()->item(0);
while (item != pjt_node) {
path.prepend("/" + item->text().trimmed());
item = item->parent();
}
return path;
}
void DocumentManager::removeNode(const QString &node_path)
{
auto node = converter(node_path);
auto activie_file_nodes = actives();
auto file_nodes = pjtins->filesGather(node);
for(auto &n : file_nodes)
if(activie_file_nodes.contains(converter(n)))
close(QList<QString>()<<converter(n));
pjtins->deleteTarget(node);
}
void DocumentManager::renameNode(const QString &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);
inst->rename(name);
front_end->active(inst);
}
}
void DocumentManager::openFile(const QString &file_node)
{
auto info = pjtins->queryInfo(converter(file_node));
if(!info.isFile())
throw new SimpleException("打开的节点不是文件节点");
if(actives().contains(file_node)){
front_end->active(doc_openning[file_node]);
return;
}
auto list = rtcore->extensions(info.suffix());
auto nview = list.first()->newInst();
nview->load(info);
doc_openning[file_node] = nview;
front_end->append(nview);
front_end->active(nview);
}
QList<QString> DocumentManager::actives() const
{
return doc_openning.keys();
}
MakeTools::ContentPresent *DocumentManager::activePresentOf(const QString &node_path)
{
return doc_openning[node_path];
}
void DocumentManager::save(const QList<QString> &docs)
{
auto actives = this->actives();
for(auto &idx : docs)
if(actives.contains(idx))
activePresentOf(idx)->saveAs();
}
void DocumentManager::close(const QList<QString> &doc_paths)
{
auto actives = this->actives();
for(auto &idx : doc_paths)
if(actives.contains(idx)){
auto inst = activePresentOf(idx);
inst->saveAs();
front_end->remove(inst);
delete inst;
doc_openning.remove(idx);
}
}
QString DocumentManager::pathOf(MakeTools::ContentPresent *ins) const
{
return doc_openning.key(ins);
}
QString DocumentManager::name() const
{
return NAME(DocumentManager);
}