From 56b4740146cddf02c76ed0d72063e8eaabdc397f Mon Sep 17 00:00:00 2001 From: Colin Wallace Date: Thu, 15 Mar 2018 18:45:13 -0700 Subject: [PATCH] Copy/paste model values to system clipboard Previously they were copy/pasted internally, and not visible across LMMS instances. --- include/AutomatableModel.h | 9 ------- include/AutomatableModelView.h | 6 ++++- src/core/AutomatableModel.cpp | 16 ------------ src/gui/AutomatableModelView.cpp | 44 ++++++++++++++++++++++++++------ 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/include/AutomatableModel.h b/include/AutomatableModel.h index 9c24fd9bb2a..ae9d5695111 100644 --- a/include/AutomatableModel.h +++ b/include/AutomatableModel.h @@ -96,11 +96,6 @@ class EXPORT AutomatableModel : public Model, public JournallingObject virtual ~AutomatableModel(); - static float copiedValue() - { - return s_copiedValue; - } - bool isAutomated() const; bool isAutomatedOrControlled() const { @@ -288,8 +283,6 @@ class EXPORT AutomatableModel : public Model, public JournallingObject public slots: virtual void reset(); - virtual void copyValue(); - virtual void pasteValue(); void unlinkControllerConnection(); @@ -352,8 +345,6 @@ public slots: ControllerConnection* m_controllerConnection; - static float s_copiedValue; - ValueBuffer m_valueBuffer; long m_lastUpdatedPeriod; static long s_periodCounter; diff --git a/include/AutomatableModelView.h b/include/AutomatableModelView.h index 81e466da506..ead5d81af46 100644 --- a/include/AutomatableModelView.h +++ b/include/AutomatableModelView.h @@ -75,7 +75,6 @@ class EXPORT AutomatableModelView : public ModelView QString m_description; QString m_unit; - } ; @@ -94,6 +93,11 @@ public slots: void unlinkAllModels(); void removeSongGlobalAutomation(); +private slots: + /// Copy the model's value to the clipboard. + void copyToClipboard(); + /// Paste the model's value from the clipboard. + void pasteFromClipboard(); protected: AutomatableModelView* m_amv; diff --git a/src/core/AutomatableModel.cpp b/src/core/AutomatableModel.cpp index e2a088e04dd..7c16179fd86 100644 --- a/src/core/AutomatableModel.cpp +++ b/src/core/AutomatableModel.cpp @@ -31,7 +31,6 @@ #include "Mixer.h" #include "ProjectJournal.h" -float AutomatableModel::s_copiedValue = 0; long AutomatableModel::s_periodCounter = 0; @@ -635,21 +634,6 @@ void AutomatableModel::reset() -void AutomatableModel::copyValue() -{ - s_copiedValue = value(); -} - - - - -void AutomatableModel::pasteValue() -{ - setValue( copiedValue() ); -} - - - float AutomatableModel::globalAutomationValueAt( const MidiTime& time ) { // get patterns that connect to this model diff --git a/src/gui/AutomatableModelView.cpp b/src/gui/AutomatableModelView.cpp index a51d71ee727..973e5c81794 100644 --- a/src/gui/AutomatableModelView.cpp +++ b/src/gui/AutomatableModelView.cpp @@ -22,6 +22,8 @@ * */ +#include +#include #include #include @@ -37,6 +39,7 @@ #include "AutomationEditor.h" +static float floatFromClipboard(bool* ok=nullptr); AutomatableModelView::AutomatableModelView( ::Model* model, QWidget* _this ) : ModelView( model, _this ), @@ -74,13 +77,18 @@ void AutomatableModelView::addDefaultActions( QMenu* menu ) AutomatableModel::tr( "&Copy value (%1%2)" ). arg( model->displayValue( model->value() ) ). arg( m_unit ), - model, SLOT( copyValue() ) ); - - menu->addAction( embed::getIconPixmap( "edit_paste" ), - AutomatableModel::tr( "&Paste value (%1%2)"). - arg( model->displayValue( AutomatableModel::copiedValue() ) ). - arg( m_unit ), - model, SLOT( pasteValue() ) ); + amvSlots, SLOT( copyToClipboard() ) ); + + bool canPaste = true; + const float valueToPaste = floatFromClipboard(&canPaste); + const QString pasteDesc = canPaste ? + AutomatableModel::tr( "&Paste value (%1%2)"). + arg( model->displayValue( valueToPaste ) ). + arg( m_unit ) + : AutomatableModel::tr( "&Paste value"); + QAction* pasteAction = menu->addAction( embed::getIconPixmap( "edit_paste" ), + pasteDesc, amvSlots, SLOT( pasteFromClipboard() ) ); + pasteAction->setEnabled(canPaste); menu->addSeparator(); @@ -162,7 +170,6 @@ void AutomatableModelView::mousePressEvent( QMouseEvent* event ) - AutomatableModelViewSlots::AutomatableModelViewSlots( AutomatableModelView* amv, QObject* parent ) : QObject(), m_amv( amv ) @@ -245,5 +252,26 @@ void AutomatableModelViewSlots::unlinkAllModels() m_amv->modelUntyped()->unlinkAllModels(); } +void AutomatableModelViewSlots::copyToClipboard() +{ + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText(QString::number(m_amv->value())); +} + +void AutomatableModelViewSlots::pasteFromClipboard() +{ + bool isNumber = false; + const float number = floatFromClipboard(&isNumber); + if (isNumber) { + m_amv->modelUntyped()->setValue(number); + } +} +/// Attempt to parse a float from the clipboard +static float floatFromClipboard(bool* ok) +{ + const QClipboard* clipboard = QApplication::clipboard(); + return clipboard->text().toFloat(ok); +} +