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

Refactor Clipboard methods #5627

Merged
merged 17 commits into from
Oct 10, 2020
Merged
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
43 changes: 29 additions & 14 deletions include/Clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,40 @@
#include <QDomElement>


class JournallingObject;

class Clipboard
namespace Clipboard
{
public:
typedef QMap<QString, QDomElement> Map;

static void copy( JournallingObject * _object );
static const QDomElement * getContent( const QString & _node_name );

static const char * mimeType()
enum class MimeType
{
return( "application/x-lmms-clipboard" );
}
StringPair,
Default
};

// Convenience Methods
const QMimeData * getMimeData();
bool hasFormat( MimeType mT );

private:
static Map content;
// Helper methods for String data
void copyString( const QString & str, MimeType mT );
QString getString( MimeType mT );

// Helper methods for String Pair data
void copyStringPair( const QString & key, const QString & value );
QString decodeKey( const QMimeData * mimeData );
QString decodeValue( const QMimeData * mimeData );

inline const char * mimeType( MimeType type )
{
switch( type )
{
case MimeType::StringPair:
return "application/x-lmms-stringpair";
break;
case MimeType::Default:
default:
return "application/x-lmms-clipboard";
break;
}
}
} ;

#endif
8 changes: 0 additions & 8 deletions include/StringPairDrag.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,8 @@ class LMMS_EXPORT StringPairDrag : public QDrag

static bool processDragEnterEvent( QDragEnterEvent * _dee,
const QString & _allowed_keys );
static QString decodeMimeKey( const QMimeData * mimeData );
static QString decodeMimeValue( const QMimeData * mimeData );
static QString decodeKey( QDropEvent * _de );
static QString decodeValue( QDropEvent * _de );

static const char * mimeType()
{
return( "application/x-lmms-stringpair" );
}

} ;


Expand Down
6 changes: 3 additions & 3 deletions include/Track.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ class LMMS_EXPORT TrackContentObject : public Model, public JournallingObject
MidiTime startTimeOffset() const;
void setStartTimeOffset( const MidiTime &startTimeOffset );

// Will copy the state of a TCO to another TCO
static void copyStateTo( TrackContentObject *src, TrackContentObject *dst );

public slots:
void copy();
void paste();
void toggleMute();


Expand Down Expand Up @@ -266,7 +267,6 @@ class TrackContentObjectView : public selectableObject, public ModelView

public slots:
virtual bool close();
void cut();
void remove();
void update() override;

Expand Down
8 changes: 6 additions & 2 deletions plugins/Lv2Instrument/Lv2Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "Lv2SubPluginFeatures.h"
#include "Mixer.h"
#include "StringPairDrag.h"
#include "Clipboard.h"

#include "embed.h"
#include "plugin_export.h"
Expand Down Expand Up @@ -238,12 +239,15 @@ Lv2InsView::Lv2InsView(Lv2Instrument *_instrument, QWidget *_parent) :

