Skip to content

Commit

Permalink
Merge pull request #1620 from 1445643474/master
Browse files Browse the repository at this point in the history
Import Material values File
  • Loading branch information
zhxx1987 authored Dec 12, 2023
2 parents 9a04700 + a38c588 commit 39990e0
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 57 deletions.
7 changes: 6 additions & 1 deletion ui/zenoedit/dialog/zforksubgrapdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@

#include <QtWidgets>
#include "zenoui/comctrl/dialog/zframelessdialog.h"
#include <rapidjson/document.h>

class ZForkSubgraphDlg : public ZFramelessDialog
{
Q_OBJECT
public:
ZForkSubgraphDlg(const QMap<QString, QString> & subgs, QWidget* parent = nullptr);
signals:

private slots:
void onOkClicked();
void onImportClicked();
private:
void initUi();
private:
QString m_version;
QTableWidget* m_pTableWidget;
QMap<QString, QString> m_subgsMap;
QString m_importPath;
QPushButton* m_pImportBtn;
};

#endif
132 changes: 101 additions & 31 deletions ui/zenoedit/dialog/zforksubgraphdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include "zenoapplication.h"
#include <zenomodel/include/graphsmanagment.h>
#include <zenomodel/include/igraphsmodel.h>
#include <zenomodel/include/nodeparammodel.h>
#include <zenomodel/include/uihelper.h>
#include "variantptr.h"
#include "nodesview/zenographseditor.h"
#include "nodesys/zenosubgraphscene.h"
#include "zenomainwindow.h"

ZForkSubgraphDlg::ZForkSubgraphDlg(const QMap<QString, QString>& subgs, QWidget* parent)
: ZFramelessDialog(parent)
Expand Down Expand Up @@ -52,45 +58,109 @@ void ZForkSubgraphDlg::initUi()
QHBoxLayout* pHLayout = new QHBoxLayout(this);
QPushButton* pOkBtn = new QPushButton(tr("Ok"), this);
QPushButton* pCancelBtn = new QPushButton(tr("Cancel"), this);
m_pImportBtn = new QPushButton(tr("Import Material File"), this);
int width = ZenoStyle::dpiScaled(80);
pOkBtn->setFixedWidth(width);
pCancelBtn->setFixedWidth(width);
int height = ZenoStyle::dpiScaled(24);
pOkBtn->setFixedSize(width, height);
pCancelBtn->setFixedSize(width, height);
m_pImportBtn->setFixedHeight(height);
pHLayout->addWidget(m_pImportBtn);
pHLayout->addStretch();
pHLayout->addWidget(pOkBtn);
pHLayout->addWidget(pCancelBtn);
pLayout->addLayout(pHLayout);
this->setMainWidget(pWidget);

