Skip to content
Permalink
Browse files

[styledock] Add saved style manager

  • Loading branch information
NathanW2 committed Jun 3, 2016
1 parent e079860 commit 9d780bac1b4b6f6208f1fcd5115baa85b8f4c25c
@@ -21,6 +21,7 @@

%Include qgsblendmodecombobox.sip


%Include qgisinterface.sip
%Include qgsactionmenu.sip
%Include qgsadvanceddigitizingcanvasitem.sip
@@ -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
@@ -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();
};
@@ -47,6 +47,8 @@ class QgsMapStylePanelFactory
#include <qgsmapstylepanel.h>
%End
public:
typedef QFlags<QgsMapLayer::LayerType> LayerTypesFlags;

/** Constructor */
QgsMapStylePanelFactory();

@@ -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.
@@ -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 )
@@ -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() ) );

@@ -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() )
@@ -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 );
@@ -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;
}
@@ -13,6 +13,7 @@
#include <QTimer>

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

class QgsLabelingWidget;
class QgsMapLayer;
@@ -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
{
@@ -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 );
@@ -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
@@ -242,6 +242,7 @@ SET(QGIS_GUI_SRCS
qgsmaplayermodel.cpp
qgsmaplayerpropertiesfactory.cpp
qgsmaplayerproxymodel.cpp
qgsmaplayerstylemanagerwidget.cpp
qgsmapmouseevent.cpp
qgsmapoverviewcanvas.cpp
qgsmaptip.cpp
@@ -392,6 +393,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsmaplayercombobox.h
qgsmaplayermodel.h
qgsmaplayerproxymodel.h
qgsmaplayerstylemanagerwidget.h
qgsmapoverviewcanvas.h
qgsmaptool.h
qgsmaptooladvanceddigitizing.h
@@ -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" );
}

}
@@ -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
@@ -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()

0 comments on commit 9d780ba

Please sign in to comment.
You can’t perform that action at this time.