void Lv2InsView::dragEnterEvent(QDragEnterEvent *_dee)
{
// For mimeType() and MimeType enum class
using namespace Clipboard;

void (QDragEnterEvent::*reaction)(void) = &QDragEnterEvent::ignore;

if (_dee->mimeData()->hasFormat(StringPairDrag::mimeType()))
if (_dee->mimeData()->hasFormat( mimeType( MimeType::StringPair )))
{
const QString txt =
_dee->mimeData()->data(StringPairDrag::mimeType());
_dee->mimeData()->data( mimeType( MimeType::StringPair ) );
if (txt.section(':', 0, 0) == "pluginpresetfile") {
reaction = &QDragEnterEvent::acceptProposedAction;
}
Expand Down
8 changes: 6 additions & 2 deletions plugins/audio_file_processor/audio_file_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "Song.h"
#include "StringPairDrag.h"
#include "ToolTip.h"
#include "Clipboard.h"

#include "embed.h"
#include "plugin_export.h"
Expand Down Expand Up @@ -568,10 +569,13 @@ AudioFileProcessorView::~AudioFileProcessorView()

void AudioFileProcessorView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
// For mimeType() and MimeType enum class
using namespace Clipboard;

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == QString( "tco_%1" ).arg(
Track::SampleTrack ) )
{
Expand Down
8 changes: 6 additions & 2 deletions plugins/patman/patman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "Song.h"
#include "StringPairDrag.h"
#include "ToolTip.h"
#include "Clipboard.h"

#include "embed.h"

Expand Down Expand Up @@ -580,10 +581,13 @@ void PatmanView::updateFilename( void )

void PatmanView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
// For mimeType() and MimeType enum class
using namespace Clipboard;

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "samplefile" )
{
_dee->acceptProposedAction();
Expand Down
15 changes: 11 additions & 4 deletions plugins/vestige/vestige.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "StringPairDrag.h"
#include "TextFloat.h"
#include "ToolTip.h"
#include "Clipboard.h"


#include "embed.h"
Expand Down Expand Up @@ -833,10 +834,13 @@ void VestigeInstrumentView::noteOffAll( void )

void VestigeInstrumentView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
// For mimeType() and MimeType enum class
using namespace Clipboard;

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "vstplugin" )
{
_dee->acceptProposedAction();
Expand Down Expand Up @@ -1175,10 +1179,13 @@ void manageVestigeInstrumentView::syncParameterText()

void manageVestigeInstrumentView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
// For mimeType() and MimeType enum class
using namespace Clipboard;

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "vstplugin" )
{
_dee->acceptProposedAction();
Expand Down
8 changes: 6 additions & 2 deletions plugins/zynaddsubfx/ZynAddSubFx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "LocalZynAddSubFx.h"
#include "Mixer.h"
#include "ControllerConnection.h"
#include "Clipboard.h"

#include "embed.h"
#include "plugin_export.h"
Expand Down Expand Up @@ -578,10 +579,13 @@ ZynAddSubFxView::~ZynAddSubFxView()

void ZynAddSubFxView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
// For mimeType() and MimeType enum class
using namespace Clipboard;

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "pluginpresetfile" )
{
_dee->acceptProposedAction();
Expand Down
70 changes: 52 additions & 18 deletions src/core/Clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,71 @@

#include <QApplication>
#include <QClipboard>
#include <QMimeData>

#include "Clipboard.h"
#include "JournallingObject.h"


Clipboard::Map Clipboard::content;
namespace Clipboard
{
const QMimeData * getMimeData()
{
return QApplication::clipboard()->mimeData( QClipboard::Clipboard );
}


void Clipboard::copy( JournallingObject * _obj )
{
QDomDocument doc;
QDomElement parent = doc.createElement( "Clipboard" );
_obj->saveState( doc, parent );
content[_obj->nodeName()] = parent.firstChild().toElement();

// Clear the QApplication clipboard, so we don't have any conflicts when LMMS has to
// decide between the QApplication clipboard and the internal clipboard data
QApplication::clipboard()->clear( QClipboard::Clipboard );
}


bool hasFormat( MimeType mT )
{
return getMimeData()->hasFormat( mimeType( mT ) );
}


const QDomElement * Clipboard::getContent( const QString & _node_name )
{
if( content.find( _node_name ) != content.end() )


void copyString( const QString & str, MimeType mT )
{
return &content[_node_name];
QMimeData *content = new QMimeData;

content->setData( mimeType( mT ), str.toUtf8() );
QApplication::clipboard()->setMimeData( content, QClipboard::Clipboard );
}




QString getString( MimeType mT )
{
return QString( getMimeData()->data( mimeType( mT ) ) );
}
return NULL;
}




void copyStringPair( const QString & key, const QString & value )
{
QString finalString = key + ":" + value;

QMimeData *content = new QMimeData;
content->setData( mimeType( MimeType::StringPair ), finalString.toUtf8() );
QApplication::clipboard()->setMimeData( content, QClipboard::Clipboard );
}




QString decodeKey( const QMimeData * mimeData )
{
return( QString::fromUtf8( mimeData->data( mimeType( MimeType::StringPair ) ) ).section( ':', 0, 0 ) );
}




QString decodeValue( const QMimeData * mimeData )
{
return( QString::fromUtf8( mimeData->data( mimeType( MimeType::StringPair ) ) ).section( ':', 1, -1 ) );
}
}
Loading