From 325ad3072c4a63af65e499a24340ddb8d7f7d096 Mon Sep 17 00:00:00 2001 From: iaomw Date: Fri, 9 Jun 2023 21:47:03 +0800 Subject: [PATCH] RelativePath node --- ui/zenoedit/dialog/zeditparamlayoutdlg.cpp | 1 + ui/zenoedit/nodesys/zenonode.cpp | 1 + ui/zenomodel/include/modeldata.h | 1 + ui/zenomodel/src/graphsmanagment.cpp | 3 + ui/zenomodel/src/uihelper.cpp | 14 ++++ ui/zenoui/comctrl/gv/zenoparamwidget.cpp | 17 ++++- ui/zenoui/comctrl/gv/zitemfactory.cpp | 1 + ui/zenoui/comctrl/zpathedit.cpp | 19 +++++- ui/zenoui/comctrl/zwidgetfactory.cpp | 2 + zeno/src/nodes/StringNodes.cpp | 79 ++++++++++++++++++++++ 10 files changed, 134 insertions(+), 4 deletions(-) diff --git a/ui/zenoedit/dialog/zeditparamlayoutdlg.cpp b/ui/zenoedit/dialog/zeditparamlayoutdlg.cpp index bbb349e06c..7e57040abd 100644 --- a/ui/zenoedit/dialog/zeditparamlayoutdlg.cpp +++ b/ui/zenoedit/dialog/zeditparamlayoutdlg.cpp @@ -26,6 +26,7 @@ static CONTROL_ITEM_INFO controlList[] = { {"Boolean", CONTROL_BOOL, "bool", ":/icons/parameter_control_boolean.svg"}, {"Multiline String", CONTROL_MULTILINE_STRING, "string", ":/icons/parameter_control_string.svg"}, {"read path", CONTROL_READPATH, "string", ":/icons/parameter_control_fold.svg"}, + {"relative path", CONTROL_RELATIVE_PATH, "string", ":/icons/parameter_control_fold.svg"}, {"write path", CONTROL_WRITEPATH, "string", ":/icons/parameter_control_fold.svg"}, {"Enum", CONTROL_ENUM, "string", ":/icons/parameter_control_enum.svg"}, {"Float Vector 4", CONTROL_VEC4_FLOAT, "vec4f", ":/icons/parameter_control_floatVector4.svg"}, diff --git a/ui/zenoedit/nodesys/zenonode.cpp b/ui/zenoedit/nodesys/zenonode.cpp index de83ba9ec8..2522cf5f67 100644 --- a/ui/zenoedit/nodesys/zenonode.cpp +++ b/ui/zenoedit/nodesys/zenonode.cpp @@ -909,6 +909,7 @@ ZGraphicsLayout* ZenoNode::addParam(const QModelIndex& viewparamIdx, ZenoSubGrap case CONTROL_VEC4_INT: case CONTROL_ENUM: case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_WRITEPATH: case CONTROL_MULTILINE_STRING: case CONTROL_PURE_COLOR: diff --git a/ui/zenomodel/include/modeldata.h b/ui/zenomodel/include/modeldata.h index c42d6f9cf4..0f6f369149 100644 --- a/ui/zenomodel/include/modeldata.h +++ b/ui/zenomodel/include/modeldata.h @@ -14,6 +14,7 @@ enum PARAM_CONTROL { CONTROL_ENUM, CONTROL_WRITEPATH, CONTROL_READPATH, + CONTROL_RELATIVE_PATH, CONTROL_MULTILINE_STRING, CONTROL_COLOR, CONTROL_PURE_COLOR, diff --git a/ui/zenomodel/src/graphsmanagment.cpp b/ui/zenomodel/src/graphsmanagment.cpp index f83320dbf4..e3658b3acb 100644 --- a/ui/zenomodel/src/graphsmanagment.cpp +++ b/ui/zenomodel/src/graphsmanagment.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "common_def.h" #include @@ -91,6 +92,8 @@ IGraphsModel* GraphsManagment::openZsgFile(const QString& fn) pModel->clearDirty(); setCurrentModel(pModel); emit fileOpened(fn); + + zeno::setConfigVariable("$ZSGPATH", fn.toStdString()); return pModel; } diff --git a/ui/zenomodel/src/uihelper.cpp b/ui/zenomodel/src/uihelper.cpp index 0c465ca964..992d6d5346 100644 --- a/ui/zenomodel/src/uihelper.cpp +++ b/ui/zenomodel/src/uihelper.cpp @@ -1,5 +1,6 @@ #include "uihelper.h" #include +#include "modeldata.h" #include "modelrole.h" #include "zassert.h" #include "curvemodel.h" @@ -139,6 +140,7 @@ bool UiHelper::validateVariant(const QVariant& var, const QString& type) case CONTROL_STRING: case CONTROL_WRITEPATH: case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_ENUM: return (QVariant::String == varType); case CONTROL_MULTILINE_STRING: @@ -248,6 +250,7 @@ QVariant UiHelper::parseStringByType(const QString &defaultValue, const QString case CONTROL_STRING: case CONTROL_WRITEPATH: case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_MULTILINE_STRING: case CONTROL_COLOR: case CONTROL_ENUM: @@ -298,6 +301,7 @@ QVariant UiHelper::parseTextValue(PARAM_CONTROL editCtrl, const QString& textVal case CONTROL_BOOL: varValue = textValue.isEmpty() ? false : textValue == "true" ? true : (textValue == "false" ? false : (bool)std::stoi(textValue.toStdString())); break; case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_WRITEPATH: case CONTROL_MULTILINE_STRING: case CONTROL_COLOR: @@ -378,6 +382,7 @@ QString UiHelper::getControlDesc(PARAM_CONTROL ctrl) case CONTROL_BOOL: return "Boolean"; case CONTROL_MULTILINE_STRING: return "Multiline String"; case CONTROL_READPATH: return "read path"; + case CONTROL_RELATIVE_PATH: return "relative path"; case CONTROL_WRITEPATH: return "write path"; case CONTROL_ENUM: return "Enum"; case CONTROL_VEC4_FLOAT: return "Float Vector 4"; @@ -426,6 +431,10 @@ PARAM_CONTROL UiHelper::getControlByDesc(const QString& descName) { return CONTROL_READPATH; } + else if (descName == "relative path") + { + return CONTROL_RELATIVE_PATH; + } else if (descName == "write path") { return CONTROL_WRITEPATH; @@ -552,6 +561,7 @@ QStringList UiHelper::getControlLists(const QString& type, bool isNodeUI) else if (type == "vec4i") { ctrls = { CONTROL_VEC4_INT }; } else if (type == "writepath") { ctrls = { CONTROL_WRITEPATH }; } else if (type == "readpath") { ctrls = { CONTROL_READPATH }; } + else if (type == "relativepath") { ctrls = { CONTROL_RELATIVE_PATH }; } else if (type == "multiline_string") { ctrls = { CONTROL_STRING, CONTROL_MULTILINE_STRING }; } else if (type == "color") { //color is more general than heatmap. ctrls = {CONTROL_COLOR, CONTROL_PURE_COLOR}; @@ -605,6 +615,8 @@ PARAM_CONTROL UiHelper::getControlByType(const QString &type) return CONTROL_WRITEPATH; } else if (type == "readpath") { return CONTROL_READPATH; + } else if (type == "relativepath") { + return CONTROL_RELATIVE_PATH; } else if (type == "multiline_string") { return CONTROL_MULTILINE_STRING; } else if (type == "color") { //color is more general than heatmap. @@ -654,6 +666,7 @@ QString UiHelper::getTypeByControl(PARAM_CONTROL ctrl) case CONTROL_VEC4_INT: return "vec4i"; case CONTROL_WRITEPATH: return "string"; case CONTROL_READPATH: return "string"; + case CONTROL_RELATIVE_PATH: return "string"; case CONTROL_COLOR: return "color"; //todo: is vec3? case CONTROL_CURVE: return "curve"; case CONTROL_ENUM: return "string"; @@ -827,6 +840,7 @@ QVariant UiHelper::initVariantByControl(PARAM_CONTROL ctrl) case CONTROL_ENUM: case CONTROL_WRITEPATH: case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_MULTILINE_STRING: case CONTROL_STRING: return ""; diff --git a/ui/zenoui/comctrl/gv/zenoparamwidget.cpp b/ui/zenoui/comctrl/gv/zenoparamwidget.cpp index 9a5b80729a..1e192f1a69 100644 --- a/ui/zenoui/comctrl/gv/zenoparamwidget.cpp +++ b/ui/zenoui/comctrl/gv/zenoparamwidget.cpp @@ -11,6 +11,8 @@ #include "../zpathedit.h" #include +#include +#include ZenoParamWidget::ZenoParamWidget(QGraphicsItem* parent, Qt::WindowFlags wFlags) : QGraphicsProxyWidget(parent, wFlags) @@ -274,7 +276,20 @@ void ZenoParamPathEdit::mousePressEvent(QGraphicsSceneMouseEvent *event) path = QFileDialog::getOpenFileName(nullptr, "File to Open", "", "All Files(*);;"); } else if (m_control == CONTROL_WRITEPATH) { path = QFileDialog::getSaveFileName(nullptr, "Path to Save", "", "All Files(*);;"); - } else { + } + else if (m_control == CONTROL_RELATIVE_PATH) { + + path = QFileDialog::getOpenFileName(nullptr, "File to Open", "", "All Files(*);;"); + std::filesystem::path selectedPath(path.toStdString()); + + auto zpath = zeno::getConfigVariable("$ZSGPATH"); + std::filesystem::path basePath(zpath); + basePath = basePath.remove_filename(); + + auto relative_path = std::filesystem::relative(selectedPath, basePath); + path = QString(relative_path.c_str()); + } + else { path = QFileDialog::getExistingDirectory(nullptr, "Path to Save", ""); } if (path.isEmpty()) { diff --git a/ui/zenoui/comctrl/gv/zitemfactory.cpp b/ui/zenoui/comctrl/gv/zitemfactory.cpp index ace70e4e9c..4c6dfa2086 100644 --- a/ui/zenoui/comctrl/gv/zitemfactory.cpp +++ b/ui/zenoui/comctrl/gv/zitemfactory.cpp @@ -151,6 +151,7 @@ namespace zenoui break; } case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_WRITEPATH: { const QString& path = UiHelper::variantToString(value); diff --git a/ui/zenoui/comctrl/zpathedit.cpp b/ui/zenoui/comctrl/zpathedit.cpp index 163ab85b7c..379f37bd79 100644 --- a/ui/zenoui/comctrl/zpathedit.cpp +++ b/ui/zenoui/comctrl/zpathedit.cpp @@ -1,10 +1,10 @@ #include "zpathedit.h" #include "zlineedit.h" +#include #include #include #include - - +#include ZPathEdit::ZPathEdit(QWidget *parent) : ZLineEdit(parent) @@ -35,7 +35,20 @@ void ZPathEdit::initUI() path = QFileDialog::getOpenFileName(nullptr, "File to Open", "", "All Files(*);;"); } else if (ctrl == CONTROL_WRITEPATH) { path = QFileDialog::getSaveFileName(nullptr, "Path to Save", "", "All Files(*);;"); - } else { + } + else if (ctrl == CONTROL_RELATIVE_PATH) { + + path = QFileDialog::getOpenFileName(nullptr, "File to Open", "", "All Files(*);;"); + std::filesystem::path selectedPath(path.toStdString()); + + auto zpath = zeno::getConfigVariable("$ZSGPATH"); + std::filesystem::path basePath(zpath); + basePath = basePath.remove_filename(); + + auto relative_path = std::filesystem::relative(selectedPath, basePath); + path = QString(relative_path.c_str()); + } + else { path = QFileDialog::getExistingDirectory(nullptr, "Path to Save", ""); } if (path.isEmpty()) { diff --git a/ui/zenoui/comctrl/zwidgetfactory.cpp b/ui/zenoui/comctrl/zwidgetfactory.cpp index ac022b5b6d..dfe73ade2d 100644 --- a/ui/zenoui/comctrl/zwidgetfactory.cpp +++ b/ui/zenoui/comctrl/zwidgetfactory.cpp @@ -59,6 +59,7 @@ namespace zenoui return pCheckbox; } case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_WRITEPATH: { ZPathEdit *pathLineEdit = new ZPathEdit(value.toString()); @@ -373,6 +374,7 @@ namespace zenoui case CONTROL_INT: case CONTROL_FLOAT: return qobject_cast(pControl) != nullptr; //be careful type changed. case CONTROL_READPATH: + case CONTROL_RELATIVE_PATH: case CONTROL_WRITEPATH: return qobject_cast(pControl) != nullptr; case CONTROL_BOOL: return qobject_cast(pControl) != nullptr; case CONTROL_VEC2_FLOAT: diff --git a/zeno/src/nodes/StringNodes.cpp b/zeno/src/nodes/StringNodes.cpp index f233a901ab..73df19128e 100644 --- a/zeno/src/nodes/StringNodes.cpp +++ b/zeno/src/nodes/StringNodes.cpp @@ -1,10 +1,15 @@ +#include +#include #include #include #include #include #include +#include #include +#include "magic_enum.hpp" + namespace zeno { namespace { @@ -38,6 +43,80 @@ ZENDEFNODE(MakeReadPath, { {"string"}, }); + + +struct RelativePath : zeno::INode { + + enum struct BasePath { + ZSGPATH, RUNPATH, NASLOC + }; + + std::string nasPath(std::string& relative) { + + auto zpath = zeno::getConfigVariable("nasloc"); + + std::filesystem::path basePath(zpath); + //basePath = basePath.remove_filename(); + basePath /= relative; + + auto p = std::filesystem::canonical(basePath); + return p.string(); + } + + std::string zsgPath(std::string& relative) { + + auto zpath = zeno::getConfigVariable("$ZSGPATH"); + + std::filesystem::path basePath(zpath); + basePath = basePath.remove_filename(); + basePath /= relative; + + auto p = std::filesystem::canonical(basePath); + return p.string(); + } + + std::string runPath(std::string& relative) { + auto path = std::filesystem::current_path(); + path /= relative; + + auto p = std::filesystem::canonical(path); + return path.string(); + } + + virtual void apply() override { + + auto baseRaw = get_input2("base:"); + auto baseEnum = magic_enum::enum_cast(baseRaw).value_or(BasePath::ZSGPATH); + + zeno::StringObject fullpath; + auto relative = get_input2("relative"); + + switch (baseEnum) { + case BasePath::ZSGPATH: { + fullpath.set(zsgPath(relative)); + } + case BasePath::RUNPATH: { + fullpath.set(runPath(relative)); + } + case BasePath::NASLOC: { + fullpath.set(nasPath(relative)); + } + default: {} + } + + set_output("fullpath", std::make_shared(fullpath)); + } +}; + +ZENDEFNODE(RelativePath, { + { + {"relativepath", "relative", ""} + }, + { {"string", "fullpath"} }, + { {"enum ZSG_PATH RUN_PATH{unsupported} NASLOC{unsupported} ", "base", "ZSG_PATH"} }, + {"string"}, +}); + struct MakeString : zeno::INode { virtual void apply() override { auto obj = std::make_unique();