Esempio n. 1
0
  foreach ( QString key, providersList )
  {
    QLibrary *library = QgsProviderRegistry::instance()->providerLibrary( key );
    if ( !library )
      continue;

    dataCapabilities_t * dataCapabilities = ( dataCapabilities_t * ) cast_to_fptr( library->resolve( "dataCapabilities" ) );
    if ( !dataCapabilities )
    {
      QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
      continue;
    }

    int capabilities = dataCapabilities();
    if ( capabilities == QgsDataProvider::NoDataCapabilities )
    {
      QgsDebugMsg( library->fileName() + " does not have any dataCapabilities" );
      continue;
    }

    dataItem_t *dataItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
    if ( !dataItem )
    {
      QgsDebugMsg( library->fileName() + " does not have dataItem" );
      continue;
    }

    QgsDataItem *item = dataItem( "", NULL );  // empty path -> top level
    if ( item )
    {
      QgsDebugMsg( "Add new top level item : " + item->name() );
      connectItem( item );
      providerMap.insertMulti( capabilities, item );
    }
  }
Esempio n. 2
0
void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
{
  QModelIndex idx = mBrowserView->indexAt( pt );
  QgsDataItem* item = mModel->dataItem( idx );
  if ( !item )
    return;

  QMenu* menu = new QMenu( this );

  if ( item->type() == QgsDataItem::Directory )
  {
    QSettings settings;
    QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
    bool inFavDirs = favDirs.contains( item->path() );

    if ( item->parent() != NULL && !inFavDirs )
    {
      // only non-root directories can be added as favourites
      menu->addAction( tr( "Add as a favourite" ), this, SLOT( addFavourite() ) );
    }
    else if ( inFavDirs )
    {
      // only favourites can be removed
      menu->addAction( tr( "Remove favourite" ), this, SLOT( removeFavourite() ) );
    }
  }

  if ( menu->actions().count() == 0 )
  {
    delete menu;
    return;
  }

  menu->popup( mBrowserView->mapToGlobal( pt ) );
}
void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
{
  QgsDebugMsg( "Entered" );
  if ( index.isValid() )
  {
    QgsDataItem *item = mModel->dataItem( index );
    if ( item )
    {
      QgsDebugMsg( "path = " + item->path() );
    }
    else
    {
      QgsDebugMsg( "invalid item" );
    }
  }

  mModel->refresh( index );

  for ( int i = 0 ; i < mModel->rowCount( index ); i++ )
  {
    QModelIndex idx = mModel->index( i, 0, index );
    if ( mBrowserView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
    {
      refreshModel( idx );
    }
  }
}
void QgsBrowserDockWidget::showEvent( QShowEvent * e )
{
  // delayed initialization of the model
  if ( mModel == NULL )
  {
    mModel = new QgsBrowserModel( mBrowserView );
    mBrowserView->setModel( mModel );

    // provide a horizontal scroll bar instead of using ellipse (...) for longer items
    mBrowserView->setTextElideMode( Qt::ElideNone );
    mBrowserView->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
    mBrowserView->header()->setStretchLastSection( false );

    // find root favourites item
    for ( int i = 0; i < mModel->rowCount(); i++ )
    {
      QModelIndex index = mModel->index( i, 0 );
      QgsDataItem* item = mModel->dataItem( index );
      if ( item && item->type() == QgsDataItem::Favourites )
        mBrowserView->expand( index );
    }
  }

  QDockWidget::showEvent( e );
}
Esempio n. 5
0
void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index )
{
  QgsDebugMsg( QString( "rowCount() = %1" ).arg( mModel->rowCount( mProxyModel->mapToSource( index ) ) ) );
  QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );

  if ( item && item->type() == QgsDataItem::Project )
  {
    QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item );
    if ( projectItem )
    {
      QApplication::setOverrideCursor( Qt::WaitCursor );
      QgisApp::instance()->openFile( projectItem->path() );
      QApplication::restoreOverrideCursor();
    }
  }
  if ( item && item->type() == QgsDataItem::Layer )
  {
    QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item );
    if ( layerItem )
    {
      QApplication::setOverrideCursor( Qt::WaitCursor );
      addLayer( layerItem );
      QApplication::restoreOverrideCursor();
    }
  }
}
Esempio n. 6
0
void QgsBrowserDockWidget::showContextMenu( QPoint pt )
{
  QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
  QgsDataItem *item = mModel->dataItem( index );
  if ( !item )
    return;

  QMenu *menu = new QMenu( this );

  if ( item->type() == QgsDataItem::Directory )
  {
    QgsSettings settings;
    QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
    bool inFavDirs = item->parent() && item->parent()->type() == QgsDataItem::Favorites;

    if ( item->parent() && !inFavDirs )
    {
      // only non-root directories can be added as favorites
      menu->addAction( tr( "Add as a Favorite" ), this, SLOT( addFavorite() ) );
    }
    else if ( inFavDirs )
    {
      // only favorites can be removed
      menu->addAction( tr( "Remove Favorite" ), this, SLOT( removeFavorite() ) );
    }
    menu->addAction( tr( "Properties..." ), this, SLOT( showProperties() ) );
    menu->addAction( tr( "Hide from Browser" ), this, SLOT( hideItem() ) );
    QAction *action = menu->addAction( tr( "Fast Scan this Directory" ), this, SLOT( toggleFastScan() ) );
    action->setCheckable( true );
    action->setChecked( settings.value( QStringLiteral( "qgis/scanItemsFastScanUris" ),
                                        QStringList() ).toStringList().contains( item->path() ) );
  }
  else if ( item->type() == QgsDataItem::Layer )
  {
    menu->addAction( tr( "Add Selected Layer(s)" ), this, SLOT( addSelectedLayers() ) );
    menu->addAction( tr( "Properties..." ), this, SLOT( showProperties() ) );
  }
  else if ( item->type() == QgsDataItem::Favorites )
  {
    menu->addAction( tr( "Add a Directory..." ), this, SLOT( addFavoriteDirectory() ) );
  }

  QList<QAction *> actions = item->actions();
  if ( !actions.isEmpty() )
  {
    if ( !menu->actions().isEmpty() )
      menu->addSeparator();
    // add action to the menu
    menu->addActions( actions );
  }

  if ( menu->actions().isEmpty() )
  {
    delete menu;
    return;
  }

  menu->popup( mBrowserView->mapToGlobal( pt ) );
}
void QgsBrowserDockWidget::addFavourite()
{
  QgsDataItem* item = mModel->dataItem( mBrowserView->currentIndex() );
  if ( !item )
    return;

  if ( item->type() != QgsDataItem::Directory )
    return;

  addFavouriteDirectory( item->path() );
}
Esempio n. 8
0
void SWGISBrowser::itemDoubleClicked(const QModelIndex &index)
{
    QgsDataItem *item = this->m_Model->dataItem(index);
    if(!item)
    {
        int a = 10;
        return;
    }

    QgsDebugMsg(QString("%1 %2 %3").arg(index.row()).arg(index.column()).arg(item->name()));
}
Esempio n. 9
0
void SWGISBrowser::tabChanged()
{
    this->updateCurrentTab();
    // Store last selected tab for selected data item
    if (this->m_Index.isValid())
    {
        QgsDataItem *item = this->m_Model->dataItem(this->m_Index);
        if (!item)
            return;

        QgsDebugMsg( QString( "save last tab %1 : %2" ).arg(item->metaObject()->className()).arg(ui->tabWidget->currentIndex()));
        this->m_LastTab[item->metaObject()->className()] = ui->tabWidget->currentIndex();
    }
}
void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex& index )
{
  QgsDataItem *dataItem = mModel->dataItem( index );

  if ( dataItem != NULL && dataItem->type() == QgsDataItem::Layer )
  {
    QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( dataItem );
    if ( layerItem != NULL )
    {
      QApplication::setOverrideCursor( Qt::WaitCursor );
      addLayer( layerItem );
      QApplication::restoreOverrideCursor();
    }
  }
}
Esempio n. 11
0
  Q_FOREACH ( QgsDataItemProvider* pr, QgsDataItemProviderRegistry::instance()->providers() )
  {
    int capabilities = pr->capabilities();
    if ( capabilities == QgsDataProvider::NoDataCapabilities )
    {
      QgsDebugMsg( pr->name() + " does not have any dataCapabilities" );
      continue;
    }

    QgsDataItem *item = pr->createDataItem( "", nullptr );  // empty path -> top level
    if ( item )
    {
      QgsDebugMsg( "Add new top level item : " + item->name() );
      connectItem( item );
      providerMap.insertMulti( capabilities, item );
    }
  }
