Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1600 procedure model issues #1657

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
4 changes: 1 addition & 3 deletions src/gui/configurationTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ class ConfigurationTab : public QWidget, public MainTab
* Signals / Slots
*/
private slots:
// Generate
void on_GenerateButton_clicked(bool checked);
// Density units changed
void on_DensityUnitsCombo_currentIndexChanged(int index);
void buttonGroupToggled(QAbstractButton *button, bool checked);
void on_ConfigurationButtonGroup_buttonToggled(QAbstractButton *button, bool checked);
};
6 changes: 1 addition & 5 deletions src/gui/configurationTabFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ ConfigurationTab::ConfigurationTab(DissolveWindow *dissolveWindow, Dissolve &dis

ui_.GlobalPotentialsFrame->setHidden(true);
ui_.TargetedPotentialsFrame->setHidden(true);
connect(ui_.ConfigurationButtonGroup, SIGNAL(buttonToggled(QAbstractButton *, bool)), this,
SLOT(buttonGroupToggled(QAbstractButton *, bool)));
}

/*
Expand Down Expand Up @@ -212,11 +210,9 @@ void ConfigurationTab::on_GenerateButton_clicked(bool checked)
dissolveWindow_->updateStatusBar();
}

// Density units changed
void ConfigurationTab::on_DensityUnitsCombo_currentIndexChanged(int index) { updateDensityLabel(); }

// Button group toggled
void ConfigurationTab::buttonGroupToggled(QAbstractButton *button, bool checked)
void ConfigurationTab::on_ConfigurationButtonGroup_buttonToggled(QAbstractButton *button, bool checked)
{
if (button == ui_.GeneratorPushButton)
{
Expand Down
88 changes: 75 additions & 13 deletions src/gui/models/procedureModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,34 @@

ProcedureModel::ProcedureModel(OptionalReferenceWrapper<Procedure> procedure) : procedure_(procedure) {}

void printSequence(const ProcedureNodeSequence &sequence, std::string indent)
{
fmt::print("{}SEQ {}\n", indent, fmt::ptr(&sequence));
auto nodeIndent = fmt::format("{} ", indent);
for (auto &node : sequence.sequence())
{
fmt::print("{} N {}\n", nodeIndent, fmt::ptr(&node));
fmt::print("{} = {}\n", nodeIndent, node->name());
if (node->branch())
printSequence(node->branch()->get(), fmt::format("{} ", indent));
}
}
void printProcedure(Procedure &proc)
{
fmt::print("Procedure:\n");
printSequence(proc.rootSequence(), "");
}
std::string modelIndexString(const QModelIndex &index)
{
return index.isValid() ? fmt::format("({},{})", index.row(), index.column()) : "INVALID";
}

// Set source Procedure
void ProcedureModel::setData(Procedure &procedure)
{
beginResetModel();
procedure_ = procedure;
printProcedure(procedure);
endResetModel();
}

Expand Down Expand Up @@ -58,7 +81,10 @@ int ProcedureModel::rowCount(const QModelIndex &parent) const
// If the index doesn't have a valid internal pointer we're probing the root of the model, so return the number of root
// sequence nodes
if (!parent.internalPointer())
{
fmt::print("RowCount for parent {} = {} (no valid internalPointer())\n", modelIndexString(parent), procedure_->get().rootSequence().nNodes());
return procedure_->get().rootSequence().nNodes();
}

auto node = static_cast<ProcedureNode *>(parent.internalPointer());
if (node && node->branch())
Expand Down Expand Up @@ -383,6 +409,9 @@ bool ProcedureModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
auto oldParent = parent(oldIndex);
auto &oldScope = oldNode->scope()->get();

fmt::print("dropMimeData::MoveAction old model index is {}\n", modelIndexString(oldIndex));
fmt::print("dropMimeData::MoveAction old model sequence is {}\n", fmt::ptr(&oldScope));

// Get the new parent scope
auto optNewScope = getScope(newParent);
if (!optNewScope)
Expand All @@ -392,6 +421,10 @@ bool ProcedureModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
// Determine the new index of the dragged node in the root sequence or scope
auto insertAtRow = row == -1 ? newScope.nNodes() : row;

fmt::print("dropMimeData::MoveAction new parent model index is {}\n", modelIndexString(newParent));
fmt::print("dropMimeData::MoveAction new model sequence is {}\n", fmt::ptr(&newScope));
fmt::print("dropMimeData::MoveAction new model row insertion index is {}\n", insertAtRow);

// Create a new row to store the data.
insertRows(insertAtRow, 1, newParent);

Expand All @@ -406,14 +439,8 @@ bool ProcedureModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
newScope.sequence()[insertAtRow] = std::move(oldScope.sequence()[oldNodeRow]);
oldNode->setScope(newScope);

// Remove the old row
beginRemoveRows(oldParent, oldNodeRow, oldNodeRow);
oldScope.sequence().erase(oldScope.sequence().begin() + oldNodeRow);
endRemoveRows();

// Set the new data - we call this just to emit dataChanged() from the correct place.
auto idx = index(insertAtRow, 0, newParent);
return setData(idx, QVariant::fromValue(mimeData->node()), ProcedureModelAction::MoveInternal);
//printProcedure(procedure_->get());
return true;
}
else if (action == Qt::CopyAction && data->hasFormat("application/dissolve.procedure.newNode"))
{
Expand Down Expand Up @@ -467,12 +494,47 @@ bool ProcedureModel::removeRows(int row, int count, const QModelIndex &parent)

// Get the scope associated to the parent index
auto scope = getScope(parent);

beginRemoveRows(parent, row, row + count - 1);
for (auto i = 0; i < count; ++i)
scope->get().removeNode(data(index(row + i, 0), Qt::UserRole).value<std::shared_ptr<ProcedureNode>>());
beginRemoveRows(parent, row, row);
if (count > 1)
scope->get().sequence().erase(scope->get().sequence().begin() + row, scope->get().sequence().begin() + row + count - 1);
else
scope->get().sequence().erase(scope->get().sequence().begin() + row);
endRemoveRows();

emit(dataChanged(QModelIndex(), QModelIndex()));
return true;
}

QModelIndex ProcedureModel::appendNew(const QString &nodeTypeString)
{
// Check the node type string is valid
if (!ProcedureNode::nodeTypes().isValid(nodeTypeString.toStdString()))
return {};

// Convert the node type string to its enumeration
auto nodeType = ProcedureNode::nodeTypes().enumeration(nodeTypeString.toStdString());
// Create a node of the node type
auto node = ProcedureNodeRegistry::create(nodeType);
assert(node);

// Get the parent scope, we know this is the root sequence, but use getScope for consistency
auto scope = getScope(QModelIndex());

// Check if the node is relevant to the context of the scope
if (!node->isContextRelevant(scope->get().context()))
return {};

// Get the target row for the new node
auto insertAtRow = rowCount();

// Create a new row to store the data. Don't use insertRows() here since creating a null node in the vector at this
// point causes no end of issues.
beginInsertRows(QModelIndex(), insertAtRow, insertAtRow);
scope->get().appendNode(node, insertAtRow);
endInsertRows();
auto idx = index(insertAtRow, 0, QModelIndex());

// Call setData() so we emit the right signals
setData(idx, QVariant::fromValue(node), ProcedureModelAction::CreateNew);

return idx;
}
1 change: 1 addition & 0 deletions src/gui/models/procedureModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ProcedureModel : public QAbstractItemModel
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &newParent) override;
bool insertRows(int row, int count, const QModelIndex &parent) override;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
QModelIndex appendNew(const QString &nodeTypeString);

signals:
void nodeNameChanged(const QModelIndex &, const QString &oldName, const QString &newName);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/procedureWidgetFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void ProcedureWidget::on_NodesTree_customContextMenuRequested(const QPoint &pos)

void ProcedureWidget::on_AvailableNodesTree_doubleClicked(const QModelIndex &index)
{
// nodeLayerModel_.appendNew(nodePaletteModel_.data(index, Qt::DisplayRole).toString());
procedureModel_.appendNew(nodePaletteFilterProxy_.data(index, Qt::DisplayRole).toString());
}

// Delete the currently selected node, and its children
Expand Down
13 changes: 0 additions & 13 deletions src/procedure/nodes/sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ int ProcedureNodeSequence::nNodes() const { return sequence_.size(); }
// Return whether the sequence is empty
bool ProcedureNodeSequence::empty() const { return sequence_.empty(); }

// Remove a node
bool ProcedureNodeSequence::removeNode(NodeRef node)
{
// Find the node in the sequence
auto it = std::find_if(sequence_.begin(), sequence_.end(), [node](const auto &n) { return n.get() == node.get(); });
if (it != sequence_.end())
{
sequence_.erase(it);
return true;
}
return false;
}

/*
* Scope
*/
Expand Down
2 changes: 0 additions & 2 deletions src/procedure/nodes/sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class ProcedureNodeSequence : public Serialisable<const CoreData &>
int nNodes() const;
// Return whether the sequence is empty
bool empty() const;
// Remove a node
bool removeNode(NodeRef node);

/*
* Scope
Expand Down
3 changes: 0 additions & 3 deletions src/procedure/procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ std::vector<ConstNodeRef> Procedure::nodes(std::optional<ProcedureNode::NodeType
return rootSequence_.nodes(optNodeType, optNodeClass);
}

// Remove a node
bool Procedure::removeNode(NodeRef node) { return rootSequence_.removeNode(node); }

/*
* Execute
*/
Expand Down
3 changes: 0 additions & 3 deletions src/procedure/procedure.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class Procedure : public Serialisable<const CoreData &>
std::vector<ConstNodeRef> nodes(std::optional<ProcedureNode::NodeType> optNodeType = std::nullopt,
std::optional<ProcedureNode::NodeClass> optNodeClass = std::nullopt) const;

// Remove a node
bool removeNode(NodeRef node);

/*
* Execute
*/
Expand Down