connect(pOkBtn, &QPushButton::clicked, this, [=]() {
int count = m_pTableWidget->rowCount();
int rowNum = qSqrt(count);
int colunmNum = count / (rowNum > 0 ? rowNum : 1);
QPointF pos;
for (int row = 0; row < count; row++)
connect(pOkBtn, &QPushButton::clicked, this, &ZForkSubgraphDlg::onOkClicked);
connect(pCancelBtn, &QPushButton::clicked, this, &ZForkSubgraphDlg::reject);
connect(m_pImportBtn, &QPushButton::clicked, this, &ZForkSubgraphDlg::onImportClicked);
}

void ZForkSubgraphDlg::onImportClicked()
{
m_importPath = QFileDialog::getOpenFileName(nullptr, "File to Open", "", "All Files(*);;");
QStringList strLst = m_importPath.split("/");
if (!strLst.isEmpty())
m_pImportBtn->setText(tr("Material File: ") + strLst.last());
}

void ZForkSubgraphDlg::onOkClicked()
{
ZenoMainWindow* pWin = zenoApp->getMainWindow();
ZASSERT_EXIT(pWin);
ZenoGraphsEditor* pEditor = pWin->getAnyEditor();
ZASSERT_EXIT(pEditor);
ZenoSubGraphView* pView = pEditor->getCurrentSubGraphView();
ZASSERT_EXIT(pView);
auto sugIdx = pView->scene()->subGraphIndex();
ZASSERT_EXIT(sugIdx.isValid());
int count = m_pTableWidget->rowCount();
int rowNum = qSqrt(count);
int colunmNum = count / (rowNum > 0 ? rowNum : 1);
QPointF pos;
for (int row = 0; row < count; row++)
{
QString subgName = m_pTableWidget->item(row, 0)->data(Qt::DisplayRole).toString();
QString name = m_pTableWidget->item(row, 1)->data(Qt::DisplayRole).toString();
QString mtlid = m_pTableWidget->item(row, 2)->data(Qt::DisplayRole).toString();
QString old_mtlid = m_pTableWidget->item(row, 2)->data(Qt::UserRole).toString();
IGraphsModel* pGraphsModel = zenoApp->graphsManagment()->currentModel();
const QModelIndex& index = pGraphsModel->forkMaterial(sugIdx, pGraphsModel->index(subgName), name, mtlid, old_mtlid);
if (!index.isValid())
{
QString subgName = m_pTableWidget->item(row, 0)->data(Qt::DisplayRole).toString();
QString name = m_pTableWidget->item(row, 1)->data(Qt::DisplayRole).toString();
QString mtlid = m_pTableWidget->item(row, 2)->data(Qt::DisplayRole).toString();
QString old_mtlid = m_pTableWidget->item(row, 2)->data(Qt::UserRole).toString();
IGraphsModel* pGraphsModel = zenoApp->graphsManagment()->currentModel();
const QModelIndex& index = pGraphsModel->forkMaterial(pGraphsModel->index(subgName), name, mtlid, old_mtlid);
if (!index.isValid())
{
QMessageBox::warning(this, tr("warring"), tr("fork preset subgraph '%1' failed.").arg(name));
}
if (row > 0)
{
int currC = row / rowNum;
int currR = row % rowNum;
QPointF newPos(pos.x() + currC * 600, pos.y() + currR * 600);
pGraphsModel->ModelSetData(index, newPos, ROLE_OBJPOS);
}
else
QMessageBox::warning(this, tr("warring"), tr("fork preset subgraph '%1' failed.").arg(name));
continue;
}
if (row > 0)
{
int currC = row / rowNum;
int currR = row % rowNum;
QPointF newPos(pos.x() + currC * 600, pos.y() + currR * 600);
pGraphsModel->ModelSetData(index, newPos, ROLE_OBJPOS);
}
else
{
pos = index.data(ROLE_OBJPOS).toPointF();
}

if (m_importPath.isEmpty())
continue;
QFile file(m_importPath);
bool ret = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (!ret) {
zeno::log_error("cannot open file: {} ({})", m_importPath.toStdString(),
file.errorString().toStdString());
continue;
}

rapidjson::Document doc;
QByteArray bytes = file.readAll();
doc.Parse(bytes);

if (!doc.IsObject())
{
zeno::log_error("json file is corrupted");
continue;
}
auto jsonObject = doc.GetObject();

if (jsonObject.HasMember(mtlid.toUtf8()))
{
const auto& objVal = jsonObject[mtlid.toStdString().c_str()];
NodeParamModel* nodeParams = QVariantPtr<NodeParamModel>::asPtr(index.data(ROLE_NODE_PARAMS));
if (nodeParams)
{
pos = index.data(ROLE_OBJPOS).toPointF();
for (const auto& inputIdx : nodeParams->getInputIndice())
{
QString name = inputIdx.data(ROLE_PARAM_NAME).toString();
if (objVal.HasMember(name.toUtf8()))
{
QVariant val = UiHelper::parseJson(objVal[name.toStdString().c_str()]);
pGraphsModel->ModelSetData(inputIdx, val, ROLE_PARAM_VALUE);
}
}
}
}
accept();
});
connect(pCancelBtn, &QPushButton::clicked, this, &ZForkSubgraphDlg::reject);
}
}
accept();
}
26 changes: 13 additions & 13 deletions ui/zenoedit/nodesview/zenographseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void ZenoGraphsEditor::initSignals()
connect(m_ui->graphsViewTab, &QTabWidget::currentChanged, this, [=](int index) {
if (m_ui->graphsViewTab->tabText(index).compare("main", Qt::CaseInsensitive) == 0)
{
ZenoSettingsManager::GetInstance().setValue(zsSubgraphType, SUBGRAPH_NOR);
closeMaterialTab();
}
});
connect(m_ui->searchEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onSearchEdited(const QString&)));
Expand All @@ -137,6 +137,16 @@ void ZenoGraphsEditor::initSignals()
ZenoSettingsManager::GetInstance().setValue("zencache-enable", true);
}

