Skip to content

Commit 16a6909

Browse files
author
Arunmozhi
committed
Adding and removing of groups using style manager done
1 parent 64b8019 commit 16a6909

File tree

4 files changed

+283
-11
lines changed

4 files changed

+283
-11
lines changed

src/core/symbology-ng/qgsstylev2.cpp

+127
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,130 @@ QStringList QgsStyleV2::symbolsWithTag( int tagid )
420420

421421
return symbols;
422422
}
423+
424+
int QgsStyleV2::addGroup( QString groupName, int parentid )
425+
{
426+
sqlite3 *db = openDB( mFileName );
427+
if ( db == NULL )
428+
return 0;
429+
sqlite3_stmt *ppStmt;
430+
431+
char *query;
432+
QByteArray groupArray = groupName.toUtf8();
433+
if ( parentid == 0 )
434+
{
435+
query = sqlite3_mprintf( "INSERT INTO symgroup VALUES (NULL, '%q', NULL);", groupArray.constData() );
436+
}
437+
else
438+
{
439+
query = sqlite3_mprintf( "INSERT INTO symgroup VALUES (NULL, '%q', %d);", groupArray.constData(), parentid );
440+
}
441+
int nErr = sqlite3_prepare_v2( db, query, -1, &ppStmt, NULL );
442+
if ( nErr == SQLITE_OK )
443+
sqlite3_step( ppStmt );
444+
sqlite3_finalize( ppStmt );
445+
446+
int groupid = (int)sqlite3_last_insert_rowid( db );
447+
sqlite3_close( db );
448+
return groupid;
449+
}
450+
451+
int QgsStyleV2::addTag( QString tagname )
452+
{
453+
sqlite3 *db = openDB( mFileName );
454+
if ( db == NULL )
455+
return 0;
456+
sqlite3_stmt *ppStmt;
457+
458+
QByteArray tagArray = tagname.toUtf8();
459+
char *query = sqlite3_mprintf( "INSERT INTO tag VALUES (NULL, '%q');", tagArray.constData() );
460+
int nErr = sqlite3_prepare_v2( db, query, -1, &ppStmt, NULL );
461+
if ( nErr == SQLITE_OK )
462+
sqlite3_step( ppStmt );
463+
sqlite3_finalize( ppStmt );
464+
465+
int tagid = (int)sqlite3_last_insert_rowid( db );
466+
sqlite3_close( db );
467+
return tagid;
468+
}
469+
470+
void QgsStyleV2::rename( StyleEntity type, int id, QString newName )
471+
{
472+
QByteArray nameArray = newName.toUtf8();
473+
char *query;
474+
switch ( type )
475+
{
476+
case SymbolEntity : query = sqlite3_mprintf( "UPDATE symbol SET name='%q' WHERE id=%d;", nameArray.constData(), id );
477+
break;
478+
case GroupEntity : query = sqlite3_mprintf( "UPDATE symgroup SET name='%q' WHERE id=%d;", nameArray.constData(), id );
479+
break;
480+
case TagEntity : query = sqlite3_mprintf( "UPDATE tag SET name='%q' WHERE id=%d;", nameArray.constData(), id );
481+
break;
482+
case ColorrampEntity : query = sqlite3_mprintf( "UPDATE colorramp SET name='%q' WHERE id=%d;", nameArray.constData(), id );
483+
break;
484+
default : QgsDebugMsg( "Invalid Style Entity indicated" );
485+
return;
486+
}
487+
if ( !runEmptyQuery( query ) )
488+
mErrorString = "Could not rename!";
489+
}
490+
491+
char* QgsStyleV2::getGroupRemoveQuery( int id )
492+
{
493+
int parentid;
494+
sqlite3 * db = openDB( mFileName );
495+
char *query = sqlite3_mprintf( "SELECT parent FROM symgroup WHERE id=%d;", id );
496+
sqlite3_stmt *ppStmt;
497+
int err = sqlite3_prepare_v2( db, query, -1, &ppStmt, NULL );
498+
if ( err == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
499+
parentid = sqlite3_column_int( ppStmt, 0 );
500+
sqlite3_finalize( ppStmt );
501+
sqlite3_close( db );
502+
if ( parentid )
503+
{
504+
query = sqlite3_mprintf( "UPDATE symbol SET groupid=%d WHERE groupid=%d;"
505+
"UPDATE symgroup SET parent=%d WHERE parent=%d;"
506+
"DELETE FROM symgroup WHERE id=%d;", parentid, id, parentid, id, id );
507+
}
508+
else
509+
{
510+
query = sqlite3_mprintf( "UPDATE symbol SET groupid=NULL WHERE groupid=%d;"
511+
"UPDATE symgroup SET parent=NULL WHERE parent=%d;"
512+
"DELETE FROM symgroup WHERE id=%d;", id, id, id );
513+
}
514+
return query;
515+
}
516+
517+
void QgsStyleV2::remove( StyleEntity type, int id )
518+
{
519+
char *query;
520+
switch ( type )
521+
{
522+
case SymbolEntity : query = sqlite3_mprintf( "DELETE FROM symbol WHERE id=%d; DELETE FROM tagmap WHERE symbol_id=%d;", id, id );
523+
break;
524+
case GroupEntity : query = getGroupRemoveQuery( id );
525+
break;
526+
case TagEntity : query = sqlite3_mprintf( "DELETE FROM tag WHERE id=%d; DELETE FROM tagmap WHERE tag_id=%d;", id, id );
527+
break;
528+
case ColorrampEntity : query = sqlite3_mprintf( "DELETE FROM colorramp WHERE id=%d;", id );
529+
break;
530+
default : QgsDebugMsg( "Invalid Style Entity indicated" );
531+
return;
532+
}
533+
if ( !runEmptyQuery( query ) )
534+
mErrorString = "Could not delete entity!";
535+
536+
}
537+
538+
bool QgsStyleV2::runEmptyQuery( char *query )
539+
{
540+
sqlite3 *db = openDB( mFileName );
541+
char *zErr = 0;
542+
if ( db == NULL )
543+
return false;
544+
int nErr = sqlite3_exec( db, query, NULL, NULL, &zErr );
545+
if ( nErr )
546+
return false;
547+
sqlite3_close( db );
548+
return true;
549+
}

src/core/symbology-ng/qgsstylev2.h

+15-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum TagTable { TagId, TagName };
4141
enum TagmapTable { TagmapTagId, TagmapSymbolId };
4242
enum ColorrampTable { ColorrampId, ColorrampName, ColorrampXML };
4343

44+
// Enums for types
45+
enum StyleEntity { SymbolEntity, GroupEntity, TagEntity, ColorrampEntity };
4446

4547
class CORE_EXPORT QgsStyleV2
4648
{
@@ -80,16 +82,21 @@ class CORE_EXPORT QgsStyleV2
8082
//! return a map of all groupid and names for the given parent
8183
//! Returns the First order groups when empty QString is passed
8284
QgsSymbolGroupMap groupNames( QString parent = "" );
83-
8485
//! returns the symbolnames of a given groupid
8586
QStringList symbolsOfGroup( int groupid );
86-
8787
//! returns the tags in the DB
8888
QgsSymbolTagMap symbolTags();
89-
9089
//! returns the symbol names with which have the given tag
9190
QStringList symbolsWithTag( int tagid );
91+
//! adds a new group and returns the group's id
92+
int addGroup( QString groupName, int parent = 0 );
93+
//! adds a new tag and returns the tag's id
94+
int addTag( QString tagName );
9295

96+
//! rename the given entity with the specified id
97+
void rename( StyleEntity type, int id, QString newName );
98+
//! remove the specified entity from the db
99+
void remove( StyleEntity type, int id );
93100

94101
//! add color ramp to style. takes ramp's ownership
95102
bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );
@@ -138,6 +145,11 @@ class CORE_EXPORT QgsStyleV2
138145

139146
//! Convinence function to open the DB and return a sqlite3 object
140147
sqlite3* openDB( QString filename );
148+
//! Convinence function that would run queries which donot generate return values
149+
//! it returns sucess result
150+
bool runEmptyQuery( char* query );
151+
//! prepares the complex query for removing a group,so that the children are not abandoned
152+
char* getGroupRemoveQuery( int id );
141153
};
142154

