Skip to content

Commit

Permalink
[styledock] Add saved style manager
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Jun 3, 2016
1 parent e079860 commit 9d780ba
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 6 deletions.
2 changes: 2 additions & 0 deletions python/gui/gui.sip
Expand Up @@ -21,6 +21,7 @@

%Include qgsblendmodecombobox.sip


%Include qgisinterface.sip
%Include qgsactionmenu.sip
%Include qgsadvanceddigitizingcanvasitem.sip
Expand Down Expand Up @@ -119,6 +120,7 @@
%Include qgsmaptooltouch.sip
%Include qgsmaptoolzoom.sip
%Include qgsmapstylepanel.sip
%Include qgsmaplayerstylemanagerwidget.sip
%Include qgsmessagebar.sip
%Include qgsmessagebaritem.sip
%Include qgsmessagelogviewer.sip
Expand Down
22 changes: 22 additions & 0 deletions python/gui/qgsmaplayerstylemanagerwidget.sip
@@ -0,0 +1,22 @@
/**
* @brief The QgsMapLayerStyleManagerWidget class which is used to visually manage
* the layer styles.
*/
class QgsMapLayerStyleManagerWidget : QgsMapStylePanel
{
%TypeHeaderCode
#include "qgsmaplayerstylemanagerwidget.h"
%End
public:

/**
* @brief Style manager widget to mange the layers styles.
* @param layer The layer for the widget
* @param canvas The canvas object.
* @param parent The parent.
*/
QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas* canvas, QWidget *parent = 0 );

public slots:
void apply();
};
4 changes: 3 additions & 1 deletion python/gui/qgsmapstylepanel.sip
Expand Up @@ -47,6 +47,8 @@ class QgsMapStylePanelFactory
#include <qgsmapstylepanel.h>
%End
public:
typedef QFlags<QgsMapLayer::LayerType> LayerTypesFlags;

/** Constructor */
QgsMapStylePanelFactory();

Expand All @@ -70,7 +72,7 @@ class QgsMapStylePanelFactory
* @brief Supported layer type for the widget.
* @return The layer type this widget is supported for.
*/
virtual QgsMapLayer::LayerType layerType() = 0;
virtual LayerTypesFlags layerType() = 0;