connect(&ZenoSettingsManager::GetInstance(), &ZenoSettingsManager::valueChanged, this, [=](QString zsName) {
if (zsName == zsSubgraphType)
{
int type = ZenoSettingsManager::GetInstance().getValue(zsName).toInt();
m_ui->label->setText(type == SUBGRAPH_TYPE::SUBGRAPH_NOR ? tr("Subnet") : type == SUBGRAPH_TYPE::SUBGRAPH_METERIAL ? tr("Material Subnet") : tr("Preset Subnet"));
if (type != SUBGRAPH_METERIAL)
closeMaterialTab();
}
});

//m_selection->setCurrentIndex(m_sideBarModel->index(0, 0), QItemSelectionModel::SelectCurrent);
}

Expand Down Expand Up @@ -189,18 +199,8 @@ void ZenoGraphsEditor::resetModel(IGraphsModel* pModel)
connect(pModel, &IGraphsModel::modelClear, this, &ZenoGraphsEditor::onModelCleared);
connect(pModel, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)), this, SLOT(onSubGraphsToRemove(const QModelIndex&, int, int)));
connect(pModel, SIGNAL(modelReset()), this, SLOT(onModelReset()));
connect(pModel, SIGNAL(graphRenamed(const QString&, const QString&)), this, SLOT(onSubGraphRename(const QString&, const QString&)));
connect(&ZenoSettingsManager::GetInstance(), &ZenoSettingsManager::valueChanged, this, [=](QString zsName) {
if (zsName == zsSubgraphType)
{
proxyModel->invalidate();
int type = ZenoSettingsManager::GetInstance().getValue(zsName).toInt();
m_ui->label->setText(type == SUBGRAPH_TYPE::SUBGRAPH_NOR ? tr("Subnet") : type == SUBGRAPH_TYPE::SUBGRAPH_METERIAL ? tr("Material Subnet") : tr("Preset Subnet"));
if (type != SUBGRAPH_METERIAL)
closeMaterialTab();
}
});
activateTab("main");
connect(pModel, SIGNAL(graphRenamed(const QString&, const QString&)), this, SLOT(onSubGraphRename(const QString&, const QString&)));
activateTab("main");
}

void ZenoGraphsEditor::onModelCleared()
Expand Down
6 changes: 6 additions & 0 deletions ui/zenoedit/nodesview/zsubnetlistitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ void ZSubnetListItemDelegate::setSelectedIndexs(const QModelIndexList &list)

SubListSortProxyModel::SubListSortProxyModel(QObject* parent) : QSortFilterProxyModel(parent)
{
connect(&ZenoSettingsManager::GetInstance(), &ZenoSettingsManager::valueChanged, this, [=](QString zsName) {
if (zsName == zsSubgraphType)
{
invalidate();
}
});
}

