16 #include <QVBoxLayout>
18 #include <QInputDialog>
19 #include <QMessageBox>
20 #include <QFileDialog>
38 mModel =
new QStandardItemModel(
this );
39 mStyleList =
new QListView(
this );
40 mStyleList->setModel( mModel );
41 mStyleList->setViewMode( QListView::ListMode );
42 mStyleList->setResizeMode( QListView::Adjust );
44 QToolBar *toolbar =
new QToolBar(
this );
45 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
47 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
48 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
50 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
51 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
53 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
54 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
55 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
56 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
57 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
67 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
69 setLayout(
new QVBoxLayout() );
70 layout()->setContentsMargins( 0, 0, 0, 0 );
71 layout()->addWidget( toolbar );
72 layout()->addWidget( mStyleList );
82 for (
const QString &styleName : styles )
84 QStandardItem *item =
new QStandardItem( styleName );
85 item->setData( styleName );
86 mModel->appendRow( item );
90 currentStyleChanged( active );
92 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
95 void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
100 QString name = index.data().toString();
104 void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
106 QList<QStandardItem *> items = mModel->findItems( name );
107 if ( items.isEmpty() )
110 QStandardItem *item = items.at( 0 );
112 mStyleList->setCurrentIndex( item->index() );
115 void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
118 QStandardItem *item =
new QStandardItem( name );
119 item->setData( name );
120 mModel->appendRow( item );
123 void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
125 QList<QStandardItem *> items = mModel->findItems( name );
126 if ( items.isEmpty() )
129 QStandardItem *item = items.at( 0 );
130 mModel->removeRow( item->row() );
133 void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
135 QList<QStandardItem *> items = mModel->findItems( oldname );
136 if ( items.isEmpty() )
139 QStandardItem *item = items.at( 0 );
140 item->setText( newname );
141 item->setData( newname );
144 void QgsMapLayerStyleManagerWidget::addStyle()
147 QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ),
148 tr(
"Style name:" ), QLineEdit::Normal,
149 QStringLiteral(
"new style" ), &ok );
150 if ( !ok || text.isEmpty() )
164 void QgsMapLayerStyleManagerWidget::removeStyle()
167 QList<QStandardItem *> items = mModel->findItems( current );
168 if ( items.isEmpty() )
171 QStandardItem *item = items.at( 0 );
175 mModel->removeRow( item->row() );
179 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
184 void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
186 const QString oldName = item->data().toString();
187 const QString newName = item->text();
188 item->setData( newName );
189 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
192 void QgsMapLayerStyleManagerWidget::saveAsDefault()
198 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
200 QMessageBox askToUser;
201 askToUser.setWindowTitle( tr(
"Save Style" ) );
202 askToUser.setText( tr(
"Save default style to: " ) );
203 askToUser.setIcon( QMessageBox::Question );
204 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
205 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
206 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
208 switch ( askToUser.exec() )
213 layer->saveStyleToDatabase( QString(), QString(),
true, QString(), errorMsg );
214 if ( errorMsg.isNull() )
225 bool defaultSavedFlag =
false;
227 if ( !defaultSavedFlag )
229 QMessageBox::warning(
this, tr(
"Default Style" ), errorMsg );
234 void QgsMapLayerStyleManagerWidget::loadDefault()
237 bool defaultLoadedFlag =
false;
241 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
243 QMessageBox askToUser;
244 askToUser.setWindowTitle( tr(
"Load Style" ) );
245 askToUser.setText( tr(
"Load default style from: " ) );
246 askToUser.setIcon( QMessageBox::Question );
247 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
248 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
249 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
251 switch ( askToUser.exec() )
256 msg = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
257 if ( !defaultLoadedFlag )
260 QMessageBox::information(
this, tr(
"Default Style" ), msg );
262 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
264 QMessageBox::information(
this, tr(
"Default Style" ),
265 tr(
"No default style was found for this layer" ) );
277 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag,
true );
281 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
288 if ( !defaultLoadedFlag )
291 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
300 void QgsMapLayerStyleManagerWidget::saveStyle()
305 void QgsMapLayerStyleManagerWidget::loadStyle()
308 QString myLastUsedDir = myQSettings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
310 QString myFileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), myLastUsedDir,
311 tr(
"QGIS Layer Style File" ) +
" (*.qml);;" + tr(
"SLD File" ) +
" (*.sld)" );
312 if ( myFileName.isNull() )
318 bool defaultLoadedFlag =
false;
320 if ( myFileName.endsWith( QLatin1String(
".sld" ), Qt::CaseInsensitive ) )
330 if ( defaultLoadedFlag )
337 QMessageBox::warning(
this, tr(
"Load Style" ), myMessage );
340 QFileInfo myFI( myFileName );
341 QString myPath = myFI.path();
342 myQSettings.
setValue( QStringLiteral(
"style/lastStyleDir" ), myPath );