143155

src/gui/symbology-ng/qgsstylev2managerdialog.cpp

+138-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
8282
{
8383
groupTree->setExpanded( groupModel->indexFromItem( groupModel->item( i )), true );
8484
}
85-
connect( groupTree->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ), this, SLOT( groupChanged( const QModelIndex& ) ) );
85+
connect( groupTree->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
86+
this, SLOT( groupChanged( const QModelIndex& ) ) );
87+
connect( groupModel, SIGNAL( itemChanged( QStandardItem* ) ), this, SLOT( groupRenamed( QStandardItem* ) ) );
88+
89+
connect( btnAddGroup, SIGNAL( clicked() ), this, SLOT( addGroup() ) );
90+
connect( btnRemoveGroup, SIGNAL( clicked() ), this, SLOT( removeGroup() ) );
8691

8792
connect( tabItemType, SIGNAL( currentChanged( int ) ), this, SLOT( populateList() ) );
8893

@@ -578,25 +583,30 @@ void QgsStyleV2ManagerDialog::populateGroups()
578583
model->clear();
579584

580585
QStandardItem *allSymbols = new QStandardItem( "All Symbols" );
581-
allSymbols->setData( QVariant( "all" ) );
586+
allSymbols->setData( "all" );
587+
allSymbols->setEditable( false );
582588
model->appendRow( allSymbols );
583589