bool SubListSortProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
Expand Down
2 changes: 1 addition & 1 deletion ui/zenoedit/nodesys/zenonode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ void ZenoNode::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
const auto& paramIdx = pNodeParams->getParam(PARAM_INPUT, "mtlid");
ZASSERT_EXIT(paramIdx.isValid());
QString mtlid = paramIdx.data(ROLE_PARAM_VALUE).toString();
if (!pGraphsModel->newMaterialSubgraph(mtlid, this->pos() + QPointF(800, 0)))
if (!pGraphsModel->newMaterialSubgraph(m_subGpIndex, mtlid, this->pos() + QPointF(800, 0)))
QMessageBox::warning(nullptr, tr("Info"), tr("Create material subgraph '%1' failed.").arg(mtlid));
});
QMenu *nodeMenu = new QMenu;
Expand Down
11 changes: 10 additions & 1 deletion ui/zenoedit/panel/zenospreadsheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "dialog/zforksubgrapdlg.h"
#include "nodesview/zenographseditor.h"
#include "settings/zenosettingsmanager.h"
#include "nodesys/zenosubgraphscene.h"

ZenoSpreadsheet::ZenoSpreadsheet(QWidget *parent) : QWidget(parent) {
dataModel = new PrimAttrTableModel();
Expand Down Expand Up @@ -254,9 +255,17 @@ bool ZenoSpreadsheet::eventFilter(QObject* watched, QEvent* event)
}

connect(newSubGraph, &QAction::triggered, this, [=]() {
ZenoMainWindow* pWin = zenoApp->getMainWindow();
ZASSERT_EXIT(pWin);
ZenoGraphsEditor* pEditor = pWin->getAnyEditor();
ZASSERT_EXIT(pEditor);
ZenoSubGraphView* pView = pEditor->getCurrentSubGraphView();
ZASSERT_EXIT(pView);
auto sugIdx = pView->scene()->subGraphIndex();
ZASSERT_EXIT(sugIdx.isValid());
for (const auto& mtlid : matLst)
{
if (!pGraphsModel->newMaterialSubgraph(mtlid, QPointF(800, 0)))
if (!pGraphsModel->newMaterialSubgraph(sugIdx, mtlid, QPointF(800, 0)))
QMessageBox::warning(nullptr, tr("Info"), tr("Create material subgraph '%1' failed.").arg(mtlid));
}
});
Expand Down
2 changes: 1 addition & 1 deletion ui/zenoedit/settings/zenosettingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ZenoSettingsManager::ZenoSettingsManager(QObject *parent) :

bool ZenoSettingsManager::setValue(const QString& name, const QVariant& value)
{
if (zsSubgraphType == name)
if (zsSubgraphType == name && value != m_settings[name])
{
m_settings[name] = value;
emit valueChanged(name);
Expand Down
4 changes: 2 additions & 2 deletions ui/zenomodel/include/igraphsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class IGraphsModel : public QAbstractItemModel
fork subnet node indexed by subnetNodeIdx under subgIdx.
*/
virtual QModelIndex fork(const QModelIndex& subgIdx, const QModelIndex& subnetNodeIdx) = 0;
virtual QModelIndex forkMaterial(const QModelIndex& subnetNodeIdx, const QString& subgName, const QString& mtlid, const QString& mtlid_old) = 0;
virtual QModelIndex forkMaterial(const QModelIndex& subgIdx, const QModelIndex& subnetNodeIdx, const QString& subgName, const QString& mtlid, const QString& mtlid_old) = 0;

virtual void updateParamInfo(const QString& id, PARAM_UPDATE_INFO info, const QModelIndex& subGpIdx, bool enableTransaction = false) = 0;
virtual void updateSocketDefl(const QString& id, PARAM_UPDATE_INFO info, const QModelIndex& subGpIdx, bool enableTransaction = false) = 0;
Expand All @@ -76,7 +76,7 @@ class IGraphsModel : public QAbstractItemModel
virtual void redo() = 0;
virtual void switchSubGraph(const QString& graphName) {}
virtual void newSubgraph(const QString& graphName, SUBGRAPH_TYPE type = SUBGRAPH_TYPE::SUBGRAPH_NOR) = 0;
virtual bool newMaterialSubgraph(const QString& graphName, const QPointF& pos) = 0;
virtual bool newMaterialSubgraph(const QModelIndex& subgIdx, const QString& graphName, const QPointF& pos) = 0;
virtual void initMainGraph() = 0;
virtual void renameSubGraph(const QString& oldName, const QString& newName) = 0;
virtual bool isDirty() const = 0;
Expand Down
8 changes: 3 additions & 5 deletions ui/zenomodel/src/graphsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void GraphsModel::newSubgraph(const QString &graphName, SUBGRAPH_TYPE type)
}
}

