1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-03 17:39:25 +02:00

Track::SetSelected is not virtual...

... which will simplify later rewrites that associate selection state with
the track group, not the track.

Since LabelTrack isn't notified immediately of selection changes, instead it
always tests whether it is selected, before using the stored selected label
index.
This commit is contained in:
Paul Licameli 2018-12-06 15:27:03 -05:00
parent 0cecc6e48d
commit 0750f62e88
4 changed files with 36 additions and 30 deletions

View File

@ -751,7 +751,7 @@ void LabelStruct::getXPos( wxDC & dc, int * xPos1, int cursorPos) const
bool LabelTrack::CalcCursorX(int * x) const
{
if (mSelIndex >= 0) {
if ( HasSelection() ) {
wxMemoryDC dc;
if (msFont.Ok()) {
@ -883,7 +883,7 @@ void LabelTrack::Draw
#ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
highlight = highlightTrack && target->GetLabelNum() == i;
#endif
bool selected = mSelIndex == i;
bool selected = GetSelectedIndex() == i;
if( selected )
dc.SetBrush( AColor::labelTextEditBrush );
@ -897,7 +897,7 @@ void LabelTrack::Draw
}
// Draw highlights
if ((mInitialCursorPos != mCurrentCursorPos) && (mSelIndex >= 0 ))
if ( (mInitialCursorPos != mCurrentCursorPos) && HasSelection() )
{
int xpos1, xpos2;
CalcHighlightXs(&xpos1, &xpos2);
@ -906,15 +906,15 @@ void LabelTrack::Draw
// Draw the text and the label boxes.
{ int i = -1; for (auto &labelStruct : mLabels) { ++i;
if( mSelIndex==i)
if( GetSelectedIndex() == i )
dc.SetBrush(AColor::labelTextEditBrush);
labelStruct.DrawText( dc, r );
if( mSelIndex==i)
if( GetSelectedIndex() == i )
dc.SetBrush(AColor::labelTextNormalBrush);
}}
// Draw the cursor, if there is one.
if( mDrawCursor && mSelIndex >=0 )
if( mDrawCursor && HasSelection() )
{
const auto &labelStruct = mLabels[mSelIndex];
int xPos = labelStruct.xText;
@ -1011,7 +1011,7 @@ void LabelTrack::calculateFontHeight(wxDC & dc) const
bool LabelTrack::IsTextSelected() const
{
if (mSelIndex == -1)
if ( !HasSelection() )
return false;
if (mCurrentCursorPos == mInitialCursorPos)
return false;
@ -1064,7 +1064,7 @@ bool LabelTrack::CutSelectedText()
/// @return true if text is selected in text box, false otherwise
bool LabelTrack::CopySelectedText()
{
if (mSelIndex == -1)
if ( !HasSelection() )
return false;
const auto &labelStruct = mLabels[mSelIndex];
@ -1095,7 +1095,7 @@ bool LabelTrack::CopySelectedText()
/// @return true if mouse is clicked in text box, false otherwise
bool LabelTrack::PasteSelectedText(double sel0, double sel1)
{
if (mSelIndex == -1)
if ( !HasSelection() )
AddLabel(SelectedRegion(sel0, sel1), wxT(""));
wxString text, left, right;
@ -1139,6 +1139,16 @@ bool LabelTrack::IsTextClipSupported()
}
int LabelTrack::GetSelectedIndex() const
{
// may make delayed update of mutable mSelIndex after track selection change
if ( GetSelected() )
return mSelIndex = std::max( -1,
std::min<int>( mLabels.size() - 1, mSelIndex ) );
else
return mSelIndex = -1;
}
double LabelTrack::GetOffset() const
{
return mOffset;
@ -1173,13 +1183,6 @@ Track::Holder LabelTrack::Clone() const
return std::make_shared<LabelTrack>( *this );
}
void LabelTrack::SetSelected(bool s)
{
Track::SetSelected(s);
if (!s)
Unselect();
}
/// TODO: Investigate what happens with large
/// numbers of labels, might need a binary search
/// rather than a linear one.
@ -1555,7 +1558,7 @@ bool LabelTrack::HandleGlyphDragRelease
MayAdjustLabel( hit, hit.mMouseOverLabelRight, +1, bAllowSwapping, fNewX );
}
if( mSelIndex >=0 )
if( HasSelection() )
{
//Set the selection region to be equal to
//the NEW size of the label.
@ -1600,7 +1603,7 @@ void LabelTrack::HandleTextDragRelease(const wxMouseEvent & evt)
}
if (evt.RightUp()) {
if ((mSelIndex != -1) && OverTextBox(GetLabel(mSelIndex), evt.m_x, evt.m_y)) {
if (HasSelection() && OverTextBox(GetLabel(mSelIndex), evt.m_x, evt.m_y)) {
// popup menu for editing
ShowContextMenu();
}
@ -1753,7 +1756,7 @@ bool LabelTrackView::DoCaptureKey(wxKeyEvent & event)
!mLabels.empty())
return true;
if ( pTrack->mSelIndex >= 0 ) {
if ( pTrack->HasSelection() ) {
if (IsGoodLabelEditKey(event)) {
return true;
}
@ -1888,7 +1891,7 @@ bool LabelTrackView::DoKeyDown(SelectedRegion &newSel, wxKeyEvent & event)
auto &mInitialCursorPos = pTrack->mInitialCursorPos;
auto &mCurrentCursorPos = pTrack->mCurrentCursorPos;
auto &mRestoreFocus = pTrack->mRestoreFocus;
if ( pTrack->mSelIndex >= 0 ) {
if ( pTrack->HasSelection() ) {
auto &labelStruct = mLabels[mSelIndex];
auto &title = labelStruct.title;
switch (keyCode) {
@ -2123,7 +2126,7 @@ bool LabelTrackView::DoChar(SelectedRegion &WXUNUSED(newSel), wxKeyEvent & event
// If we've reached this point and aren't currently editing, add NEW label
const auto pTrack = FindLabelTrack();
if ( pTrack->mSelIndex < 0 ) {
if ( !pTrack->HasSelection() ) {
// Don't create a NEW label for a space
if (wxIsspace(charCode)) {
event.Skip();
@ -2216,7 +2219,11 @@ void LabelTrack::ShowContextMenu()
menu.Enable(OnDeleteSelectedLabelID, true);
menu.Enable(OnEditSelectedLabelID, true);
wxASSERT(mSelIndex >= 0);
if( !HasSelection() ) {
wxASSERT( false );
return;
}
const LabelStruct *ls = GetLabel(mSelIndex);
wxClientDC dc(parent);
@ -2318,7 +2325,8 @@ void LabelTrack::Unselect()
bool LabelTrack::HasSelection() const
{
return (mSelIndex >= 0 && mSelIndex < (int)mLabels.size());
auto selIndex = GetSelectedIndex();
return (selIndex >= 0 && selIndex < (int)mLabels.size());
}
/// Export labels including label start and end-times.

View File

@ -130,7 +130,7 @@ class AUDACITY_DLL_API LabelTrack final : public Track
void Draw( TrackPanelDrawingContext &context, const wxRect & r ) const;
int getSelectedIndex() const { return mSelIndex; }
int GetSelectedIndex() const;
double GetOffset() const override;
double GetStartTime() const override;
@ -142,8 +142,6 @@ private:
Track::Holder Clone() const override;
public:
void SetSelected(bool s) override;
bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
XMLTagHandler *HandleXMLChild(const wxChar *tag) override;
void WriteXML(XMLWriter &xmlFile) const override;
@ -258,7 +256,7 @@ public:
void ShowContextMenu();
void OnContextMenu(wxCommandEvent & evt);
int mSelIndex; /// Keeps track of the currently selected label
mutable int mSelIndex; /// Keeps track of the currently selected label
int mxMouseDisplacement; /// Displacement of mouse cursor from the centre being dragged.
LabelArray mLabels;

View File

@ -334,7 +334,7 @@ private:
bool GetSelected() const { return mSelected; }
virtual void SetSelected(bool s);
void SetSelected(bool s);
public:

View File

@ -132,9 +132,9 @@ UIHandle::Result LabelTextHandle::Drag
mLabelTrackStartYPos = event.m_y;
if (pLT &&
(pLT->getSelectedIndex() != -1) &&
(pLT->GetSelectedIndex() != -1) &&
pLT->OverTextBox(
pLT->GetLabel(pLT->getSelectedIndex()),
pLT->GetLabel(pLT->GetSelectedIndex()),
mLabelTrackStartXPos,
mLabelTrackStartYPos))
mLabelTrackStartYPos = -1;