Skip to content

Commit

Permalink
grass support for qbrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed May 22, 2011
1 parent b02d06b commit 9784cd7
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 113 deletions.
2 changes: 2 additions & 0 deletions images/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<file>themes/default/geographic.png</file>
<file>themes/default/gpsicons/barchart.svg</file>
<file>themes/default/gpsicons/polarchart.svg</file>
<file>themes/default/grass_location.png</file>
<file>themes/default/grass_mapset.png</file>
<file>themes/default/join_bevel.png</file>
<file>themes/default/join_miter.png</file>
<file>themes/default/join_round.png</file>
Expand Down
Binary file added images/themes/default/grass_location.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/themes/default/grass_mapset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/browser/qgsbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ void QgsBrowser::updateCurrentTab()

if (current == Metadata && mDirtyMetadata)
{
if (mLayer)
if (mLayer && mLayer->isValid())
{
// Set meta
QString myStyle = QgsApplication::reportStyleSheet();
Expand All @@ -436,7 +436,7 @@ void QgsBrowser::updateCurrentTab()

if (current == Preview && mDirtyPreview)
{
if (mLayer)
if (mLayer && mLayer->isValid())
{
// Create preview: add to map canvas
QList<QgsMapCanvasLayer> layers;
Expand All @@ -452,7 +452,7 @@ void QgsBrowser::updateCurrentTab()

if (current == Attributes && mDirtyAttributes)
{
if ( mLayer && mLayer->type() == QgsMapLayer::VectorLayer )
if ( mLayer && mLayer->isValid() && mLayer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( mLayer );
QApplication::setOverrideCursor(Qt::WaitCursor);
Expand Down
24 changes: 20 additions & 4 deletions src/core/qgsdataitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const QIcon &QgsDataCollectionItem::iconDir()
{
static QIcon icon;

if ( !icon.isNull() )
if ( icon.isNull() )
{
// initialize shared icons
QStyle *style = QApplication::style();
Expand Down Expand Up @@ -250,6 +250,16 @@ void QgsDataItem::refresh()
}
}

bool QgsDataItem::equal( const QgsDataItem *other )
{
if ( typeid ( this ) == typeid ( other ) &&
mPath == other->path() )
{
return true;
}
return false;
}

// ---------------------------------------------------------------------

QgsLayerItem::QgsLayerItem( QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey )
Expand Down Expand Up @@ -327,9 +337,10 @@ QgsDirectoryItem::QgsDirectoryItem( QgsDataItem* parent, QString name, QString p
}
if ( dataCapabilities() == QgsDataProvider::NoDataCapabilities )
{
QgsDebugMsg( library->fileName() + " does not have File capability" );
QgsDebugMsg( library->fileName() + " has NoDataCapabilities" );
continue;
}
QgsDebugMsg( QString ( "%1 dataCapabilities : %2").arg(library->fileName()).arg(dataCapabilities() ) );
mLibraries.append( library );
}
else
Expand Down Expand Up @@ -360,10 +371,11 @@ QVector<QgsDataItem*> QgsDirectoryItem::createChildren( )
children.append( item );
}

QStringList fileEntries = dir.entryList( QDir::Files, QDir::Name );
QStringList fileEntries = dir.entryList( QDir::Dirs|QDir::NoDotAndDotDot|QDir::Files, QDir::Name );
foreach( QString name, fileEntries )
{
QString path = dir.absoluteFilePath( name );
QFileInfo fileInfo ( path );
foreach( QLibrary *library, mLibraries )
{
// we could/should create separate list of providers for each purpose
Expand All @@ -374,7 +386,11 @@ QVector<QgsDataItem*> QgsDirectoryItem::createChildren( )

int capabilities = dataCapabilities();

if ( !( capabilities & QgsDataProvider::File ) ) continue;
if ( !( (fileInfo.isFile() && (capabilities & QgsDataProvider::File)) ||
(fileInfo.isDir() && (capabilities & QgsDataProvider::Dir))) )
{
continue;
}

dataItem_t * dataItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
if ( ! dataItem )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsdataitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CORE_EXPORT QgsDataItem : public QObject
// remove and delete child item, signals to browser are emited
virtual void deleteChildItem( QgsDataItem * child );

virtual bool equal( const QgsDataItem *other ) { return false; }
virtual bool equal( const QgsDataItem *other );

virtual QWidget * paramWidget() { return 0; }

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void QgsGrassBrowser::addMap()
else if ( type == QgsGrassModel::VectorLayer )
{

QStringList list = QgsGrassSelect::vectorLayers(
QStringList list = QgsGrass::vectorLayers(
QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
mModel->itemMapset( *it ), map );
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ void QgsGrassModel::refreshItem( QgsGrassModelItem *item )

case QgsGrassModel::Vector:
{
QStringList list = QgsGrassSelect::vectorLayers(
QStringList list = QgsGrass::vectorLayers(
QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
item->mMapset, item->mMap );
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ void QgsGrassModule::viewOutput()
{
QString map = mOutputVector.at( i );

QStringList layers = QgsGrassSelect::vectorLayers(
QStringList layers = QgsGrass::vectorLayers(
QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
QgsGrass::getDefaultMapset(), map );
Expand Down
96 changes: 1 addition & 95 deletions src/plugins/grass/qgsgrassselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void QgsGrassSelect::setLayers()
if ( type != VECTOR ) return;
if ( emap->count() < 1 ) return;

QStringList layers = vectorLayers( egisdbase->text(),
QStringList layers = QgsGrass::vectorLayers( egisdbase->text(),
elocation->currentText(), emapset->currentText(),
emap->currentText().toUtf8() );

Expand Down Expand Up @@ -369,100 +369,6 @@ void QgsGrassSelect::setLayers()
}
}

QStringList QgsGrassSelect::vectorLayers( QString gisdbase,
QString location, QString mapset, QString mapName )
{
QStringList list;

// Set location
QgsGrass::setLocation( gisdbase, location );

/* Open vector */
QgsGrass::resetError();
//Vect_set_open_level( 2 );
struct Map_info map;
int level = -1;

try
{
level = Vect_open_old_head( &map, ( char * ) mapName.toUtf8().data(), ( char * ) mapset.toUtf8().data() );
}
catch ( QgsGrass::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Cannot open GRASS vector: %1" ).arg( e.what() ) );
return list;
}

if ( level == 1 )
{
QgsDebugMsg( "Cannot open vector on level 2" );
QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector %1 in mapset %2 on level 2 (topology not available, try to rebuild topology using v.build module)." ).arg( mapName ).arg( mapset ) );
// Vect_close here is correct, it should work, but it seems to cause
// crash on win http://trac.osgeo.org/qgis/ticket/2003
// disabled on win test it
#if !defined(WIN32)
Vect_close( &map );
#endif
return list;
}
else if ( level < 1 )
{
QgsDebugMsg( "Cannot open vector" );
QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector %1 in mapset %2" ).arg( mapName ).arg( mapset ) );
return list;
}

QgsDebugMsg( "GRASS vector successfully opened" );


// Get layers
int ncidx = Vect_cidx_get_num_fields( &map );

for ( int i = 0; i < ncidx; i++ )
{
int field = Vect_cidx_get_field_number( &map, i );
QString fs;
fs.sprintf( "%d", field );

QgsDebugMsg( QString( "i = %1 layer = %2" ).arg( i ).arg( field ) );

/* Points */
int npoints = Vect_cidx_get_type_count( &map, field, GV_POINT );
if ( npoints > 0 )
{
QString l = fs + "_point";
list.append( l );
}

/* Lines */
/* Lines without category appears in layer 0, but not boundaries */
int tp;
if ( field == 0 )
tp = GV_LINE;
else
tp = GV_LINE | GV_BOUNDARY;

int nlines = Vect_cidx_get_type_count( &map, field, tp );
if ( nlines > 0 )
{
QString l = fs + "_line";
list.append( l );
}

/* Polygons */
int nareas = Vect_cidx_get_type_count( &map, field, GV_AREA );
if ( nareas > 0 )
{
QString l = fs + "_polygon";
list.append( l );
}
}
Vect_close( &map );

return list;
}

void QgsGrassSelect::on_GisdbaseBrowse_clicked()
{
QString Gisdbase = QFileDialog::getExistingDirectory( this,
Expand Down
3 changes: 0 additions & 3 deletions src/plugins/grass/qgsgrassselect.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class QgsGrassSelect: public QDialog, private Ui::QgsGrassSelectBase
MAPCALC // file in $MAPSET/mapcalc directory (used by QgsGrassMapcalc)
};

//! Get list of vector layer
static QStringList vectorLayers( QString, QString, QString, QString );

QString gisdbase;
QString location;
QString mapset;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ QString QgsGrassUtils::vectorLayerName( QString map, QString layer,
void QgsGrassUtils::addVectorLayers( QgisInterface *iface,
QString gisbase, QString location, QString mapset, QString map )
{
QStringList layers = QgsGrassSelect::vectorLayers(
QStringList layers = QgsGrass::vectorLayers(
gisbase, location, mapset, map );


Expand Down
4 changes: 2 additions & 2 deletions src/providers/grass/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ ADD_DEFINITIONS(-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\")
########################################################
# Files

SET(GRASS_PROVIDER_SRCS provider.cpp)
SET(GRASS_PROVIDER_SRCS provider.cpp qgsgrassprovider.cpp )

SET(GRASS_RASTER_PROVIDER_SRCS qgsgrassrasterprovider.cpp)

SET(GRASS_LIB_SRCS qgsgrassprovider.cpp qgsgrass.cpp)
SET(GRASS_LIB_SRCS qgsgrass.cpp)

SET(QGIS_D_RAST_SRCS qgis.d.rast.c)

Expand Down
93 changes: 93 additions & 0 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,99 @@ QStringList GRASS_EXPORT QgsGrass::vectors( QString mapsetPath )
}
return list;
}
QStringList GRASS_EXPORT QgsGrass::vectorLayers( QString gisdbase,
QString location, QString mapset, QString mapName )
{
QStringList list;

// Set location
QgsGrass::setLocation( gisdbase, location );

/* Open vector */
QgsGrass::resetError();
//Vect_set_open_level( 2 );
struct Map_info map;
int level = -1;

try
{
level = Vect_open_old_head( &map, ( char * ) mapName.toUtf8().data(), ( char * ) mapset.toUtf8().data() );
}
catch ( QgsGrass::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Cannot open GRASS vector: %1" ).arg( e.what() ) );
return list;
}

if ( level == 1 )
{
QgsDebugMsg( "Cannot open vector on level 2" );
QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot open vector %1 in mapset %2 on level 2 (topology not available, try to rebuild topology using v.build module)." ).arg( mapName ).arg( mapset ) );
// Vect_close here is correct, it should work, but it seems to cause
// crash on win http://trac.osgeo.org/qgis/ticket/2003
// disabled on win test it
#if !defined(WIN32)
Vect_close( &map );
#endif
return list;
}
else if ( level < 1 )
{
QgsDebugMsg( "Cannot open vector" );
QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot open vector %1 in mapset %2" ).arg( mapName ).arg( mapset ) );
return list;
}

QgsDebugMsg( "GRASS vector successfully opened" );


// Get layers
int ncidx = Vect_cidx_get_num_fields( &map );

for ( int i = 0; i < ncidx; i++ )
{
int field = Vect_cidx_get_field_number( &map, i );
QString fs;
fs.sprintf( "%d", field );

QgsDebugMsg( QString( "i = %1 layer = %2" ).arg( i ).arg( field ) );

/* Points */
int npoints = Vect_cidx_get_type_count( &map, field, GV_POINT );
if ( npoints > 0 )
{
QString l = fs + "_point";
list.append( l );
}

/* Lines */
/* Lines without category appears in layer 0, but not boundaries */
int tp;
if ( field == 0 )
tp = GV_LINE;
else
tp = GV_LINE | GV_BOUNDARY;

int nlines = Vect_cidx_get_type_count( &map, field, tp );
if ( nlines > 0 )
{
QString l = fs + "_line";
list.append( l );
}

/* Polygons */
int nareas = Vect_cidx_get_type_count( &map, field, GV_AREA );
if ( nareas > 0 )
{
QString l = fs + "_polygon";
list.append( l );
}
}
Vect_close( &map );

return list;
}

QStringList GRASS_EXPORT QgsGrass::rasters( QString gisbase, QString locationName,
QString mapsetName )
Expand Down
3 changes: 3 additions & 0 deletions src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class QgsGrass
QString mapsetName );
static GRASS_EXPORT QStringList rasters( QString mapsetPath );

//! Get list of vector layers
static QStringList vectorLayers( QString, QString, QString, QString );

//! List of elements
static GRASS_EXPORT QStringList elements( QString gisbase, QString locationName,
QString mapsetName, QString element );
Expand Down
Loading

0 comments on commit 9784cd7

Please sign in to comment.