Skip to content

Commit

Permalink
[FEATURE] Allow copying symbols from style manager
Browse files Browse the repository at this point in the history
Allows symbols to be copied from the style manager dialog, so that
they can be pasted elsewhere in QGIS (e.g. in symbol or color buttons)
  • Loading branch information
nyalldawson committed Jul 25, 2019
1 parent 4e33bc1 commit 732bc29
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/gui/symbology/qgsstylemanagerdialog.cpp
Expand Up @@ -41,6 +41,7 @@
#include <QPushButton>
#include <QStandardItemModel>
#include <QMenu>
#include <QClipboard>

#include "qgsapplication.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -222,6 +223,9 @@ QgsStyleManagerDialog::QgsStyleManagerDialog( QgsStyle *style, QWidget *parent,
mCopyToDefaultButton->hide();
}

mActionCopySymbol = new QAction( tr( "Copy Item" ), this );
connect( mActionCopySymbol, &QAction::triggered, this, &QgsStyleManagerDialog::copyItem );

shareMenu->addSeparator();
shareMenu->addAction( actnExportAsPNG );
shareMenu->addAction( actnExportAsSVG );
Expand Down Expand Up @@ -366,6 +370,7 @@ QgsStyleManagerDialog::QgsStyleManagerDialog( QgsStyle *style, QWidget *parent,
mGroupMenu->addSeparator()->setParent( this );
mGroupMenu->addAction( actnRemoveItem );
mGroupMenu->addAction( actnEditItem );
mGroupMenu->addAction( mActionCopySymbol );
mGroupMenu->addSeparator()->setParent( this );
}
else
Expand All @@ -376,6 +381,8 @@ QgsStyleManagerDialog::QgsStyleManagerDialog( QgsStyle *style, QWidget *parent,
btnAddSmartgroup->setVisible( false );
btnAddTag->setVisible( false );
btnManageGroups->setVisible( false );

mGroupMenu->addAction( mActionCopySymbol );
}
if ( mActionCopyToDefault )
{
Expand Down Expand Up @@ -532,6 +539,46 @@ void QgsStyleManagerDialog::copyItemsToDefault()
}
}

void QgsStyleManagerDialog::copyItem()
{
const QList< ItemDetails > items = selectedItems();
if ( items.empty() )
return;

ItemDetails details = items.at( 0 );
switch ( details.entityType )
{
case QgsStyle::SymbolEntity:
{
std::unique_ptr< QgsSymbol > symbol( mStyle->symbol( details.name ) );
if ( !symbol )
return;
QApplication::clipboard()->setMimeData( QgsSymbolLayerUtils::symbolToMimeData( symbol.get() ) );
break;
}

case QgsStyle::TextFormatEntity:
{
const QgsTextFormat format( mStyle->textFormat( details.name ) );
QApplication::clipboard()->setMimeData( format.toMimeData() );
break;
}

case QgsStyle::LabelSettingsEntity:
{
const QgsTextFormat format( mStyle->labelSettings( details.name ).format() );
QApplication::clipboard()->setMimeData( format.toMimeData() );
break;
}

case QgsStyle::ColorrampEntity:
case QgsStyle::TagEntity:
case QgsStyle::SmartgroupEntity:
return;

};
}

int QgsStyleManagerDialog::selectedItemType()
{
QModelIndex index = listItems->selectionModel()->currentIndex();
Expand Down Expand Up @@ -2076,6 +2123,7 @@ void QgsStyleManagerDialog::enableItemsForGroupingMode( bool enable )
// The actions
actnRemoveItem->setEnabled( enable );
actnEditItem->setEnabled( enable );
mActionCopySymbol->setEnabled( enable );
}

void QgsStyleManagerDialog::grouptreeContextMenu( QPoint point )
Expand Down Expand Up @@ -2120,6 +2168,9 @@ void QgsStyleManagerDialog::listitemsContextMenu( QPoint point )
mGroupListMenu->addAction( a );
}

const QList< ItemDetails > items = selectedItems();
mActionCopySymbol->setEnabled( !items.isEmpty() && ( items.at( 0 ).entityType != QgsStyle::ColorrampEntity ) );

mGroupMenu->popup( globalPos );
}

Expand Down
4 changes: 4 additions & 0 deletions src/gui/symbology/qgsstylemanagerdialog.h
Expand Up @@ -331,6 +331,8 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan

void copyItemsToDefault();

void copyItem();

private:
int selectedItemType();

Expand Down Expand Up @@ -391,6 +393,8 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan

QAction *mActionCopyToDefault = nullptr;

QAction *mActionCopySymbol = nullptr;

int mBlockGroupUpdates = 0;

bool mReadOnly = false;
Expand Down

0 comments on commit 732bc29

Please sign in to comment.