Skip to content

Commit

Permalink
Makes Clipboard a namespace instead of a class
Browse files Browse the repository at this point in the history
	Makes Clipboard a namespace instead of a class and adds "using namespace Clipboard;" statements to methods that required functions from that namespace. The only two methods where the statement wasn't added were StringPairDrag::decodeKey and StringPairDrag::decodeValue because they are both one-liners.
  • Loading branch information
IanCaio committed Sep 17, 2020
1 parent 53bb73d commit f194146
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 71 deletions.
23 changes: 11 additions & 12 deletions include/Clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,35 @@
#include <QDomElement>


class Clipboard
namespace Clipboard
{
public:
enum class MimeType
{
StringPair,
Default
};

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

// Helper methods for String data
static void copyString( const QString & str, MimeType mT );
static QString getString( MimeType mT );
void copyString( const QString & str, MimeType mT );
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 );
void copyStringPair( const QString & key, const QString & value );
QString decodeKey( const QMimeData * mimeData );
QString decodeValue( const QMimeData * mimeData );

static const char * mimeType( MimeType type )
inline const char * mimeType( MimeType type )
{
switch( type )
{
case Clipboard::MimeType::StringPair:
case MimeType::StringPair:
return "application/x-lmms-stringpair";
break;
case Clipboard::MimeType::Default:
case MimeType::Default:
default:
return "application/x-lmms-clipboard";
break;
Expand Down
7 changes: 5 additions & 2 deletions plugins/Lv2Instrument/Lv2Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,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( Clipboard::mimeType( Clipboard::MimeType::StringPair )))
if (_dee->mimeData()->hasFormat( mimeType( MimeType::StringPair )))
{
const QString txt =
_dee->mimeData()->data( Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
_dee->mimeData()->data( mimeType( MimeType::StringPair ) );
if (txt.section(':', 0, 0) == "pluginpresetfile") {
reaction = &QDragEnterEvent::acceptProposedAction;
}
Expand Down
7 changes: 5 additions & 2 deletions plugins/audio_file_processor/audio_file_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,13 @@ AudioFileProcessorView::~AudioFileProcessorView()

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

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == QString( "tco_%1" ).arg(
Track::SampleTrack ) )
{
Expand Down
7 changes: 5 additions & 2 deletions plugins/patman/patman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,13 @@ void PatmanView::updateFilename( void )

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

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "samplefile" )
{
_dee->acceptProposedAction();
Expand Down
14 changes: 10 additions & 4 deletions plugins/vestige/vestige.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,13 @@ void VestigeInstrumentView::noteOffAll( void )

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

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

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

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "vstplugin" )
{
_dee->acceptProposedAction();
Expand Down
7 changes: 5 additions & 2 deletions plugins/zynaddsubfx/ZynAddSubFx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,13 @@ ZynAddSubFxView::~ZynAddSubFxView()

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

if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
{
QString txt = _dee->mimeData()->data(
Clipboard::mimeType( Clipboard::MimeType::StringPair ) );
mimeType( MimeType::StringPair ) );
if( txt.section( ':', 0, 0 ) == "pluginpresetfile" )
{
_dee->acceptProposedAction();
Expand Down
65 changes: 34 additions & 31 deletions src/core/Clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,62 +30,65 @@
#include "JournallingObject.h"


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




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




void Clipboard::copyString( const QString & str, MimeType mT )
{
QMimeData *content = new QMimeData;
void copyString( const QString & str, MimeType mT )
{
QMimeData *content = new QMimeData;

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




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




void Clipboard::copyStringPair( const QString & key, const QString & value )
{
QString finalString = key + ":" + value;
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 );
}
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 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 ) );
QString decodeValue( const QMimeData * mimeData )
{
return( QString::fromUtf8( mimeData->data( mimeType( MimeType::StringPair ) ) ).section( ':', 1, -1 ) );
}
}
36 changes: 27 additions & 9 deletions src/core/Track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,11 +1216,14 @@ void TrackContentObjectView::remove( QVector<TrackContentObjectView *> tcovs )

void TrackContentObjectView::copy( QVector<TrackContentObjectView *> tcovs )
{
// For copyStringPair()
using namespace Clipboard;

// Write the TCOs to a DataFile for copying
DataFile dataFile = createTCODataFiles( tcovs );

// Copy the TCO type as a key and the TCO data file to the clipboard
Clipboard::copyStringPair( QString( "tco_%1" ).arg( m_tco->getTrack()->type() ),
copyStringPair( QString( "tco_%1" ).arg( m_tco->getTrack()->type() ),
dataFile.toString() );
}

Expand All @@ -1235,12 +1238,15 @@ void TrackContentObjectView::cut( QVector<TrackContentObjectView *> tcovs )

void TrackContentObjectView::paste()
{
// For getMimeData()
using namespace Clipboard;

// If possible, paste the selection on the MidiTime of the selected Track and remove it
MidiTime tcoPos = MidiTime( m_tco->startPosition() );

TrackContentWidget *tcw = getTrackView()->getTrackContentWidget();

if( tcw->pasteSelection( tcoPos, Clipboard::getMimeData() ) )
if( tcw->pasteSelection( tcoPos, getMimeData() ) )
{
// If we succeed on the paste we delete the TCO we pasted on
remove();
Expand Down Expand Up @@ -1641,9 +1647,12 @@ bool TrackContentWidget::canPasteSelection( MidiTime tcoPos, const QDropEvent* d
// Overloaded method to make it possible to call this method without a Drag&Drop event
bool TrackContentWidget::canPasteSelection( MidiTime tcoPos, const QMimeData* md , bool allowSameBar )
{
// For decodeKey() and decodeValue()
using namespace Clipboard;

Track * t = getTrack();
QString type = Clipboard::decodeKey( md );
QString value = Clipboard::decodeValue( md );
QString type = decodeKey( md );
QString value = decodeValue( md );

// We can only paste into tracks of the same type
if( type != ( "tco_" + QString::number( t->type() ) ) ||
Expand Down Expand Up @@ -1727,14 +1736,17 @@ bool TrackContentWidget::pasteSelection( MidiTime tcoPos, QDropEvent * de )
// Overloaded method so we can call it without a Drag&Drop event
bool TrackContentWidget::pasteSelection( MidiTime tcoPos, const QMimeData * md, bool skipSafetyCheck )
{
// For decodeKey() and decodeValue()
using namespace Clipboard;

// When canPasteSelection was already called before, skipSafetyCheck will skip this
if( !skipSafetyCheck && canPasteSelection( tcoPos, md ) == false )
{
return false;
}

QString type = Clipboard::decodeKey( md );
QString value = Clipboard::decodeValue( md );
QString type = decodeKey( md );
QString value = decodeValue( md );

getTrack()->addJournalCheckPoint();

Expand Down Expand Up @@ -1928,14 +1940,17 @@ MidiTime TrackContentWidget::endPosition( const MidiTime & posStart )

void TrackContentWidget::contextMenuEvent( QContextMenuEvent * cme )
{
// For hasFormat(), MimeType enum class and getMimeData()
using namespace Clipboard;

if( cme->modifiers() )
{
return;
}

// If we don't have TCO data in the clipboard there's no need to create this menu
// since "paste" is the only action at the moment.
if( ! Clipboard::hasFormat( Clipboard::MimeType::StringPair ) )
if( ! hasFormat( MimeType::StringPair ) )
{
return;
}
Expand All @@ -1944,20 +1959,23 @@ void TrackContentWidget::contextMenuEvent( QContextMenuEvent * cme )
QAction *pasteA = contextMenu.addAction( embed::getIconPixmap( "edit_paste" ),
tr( "Paste" ), [this, cme](){ contextMenuAction( cme, Paste ); } );
// If we can't paste in the current TCW for some reason, disable the action so the user knows
pasteA->setEnabled( canPasteSelection( getPosition( cme->x() ), Clipboard::getMimeData() ) ? true : false );
pasteA->setEnabled( canPasteSelection( getPosition( cme->x() ), getMimeData() ) ? true : false );

contextMenu.exec( QCursor::pos() );
}

void TrackContentWidget::contextMenuAction( QContextMenuEvent * cme, ContextMenuAction action )
{
// For getMimeData()
using namespace Clipboard;

switch( action )
{
case Paste:
// Paste the selection on the MidiTime of the context menu event
MidiTime tcoPos = getPosition( cme->x() );

pasteSelection( tcoPos, Clipboard::getMimeData() );
pasteSelection( tcoPos, getMimeData() );
break;
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/gui/AutomatableModelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ void AutomatableModelViewSlots::unlinkAllModels()

void AutomatableModelViewSlots::copyToClipboard()
{
Clipboard::copyString( QString::number(m_amv->value<float>()), Clipboard::MimeType::Default );
// For copyString() and MimeType enum class
using namespace Clipboard;

copyString( QString::number(m_amv->value<float>()), MimeType::Default );
}

void AutomatableModelViewSlots::pasteFromClipboard()
Expand All @@ -258,6 +261,9 @@ void AutomatableModelViewSlots::pasteFromClipboard()
/// Attempt to parse a float from the clipboard
static float floatFromClipboard(bool* ok)
{
return Clipboard::getString( Clipboard::MimeType::Default ).toFloat(ok);
// For getString() and MimeType enum class
using namespace Clipboard;

return getString( MimeType::Default ).toFloat(ok);
}

Loading

0 comments on commit f194146

Please sign in to comment.