Skip to content

Commit 279705d

Browse files
committed
Adjust left margin of LFP Display dynamically based on channel name width
1 parent d2dda07 commit 279705d

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Plugins/LfpViewer/LfpDisplayCanvas.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "ShowHideOptionsButton.h"
3131

3232
#include <math.h>
33+
#include <algorithm>
34+
#include <cmath>
3335

3436
#define MS_FROM_START Time::highResolutionTicksToSeconds (Time::getHighResolutionTicks() - start) * 1000
3537

@@ -750,6 +752,27 @@ String LfpDisplaySplitter::getStreamKey()
750752
return stream->getKey();
751753
}
752754

755+
void LfpDisplaySplitter::refreshLeftMargin()
756+
{
757+
int newMargin = minimumLeftMargin;
758+
759+
if (displayBuffer != nullptr)
760+
{
761+
const auto labelFont = Font (FontOptions (14.0f));
762+
constexpr int padding = 20;
763+
764+
for (int i = 0; i < displayBuffer->channelMetadata.size(); ++i)
765+
{
766+
const auto& metadata = displayBuffer->channelMetadata.getReference (i);
767+
const float textWidth = labelFont.getStringWidthFloat (metadata.name);
768+
const int requiredWidth = static_cast<int> (std::ceil (textWidth)) + padding;
769+
newMargin = std::max (newMargin, requiredWidth);
770+
}
771+
}
772+
773+
leftmargin = newMargin;
774+
}
775+
753776
void LfpDisplaySplitter::resized()
754777
{
755778
const int timescaleHeight = 30;
@@ -928,6 +951,7 @@ void LfpDisplaySplitter::updateSettings()
928951
sampleRate = 44100.0f;
929952

930953
options->setEnabled (true);
954+
leftmargin = minimumLeftMargin;
931955
}
932956
else
933957
{
@@ -942,6 +966,8 @@ void LfpDisplaySplitter::updateSettings()
942966

943967
options->setEnabled (true);
944968
channelOverlapFactor = options->selectedOverlapValue.getFloatValue();
969+
970+
refreshLeftMargin();
945971
}
946972

947973
if (eventDisplayBuffer == nullptr) // not yet initialized
@@ -968,12 +994,14 @@ void LfpDisplaySplitter::updateSettings()
968994
lfpDisplay->channels[i]->setDepth (displayBuffer->channelMetadata[i].ypos);
969995
lfpDisplay->channels[i]->setRecorded (displayBuffer->channelMetadata[i].isRecorded);
970996
lfpDisplay->channels[i]->updateType (displayBuffer->channelMetadata[i].type);
997+
lfpDisplay->channels[i]->setUnits (displayBuffer->channelMetadata[i].units);
971998

972999
lfpDisplay->channelInfo[i]->setName (displayBuffer->channelMetadata[i].name);
9731000
lfpDisplay->channelInfo[i]->setGroup (displayBuffer->channelMetadata[i].group);
9741001
lfpDisplay->channelInfo[i]->setDepth (displayBuffer->channelMetadata[i].ypos);
9751002
lfpDisplay->channelInfo[i]->setRecorded (displayBuffer->channelMetadata[i].isRecorded);
9761003
lfpDisplay->channelInfo[i]->updateType (displayBuffer->channelMetadata[i].type);
1004+
lfpDisplay->channelInfo[i]->setUnits (displayBuffer->channelMetadata[i].units);
9771005

9781006
lfpDisplay->updateRange (i);
9791007

Plugins/LfpViewer/LfpDisplayCanvas.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,11 @@ class LfpDisplaySplitter : public Component,
308308
*/
309309
bool fullredraw;
310310

311-
/** Left margin for lfp plots (so the ch number text doesnt overlap) */
312-
static const int leftmargin = 60; //
311+
/** Minimum left margin for LFP plots (so the channel label has space) */
312+
static constexpr int minimumLeftMargin = 60;
313+
314+
/** Left margin for LFP plots, adjusted to fit the widest channel label */
315+
int leftmargin = minimumLeftMargin;
313316

314317
Array<bool> isChannelEnabled;
315318

@@ -359,6 +362,8 @@ class LfpDisplaySplitter : public Component,
359362
String getStreamKey();
360363

361364
private:
365+
void refreshLeftMargin();
366+
362367
bool isSelected;
363368
bool isUpdating;
364369

0 commit comments

Comments
 (0)