209 lines
6.3 KiB
C++
209 lines
6.3 KiB
C++
#include "vcontrol_project.h"
|
||
|
||
#include <QInputDialog>
|
||
#include <QMenu>
|
||
#include <QMessageBox>
|
||
#include <QRandomGenerator>
|
||
#include <QVBoxLayout>
|
||
|
||
Components::VisiableProjectController::VisiableProjectController(Core::AppCore *src, Project::ProjectManager *project, QWidget *parent)
|
||
: QTreeView(parent), rtcore(src), pjtins(project)
|
||
{
|
||
setModel(pjtins->model());
|
||
setHeaderHidden(true);
|
||
setIconSize(QSize(24, 24));
|
||
|
||
// 右键菜单
|
||
setContextMenuPolicy(Qt::CustomContextMenu);
|
||
connect(this, &QWidget::customContextMenuRequested, this, &VisiableProjectController::contexMenuPopup);
|
||
|
||
// 双击打开
|
||
connect(this, &QTreeView::doubleClicked, this, &VisiableProjectController::explicit_active_document);
|
||
}
|
||
|
||
void Components::VisiableProjectController::explicit_active_document(const QModelIndex &curr)
|
||
{
|
||
if(!curr.isValid())
|
||
return;
|
||
|
||
auto info = pjtins->queryInfo(curr);
|
||
if(info.isFile())
|
||
emit this->activeDocument(info.canonicalFilePath(), curr.data().toString());
|
||
}
|
||
|
||
void Components::VisiableProjectController::contexMenuPopup(const QPoint &pos)
|
||
{
|
||
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();
|
||
}
|
||
|
||
pjtins->newPackage(names);
|
||
});
|
||
|
||
popup.addSeparator();
|
||
auto newm = popup.addMenu("新建文件");
|
||
newm->addAction("新建章节", [this, &idx_it](){ alloc_file(idx_it, "章节", "txt"); });
|
||
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"); });
|
||
newm->addAction("新建概念", [this, &idx_it](){ alloc_file(idx_it, "概念", "storyconcept"); });
|
||
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);
|
||
auto pnode = item->parent();
|
||
if(pnode == nullptr){
|
||
item->setText(name);
|
||
}
|
||
else
|
||
{
|
||
for(auto idx=0; idx<pnode->rowCount(); ++idx)
|
||
if(pnode->child(idx)->text() == name) {
|
||
QMessageBox::critical(this, "系统提示", "发现重名节点");
|
||
return;
|
||
}
|
||
item->setText(name);
|
||
}
|
||
});
|
||
popup.addSeparator();
|
||
popup.addAction("删除节点与文件", [this, &idx_it](){
|
||
this->remove_node_tree(idx_it);
|
||
});
|
||
|
||
auto item = this->pjtins->model()->itemFromIndex(idx_it);
|
||
if(item->parent() == nullptr){
|
||
popup.addSeparator();
|
||
popup.addAction("项目配置", this, &VisiableProjectController::project_configration);
|
||
}
|
||
|
||
|
||
popup.exec(mapToGlobal(pos));
|
||
}
|
||
|
||
QRandomGenerator gen_x;
|
||
void Components::VisiableProjectController::alloc_file(const QModelIndex &idx_it, const QString &type, const QString &suffix)
|
||
{
|
||
// 输入节点名称
|
||
auto name = QInputDialog::getText(this, "输入" + type + "名称", type + "名称:");
|
||
if(name == "")
|
||
return;
|
||
|
||
// 通过节点路径,获取放置目标文件的文件夹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);
|
||
}
|
||
|
||
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){
|
||
// auto widget = ext->getNewPanel(project->configraions());
|
||
// if(widget)
|
||
// tab->addTab(widget, ext->extensionName());
|
||
}
|
||
|
||
dia.exec();
|
||
}
|
||
|
||
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)
|
||
{
|
||
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;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|