Skip to content

Commit

Permalink
Style issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-cukic committed Nov 26, 2024
1 parent 90505e9 commit 28b670b
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 83 deletions.
1 change: 0 additions & 1 deletion src/ui/Dashboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ Dashboard::Plot::Plot() {
Dashboard::Dashboard(PrivateTag, FlowGraphItem* fgItem, const std::shared_ptr<DashboardDescription>& desc) : m_desc(desc), m_fgItem(fgItem) {
m_desc->lastUsed = std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now());

// TODO: Block pointer
localFlowGraph.plotSinkBlockAddedCallback = [this](Block* b) {
const auto color = std::get_if<uint32_t>(&b->settings().at("color"));
assert(color);
Expand Down
1 change: 0 additions & 1 deletion src/ui/Dashboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ class Dashboard : public std::enable_shared_from_this<Dashboard> {

inline auto& remoteServices() { return m_services; }

// TODO: Block pointer
Block* createSink();
void loadPlotSources();

Expand Down
1 change: 0 additions & 1 deletion src/ui/DashboardPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class DashboardPage {
void drawLegend(Dashboard& dashboard, const Mode& mode) noexcept;
static void drawPlot(Dashboard& dashboard, DigitizerUi::Dashboard::Plot& plot) noexcept;

// TODO: Block pointer
void addSignalCallback(Dashboard& dashboard, Block* block);

//
Expand Down
9 changes: 3 additions & 6 deletions src/ui/Flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ BlockRegistry& BlockRegistry::instance() {
}

const BlockDefinition* BlockRegistry::get(std::string_view id) const {
auto it = m_types.find(id);
return it == m_types.end() ? nullptr : it->second.get();
auto it = _types.find(id);
return it == _types.end() ? nullptr : it->second.get();
}

void BlockRegistry::addBlockDefinitionsFromPluginLoader(gr::PluginLoader& pluginLoader) {
Expand Down Expand Up @@ -135,7 +135,7 @@ void BlockRegistry::addBlockDefinitionsFromPluginLoader(gr::PluginLoader& plugin
}
}

void BlockRegistry::addBlockDefinition(std::unique_ptr<BlockDefinition>&& t) { m_types.insert({t->name, std::move(t)}); }
void BlockRegistry::addBlockDefinition(std::unique_ptr<BlockDefinition>&& t) { _types.insert({t->name, std::move(t)}); }

Block::Block(std::string_view name, const BlockDefinition* t, gr::property_map settings) : name(name), m_type(t), m_settings(std::move(settings)) { //
setCurrentInstantiation(m_type->instantiations.cbegin()->first);
Expand Down Expand Up @@ -320,7 +320,6 @@ void FlowGraph::parse(const std::filesystem::path& file) {
}

void FlowGraph::parse(const std::string& str) {
fmt::print("FlowGraph::parse {}\n", str);
clear();

auto grGraph = [this, &str]() {
Expand All @@ -331,8 +330,6 @@ void FlowGraph::parse(const std::string& str) {
}
}();

fmt::print("Number of blocks {}, number of edges {}\n", grGraph.blocks().size(), grGraph.edges().size());

grGraph.forEachBlock([&](const auto& grBlock) {
auto typeName = grBlock.typeName();
typeName = std::string_view(typeName.begin(), typeName.find('<'));
Expand Down
8 changes: 3 additions & 5 deletions src/ui/Flowgraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ struct BlockRegistry {

const BlockDefinition* get(std::string_view id) const;

inline const auto& types() const { return m_types; }
inline const auto& types() const { return _types; }

static BlockRegistry& instance();

private:
// This stuff is to enable looking up in the m_types map with string_view
// This stuff is to enable looking up in the _types map with string_view
template<typename... Keys>
struct transparent_hash : std::hash<Keys>... {
using is_transparent = void;
Expand All @@ -251,7 +251,7 @@ struct BlockRegistry {

using transparent_string_hash = transparent_hash<std::string, std::string_view, const char*, char*>;

std::unordered_map<std::string, std::unique_ptr<BlockDefinition>, transparent_string_hash, std::equal_to<>> m_types;
std::unordered_map<std::string, std::unique_ptr<BlockDefinition>, transparent_string_hash, std::equal_to<>> _types;
};

class Block {
Expand Down Expand Up @@ -433,8 +433,6 @@ class FlowGraph {

void changeBlockDefinition(Block* block, const std::string& type);

// TODO this will replace the whole flowgraph, for the time being, we
// are just testing the model
UiGraphModel graphModel;

private:
Expand Down
17 changes: 3 additions & 14 deletions src/ui/FlowgraphItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,8 @@ static bool blockInTree(const Block* block, const Block* start) {
return blockInTreeHelper<inputs, &Connection::src>(block, start) || blockInTreeHelper<outputs, &Connection::dst>(block, start);
}

namespace {
template<class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

} // namespace

void valToString(const pmtv::pmt& val, std::string& str) {
std::visit(overloaded{
std::visit(gr::meta::overloaded{
[&](const std::string& s) { str = s; },
[&](const auto& a) { str = "na"; },
},
Expand Down Expand Up @@ -573,7 +563,8 @@ void newDrawGraph(UiGraphModel& graphModel, const ImVec2& size) {

for (std::size_t i = 0; i < ports.size(); ++i) {
widths[i] = ImGui::CalcTextSize(ports[i].portName.c_str()).x + textMargin * 2;
// if (!filteredOut) { TODO(NOW)
// TODO Reimplement block visual filtering
// if (!filteredOut) {
addPin(ax::NodeEditor::PinId(&ports[i]), pinType, position, {widths[i], pinHeight});
// }
position.y += pinHeight + pinSpacing;
Expand Down Expand Up @@ -632,7 +623,6 @@ void newDrawGraph(UiGraphModel& graphModel, const ImVec2& size) {
.y = boundingBox.maxY, //
.width = blockSize[0], //
.height = blockSize[1]};
fmt::print(">>>>>>>>>> new position/layout for {} x{} y{} w{} h{}\n", block.blockUniqueName, block.view->x, block.view->y, block.view->width, block.view->height);
ax::NodeEditor::SetNodePosition(blockId, ImVec2(block.view->x, block.view->y));
boundingBox.minX += blockSize[0] + padding.x;
}
Expand Down Expand Up @@ -672,7 +662,6 @@ void newDrawGraph(UiGraphModel& graphModel, const ImVec2& size) {
} else {
if (ax::NodeEditor::AcceptNewItem()) {
// AcceptNewItem() return true when user release mouse button.
fmt::print("Send the connect message\n");
gr::Message message;
message.cmd = gr::message::Command::Set;
message.endpoint = gr::graph::property::kEmplaceEdge;
Expand Down
2 changes: 0 additions & 2 deletions src/ui/FlowgraphItem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class FlowGraphItem {
void clear();
void setStyle(LookAndFeel::Style style);

// TODO: Block pointer
std::function<Block*(FlowGraph*)> newSinkCallback;
std::function<void(components::BlockControlsPanelContext&, const ImVec2&, const ImVec2&, bool)> requestBlockControlsPanel;

Expand All @@ -53,7 +52,6 @@ class FlowGraphItem {
SignalSelector m_signalSelector;

// new block dialog data
// TODO: Block pointer
const Block* m_filterBlock = nullptr;
struct Context {
Context();
Expand Down
73 changes: 33 additions & 40 deletions src/ui/GraphModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ void updateFieldFrom(FieldType& field, const auto& data, const std::string& fiel
field = getProperty<FieldType>(data, fieldName);
};
} // namespace
//

void UiGraphModel::processMessage(const gr::Message& message) {
fmt::print("\u001b[32m>> GraphModel got {} for {}\n\u001b[0m", message.endpoint, message.serviceName);
namespace graph = gr::graph::property;
namespace block = gr::block::property;

if (!message.data) {
// TODO report error
DigitizerUi::components::Notification::error(fmt::format("Received an error: {}\n", message.data.error().toString());
return;
}

const auto& data = *message.data;
Expand All @@ -67,7 +66,6 @@ void UiGraphModel::processMessage(const gr::Message& message) {
handleBlockRemoved(uniqueName());

} else if (message.endpoint == graph::kBlockReplaced) {
fmt::print("Block replaced: {} with {}\n", uniqueName("replacedBlockUniqueName"), data);
handleBlockRemoved(uniqueName("replacedBlockUniqueName"));
handleBlockEmplaced(data);

Expand All @@ -80,7 +78,6 @@ void UiGraphModel::processMessage(const gr::Message& message) {

} else if (message.endpoint == block::kStagedSetting) {
// serviceNames is used for block's unique name in settings messages
fmt::print("\u001b[32mStaged settings: {}\n\u001b[0m", data);
handleBlockSettingsStaged(message.serviceName, data);

} else if (message.endpoint == graph::kEdgeEmplaced) {
Expand All @@ -93,10 +90,10 @@ void UiGraphModel::processMessage(const gr::Message& message) {
handleGraphRedefined(data);

} else if (message.endpoint == "LifecycleState") {
fmt::print("\u001b[32mNew state {}\n\u001b[0m", data);
// Nothing to do for lifecycle state changes

} else {
fmt::print("\u001b[32mNot processed: {} data: {}\n\u001b[0m", message.endpoint, message.data.error());
fmt::print("Not processed: {} data: {}\n", message.endpoint, message.data.error());
if (!message.data) {
auto msg = fmt::format("Error: {}\n", message.data.error().message);
DigitizerUi::components::Notification::error(msg);
Expand All @@ -106,6 +103,9 @@ void UiGraphModel::processMessage(const gr::Message& message) {
}

void UiGraphModel::requestGraphUpdate() {
if (_newGraphDataBeingSet) {
return;
}
gr::Message message;
message.cmd = gr::message::Command::Set;
message.endpoint = gr::graph::property::kGraphInspect;
Expand All @@ -114,8 +114,8 @@ void UiGraphModel::requestGraphUpdate() {
}

auto UiGraphModel::findBlockByName(const std::string& uniqueName) {
auto it = std::ranges::find_if(m_blocks, [&](const auto& block) { return block.blockUniqueName == uniqueName; });
return std::make_pair(it, it != m_blocks.end());
auto it = std::ranges::find_if(_blocks, [&](const auto& block) { return block.blockUniqueName == uniqueName; });
return std::make_pair(it, it != _blocks.end());
}

auto UiGraphModel::findPortByName(auto& ports, const std::string& portName) {
Expand All @@ -126,14 +126,13 @@ auto UiGraphModel::findPortByName(auto& ports, const std::string& portName) {
bool UiGraphModel::handleBlockRemoved(const std::string& uniqueName) {
auto [blockIt, found] = findBlockByName(uniqueName);
if (!found) {
// TODO warning
fmt::print("\u001b[32mWARNING: Unknown block removed {}\n\u001b[0m", uniqueName);
requestGraphUpdate();
return false;
}

// Delete edges for the removed block
removeEdgesForBlock(*blockIt);
m_blocks.erase(blockIt);
_blocks.erase(blockIt);
return true;
}

Expand All @@ -143,16 +142,15 @@ void UiGraphModel::handleBlockEmplaced(const gr::property_map& blockData) {
if (found) {
setBlockData(*it, blockData);
} else {
auto& newBlock = m_blocks.emplace_back(/*owner*/ this);
auto& newBlock = _blocks.emplace_back(/*owner*/ this);
setBlockData(newBlock, blockData);
}
}

void UiGraphModel::handleBlockDataUpdated(const std::string& uniqueName, const gr::property_map& blockData) {
auto [blockIt, found] = findBlockByName(uniqueName);
if (!found) {
// TODO warning
fmt::print("\u001b[32mWARNING: Data updated for an unknown block {}\n\u001b[0m", uniqueName);
requestGraphUpdate();
return;
}

Expand All @@ -162,11 +160,9 @@ void UiGraphModel::handleBlockDataUpdated(const std::string& uniqueName, const g
void UiGraphModel::handleBlockSettingsChanged(const std::string& uniqueName, const gr::property_map& data) {
auto [blockIt, found] = findBlockByName(uniqueName);
if (!found) {
// TODO warning
fmt::print("\u001b[32mWARNING: Settings updated for an unknown block {} -> {}\n\u001b[0m", uniqueName, data);
requestGraphUpdate();
return;
}
fmt::print("\u001b[32mNew settings for {} are {}\n\u001b[0m", uniqueName, data);
for (const auto& [key, value] : data) {
blockIt->blockSettings.insert_or_assign(key, value);
}
Expand All @@ -177,17 +173,23 @@ void UiGraphModel::handleBlockSettingsStaged(const std::string& uniqueName, cons
void UiGraphModel::handleEdgeEmplaced(const gr::property_map& data) {
UiGraphEdge edge(this);
if (setEdgeData(edge, data)) {
m_edges.emplace_back(std::move(edge));
_edges.emplace_back(std::move(edge));
} else {
// Failed to read edge data
fmt::print("Failed to read edge data {}\n", data);
requestGraphUpdate();
}
}

void UiGraphModel::handleEdgeRemoved(const gr::property_map& data) { fmt::print("handleEdgeRemoved {}\n", data); }
void UiGraphModel::handleEdgeRemoved(const gr::property_map& data) {}

void UiGraphModel::handleGraphRedefined(const gr::property_map& data) {
_newGraphDataBeingSet = true;
struct scope_guard {
~scope_guard() { flag = false; }
bool& flag;
};
scope_guard resetDataBeingSet(_newGraphDataBeingSet);

// Strictly speaking, UiGraphModel is not a block even if
// gr::Graph is a gr::Block, but we can set some basic
// properties like this
Expand All @@ -196,38 +198,34 @@ void UiGraphModel::handleGraphRedefined(const gr::property_map& data) {
// Update or create blocks that GR knows
const auto& children = getProperty<gr::property_map>(data, "children");
for (const auto& [blockUniqueName, blockData] : children) {
fmt::print("\u001b[32mGraph contains child {}\n\u001b[0m", blockUniqueName);
const auto [blockIt, found] = findBlockByName(blockUniqueName);
if (found) {
fmt::print("\u001b[32mBlock {} exists, updating...\n\u001b[0m", blockUniqueName);
setBlockData(*blockIt, std::get<gr::property_map>(blockData));
} else {
fmt::print("\u001b[32mBlock {} does not exist, creating...\n\u001b[0m", blockUniqueName);
handleBlockEmplaced(std::get<gr::property_map>(blockData));
}
}

// Delete blocks that GR doesn't know about.
// This is similar to erase-remove, but we need the list of blocks
// we want to delete in order to disconnect them first.
auto toRemove = std::partition(m_blocks.begin(), m_blocks.end(), [&children](const auto& block) { //
auto toRemove = std::partition(_blocks.begin(), _blocks.end(), [&children](const auto& block) { //
return children.contains(block.blockUniqueName);
});
for (; toRemove != m_blocks.end(); ++toRemove) {
fmt::print("\u001b[32mBlock {} no longer exists, removing...\n\u001b[0m", toRemove->blockUniqueName);
for (; toRemove != _blocks.end(); ++toRemove) {
removeEdgesForBlock(*toRemove);
}
m_blocks.erase(toRemove, m_blocks.end());
_blocks.erase(toRemove, _blocks.end());

// Establish new edges
m_edges.clear();
_edges.clear();
const auto& edges = getProperty<gr::property_map>(data, "edges");
for (const auto& [index, edgeData_] : edges) {
const auto edgeData = std::get<gr::property_map>(edgeData_);

UiGraphEdge edge(this);
if (setEdgeData(edge, edgeData)) {
m_edges.emplace_back(std::move(edge));
_edges.emplace_back(std::move(edge));
} else {
components::Notification::error("Invalid edge ignored");
}
Expand All @@ -251,11 +249,10 @@ void UiGraphModel::setBlockData(auto& block, const gr::property_map& blockData)
portsCollection.clear();
for (const auto& [portName, portData_] : getProperty<gr::property_map>(blockData, portsField)) {
const auto& portData = std::get<gr::property_map>(portData_);
fmt::print("\u001b[32mEncoded input port: {} {}\n\u001b[0m", portName, portData);
auto& port = portsCollection.emplace_back(/*owner*/ std::addressof(block));
port.portName = portName;
port.portType = getProperty<std::string>(portData, "type"s);
port.portDirection = direction;
auto& port = portsCollection.emplace_back(/*owner*/ std::addressof(block));
port.portName = portName;
port.portType = getProperty<std::string>(portData, "type"s);
port.portDirection = direction;
}
};

Expand Down Expand Up @@ -291,8 +288,6 @@ bool UiGraphModel::setEdgeData(auto& edge, const gr::property_map& edgeData) {
auto findPortFor = [this](std::string& currentBlockName, auto member, const gr::PortDefinition& portDefinition) -> UiGraphPort* {
auto [it, found] = findBlockByName(currentBlockName);
if (!found) {
// TODO report error
fmt::print("\u001b[32mGot an unknown block in a connection {}\n\u001b[0m", currentBlockName);
return nullptr;
}

Expand All @@ -304,7 +299,6 @@ bool UiGraphModel::setEdgeData(auto& edge, const gr::property_map& edgeData) {
// TODO: sub-index for ports -- when we add UI support for
// port arrays
if (indexBasedDefinition.topLevel >= ports.size()) {
fmt::print("\u001b[32mPort {} not found in in block while trying to connect\n\u001b[0m", indexBasedDefinition.topLevel);
return nullptr;
}

Expand All @@ -315,7 +309,6 @@ bool UiGraphModel::setEdgeData(auto& edge, const gr::property_map& edgeData) {
return port.portName == stringBasedDefinition.name;
});
if (portIt == ports.end()) {
fmt::print("\u001b[32mPort {} not found in in block while trying to connect\n\u001b[0m", stringBasedDefinition.name);
return nullptr;
}
return std::addressof(*portIt);
Expand All @@ -342,7 +335,7 @@ bool UiGraphModel::setEdgeData(auto& edge, const gr::property_map& edgeData) {
}

void UiGraphModel::removeEdgesForBlock(UiGraphBlock& block) {
std::erase_if(m_edges, [blockPtr = std::addressof(block)](const auto& edge) {
std::erase_if(_edges, [blockPtr = std::addressof(block)](const auto& edge) {
return edge.edgeSourcePort->ownerBlock == blockPtr || //
edge.edgeDestinationPort->ownerBlock == blockPtr;
});
Expand Down
Loading

0 comments on commit 28b670b

Please sign in to comment.