Skip to content

Commit 9d780ba

Browse files
committedJun 3, 2016
[styledock] Add saved style manager
1 parent e079860 commit 9d780ba

10 files changed

+284
-6
lines changed
 

‎python/gui/gui.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
%Include qgsblendmodecombobox.sip
2323

24+
2425
%Include qgisinterface.sip
2526
%Include qgsactionmenu.sip
2627
%Include qgsadvanceddigitizingcanvasitem.sip
@@ -119,6 +120,7 @@
119120
%Include qgsmaptooltouch.sip
120121
%Include qgsmaptoolzoom.sip
121122
%Include qgsmapstylepanel.sip
123+
%Include qgsmaplayerstylemanagerwidget.sip
122124
%Include qgsmessagebar.sip
123125
%Include qgsmessagebaritem.sip
124126
%Include qgsmessagelogviewer.sip
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @brief The QgsMapLayerStyleManagerWidget class which is used to visually manage
3+
* the layer styles.
4+
*/
5+
class QgsMapLayerStyleManagerWidget : QgsMapStylePanel
6+
{
7+
%TypeHeaderCode
8+
#include "qgsmaplayerstylemanagerwidget.h"
9+
%End
10+
public:
11+
12+
/**
13+
* @brief Style manager widget to mange the layers styles.
14+
* @param layer The layer for the widget
15+
* @param canvas The canvas object.
16+
* @param parent The parent.
17+
*/
18+
QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas* canvas, QWidget *parent = 0 );
19+
20+
public slots:
21+
void apply();
22+
};

‎python/gui/qgsmapstylepanel.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class QgsMapStylePanelFactory
4747
#include <qgsmapstylepanel.h>
4848
%End
4949
public:
50+
typedef QFlags<QgsMapLayer::LayerType> LayerTypesFlags;
51+
5052
/** Constructor */
5153
QgsMapStylePanelFactory();
5254

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

