29 : QAbstractListModel( parent )
34 int QgsVectorTileBasicLabelingListModel::rowCount(
const QModelIndex &parent )
const
36 if ( parent.isValid() )
39 return mLabeling->styles().count();
42 int QgsVectorTileBasicLabelingListModel::columnCount(
const QModelIndex & )
const
47 QVariant QgsVectorTileBasicLabelingListModel::data(
const QModelIndex &index,
int role )
const
49 if ( index.row() < 0 || index.row() >= mLabeling->styles().count() )
52 const QList<QgsVectorTileBasicLabelingStyle> styles = mLabeling->styles();
59 if ( index.column() == 0 )
61 else if ( index.column() == 1 )
62 return style.
layerName().isEmpty() ? tr(
"(all layers)" ) : style.layerName();
63 else if ( index.column() == 2 )
65 else if ( index.column() == 3 )
67 else if ( index.column() == 4 )
68 return style.
filterExpression().isEmpty() ? tr(
"(no filter)" ) : style.filterExpression();
75 if ( index.column() == 0 )
77 else if ( index.column() == 1 )
79 else if ( index.column() == 2 )
81 else if ( index.column() == 3 )
83 else if ( index.column() == 4 )
89 case Qt::CheckStateRole:
91 if ( index.column() != 0 )
93 return style.
isEnabled() ? Qt::Checked : Qt::Unchecked;
100 QVariant QgsVectorTileBasicLabelingListModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
102 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 5 )
105 lst << tr(
"Label" ) << tr(
"Layer" ) << tr(
"Min. Zoom" ) << tr(
"Max. Zoom" ) << tr(
"Filter" );
112 Qt::ItemFlags QgsVectorTileBasicLabelingListModel::flags(
const QModelIndex &index )
const
114 if ( !index.isValid() )
115 return Qt::ItemIsDropEnabled;
117 Qt::ItemFlag checkable = ( index.column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
119 return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
120 Qt::ItemIsEditable | checkable |
121 Qt::ItemIsDragEnabled;
124 bool QgsVectorTileBasicLabelingListModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
126 if ( !index.isValid() )
131 if ( role == Qt::CheckStateRole )
133 style.
setEnabled( value.toInt() == Qt::Checked );
134 mLabeling->setStyle( index.row(), style );
135 emit dataChanged( index, index );
139 if ( role == Qt::EditRole )
141 if ( index.column() == 0 )
143 else if ( index.column() == 1 )
145 else if ( index.column() == 2 )
147 else if ( index.column() == 3 )
149 else if ( index.column() == 4 )
152 mLabeling->setStyle( index.row(), style );
153 emit dataChanged( index, index );
160 bool QgsVectorTileBasicLabelingListModel::removeRows(
int row,
int count,
const QModelIndex &parent )
162 QList<QgsVectorTileBasicLabelingStyle> styles = mLabeling->styles();
164 if ( row < 0 || row >= styles.count() )
167 beginRemoveRows( parent, row, row + count - 1 );
169 for (
int i = 0; i < count; i++ )
171 if ( row < styles.count() )
173 styles.removeAt( row );
177 mLabeling->setStyles( styles );
185 beginInsertRows( QModelIndex(), row, row );
187 QList<QgsVectorTileBasicLabelingStyle> styles = mLabeling->styles();
188 styles.insert( row, style );
189 mLabeling->setStyles( styles );
194 Qt::DropActions QgsVectorTileBasicLabelingListModel::supportedDropActions()
const
196 return Qt::MoveAction;
199 QStringList QgsVectorTileBasicLabelingListModel::mimeTypes()
const
202 types << QStringLiteral(
"application/vnd.text.list" );
206 QMimeData *QgsVectorTileBasicLabelingListModel::mimeData(
const QModelIndexList &indexes )
const
208 QMimeData *mimeData =
new QMimeData();
209 QByteArray encodedData;
211 QDataStream stream( &encodedData, QIODevice::WriteOnly );
213 const auto constIndexes = indexes;
214 for (
const QModelIndex &index : constIndexes )
217 if ( !index.isValid() || index.column() != 0 )
223 QDomElement rootElem = doc.createElement( QStringLiteral(
"vector_tile_basic_labeling_style_mime" ) );
225 doc.appendChild( rootElem );
227 stream << doc.toString( -1 );
230 mimeData->setData( QStringLiteral(
"application/vnd.text.list" ), encodedData );
234 bool QgsVectorTileBasicLabelingListModel::dropMimeData(
const QMimeData *data,
235 Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
239 if ( action == Qt::IgnoreAction )
242 if ( !data->hasFormat( QStringLiteral(
"application/vnd.text.list" ) ) )
245 if ( parent.column() > 0 )
248 QByteArray encodedData = data->data( QStringLiteral(
"application/vnd.text.list" ) );
249 QDataStream stream( &encodedData, QIODevice::ReadOnly );
255 row = rowCount( parent );
258 while ( !stream.atEnd() )
264 if ( !doc.setContent( text ) )
266 QDomElement rootElem = doc.documentElement();
267 if ( rootElem.tagName() != QLatin1String(
"vector_tile_basic_labeling_style_mime" ) )
273 insertStyle( row + rows, style );
285 , mMessageBar( messageBar )
289 layout()->setContentsMargins( 0, 0, 0, 0 );
291 QMenu *menuAddRule =
new QMenu( btnAddRule );
295 btnAddRule->setMenu( menuAddRule );
298 connect( btnEditRule, &QPushButton::clicked,
this, &QgsVectorTileBasicLabelingWidget::editStyle );
299 connect( btnRemoveRule, &QAbstractButton::clicked,
this, &QgsVectorTileBasicLabelingWidget::removeStyle );
301 connect( viewStyles, &QAbstractItemView::doubleClicked,
this, &QgsVectorTileBasicLabelingWidget::editStyleAtIndex );
319 mModel =
new QgsVectorTileBasicLabelingListModel( mLabeling.get(), viewStyles );
320 viewStyles->setModel( mModel );
327 QgsVectorTileBasicLabelingWidget::~QgsVectorTileBasicLabelingWidget() =
default;
329 void QgsVectorTileBasicLabelingWidget::apply()
331 mVTLayer->setLabeling( mLabeling->clone() );
339 int rows = mModel->rowCount();
340 mModel->insertStyle( rows, style );
341 viewStyles->selectionModel()->setCurrentIndex( mModel->index( rows, 0 ), QItemSelectionModel::ClearAndSelect );
344 void QgsVectorTileBasicLabelingWidget::editStyle()
346 editStyleAtIndex( viewStyles->selectionModel()->currentIndex() );
349 void QgsVectorTileBasicLabelingWidget::editStyleAtIndex(
const QModelIndex &index )
366 QgsLabelingPanelWidget *widget =
new QgsLabelingPanelWidget( labelSettings, vectorLayer, mMapCanvas, panel );
367 widget->setContext( context );
368 widget->setPanelTitle( style.
styleName() );
378 void QgsVectorTileBasicLabelingWidget::updateLabelingFromWidget()
380 int index = viewStyles->selectionModel()->currentIndex().row();
383 QgsLabelingPanelWidget *widget = qobject_cast<QgsLabelingPanelWidget *>( sender() );
386 mLabeling->setStyle( index, style );
387 emit widgetChanged();
390 void QgsVectorTileBasicLabelingWidget::removeStyle()
392 QItemSelection sel = viewStyles->selectionModel()->selection();
393 const auto constSel = sel;
394 for (
const QItemSelectionRange &range : constSel )
396 if ( range.isValid() )
397 mModel->removeRows( range.top(), range.bottom() - range.top() + 1, range.parent() );
400 viewStyles->selectionModel()->clear();
410 mLabelingGui =
new QgsLabelingGui(
vectorLayer, mapCanvas, labelSettings,
this, labelSettings.
layerType );
411 mLabelingGui->setLabelMode( QgsLabelingGui::Labels );
413 mLabelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
414 QVBoxLayout *l =
new QVBoxLayout;
415 l->addWidget( mLabelingGui );
421 void QgsLabelingPanelWidget::setDockMode(
bool dockMode )
424 mLabelingGui->setDockMode( dockMode );
429 mLabelingGui->setContext( context );
434 return mLabelingGui->layerSettings();