Skip to content

Commit a561a4a

Browse files
committed
Move PG data items to separate cpp/h files
1 parent eb298d8 commit a561a4a

File tree

5 files changed

+294
-276
lines changed

5 files changed

+294
-276
lines changed

src/providers/postgres/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
SET(PG_SRCS
66
qgspostgresprovider.cpp
77
qgspostgresconnection.cpp
8+
qgspostgresdataitems.cpp
89
qgspgsourceselect.cpp
910
qgspgnewconnection.cpp
1011
)
1112
SET(PG_MOC_HDRS
1213
qgspostgresprovider.h
1314
qgspostgresconnection.h
15+
qgspostgresdataitems.h
1416
qgspgsourceselect.h
1517
qgspgnewconnection.h
1618
)
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
#include "qgspostgresdataitems.h"
2+
3+
#include "qgslogger.h"
4+
5+
#include "qgspostgresconnection.h"
6+
#include "qgspgsourceselect.h"
7+
#include "qgspgnewconnection.h"
8+
9+
// ---------------------------------------------------------------------------
10+
QgsPGConnectionItem::QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path )
11+
: QgsDataCollectionItem( parent, name, path )
12+
{
13+
}
14+
15+
QgsPGConnectionItem::~QgsPGConnectionItem()
16+
{
17+
}
18+
19+
QVector<QgsDataItem*> QgsPGConnectionItem::createChildren()
20+
{
21+
QgsDebugMsg( "Entered" );
22+
QVector<QgsDataItem*> children;
23+
QgsPostgresConnection connection( mName );
24+
QgsPostgresProvider *pgProvider = connection.provider( );
25+
if ( !pgProvider )
26+
return children;
27+
28+
QString mConnInfo = connection.connectionInfo();
29+
QgsDebugMsg( "mConnInfo = " + mConnInfo );
30+
31+
if ( !pgProvider->supportedLayers( mLayerProperties, true, false, false ) )
32+
return children;
33+
34+
QMap<QString, QVector<QgsPostgresLayerProperty> > schemasMap;
35+
foreach( QgsPostgresLayerProperty layerProperty, mLayerProperties )
36+
{
37+
schemasMap[ layerProperty.schemaName ].push_back( layerProperty );
38+
}
39+
40+
QMap<QString, QVector<QgsPostgresLayerProperty> >::const_iterator it = schemasMap.constBegin();
41+
for ( ; it != schemasMap.constEnd(); it++ )
42+
{
43+
QgsDebugMsg( "schema: " + it.key() );
44+
QgsPGSchemaItem * schema = new QgsPGSchemaItem( this, it.key(), mPath + "/" + it.key(), mConnInfo, it.value() );
45+
46+
children.append( schema );
47+
}
48+
return children;
49+
}
50+
51+
bool QgsPGConnectionItem::equal( const QgsDataItem *other )
52+
{
53+
if ( type() != other->type() )
54+
{
55+
return false;
56+
}
57+
const QgsPGConnectionItem *o = dynamic_cast<const QgsPGConnectionItem *>( other );
58+
return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo );
59+
}
60+
61+
QList<QAction*> QgsPGConnectionItem::actions()
62+
{
63+
QList<QAction*> lst;
64+
65+
QAction* actionEdit = new QAction( tr( "Edit..." ), this );
66+
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
67+
lst.append( actionEdit );
68+
69+
QAction* actionDelete = new QAction( tr( "Delete" ), this );
70+
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
71+
lst.append( actionDelete );
72+
73+
return lst;
74+
}
75+
76+
void QgsPGConnectionItem::editConnection()
77+
{
78+
QgsPgNewConnection nc( NULL, mName );
79+
if ( nc.exec() )
80+
{
81+
// the parent should be updated
82+
mParent->refresh();
83+
}
84+
}
85+
86+
void QgsPGConnectionItem::deleteConnection()
87+
{
88+
QgsPgSourceSelect::deleteConnection( mName );
89+
// the parent should be updated
90+
mParent->refresh();
91+
}
92+
93+
94+
// ---------------------------------------------------------------------------
95+
QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty )
96+
: QgsLayerItem( parent, name, path, QString(), layerType, "postgres" ),
97+
mConnInfo( connInfo ),
98+
mLayerProperty( layerProperty )
99+
{
100+
mUri = createUri();
101+
mPopulated = true;
102+
}
103+
104+
QgsPGLayerItem::~QgsPGLayerItem()
105+
{
106+
}
107+
108+
QString QgsPGLayerItem::createUri()
109+
{
110+
QString pkColName = mLayerProperty.pkCols.size() > 0 ? mLayerProperty.pkCols.at( 0 ) : QString::null;
111+
QgsDataSourceURI uri( mConnInfo );
112+
uri.setDataSource( mLayerProperty.schemaName, mLayerProperty.tableName, mLayerProperty.geometryColName, mLayerProperty.sql, pkColName );
113+
return uri.uri();
114+
}
115+
116+
// ---------------------------------------------------------------------------
117+
QgsPGSchemaItem::QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties )
118+
: QgsDataCollectionItem( parent, name, path )
119+
{
120+
mIcon = QIcon( getThemePixmap( "mIconNamespace.png" ) );
121+
122+
// Populate everything, it costs nothing, all info about layers is collected
123+
foreach( QgsPostgresLayerProperty layerProperty, layerProperties )
124+
{
125+
QgsDebugMsg( "table: " + layerProperty.schemaName + "." + layerProperty.tableName );
126+
127+
QgsLayerItem::LayerType layerType = QgsLayerItem::NoType;
128+
if ( layerProperty.type.contains( "POINT" ) )
129+
{
130+
layerType = QgsLayerItem::Point;
131+
}
132+
else if ( layerProperty.type.contains( "LINE" ) )
133+
{
134+
layerType = QgsLayerItem::Line;
135+
}
136+
else if ( layerProperty.type.contains( "POLYGON" ) )
137+
{
138+
layerType = QgsLayerItem::Polygon;
139+
}
140+
else if ( layerProperty.type == QString::null )
141+
{
142+
if ( layerProperty.geometryColName == QString::null )
143+
{
144+
layerType = QgsLayerItem::TableLayer;
145+
}
146+
}
147+
148+
QgsPGLayerItem * layer = new QgsPGLayerItem( this, layerProperty.tableName, mPath + "/" + layerProperty.tableName, connInfo, layerType, layerProperty );
149+
mChildren.append( layer );
150+
}
151+
152+
mPopulated = true;
153+
}
154+
155+
QgsPGSchemaItem::~QgsPGSchemaItem()
156+
{
157+
}
158+
159+
// ---------------------------------------------------------------------------
160+
QgsPGRootItem::QgsPGRootItem( QgsDataItem* parent, QString name, QString path )
161+
: QgsDataCollectionItem( parent, name, path )
162+
{
163+
//mIcon = QIcon( getThemePixmap( "mIconPg.png" ) );
164+
populate();
165+
}
166+
167+
QgsPGRootItem::~QgsPGRootItem()
168+
{
169+
}
170+
171+
QVector<QgsDataItem*>QgsPGRootItem::createChildren()
172+
{
173+
QVector<QgsDataItem*> connections;
174+
foreach( QString connName, QgsPostgresConnection::connectionList() )
175+
{
176+
QgsDataItem * conn = new QgsPGConnectionItem( this, connName, mPath + "/" + connName );
177+
connections.push_back( conn );
178+
}
179+
return connections;
180+
}
181+
182+
QList<QAction*> QgsPGRootItem::actions()
183+
{
184+
QList<QAction*> lst;
185+
186+
QAction* actionNew = new QAction( tr( "New..." ), this );
187+
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
188+
lst.append( actionNew );
189+
190+
return lst;
191+
}
192+
193+
QWidget * QgsPGRootItem::paramWidget()
194+
{
195+
QgsPgSourceSelect *select = new QgsPgSourceSelect( 0, 0, true, true );
196+
connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) );
197+
return select;
198+
}
199+
void QgsPGRootItem::connectionsChanged()
200+
{
201+
refresh();
202+
}
203+
204+
void QgsPGRootItem::newConnection()
205+
{
206+
QgsPgNewConnection nc( NULL );
207+
if ( nc.exec() )
208+
{
209+
refresh();
210+
}
211+
}
212+
213+
// ---------------------------------------------------------------------------
214+
215+
QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
216+
{
217+
Q_UNUSED( thePath );
218+
return new QgsPGRootItem( parentItem, "PostGIS", "pg:" );
219+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef QGSPOSTGRESDATAITEMS_H
2+
#define QGSPOSTGRESDATAITEMS_H
3+
4+
#include "qgsdataitem.h"
5+
6+
#include "qgspostgresprovider.h"
7+
8+
class QgsPGConnectionItem : public QgsDataCollectionItem
9+
{
10+
Q_OBJECT
11+
public:
12+
QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path );
13+
~QgsPGConnectionItem();
14+
15+
QVector<QgsDataItem*> createChildren();
16+
virtual bool equal( const QgsDataItem *other );
17+
18+
virtual QList<QAction*> actions();
19+
20+
QString mConnInfo;
21+
QVector<QgsPostgresLayerProperty> mLayerProperties;
22+
23+
public slots:
24+
void editConnection();
25+
void deleteConnection();
26+
};
27+
28+
// WMS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem
29+
// We have to use QgsDataCollectionItem and support layer methods if necessary
30+
class QgsPGLayerItem : public QgsLayerItem
31+
{
32+
Q_OBJECT
33+
public:
34+
QgsPGLayerItem( QgsDataItem* parent, QString name, QString path,
35+
QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperties );
36+
~QgsPGLayerItem();
37+
38+
QString createUri();
39+
40+
QString mConnInfo;
41+
QgsPostgresLayerProperty mLayerProperty;
42+
};
43+
44+
class QgsPGSchemaItem : public QgsDataCollectionItem
45+
{
46+
Q_OBJECT
47+
public:
48+
QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path,
49+
QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties );
50+
~QgsPGSchemaItem();
51+
};
52+
53+
class QgsPGRootItem : public QgsDataCollectionItem
54+
{
55+
Q_OBJECT
56+
public:
57+
QgsPGRootItem( QgsDataItem* parent, QString name, QString path );
58+
~QgsPGRootItem();
59+
60+
QVector<QgsDataItem*> createChildren();
61+
62+
virtual QWidget * paramWidget();
63+
64+
virtual QList<QAction*> actions();
65+
66+
public slots:
67+
void connectionsChanged();
68+
void newConnection();
69+
};
70+
71+
72+
#endif // QGSPOSTGRESDATAITEMS_H

0 commit comments

Comments
 (0)