7577
/**
7678
* @brief Factory fucntion to create the widget on demand as needed by the dock.

‎src/app/qgsmapstylingwidget.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsmaplayerregistry.h"
2626
#include "qgsrasterlayer.h"
2727
#include "qgsmapstylepanel.h"
28+
#include "qgsmaplayerstylemanagerwidget.h"
2829

2930
QgsMapStylingWidget::QgsMapStylingWidget( QgsMapCanvas* canvas, QList<QgsMapStylePanelFactory*> pages, QWidget *parent )
3031
: QWidget( parent )
@@ -48,6 +49,9 @@ QgsMapStylingWidget::QgsMapStylingWidget( QgsMapCanvas* canvas, QList<QgsMapStyl
4849
mUndoWidget = new QgsUndoWidget( this, mMapCanvas );
4950
mUndoWidget->setObjectName( "Undo Styles" );
5051
mUndoWidget->hide();
52+
53+
mStyleManagerFactory = new QgsMapLayerStyleManagerWidgetFactory();
54+
5155
connect( mUndoButton, SIGNAL( pressed() ), mUndoWidget, SLOT( undo() ) );
5256
connect( mRedoButton, SIGNAL( pressed() ), mUndoWidget, SLOT( redo() ) );
5357

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

68+
QgsMapStylingWidget::~QgsMapStylingWidget()
69+
{
70+
delete mStyleManagerFactory;
71+
}
72+
73+
void QgsMapStylingWidget::setPageFactories( QList<QgsMapStylePanelFactory *> factories )
74+
{
75+
mPageFactories = factories;
76+
// Always append the style manager factory at the bottom of the list
77+
mPageFactories.append( mStyleManagerFactory );
78+
}
79+
6480
void QgsMapStylingWidget::setLayer( QgsMapLayer *layer )
6581
{
6682
if ( !layer || !layer->isSpatial() )
@@ -95,7 +111,7 @@ void QgsMapStylingWidget::setLayer( QgsMapLayer *layer )
95111

96112
Q_FOREACH ( QgsMapStylePanelFactory* factory, mPageFactories )
97113
{
98-
if ( factory->layerType() == layer->type() )
114+
if ( factory->layerType().testFlag( layer->type() ) )
99115
{
100116
QListWidgetItem* item = new QListWidgetItem( factory->icon(), "" );
101117
mOptionsListWidget->addItem( item );
@@ -363,3 +379,24 @@ void QgsMapLayerStyleCommand::redo()
363379
mLayer->readStyle( mXml, error );
364380
mLayer->triggerRepaint();
365381
}
382+
383+
QIcon QgsMapLayerStyleManagerWidgetFactory::icon()
384+
{
385+
return QgsApplication::getThemeIcon( "propertyicons/symbology.png" );
386+
}
387+
388+
QString QgsMapLayerStyleManagerWidgetFactory::title()
389+
{
390+
return QString();
391+
}
392+
393+
QgsMapStylePanel *QgsMapLayerStyleManagerWidgetFactory::createPanel( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
394+
{
395+
return new QgsMapLayerStyleManagerWidget( layer, canvas, parent );
396+
397+
}
398+
399+
QgsMapStylePanelFactory::LayerTypesFlags QgsMapLayerStyleManagerWidgetFactory::layerType()
400+
{
401+
return QgsMapLayer::VectorLayer;
402+
}

‎src/app/qgsmapstylingwidget.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <QTimer>
1414

1515
#include "ui_qgsmapstylingwidgetbase.h"
16+
#include "qgsmapstylepanel.h"
1617

1718
class QgsLabelingWidget;
1819
class QgsMapLayer;
@@ -22,6 +23,16 @@ class QgsRendererRasterPropertiesWidget;
2223
class QgsUndoWidget;
2324
class QgsRasterHistogramWidget;
2425
class QgsMapStylePanelFactory;
26+
class QgsMapLayerStyleManagerWidget;
27+
28+
class APP_EXPORT QgsMapLayerStyleManagerWidgetFactory : public QgsMapStylePanelFactory
29+
{
30+
public:
31+
QIcon icon() override;
32+
QString title() override;
33+
QgsMapStylePanel *createPanel( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent ) override;
34+
LayerTypesFlags layerType() override;
35+
};
2536

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

47-
void setPageFactories( QList<QgsMapStylePanelFactory*> factories ) { mPageFactories = factories; }
59+
void setPageFactories( QList<QgsMapStylePanelFactory*> factories );
4860

4961
signals:
5062
void styleChanged( QgsMapLayer* layer );
@@ -73,6 +85,7 @@ class APP_EXPORT QgsMapStylingWidget : public QWidget, private Ui::QgsMapStyling
7385
QgsRendererRasterPropertiesWidget* mRasterStyleWidget;
7486
QList<QgsMapStylePanelFactory*> mPageFactories;
7587
QMap<int, QgsMapStylePanelFactory*> mUserPages;
88+
QgsMapLayerStyleManagerWidgetFactory* mStyleManagerFactory;
7689
};
7790

7891
#endif // QGSMAPSTYLESDOCK_H

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ SET(QGIS_GUI_SRCS
242242
qgsmaplayermodel.cpp
243243
qgsmaplayerpropertiesfactory.cpp
244244
qgsmaplayerproxymodel.cpp
245+
qgsmaplayerstylemanagerwidget.cpp
245246
qgsmapmouseevent.cpp
246247
qgsmapoverviewcanvas.cpp
247248
qgsmaptip.cpp
@@ -392,6 +393,7 @@ SET(QGIS_GUI_MOC_HDRS
392393
qgsmaplayercombobox.h
393394
qgsmaplayermodel.h
394395
qgsmaplayerproxymodel.h
396+
qgsmaplayerstylemanagerwidget.h
395397
qgsmapoverviewcanvas.h
396398
qgsmaptool.h
397399
qgsmaptooladvanceddigitizing.h
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include <QAction>
2+
#include <QVBoxLayout>
3+
#include <QToolBar>
4+
#include <QInputDialog>
5+
6+
#include "qgsmaplayerstylemanagerwidget.h"
7+
#include "qgslogger.h"
8+
#include "qgsmaplayer.h"
9+
#include "qgsmapcanvas.h"
10+
#include "qgsmapstylepanel.h"
11+
#include "qgsmaplayerstylemanager.h"
12+
13+
14+
QgsMapLayerStyleManagerWidget::QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas *canvas, QWidget *parent )
15+
: QgsMapStylePanel( layer, canvas, parent )
16+
{
17+
mModel = new QStandardItemModel( this );
18+
mStyleList = new QListView( this );
19+
mStyleList->setModel( mModel );
20+
mStyleList->setViewMode( QListView::ListMode );
21+
mStyleList->setResizeMode( QListView::Adjust );
22+
23+
QToolBar* toolbar = new QToolBar( this );
24+
QAction* addAction = toolbar->addAction( tr( "Add" ) );
25+
connect( addAction, SIGNAL( triggered() ), this, SLOT( addStyle() ) );
26+
QAction* removeAction = toolbar->addAction( tr( "Remove Current" ) );
27+
connect( removeAction, SIGNAL( triggered() ), this, SLOT( removeStyle() ) );
28+
29+
connect( canvas, SIGNAL( mapCanvasRefreshed() ), this, SLOT( updateCurrent() ) );
30+
31+
connect( mStyleList, SIGNAL( clicked( QModelIndex ) ), this, SLOT( styleClicked( QModelIndex ) ) );
32+
33+
setLayout( new QVBoxLayout() );
34+
layout()->setContentsMargins( 0, 0, 0, 0 );
35+
layout()->addWidget( toolbar );
36+
layout()->addWidget( mStyleList );
37+
38+
connect( mLayer->styleManager(), SIGNAL( currentStyleChanged( QString ) ), this, SLOT( currentStyleChanged( QString ) ) );
39+
connect( mLayer->styleManager(), SIGNAL( styleAdded( QString ) ), this, SLOT( styleAdded( QString ) ) );
40+
connect( mLayer->styleManager(), SIGNAL( styleremoved( QString ) ), this, SLOT( styleRemoved( QString ) ) );
41+
connect( mLayer->styleManager(), SIGNAL( styleRenamed( QString, QString ) ), this, SLOT( styleRenamed( QString, QString ) ) );
42+
43+
mModel->clear();
44+
45+
Q_FOREACH ( const QString name, mLayer->styleManager()->styles() )
46+
{
47+
QString stylename = name;
48+
49+
if ( stylename.isEmpty() )
50+
stylename = "(default)";
51+
52+
QStandardItem* item = new QStandardItem( stylename );
53+
mModel->appendRow( item );
54+
}
55+
56+
QString active = mLayer->styleManager()->currentStyle();
57+
currentStyleChanged( active );
58+
}
59+
60+
void QgsMapLayerStyleManagerWidget::styleClicked( QModelIndex index )
61+
{
62+
if ( !mLayer || !index.isValid() )
63+
return;
64+
65+
QString name = index.data().toString();
66+
mLayer->styleManager()->setCurrentStyle( name );
67+
}
68+
69+
void QgsMapLayerStyleManagerWidget::currentStyleChanged( QString name )
70+
{
71+
QList<QStandardItem*> items = mModel->findItems( name );
72+
if ( items.isEmpty() )
73+
return;
74+
75+
QStandardItem* item = items.at( 0 );
76+
77+
mStyleList->setCurrentIndex( item->index() );
78+
}
79+
80+
void QgsMapLayerStyleManagerWidget::styleAdded( QString name )
81+
{
82+
QgsDebugMsg( "Style added" );
83+
QStandardItem* item = new QStandardItem( name );
84+
mModel->appendRow( item );
85+
}
86+
87+
void QgsMapLayerStyleManagerWidget::styleRemoved( QString name )
88+
{
89+
QList<QStandardItem*> items = mModel->findItems( name );
90+
if ( items.isEmpty() )
91+
return;
92+
93+
QStandardItem* item = items.at( 0 );
94+
mModel->removeRow( item->row() );
95+
}
96+
97+
void QgsMapLayerStyleManagerWidget::styleRenamed( QString oldname, QString newname )
98+
{
99+
QList<QStandardItem*> items = mModel->findItems( oldname );
100+
if ( items.isEmpty() )
101+
return;
102+
103+
QStandardItem* item = items.at( 0 );
104+
item->setText( newname );
105+
}
106+
107+
void QgsMapLayerStyleManagerWidget::addStyle()
108+
{
109+
bool ok;
110+
QString text = QInputDialog::getText( nullptr, tr( "New style" ),
111+
tr( "Style name:" ), QLineEdit::Normal,
112+
"new style", &ok );
113+
if ( !ok || text.isEmpty() )
114+
return;
115+
116+
bool res = mLayer->styleManager()->addStyleFromLayer( text );
117+
if ( res ) // make it active!
118+
{
119+
mLayer->styleManager()->setCurrentStyle( text );
120+
}
121+
else
122+
{
123+
QgsDebugMsg( "Failed to add style: " + text );
124+
}
125+
}
126+
127+
void QgsMapLayerStyleManagerWidget::removeStyle()
128+
{
129+
QString current = mLayer->styleManager()->currentStyle();
130+
QList<QStandardItem*> items = mModel->findItems( current );
131+
if ( items.isEmpty() )
132+
return;
133+
134+
QStandardItem* item = items.at( 0 );
135+
bool res = mLayer->styleManager()->removeStyle( current );
136+
if ( res )
137+
{
138+
mModel->removeRow( item->row() );
139+
}
140+
else
141+
{
142+
QgsDebugMsg( "Failed to remove current style" );
143+
}
144+
145+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef QGSMAPLAYERSTYLEMANAGERWIDGET_H
2+
#define QGSMAPLAYERSTYLEMANAGERWIDGET_H
3+
4+
#include <QWidget>
5+
#include <QListView>
6+
#include <QStandardItemModel>
7+
8+
#include "qgsmapstylepanel.h"
9+
10+
class QgsMapLayer;
11+
class QgsMapCanvas;
12+
13+
14+
/**
15+
* @brief The QgsMapLayerStyleManagerWidget class which is used to visually manage
16+
* the layer styles.
17+
*/
18+
class GUI_EXPORT QgsMapLayerStyleManagerWidget : public QgsMapStylePanel
19+
{
20+
Q_OBJECT
21+
public:
22+
23+
/**
24+
* @brief Style manager widget to mange the layers styles.
25+
* @param layer The layer for the widget
26+
* @param canvas The canvas object.
27+
* @param parent The parent.
28+
*/
29+
QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas* canvas, QWidget *parent = 0 );
30+
31+
public slots:
32+
void apply() override {}
33+
34+
private slots:
35+
void styleClicked( QModelIndex index );
36+
void currentStyleChanged( QString name );
37+
void styleAdded( QString name );
38+
void styleRemoved( QString name );
39+
void styleRenamed( QString oldname, QString newname );
40+
void addStyle();
41+
void removeStyle();
42+
43+
private:
44+
QStandardItemModel* mModel;
45+
QListView* mStyleList;
46+
};
47+
48+
#endif // QGSMAPLAYERSTYLEMANAGERWIDGET_H