void QgsBrowserDockWidget::addFavourite()
{
  QgsDataItem* item = mModel->dataItem( mBrowserView->currentIndex() );
  if ( !item )
    return;
  if ( item->type() != QgsDataItem::Directory )
    return;

  QString newFavDir = item->path();

  QSettings settings;
  QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
  favDirs.append( newFavDir );
  settings.setValue( "/browser/favourites", favDirs );

  // reload the browser model so that the newly added favourite directory is shown
  mModel->reload();
}
void QgsBrowserDockWidget::removeFavourite()
{
  QgsDataItem* item = mModel->dataItem( mBrowserView->currentIndex() );
  if ( !item )
    return;
  if ( item->type() != QgsDataItem::Directory )
    return;

  QString favDir  = item->path();

  QSettings settings;
  QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
  favDirs.removeAll( favDir );
  settings.setValue( "/browser/favourites", favDirs );

  // reload the browser model so that the favourite directory is not shown anymore
  mModel->reload();
}
Esempio n. 14
0
Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const
{
  if ( !index.isValid() )
    return Qt::ItemFlags();

  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

  QgsDataItem *ptr = reinterpret_cast< QgsDataItem * >( index.internalPointer() );
  if ( ptr->hasDragEnabled() )
    flags |= Qt::ItemIsDragEnabled;

  if ( ptr->acceptDrop() )
    flags |= Qt::ItemIsDropEnabled;

  if ( ptr->capabilities2() & QgsDataItem::Rename )
    flags |= Qt::ItemIsEditable;

  return flags;
}
Esempio n. 15
0
  Q_FOREACH ( QgsDataItemProvider *pr, QgsApplication::dataItemProviderRegistry()->providers() )
  {
    int capabilities = pr->capabilities();
    if ( capabilities == QgsDataProvider::NoDataCapabilities )
    {
      QgsDebugMsgLevel( pr->name() + " does not have any dataCapabilities", 4 );
      continue;
    }

    QgsDataItem *item = pr->createDataItem( QString(), nullptr );  // empty path -> top level
    if ( item )
    {
      // Forward the signal from the root items to the model (and then to the app)
      connect( item, &QgsDataItem::connectionsChanged, this, &QgsBrowserModel::connectionsChanged );
      QgsDebugMsgLevel( "Add new top level item : " + item->name(), 4 );
      connectItem( item );
      providerMap.insertMulti( capabilities, item );
    }
  }