584590
QStandardItem *projectSymbols = new QStandardItem( "Project Symbols" );
585591
projectSymbols->setData( "project" );
592+
projectSymbols->setEditable( false );
586593
model->appendRow( projectSymbols );
587594

588595
QStandardItem *recent = new QStandardItem( "Recently Used" );
589-
recent->setData( QVariant( "recent" ) );
596+
recent->setData( "recent" );
597+
recent->setEditable( false );
590598
model->appendRow( recent );
591599

592600
QStandardItem *group = new QStandardItem( "" ); //require empty name to get first order groups
593-
group->setData( QVariant( "groups" ) );
601+
group->setData( "groups" );
602+
group->setEditable( false );
594603
buildGroupTree( group );
595604
group->setText( "Groups" );//set title later
596605
model->appendRow( group );
597606

598607
QStandardItem *tag = new QStandardItem( "Smart Groups" );
599-
tag->setData( QVariant( "tags" ) );
608+
tag->setData( "tags" );
609+
tag->setEditable( false );
600610
buildTagTree( tag );
601611
model->appendRow( tag );
602612

@@ -609,7 +619,7 @@ void QgsStyleV2ManagerDialog::buildGroupTree( QStandardItem* &parent )
609619
while ( i != groups.constEnd() )
610620
{
611621
QStandardItem *item = new QStandardItem( i.value() );
612-
item->setData( QVariant( i.key() ) );
622+
item->setData( i.key() );
613623
parent->appendRow( item );
614624
buildGroupTree( item );
615625
++i;
@@ -618,12 +628,12 @@ void QgsStyleV2ManagerDialog::buildGroupTree( QStandardItem* &parent )
618628

619629
void QgsStyleV2ManagerDialog::buildTagTree( QStandardItem* &parent )
620630
{
621-
QgsSymbolTagMap tags;
631+
QgsSymbolTagMap tags = mStyle->symbolTags();
622632
QgsSymbolTagMap::const_iterator i = tags.constBegin();
623633
while ( i != tags.constEnd() )
624634
{
625635
QStandardItem *item = new QStandardItem( i.value() );
626-
item->setData( QVariant( i.key() ) );
636+
item->setData( i.key() );
627637
parent->appendRow( item );
628638
++i;
629639
}
@@ -685,3 +695,123 @@ void QgsStyleV2ManagerDialog::groupChanged( const QModelIndex& index )
685695
}
686696
}
687697

