13
13
* *
14
14
***************************************************************************/
15
15
16
- #include " qgsloadstylefromdbdialog.h"
16
+ #include < QMessageBox>
17
+ #include < QVector>
18
+
19
+ #include " qgsvectorlayerloadstyledialog.h"
17
20
#include " qgslogger.h"
18
21
#include " qgisapp.h"
19
22
#include " qgssettings.h"
23
+ #include " qgsvectorlayerproperties.h"
24
+
20
25
21
- #include < QMessageBox>
22
- #include < QVector>
23
26
24
- QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog ( QWidget *parent )
27
+ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog ( QgsVectorLayer *layer, QWidget *parent )
25
28
: QDialog( parent )
29
+ , mLayer( layer )
26
30
{
27
31
setupUi ( this );
28
32
setWindowTitle ( QStringLiteral ( " Database styles manager" ) );
29
33
34
+ QgsSettings settings;
35
+
36
+ QString providerName = mLayer ->providerType ();
37
+ if ( providerName == QLatin1String ( " ogr" ) )
38
+ {
39
+ providerName = mLayer ->dataProvider ()->storageType ();
40
+ if ( providerName == QLatin1String ( " GPKG" ) )
41
+ providerName = QStringLiteral ( " GeoPackage" );
42
+ }
43
+
44
+ QString myLastUsedDir = settings.value ( QStringLiteral ( " style/lastStyleDir" ), QDir::homePath () ).toString ();
45
+
46
+ // load style type combobox
47
+ connect ( mStyleTypeComboBox , qgis::overload<int >::of ( &QComboBox::currentIndexChanged ), this , [ = ]( int )
48
+ {
49
+ QgsVectorLayerProperties::StyleType type = currentStyleType ();
50
+ mFromFileWidget ->setVisible ( type != QgsVectorLayerProperties::DB );
51
+ mFromDbWidget ->setVisible ( type == QgsVectorLayerProperties::DB );
52
+ mDeleteButton ->setVisible ( type == QgsVectorLayerProperties::DB && mLayer ->dataProvider ()->isDeleteStyleFromDatabaseSupported () );
53
+ mStyleCategoriesListWidget ->setEnabled ( currentStyleType () != QgsVectorLayerProperties::SLD );
54
+ updateLoadButtonState ();
55
+ } );
56
+ mStyleTypeComboBox ->addItem ( tr ( " from file" ), QgsVectorLayerProperties::QML ); // QML is used as entry, but works for SLD too, see currentStyleType()
57
+ if ( mLayer ->dataProvider ()->isSaveAndLoadStyleToDatabaseSupported () )
58
+ mStyleTypeComboBox ->addItem ( tr ( " from database (%1)" ).arg ( providerName ), QgsVectorLayerProperties::DB );
59
+
60
+ // fill style categories
61
+ QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue ( QStringLiteral ( " style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
62
+ for ( QgsMapLayer::StyleCategory category : qgsEnumMap<QgsMapLayer::StyleCategory>().keys () )
63
+ {
64
+ if ( category == QgsMapLayer::AllStyleCategories )
65
+ continue ;
66
+
67
+ QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory ( category );
68
+
69
+ QListWidgetItem *item = new QListWidgetItem ( readableCategory.icon (), readableCategory.name (), mStyleCategoriesListWidget );
70
+ item->setFlags ( ( item->flags () | Qt::ItemIsUserCheckable ) & ~Qt::ItemIsSelectable );
71
+ item->setCheckState ( lastStyleCategories.testFlag ( category ) ? Qt::Checked : Qt::Unchecked );
72
+ item->setData ( Qt::UserRole, category );
73
+ }
74
+
75
+ // load from file setup
76
+ mFileWidget ->setFilter ( tr ( " QGIS Layer Style File, SLD File" ) + QStringLiteral ( " (*.qml *.sld)" ) );
77
+ mFileWidget ->setStorageMode ( QgsFileWidget::GetFile );
78
+ mFileWidget ->setDefaultRoot ( myLastUsedDir );
79
+ connect ( mFileWidget , &QgsFileWidget::fileChanged, this , [ = ]( const QString & )
80
+ {
81
+ mStyleCategoriesListWidget ->setEnabled ( currentStyleType () != QgsVectorLayerProperties::SLD );
82
+ updateLoadButtonState ();
83
+ } );
84
+
85
+ // load from DB
30
86
mLoadButton ->setDisabled ( true );
31
87
mDeleteButton ->setDisabled ( true );
32
88
mRelatedTable ->setEditTriggers ( QTableWidget::NoEditTriggers );
33
89
mRelatedTable ->horizontalHeader ()->setStretchLastSection ( true );
34
90
mRelatedTable ->setSelectionBehavior ( QTableWidget::SelectRows );
35
91
mRelatedTable ->verticalHeader ()->setVisible ( false );
36
-
37
92
mOthersTable ->setEditTriggers ( QTableWidget::NoEditTriggers );
38
93
mOthersTable ->horizontalHeader ()->setStretchLastSection ( true );
39
94
mOthersTable ->setSelectionBehavior ( QTableWidget::SelectRows );
40
95
mOthersTable ->verticalHeader ()->setVisible ( false );
41
-
42
- connect ( mRelatedTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
43
- connect ( mOthersTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
96
+ connect ( mRelatedTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged );
97
+ connect ( mOthersTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsVectorLayerLoadStyleDialog::onOthersTableSelectionChanged );
44
98
connect ( mRelatedTable , &QTableWidget::doubleClicked, this , &QDialog::accept );
45
99
connect ( mOthersTable , &QTableWidget::doubleClicked, this , &QDialog::accept );
46
100
connect ( mCancelButton , &QPushButton::clicked, this , &QDialog::reject );
47
101
connect ( mLoadButton , &QPushButton::clicked, this , &QDialog::accept );
48
- connect ( mDeleteButton , &QPushButton::clicked, this , &QgsLoadStyleFromDBDialog::deleteStyleFromDB );
49
-
102
+ connect ( mDeleteButton , &QPushButton::clicked, this , &QgsVectorLayerLoadStyleDialog::deleteStyleFromDB );
50
103
setTabOrder ( mRelatedTable , mOthersTable );
51
- setTabOrder ( mOthersTable , mCancelButton );
52
- setTabOrder ( mCancelButton , mDeleteButton );
53
- setTabOrder ( mDeleteButton , mLoadButton );
54
104
55
- QgsSettings settings;
56
- restoreGeometry ( settings. value ( QStringLiteral ( " Windows/loadStyleFromDb/geometry " ) ). toByteArray () );
105
+ restoreGeometry ( settings. value ( QStringLiteral ( " Windows/vectorLayerLoadStyle/geometry " ) ). toByteArray () ) ;
106
+ mStyleCategoriesListWidget -> adjustSize ( );
57
107
}
58
108
59
- QgsLoadStyleFromDBDialog ::~QgsLoadStyleFromDBDialog ()
109
+ QgsVectorLayerLoadStyleDialog ::~QgsVectorLayerLoadStyleDialog ()
60
110
{
61
111
QgsSettings settings;
62
- settings.setValue ( QStringLiteral ( " Windows/loadStyleFromDb /geometry" ), saveGeometry () );
112
+ settings.setValue ( QStringLiteral ( " Windows/vectorLayerLoadStyle /geometry" ), saveGeometry () );
63
113
}
64
114
65
- void QgsLoadStyleFromDBDialog::initializeLists ( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
115
+ QgsMapLayer::StyleCategories QgsVectorLayerLoadStyleDialog::styleCategories () const
116
+ {
117
+ QgsMapLayer::StyleCategories categories;
118
+ for ( int row = 0 ; row < mStyleCategoriesListWidget ->count (); ++row )
119
+ {
120
+ QListWidgetItem *item = mStyleCategoriesListWidget ->item ( row );
121
+ if ( item->checkState () == Qt::Checked )
122
+ categories |= item->data ( Qt::UserRole ).value <QgsMapLayer::StyleCategory>();
123
+ }
124
+ return categories;
125
+ }
126
+
127
+ QgsVectorLayerProperties::StyleType QgsVectorLayerLoadStyleDialog::currentStyleType () const
128
+ {
129
+ QgsVectorLayerProperties::StyleType type = mStyleTypeComboBox ->currentData ().value <QgsVectorLayerProperties::StyleType>();
130
+ if ( type == QgsVectorLayerProperties::QML )
131
+ {
132
+ QFileInfo fi ( mFileWidget ->filePath () );
133
+ if ( fi.exists () && fi.suffix ().compare ( QStringLiteral ( " sld" ), Qt::CaseInsensitive ) == 0 )
134
+ type = QgsVectorLayerProperties::SLD;
135
+ }
136
+ return type;
137
+ }
138
+
139
+ QString QgsVectorLayerLoadStyleDialog::filePath () const
140
+ {
141
+ return mFileWidget ->filePath ();
142
+ }
143
+
144
+ void QgsVectorLayerLoadStyleDialog::initializeLists ( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
66
145
{
67
146
mSectionLimit = sectionLimit;
68
147
int relatedTableNOfCols = sectionLimit > 0 ? 2 : 1 ;
@@ -98,48 +177,42 @@ void QgsLoadStyleFromDBDialog::initializeLists( const QStringList &ids, const QS
98
177
}
99
178
}
100
179
101
- QString QgsLoadStyleFromDBDialog::getSelectedStyleId ()
180
+ QString QgsVectorLayerLoadStyleDialog::selectedStyleId ()
102
181
{
103
182
return mSelectedStyleId ;
104
183
}
105
184
106
- void QgsLoadStyleFromDBDialog::setLayer ( QgsVectorLayer *l )
107
- {
108
- mLayer = l;
109
- mDeleteButton ->setVisible ( mLayer ->dataProvider ()->isDeleteStyleFromDatabaseSupported () );
110
- }
111
-
112
- void QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged ()
185
+ void QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged ()
113
186
{
114
187
selectionChanged ( mRelatedTable );
115
188
if ( mRelatedTable ->selectionModel ()->hasSelection () )
116
189
{
117
190
if ( mOthersTable ->selectionModel ()->hasSelection () )
118
191
{
119
- disconnect ( mOthersTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsLoadStyleFromDBDialog ::onOthersTableSelectionChanged );
192
+ disconnect ( mOthersTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsVectorLayerLoadStyleDialog ::onOthersTableSelectionChanged );
120
193
QTableWidgetSelectionRange range ( 0 , 0 , mOthersTable ->rowCount () - 1 , mOthersTable ->columnCount () - 1 );
121
194
mOthersTable ->setRangeSelected ( range, false );
122
- connect ( mOthersTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsLoadStyleFromDBDialog ::onOthersTableSelectionChanged );
195
+ connect ( mOthersTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsVectorLayerLoadStyleDialog ::onOthersTableSelectionChanged );
123
196
}
124
197
}
125
198
}
126
199
127
- void QgsLoadStyleFromDBDialog ::onOthersTableSelectionChanged ()
200
+ void QgsVectorLayerLoadStyleDialog ::onOthersTableSelectionChanged ()
128
201
{
129
202
selectionChanged ( mOthersTable );
130
203
if ( mOthersTable ->selectionModel ()->hasSelection () )
131
204
{
132
205
if ( mRelatedTable ->selectionModel ()->hasSelection () )
133
206
{
134
- disconnect ( mRelatedTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsLoadStyleFromDBDialog ::onRelatedTableSelectionChanged );
207
+ disconnect ( mRelatedTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsVectorLayerLoadStyleDialog ::onRelatedTableSelectionChanged );
135
208
QTableWidgetSelectionRange range ( 0 , 0 , mRelatedTable ->rowCount () - 1 , mRelatedTable ->columnCount () - 1 );
136
209
mRelatedTable ->setRangeSelected ( range, false );
137
- connect ( mRelatedTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsLoadStyleFromDBDialog ::onRelatedTableSelectionChanged );
210
+ connect ( mRelatedTable ->selectionModel (), &QItemSelectionModel::selectionChanged, this , &QgsVectorLayerLoadStyleDialog ::onRelatedTableSelectionChanged );
138
211
}
139
212
}
140
213
}
141
214
142
- void QgsLoadStyleFromDBDialog ::selectionChanged ( QTableWidget *styleTable )
215
+ void QgsVectorLayerLoadStyleDialog ::selectionChanged ( QTableWidget *styleTable )
143
216
{
144
217
QTableWidgetItem *item = nullptr ;
145
218
QList<QTableWidgetItem *> selected = styleTable->selectedItems ();
@@ -159,9 +232,17 @@ void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
159
232
mLoadButton ->setEnabled ( false );
160
233
mDeleteButton ->setEnabled ( false );
161
234
}
235
+
236
+ updateLoadButtonState ();
237
+ }
238
+
239
+ void QgsVectorLayerLoadStyleDialog::accept ()
240
+ {
241
+ QgsSettings ().setFlagValue ( QStringLiteral ( " style/lastStyleCategories" ), styleCategories () );
242
+ QDialog::accept ();
162
243
}
163
244
164
- void QgsLoadStyleFromDBDialog ::deleteStyleFromDB ()
245
+ void QgsVectorLayerLoadStyleDialog ::deleteStyleFromDB ()
165
246
{
166
247
QString msgError;
167
248
QString opInfo = QObject::tr ( " Delete style %1 from %2" ).arg ( mSelectedStyleName , mLayer ->providerType () );
@@ -200,3 +281,15 @@ void QgsLoadStyleFromDBDialog::deleteStyleFromDB()
200
281
}
201
282
}
202
283
}
284
+
285
+ void QgsVectorLayerLoadStyleDialog::updateLoadButtonState ()
286
+ {
287
+ QgsVectorLayerProperties::StyleType type = currentStyleType ();
288
+ mLoadButton ->setEnabled ( ( type == QgsVectorLayerProperties::DB
289
+ && ( mRelatedTable ->selectionModel ()->hasSelection () || mOthersTable ->selectionModel ()->hasSelection ()
290
+ ) ) ||
291
+ ( type != QgsVectorLayerProperties::DB && !mFileWidget ->filePath ().isEmpty () ) );
292
+
293
+
294
+
295
+ }
0 commit comments