void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
{
  QModelIndex idx = mBrowserView->indexAt( pt );
  QgsDataItem* item = mModel->dataItem( idx );
  if ( !item )
    return;

  QMenu *menu = new QMenu( this );

  if ( item->type() == QgsDataItem::Directory )
  {
    QSettings settings;
    QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
    bool inFavDirs = favDirs.contains( item->path() );

    if ( item->parent() != NULL && !inFavDirs )
    {
      // only non-root directories can be added as favourites
      menu->addAction( tr( "Add as a favourite" ), this, SLOT( addFavourite() ) );
    }
    else if ( inFavDirs )
    {
      // only favourites can be removed
      menu->addAction( tr( "Remove favourite" ), this, SLOT( removeFavourite() ) );
    }

  }
  else if ( item->type() == QgsDataItem::Layer )
  {
    menu->addAction( tr( "Add Layer" ), this, SLOT( addCurrentLayer( ) ) );
    menu->addAction( tr( "Add Selected Layers" ), this, SLOT( addSelectedLayers() ) );
    menu->addAction( tr( "Properties" ), this, SLOT( showProperties( ) ) );

  }
  else if ( item->type() == QgsDataItem::Favourites )
  {
    menu->addAction( tr( "Add a directory" ), this, SLOT( addFavouriteDirectory() ) );

  }

  QList<QAction*> actions = item->actions();
  if ( !actions.isEmpty() )
  {
    if ( !menu->actions().isEmpty() )
      menu->addSeparator();
    // add action to the menu
    menu->addActions( actions );
  }

  if ( menu->actions().count() == 0 )
  {
    delete menu;
    return;
  }

  menu->popup( mBrowserView->mapToGlobal( pt ) );
}
Esempio n. 17
0
void SWGISBrowser::refresh(const QModelIndex &index)
{
    //这个是做什么用的?
    if(index.isValid())
    {
        QgsDataItem *item = m_Model->dataItem(index);
        if(item)
            qDebug() << "path = " + item->path();
        else
            qDebug() << "invalid item";
    }

    this->m_Model->refresh(index);

    for(int i = 0; i < this->m_Model->rowCount(index); i++)
    {
        QModelIndex idx = this->m_Model->index(i, 0, index);
        if(ui->treeView->isExpanded(idx) || !this->m_Model->hasChildren(idx))
            this->refresh(idx);
    }
}
void QgsBrowserDockWidget::addSelectedLayers()
{
  QApplication::setOverrideCursor( Qt::WaitCursor );

  // get a sorted list of selected indexes
  QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
  qSort( list );

  // add items in reverse order so they are in correct order in the layers dock
  for ( int i = list.size() - 1; i >= 0; i-- )
  {
    QModelIndex index = list[i];
    QgsDataItem *dataItem = mModel->dataItem( index );
    if ( dataItem && dataItem->type() == QgsDataItem::Layer )
    {
      QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( dataItem );
      if ( layerItem )
        addLayer( layerItem );
    }
  }

  QApplication::restoreOverrideCursor();
}
Esempio n. 19
0
bool QgsBrowserModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
  if ( !index.isValid() )
    return false;


  QgsDataItem *item = dataItem( index );
  if ( !item )
  {
    return false;
  }

  if ( !( item->capabilities2() & QgsDataItem::Rename ) )
    return false;

  switch ( role )
  {
    case Qt::EditRole:
    {
      return item->rename( value.toString() );
    }
  }
  return false;
}
Esempio n. 20
0
QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
{
  if ( !index.isValid() )
    return QVariant();

  QgsDataItem *item = dataItem( index );
  if ( !item )
  {
    return QVariant();
  }
  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
  {
    return item->name();
  }
  else if ( role == QgsBrowserModel::SortRole )
  {
    return item->sortKey();
  }
  else if ( role == Qt::ToolTipRole )
  {
    return item->toolTip();
  }
  else if ( role == Qt::DecorationRole && index.column() == 0 )
  {
    return item->icon();
  }
  else if ( role == QgsBrowserModel::PathRole )
  {
    return item->path();
  }
  else if ( role == QgsBrowserModel::CommentRole )
  {
    if ( item->type() == QgsDataItem::Layer )
    {
      QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
      return lyrItem->comments();
    }
    return QVariant();
  }
  else
  {
    // unsupported role
    return QVariant();
  }
}
Esempio n. 21
0
void QgsBrowserDockWidget::refreshModel( const QModelIndex &index )
{
  QgsDataItem *item = mModel->dataItem( index );
  if ( item )
  {
    QgsDebugMsg( "path = " + item->path() );
  }
  else
  {
    QgsDebugMsg( "invalid item" );
  }

  if ( item && ( item->capabilities2() & QgsDataItem::Fertile ) )
  {
    mModel->refresh( index );
  }

  for ( int i = 0 ; i < mModel->rowCount( index ); i++ )
  {
    QModelIndex idx = mModel->index( i, 0, index );
    QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
    QgsDataItem *child = mModel->dataItem( idx );

    // Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
    // Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
    if ( mBrowserView->isExpanded( proxyIdx ) || mBrowserView->hasExpandedDescendant( proxyIdx ) || ( child && child->capabilities2() & QgsDataItem::Fast ) )
    {
      refreshModel( idx );
    }
    else
    {
      if ( child && ( child->capabilities2() & QgsDataItem::Fertile ) )
      {
        child->depopulate();
      }
    }
  }
}
Esempio n. 22
0
void QgsBrowserModel::addRootItems()
{
  updateProjectHome();

  // give the home directory a prominent third place
  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, tr( "Home" ), QDir::homePath(), QStringLiteral( HOME_PREFIX ) + QDir::homePath() );
  item->setSortKey( QStringLiteral( " 2" ) );
  setupItemConnections( item );
  mRootItems << item;

  // add favorite directories
  mFavorites = new QgsFavoritesItem( nullptr, tr( "Favorites" ) );
  if ( mFavorites )
  {
    setupItemConnections( mFavorites );
    mRootItems << mFavorites;
  }

  // add drives
  const auto drives { QDir::drives() };
  for ( const QFileInfo &drive : drives )
  {
    const QString path = drive.absolutePath();

    if ( QgsDirectoryItem::hiddenPath( path ) )
      continue;

    QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
    item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
    mDriveItems.insert( path, item );

    setupItemConnections( item );
    mRootItems << item;
  }