698+
void QgsStyleV2ManagerDialog::addGroup()
699+
{
700+
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
701+
QModelIndex parentIndex = groupTree->currentIndex();
702+
703+
// Violation 1: Creating sub-groups of system defined groups
704+
QString parentData = parentIndex.data( Qt::UserRole + 1 ).toString();
705+
if ( parentData == "all" || parentData == "recent" || parentData == "project" )
706+
{
707+
int err = QMessageBox::critical( this, tr( "Invalid Selection" ),
708+
tr( "The parent group you have selected is not user editable.\n"
709+
"Kindly select a Group or Smart Group."));
710+
if ( err )
711+
return;
712+
}
713+
714+
// Violation 2: Creating a nested tag
715+
if ( parentIndex.parent().data( Qt::UserRole + 1 ).toString() == "tags" )
716+
{
717+
int err = QMessageBox::critical( this, tr( "Operation Not Allowed" ),
718+
tr( "Creation of nested Smart Groups are not allowed\n"
719+
"Select the 'Smart Group' to create a new group." ) );
720+
if ( err )
721+
return;
722+
}
723+
724+
// No violation
725+
QStandardItem *parentItem = model->itemFromIndex( parentIndex );
726+
QStandardItem *childItem = new QStandardItem( "New Group" );
727+
childItem->setData( QVariant( "newgroup" ) );
728+
parentItem->appendRow( childItem );
729+
730+
groupTree->setCurrentIndex( childItem->index() );
731+
groupTree->edit( childItem->index() );
732+
}
733+
734+
void QgsStyleV2ManagerDialog::removeGroup()
735+
{
736+
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
737+
QModelIndex index = groupTree->currentIndex();
738+
739+
// Violation: removing system groups
740+
QString data = index.data( Qt::UserRole + 1 ).toString();
741+
if ( data == "all" || data == "recent" || data == "project" || data == "groups" || data == "tags" )
742+
{
743+
int err = QMessageBox::critical( this, tr( "Invalid slection" ),
744+
tr( "Cannot delete system defined categories.\n"
745+
"Kindly select a group or smart group you might want to delete."));
746+
if ( err )
747+
return;
748+
}
749+
750+
QStandardItem *parentItem = model->itemFromIndex( index.parent() );
751+
if ( parentItem->data( Qt::UserRole + 1 ).toString() == "tags" )
752+
{
753+
mStyle->remove( TagEntity, index.data( Qt::UserRole + 1 ).toInt() );
754+
}
755+
else
756+
{
757+
mStyle->remove( GroupEntity, index.data( Qt::UserRole + 1 ).toInt() );
758+
QStandardItem *item = model->itemFromIndex( index );
759+
if ( item->hasChildren() )
760+
{
761+
QStandardItem *parent = item->parent();
762+
for( int i = 0; i < item->rowCount(); i++ )
763+
{
764+
parent->appendRow( item->takeChild( i ) );
765+
}
766+
}
767+
}
768+
parentItem->removeRow( index.row() );
769+
}
770+
771+
void QgsStyleV2ManagerDialog::groupRenamed( QStandardItem * item )
772+
{
773+
QString data = item->data( Qt::UserRole + 1 ).toString();
774+
QgsDebugMsg( "Symbol group edited: data=" + data + " text=" + item->text() );
775+
if ( data == "newgroup" )
776+
{
777+
int id;
778+
if ( item->parent()->data( Qt::UserRole + 1 ).toString() == "tags" )
779+
{
780+
id = mStyle->addTag( item->text() );
781+
}
782+
else if ( item->parent()->data( Qt::UserRole + 1 ).toString() == "groups" )
783+
{
784+
id = mStyle->addGroup( item->text() );
785+
}
786+
else
787+
{
788+
int parentid = item->parent()->data( Qt::UserRole + 1 ).toInt();
789+
id = mStyle->addGroup( item->text(), parentid );
790+
}
791+
if ( !id )
792+
{
793+
QMessageBox::critical( this, tr( "Error!" ),
794+
tr( "New group could not be created.\n"
795+
"There was a problem with your symbol database." ) );
796+
item->parent()->removeRow( item->row() );
797+
return;
798+
}
799+
else
800+
{
801+
item->setData( id );
802+
}
803+
}
804+
else
805+
{
806+
int id = item->data( Qt::UserRole + 1 ).toInt();
807+
QString name = item->text();
808+
if ( item->parent()->data( Qt::UserRole + 1 ) == "tags" )
809+
{
810+
mStyle->rename( TagEntity, id, name );
811+
}
812+
else
813+
{
814+
mStyle->rename( GroupEntity, id, name );
815+
}
816+
}
817+
}

src/gui/symbology-ng/qgsstylev2managerdialog.h

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
5151
void itemChanged( QStandardItem* item );
5252

5353
void groupChanged( const QModelIndex& );
54+
void groupRenamed( QStandardItem * );
55+
void addGroup();
56+
void removeGroup();
5457

5558
protected:
5659

0 commit comments

Comments
 (0)