32 : QAbstractListModel( parent )
37 int QgsVectorTileBasicLabelingListModel::rowCount(
const QModelIndex &parent )
const
39 if ( parent.isValid() )
42 return mLabeling->styles().count();
45 int QgsVectorTileBasicLabelingListModel::columnCount(
const QModelIndex & )
const
50 QVariant QgsVectorTileBasicLabelingListModel::data(
const QModelIndex &index,
int role )
const
52 if ( index.row() < 0 || index.row() >= mLabeling->styles().count() )
55 const QList<QgsVectorTileBasicLabelingStyle> styles = mLabeling->styles();
63 if ( index.column() == 0 )
65 else if ( index.column() == 1 )
66 return style.
layerName().isEmpty() ? tr(
"(all layers)" ) : style.layerName();
67 else if ( index.column() == 2 )
69 else if ( index.column() == 3 )
71 else if ( index.column() == 4 )
72 return style.
filterExpression().isEmpty() ? tr(
"(no filter)" ) : style.filterExpression();
79 if ( index.column() == 0 )
81 else if ( index.column() == 1 )
83 else if ( index.column() == 2 )
85 else if ( index.column() == 3 )
87 else if ( index.column() == 4 )
93 case Qt::CheckStateRole:
95 if ( index.column() != 0 )
97 return style.
isEnabled() ? Qt::Checked : Qt::Unchecked;
100 case Qt::DecorationRole:
102 if ( index.column() == 0 )
120 QVariant QgsVectorTileBasicLabelingListModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
122 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 5 )
125 lst << tr(
"Label" ) << tr(
"Layer" ) << tr(
"Min. Zoom" ) << tr(
"Max. Zoom" ) << tr(
"Filter" );
132 Qt::ItemFlags QgsVectorTileBasicLabelingListModel::flags(
const QModelIndex &index )
const
134 if ( !index.isValid() )
135 return Qt::ItemIsDropEnabled;
137 const Qt::ItemFlag checkable = ( index.column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
139 return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
140 Qt::ItemIsEditable | checkable |
141 Qt::ItemIsDragEnabled;
144 bool QgsVectorTileBasicLabelingListModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
146 if ( !index.isValid() )
151 if ( role == Qt::CheckStateRole )
153 style.
setEnabled( value.toInt() == Qt::Checked );
154 mLabeling->setStyle( index.row(), style );
155 emit dataChanged( index, index );
159 if ( role == Qt::EditRole )
161 if ( index.column() == 0 )
163 else if ( index.column() == 1 )
165 else if ( index.column() == 2 )
167 else if ( index.column() == 3 )
169 else if ( index.column() == 4 )
172 mLabeling->setStyle( index.row(), style );
173 emit dataChanged( index, index );
180 bool QgsVectorTileBasicLabelingListModel::removeRows(
int row,
int count,
const QModelIndex &parent )
182 QList<QgsVectorTileBasicLabelingStyle> styles = mLabeling->styles();
184 if ( row < 0 || row >= styles.count() )
187 beginRemoveRows( parent, row, row + count - 1 );
189 for (
int i = 0; i < count; i++ )
191 if ( row < styles.count() )
193 styles.removeAt( row );
197 mLabeling->setStyles( styles );
205 beginInsertRows( QModelIndex(), row, row );
207 QList<QgsVectorTileBasicLabelingStyle> styles = mLabeling->styles();
208 styles.insert( row, style );
209 mLabeling->setStyles( styles );
214 Qt::DropActions QgsVectorTileBasicLabelingListModel::supportedDropActions()
const
216 return Qt::MoveAction;
219 QStringList QgsVectorTileBasicLabelingListModel::mimeTypes()
const
222 types << QStringLiteral(
"application/vnd.text.list" );
226 QMimeData *QgsVectorTileBasicLabelingListModel::mimeData(
const QModelIndexList &indexes )
const
228 QMimeData *mimeData =
new QMimeData();
229 QByteArray encodedData;
231 QDataStream stream( &encodedData, QIODevice::WriteOnly );
233 const auto constIndexes = indexes;
234 for (
const QModelIndex &index : constIndexes )
237 if ( !index.isValid() || index.column() != 0 )
243 QDomElement rootElem = doc.createElement( QStringLiteral(
"vector_tile_basic_labeling_style_mime" ) );
245 doc.appendChild( rootElem );
247 stream << doc.toString( -1 );
250 mimeData->setData( QStringLiteral(
"application/vnd.text.list" ), encodedData );
254 bool QgsVectorTileBasicLabelingListModel::dropMimeData(
const QMimeData *data,
255 Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
259 if ( action == Qt::IgnoreAction )
262 if ( !data->hasFormat( QStringLiteral(
"application/vnd.text.list" ) ) )
265 if ( parent.column() > 0 )
268 QByteArray encodedData = data->data( QStringLiteral(
"application/vnd.text.list" ) );
269 QDataStream stream( &encodedData, QIODevice::ReadOnly );
275 row = rowCount( parent );
278 while ( !stream.atEnd() )
284 if ( !doc.setContent( text ) )
286 const QDomElement rootElem = doc.documentElement();
287 if ( rootElem.tagName() != QLatin1String(
"vector_tile_basic_labeling_style_mime" ) )
293 insertStyle( row + rows, style );
305 , mMapCanvas( canvas )
306 , mMessageBar( messageBar )
310 layout()->setContentsMargins( 0, 0, 0, 0 );
312 QMenu *menuAddRule =
new QMenu( btnAddRule );
316 btnAddRule->setMenu( menuAddRule );
319 connect( btnEditRule, &QPushButton::clicked,
this, &QgsVectorTileBasicLabelingWidget::editStyle );
320 connect( btnRemoveRule, &QAbstractButton::clicked,
this, &QgsVectorTileBasicLabelingWidget::removeStyle );
322 connect( viewStyles, &QAbstractItemView::doubleClicked,
this, &QgsVectorTileBasicLabelingWidget::editStyleAtIndex );
329 mLabelCurrentZoom->setText( tr(
"Current zoom: %1" ).arg( zoom ) );
331 mProxyModel->setCurrentZoom( zoom );
336 connect( mCheckVisibleOnly, &QCheckBox::toggled,
this, [ = ](
bool filter )
338 mProxyModel->setFilterVisible( filter );
357 mModel =
new QgsVectorTileBasicLabelingListModel( mLabeling.get(), viewStyles );
358 mProxyModel =
new QgsVectorTileBasicLabelingProxyModel( mModel, viewStyles );
359 viewStyles->setModel( mProxyModel );
364 mProxyModel->setCurrentZoom( zoom );
372 QgsVectorTileBasicLabelingWidget::~QgsVectorTileBasicLabelingWidget() =
default;
374 void QgsVectorTileBasicLabelingWidget::apply()
376 mVTLayer->setLabeling( mLabeling->clone() );
399 const int rows = mModel->rowCount();
400 mModel->insertStyle( rows, style );
401 viewStyles->selectionModel()->setCurrentIndex( mProxyModel->mapFromSource( mModel->index( rows, 0 ) ), QItemSelectionModel::ClearAndSelect );
404 void QgsVectorTileBasicLabelingWidget::editStyle()
406 editStyleAtIndex( viewStyles->selectionModel()->currentIndex() );
409 void QgsVectorTileBasicLabelingWidget::editStyleAtIndex(
const QModelIndex &proxyIndex )
411 const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
412 if ( index.row() < 0 || index.row() >= mLabeling->styles().count() )
441 QgsLabelingPanelWidget *widget =
new QgsLabelingPanelWidget( labelSettings, vectorLayer, mMapCanvas, panel );
442 widget->setContext( context );
443 widget->setPanelTitle( style.
styleName() );
449 QgsLabelSettingsDialog dlg( labelSettings, vectorLayer, mMapCanvas,
this, labelSettings.
layerType );
454 mLabeling->setStyle( index.row(), style );
455 emit widgetChanged();
460 void QgsVectorTileBasicLabelingWidget::updateLabelingFromWidget()
462 const int index = mProxyModel->mapToSource( viewStyles->selectionModel()->currentIndex() ).row();
468 QgsLabelingPanelWidget *widget = qobject_cast<QgsLabelingPanelWidget *>( sender() );
471 mLabeling->setStyle( index, style );
472 emit widgetChanged();
475 void QgsVectorTileBasicLabelingWidget::removeStyle()
477 const QModelIndexList sel = viewStyles->selectionModel()->selectedIndexes();
480 for (
const QModelIndex &proxyIndex : sel )
482 const QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
483 if ( !res.contains( sourceIndex.row() ) )
484 res << sourceIndex.row();
486 std::sort( res.begin(), res.end() );
488 for (
int i = res.size() - 1; i >= 0; --i )
490 mModel->removeRow( res[ i ] );
493 viewStyles->selectionModel()->clear();
503 mLabelingGui =
new QgsLabelingGui( vectorLayer, mapCanvas, labelSettings,
this, labelSettings.
layerType );
504 mLabelingGui->setLabelMode( QgsLabelingGui::Labels );
506 mLabelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
507 QVBoxLayout *l =
new QVBoxLayout;
508 l->addWidget( mLabelingGui );
514 void QgsLabelingPanelWidget::setDockMode(
bool dockMode )
517 mLabelingGui->setDockMode( dockMode );
522 mLabelingGui->setContext( context );
527 return mLabelingGui->layerSettings();
531 QgsVectorTileBasicLabelingProxyModel::QgsVectorTileBasicLabelingProxyModel( QgsVectorTileBasicLabelingListModel *source, QObject *parent )
532 : QSortFilterProxyModel( parent )
534 setSourceModel( source );
535 setDynamicSortFilter(
true );
538 void QgsVectorTileBasicLabelingProxyModel::setCurrentZoom(
int zoom )
544 void QgsVectorTileBasicLabelingProxyModel::setFilterVisible(
bool enabled )
546 mFilterVisible = enabled;
550 bool QgsVectorTileBasicLabelingProxyModel::filterAcceptsRow(
int source_row,
const QModelIndex &source_parent )
const
552 if ( mCurrentZoom < 0 || !mFilterVisible )
555 const int rowMinZoom = sourceModel()->data( sourceModel()->index( source_row, 0, source_parent ), QgsVectorTileBasicLabelingListModel::MinZoom ).toInt();
556 const int rowMaxZoom = sourceModel()->data( sourceModel()->index( source_row, 0, source_parent ), QgsVectorTileBasicLabelingListModel::MaxZoom ).toInt();
558 if ( rowMinZoom >= 0 && rowMinZoom > mCurrentZoom )
561 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.
A bar for displaying non-blocking messages to the user.
Contains settings for how a map layer will be labeled.
static QPixmap labelSettingsPreviewPixmap(const QgsPalLayerSettings &settings, QSize size, const QString &previewText=QString(), int padding=0)
Returns a pixmap preview for label settings.
QgsWkbTypes::GeometryType layerType
Geometry type of layers associated with these settings.
The class is used as a container of context for various read/write operations on other objects.
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.
void widgetChanged()
Emitted when the text format defined by the widget changes.
Represents a vector layer which manages a vector based data sets.
Configuration of a single style within QgsVectorTileBasicLabeling.
QgsPalLayerSettings labelSettings() const
Returns labeling configuration of this style.
void setGeometryType(QgsWkbTypes::GeometryType geomType)
Sets type of the geometry that will be used (point / line / polygon)
QString layerName() const
Returns name of the sub-layer to render (empty layer means that all layers match)
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)
void setMinZoomLevel(int minZoom)
Sets minimum zoom level index (negative number means no limit)
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const
Writes object content to given DOM element.
QgsWkbTypes::GeometryType geometryType() const
Returns type of the geometry that will be used (point / line / polygon)
int maxZoomLevel() const
Returns maxnimum zoom level index (negative number means no limit)
void setFilterExpression(const QString &expr)
Sets filter expression (empty filter means that all features match)
int minZoomLevel() const
Returns minimum zoom level index (negative number means no limit)
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads object content from given DOM element.
void setMaxZoomLevel(int maxZoom)
Sets maximum zoom level index (negative number means no limit)
void setStyleName(const QString &name)
Sets human readable name of this style.
void setLabelSettings(const QgsPalLayerSettings &settings)
Sets labeling configuration of this style.
void setEnabled(bool enabled)
Sets whether this style is enabled (used for rendering)
QString styleName() const
Returns human readable name of this style.
bool isEnabled() const
Returns whether this style is enabled (used for rendering)
Basic labeling configuration for vector tile layers.
virtual QString type() const =0
Unique type string of the labeling configuration implementation.
virtual QgsVectorTileLabeling * clone() const =0SIP_FACTORY
Returns a new copy of the object.
Implements a map layer that is dedicated to rendering of vector tiles.
QgsVectorTileLabeling * labeling() const
Returns currently assigned labeling.
static int scaleToZoomLevel(double mapScale, int sourceMinZoom, int sourceMaxZoom)
Finds best fitting zoom level (assuming GoogleCRS84Quad tile matrix set) given map scale denominator ...
static double scaleToZoom(double mapScale)
Finds zoom level (assuming GoogleCRS84Quad tile matrix set) given map scale denominator.
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,...
const double ICON_PADDING_FACTOR