QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsrulebasedrendererwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrulebasedrendererwidget.h - Settings widget for rule-based renderer
3  ---------------------
4  begin : May 2010
5  copyright : (C) 2010 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 #ifndef QGSRULEBASEDRENDERERWIDGET_H
17 #define QGSRULEBASEDRENDERERWIDGET_H
18 
19 #include "qgsrendererwidget.h"
20 #include "qgis_sip.h"
21 
22 #include "qgsrulebasedrenderer.h"
23 class QMenu;
25 
27 
28 #include <QAbstractItemModel>
29 
30 /* Features count for rule */
32 {
33  int count; // number of features
34  int duplicateCount; // number of features present also in other rule(s)
35  // map of feature counts in other rules
36  QHash<QgsRuleBasedRenderer::Rule *, int> duplicateCountMap;
37 };
38 
47 class GUI_EXPORT QgsRuleBasedRendererModel : public QAbstractItemModel
48 {
49  Q_OBJECT
50 
51  public:
52 
56  QgsRuleBasedRendererModel( QgsRuleBasedRenderer *renderer, QObject *parent );
57 
58  Qt::ItemFlags flags( const QModelIndex &index ) const override;
59  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
60  QVariant headerData( int section, Qt::Orientation orientation,
61  int role = Qt::DisplayRole ) const override;
62  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
63  int columnCount( const QModelIndex & = QModelIndex() ) const override;
64  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
65  QModelIndex parent( const QModelIndex &index ) const override;
66 
67  // editing support
68  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
69 
70  // drag'n'drop support
71  Qt::DropActions supportedDropActions() const override;
72  QStringList mimeTypes() const override;
73  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
74  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
75 
76  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
77 
78  // new methods
79 
80  QgsRuleBasedRenderer::Rule *ruleForIndex( const QModelIndex &index ) const;
81 
82  void insertRule( const QModelIndex &parent, int before, QgsRuleBasedRenderer::Rule *newrule );
83  void updateRule( const QModelIndex &parent, int row );
84  // update rule and all its descendants
85  void updateRule( const QModelIndex &index );
86  void removeRule( const QModelIndex &index );
87 
94  void setSymbol( const QModelIndex &index, QgsSymbol *symbol SIP_TRANSFER );
95 
96  void willAddRules( const QModelIndex &parent, int count ); // call beginInsertRows
97  void finishedAddingRules(); // call endInsertRows
98 
100  void setFeatureCounts( const QHash<QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount> &countMap ) SIP_SKIP;
101  void clearFeatureCounts();
102 
103  protected:
104  QgsRuleBasedRenderer *mR = nullptr;
105  QHash<QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount> mFeatureCountMap;
106 };
107 
108 
110 
111 #include "ui_qgsrulebasedrendererwidget.h"
112 
117 class GUI_EXPORT QgsRuleBasedRendererWidget : public QgsRendererWidget, private Ui::QgsRuleBasedRendererWidget
118 {
119  Q_OBJECT
120 
121  public:
122 
123  static QgsRendererWidget *create( QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer ) SIP_FACTORY;
124 
125  QgsRuleBasedRendererWidget( QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer );
126  ~QgsRuleBasedRendererWidget() override;
127 
128  QgsFeatureRenderer *renderer() override;
129  void setDockMode( bool dockMode ) override;
130 
131  public slots:
132 
133  void addRule();
134  void editRule();
135  void editRule( const QModelIndex &index );
136  void removeRule();
137  void countFeatures();
138  void clearFeatureCounts() { mModel->clearFeatureCounts(); }
139 
140  void refineRuleScales();
141  void refineRuleCategories();
142  void refineRuleRanges();
143 
144  void setRenderingOrder();
145 
146  void currentRuleChanged( const QModelIndex &current = QModelIndex(), const QModelIndex &previous = QModelIndex() );
147  void selectedRulesChanged();
148 
149  void saveSectionWidth( int section, int oldSize, int newSize );
150  void restoreSectionWidths();
151 
152  protected:
153  void refineRule( int type );
155  void refineRuleCategoriesGui();
157  void refineRuleRangesGui();
158  void refineRuleScalesGui( const QModelIndexList &index );
159 
160  QgsRuleBasedRenderer::Rule *currentRule();
161 
162  QList<QgsSymbol *> selectedSymbols() override;
163  QgsRuleBasedRenderer::RuleList selectedRules();
164  void refreshSymbolView() override;
165  void keyPressEvent( QKeyEvent *event ) override;
166 
167  QgsRuleBasedRenderer *mRenderer = nullptr;
168  QgsRuleBasedRendererModel *mModel = nullptr;
169 
170  QMenu *mRefineMenu = nullptr;
171  QAction *mDeleteAction = nullptr;
172 
174  QMenu *mContextMenu = nullptr;
175 
176  protected slots:
177  void copy() override;
178  void paste() override;
179  void pasteSymbolToSelection() override;
180 
181  private slots:
182  void refineRuleCategoriesAccepted( QgsPanelWidget *panel );
183  void refineRuleRangesAccepted( QgsPanelWidget *panel );
184  void ruleWidgetPanelAccepted( QgsPanelWidget *panel );
185  void liveUpdateRuleFromPanel();
186  void showContextMenu( QPoint p );
187 };
188 
190 
191 #include <QDialog>
192 
193 #include "ui_qgsrendererrulepropsdialogbase.h"
194 #include "qgis_gui.h"
195 
200 class GUI_EXPORT QgsRendererRulePropsWidget : public QgsPanelWidget, private Ui::QgsRendererRulePropsWidget
201 {
202  Q_OBJECT
203 
204  public:
205 
214  QgsRendererRulePropsWidget( QgsRuleBasedRenderer::Rule *rule,
215  QgsVectorLayer *layer,
216  QgsStyle *style,
217  QWidget *parent SIP_TRANSFERTHIS = nullptr,
218  const QgsSymbolWidgetContext &context = QgsSymbolWidgetContext() );
219 
224  QgsRuleBasedRenderer::Rule *rule() { return mRule; }
225 
226  public slots:
227 
231  void testFilter();
232 
236  void buildExpression();
237 
241  void apply();
242 
247  void setDockMode( bool dockMode ) override;
248 
249  protected:
251  QgsVectorLayer *mLayer = nullptr;
252 
253  QgsSymbolSelectorWidget *mSymbolSelector = nullptr;
254  QgsSymbol *mSymbol = nullptr; // a clone of original symbol
255 
257 };
258 
263 class GUI_EXPORT QgsRendererRulePropsDialog : public QDialog
264 {
265  Q_OBJECT
266 
267  public:
268 
278 
279  QgsRuleBasedRenderer::Rule *rule() { return mPropsWidget->rule(); }
280 
281  public slots:
282  void testFilter();
283  void buildExpression();
284  void accept() override;
285 
286  private slots:
287  void showHelp();
288 
289  private:
290  QgsRendererRulePropsWidget *mPropsWidget = nullptr;
291  QDialogButtonBox *buttonBox = nullptr;
292 };
293 
294 
295 #endif // QGSRULEBASEDRENDERERWIDGET_H
QgsRendererWidget::renderer
virtual QgsFeatureRenderer * renderer()=0
Returns pointer to the renderer (no transfer of ownership)
QgsRendererWidget::refreshSymbolView
virtual void refreshSymbolView()
Definition: qgsrendererwidget.h:125
QgsRuleBasedRenderer
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
Definition: qgsrulebasedrenderer.h:38
QgsRuleBasedRendererCount::duplicateCountMap
QHash< QgsRuleBasedRenderer::Rule *, int > duplicateCountMap
Definition: qgsrulebasedrendererwidget.h:36
QgsPanelWidget::setDockMode
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgspanelwidget.cpp:44
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:36
QgsSymbolSelectorWidget
Symbol selector widget that can be used to select and build a symbol.
Definition: qgssymbolselectordialog.h:88
QgsRuleBasedRenderer::RuleList
QList< QgsRuleBasedRenderer::Rule * > RuleList
Definition: qgsrulebasedrenderer.h:124
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsRendererRulePropsDialog::rule
QgsRuleBasedRenderer::Rule * rule()
Definition: qgsrulebasedrendererwidget.h:279
QgsRuleBasedRendererWidget::mCopyBuffer
QgsRuleBasedRenderer::RuleList mCopyBuffer
Definition: qgsrulebasedrendererwidget.h:173
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsRuleBasedRendererCount
Definition: qgsrulebasedrendererwidget.h:32
QgsRendererRulePropsWidget::rule
QgsRuleBasedRenderer::Rule * rule()
Returns the current set rule.
Definition: qgsrulebasedrendererwidget.h:224
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:30
QgsRendererWidget::copy
virtual void copy()
Definition: qgsrendererwidget.h:150
QgsRuleBasedRendererModel::mFeatureCountMap
QHash< QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount > mFeatureCountMap
Definition: qgsrulebasedrendererwidget.h:105
QgsRendererWidget::pasteSymbolToSelection
virtual void pasteSymbolToSelection()
Pastes the clipboard symbol over selected items.
Definition: qgsrendererwidget.cpp:300
qgis_sip.h
QgsRendererWidget::setDockMode
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgsrendererwidget.cpp:352
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsPanelWidget::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Overridden key press event to handle the esc event on the widget.
Definition: qgspanelwidget.cpp:112
QgsRuleBasedRendererModel
Tree model for the rules:
Definition: qgsrulebasedrendererwidget.h:48
qgsrulebasedrenderer.h
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsRendererWidget::selectedSymbols
virtual QList< QgsSymbol * > selectedSymbols()
Subclasses may provide the capability of changing multiple symbols at once by implementing the follow...
Definition: qgsrendererwidget.h:124
QgsFeatureRenderer
Definition: qgsrenderer.h:103
QgsRendererWidget::paste
virtual void paste()
Definition: qgsrendererwidget.h:151
QgsRuleBasedRendererCount::duplicateCount
int duplicateCount
Definition: qgsrulebasedrendererwidget.h:34
QgsStyle
Definition: qgsstyle.h:160
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
qgsrendererwidget.h
QgsRendererRulePropsWidget::mRule
QgsRuleBasedRenderer::Rule * mRule
Definition: qgsrulebasedrendererwidget.h:250
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsRuleBasedRendererWidget::clearFeatureCounts
void clearFeatureCounts()
Definition: qgsrulebasedrendererwidget.h:138
QgsRuleBasedRenderer::Rule
This class keeps data about a rules for rule-based renderer.
Definition: qgsrulebasedrenderer.h:137
QgsRendererRulePropsDialog
Definition: qgsrulebasedrendererwidget.h:264
QgsRendererRulePropsWidget::mContext
QgsSymbolWidgetContext mContext
Definition: qgsrulebasedrendererwidget.h:256
QgsRuleBasedRendererCount::count
int count
Definition: qgsrulebasedrendererwidget.h:33