Skip to content

Commit fe686d3

Browse files
committed
Added removal of projects
1 parent 0c701fb commit fe686d3

4 files changed

+61
-24
lines changed

src/providers/postgres/qgspostgresprojectstorage.cpp

+9-17
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ QStringList QgsPostgresProjectStorage::listProjects( const QString &uri )
5252
{
5353
QStringList lst;
5454

55-
QgsPostgresProjectUri projectUri = parseUri( uri );
55+
QgsPostgresProjectUri projectUri = decodeUri( uri );
5656
if ( !projectUri.valid )
5757
return lst;
5858

@@ -81,7 +81,7 @@ QStringList QgsPostgresProjectStorage::listProjects( const QString &uri )
8181

8282
bool QgsPostgresProjectStorage::readProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context )
8383
{
84-
QgsPostgresProjectUri projectUri = parseUri( uri );
84+
QgsPostgresProjectUri projectUri = decodeUri( uri );
8585
if ( !projectUri.valid )
8686
{
8787
context.pushMessage( "Invalid URI for PostgreSQL provider: " + uri, Qgis::Critical );
@@ -121,7 +121,7 @@ bool QgsPostgresProjectStorage::readProject( const QString &uri, QIODevice *devi
121121

122122
bool QgsPostgresProjectStorage::writeProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context )
123123
{
124-
QgsPostgresProjectUri projectUri = parseUri( uri );
124+
QgsPostgresProjectUri projectUri = decodeUri( uri );
125125
if ( !projectUri.valid )
126126
{
127127
context.pushMessage( "Invalid URI for PostgreSQL provider: " + uri, Qgis::Critical );
@@ -173,7 +173,7 @@ bool QgsPostgresProjectStorage::writeProject( const QString &uri, QIODevice *dev
173173

174174
bool QgsPostgresProjectStorage::removeProject( const QString &uri )
175175
{
176-
QgsPostgresProjectUri projectUri = parseUri( uri );
176+
QgsPostgresProjectUri projectUri = decodeUri( uri );
177177
if ( !projectUri.valid )
178178
return false;
179179

@@ -195,7 +195,7 @@ bool QgsPostgresProjectStorage::removeProject( const QString &uri )
195195

196196
bool QgsPostgresProjectStorage::readProjectMetadata( const QString &uri, QgsProjectStorage::Metadata &metadata )
197197
{
198-
QgsPostgresProjectUri projectUri = parseUri( uri );
198+
QgsPostgresProjectUri projectUri = decodeUri( uri );
199199
if ( !projectUri.valid )
200200
return false;
201201

@@ -235,11 +235,7 @@ QString QgsPostgresProjectStorage::showLoadGui()
235235
if ( !dlg.exec() )
236236
return QString();
237237

238-
QgsPostgresProjectUri postUri;
239-
postUri.connInfo = QgsPostgresConn::connUri( dlg.connectionName() );
240-
postUri.schemaName = dlg.schemaName();
241-
postUri.projectName = dlg.projectName();
242-
return makeUri( postUri );
238+
return dlg.currentProjectUri();
243239
}
244240

245241
QString QgsPostgresProjectStorage::showSaveGui()
@@ -248,17 +244,13 @@ QString QgsPostgresProjectStorage::showSaveGui()
248244
if ( !dlg.exec() )
249245
return QString();
250246

251-
QgsPostgresProjectUri postUri;
252-
postUri.connInfo = QgsPostgresConn::connUri( dlg.connectionName() );
253-
postUri.schemaName = dlg.schemaName();
254-
postUri.projectName = dlg.projectName();
255-
return makeUri( postUri );
247+
return dlg.currentProjectUri();
256248
}
257249

258250
#endif
259251

260252

261-
QString QgsPostgresProjectStorage::makeUri( const QgsPostgresProjectUri &postUri )
253+
QString QgsPostgresProjectStorage::encodeUri( const QgsPostgresProjectUri &postUri )
262254
{
263255
QUrl u;
264256
QUrlQuery urlQuery;
@@ -283,7 +275,7 @@ QString QgsPostgresProjectStorage::makeUri( const QgsPostgresProjectUri &postUri
283275
}
284276

285277

286-
QgsPostgresProjectUri QgsPostgresProjectStorage::parseUri( const QString &uri )
278+
QgsPostgresProjectUri QgsPostgresProjectStorage::decodeUri( const QString &uri )
287279
{
288280
QUrl u = QUrl::fromEncoded( uri.toUtf8() );
289281
QUrlQuery urlQuery( u.query() );

src/providers/postgres/qgspostgresprojectstorage.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class QgsPostgresProjectStorage : public QgsProjectStorage
4444
virtual QString showSaveGui() override;
4545
#endif
4646

47-
static QString makeUri( const QgsPostgresProjectUri &postUri );
48-
static QgsPostgresProjectUri parseUri( const QString &uri );
47+
static QString encodeUri( const QgsPostgresProjectUri &postUri );
48+
static QgsPostgresProjectUri decodeUri( const QString &uri );
4949
};
5050

5151
#endif // QGSPOSTGRESPROJECTSTORAGE_H

src/providers/postgres/qgspostgresprojectstoragedialog.cpp

+44-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include "qgsprojectstorage.h"
99
#include "qgsprojectstorageregistry.h"
1010

11+
#include <QMenu>
1112
#include <QMessageBox>
13+
#include <QPushButton>
1214

1315
QgsPostgresProjectStorageDialog::QgsPostgresProjectStorageDialog( bool saving, QWidget *parent )
1416
: QDialog( parent )
@@ -18,6 +20,12 @@ QgsPostgresProjectStorageDialog::QgsPostgresProjectStorageDialog( bool saving, Q
1820

1921
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsPostgresProjectStorageDialog::onOK );
2022

23+
QPushButton *btnManageProjects = new QPushButton( tr( "Manage Projects" ), this );
24+
QMenu *menuManageProjects = new QMenu( btnManageProjects );
25+
mActionRemoveProject = menuManageProjects->addAction( tr( "Remove Project" ), this, &QgsPostgresProjectStorageDialog::removeProject );
26+
btnManageProjects->setMenu( menuManageProjects );
27+
buttonBox->addButton( btnManageProjects, QDialogButtonBox::ActionRole );
28+
2129
if ( saving )
2230
{
2331
setWindowTitle( tr( "Save project to PostgreSQL" ) );
@@ -38,6 +46,9 @@ QgsPostgresProjectStorageDialog::QgsPostgresProjectStorageDialog( bool saving, Q
3846
mCboConnection->setCurrentIndex( mCboConnection->findText( toSelect ) );
3947

4048
connect( mCboSchema, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPostgresProjectStorageDialog::populateProjects );
49+
connect( mCboProject, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPostgresProjectStorageDialog::projectChanged );
50+
51+
projectChanged();
4152
}
4253

4354
QString QgsPostgresProjectStorageDialog::connectionName() const
@@ -89,20 +100,19 @@ void QgsPostgresProjectStorageDialog::populateSchemas()
89100
{
90101
mCboSchema->addItem( schema.name );
91102
}
103+
104+
projectChanged();
92105
}
93106

94107
void QgsPostgresProjectStorageDialog::populateProjects()
95108
{
96109
mCboProject->clear();
97110

98-
QgsPostgresProjectUri postUri;
99-
postUri.connInfo = QgsPostgresConn::connUri( mCboConnection->currentText() );
100-
postUri.schemaName = mCboSchema->currentText();
101-
QString uri = QgsPostgresProjectStorage::makeUri( postUri );
102-
111+
QString uri = currentProjectUri();
103112
QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromType( "postgresql" );
104113
Q_ASSERT( storage );
105114
mCboProject->addItems( storage->listProjects( uri ) );
115+
projectChanged();
106116
}
107117

108118
void QgsPostgresProjectStorageDialog::onOK()
@@ -125,3 +135,32 @@ void QgsPostgresProjectStorageDialog::onOK()
125135

126136
accept();
127137
}
138+
139+
void QgsPostgresProjectStorageDialog::projectChanged()
140+
{
141+
mActionRemoveProject->setEnabled( mCboProject->count() != 0 && mCboProject->findText( mCboProject->currentText() ) != -1 );
142+
}
143+
144+
void QgsPostgresProjectStorageDialog::removeProject()
145+
{
146+
int res = QMessageBox::question( this, tr( "Remove project" ),
147+
tr( "Do you really want to remove the project \"%1\"?" ).arg( mCboProject->currentText() ),
148+
QMessageBox::Yes | QMessageBox::No );
149+
if ( res != QMessageBox::Yes )
150+
return;
151+
152+
QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromType( "postgresql" );
153+
Q_ASSERT( storage );
154+
storage->removeProject( currentProjectUri() );
155+
populateProjects();
156+
}
157+
158+
QString QgsPostgresProjectStorageDialog::currentProjectUri( bool schemaOnly )
159+
{
160+
QgsPostgresProjectUri postUri;
161+
postUri.connInfo = QgsPostgresConn::connUri( mCboConnection->currentText() );
162+
postUri.schemaName = mCboSchema->currentText();
163+
if ( !schemaOnly )
164+
postUri.projectName = mCboProject->currentText();
165+
return QgsPostgresProjectStorage::encodeUri( postUri );
166+
}

src/providers/postgres/qgspostgresprojectstoragedialog.h

+6
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ class QgsPostgresProjectStorageDialog : public QDialog, private Ui::QgsPostgresP
1616
QString schemaName() const;
1717
QString projectName() const;
1818

19+
QString currentProjectUri( bool schemaOnly = false );
20+
1921
signals:
2022

2123
private slots:
2224
void populateSchemas();
2325
void populateProjects();
2426
void onOK();
27+
void projectChanged();
28+
void removeProject();
2529

2630
private:
31+
2732
bool mSaving; //!< Whether using this dialog for loading or saving a project
33+
QAction *mActionRemoveProject = nullptr;
2834
};
2935

3036
#endif // QGSPOSTGRESPROJECTSTORAGEDIALOG_H

0 commit comments

Comments
 (0)