Skip to content

Commit

Permalink
Fix crashes in PopupConfigurationWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
anjaldoshi committed Jan 22, 2025
1 parent 3dcb9ff commit fe35b76
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 52 deletions.
78 changes: 43 additions & 35 deletions Plugins/SpikeDetector/PopupConfigurationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ ThresholdSelectorCustomComponent::~ThresholdSelectorCustomComponent()

void ThresholdSelectorCustomComponent::mouseDown (const MouseEvent& event)
{
if (channel == nullptr)
if (channel == nullptr || ! channel->isValid())
return;

auto* popupComponent = new PopupThresholdComponent (table,
Expand Down Expand Up @@ -334,7 +334,9 @@ void ThresholdSelectorCustomComponent::paint (Graphics& g)

thresholdString = thresholdString.substring (0, thresholdString.length() - 1);

g.setColour (findColour (ThemeColours::defaultText));
float alpha = getParentComponent()->isEnabled() ? 1.0f : 0.5f;

g.setColour (findColour (ThemeColours::defaultText).withAlpha (alpha));
g.setFont (FontOptions ("Inter", "Regular", 14.0f));
g.drawFittedText (thresholdString, 4, 0, getWidth() - 8, getHeight(), Justification::centredLeft, 1, 0.75f);
}
Expand Down Expand Up @@ -380,7 +382,7 @@ void ChannelSelectorCustomComponent::showAsPopup()

void ChannelSelectorCustomComponent::mouseDown (const juce::MouseEvent& event)
{
if (acquisitionIsActive)
if (acquisitionIsActive || ! getParentComponent()->isEnabled())
return;

showAsPopup();
Expand Down Expand Up @@ -449,7 +451,7 @@ void WaveformSelectorCustomComponent::setRowAndColumn (const int newRow, const i

void DeleteButtonCustomComponent::mouseDown (const juce::MouseEvent& event)
{
if (acquisitionIsActive)
if (acquisitionIsActive || ! getParentComponent()->isEnabled())
return;

table->deleteSelectedRows (row);
Expand All @@ -460,7 +462,7 @@ void DeleteButtonCustomComponent::paint (Graphics& g)
int width = getWidth();
int height = getHeight();

if (acquisitionIsActive)
if (acquisitionIsActive || ! getParentComponent()->isEnabled())
{
g.setColour (Colours::grey);
}
Expand All @@ -471,7 +473,7 @@ void DeleteButtonCustomComponent::paint (Graphics& g)

g.fillEllipse (7, 7, width - 14, height - 14);
g.setColour (Colours::white);
g.fillRect (9, (height / 2) - 2, width - 19, 3);
g.fillRect (9, (height / 2) - 1, width - 19, 2);
}

void DeleteButtonCustomComponent::setRowAndColumn (const int newRow, const int newColumn)
Expand Down Expand Up @@ -758,6 +760,15 @@ void SpikeDetectorTableModel::update (Array<SpikeChannel*> spikeChannels_)

table->updateContent();

if (spikeChannels.size() > 0 && ! spikeChannels[0]->isValid())
{
table->setEnabled (false);
}
else
{
table->setEnabled (true);
}

waveformComponents.clear();
thresholdComponents.clear();

Expand All @@ -781,33 +792,23 @@ void SpikeDetectorTableModel::paintRowBackground (Graphics& g, int rowNumber, in
if (rowNumber >= spikeChannels.size())
return;

if (rowIsSelected)
{
if (rowNumber % 2 == 0)
g.fillAll (owner->findColour (ThemeColours::componentBackground));
else
g.fillAll (owner->findColour (ThemeColours::componentBackground).darker (0.25f));

g.setColour (owner->findColour (ThemeColours::highlightedFill));
g.drawRoundedRectangle (2, 2, width - 4, height - 4, 5, 2);

return;
}
Colour rowColour;

if (spikeChannels[rowNumber]->isValid())
{
if (rowNumber % 2 == 0)
g.fillAll (owner->findColour (ThemeColours::componentBackground));
else
g.fillAll (owner->findColour (ThemeColours::componentBackground).darker (0.25f));

return;
}
rowColour = owner->findColour (ThemeColours::componentBackground);
else
rowColour = Colour (90, 50, 50);

if (rowNumber % 2 == 0)
g.fillAll (Colour (90, 50, 50));
g.fillAll (rowColour);
else
g.fillAll (Colour (60, 30, 30));
g.fillAll (rowColour.darker (0.25f));

if (rowIsSelected)
{
g.setColour (owner->findColour (ThemeColours::highlightedFill));
g.drawRoundedRectangle (2, 2, width - 4, height - 4, 5, 2);
}
}

void SpikeDetectorTableModel::paintCell (Graphics& g, int rowNumber, int columnId, int width, int height, bool rowIsSelected)
Expand Down Expand Up @@ -1055,8 +1056,14 @@ PopupConfigurationWindow::PopupConfigurationWindow (SpikeDetectorEditor* editor_
addAndMakeVisible (viewport.get());
update (spikeChannels);

auto stream = editor->getProcessor()->getDataStream (editor->getCurrentStream());
popupTitle = String(stream->getSourceNodeId()) + " " + stream->getSourceNodeName() + " - " + stream->getName();
if (auto stream = editor->getProcessor()->getDataStream (editor->getCurrentStream()))
{
popupTitle = String (stream->getSourceNodeId()) + " " + stream->getSourceNodeName() + " - " + stream->getName();
}
else
{
popupTitle = "No streams available";
}
}

void PopupConfigurationWindow::scrollBarMoved (ScrollBar* scrollBar, double newRangeStart)
Expand Down Expand Up @@ -1128,12 +1135,13 @@ void PopupConfigurationWindow::paint (Graphics& g)
bool PopupConfigurationWindow::keyPressed (const KeyPress& key)
{
// Popup component handles globally reserved undo/redo keys
PopupComponent::keyPressed (key);

// Pressing 'a' key adds a new spike channel
if (key.getTextCharacter() == 'a')
if (! PopupComponent::keyPressed (key))
{
editor->addSpikeChannels (this, spikeChannelGenerator->getSelectedType(), 1);
// Pressing 'a' key adds a new spike channel
if (key.getTextCharacter() == 'a')
{
editor->addSpikeChannels (this, spikeChannelGenerator->getSelectedType(), 1);
}
}

return true;
Expand Down
32 changes: 17 additions & 15 deletions Plugins/SpikeDetector/SpikeDetectorEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,34 @@ void SpikeDetectorEditor::addSpikeChannels (PopupConfigurationWindow* window, Sp
{
SpikeDetector* processor = (SpikeDetector*) getProcessor();

DataStream* stream = processor->getDataStream (getCurrentStream());

int nextAvailableChannel = processor->getNextAvailableChannelForStream (stream->getStreamId());
if (auto stream = processor->getDataStream (getCurrentStream()))
{
int nextAvailableChannel = processor->getNextAvailableChannelForStream (stream->getStreamId());

AddSpikeChannels* action = new AddSpikeChannels (processor, stream, type, count, startChannels, nextAvailableChannel);
AddSpikeChannels* action = new AddSpikeChannels (processor, stream, type, count, startChannels, nextAvailableChannel);

CoreServices::getUndoManager()->beginNewTransaction ("Disabled during acquisition");
CoreServices::getUndoManager()->perform ((UndoableAction*) action);
CoreServices::getUndoManager()->beginNewTransaction ("Disabled during acquisition");
CoreServices::getUndoManager()->perform ((UndoableAction*) action);

if (window != nullptr)
window->update (processor->getSpikeChannelsForStream (getCurrentStream()));
if (window != nullptr)
window->update (processor->getSpikeChannelsForStream (getCurrentStream()));
}
}

void SpikeDetectorEditor::removeSpikeChannels (PopupConfigurationWindow* window, Array<SpikeChannel*> spikeChannelsToRemove, Array<int> indeces)
{
SpikeDetector* processor = (SpikeDetector*) getProcessor();

DataStream* stream = processor->getDataStream (getCurrentStream());

RemoveSpikeChannels* action = new RemoveSpikeChannels (processor, stream, spikeChannelsToRemove, indeces);
if (auto stream = processor->getDataStream (getCurrentStream()))
{
RemoveSpikeChannels* action = new RemoveSpikeChannels (processor, stream, spikeChannelsToRemove, indeces);

CoreServices::getUndoManager()->beginNewTransaction ("Disabled during acquisition");
CoreServices::getUndoManager()->perform ((UndoableAction*) action);
CoreServices::getUndoManager()->beginNewTransaction ("Disabled during acquisition");
CoreServices::getUndoManager()->perform ((UndoableAction*) action);

if (window != nullptr)
window->update (processor->getSpikeChannelsForStream (getCurrentStream()));
if (window != nullptr)
window->update (processor->getSpikeChannelsForStream (getCurrentStream()));
}
}

int SpikeDetectorEditor::getNumChannelsForCurrentStream()
Expand Down
7 changes: 5 additions & 2 deletions Source/Processors/GenericProcessor/GenericProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ void GenericProcessor::addTtlLineParameter (
}

void GenericProcessor::parameterChangeRequest (Parameter* param)
{
{
currentParameter = param;

setParameter (-1, 0.0f);
Expand Down Expand Up @@ -1622,7 +1622,10 @@ const SpikeChannel* GenericProcessor::getSpikeChannel (uint16 processorId, uint1

DataStream* GenericProcessor::getDataStream (uint16 streamId) const
{
return dataStreamMap.at (streamId);
if (dataStreamMap.find (streamId) != dataStreamMap.end())
return dataStreamMap.at (streamId);
else
return nullptr;
}

DataStream* GenericProcessor::getDataStream (String streamKey) const
Expand Down

0 comments on commit fe35b76

Please sign in to comment.