‎src/gui/qgsmapstylepanel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
QgsMapStylePanel::QgsMapStylePanel( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
44
: QWidget( parent )
5+
, mLayer( layer )
6+
, mMapCanvas( canvas )
57
{
6-
Q_UNUSED( layer );
7-
Q_UNUSED( canvas );
8+
89
}
910

1011
QgsMapStylePanelFactory::QgsMapStylePanelFactory()

‎src/gui/qgsmapstylepanel.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class GUI_EXPORT QgsMapStylePanel : public QWidget
4343
* Will be called when live update is enabled.
4444
*/
4545
virtual void apply() = 0;
46+
47+
protected:
48+
QgsMapLayer* mLayer;
49+
QgsMapCanvas* mMapCanvas;
4650
};
4751

4852

@@ -53,6 +57,8 @@ class GUI_EXPORT QgsMapStylePanel : public QWidget
5357
class GUI_EXPORT QgsMapStylePanelFactory
5458
{
5559
public:
60+
Q_DECLARE_FLAGS( LayerTypesFlags, QgsMapLayer::LayerType )
61+
5662
/** Constructor */
5763
QgsMapStylePanelFactory();
5864

@@ -76,7 +82,7 @@ class GUI_EXPORT QgsMapStylePanelFactory
7682
* @brief Supported layer type for the widget.
7783
* @return The layer type this widget is supported for.
7884
*/
79-
virtual QgsMapLayer::LayerType layerType() = 0;
85+
virtual LayerTypesFlags layerType() = 0;
8086

8187
/**
8288
* @brief Factory fucntion to create the widget on demand as needed by the dock.

0 commit comments

Comments
 (0)
Please sign in to comment.