|
| 1 | +#include "qgswmsdataitems.h" |
| 2 | + |
| 3 | +#include "qgslogger.h" |
| 4 | + |
| 5 | +#include "qgswmsconnection.h" |
| 6 | +#include "qgswmssourceselect.h" |
| 7 | + |
| 8 | +#include "qgsnewhttpconnection.h" |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem* parent, QString name, QString path ) |
| 12 | + : QgsDataCollectionItem( parent, name, path ) |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +QgsWMSConnectionItem::~QgsWMSConnectionItem() |
| 17 | +{ |
| 18 | +} |
| 19 | + |
| 20 | +QVector<QgsDataItem*> QgsWMSConnectionItem::createChildren() |
| 21 | +{ |
| 22 | + QgsDebugMsg( "Entered" ); |
| 23 | + QVector<QgsDataItem*> children; |
| 24 | + QgsWMSConnection connection( mName ); |
| 25 | + QgsWmsProvider *wmsProvider = connection.provider( ); |
| 26 | + if ( !wmsProvider ) |
| 27 | + return children; |
| 28 | + |
| 29 | + QString mConnInfo = connection.connectionInfo(); |
| 30 | + QgsDebugMsg( "mConnInfo = " + mConnInfo ); |
| 31 | + |
| 32 | + // Attention: supportedLayers() gives tree leafes, not top level |
| 33 | + if ( !wmsProvider->supportedLayers( mLayerProperties ) ) |
| 34 | + { |
| 35 | + children.append( new QgsErrorItem( this, tr( "Failed to retrieve layers" ), mPath + "/error" ) ); |
| 36 | + return children; |
| 37 | + } |
| 38 | + |
| 39 | + QgsWmsCapabilitiesProperty mCapabilitiesProperty = wmsProvider->capabilitiesProperty(); |
| 40 | + QgsWmsCapabilityProperty capabilityProperty = mCapabilitiesProperty.capability; |
| 41 | + |
| 42 | + // Top level layer is present max once |
| 43 | + // <element name="Capability"> |
| 44 | + // <element ref="wms:Layer" minOccurs="0"/> - default maxOccurs=1 |
| 45 | + QgsWmsLayerProperty topLayerProperty = capabilityProperty.layer; |
| 46 | + foreach( QgsWmsLayerProperty layerProperty, topLayerProperty.layer ) |
| 47 | + { |
| 48 | + // Attention, the name may be empty |
| 49 | + QgsDebugMsg( QString::number( layerProperty.orderId ) + " " + layerProperty.name + " " + layerProperty.title ); |
| 50 | + QString pathName = layerProperty.name.isEmpty() ? QString::number( layerProperty.orderId ) : layerProperty.name; |
| 51 | + |
| 52 | + QgsWMSLayerItem * layer = new QgsWMSLayerItem( this, layerProperty.title, mPath + "/" + pathName, mCapabilitiesProperty, mConnInfo, layerProperty ); |
| 53 | + |
| 54 | + children.append( layer ); |
| 55 | + } |
| 56 | + return children; |
| 57 | +} |
| 58 | + |
| 59 | +bool QgsWMSConnectionItem::equal( const QgsDataItem *other ) |
| 60 | +{ |
| 61 | + if ( type() != other->type() ) |
| 62 | + { |
| 63 | + return false; |
| 64 | + } |
| 65 | + const QgsWMSConnectionItem *o = dynamic_cast<const QgsWMSConnectionItem *>( other ); |
| 66 | + return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo ); |
| 67 | +} |
| 68 | + |
| 69 | +QList<QAction*> QgsWMSConnectionItem::actions() |
| 70 | +{ |
| 71 | + QList<QAction*> lst; |
| 72 | + |
| 73 | + QAction* actionEdit = new QAction( tr( "Edit..." ), this ); |
| 74 | + connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) ); |
| 75 | + lst.append( actionEdit ); |
| 76 | + |
| 77 | + QAction* actionDelete = new QAction( tr( "Delete" ), this ); |
| 78 | + connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) ); |
| 79 | + lst.append( actionDelete ); |
| 80 | + |
| 81 | + return lst; |
| 82 | +} |
| 83 | + |
| 84 | +void QgsWMSConnectionItem::editConnection() |
| 85 | +{ |
| 86 | + QgsNewHttpConnection nc( 0, "/Qgis/connections-wms/", mName ); |
| 87 | + |
| 88 | + if ( nc.exec() ) |
| 89 | + { |
| 90 | + // the parent should be updated |
| 91 | + mParent->refresh(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void QgsWMSConnectionItem::deleteConnection() |
| 96 | +{ |
| 97 | + QgsWMSConnection::deleteConnection( mName ); |
| 98 | + // the parent should be updated |
| 99 | + mParent->refresh(); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +// --------------------------------------------------------------------------- |
| 104 | + |
| 105 | +QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem* parent, QString name, QString path, QgsWmsCapabilitiesProperty capabilitiesProperty, QString connInfo, QgsWmsLayerProperty layerProperty ) |
| 106 | + : QgsLayerItem( parent, name, path, QString(), QgsLayerItem::Raster, "wms" ), |
| 107 | + mCapabilitiesProperty( capabilitiesProperty ), |
| 108 | + mConnInfo( connInfo ), |
| 109 | + mLayerProperty( layerProperty ) |
| 110 | + //mProviderKey ("wms"), |
| 111 | + //mLayerType ( QgsLayerItem::Raster ) |
| 112 | +{ |
| 113 | + mUri = createUri(); |
| 114 | + // Populate everything, it costs nothing, all info about layers is collected |
| 115 | + foreach( QgsWmsLayerProperty layerProperty, mLayerProperty.layer ) |
| 116 | + { |
| 117 | + // Attention, the name may be empty |
| 118 | + QgsDebugMsg( QString::number( layerProperty.orderId ) + " " + layerProperty.name + " " + layerProperty.title ); |
| 119 | + QString pathName = layerProperty.name.isEmpty() ? QString::number( layerProperty.orderId ) : layerProperty.name; |
| 120 | + QgsWMSLayerItem * layer = new QgsWMSLayerItem( this, layerProperty.title, mPath + "/" + pathName, mCapabilitiesProperty, mConnInfo, layerProperty ); |
| 121 | + mChildren.append( layer ); |
| 122 | + } |
| 123 | + |
| 124 | + if ( mChildren.size() == 0 ) |
| 125 | + { |
| 126 | + mIcon = QIcon( getThemePixmap( "mIconWmsLayer.png" ) ); |
| 127 | + } |
| 128 | + mPopulated = true; |
| 129 | +} |
| 130 | + |
| 131 | +QgsWMSLayerItem::~QgsWMSLayerItem() |
| 132 | +{ |
| 133 | +} |
| 134 | + |
| 135 | +QString QgsWMSLayerItem::createUri() |
| 136 | +{ |
| 137 | + QString uri; |
| 138 | + if ( mLayerProperty.name.isEmpty() ) |
| 139 | + return uri; // layer collection |
| 140 | + |
| 141 | + QString rasterLayerPath = mConnInfo; |
| 142 | + QString baseName = mLayerProperty.name; |
| 143 | + |
| 144 | + // Number of styles must match number of layers |
| 145 | + QStringList layers; |
| 146 | + layers << mLayerProperty.name; |
| 147 | + QStringList styles; |
| 148 | + if ( mLayerProperty.style.size() > 0 ) |
| 149 | + { |
| 150 | + styles.append( mLayerProperty.style[0].name ); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + styles << ""; // TODO: use loadDefaultStyleFlag |
| 155 | + } |
| 156 | + |
| 157 | + QString format; |
| 158 | + // get first supporte by qt and server |
| 159 | + QVector<QgsWmsSupportedFormat> formats = QgsWmsProvider::supportedFormats(); |
| 160 | + foreach( QgsWmsSupportedFormat f, formats ) |
| 161 | + { |
| 162 | + if ( mCapabilitiesProperty.capability.request.getMap.format.indexOf( f.format ) >= 0 ) |
| 163 | + { |
| 164 | + format = f.format; |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + QString crs; |
| 169 | + // get first known if possible |
| 170 | + QgsCoordinateReferenceSystem testCrs; |
| 171 | + foreach( QString c, mLayerProperty.crs ) |
| 172 | + { |
| 173 | + testCrs.createFromOgcWmsCrs( c ); |
| 174 | + if ( testCrs.isValid() ) |
| 175 | + { |
| 176 | + crs = c; |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + if ( crs.isEmpty() && mLayerProperty.crs.size() > 0 ) |
| 181 | + { |
| 182 | + crs = mLayerProperty.crs[0]; |
| 183 | + } |
| 184 | + uri = rasterLayerPath + "|layers=" + layers.join( "," ) + "|styles=" + styles.join( "," ) + "|format=" + format + "|crs=" + crs; |
| 185 | + |
| 186 | + return uri; |
| 187 | +} |
| 188 | + |
| 189 | +// --------------------------------------------------------------------------- |
| 190 | +QgsWMSRootItem::QgsWMSRootItem( QgsDataItem* parent, QString name, QString path ) |
| 191 | + : QgsDataCollectionItem( parent, name, path ) |
| 192 | +{ |
| 193 | + mIcon = QIcon( getThemePixmap( "mIconWms.png" ) ); |
| 194 | + |
| 195 | + populate(); |
| 196 | +} |
| 197 | + |
| 198 | +QgsWMSRootItem::~QgsWMSRootItem() |
| 199 | +{ |
| 200 | +} |
| 201 | + |
| 202 | +QVector<QgsDataItem*>QgsWMSRootItem::createChildren() |
| 203 | +{ |
| 204 | + QVector<QgsDataItem*> connections; |
| 205 | + |
| 206 | + foreach( QString connName, QgsWMSConnection::connectionList() ) |
| 207 | + { |
| 208 | + QgsDataItem * conn = new QgsWMSConnectionItem( this, connName, mPath + "/" + connName ); |
| 209 | + connections.append( conn ); |
| 210 | + } |
| 211 | + return connections; |
| 212 | +} |
| 213 | + |
| 214 | +QList<QAction*> QgsWMSRootItem::actions() |
| 215 | +{ |
| 216 | + QList<QAction*> lst; |
| 217 | + |
| 218 | + QAction* actionNew = new QAction( tr( "New..." ), this ); |
| 219 | + connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) ); |
| 220 | + lst.append( actionNew ); |
| 221 | + |
| 222 | + return lst; |
| 223 | +} |
| 224 | + |
| 225 | + |
| 226 | +QWidget * QgsWMSRootItem::paramWidget() |
| 227 | +{ |
| 228 | + QgsWMSSourceSelect *select = new QgsWMSSourceSelect( 0, 0, true, true ); |
| 229 | + connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) ); |
| 230 | + return select; |
| 231 | +} |
| 232 | +void QgsWMSRootItem::connectionsChanged() |
| 233 | +{ |
| 234 | + refresh(); |
| 235 | +} |
| 236 | + |
| 237 | +void QgsWMSRootItem::newConnection() |
| 238 | +{ |
| 239 | + QgsNewHttpConnection nc( 0 ); |
| 240 | + |
| 241 | + if ( nc.exec() ) |
| 242 | + { |
| 243 | + refresh(); |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | + |
| 248 | +// --------------------------------------------------------------------------- |
| 249 | + |
| 250 | +QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem ) |
| 251 | +{ |
| 252 | + Q_UNUSED( thePath ); |
| 253 | + |
| 254 | + return new QgsWMSRootItem( parentItem, "WMS", "wms:" ); |
| 255 | +} |
| 256 | + |
0 commit comments