diff --git a/include/AutomatableModelView.h b/include/AutomatableModelView.h index 12b2e4d4919..9e0c5ca7706 100644 --- a/include/AutomatableModelView.h +++ b/include/AutomatableModelView.h @@ -39,7 +39,7 @@ class LMMS_EXPORT AutomatableModelView : public ModelView { public: AutomatableModelView( Model* model, QWidget* _this ); - ~AutomatableModelView() override = default; + ~AutomatableModelView() override; // some basic functions for convenience AutomatableModel* modelUntyped() @@ -76,6 +76,11 @@ class LMMS_EXPORT AutomatableModelView : public ModelView void setConversionFactor( float factor ); float getConversionFactor(); + static AutomatableModel* lastChangedModel() + { + return s_lastChangedModel; + } + protected: virtual void mousePressEvent( QMouseEvent* event ); @@ -83,6 +88,9 @@ class LMMS_EXPORT AutomatableModelView : public ModelView QString m_description; QString m_unit; float m_conversionFactor; // Factor to be applied when the m_model->value is displayed + +private: + inline static AutomatableModel* s_lastChangedModel = nullptr; } ; diff --git a/include/AutomationClipView.h b/include/AutomationClipView.h index bdd2f056872..9be35c25d9a 100644 --- a/include/AutomationClipView.h +++ b/include/AutomationClipView.h @@ -60,6 +60,7 @@ protected slots: void toggleRecording(); void flipY(); void flipX(); + void connectLastChangedModel(); protected: void constructContextMenu( QMenu * ) override; diff --git a/src/core/AutomatableModel.cpp b/src/core/AutomatableModel.cpp index e006be651a4..0550df8a14e 100644 --- a/src/core/AutomatableModel.cpp +++ b/src/core/AutomatableModel.cpp @@ -32,6 +32,7 @@ #include "LocaleHelper.h" #include "ProjectJournal.h" #include "Song.h" +#include namespace lmms { diff --git a/src/gui/AutomatableModelView.cpp b/src/gui/AutomatableModelView.cpp index 0e364993f06..3216b49277d 100644 --- a/src/gui/AutomatableModelView.cpp +++ b/src/gui/AutomatableModelView.cpp @@ -49,6 +49,15 @@ AutomatableModelView::AutomatableModelView( Model* model, QWidget* _this ) : { widget()->setAcceptDrops( true ); widget()->setCursor( QCursor( embed::getIconPixmap( "hand" ), 3, 3 ) ); + if (modelUntyped() != nullptr) + { + QObject::connect(modelUntyped(), &AutomatableModel::dataChanged, [this](){ s_lastChangedModel = modelUntyped(); }); + } +} + +AutomatableModelView::~AutomatableModelView() +{ + if (s_lastChangedModel != nullptr && s_lastChangedModel == modelUntyped()) { s_lastChangedModel = nullptr; } } void AutomatableModelView::addDefaultActions( QMenu* menu ) @@ -138,6 +147,10 @@ void AutomatableModelView::addDefaultActions( QMenu* menu ) void AutomatableModelView::setModel( Model* model, bool isOldModelValid ) { + if (model != nullptr && model != modelUntyped()) + { + QObject::connect(model, &AutomatableModel::dataChanged, [this](){ s_lastChangedModel = modelUntyped(); }); + } ModelView::setModel( model, isOldModelValid ); } diff --git a/src/gui/clips/AutomationClipView.cpp b/src/gui/clips/AutomationClipView.cpp index 9b71bb74c74..82e9bcde150 100644 --- a/src/gui/clips/AutomationClipView.cpp +++ b/src/gui/clips/AutomationClipView.cpp @@ -30,6 +30,7 @@ #include #include "AutomationEditor.h" +#include "AutomatableModelView.h" #include "embed.h" #include "GuiApplication.h" #include "ProjectJournal.h" @@ -100,6 +101,26 @@ void AutomationClipView::changeName() +void AutomationClipView::connectLastChangedModel() +{ + if (AutomatableModelView::lastChangedModel() != nullptr) + { + bool added = m_clip->addObject(AutomatableModelView::lastChangedModel()); + if (added) + { + update(); + } + else + { + TextFloat::displayMessage(AutomatableModelView::lastChangedModel()->displayName(), + tr("Model is already connected to this clip."), + embed::getIconPixmap("automation"), + 2000); + } + } +} + + void AutomationClipView::disconnectObject( QAction * _a ) { @@ -185,6 +206,14 @@ void AutomationClipView::constructContextMenu( QMenu * _cm ) _cm->addAction( embed::getIconPixmap( "flip_x" ), tr( "Flip Horizontally (Visible)" ), this, SLOT(flipX())); + if (AutomatableModelView::lastChangedModel() != nullptr) + { + _cm->addAction(tr("Connect last changed model (%1)").arg( + !AutomatableModelView::lastChangedModel()->displayName().isEmpty() + ? AutomatableModelView::lastChangedModel()->fullDisplayName() + : "Unknown Model Name"), + this, &AutomationClipView::connectLastChangedModel); + } if (!m_clip->m_objects.empty()) { _cm->addSeparator();