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 15 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
40 changes: 28 additions & 12 deletions include/Clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,41 @@
#include <QDomElement>


class JournallingObject;

class 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
static const QMimeData * getMimeData();
static bool hasFormat( MimeType mT );

private:
static Map content;
// Helper methods for String data
static void copyString( const QString & str, MimeType mT );
IanCaio marked this conversation as resolved.
Show resolved Hide resolved
static QString getString( MimeType mT );

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

static const char * mimeType( MimeType type )
{
switch( type )
{
case Clipboard::MimeType::StringPair:
return "application/x-lmms-stringpair";
break;
case Clipboard::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
5 changes: 3 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 @@ -240,10 +241,10 @@ void Lv2InsView::dragEnterEvent(QDragEnterEvent *_dee)
{
void (QDragEnterEvent::*reaction)(void) = &QDragEnterEvent::ignore;

if (_dee->mimeData()->hasFormat(StringPairDrag::mimeType()))
if (_dee->mimeData()->hasFormat( Clipboard::mimeType( Clipboard::MimeType::StringPair )))
{
const QString txt =
_dee->mimeData()->data(StringPairDrag::mimeType());
_dee->mimeData()->data( Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
if (txt.section(':', 0, 0) == "pluginpresetfile") {
reaction = &QDragEnterEvent::acceptProposedAction;
}
Expand Down
5 changes: 3 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,10 @@ AudioFileProcessorView::~AudioFileProcessorView()

void AudioFileProcessorView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
if( _dee->mimeData()->hasFormat( Clipboard::mimeType( Clipboard::MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == QString( "tco_%1" ).arg(
Track::SampleTrack ) )
{
Expand Down
5 changes: 3 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,10 @@ void PatmanView::updateFilename( void )

void PatmanView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
if( _dee->mimeData()->hasFormat( Clipboard::mimeType( Clipboard::MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "samplefile" )
{
_dee->acceptProposedAction();
Expand Down
9 changes: 5 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,10 @@ void VestigeInstrumentView::noteOffAll( void )

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

void manageVestigeInstrumentView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
if( _dee->mimeData()->hasFormat( Clipboard::mimeType( Clipboard::MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "vstplugin" )
{
_dee->acceptProposedAction();
Expand Down
5 changes: 3 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,10 @@ ZynAddSubFxView::~ZynAddSubFxView()

void ZynAddSubFxView::dragEnterEvent( QDragEnterEvent * _dee )
{
if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
if( _dee->mimeData()->hasFormat( Clipboard::mimeType( Clipboard::MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
StringPairDrag::mimeType() );
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "pluginpresetfile" )
{
_dee->acceptProposedAction();
Expand Down
63 changes: 47 additions & 16 deletions src/core/Clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,68 @@

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

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


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




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



void Clipboard::copy( JournallingObject * _obj )

void Clipboard::copyString( const QString & str, MimeType mT )
{
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 );
QMimeData *content = new QMimeData;

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




const QDomElement * Clipboard::getContent( const QString & _node_name )
QString Clipboard::getString( MimeType mT )
{
if( content.find( _node_name ) != content.end() )
{
return &content[_node_name];
}
return NULL;
return QString( getMimeData()->data( mimeType( mT ) ) );
}




void Clipboard::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 Clipboard::decodeKey( const QMimeData * mimeData )
{
return( QString::fromUtf8( mimeData->data( mimeType( MimeType::StringPair ) ) ).section( ':', 0, 0 ) );
}




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