/**
* @brief Factory fucntion to create the widget on demand as needed by the dock.
Expand Down
39 changes: 38 additions & 1 deletion src/app/qgsmapstylingwidget.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsmaplayerregistry.h"
#include "qgsrasterlayer.h"
#include "qgsmapstylepanel.h"
#include "qgsmaplayerstylemanagerwidget.h"

QgsMapStylingWidget::QgsMapStylingWidget( QgsMapCanvas* canvas, QList<QgsMapStylePanelFactory*> pages, QWidget *parent )
: QWidget( parent )
Expand All @@ -48,6 +49,9 @@ QgsMapStylingWidget::QgsMapStylingWidget( QgsMapCanvas* canvas, QList<QgsMapStyl
mUndoWidget = new QgsUndoWidget( this, mMapCanvas );
mUndoWidget->setObjectName( "Undo Styles" );
mUndoWidget->hide();

mStyleManagerFactory = new QgsMapLayerStyleManagerWidgetFactory();

connect( mUndoButton, SIGNAL( pressed() ), mUndoWidget, SLOT( undo() ) );
connect( mRedoButton, SIGNAL( pressed() ), mUndoWidget, SLOT( redo() ) );

Expand All @@ -61,6 +65,18 @@ QgsMapStylingWidget::QgsMapStylingWidget( QgsMapCanvas* canvas, QList<QgsMapStyl
mButtonBox->button( QDialogButtonBox::Apply )->setEnabled( false );
}

QgsMapStylingWidget::~QgsMapStylingWidget()
{
delete mStyleManagerFactory;
}

void QgsMapStylingWidget::setPageFactories( QList<QgsMapStylePanelFactory *> factories )
{
mPageFactories = factories;
// Always append the style manager factory at the bottom of the list
mPageFactories.append( mStyleManagerFactory );
}

void QgsMapStylingWidget::setLayer( QgsMapLayer *layer )
{
if ( !layer || !layer->isSpatial() )
Expand Down Expand Up @@ -95,7 +111,7 @@ void QgsMapStylingWidget::setLayer( QgsMapLayer *layer )

Q_FOREACH ( QgsMapStylePanelFactory* factory, mPageFactories )
{
if ( factory->layerType() == layer->type() )
if ( factory->layerType().testFlag( layer->type() ) )
{
QListWidgetItem* item = new QListWidgetItem( factory->icon(), "" );
mOptionsListWidget->addItem( item );
Expand Down Expand Up @@ -363,3 +379,24 @@ void QgsMapLayerStyleCommand::redo()
mLayer->readStyle( mXml, error );
mLayer->triggerRepaint();
}

QIcon QgsMapLayerStyleManagerWidgetFactory::icon()
{
return QgsApplication::getThemeIcon( "propertyicons/symbology.png" );
}

QString QgsMapLayerStyleManagerWidgetFactory::title()
{
return QString();
}

QgsMapStylePanel *QgsMapLayerStyleManagerWidgetFactory::createPanel( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
{
return new QgsMapLayerStyleManagerWidget( layer, canvas, parent );

}

QgsMapStylePanelFactory::LayerTypesFlags QgsMapLayerStyleManagerWidgetFactory::layerType()
{
return QgsMapLayer::VectorLayer;
}
15 changes: 14 additions & 1 deletion src/app/qgsmapstylingwidget.h
Expand Up @@ -13,6 +13,7 @@
#include <QTimer>

#include "ui_qgsmapstylingwidgetbase.h"
#include "qgsmapstylepanel.h"

class QgsLabelingWidget;
class QgsMapLayer;
Expand All @@ -22,6 +23,16 @@ class QgsRendererRasterPropertiesWidget;
class QgsUndoWidget;
class QgsRasterHistogramWidget;
class QgsMapStylePanelFactory;
class QgsMapLayerStyleManagerWidget;

class APP_EXPORT QgsMapLayerStyleManagerWidgetFactory : public QgsMapStylePanelFactory
{
public:
QIcon icon() override;
QString title() override;
QgsMapStylePanel *createPanel( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent ) override;
LayerTypesFlags layerType() override;
};

class APP_EXPORT QgsMapLayerStyleCommand : public QUndoCommand
{
Expand All @@ -42,9 +53,10 @@ class APP_EXPORT QgsMapStylingWidget : public QWidget, private Ui::QgsMapStyling
Q_OBJECT
public:
QgsMapStylingWidget( QgsMapCanvas *canvas, QList<QgsMapStylePanelFactory *> pages, QWidget *parent = 0 );
~QgsMapStylingWidget();
QgsMapLayer* layer() { return mCurrentLayer; }

void setPageFactories( QList<QgsMapStylePanelFactory*> factories ) { mPageFactories = factories; }
void setPageFactories( QList<QgsMapStylePanelFactory*> factories );

signals:
void styleChanged( QgsMapLayer* layer );
Expand Down Expand Up @@ -73,6 +85,7 @@ class APP_EXPORT QgsMapStylingWidget : public QWidget, private Ui::QgsMapStyling
QgsRendererRasterPropertiesWidget* mRasterStyleWidget;
QList<QgsMapStylePanelFactory*> mPageFactories;
QMap<int, QgsMapStylePanelFactory*> mUserPages;
QgsMapLayerStyleManagerWidgetFactory* mStyleManagerFactory;
};

#endif // QGSMAPSTYLESDOCK_H
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -242,6 +242,7 @@ SET(QGIS_GUI_SRCS
qgsmaplayermodel.cpp
qgsmaplayerpropertiesfactory.cpp
qgsmaplayerproxymodel.cpp
qgsmaplayerstylemanagerwidget.cpp
qgsmapmouseevent.cpp
qgsmapoverviewcanvas.cpp
qgsmaptip.cpp
Expand Down Expand Up @@ -392,6 +393,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsmaplayercombobox.h
qgsmaplayermodel.h
qgsmaplayerproxymodel.h
qgsmaplayerstylemanagerwidget.h
qgsmapoverviewcanvas.h
qgsmaptool.h
qgsmaptooladvanceddigitizing.h
Expand Down
145 changes: 145 additions & 0 deletions src/gui/qgsmaplayerstylemanagerwidget.cpp
@@ -0,0 +1,145 @@
#include <QAction>
#include <QVBoxLayout>
#include <QToolBar>
#include <QInputDialog>

#include "qgsmaplayerstylemanagerwidget.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsmapcanvas.h"
#include "qgsmapstylepanel.h"
#include "qgsmaplayerstylemanager.h"


QgsMapLayerStyleManagerWidget::QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas *canvas, QWidget *parent )
: QgsMapStylePanel( layer, canvas, parent )
{
mModel = new QStandardItemModel( this );
mStyleList = new QListView( this );
mStyleList->setModel( mModel );
mStyleList->setViewMode( QListView::ListMode );
mStyleList->setResizeMode( QListView::Adjust );

QToolBar* toolbar = new QToolBar( this );
QAction* addAction = toolbar->addAction( tr( "Add" ) );
connect( addAction, SIGNAL( triggered() ), this, SLOT( addStyle() ) );
QAction* removeAction = toolbar->addAction( tr( "Remove Current" ) );
connect( removeAction, SIGNAL( triggered() ), this, SLOT( removeStyle() ) );

connect( canvas, SIGNAL( mapCanvasRefreshed() ), this, SLOT( updateCurrent() ) );

connect( mStyleList, SIGNAL( clicked( QModelIndex ) ), this, SLOT( styleClicked( QModelIndex ) ) );

setLayout( new QVBoxLayout() );
layout()->setContentsMargins( 0, 0, 0, 0 );
layout()->addWidget( toolbar );
layout()->addWidget( mStyleList );

connect( mLayer->styleManager(), SIGNAL( currentStyleChanged( QString ) ), this, SLOT( currentStyleChanged( QString ) ) );
connect( mLayer->styleManager(), SIGNAL( styleAdded( QString ) ), this, SLOT( styleAdded( QString ) ) );
connect( mLayer->styleManager(), SIGNAL( styleremoved( QString ) ), this, SLOT( styleRemoved( QString ) ) );
connect( mLayer->styleManager(), SIGNAL( styleRenamed( QString, QString ) ), this, SLOT( styleRenamed( QString, QString ) ) );

mModel->clear();

Q_FOREACH ( const QString name, mLayer->styleManager()->styles() )
{
QString stylename = name;

if ( stylename.isEmpty() )
stylename = "(default)";

QStandardItem* item = new QStandardItem( stylename );
mModel->appendRow( item );
}

QString active = mLayer->styleManager()->currentStyle();
currentStyleChanged( active );
}

void QgsMapLayerStyleManagerWidget::styleClicked( QModelIndex index )
{
if ( !mLayer || !index.isValid() )
return;

QString name = index.data().toString();
mLayer->styleManager()->setCurrentStyle( name );
}

void QgsMapLayerStyleManagerWidget::currentStyleChanged( QString name )
{
QList<QStandardItem*> items = mModel->findItems( name );
if ( items.isEmpty() )
return;

QStandardItem* item = items.at( 0 );

mStyleList->setCurrentIndex( item->index() );
}

void QgsMapLayerStyleManagerWidget::styleAdded( QString name )
{
QgsDebugMsg( "Style added" );
QStandardItem* item = new QStandardItem( name );
mModel->appendRow( item );
}

void QgsMapLayerStyleManagerWidget::styleRemoved( QString name )
{
QList<QStandardItem*> items = mModel->findItems( name );
if ( items.isEmpty() )
return;

QStandardItem* item = items.at( 0 );
mModel->removeRow( item->row() );
}

void QgsMapLayerStyleManagerWidget::styleRenamed( QString oldname, QString newname )
{
QList<QStandardItem*> items = mModel->findItems( oldname );
if ( items.isEmpty() )
return;

QStandardItem* item = items.at( 0 );
item->setText( newname );
}

void QgsMapLayerStyleManagerWidget::addStyle()
{
bool ok;
QString text = QInputDialog::getText( nullptr, tr( "New style" ),
tr( "Style name:" ), QLineEdit::Normal,
"new style", &ok );
if ( !ok || text.isEmpty() )
return;

bool res = mLayer->styleManager()->addStyleFromLayer( text );
if ( res ) // make it active!
{
mLayer->styleManager()->setCurrentStyle( text );
}
else
{
QgsDebugMsg( "Failed to add style: " + text );
}
}

void QgsMapLayerStyleManagerWidget::removeStyle()
{
QString current = mLayer->styleManager()->currentStyle();
QList<QStandardItem*> items = mModel->findItems( current );
if ( items.isEmpty() )
return;

QStandardItem* item = items.at( 0 );
bool res = mLayer->styleManager()->removeStyle( current );
if ( res )
{
mModel->removeRow( item->row() );
}
else
{
QgsDebugMsg( "Failed to remove current style" );
}

}
48 changes: 48 additions & 0 deletions src/gui/qgsmaplayerstylemanagerwidget.h
@@ -0,0 +1,48 @@
#ifndef QGSMAPLAYERSTYLEMANAGERWIDGET_H
#define QGSMAPLAYERSTYLEMANAGERWIDGET_H

#include <QWidget>
#include <QListView>
#include <QStandardItemModel>

#include "qgsmapstylepanel.h"

class QgsMapLayer;
class QgsMapCanvas;


/**
* @brief The QgsMapLayerStyleManagerWidget class which is used to visually manage
* the layer styles.
*/
class GUI_EXPORT QgsMapLayerStyleManagerWidget : public QgsMapStylePanel
{
Q_OBJECT
public:

/**
* @brief Style manager widget to mange the layers styles.
* @param layer The layer for the widget
* @param canvas The canvas object.
* @param parent The parent.
*/
QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas* canvas, QWidget *parent = 0 );

public slots:
void apply() override {}

private slots:
void styleClicked( QModelIndex index );
void currentStyleChanged( QString name );
void styleAdded( QString name );
void styleRemoved( QString name );
void styleRenamed( QString oldname, QString newname );
void addStyle();
void removeStyle();

private:
QStandardItemModel* mModel;
QListView* mStyleList;
};

#endif // QGSMAPLAYERSTYLEMANAGERWIDGET_H
5 changes: 3 additions & 2 deletions src/gui/qgsmapstylepanel.cpp
Expand Up @@ -2,9 +2,10 @@

QgsMapStylePanel::QgsMapStylePanel( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
: QWidget( parent )
, mLayer( layer )
, mMapCanvas( canvas )
{
Q_UNUSED( layer );
Q_UNUSED( canvas );

}

QgsMapStylePanelFactory::QgsMapStylePanelFactory()
Expand Down

0 comments on commit 9d780ba

Please sign in to comment.