#ifdef Q_OS_MAC
  QString path = QString( "/Volumes" );
  QgsDirectoryItem *vols = new QgsDirectoryItem( nullptr, path, path );
  mRootItems << vols;
#endif

  // container for displaying providers as sorted groups (by QgsDataProvider::DataCapability enum)
  QMap<int, QgsDataItem *> providerMap;

  const auto constProviders = QgsApplication::dataItemProviderRegistry()->providers();
  for ( QgsDataItemProvider *pr : constProviders )
  {
    int capabilities = pr->capabilities();
    if ( capabilities == QgsDataProvider::NoDataCapabilities )
    {
      QgsDebugMsgLevel( pr->name() + " does not have any dataCapabilities", 4 );
      continue;
    }

    QgsDataItem *item = pr->createDataItem( QString(), nullptr );  // empty path -> top level
    if ( item )
    {
      // Forward the signal from the root items to the model (and then to the app)
      connect( item, &QgsDataItem::connectionsChanged, this, &QgsBrowserModel::connectionsChanged );
      QgsDebugMsgLevel( "Add new top level item : " + item->name(), 4 );
      setupItemConnections( item );
      providerMap.insertMulti( capabilities, item );
    }
  }

  // add as sorted groups by QgsDataProvider::DataCapability enum
  const auto constUniqueKeys = providerMap.uniqueKeys();
  for ( int key : constUniqueKeys )
  {
    QList<QgsDataItem *> providerGroup = providerMap.values( key );
    if ( providerGroup.size() > 1 )
    {
      std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
    }

    const auto constProviderGroup = providerGroup;
    for ( QgsDataItem *ditem : constProviderGroup )
    {
      mRootItems << ditem;
    }
  }
}
Esempio n. 23
0
void SWGISBrowser::itemClicked(const QModelIndex &index)
{
    this->m_Index = index;
    QgsDataItem *item = this->m_Model->dataItem(index);
    if(!item)
        return;

    bool paramEnable = false;
    bool metaEnable = false;
    bool previewEnable = false;
    bool attributesEnable = false;

    this->m_DirtyAttributes = true;
    this->m_DirtyMetadata = true;
    this->m_DirtyPreview = true;

    this->setLayer(nullptr);

    QList<QgsMapCanvasLayer> noLayers;
    ui->mapCanvas->setLayerSet(noLayers);
    ui->metaTextBrowser->clear();
    if(this->m_ParamWidget)
    {
        ui->paramLayout->removeWidget(this->m_ParamWidget);
        this->m_ParamWidget->hide();
        delete this->m_ParamWidget;
        this->m_ParamWidget = nullptr;
    }

    QgsMapLayerRegistry::instance()->removeAllMapLayers();
    this->m_Layer = nullptr;

    this->m_ParamWidget = item->paramWidget();
    if(this->m_ParamWidget)
    {
        ui->paramLayout->addWidget(this->m_ParamWidget);
        this->m_ParamWidget->show();
        paramEnable = true;
    }

    QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*> (this->m_Model->dataItem(index));
    if(layerItem)
    {
        bool res = this->layerClicked(layerItem);
        if(res)
        {
            metaEnable = true;
            previewEnable = true;
            if(this->m_Layer->type() == QgsMapLayer::VectorLayer)
                attributesEnable = true;
        }
    }
    else
        ui->action_Set_Projection->setEnabled(false);

    this->updateCurrentTab();

    int selected = -1;
    if(this->m_LastTab.contains(item->metaObject()->className()))
        selected = this->m_LastTab[item->metaObject()->className()];

    ui->tabWidget->setTabEnabled(ui->tabWidget->indexOf(ui->paramTab),paramEnable);
    ui->tabWidget->setTabEnabled(ui->tabWidget->indexOf(ui->metaDataTab),metaEnable);
    ui->tabWidget->setTabEnabled(ui->tabWidget->indexOf(ui->PreviewTab),previewEnable);
    ui->tabWidget->setTabEnabled(ui->tabWidget->indexOf(ui->attributesTab),attributesEnable);

    if(selected > 0)
    {
        QgsDebugMsg(QString(" set tab %1 %2").arg(item->metaObject()->className()).arg(item->name()));
        ui->tabWidget->setCurrentIndex(selected);
    }

    QgsDebugMsg( QString( "clicked: %1 %2 %3" ).arg( index.row() ).arg( index.column() ).arg( item->name() ) );

}
void QgsBrowserDockWidget::showProperties( )
{
  QgsDebugMsg( "Entered" );
  QgsDataItem* dataItem = mModel->dataItem( mBrowserView->currentIndex() );

  if ( dataItem != NULL && dataItem->type() == QgsDataItem::Layer )
  {
    QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( dataItem );
    if ( layerItem != NULL )
    {
      QgsMapLayer::LayerType type = layerItem->mapLayerType();
      QString layerMetadata = tr( "Error" );
      QgsCoordinateReferenceSystem layerCrs;
      QString notice;

      // temporarily override /Projections/defaultBehaviour to avoid dialog prompt
      QSettings settings;
      QString defaultProjectionOption = settings.value( "/Projections/defaultBehaviour", "prompt" ).toString();
      if ( settings.value( "/Projections/defaultBehaviour", "prompt" ).toString() == "prompt" )
      {
        settings.setValue( "/Projections/defaultBehaviour", "useProject" );
      }

      // find root item
      // we need to create a temporary layer to get metadata
      // we could use a provider but the metadata is not as complete and "pretty"  and this is easier
      QgsDebugMsg( QString( "creating temporary layer using path %1" ).arg( layerItem->path() ) );
      if ( type == QgsMapLayer::RasterLayer )
      {
        QgsDebugMsg( "creating raster layer" );
        // should copy code from addLayer() to split uri ?
        QgsRasterLayer* layer = new QgsRasterLayer( layerItem->uri(), layerItem->uri(), layerItem->providerKey() );
        if ( layer != NULL )
        {
          layerCrs = layer->crs();
          layerMetadata = layer->metadata();
          delete layer;
        }
      }
      else if ( type == QgsMapLayer::VectorLayer )
      {
        QgsDebugMsg( "creating vector layer" );
        QgsVectorLayer* layer = new QgsVectorLayer( layerItem->uri(), layerItem->name(), layerItem->providerKey() );
        if ( layer != NULL )
        {
          layerCrs = layer->crs();
          layerMetadata = layer->metadata();
          delete layer;
        }
      }

      // restore /Projections/defaultBehaviour
      if ( defaultProjectionOption == "prompt" )
      {
        settings.setValue( "/Projections/defaultBehaviour", defaultProjectionOption );
      }

      // initialize dialog
      QDialog *dialog = new QDialog( this );
      Ui::QgsBrowserLayerPropertiesBase ui;
      ui.setupUi( dialog );

      dialog->setWindowTitle( tr( "Layer Properties" ) );
      ui.leName->setText( layerItem->name() );
      ui.leSource->setText( layerItem->path() );
      ui.leProvider->setText( layerItem->providerKey() );
      QString myStyle = QgsApplication::reportStyleSheet();
      ui.txtbMetadata->document()->setDefaultStyleSheet( myStyle );
      ui.txtbMetadata->setHtml( layerMetadata );

      // report if layer was set to to project crs without prompt (may give a false positive)
      if ( defaultProjectionOption == "prompt" )
      {
        QgsCoordinateReferenceSystem defaultCrs =
          QgisApp::instance()->mapCanvas()->mapRenderer()->destinationCrs();
        if ( layerCrs == defaultCrs )
          ui.lblNotice->setText( "NOTICE: Layer srs set from project (" + defaultCrs.authid() + ")" );
      }

      dialog->show();
    }
  }
}
Esempio n. 25
0
QVector<QgsDataItem *> QgsGeoNodeServiceItem::createChildren()
{
  QVector<QgsDataItem *> children;
  QHash<QgsDataItem *, QString> serviceItems; // service/provider key

  int layerCount = 0;
  // Try to open with service provider
  bool skipProvider = false;

  QgsGeoNodeConnectionItem *parentItem = dynamic_cast<QgsGeoNodeConnectionItem *>( mParent );
  QString pathPrefix = parentItem->mGeoNodeName.toLower() + QStringLiteral( ":/" );

  while ( !skipProvider )
  {
    const QString &key = mServiceName != QStringLiteral( "WFS" ) ? QStringLiteral( "wms" ) : mServiceName;
    std::unique_ptr< QLibrary > library( QgsProviderRegistry::instance()->createProviderLibrary( key ) );
    if ( !library )
    {
      skipProvider = true;
      continue;
    }

    dataItemProviders_t *dataItemProvidersFn = reinterpret_cast< dataItemProviders_t * >( cast_to_fptr( library->resolve( "dataItemProviders" ) ) );
    dataItem_t *dItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
    if ( !dItem && !dataItemProvidersFn )
    {
      skipProvider = true;
      continue;
    }

    QString path = pathPrefix + mName;

    QVector<QgsDataItem *> items;
    QList<QgsDataItemProvider *> *providerList = dataItemProvidersFn();
    for ( QgsDataItemProvider *pr : qgis::as_const( *providerList ) )
    {
      if ( !pr->name().startsWith( mServiceName ) )
        continue;

      items = pr->createDataItems( path, this );
      if ( !items.isEmpty() )
      {
        break;
      }
    }

    if ( items.isEmpty() )
    {
      skipProvider = true;
      continue;
    }

    if ( mServiceName == QStringLiteral( "XYZ" ) )
    {
      return items;
    }

    for ( QgsDataItem *item : qgis::as_const( items ) )
    {
      item->populate( true ); // populate in foreground - this is already run in a thread

      layerCount += item->rowCount();
      if ( item->rowCount() > 0 )
      {
        serviceItems.insert( item, key );
      }
      else
      {
        //delete item;
      }
    }

    skipProvider = true;
  }

  auto serviceItemIt = serviceItems.constBegin();
  for ( ; serviceItemIt != serviceItems.constEnd(); ++serviceItemIt )
  {
    QgsDataItem *item = serviceItemIt.key();
    QString providerKey = serviceItemIt.value();

    // Add layers directly to service item
    const QVector< QgsDataItem * > serviceChildItems = item->children();
    for ( QgsDataItem *subItem : serviceChildItems )
    {
      if ( subItem->path().endsWith( QStringLiteral( "error" ) ) )
      {
        continue;
      }
      item->removeChildItem( subItem );
      subItem->setParent( this );
      replacePath( subItem, providerKey.toLower() + QStringLiteral( ":/" ), pathPrefix );
      children.append( subItem );
    }

    delete item;
  }

  return children;
}
Esempio n. 26
0
QVector<QgsDataItem *> QgsWMSConnectionItem::createChildren()
{
  QVector<QgsDataItem *> children;

  QgsDataSourceUri uri;
  uri.setEncodedUri( mUri );

  QgsDebugMsg( "mUri = " + mUri );

  QgsWmsSettings wmsSettings;
  if ( !wmsSettings.parseUri( mUri ) )
  {
    children.append( new QgsErrorItem( this, tr( "Failed to parse WMS URI" ), mPath + "/error" ) );
    return children;
  }

  bool res = mCapabilitiesDownload->downloadCapabilities( wmsSettings.baseUrl(), wmsSettings.authorization() );

  if ( !res )
  {
    children.append( new QgsErrorItem( this, tr( "Failed to download capabilities" ), mPath + "/error" ) );
    return children;
  }

  QgsWmsCapabilities caps;
  if ( !caps.parseResponse( mCapabilitiesDownload->response(), wmsSettings.parserSettings() ) )
  {
    children.append( new QgsErrorItem( this, tr( "Failed to parse capabilities" ), mPath + "/error" ) );
    return children;
  }

  // Attention: supportedLayers() gives tree leafs, not top level
  QVector<QgsWmsLayerProperty> layerProperties = caps.supportedLayers();
  if ( !layerProperties.isEmpty() )
  {
    QgsWmsCapabilitiesProperty capabilitiesProperty = caps.capabilitiesProperty();
    const QgsWmsCapabilityProperty &capabilityProperty = capabilitiesProperty.capability;

    for ( const QgsWmsLayerProperty &layerProperty : qgis::as_const( capabilityProperty.layers ) )
    {
      // Attention, the name may be empty
      QgsDebugMsg( QString::number( layerProperty.orderId ) + ' ' + layerProperty.name + ' ' + layerProperty.title );
      QString pathName = layerProperty.name.isEmpty() ? QString::number( layerProperty.orderId ) : layerProperty.name;

      QgsWMSLayerItem *layer = new QgsWMSLayerItem( this, layerProperty.title, mPath + '/' + pathName, capabilitiesProperty, uri, layerProperty );

      children << layer;
    }
  }

  QList<QgsWmtsTileLayer> tileLayers = caps.supportedTileLayers();
  if ( !tileLayers.isEmpty() )
  {
    QHash<QString, QgsWmtsTileMatrixSet> tileMatrixSets = caps.supportedTileMatrixSets();

    const auto constTileLayers = tileLayers;
    for ( const QgsWmtsTileLayer &l : constTileLayers )
    {
      QString title = l.title.isEmpty() ? l.identifier : l.title;
      QgsDataItem *layerItem = l.styles.size() == 1 ? this : new QgsDataCollectionItem( this, title, mPath + '/' + l.identifier );
      if ( layerItem != this )
      {
        layerItem->setCapabilities( layerItem->capabilities2() & ~QgsDataItem::Fertile );
        layerItem->setState( QgsDataItem::Populated );
        layerItem->setToolTip( title );
        children << layerItem;
      }

      for ( const QgsWmtsStyle &style : qgis::as_const( l.styles ) )
      {
        QString styleName = style.title.isEmpty() ? style.identifier : style.title;
        if ( layerItem == this )
          styleName = title;  // just one style so no need to display it

        QgsDataItem *styleItem = l.setLinks.size() == 1 ? layerItem : new QgsDataCollectionItem( layerItem, styleName, layerItem->path() + '/' + style.identifier );
        if ( styleItem != layerItem )
        {
          styleItem->setCapabilities( styleItem->capabilities2() & ~QgsDataItem::Fertile );
          styleItem->setState( QgsDataItem::Populated );
          styleItem->setToolTip( styleName );
          if ( layerItem == this )
            children << styleItem;
          else
            layerItem->addChildItem( styleItem );
        }

        for ( const QgsWmtsTileMatrixSetLink &setLink : qgis::as_const( l.setLinks ) )
        {
          QString linkName = setLink.tileMatrixSet;
          if ( styleItem == layerItem )
            linkName = styleName;  // just one link so no need to display it

          QgsDataItem *linkItem = l.formats.size() == 1 ? styleItem : new QgsDataCollectionItem( styleItem, linkName, styleItem->path() + '/' + setLink.tileMatrixSet );
          if ( linkItem != styleItem )
          {
            linkItem->setCapabilities( linkItem->capabilities2() & ~QgsDataItem::Fertile );
            linkItem->setState( QgsDataItem::Populated );
            linkItem->setToolTip( linkName );
            if ( styleItem == this )
              children << linkItem;
            else
              styleItem->addChildItem( linkItem );
          }

          for ( const QString &format : qgis::as_const( l.formats ) )
          {
            QString name = format;
            if ( linkItem == styleItem )
              name = linkName;  // just one format so no need to display it

            QgsDataItem *tileLayerItem = new QgsWMTSLayerItem( linkItem, name, linkItem->path() + '/' + name, uri,
                l.identifier, format, style.identifier, setLink.tileMatrixSet, tileMatrixSets[ setLink.tileMatrixSet ].crs, title );
            tileLayerItem->setToolTip( name );
            if ( linkItem == this )
              children << tileLayerItem;
            else
              linkItem->addChildItem( tileLayerItem );
          }
        }
      }
    }
  }

  return children;
}