Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SongEditor Snap Size to Adjust with Time Signature #7620

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/SongEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public slots:
void updatePosition( const lmms::TimePos & t );
void updatePositionLine();
void selectAllClips( bool select );
void updateSnapSizes();

protected:
void closeEvent( QCloseEvent * ce ) override;
Expand Down Expand Up @@ -146,6 +147,7 @@ private slots:
IntModel* m_zoomingModel;
ComboBoxModel* m_snappingModel;
bool m_proportionalSnap;
std::vector<float> m_snapSizes;

bool m_scrollBack;
bool m_smoothScroll;
Expand Down
49 changes: 29 additions & 20 deletions src/gui/editors/SongEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ namespace
constexpr int MIN_PIXELS_PER_BAR = 4;
constexpr int MAX_PIXELS_PER_BAR = 400;
constexpr int ZOOM_STEPS = 200;

constexpr std::array SNAP_SIZES{8.f, 4.f, 2.f, 1.f, 1/2.f, 1/4.f, 1/8.f, 1/16.f};
constexpr std::array PROPORTIONAL_SNAP_SIZES{64.f, 32.f, 16.f, 8.f, 4.f, 2.f, 1.f, 1/2.f, 1/4.f, 1/8.f, 1/16.f, 1/32.f, 1/64.f};
constexpr int SNAP_SIZES_SMALL = 5;
constexpr int SNAP_SIZES_LARGE = 4;

}

Expand Down Expand Up @@ -247,31 +246,41 @@ SongEditor::SongEditor( Song * song ) :
//zoom connects
connect(m_zoomingModel, SIGNAL(dataChanged()), this, SLOT(zoomingChanged()));

// Set up snapping model
for (float bars : SNAP_SIZES)
connect(m_song, &Song::timeSignatureChanged, this, &SongEditor::updateSnapSizes);
updateSnapSizes();
m_snappingModel->setInitValue(SNAP_SIZES_LARGE + 1);

setFocusPolicy( Qt::StrongFocus );
setFocus();
}

void SongEditor::updateSnapSizes()
{
int oldIndex = m_snappingModel->value();
m_snappingModel->clear();
m_snapSizes.clear();
int numerator = m_song->getTimeSigModel().getNumerator();
for (int i = SNAP_SIZES_LARGE; i >= -SNAP_SIZES_SMALL; i--)
{
if (bars > 1.0f)
// Using exp2(i + 1) instead of exp2(i) for the first half so that it leaves
// a gap for a (snap size = numerator) and a (snap size = 1) in the middle
if (i < 0)
{
m_snappingModel->addItem(QString("%1 Bars").arg(bars));
}
else if (bars == 1.0f)
{
m_snappingModel->addItem( "1 Bar" );
float snapSize = std::exp2(i + 1) / numerator;
m_snapSizes.push_back(snapSize);
m_snappingModel->addItem(QString("1/%1 Bar").arg(std::exp2(-(i + 1)) * numerator));
}
else
{
m_snappingModel->addItem(QString("1/%1 Bar").arg(1 / bars));
float snapSize =std::exp2(i);
m_snapSizes.push_back(snapSize);
m_snappingModel->addItem(QString("%1 Bar").arg(snapSize));
}
}
m_snappingModel->setInitValue( m_snappingModel->findText( "1/4 Bar" ) );

setFocusPolicy( Qt::StrongFocus );
setFocus();
m_snappingModel->setValue(oldIndex);
}




void SongEditor::saveSettings( QDomDocument& doc, QDomElement& element )
{
MainWindow::saveWidgetState( parentWidget(), element );
Expand All @@ -288,14 +297,14 @@ void SongEditor::loadSettings( const QDomElement& element )
/*! \brief Return grid size as number of bars */
float SongEditor::getSnapSize() const
{
float snapSize = SNAP_SIZES[m_snappingModel->value()];
float snapSize = m_snapSizes[m_snappingModel->value()];

// If proportional snap is on, we snap to finer values when zoomed in
if (m_proportionalSnap)
{
// Finds the closest available snap size
const float optimalSize = snapSize * DEFAULT_PIXELS_PER_BAR / pixelsPerBar();
return *std::min_element(PROPORTIONAL_SNAP_SIZES.begin(), PROPORTIONAL_SNAP_SIZES.end(), [optimalSize](float a, float b)
return *std::min_element(m_snapSizes.begin(), m_snapSizes.end(), [optimalSize](float a, float b)
{
return std::abs(a - optimalSize) < std::abs(b - optimalSize);
});
Expand Down
Loading