32 #include <QProgressDialog> 34 #include <QTreeWidgetItem> 35 #include <QVBoxLayout> 36 #include <QMessageBox> 38 #ifdef ENABLE_MODELTEST 39 #include "modeltest.h" 70 #ifdef ENABLE_MODELTEST 71 new ModelTest(
mModel,
this );
73 viewRules->setModel(
mModel );
101 connect( btnAddRule, SIGNAL( clicked() ),
this, SLOT(
addRule() ) );
117 connect( viewRules->header(), SIGNAL( sectionResized(
int,
int,
int ) ),
this, SLOT(
saveSectionWidth(
int,
int,
int ) ) );
143 QModelIndex currentIndex = viewRules->selectionModel()->currentIndex();
146 viewRules->selectionModel()->setCurrentIndex( newindex, QItemSelectionModel::ClearAndSelect );
154 viewRules->selectionModel()->setCurrentIndex( newindex, QItemSelectionModel::ClearAndSelect );
170 editRule( viewRules->selectionModel()->currentIndex() );
198 viewRules->selectionModel()->clear();
204 Q_UNUSED( previous );
205 btnEditRule->setEnabled( current.
isValid() );
214 #include <QDialogButtonBox> 215 #include <QInputDialog> 216 #include <QClipboard> 220 QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
222 if ( indexlist.isEmpty() )
228 else if ( type == 1 )
237 viewRules->expand( index );
280 if ( !initialRule->
symbol() )
282 QMessageBox::warning(
this,
tr(
"Scale refinement" ),
tr(
"Parent rule %1 must have a symbol for this operation." ).arg( initialRule->
label() ) );
288 tr(
"Scale refinement" ),
289 tr(
"Please enter scale denominators at which will split the rule, separate them by commas (e.g. 1000,5000):" ) );
297 int scale = item.
toInt( &ok );
328 for (
int row = range.
top(); row <= range.
bottom(); row++ )
330 symbolList.
append( children[row]->symbol() );
346 for (
int row = range.
top(); row <= range.
bottom(); row++ )
348 rl.
append( children[row]->clone() );
373 if ( event->
key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier )
379 else if ( event->
key() == Qt::Key_V &&
event->modifiers() == Qt::ControlModifier )
416 QString path =
"/Windows/RuleBasedTree/sectionWidth/";
428 QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
431 if ( indexlist.isEmpty() )
441 QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
443 if ( indexlist.isEmpty() )
446 index = indexlist.first();
450 void QgsRuleBasedRendererV2Widget::refineRuleCategoriesAccepted(
QgsPanelWidget *panel )
456 QModelIndexList indexList = viewRules->selectionModel()->selectedRows();
466 void QgsRuleBasedRendererV2Widget::refineRuleRangesAccepted(
QgsPanelWidget *panel )
471 QModelIndexList indexList = viewRules->selectionModel()->selectedRows();
481 void QgsRuleBasedRendererV2Widget::ruleWidgetPanelAccepted(
QgsPanelWidget *panel )
495 void QgsRuleBasedRendererV2Widget::liveUpdateRuleFromPanel()
497 ruleWidgetPanelAccepted( qobject_cast<QgsPanelWidget*>(
sender() ) );
513 countMap[rule].
count = 0;
514 countMap[rule].duplicateCount = 0;
547 int featuresCounted = 0;
557 countMap[rule].
count++;
558 if ( featureRuleList.
size() > 1 )
560 countMap[rule].duplicateCount++;
564 if ( duplicateRule == rule )
continue;
565 countMap[rule].duplicateCountMap[duplicateRule] += 1;
569 if ( featuresCounted % 50 == 0 )
571 if ( featuresCounted > nFeatures )
598 bool enabled = !viewRules->selectionModel()->selectedIndexes().isEmpty();
599 btnRefineRule->setEnabled( enabled );
600 btnRemoveRule->setEnabled( enabled );
609 , mSymbolSelector( nullptr )
625 groupScale->setChecked(
true );
628 mScaleRangeWidget->setMaximumScale( 1.0 / rule->
scaleMinDenom() );
630 mScaleRangeWidget->setMinimumScale( 1.0 / rule->
scaleMaxDenom() );
632 mScaleRangeWidget->setMapCanvas(
mMapCanvas );
636 groupSymbol->setChecked(
true );
641 groupSymbol->setChecked(
false );
652 groupSymbol->setLayout( l );
661 connect( mScaleRangeWidget, SIGNAL( rangeChanged(
double,
double ) ),
this, SIGNAL(
widgetChanged() ) );
678 buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
709 mPropsWidget->
apply();
733 editFilter->setText( dlg.expressionText() );
739 if ( filter.hasParserError() )
760 if ( !filter.prepare( &context ) )
771 .setExpressionContext( context );
789 mRule->setFilterExpression( editFilter->text() );
790 mRule->setLabel( editLabel->text() );
791 mRule->setDescription( editDescription->text() );
793 mRule->setScaleMinDenom( groupScale->isChecked() ? mScaleRangeWidget->minimumScaleDenom() : 0 );
794 mRule->setScaleMaxDenom( groupScale->isChecked() ? mScaleRangeWidget->maximumScaleDenom() : 0 );
795 mRule->setSymbol( groupSymbol->isChecked() ? mSymbol->clone() : nullptr );
801 mSymbolSelector->setDockMode( dockMode );
834 return Qt::ItemIsDropEnabled;
837 Qt::ItemFlag drop = ( index.
column() == 0 ? Qt::ItemIsDropEnabled : Qt::NoItemFlags );
839 Qt::ItemFlag checkable = ( index.
column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
841 return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
842 Qt::ItemIsEditable | checkable |
843 Qt::ItemIsDragEnabled | drop;
853 if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
858 return rule->
label();
881 if ( role == Qt::DisplayRole )
889 QString tip =
"<p style='margin:0px;'><ul>";
893 tip +=
tr(
"<li><nobr>%1 features also in rule %2</nobr></li>" ).
arg(
mFeatureCountMap[rule].duplicateCountMap[duplicateRule] ).
arg( label );
909 else if ( role == Qt::DecorationRole && index.
column() == 0 && rule->
symbol() )
913 else if ( role == Qt::TextAlignmentRole )
915 return ( index.
column() == 2 || index.
column() == 3 ) ? Qt::AlignRight : Qt::AlignLeft;
917 else if ( role == Qt::FontRole && index.
column() == 1 )
927 else if ( role == Qt::EditRole )
932 return rule->
label();
943 else if ( role == Qt::CheckStateRole )
945 if ( index.
column() != 0 )
947 return rule->
active() ? Qt::Checked : Qt::Unchecked;
955 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 7 )
958 lst <<
tr(
"Label" ) <<
tr(
"Rule" ) <<
tr(
"Min. scale" ) <<
tr(
"Max. scale" ) <<
tr(
"Count" ) <<
tr(
"Duplicate count" );
961 else if ( orientation == Qt::Horizontal && role == Qt::ToolTipRole )
965 return tr(
"Number of features in this rule." );
967 else if ( section == 5 )
969 return tr(
"Number of features in this rule which are also present in other rule(s)." );
978 if ( parent.
column() > 0 )
993 if (
hasIndex( row, column, parent ) )
1026 if ( role == Qt::CheckStateRole )
1033 if ( role != Qt::EditRole )
1036 switch ( index.
column() )
1060 return Qt::MoveAction;
1066 types <<
"application/vnd.text.list";
1075 QDataStream stream( &encodedData, QIODevice::WriteOnly );
1102 mimeData->
setData(
"application/vnd.text.list", encodedData );
1116 while ( !childRuleElem.
isNull() )
1129 if ( action == Qt::IgnoreAction )
1132 if ( !data->
hasFormat(
"application/vnd.text.list" ) )
1135 if ( parent.
column() > 0 )
1138 QByteArray encodedData = data->
data(
"application/vnd.text.list" );
1139 QDataStream stream( &encodedData, QIODevice::ReadOnly );
1148 while ( !stream.
atEnd() )
1157 if ( rootElem.
tagName() !=
"rule_mime" )
1159 if ( rootElem.
attribute(
"type" ) ==
"labeling" )
1162 if ( symbolsElem.
isNull() )
1166 if ( rootElem.
attribute(
"type" ) ==
"labeling" )
1180 return static_cast<QgsRuleBasedRendererV2::Rule*>( index.
internalPointer() );
1191 QgsDebugMsg(
QString(
"Called: row %1 count %2 parent ~~%3~~" ).arg( row ).arg( count ).arg( parentRule->
dump() ) );
1195 for (
int i = 0; i < count; i++ )
1197 if ( row < parentRule->
children().count() )
1205 QgsDebugMsg(
"trying to remove invalid index - this should not happen!" );
1238 for (
int i = 0; i <
rowCount( idx ); i++ )
1253 rule->parent()->removeChild( rule );
bool hasIndex(int row, int column, const QModelIndex &parent) const
bool active() const
Returns if this rule is active.
QgsRendererRulePropsDialog(QgsRuleBasedRendererV2::Rule *rule, QgsVectorLayer *layer, QgsStyleV2 *style, QWidget *parent=nullptr, QgsMapCanvas *mapCanvas=nullptr)
static QDomElement saveSymbols(QgsSymbolV2Map &symbols, const QString &tagName, QDomDocument &doc)
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsRuleBasedRendererV2 * mR
void setLabel(const QString &label)
Wrapper for iterator of features from vector data provider or vector layer.
static QgsSymbolV2Map loadSymbols(QDomElement &element)
QByteArray toByteArray() const
void setContentsMargins(int left, int top, int right, int bottom)
virtual QString filter(const QgsFields &fields=QgsFields()) override
If a renderer does not require all the features this method may be overridden and return an expressio...
QByteArray data(const QString &mimeType) const
~QgsRendererRulePropsDialog()
QModelIndex currentIndex() const
const QMimeData * mimeData(Mode mode) const
QDomNode appendChild(const QDomNode &newChild)
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
void setMaximum(int maximum)
static QgsExpressionContextScope * atlasScope(const QgsAtlasComposition *atlas)
Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
QString attribute(const QString &name, const QString &defValue) const
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setActive(bool state)
Sets if this rule is active.
QString toString(int indent) const
void setScaleMaxDenom(int scaleMaxDenom)
Set the maximum denominator for which this rule shall apply.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
virtual QgsSymbolV2 * clone() const =0
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
void setRendererScale(double scale)
void setIcon(const QIcon &icon)
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
const QObjectList & children() const
QgsRuleBasedRendererV2::Rule * ruleForIndex(const QModelIndex &index) const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsRuleBasedRendererV2Model(QgsRuleBasedRendererV2 *r)
QDomElement nextSiblingElement(const QString &tagName) const
Qt::DropActions supportedDropActions() const override
QDomElement save(QDomDocument &doc, QgsSymbolV2Map &symbolMap) const
QDomElement documentElement() const
void updateRule(const QModelIndex &parent, int row)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool isElse()
Check if this rule is an ELSE rule.
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
void removeChildAt(int i)
delete child rule
QgsFields fields() const
Returns the list of fields of this layer.
QString description() const
A human readable description for this rule.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
The QgsMapSettings class contains configuration for rendering of the map.
int indexOf(const T &value, int from) const
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
QList< Key > keys() const
void setValue(const QString &key, const QVariant &value)
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
void setValue(int progress)
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QString number(int n, int base)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
int count(const T &value) const
void append(const T &value)
static void refineRuleCategories(Rule *initialRule, QgsCategorizedSymbolRendererV2 *r)
take a rule and create a list of new rules based on the categories from categorized symbol renderer ...
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
int toInt(bool *ok) const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
bool hasAttribute(const QString &name) const
virtual void stopRender(QgsRenderContext &context) override
Needs to be called when a render cycle has finished to clean up.
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
void setAttribute(const QString &name, const QString &value)
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int toInt(bool *ok, int base) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static Rule * create(QDomElement &ruleElem, QgsSymbolV2Map &symbolMap)
Create a rule from an XML definition.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void setOverrideCursor(const QCursor &cursor)
QList< int > QgsAttributeList
This class keeps data about a rules for rule-based renderer.
void restoreOverrideCursor()
void * internalPointer() const
Rule * clone() const
clone this rule, return new instance
QModelIndex parent() const
RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra...
QgsFeatureRequest & setFlags(const QgsFeatureRequest::Flags &flags)
Set flags that affect how features will be fetched.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
int scaleMaxDenom() const
Single scope for storing variables and functions for use within a QgsExpressionContext.
QMap< QgsRuleBasedRendererV2::Rule *, QgsRuleBasedRendererV2Count > mFeatureCountMap
void setMimeData(QMimeData *src, Mode mode)
QModelIndex parent() const
void setMargin(int margin)
void removeRule(const QModelIndex &index)
static void refineRuleRanges(Rule *initialRule, QgsGraduatedSymbolRendererV2 *r)
take a rule and create a list of new rules based on the ranges from graduated symbol renderer ...
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, QFlags< Qt::WindowType > flags, QFlags< Qt::InputMethodHint > inputMethodHints)
void setFeatureCounts(const QMap< QgsRuleBasedRendererV2::Rule *, QgsRuleBasedRendererV2Count > &theCountMap)
QModelIndex createIndex(int row, int column, void *ptr) const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void setShortcut(const QKeySequence &shortcut)
QgsExpressionContext & expressionContext()
Gets the expression context.
QString filterExpression() const
A filter that will check if this rule applies.
QMimeData * mimeData(const QModelIndexList &indexes) const override
void setItalic(bool enable)
void beginInsertRows(const QModelIndex &parent, int first, int last)
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
QString & replace(int position, int n, QChar after)
void setForceOrderingEnabled(bool enabled)
QVariant value(const QString &key, const QVariant &defaultValue) const
QStringList mimeTypes() const override
Contains information about the context of a rendering operation.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
void insertRule(const QModelIndex &parent, int before, QgsRuleBasedRendererV2::Rule *newrule)
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
void finishedAddingRules()
static QgsSymbolV2 * defaultSymbol(QGis::GeometryType geomType)
return new default symbol for specified geometry type
Rule * parent()
The parent rule.
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
RuleList rulesForFeature(QgsFeature &feat, QgsRenderContext *context=nullptr)
tell which rules will be used to render the feature
QDomElement firstChildElement(const QString &tagName) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
provide model index for parent's child item
virtual int columnCount(const QModelIndex &=QModelIndex()) const override
bool dependsOnScale() const
virtual void startRender(QgsRenderContext &context, const QgsFields &fields) override
Needs to be called when a new render cycle is started.
RuleList & children()
Return all children rules of this rule.
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
virtual QList< QString > usedAttributes() override
Returns a set of attributes required for this renderer.
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
static QgsRuleBasedRendererV2 * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsRuleBasedRendererV2 from an existing renderer.
void setData(const QString &mimeType, const QByteArray &data)
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
void clearFeatureCounts()
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
const_iterator constEnd() const
void insertChild(int i, Rule *rule)
add child rule, take ownership, sets this as parent
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
QDomElement createElement(const QString &tagName)
bool nextFeature(QgsFeature &f)
const_iterator constBegin() const
Geometry is not required. It may still be returned if e.g. required for a filter condition.
void setScaleMinDenom(int scaleMinDenom)
Set the minimum denominator for which this rule shall apply.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int scaleMinDenom() const
Represents a vector layer which manages a vector based data sets.
Tree model for the rules:
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
int count(const Key &key) const
A generic dialog for building expression strings.
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="") override
return a list of item text / symbol
QString dump(int indent=0) const
Dump for debug purpose.
void willAddRules(const QModelIndex &parent, int count)
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
static void refineRuleScales(Rule *initialRule, QList< int > scales)
take a rule and create a list of new rules with intervals of scales given by the passed scale denomin...