25 #include <QAbstractListModel>
26 #include <QInputDialog>
34 : QAbstractListModel( parent )
39 int QgsVectorTileBasicRendererListModel::rowCount(
const QModelIndex &parent )
const
41 if ( parent.isValid() )
44 return mRenderer->styles().count();
47 int QgsVectorTileBasicRendererListModel::columnCount(
const QModelIndex & )
const
52 QVariant QgsVectorTileBasicRendererListModel::data(
const QModelIndex &index,
int role )
const
54 if ( index.row() < 0 || index.row() >= mRenderer->styles().count() )
57 const QList<QgsVectorTileBasicRendererStyle> styles = mRenderer->styles();
64 if ( index.column() == 0 )
66 else if ( index.column() == 1 )
67 return style.
layerName().isEmpty() ? tr(
"(all layers)" ) : style.layerName();
68 else if ( index.column() == 2 )
70 else if ( index.column() == 3 )
72 else if ( index.column() == 4 )
73 return style.
filterExpression().isEmpty() ? tr(
"(no filter)" ) : style.filterExpression();
80 if ( index.column() == 0 )
82 else if ( index.column() == 1 )
84 else if ( index.column() == 2 )
86 else if ( index.column() == 3 )
88 else if ( index.column() == 4 )
94 case Qt::DecorationRole:
96 if ( index.column() == 0 && style.
symbol() )
104 case Qt::CheckStateRole:
106 if ( index.column() != 0 )
108 return style.
isEnabled() ? Qt::Checked : Qt::Unchecked;
115 QVariant QgsVectorTileBasicRendererListModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
117 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 5 )
120 lst << tr(
"Label" ) << tr(
"Layer" ) << tr(
"Min. Zoom" ) << tr(
"Max. Zoom" ) << tr(
"Filter" );
127 Qt::ItemFlags QgsVectorTileBasicRendererListModel::flags(
const QModelIndex &index )
const
129 if ( !index.isValid() )
130 return Qt::ItemIsDropEnabled;
132 Qt::ItemFlag checkable = ( index.column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
134 return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
135 Qt::ItemIsEditable | checkable |
136 Qt::ItemIsDragEnabled;
139 bool QgsVectorTileBasicRendererListModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
141 if ( !index.isValid() )
146 if ( role == Qt::CheckStateRole )
148 style.
setEnabled( value.toInt() == Qt::Checked );
149 mRenderer->setStyle( index.row(), style );
150 emit dataChanged( index, index );
154 if ( role == Qt::EditRole )
156 if ( index.column() == 0 )
158 else if ( index.column() == 1 )
160 else if ( index.column() == 2 )
162 else if ( index.column() == 3 )
164 else if ( index.column() == 4 )
167 mRenderer->setStyle( index.row(), style );
168 emit dataChanged( index, index );
175 bool QgsVectorTileBasicRendererListModel::removeRows(
int row,
int count,
const QModelIndex &parent )
177 QList<QgsVectorTileBasicRendererStyle> styles = mRenderer->styles();
179 if ( row < 0 || row >= styles.count() )
182 beginRemoveRows( parent, row, row + count - 1 );
184 for (
int i = 0; i < count; i++ )
186 if ( row < styles.count() )
188 styles.removeAt( row );
192 mRenderer->setStyles( styles );
200 beginInsertRows( QModelIndex(), row, row );
202 QList<QgsVectorTileBasicRendererStyle> styles = mRenderer->styles();
203 styles.insert( row, style );
204 mRenderer->setStyles( styles );
209 Qt::DropActions QgsVectorTileBasicRendererListModel::supportedDropActions()
const
211 return Qt::MoveAction;
214 QStringList QgsVectorTileBasicRendererListModel::mimeTypes()
const
217 types << QStringLiteral(
"application/vnd.text.list" );
221 QMimeData *QgsVectorTileBasicRendererListModel::mimeData(
const QModelIndexList &indexes )
const
223 QMimeData *mimeData =
new QMimeData();
224 QByteArray encodedData;
226 QDataStream stream( &encodedData, QIODevice::WriteOnly );
228 const auto constIndexes = indexes;
229 for (
const QModelIndex &index : constIndexes )
232 if ( !index.isValid() || index.column() != 0 )
238 QDomElement rootElem = doc.createElement( QStringLiteral(
"vector_tile_basic_renderer_style_mime" ) );
240 doc.appendChild( rootElem );
242 stream << doc.toString( -1 );
245 mimeData->setData( QStringLiteral(
"application/vnd.text.list" ), encodedData );
249 bool QgsVectorTileBasicRendererListModel::dropMimeData(
const QMimeData *data,
250 Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
254 if ( action == Qt::IgnoreAction )
257 if ( !data->hasFormat( QStringLiteral(
"application/vnd.text.list" ) ) )
260 if ( parent.column() > 0 )
263 QByteArray encodedData = data->data( QStringLiteral(
"application/vnd.text.list" ) );
264 QDataStream stream( &encodedData, QIODevice::ReadOnly );
270 row = rowCount( parent );
273 while ( !stream.atEnd() )
279 if ( !doc.setContent( text ) )
281 QDomElement rootElem = doc.documentElement();
282 if ( rootElem.tagName() != QLatin1String(
"vector_tile_basic_renderer_style_mime" ) )
288 insertStyle( row + rows, style );
300 , mMessageBar( messageBar )
303 layout()->setContentsMargins( 0, 0, 0, 0 );
305 QMenu *menuAddRule =
new QMenu( btnAddRule );
309 btnAddRule->setMenu( menuAddRule );
311 connect( btnEditRule, &QPushButton::clicked,
this, &QgsVectorTileBasicRendererWidget::editStyle );
312 connect( btnRemoveRule, &QAbstractButton::clicked,
this, &QgsVectorTileBasicRendererWidget::removeStyle );
314 connect( viewStyles, &QAbstractItemView::doubleClicked,
this, &QgsVectorTileBasicRendererWidget::editStyleAtIndex );
332 mModel =
new QgsVectorTileBasicRendererListModel( mRenderer.get(), viewStyles );
333 viewStyles->setModel( mModel );
340 QgsVectorTileBasicRendererWidget::~QgsVectorTileBasicRendererWidget() =
default;
342 void QgsVectorTileBasicRendererWidget::apply()
344 mVTLayer->setRenderer( mRenderer->clone() );
352 int rows = mModel->rowCount();
353 mModel->insertStyle( rows, style );
354 viewStyles->selectionModel()->setCurrentIndex( mModel->index( rows, 0 ), QItemSelectionModel::ClearAndSelect );
357 void QgsVectorTileBasicRendererWidget::editStyle()
359 editStyleAtIndex( viewStyles->selectionModel()->currentIndex() );
362 void QgsVectorTileBasicRendererWidget::editStyleAtIndex(
const QModelIndex &index )
369 std::unique_ptr< QgsSymbol > symbol( style.
symbol()->
clone() );
391 if ( !dlg.exec() || !symbol )
397 mRenderer->setStyle( index.row(), style );
398 emit widgetChanged();
402 void QgsVectorTileBasicRendererWidget::updateSymbolsFromWidget()
404 int index = viewStyles->selectionModel()->currentIndex().row();
410 mRenderer->setStyle( index, style );
411 emit widgetChanged();
414 void QgsVectorTileBasicRendererWidget::cleanUpSymbolSelector(
QgsPanelWidget *container )
423 void QgsVectorTileBasicRendererWidget::removeStyle()
425 QItemSelection sel = viewStyles->selectionModel()->selection();
426 const auto constSel = sel;
427 for (
const QItemSelectionRange &range : constSel )
429 if ( range.isValid() )
430 mModel->removeRows( range.top(), range.bottom() - range.top() + 1, range.parent() );
433 viewStyles->selectionModel()->clear();