16 #include <QVBoxLayout>
18 #include <QInputDialog>
19 #include <QMessageBox>
20 #include <QFileDialog>
39 mModel =
new QStandardItemModel(
this );
40 mStyleList =
new QListView(
this );
41 mStyleList->setModel( mModel );
42 mStyleList->setViewMode( QListView::ListMode );
43 mStyleList->setResizeMode( QListView::Adjust );
45 QToolBar *toolbar =
new QToolBar(
this );
46 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
48 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
49 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
51 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
52 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
54 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
55 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
56 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
57 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
58 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
68 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
70 setLayout(
new QVBoxLayout() );
71 layout()->setContentsMargins( 0, 0, 0, 0 );
72 layout()->addWidget( toolbar );
73 layout()->addWidget( mStyleList );
83 for (
const QString &styleName : styles )
85 QStandardItem *item =
new QStandardItem( styleName );
86 item->setData( styleName );
87 mModel->appendRow( item );
91 currentStyleChanged( active );
93 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
96 void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
101 const QString name = index.data().toString();
105 void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
107 const QList<QStandardItem *> items = mModel->findItems( name );
108 if ( items.isEmpty() )
111 QStandardItem *item = items.at( 0 );
113 mStyleList->setCurrentIndex( item->index() );
116 void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
119 QStandardItem *item =
new QStandardItem( name );
120 item->setData( name );
121 mModel->appendRow( item );
124 void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
126 const QList<QStandardItem *> items = mModel->findItems( name );
127 if ( items.isEmpty() )
130 QStandardItem *item = items.at( 0 );
131 mModel->removeRow( item->row() );
134 void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
136 const QList<QStandardItem *> items = mModel->findItems( oldname );
137 if ( items.isEmpty() )
140 QStandardItem *item = items.at( 0 );
141 item->setText( newname );
142 item->setData( newname );
145 void QgsMapLayerStyleManagerWidget::addStyle()
148 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ),
149 tr(
"Style name:" ), QLineEdit::Normal,
150 QStringLiteral(
"new style" ), &ok );
151 if ( !ok || text.isEmpty() )
165 void QgsMapLayerStyleManagerWidget::removeStyle()
168 const QList<QStandardItem *> items = mModel->findItems( current );
169 if ( items.isEmpty() )
172 QStandardItem *item = items.at( 0 );
176 mModel->removeRow( item->row() );
180 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
185 void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
187 const QString oldName = item->data().toString();
188 const QString newName = item->text();
189 item->setData( newName );
190 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
193 void QgsMapLayerStyleManagerWidget::saveAsDefault()
199 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
201 QMessageBox askToUser;
202 askToUser.setWindowTitle( tr(
"Save Style" ) );
203 askToUser.setText( tr(
"Save default style to: " ) );
204 askToUser.setIcon( QMessageBox::Question );
205 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
206 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
207 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
209 switch ( askToUser.exec() )
215 QString errorMessage;
218 if ( QMessageBox::question(
nullptr, tr(
"Save style in database" ),
219 tr(
"A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
220 QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
225 else if ( !errorMessage.isEmpty() )
227 QMessageBox::warning(
nullptr, tr(
"Save style in database" ),
232 layer->saveStyleToDatabase( QString(), QString(),
true, QString(), errorMsg );
233 if ( errorMsg.isNull() )
245 bool defaultSavedFlag =
false;
251 if ( !defaultSavedFlag )
253 QMessageBox::warning(
this, tr(
"Default Style" ), errorMsg );
258 void QgsMapLayerStyleManagerWidget::loadDefault()
261 bool defaultLoadedFlag =
false;
265 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
267 QMessageBox askToUser;
268 askToUser.setWindowTitle( tr(
"Load Style" ) );
269 askToUser.setText( tr(
"Load default style from: " ) );
270 askToUser.setIcon( QMessageBox::Question );
271 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
272 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
273 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
275 switch ( askToUser.exec() )
280 msg = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
281 if ( !defaultLoadedFlag )
284 QMessageBox::information(
this, tr(
"Default Style" ), msg );
286 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
288 QMessageBox::information(
this, tr(
"Default Style" ),
289 tr(
"No default style was found for this layer" ) );
301 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag,
true );
305 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
312 if ( !defaultLoadedFlag )
315 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
324 void QgsMapLayerStyleManagerWidget::saveStyle()
329 void QgsMapLayerStyleManagerWidget::loadStyle()
332 const QString myLastUsedDir = myQSettings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
334 const QString myFileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), myLastUsedDir,
335 tr(
"QGIS Layer Style File" ) +
" (*.qml);;" + tr(
"SLD File" ) +
" (*.sld)" );
336 if ( myFileName.isNull() )
342 bool defaultLoadedFlag =
false;
344 if ( myFileName.endsWith( QLatin1String(
".sld" ), Qt::CaseInsensitive ) )
354 if ( defaultLoadedFlag )
361 QMessageBox::warning(
this, tr(
"Load Style" ), myMessage );
364 const QFileInfo myFI( myFileName );
365 const QString myPath = myFI.path();
366 myQSettings.
setValue( QStringLiteral(
"style/lastStyleDir" ), myPath );