Skip to content

Commit

Permalink
freeze/thaw map
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@5860 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Sep 22, 2006
1 parent 76a2a8b commit cc4d3b9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 14 deletions.
79 changes: 68 additions & 11 deletions src/providers/grass/qgsgrassprovider.cpp
Expand Up @@ -289,7 +289,7 @@ QgsFeature *QgsGrassProvider::getFirstFeature(bool fetchAttributes)
std::cout << "QgsGrassProvider::getFirstFeature()" << std::endl;
#endif

if ( isEdited() )
if ( isEdited() || isFrozen() )
return 0;

if ( mCidxFieldIndex < 0 ) return 0; // No features, no features in this layer
Expand All @@ -309,7 +309,7 @@ bool QgsGrassProvider::getNextFeature(QgsFeature &feature, bool fetchAttributes)
std::cout << "QgsGrassProvider::getNextFeature()" << std::endl;
#endif

if ( isEdited() )
if ( isEdited() || isFrozen() )
return 0;

if ( mCidxFieldIndex < 0 ) return 0; // No features, no features in this layer
Expand All @@ -330,7 +330,7 @@ QgsFeature *QgsGrassProvider::getNextFeature(bool fetchAttributes)
<< " fetchAttributes = " << fetchAttributes << std::endl;
#endif

if ( isEdited() )
if ( isEdited() || isFrozen() )
return 0;

if ( mCidxFieldIndex < 0 ) return 0; // No features, no features in this layer
Expand All @@ -357,7 +357,7 @@ QgsFeature* QgsGrassProvider::getNextFeature(std::list<int> const& attlist, int
std::cout << "QgsGrassProvider::getNextFeature( attlist )" << std::endl;
#endif

if ( isEdited() )
if ( isEdited() || isFrozen() )
return 0;

if ( mCidxFieldIndex < 0 ) return 0; // No features, no features in this layer
Expand Down Expand Up @@ -486,7 +486,7 @@ void QgsGrassProvider::select(QgsRect *rect, bool useIntersect)
std::cout << "QgsGrassProvider::select() useIntersect = " << useIntersect << std::endl;
#endif

if ( isEdited() )
if ( isEdited() || isFrozen() )
return;

// check if outdated and update if necessary
Expand Down Expand Up @@ -560,7 +560,7 @@ std::vector<QgsFeature>& QgsGrassProvider::identify(QgsRect * rect)

// TODO: does not return vector of features! Should it?

if ( !isEdited() ) {
if ( !isEdited() && !isFrozen() ) {
select(rect, true);
}
}
Expand Down Expand Up @@ -614,7 +614,7 @@ int QgsGrassProvider::keyField()

void QgsGrassProvider::reset()
{
if ( isEdited() )
if ( isEdited() || isFrozen() )
return;

int mapId = mLayers[mLayerId].mapId;
Expand Down Expand Up @@ -1013,6 +1013,8 @@ int QgsGrassProvider::openMap(QString gisdbase, QString location, QString mapset
}

GMAP map;
map.valid = false;
map.frozen = false;
map.gisdbase = gisdbase;
map.location = location;
map.mapset = mapset;
Expand Down Expand Up @@ -1096,6 +1098,8 @@ int QgsGrassProvider::openMap(QString gisdbase, QString location, QString mapset
#ifdef QGISDEBUG
std::cerr << "GRASS map successfully opened" << std::endl;
#endif

map.valid = true;

// Add new map to maps
mMaps.push_back(map);
Expand Down Expand Up @@ -1373,6 +1377,49 @@ bool QgsGrassProvider::isEdited ( void )
return (map->update);
}

bool QgsGrassProvider::isFrozen ( void )
{
#if QGISDEBUG > 3
std::cerr << "QgsGrassProvider::isFrozen" << std::endl;
#endif

GMAP *map = &(mMaps[mLayers[mLayerId].mapId]);
return (map->frozen);
}

void QgsGrassProvider::freeze()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassProvider::freeze" << std::endl;
#endif

if ( !isValid() ) return;

GMAP *map = &(mMaps[mLayers[mLayerId].mapId]);

if ( map->frozen ) return;

map->frozen = true;
Vect_close ( map->map );
}

void QgsGrassProvider::thaw()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassProvider::thaw" << std::endl;
#endif

if ( !isValid() ) return;
GMAP *map = &(mMaps[mLayers[mLayerId].mapId]);

if ( !map->frozen ) return;

if ( reopenMap() )
{
map->frozen = false;
}
}

