From b96bc82d50f726ee5bfca08981a22f13b7a71474 Mon Sep 17 00:00:00 2001 From: Pavel Kulik Date: Tue, 12 Dec 2023 13:48:54 -0800 Subject: [PATCH] Add ChannelMappingNodeActions --- Plugins/ChannelMappingNode/CMakeLists.txt | 2 + .../ChannelMappingEditor.cpp | 14 +-- .../ChannelMappingNodeActions.cpp | 93 +++++++++++++++++++ .../ChannelMappingNodeActions.h | 44 +++++++++ 4 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 Plugins/ChannelMappingNode/ChannelMappingNodeActions.cpp create mode 100644 Plugins/ChannelMappingNode/ChannelMappingNodeActions.h diff --git a/Plugins/ChannelMappingNode/CMakeLists.txt b/Plugins/ChannelMappingNode/CMakeLists.txt index caa066e4f..8980fd112 100644 --- a/Plugins/ChannelMappingNode/CMakeLists.txt +++ b/Plugins/ChannelMappingNode/CMakeLists.txt @@ -6,6 +6,8 @@ include(../PluginRules.cmake) #add sources, not including OpenEphysLib.cpp add_sources(${PLUGIN_NAME} + ChannelMappingNodeActions.cpp + ChannelMappingNodeActions.h ChannelMappingNode.cpp ChannelMappingNode.h ChannelMappingEditor.cpp diff --git a/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp b/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp index 491248e63..993335dc2 100644 --- a/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp +++ b/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp @@ -21,11 +21,12 @@ along with this program. If not, see . */ +#include #include "ChannelMappingEditor.h" #include "ChannelMappingNode.h" -#include +#include "ChannelMappingNodeActions.h" ChannelMappingEditor::ChannelMappingEditor(GenericProcessor* parentNode) : GenericEditor(parentNode), @@ -423,18 +424,17 @@ void ChannelMappingEditor::mouseUp(const MouseEvent& e) electrodeButtons[lastHoverButton]->setVisible(true); - Array order; + Array newChannelOrder; for (auto button : electrodeButtons) { - order.add(button->getChannelNum() - 1); + newChannelOrder.add(button->getChannelNum() - 1); } - ChannelMappingNode* processor = (ChannelMappingNode*)getProcessor(); - - processor->setChannelOrder(getCurrentStream(), order); + MapChannelsAction* action = new MapChannelsAction((ChannelMappingNode*)getProcessor(), getProcessor()->getDataStream(getCurrentStream()), newChannelOrder); - CoreServices::updateSignalChain(this); + CoreServices::getUndoManager()->beginNewTransaction(); + CoreServices::getUndoManager()->perform((UndoableAction*)action); /*int from, to; if (lastHoverButton == initialDraggedButton) diff --git a/Plugins/ChannelMappingNode/ChannelMappingNodeActions.cpp b/Plugins/ChannelMappingNode/ChannelMappingNodeActions.cpp new file mode 100644 index 000000000..372f38bc0 --- /dev/null +++ b/Plugins/ChannelMappingNode/ChannelMappingNodeActions.cpp @@ -0,0 +1,93 @@ +/* + ------------------------------------------------------------------ + + This file is part of the Open Ephys GUI + Copyright (C) 2023 Open Ephys + + ------------------------------------------------------------------ + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +#include + +#include "ChannelMappingNodeActions.h" + +MapChannelsAction::MapChannelsAction(ChannelMappingNode* processor, + DataStream* stream, + Array newMap) : + OpenEphysAction("MapChannels"), + channelMapper(processor), + streamKey(stream->getKey()), + prevChannelOrder(processor->getChannelOrder(stream->getStreamId())), + nextChannelOrder(newMap) +{ + settings = nullptr; +} + +MapChannelsAction::~MapChannelsAction() +{ + if (settings != nullptr) + delete settings; +} + +bool MapChannelsAction::perform() +{ + uint16 streamId = 0; + for (auto stream : channelMapper->getDataStreams()) + { + if (stream->getKey() == streamKey) + { + streamId = stream->getStreamId(); + break; + } + } + if (streamId == 0) return false; + + channelMapper->setChannelOrder(streamId, nextChannelOrder); + + channelMapper->registerUndoableAction(channelMapper->getNodeId(), this); + + CoreServices::updateSignalChain(channelMapper); + + return true; +} + +bool MapChannelsAction::undo() +{ + uint16 streamId = 0; + for (auto stream : channelMapper->getDataStreams()) + { + if (stream->getKey() == streamKey) + { + streamId = stream->getStreamId(); + break; + } + } + if (streamId == 0) return false; + + channelMapper->setChannelOrder(streamId, prevChannelOrder); + + channelMapper->registerUndoableAction(channelMapper->getNodeId(), this); + + CoreServices::updateSignalChain(channelMapper); + + return true; +} + +void MapChannelsAction::restoreOwner(GenericProcessor* owner) +{ + channelMapper = (ChannelMappingNode*)owner; +} \ No newline at end of file diff --git a/Plugins/ChannelMappingNode/ChannelMappingNodeActions.h b/Plugins/ChannelMappingNode/ChannelMappingNodeActions.h new file mode 100644 index 000000000..15ce053d7 --- /dev/null +++ b/Plugins/ChannelMappingNode/ChannelMappingNodeActions.h @@ -0,0 +1,44 @@ +// Define the actions available for the ChannelMappingNode plugin. + +// ===================================================== +#ifndef ChannelMappingNodeActions_h +#define ChannelMappingNodeActions_h + +#include + +#include "ChannelMappingNode.h" +#include "ChannelMappingEditor.h" + +class MapChannelsAction : public OpenEphysAction +{ + +public: + + /** Constructor*/ + MapChannelsAction(ChannelMappingNode* processor, + DataStream* stream, + Array nextChannelOrder); + + /** Destructor */ + ~MapChannelsAction(); + + void restoreOwner(GenericProcessor* processor) override; + + /** Perform the action*/ + bool perform(); + + /** Undo the action*/ + bool undo(); + + XmlElement* settings; + +private: + + ChannelMappingNode* channelMapper; + String streamKey; + Array prevChannelOrder; + Array nextChannelOrder; + +}; + +#endif /* ChannelMappingNodeActions_h */ \ No newline at end of file