Skip to content

Commit

Permalink
[style manager] sort displayed symbols, insure tags are added only once
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 18, 2016
1 parent 13a0e48 commit 5f3ba72
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
9 changes: 9 additions & 0 deletions python/core/symbology-ng/qgsstyle.sip
Expand Up @@ -236,6 +236,15 @@ class QgsStyle : QObject
*/
QStringList tagsOfSymbol( StyleEntity type, const QString& symbol );

/** Returns wheter a given tag is associated with the symbol
*
* \param type is either SymbolEntity or ColorrampEntity
* \param symbol is the name of the symbol or color ramp
* \param tag the name of the tag to look for
* \return A boolean value identicating whether a tag was found attached to the symbol
*/
bool symbolHasTag( StyleEntity type, const QString& symbol, const QString& tag );

//! Returns the tag name for the given id
QString tag( int id ) const;

Expand Down
58 changes: 49 additions & 9 deletions src/core/symbology-ng/qgsstyle.cpp
Expand Up @@ -842,16 +842,19 @@ bool QgsStyle::tagSymbol( StyleEntity type, const QString& symbol, const QString

sqlite3_finalize( ppStmt );

// Now map the tag to the symbol
query = type == SymbolEntity
? sqlite3_mprintf( "INSERT INTO tagmap VALUES (%d,%d)", tagid, symbolid )
: sqlite3_mprintf( "INSERT INTO ctagmap VALUES (%d,%d)", tagid, symbolid );

char *zErr = nullptr;
nErr = sqlite3_exec( mCurrentDB, query, nullptr, nullptr, &zErr );
if ( nErr )
// Now map the tag to the symbol if it's not already tagged
if ( !symbolHasTag( type, symbol, tag ) )
{
QgsDebugMsg( zErr );
query = type == SymbolEntity
? sqlite3_mprintf( "INSERT INTO tagmap VALUES (%d,%d)", tagid, symbolid )
: sqlite3_mprintf( "INSERT INTO ctagmap VALUES (%d,%d)", tagid, symbolid );

char *zErr = nullptr;
nErr = sqlite3_exec( mCurrentDB, query, nullptr, nullptr, &zErr );
if ( nErr )
{
QgsDebugMsg( zErr );
}
}
}
}
Expand Down Expand Up @@ -995,6 +998,43 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString& symbol )
return tagList;
}

bool QgsStyle::symbolHasTag( StyleEntity type, const QString& symbol, const QString& tag )
{
if ( !mCurrentDB )
{
QgsDebugMsg( "Sorry! Cannot open database for getting the tags." );
return false;
}

int symbolid = type == SymbolEntity ? symbolId( symbol ) : colorrampId( symbol );
if ( !symbolid )
{
return false;
}
int tagid = tagId( tag );
if ( !tagid )
{
return false;
}

// get the ids of tags for the symbol
char *query = type == SymbolEntity
? sqlite3_mprintf( "SELECT tag_id FROM tagmap WHERE tag_id=%d AND symbol_id=%d", tagid, symbolid )
: sqlite3_mprintf( "SELECT tag_id FROM ctagmap WHERE tag_id=%d AND colorramp_id=%d", tagid, symbolid );

sqlite3_stmt *ppStmt;
int nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, nullptr );

if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
{
return true;
}
else
{
return false;
}
}

QString QgsStyle::tag( int id ) const
{
if ( !mCurrentDB )
Expand Down
9 changes: 9 additions & 0 deletions src/core/symbology-ng/qgsstyle.h
Expand Up @@ -301,6 +301,15 @@ class CORE_EXPORT QgsStyle : public QObject
*/
QStringList tagsOfSymbol( StyleEntity type, const QString& symbol );

/** Returns wheter a given tag is associated with the symbol
*
* \param type is either SymbolEntity or ColorrampEntity
* \param symbol is the name of the symbol or color ramp
* \param tag the name of the tag to look for
* \return A boolean value identicating whether a tag was found attached to the symbol
*/
bool symbolHasTag( StyleEntity type, const QString& symbol, const QString& tag );

//! Returns the tag name for the given id
QString tag( int id ) const;

Expand Down
9 changes: 5 additions & 4 deletions src/gui/symbology-ng/qgsstylemanagerdialog.cpp
Expand Up @@ -271,19 +271,19 @@ void QgsStyleManagerDialog::populateSymbols( const QStringList& symbolNames, boo
model->clear();

int type = currentItemType();

for ( int i = 0; i < symbolNames.count(); ++i )
{
QString name = symbolNames[i];
QgsSymbol* symbol = mStyle->symbol( name );
if ( symbol && symbol->type() == type )
{
QStringList tags = mStyle->tagsOfSymbol( QgsStyle::SymbolEntity, name );
QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( symbol, listItems->iconSize(), 18 );
item->setIcon( icon );
item->setData( name ); // used to find out original name when user edited the name
item->setCheckable( check );
item->setToolTip( name );
item->setToolTip( QString( "<b>%1</b><br><i>%2</i>" ).arg( name ).arg( tags.count() > 0 ? tags.join( ", " ) : tr( "Not tagged" ) ) );
// add to model
model->appendRow( item );
}
Expand Down Expand Up @@ -994,6 +994,7 @@ void QgsStyleManagerDialog::groupChanged( const QModelIndex& index )
}
}

symbolNames.sort();
if ( currentItemType() < 3 )
{
populateSymbols( symbolNames, mGrouppingMode );
Expand Down Expand Up @@ -1271,14 +1272,14 @@ void QgsStyleManagerDialog::setSymbolsChecked( const QStringList& symbols )
void QgsStyleManagerDialog::filterSymbols( const QString& qword )
{
QStringList items;
items = mStyle->findSymbols( currentItemType() < 3 ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity, qword );
items.sort();
if ( currentItemType() == 3 )
{
items = mStyle->findSymbols( QgsStyle::ColorrampEntity, qword );
populateColorRamps( items );
}
else
{
items = mStyle->findSymbols( QgsStyle::SymbolEntity, qword );
populateSymbols( items );
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/gui/symbology-ng/qgssymbolslistwidget.cpp
Expand Up @@ -211,6 +211,7 @@ void QgsSymbolsListWidget::populateSymbolView()
symbols = mStyle->symbolsWithTag( QgsStyle::SymbolEntity, id );
}

symbols.sort();
populateSymbols( symbols );
}

Expand All @@ -233,10 +234,11 @@ void QgsSymbolsListWidget::populateSymbols( const QStringList& names )
delete s;
continue;
}
QStringList tags = mStyle->tagsOfSymbol( QgsStyle::SymbolEntity, names[i] );
QStandardItem* item = new QStandardItem( names[i] );
item->setData( names[i], Qt::UserRole ); //so we can load symbol with that name
item->setText( names[i] );
item->setToolTip( names[i] );
item->setToolTip( QString( "<b>%1</b><br><i>%2</i>" ).arg( names[i] ).arg( tags.count() > 0 ? tags.join( ", " ) : tr( "Not tagged" ) ) );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
// Set font to 10points to show reasonable text
QFont itemFont = item->font();
Expand Down

0 comments on commit 5f3ba72

Please sign in to comment.