27 #include <QAbstractListModel>
28 #include <QInputDialog>
36 : QAbstractListModel( parent )
41 int QgsVectorTileBasicRendererListModel::rowCount(
const QModelIndex &parent )
const
43 if ( parent.isValid() )
46 return mRenderer->styles().count();
49 int QgsVectorTileBasicRendererListModel::columnCount(
const QModelIndex & )
const
54 QVariant QgsVectorTileBasicRendererListModel::data(
const QModelIndex &index,
int role )
const
56 if ( index.row() < 0 || index.row() >= mRenderer->styles().count() )
59 const QList<QgsVectorTileBasicRendererStyle> styles = mRenderer->styles();
67 if ( index.column() == 0 )
69 else if ( index.column() == 1 )
70 return style.
layerName().isEmpty() ? tr(
"(all layers)" ) : style.layerName();
71 else if ( index.column() == 2 )
73 else if ( index.column() == 3 )
75 else if ( index.column() == 4 )
76 return style.
filterExpression().isEmpty() ? tr(
"(no filter)" ) : style.filterExpression();
83 if ( index.column() == 0 )
85 else if ( index.column() == 1 )
87 else if ( index.column() == 2 )
89 else if ( index.column() == 3 )
91 else if ( index.column() == 4 )
97 case Qt::DecorationRole:
99 if ( index.column() == 0 && style.
symbol() )
107 case Qt::CheckStateRole:
109 if ( index.column() != 0 )
111 return style.
isEnabled() ? Qt::Checked : Qt::Unchecked;
124 QVariant QgsVectorTileBasicRendererListModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
126 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 5 )
129 lst << tr(
"Label" ) << tr(
"Layer" ) << tr(
"Min. Zoom" ) << tr(
"Max. Zoom" ) << tr(
"Filter" );
136 Qt::ItemFlags QgsVectorTileBasicRendererListModel::flags(
const QModelIndex &index )
const
138 if ( !index.isValid() )
139 return Qt::ItemIsDropEnabled;
141 const Qt::ItemFlag checkable = ( index.column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
143 return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
144 Qt::ItemIsEditable | checkable |
145 Qt::ItemIsDragEnabled;
148 bool QgsVectorTileBasicRendererListModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
150 if ( !index.isValid() )
155 if ( role == Qt::CheckStateRole )
157 style.
setEnabled( value.toInt() == Qt::Checked );
158 mRenderer->setStyle( index.row(), style );
159 emit dataChanged( index, index );
163 if ( role == Qt::EditRole )
165 if ( index.column() == 0 )
167 else if ( index.column() == 1 )
169 else if ( index.column() == 2 )
171 else if ( index.column() == 3 )
173 else if ( index.column() == 4 )
176 mRenderer->setStyle( index.row(), style );
177 emit dataChanged( index, index );
184 bool QgsVectorTileBasicRendererListModel::removeRows(
int row,
int count,
const QModelIndex &parent )
186 QList<QgsVectorTileBasicRendererStyle> styles = mRenderer->styles();
188 if ( row < 0 || row >= styles.count() )
191 beginRemoveRows( parent, row, row + count - 1 );
193 for (
int i = 0; i < count; i++ )
195 if ( row < styles.count() )
197 styles.removeAt( row );
201 mRenderer->setStyles( styles );
209 beginInsertRows( QModelIndex(), row, row );
211 QList<QgsVectorTileBasicRendererStyle> styles = mRenderer->styles();
212 styles.insert( row, style );
213 mRenderer->setStyles( styles );
218 Qt::DropActions QgsVectorTileBasicRendererListModel::supportedDropActions()
const
220 return Qt::MoveAction;
223 QStringList QgsVectorTileBasicRendererListModel::mimeTypes()
const
226 types << QStringLiteral(
"application/vnd.text.list" );
230 QMimeData *QgsVectorTileBasicRendererListModel::mimeData(
const QModelIndexList &indexes )
const
232 QMimeData *mimeData =
new QMimeData();
233 QByteArray encodedData;
235 QDataStream stream( &encodedData, QIODevice::WriteOnly );
237 const auto constIndexes = indexes;
238 for (
const QModelIndex &index : constIndexes )
241 if ( !index.isValid() || index.column() != 0 )
247 QDomElement rootElem = doc.createElement( QStringLiteral(
"vector_tile_basic_renderer_style_mime" ) );
249 doc.appendChild( rootElem );
251 stream << doc.toString( -1 );
254 mimeData->setData( QStringLiteral(
"application/vnd.text.list" ), encodedData );
258 bool QgsVectorTileBasicRendererListModel::dropMimeData(
const QMimeData *data,
259 Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
263 if ( action == Qt::IgnoreAction )
266 if ( !data->hasFormat( QStringLiteral(
"application/vnd.text.list" ) ) )
269 if ( parent.column() > 0 )
272 QByteArray encodedData = data->data( QStringLiteral(
"application/vnd.text.list" ) );
273 QDataStream stream( &encodedData, QIODevice::ReadOnly );
279 row = rowCount( parent );
282 while ( !stream.atEnd() )
288 if ( !doc.setContent( text ) )
290 const QDomElement rootElem = doc.documentElement();
291 if ( rootElem.tagName() != QLatin1String(
"vector_tile_basic_renderer_style_mime" ) )
297 insertStyle( row + rows, style );
309 , mMapCanvas( canvas )
310 , mMessageBar( messageBar )
313 layout()->setContentsMargins( 0, 0, 0, 0 );
315 QMenu *menuAddRule =
new QMenu( btnAddRule );
319 btnAddRule->setMenu( menuAddRule );
321 connect( btnEditRule, &QPushButton::clicked,
this, &QgsVectorTileBasicRendererWidget::editStyle );
322 connect( btnRemoveRule, &QAbstractButton::clicked,
this, &QgsVectorTileBasicRendererWidget::removeStyle );
324 connect( viewStyles, &QAbstractItemView::doubleClicked,
this, &QgsVectorTileBasicRendererWidget::editStyleAtIndex );
331 const double tileScale = mVTLayer ? mVTLayer->tileMatrixSet().calculateTileScaleForMap( mMapCanvas->scale(),
337 mLabelCurrentZoom->setText( tr(
"Current zoom: %1" ).arg( zoom ) );
339 mProxyModel->setCurrentZoom( zoom );
343 const double tileScale = mVTLayer ? mVTLayer->tileMatrixSet().calculateTileScaleForMap( mMapCanvas->scale(),
344 mapSettings.destinationCrs(),
345 mapSettings.visibleExtent(),
346 mapSettings.outputSize(),
347 mapSettings.outputDpi() ) : mMapCanvas->
scale();
348 mLabelCurrentZoom->setText( tr(
"Current zoom: %1" ).arg( mVTLayer ? mVTLayer->tileMatrixSet().scaleToZoomLevel( tileScale ) :
QgsVectorTileUtils::scaleToZoomLevel( tileScale, 0, 99 ) ) );
351 connect( mCheckVisibleOnly, &QCheckBox::toggled,
this, [ = ](
bool filter )
353 mProxyModel->setFilterVisible( filter );
372 mModel =
new QgsVectorTileBasicRendererListModel( mRenderer.get(), viewStyles );
373 mProxyModel =
new QgsVectorTileBasicRendererProxyModel( mModel, viewStyles );
374 viewStyles->setModel( mProxyModel );
379 const double tileScale = mVTLayer ? mVTLayer->tileMatrixSet().calculateTileScaleForMap( mMapCanvas->scale(),
385 mProxyModel->setCurrentZoom( zoom );
393 QgsVectorTileBasicRendererWidget::~QgsVectorTileBasicRendererWidget() =
default;
395 void QgsVectorTileBasicRendererWidget::apply()
397 mVTLayer->setRenderer( mRenderer->clone() );
421 const int rows = mModel->rowCount();
422 mModel->insertStyle( rows, style );
423 viewStyles->selectionModel()->setCurrentIndex( mProxyModel->mapFromSource( mModel->index( rows, 0 ) ), QItemSelectionModel::ClearAndSelect );
426 void QgsVectorTileBasicRendererWidget::editStyle()
428 editStyleAtIndex( viewStyles->selectionModel()->currentIndex() );
431 void QgsVectorTileBasicRendererWidget::editStyleAtIndex(
const QModelIndex &proxyIndex )
433 const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
434 if ( index.row() < 0 || index.row() >= mRenderer->styles().count() )
442 std::unique_ptr< QgsSymbol > symbol( style.
symbol()->
clone() );
451 const double tileScale = mVTLayer ? mVTLayer->tileMatrixSet().calculateTileScaleForMap( mMapCanvas->scale(),
481 if ( !dlg.exec() || !symbol )
487 mRenderer->setStyle( index.row(), style );
488 emit widgetChanged();
492 void QgsVectorTileBasicRendererWidget::updateSymbolsFromWidget()
494 const int index = mProxyModel->mapToSource( viewStyles->selectionModel()->currentIndex() ).row();
503 mRenderer->setStyle( index, style );
504 emit widgetChanged();
507 void QgsVectorTileBasicRendererWidget::cleanUpSymbolSelector(
QgsPanelWidget *container )
516 void QgsVectorTileBasicRendererWidget::removeStyle()
518 const QModelIndexList sel = viewStyles->selectionModel()->selectedIndexes();
521 for (
const QModelIndex &proxyIndex : sel )
523 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
524 if ( !res.contains( sourceIndex.row() ) )
525 res << sourceIndex.row();
527 std::sort( res.begin(), res.end() );
529 for (
int i = res.size() - 1; i >= 0; --i )
531 mModel->removeRow( res[ i ] );
534 viewStyles->selectionModel()->clear();
537 QgsVectorTileBasicRendererProxyModel::QgsVectorTileBasicRendererProxyModel( QgsVectorTileBasicRendererListModel *source, QObject *parent )
538 : QSortFilterProxyModel( parent )
540 setSourceModel( source );
541 setDynamicSortFilter(
true );
544 void QgsVectorTileBasicRendererProxyModel::setCurrentZoom(
int zoom )
550 void QgsVectorTileBasicRendererProxyModel::setFilterVisible(
bool enabled )
552 mFilterVisible = enabled;
556 bool QgsVectorTileBasicRendererProxyModel::filterAcceptsRow(
int source_row,
const QModelIndex &source_parent )
const
558 if ( mCurrentZoom < 0 || !mFilterVisible )
561 const int rowMinZoom = sourceModel()->data( sourceModel()->index( source_row, 0, source_parent ), QgsVectorTileBasicRendererListModel::MinZoom ).toInt();
562 const int rowMaxZoom = sourceModel()->data( sourceModel()->index( source_row, 0, source_parent ), QgsVectorTileBasicRendererListModel::MaxZoom ).toInt();
564 if ( rowMinZoom >= 0 && rowMinZoom > mCurrentZoom )
567 if ( rowMaxZoom >= 0 && rowMaxZoom < mCurrentZoom )
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
Map canvas is a class for displaying all GIS data types on a canvas.
void scaleChanged(double)
Emitted when the scale of the map changes.
The QgsMapSettings class contains configuration for rendering of the map.
double scale() const
Returns the calculated map scale.
QSize outputSize() const
Returns the size of the resulting map image, in pixels.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
A bar for displaying non-blocking messages to the user.
The class is used as a container of context for various read/write operations on other objects.
static QgsStyle * defaultStyle()
Returns default application-wide style.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr)
Returns an icon preview for a color ramp.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setAdditionalExpressionContextScopes(const QList< QgsExpressionContextScope > &scopes)
Sets a list of additional expression context scopes to show as available within the layer.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
Represents a vector layer which manages a vector based data sets.
Definition of map rendering of a subset of vector tile data.
QgsSymbol * symbol() const
Returns symbol for rendering.
void setEnabled(bool enabled)
Sets whether this style is enabled (used for rendering)
void setMinZoomLevel(int minZoom)
Sets minimum zoom level index (negative number means no limit)
void setLayerName(const QString &name)
Sets name of the sub-layer to render (empty layer means that all layers match)
QString filterExpression() const
Returns filter expression (empty filter means that all features match)
QString styleName() const
Returns human readable name of this style.
void setFilterExpression(const QString &expr)
Sets filter expression (empty filter means that all features match)
void setSymbol(QgsSymbol *sym)
Sets symbol for rendering. Takes ownership of the symbol.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const
Writes object content to given DOM element.
void setStyleName(const QString &name)
Sets human readable name of this style.
bool isEnabled() const
Returns whether this style is enabled (used for rendering)
void setMaxZoomLevel(int maxZoom)
Sets maximum zoom level index (negative number means no limit)
int minZoomLevel() const
Returns minimum zoom level index (negative number means no limit)
int maxZoomLevel() const
Returns maxnimum zoom level index (negative number means no limit)
QString layerName() const
Returns name of the sub-layer to render (empty layer means that all layers match)
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads object content from given DOM element.
The default vector tile renderer implementation.
Implements a map layer that is dedicated to rendering of vector tiles.
QgsVectorTileRenderer * renderer() const
Returns currently assigned renderer.
virtual QgsVectorTileRenderer * clone() const =0
Returns a clone of the renderer.
virtual QString type() const =0
Returns unique type name of the renderer implementation.
static double scaleToZoom(double mapScale, double z0Scale=559082264.0287178)
Finds zoom level given map scale denominator.
static int scaleToZoomLevel(double mapScale, int sourceMinZoom, int sourceMaxZoom, double z0Scale=559082264.0287178)
Finds the best fitting zoom level given a map scale denominator and allowed zoom level range.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...