bool QgsGrassProvider::startEdit ( void )
{
#ifdef QGISDEBUG
Expand Down Expand Up @@ -1485,6 +1532,19 @@ bool QgsGrassProvider::closeEdit ( bool newMap )

Vect_close ( map->map );

map->update = false;

if ( !reopenMap() ) return false;

map->valid = true;

return true;
}

bool QgsGrassProvider::reopenMap()
{
GMAP *map = &(mMaps[mLayers[mLayerId].mapId]);

QFileInfo di ( mGisdbase + "/" + mLocation + "/" + mMapset + "/vector/" + mMapName );
map->lastModified = di.lastModified();

Expand All @@ -1499,7 +1559,7 @@ bool QgsGrassProvider::closeEdit ( bool newMap )

if ( QgsGrass::getError() == QgsGrass::FATAL ) {
std::cerr << "Cannot reopen GRASS vector: " << QgsGrass::getErrorMessage().toLocal8Bit().data() << std::endl;
return -1;
return false;
}

#ifdef QGISDEBUG
Expand All @@ -1515,9 +1575,6 @@ bool QgsGrassProvider::closeEdit ( bool newMap )
}
}

map->update = false;
map->valid = true;

return true;
}

Expand Down
34 changes: 31 additions & 3 deletions src/providers/grass/qgsgrassprovider.h
Expand Up @@ -34,6 +34,12 @@ class QgsField;
* Member variables which must be updated after updateMap() are marked !UPDATE! in this file.
*/

/* Freezing.
* Because open file cannot be deleted on Windows it is necessary to
* close output vector from GRASS tools before a module is run.
* This is not however solution for multiple instances of QGIS.
*/

/* Editing.
* If editing is started by startEdit, vector map is reopened in update mode, and GMAP.update
* is set to true. All data loaded from the map to QgsGrassProvider remain unchanged
Expand Down Expand Up @@ -82,6 +88,10 @@ struct GMAP
QString path; // path to the layer gisdbase+location+mapset+mapName
bool valid; // true if map is opened, once the map is closed,
// valid is set to false and no more used

// Vector temporally disabled. Necessary for GRASS Tools on Windows
bool frozen;

struct Map_info *map; // map header
int nUsers; // number layers using this map
int update; // true if the map is opened in update mode -> disabled standard reading
Expand Down Expand Up @@ -219,12 +229,27 @@ class QgsGrassProvider : public QgsVectorDataProvider
*/
bool isEdited();

/** Returns true if the layer is currently froze, i.e. a module
* from GRASS Tools is writing to this vector
* @return true in update mode
* @return false not edited
*/
bool isFrozen();

/** Start editing. Reopen the vector for update and set GMAP.update = true
* @return true success
* @return false failed to reopen success
* @return true is frozen
* @return false is not frozen
*/
bool startEdit();

/** Freeze vector.
*/
void freeze();

/** Thaw vector.
*/
void thaw();

/** Close editing. Rebuild topology, GMAP.update = false
* @param newMap set to true if a new map was created
* and it is not yet used as layer
Expand Down Expand Up @@ -540,9 +565,12 @@ class QgsGrassProvider : public QgsVectorDataProvider

bool mValid; // !UPDATE!
long mNumberFeatures; // !UPDATE!

void resetSelection(bool sel); // reset selection

// Reopen map after edit or freeze
bool reopenMap();

// -----------------------------------------------------------------------------------------
/* Static variables and methods.
* These methods opens GRASS vectors and loads some parts of vectors to the memory.
Expand Down

0 comments on commit cc4d3b9

Please sign in to comment.