QtNovelUI/WordsIDE/vcontrol_project.cpp

209 lines
6.3 KiB
C++
Raw Normal View History

2023-03-01 14:19:13 +00:00
#include "vcontrol_project.h"
2022-11-17 08:26:05 +00:00
#include <QInputDialog>
#include <QMenu>
#include <QMessageBox>
2023-02-26 14:44:00 +00:00
#include <QRandomGenerator>
#include <QVBoxLayout>
2022-11-17 08:26:05 +00:00
2023-02-26 14:44:00 +00:00
Components::VisiableProjectController::VisiableProjectController(Core::AppCore *src, Project::ProjectManager *project, QWidget *parent)
: QTreeView(parent), rtcore(src), pjtins(project)
2022-11-17 08:26:05 +00:00
{
setModel(pjtins->model());
2022-11-17 08:26:05 +00:00
setHeaderHidden(true);
setIconSize(QSize(24, 24));
// 右键菜单
setContextMenuPolicy(Qt::CustomContextMenu);
2023-02-26 14:44:00 +00:00
connect(this, &QWidget::customContextMenuRequested, this, &VisiableProjectController::contexMenuPopup);
2022-11-17 08:26:05 +00:00
// 双击打开
2023-02-26 14:44:00 +00:00
connect(this, &QTreeView::doubleClicked, this, &VisiableProjectController::explicit_active_document);
2022-11-17 08:26:05 +00:00
}
2023-02-26 14:44:00 +00:00
void Components::VisiableProjectController::explicit_active_document(const QModelIndex &curr)
2022-11-17 08:26:05 +00:00
{
if(!curr.isValid())
return;
2023-02-26 14:44:00 +00:00
auto info = pjtins->queryInfo(curr);
if(info.isFile())
emit this->activeDocument(info.canonicalFilePath(), curr.data().toString());
2022-11-17 08:26:05 +00:00
}
2023-02-26 14:44:00 +00:00
void Components::VisiableProjectController::contexMenuPopup(const QPoint &pos)
2022-11-17 08:26:05 +00:00
{
auto idx_it = indexAt(pos);
if(!idx_it.isValid())
return;
QMenu popup;
auto npath = popup.addAction("新建路径");
connect(npath, &QAction::triggered, [this](){
auto packages = QInputDialog::getText(this, "输入包路径名称", "名称A#名称B#名称C");
if(packages == "")
return;
QList<QString> names;
QRegExp exp("([^#]+)");
auto idx=0;
while ((idx = exp.indexIn(packages, idx)) != -1) {
names << exp.capturedTexts()[1];
idx += names.last().length();
}
2023-02-26 14:44:00 +00:00
pjtins->newPackage(names);
2022-11-17 08:26:05 +00:00
});
popup.addSeparator();
auto newm = popup.addMenu("新建文件");
newm->addAction("新建章节", [this, &idx_it](){ alloc_file(idx_it, "章节", "txt"); });
2022-11-17 08:26:05 +00:00
newm->addSeparator();
newm->addAction("新建脉络", [this, &idx_it](){ alloc_file(idx_it, "脉络", "storychain"); });
newm->addAction("新建单元", [this, &idx_it](){ alloc_file(idx_it, "单元", "storyunit"); });
newm->addAction("新建故事", [this, &idx_it](){ alloc_file(idx_it, "故事", "storyboard"); });
newm->addAction("新建行文", [this, &idx_it](){ alloc_file(idx_it, "行文", "storyvolume"); });
2022-11-25 01:18:54 +00:00
newm->addAction("新建概念", [this, &idx_it](){ alloc_file(idx_it, "概念", "storyconcept"); });
2022-11-17 08:26:05 +00:00
popup.addSeparator();
popup.addAction("修改名称", [this, &idx_it](){
if(!idx_it.isValid())
return;
auto name = QInputDialog::getText(this, "输入名称", "节点名称");
if(name == "")
return;
auto item = pjtins->model()->itemFromIndex(idx_it);
2022-11-17 08:26:05 +00:00
auto pnode = item->parent();
if(pnode == nullptr){
item->setText(name);
}
else
{
for(auto idx=0; idx<pnode->rowCount(); ++idx)
2023-02-26 14:44:00 +00:00
if(pnode->child(idx)->text() == name) {
2022-11-17 08:26:05 +00:00
QMessageBox::critical(this, "系统提示", "发现重名节点");
return;
}
item->setText(name);
}
});
popup.addSeparator();
popup.addAction("删除节点与文件", [this, &idx_it](){
2023-02-26 14:44:00 +00:00
this->remove_node_tree(idx_it);
2022-11-17 08:26:05 +00:00
});
auto item = this->pjtins->model()->itemFromIndex(idx_it);
if(item->parent() == nullptr){
popup.addSeparator();
2023-02-26 14:44:00 +00:00
popup.addAction("项目配置", this, &VisiableProjectController::project_configration);
}
2022-11-17 08:26:05 +00:00
popup.exec(mapToGlobal(pos));
}
2023-02-26 14:44:00 +00:00
QRandomGenerator gen_x;
void Components::VisiableProjectController::alloc_file(const QModelIndex &idx_it, const QString &type, const QString &suffix)
2022-11-17 08:26:05 +00:00
{
2023-02-26 14:44:00 +00:00
// 输入节点名称
auto name = QInputDialog::getText(this, "输入" + type + "名称", type + "名称:");
2022-11-17 08:26:05 +00:00
if(name == "")
return;
2023-02-26 14:44:00 +00:00
// 通过节点路径获取放置目标文件的文件夹fileinfo
auto target_info = pjtins->queryInfo(idx_it);
QDir target_dir;
if(target_info.isFile())
target_dir = target_info.absoluteDir();
else
target_dir = QDir(target_info.absoluteFilePath());
// 禁止重名
QString new_name = "";
do {
new_name = QString("contentfile_%1.%2").arg(gen_x.generate64()).arg(suffix);
}while (target_dir.exists(new_name));
auto relative_file = target_dir.relativeFilePath(new_name);
auto absolute_file = target_dir.absoluteFilePath(new_name);
QFile xfile(absolute_file);
if(!xfile.open(QIODevice::WriteOnly))
throw new std::exception(QString("无法创建指定文件:" + absolute_file).toLocal8Bit());
pjtins->signFile(idx_it, name, relative_file);
emit this->activeDocument(absolute_file, name);
2022-11-17 08:26:05 +00:00
}
2023-02-26 14:44:00 +00:00
void Components::VisiableProjectController::project_configration()
{
QDialog dia(this);
auto layout = new QVBoxLayout(&dia);
auto tab = new QTabWidget(&dia);
tab->setTabPosition(QTabWidget::West);
layout->addWidget(tab);
auto extions = rtcore->extensions();
auto project = pjtins;
for(auto &ext : extions){
2023-02-26 14:44:00 +00:00
// auto widget = ext->getNewPanel(project->configraions());
// if(widget)
// tab->addTab(widget, ext->extensionName());
}
dia.exec();
}
2023-02-26 14:44:00 +00:00
void Components::VisiableProjectController::remove_node_tree(const QModelIndex &idx_it)
{
if(!idx_it.isValid())
{
QMessageBox::critical(this, "系统提示", "指定的路径无效");
return;
}
auto btn = QMessageBox::warning(this, "系统提示",
"删除操作涉及文件变更,无法找回,请慎重对待!\n"
"是否坚持删除指定节点以及包含的所有文件?",
QMessageBox::Yes|QMessageBox::No);
if(btn == QMessageBox::No)
return;
auto file_list = pjtins->filesGather(idx_it);
emit this->aboutToBoDelete(file_list);
this->pjtins->deleteTarget(idx_it);
}
QList<QStandardItem *> Components::VisiableProjectController::nodes_contained(QStandardItem *root)
2022-11-29 03:47:12 +00:00
{
QList<QStandardItem *> coll;
coll << root;
for(auto idx=0; idx<root->rowCount(); ++idx)
{
coll << root->child(idx);
coll.append(nodes_contained(root->child(idx)));
}
return coll;
}