Skip to content

Commit 95e571c

Browse files
committed
added functions to QgsStyleV2 for getting groups by id
1 parent 17ed9d1 commit 95e571c

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

python/core/symbology-ng/qgsstylev2.sip

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class QgsStyleV2 : QObject
127127
int symbolId( const QString& name );
128128
//! return the DB id for the given group name
129129
int groupId( const QString& group );
130+
//! return the group name for the given DB id
131+
QString groupName( int groupId ) const;
130132
//! return the DB id for the given tag name
131133
int tagId( const QString& tag );
132134
//! return the DB id for the given smartgroup name
@@ -135,6 +137,9 @@ class QgsStyleV2 : QObject
135137
//! return the all the groups in the style
136138
QStringList groupNames();
137139

140+
//! return the ids of all the groups in the style
141+
QList<int> groupIds() const;
142+
138143
//! returns the symbolnames of a given groupid
139144
/*!
140145
* \param type is either SymbolEntity or ColorampEntity
@@ -270,6 +275,9 @@ class QgsStyleV2 : QObject
270275
//! gets the id from the table for the given name from the database, 0 if not found
271276
int getId( const QString& table, const QString& name );
272277

278+
//! gets the name from the table for the given id from the database, empty if not found
279+
QString getName( const QString& table, int id ) const;
280+
273281
//! updates the properties of an existing symbol/colorramp
274282
/*!
275283
* \note This should not be called separately, only called through addSymbol or addColorRamp

src/core/symbology-ng/qgsstylev2.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,20 @@ QStringList QgsStyleV2::groupNames()
475475
return groupNames;
476476
}
477477

478+
QList<int> QgsStyleV2::groupIds() const
479+
{
480+
QList<int> groupIds;
481+
sqlite3_stmt *ppStmt;
482+
const char *query = "SELECT * FROM symgroup";
483+
int nError = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, nullptr );
484+
while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
485+
{
486+
groupIds << QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, SymgroupId ) ) ).toInt();
487+
}
488+
sqlite3_finalize( ppStmt );
489+
return groupIds;
490+
}
491+
478492
QgsSymbolGroupMap QgsStyleV2::childGroupNames( const QString& parent )
479493
{
480494
// get the name list from the sqlite database and return as a QStringList
@@ -947,6 +961,24 @@ int QgsStyleV2::getId( const QString& table, const QString& name )
947961
return id;
948962
}
949963

964+
QString QgsStyleV2::getName( const QString& table, int id ) const
965+
{
966+
char *query = sqlite3_mprintf( "SELECT name FROM %q WHERE id='%q'", table.toUtf8().constData(), QString::number( id ).toUtf8().constData() );
967+
968+
sqlite3_stmt *ppStmt;
969+
int nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, nullptr );
970+
971+
QString name;
972+
if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
973+
{
974+
name = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( ppStmt, 0 ) ) );
975+
}
976+
977+
sqlite3_finalize( ppStmt );
978+
979+
return name;
980+
}
981+
950982
int QgsStyleV2::symbolId( const QString& name )
951983
{
952984
return getId( "symbol", name );
@@ -962,6 +994,11 @@ int QgsStyleV2::groupId( const QString& name )
962994
return getId( "symgroup", name );
963995
}
964996

997+
QString QgsStyleV2::groupName( int groupId ) const
998+
{
999+
return getName( "symgroup", groupId );
1000+
}
1001+
9651002
int QgsStyleV2::tagId( const QString& name )
9661003
{
9671004
return getId( "tag", name );

src/core/symbology-ng/qgsstylev2.h

+8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class CORE_EXPORT QgsStyleV2 : public QObject
190190
int symbolId( const QString& name );
191191
//! return the DB id for the given group name
192192
int groupId( const QString& group );
193+
//! return the group name for the given DB id
194+
QString groupName( int groupId ) const;
193195
//! return the DB id for the given tag name
194196
int tagId( const QString& tag );
195197
//! return the DB id for the given smartgroup name
@@ -198,6 +200,9 @@ class CORE_EXPORT QgsStyleV2 : public QObject
198200
//! return the all the groups in the style
199201
QStringList groupNames();
200202

203+
//! return the ids of all the groups in the style
204+
QList<int> groupIds() const;
205+
201206
//! returns the symbolnames of a given groupid
202207
/*!
203208
* \param type is either SymbolEntity or ColorampEntity
@@ -344,6 +349,9 @@ class CORE_EXPORT QgsStyleV2 : public QObject
344349
//! gets the id from the table for the given name from the database, 0 if not found
345350
int getId( const QString& table, const QString& name );
346351

352+
//! gets the name from the table for the given id from the database, empty if not found
353+
QString getName( const QString& table, int id ) const;
354+
347355
//! updates the properties of an existing symbol/colorramp
348356
/*!
349357
* \note This should not be called separately, only called through addSymbol or addColorRamp

0 commit comments

Comments
 (0)