bool GraphsModel::newMaterialSubgraph(const QString& graphName, const QPointF& pos)
bool GraphsModel::newMaterialSubgraph(const QModelIndex & currentSubIdx, const QString& graphName, const QPointF& pos)
{
if (subGraph(graphName))
{
Expand All @@ -129,7 +129,6 @@ bool GraphsModel::newMaterialSubgraph(const QString& graphName, const QPointF& p
}
beginTransaction("extract a new graph");

int currentRow = m_selection->currentIndex().row();
//first, new the target subgraph
newSubgraph(graphName, SUBGRAPH_TYPE::SUBGRAPH_METERIAL);
QModelIndex subgIdx = index(graphName);
Expand All @@ -155,7 +154,6 @@ bool GraphsModel::newMaterialSubgraph(const QString& graphName, const QPointF& p
link.outSockPath = inParam.data(ROLE_OBJPATH).toString();
addLink(subgIdx, link, false);
//add material subgraph node
QModelIndex currentSubIdx = index(currentRow, 0);
const QString& subIdent = NodesMgr::createNewNode(this, currentSubIdx, graphName, pos);
auto subgNodeIdx = nodeIndex(subIdent);
QVariant newValue = OPT_VIEW;
Expand Down Expand Up @@ -857,13 +855,13 @@ QModelIndex GraphsModel::fork(const QModelIndex& subgIdx, const QModelIndex &sub
return newForkNodeIdx;
}

QModelIndex GraphsModel::forkMaterial(const QModelIndex& subnetNodeIdx, const QString& subgName, const QString& mtlid, const QString& mtlid_old)
QModelIndex GraphsModel::forkMaterial(const QModelIndex& currSubgIdx, const QModelIndex& subnetNodeIdx, const QString& subgName, const QString& mtlid, const QString& mtlid_old)
{
if (subGraph(subgName))
return QModelIndex();
if (!subnetNodeIdx.isValid())
return QModelIndex();
QModelIndex index = fork(m_selection->currentIndex(), subnetNodeIdx);
QModelIndex index = fork(currSubgIdx, subnetNodeIdx);
ModelSetData(index, OPT_VIEW, ROLE_OPTIONS);

QString name = index.data(ROLE_OBJNAME).toString();
Expand Down
4 changes: 2 additions & 2 deletions ui/zenomodel/src/graphsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GraphsModel : public IGraphsModel
SubGraphModel *currentGraph();
void switchSubGraph(const QString& graphName) override;
void newSubgraph(const QString& graphName, SUBGRAPH_TYPE type = SUBGRAPH_TYPE::SUBGRAPH_NOR) override;
bool newMaterialSubgraph(const QString& graphName, const QPointF& pos) override;
bool newMaterialSubgraph(const QModelIndex& subgIdx, const QString& graphName, const QPointF& pos) override;
void initMainGraph() override;
void renameSubGraph(const QString& oldName, const QString& newName) override;
QItemSelectionModel* selectionModel() const;
Expand All @@ -44,7 +44,7 @@ class GraphsModel : public IGraphsModel
//NODE_DESC
void appendSubGraph(SubGraphModel* pGraph);
QModelIndex fork(const QModelIndex& subgIdx, const QModelIndex& subnetNodeIdx) override;
QModelIndex forkMaterial(const QModelIndex& subnetNodeIdx, const QString&subgName, const QString& mtlid, const QString& mtlid_old) override;
QModelIndex forkMaterial(const QModelIndex& subgIdx, const QModelIndex& subnetNodeIdx, const QString&subgName, const QString& mtlid, const QString& mtlid_old) override;
void removeGraph(int idx) override;
bool isDirty() const override;
void markDirty() override;
Expand Down

0 comments on commit 39990e0

Please sign in to comment.