@@ -25,10 +25,12 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
25
25
, mSectionLimit( 0 )
26
26
{
27
27
setupUi ( this );
28
- setWindowTitle ( QStringLiteral ( " Load style from database " ) );
28
+ setWindowTitle ( QStringLiteral ( " Saved styles manager " ) );
29
29
mSelectedStyleId = QLatin1String ( " " );
30
+ mSelectedStyleName = QLatin1String ( " " );
30
31
31
32
mLoadButton ->setDisabled ( true );
33
+ mDeleteButton ->setDisabled ( true );
32
34
mRelatedTable ->setEditTriggers ( QTableWidget::NoEditTriggers );
33
35
mRelatedTable ->horizontalHeader ()->setStretchLastSection ( true );
34
36
mRelatedTable ->setSelectionBehavior ( QTableWidget::SelectRows );
@@ -39,16 +41,18 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
39
41
mOthersTable ->setSelectionBehavior ( QTableWidget::SelectRows );
40
42
mOthersTable ->verticalHeader ()->setVisible ( false );
41
43
42
- connect ( mRelatedTable , SIGNAL ( cellClicked ( int , int ) ), this , SLOT ( cellSelectedRelatedTable ( int ) ) );
43
- connect ( mOthersTable , SIGNAL ( cellClicked ( int , int ) ), this , SLOT ( cellSelectedOthersTable ( int ) ) );
44
+ connect ( mRelatedTable , SIGNAL ( itemSelectionChanged ( ) ), this , SLOT ( relatedTableSelectionChanged ( ) ) );
45
+ connect ( mOthersTable , SIGNAL ( itemSelectionChanged ( ) ), this , SLOT ( otherTableSelectionChanged ( ) ) );
44
46
connect ( mRelatedTable , SIGNAL ( doubleClicked ( QModelIndex ) ), this , SLOT ( accept () ) );
45
47
connect ( mOthersTable , SIGNAL ( doubleClicked ( QModelIndex ) ), this , SLOT ( accept () ) );
46
48
connect ( mCancelButton , SIGNAL ( clicked () ), this , SLOT ( reject () ) );
49
+ connect ( mDeleteButton , SIGNAL ( clicked () ), this , SLOT ( deleteStyleFromDB () ) );
47
50
connect ( mLoadButton , SIGNAL ( clicked () ), this , SLOT ( accept () ) );
48
51
49
52
setTabOrder ( mRelatedTable , mOthersTable );
50
53
setTabOrder ( mOthersTable , mCancelButton );
51
- setTabOrder ( mCancelButton , mLoadButton );
54
+ setTabOrder ( mCancelButton , mDeleteButton );
55
+ setTabOrder ( mDeleteButton , mLoadButton );
52
56
53
57
QSettings settings;
54
58
restoreGeometry ( settings.value ( QStringLiteral ( " /Windows/loadStyleFromDb/geometry" ) ).toByteArray () );
@@ -102,14 +106,96 @@ QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
102
106
return mSelectedStyleId ;
103
107
}
104
108
105
- void QgsLoadStyleFromDBDialog::cellSelectedRelatedTable ( int r )
109
+ void QgsLoadStyleFromDBDialog::setLayer ( QgsVectorLayer *l )
106
110
{
107
- mLoadButton ->setEnabled ( true );
108
- mSelectedStyleId = mRelatedTable ->item ( r, 0 )->data ( Qt::UserRole ).toString ();
111
+ mLayer = l;
112
+ if ( mLayer ->dataProvider ()->isDeleteStyleFromDBSupported () )
113
+ {
114
+ // QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider supports isDeleteStyleFromDBSupported" );
115
+ mDeleteButton ->setVisible ( true );
116
+ }
117
+ else
118
+ {
119
+ // QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider does not supports isDeleteStyleFromDBSupported" );
120
+ mDeleteButton ->setVisible ( false );
121
+ }
122
+ }
123
+
124
+ void QgsLoadStyleFromDBDialog::relatedTableSelectionChanged ()
125
+ {
126
+ selectionChanged ( mRelatedTable );
127
+ // deselect any other row on the other table widget
128
+ QTableWidgetSelectionRange range ( 0 , 0 , mOthersTable ->rowCount () - 1 , mOthersTable ->columnCount () - 1 );
129
+ mOthersTable ->setRangeSelected ( range, false );
109
130
}
110
131
111
- void QgsLoadStyleFromDBDialog::cellSelectedOthersTable ( int r )
132
+ void QgsLoadStyleFromDBDialog::otherTableSelectionChanged ( )
112
133
{
113
- mLoadButton ->setEnabled ( true );
114
- mSelectedStyleId = mOthersTable ->item ( r, 0 )->data ( Qt::UserRole ).toString ();
134
+ selectionChanged ( mOthersTable );
135
+ // deselect any other row on the other table widget
136
+ QTableWidgetSelectionRange range ( 0 , 0 , mRelatedTable ->rowCount () - 1 , mRelatedTable ->columnCount () - 1 );
137
+ mRelatedTable ->setRangeSelected ( range, false );
138
+
139
+ }
140
+
141
+ void QgsLoadStyleFromDBDialog::selectionChanged ( QTableWidget *styleTable )
142
+ {
143
+ QTableWidgetItem *item;
144
+ QList<QTableWidgetItem *> selected = styleTable->selectedItems ();
145
+ // QgsDebugMsg( QString( "itemSelectionChanged(): count() = %1" ).arg( selected.count() ) );
146
+ if ( selected.count () > 0 )
147
+ {
148
+ item = selected.at ( 0 );
149
+ mSelectedStyleName = item->text ();
150
+ mSelectedStyleId = item->data ( Qt::UserRole ).toString ();
151
+ mLoadButton ->setEnabled ( true );
152
+ mDeleteButton ->setEnabled ( true );
153
+ }
154
+ else
155
+ {
156
+ mSelectedStyleName = " " ;
157
+ mSelectedStyleId = " " ;
158
+ mLoadButton ->setEnabled ( false );
159
+ mDeleteButton ->setEnabled ( false );
160
+ }
161
+ }
162
+
163
+ void QgsLoadStyleFromDBDialog::deleteStyleFromDB ()
164
+ {
165
+ QString uri, msgError;
166
+ QString infoWindowTitle = QObject::tr ( " Delete style %1 from %2" ).arg ( mSelectedStyleName , mLayer ->providerType () );
167
+ // QgsDebugMsg( QString( "Delete style: %1 " ).arg( mSelectedStyleName ) );
168
+
169
+ if ( QMessageBox::question ( nullptr , QObject::tr ( " Delete style" ),
170
+ QObject::tr ( " Are you sure you want to delete the style %1?" ).arg ( mSelectedStyleName ),
171
+ QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
172
+ return ;
173
+
174
+ uri = mLayer ->dataProvider ()->dataSourceUri ();
175
+ mLayer ->dataProvider ()->deleteStyleById ( uri, mSelectedStyleId , msgError );
176
+
177
+ if ( !msgError.isNull () )
178
+ {
179
+ QMessageBox::warning ( this , infoWindowTitle, msgError );
180
+ }
181
+ else
182
+ {
183
+ QMessageBox::information ( this , infoWindowTitle, tr ( " Style deleted" ) );
184
+
185
+ // Delete all rows from the UI table widgets
186
+ mRelatedTable ->setRowCount ( 0 );
187
+ mOthersTable ->setRowCount ( 0 );
188
+
189
+ // Fill UI widgets again from DB. Other users might have change the styles meanwhile.
190
+ QString errorMsg;
191
+ QStringList ids, names, descriptions;
192
+ // get the list of styles in the db
193
+ int sectionLimit = mLayer ->listStylesInDatabase ( ids, names, descriptions, errorMsg );
194
+ if ( !errorMsg.isNull () )
195
+ {
196
+ QMessageBox::warning ( this , tr ( " Error occurred retrieving styles from database" ), errorMsg );
197
+ return ;
198
+ }
199
+ initializeLists ( ids, names, descriptions, sectionLimit );
200
+ }
115
201
}
0 commit comments