|
| 1 | +/*************************************************************************** |
| 2 | + qgsmaplayerstyleguiutils.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : January 2015 |
| 5 | + Copyright : (C) 2015 by Martin Dobias |
| 6 | + Email : wonder dot sk at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgsmaplayerstyleguiutils.h" |
| 17 | + |
| 18 | +#include <QAction> |
| 19 | +#include <QInputDialog> |
| 20 | +#include <QMenu> |
| 21 | + |
| 22 | +#include "qgslogger.h" |
| 23 | +#include "qgsmapcanvas.h" |
| 24 | +#include "qgsmaplayer.h" |
| 25 | +#include "qgsmaplayerstylemanager.h" |
| 26 | + |
| 27 | + |
| 28 | +QMenu* QgsMapLayerStyleGuiUtils::createStyleManagerMenu( QgsMapLayer* layer ) |
| 29 | +{ |
| 30 | + QMenu* m = new QMenu( tr( "Styles" ) ); |
| 31 | + QAction* actionAdd = m->addAction( tr( "Add..." ), this, SLOT( addStyle() ) ); |
| 32 | + actionAdd->setData( QVariant::fromValue<QObject*>( layer ) ); |
| 33 | + |
| 34 | + QgsMapLayerStyleManager* mgr = layer->styleManager(); |
| 35 | + |
| 36 | + if ( !mgr ) |
| 37 | + return m; |
| 38 | + |
| 39 | + QMenu* mRemove = m->addMenu( tr( "Remove" ) ); |
| 40 | + m->addSeparator(); |
| 41 | + |
| 42 | + foreach ( QString name, mgr->styles() ) |
| 43 | + { |
| 44 | + bool active = name == mgr->currentStyle(); |
| 45 | + if ( name.isEmpty() ) |
| 46 | + name = defaultStyleName(); |
| 47 | + QAction* actionUse = m->addAction( name, this, SLOT( useStyle() ) ); |
| 48 | + actionUse->setCheckable( true ); |
| 49 | + actionUse->setChecked( active ); |
| 50 | + actionUse->setData( QVariant::fromValue<QObject*>( layer ) ); |
| 51 | + |
| 52 | + QAction* actionRemove = mRemove->addAction( name, this, SLOT( removeStyle() ) ); |
| 53 | + actionRemove->setData( QVariant::fromValue<QObject*>( layer ) ); |
| 54 | + } |
| 55 | + |
| 56 | + return m; |
| 57 | +} |
| 58 | + |
| 59 | +QString QgsMapLayerStyleGuiUtils::defaultStyleName() |
| 60 | +{ |
| 61 | + return tr( "(default)" ); |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +void QgsMapLayerStyleGuiUtils::addStyle() |
| 66 | +{ |
| 67 | + QAction* a = qobject_cast<QAction*>( sender() ); |
| 68 | + if ( !a ) |
| 69 | + return; |
| 70 | + QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( a->data().value<QObject*>() ); |
| 71 | + if ( !layer ) |
| 72 | + return; |
| 73 | + |
| 74 | + bool ok; |
| 75 | + QString text = QInputDialog::getText( 0, tr( "New style" ), |
| 76 | + tr( "Style name:" ), QLineEdit::Normal, |
| 77 | + "new style", &ok ); |
| 78 | + if ( !ok || text.isEmpty() ) |
| 79 | + return; |
| 80 | + |
| 81 | + layer->enableStyleManager(); // make sure it exists |
| 82 | + |
| 83 | + bool res = layer->styleManager()->addStyleFromLayer( text ); |
| 84 | + |
| 85 | + if ( res ) // make it active! |
| 86 | + layer->styleManager()->setCurrentStyle( text ); |
| 87 | + else |
| 88 | + QgsDebugMsg( "Failed to add style: " + text ); |
| 89 | +} |
| 90 | + |
| 91 | +void QgsMapLayerStyleGuiUtils::useStyle() |
| 92 | +{ |
| 93 | + QAction* a = qobject_cast<QAction*>( sender() ); |
| 94 | + if ( !a ) |
| 95 | + return; |
| 96 | + QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( a->data().value<QObject*>() ); |
| 97 | + if ( !layer ) |
| 98 | + return; |
| 99 | + QString name = a->text(); |
| 100 | + if ( name == defaultStyleName() ) |
| 101 | + name.clear(); |
| 102 | + |
| 103 | + bool res = layer->styleManager()->setCurrentStyle( name ); |
| 104 | + if ( !res ) |
| 105 | + QgsDebugMsg( "Failed to set current style: " + name ); |
| 106 | + |
| 107 | + layer->triggerRepaint(); |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +void QgsMapLayerStyleGuiUtils::removeStyle() |
| 112 | +{ |
| 113 | + QAction* a = qobject_cast<QAction*>( sender() ); |
| 114 | + if ( !a ) |
| 115 | + return; |
| 116 | + QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( a->data().value<QObject*>() ); |
| 117 | + if ( !layer ) |
| 118 | + return; |
| 119 | + |
| 120 | + if ( layer->styleManager()->styles().count() == 1 ) |
| 121 | + { |
| 122 | + // let's get rid of the style manager altogether |
| 123 | + layer->enableStyleManager( false ); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + QString name = a->text(); |
| 128 | + if ( name == defaultStyleName() ) |
| 129 | + name.clear(); |
| 130 | + |
| 131 | + bool needsRefresh = ( layer->styleManager()->currentStyle() == name ); |
| 132 | + |
| 133 | + bool res = layer->styleManager()->removeStyle( name ); |
| 134 | + if ( !res ) |
| 135 | + QgsDebugMsg( "Failed to remove style: " + name ); |
| 136 | + |
| 137 | + if ( needsRefresh ) |
| 138 | + layer->triggerRepaint(); |
| 139 | +} |
0 commit comments