Skip to content

Commit

Permalink
Adding and removing of groups using style manager done
Browse files Browse the repository at this point in the history
  • Loading branch information
Arunmozhi committed Jul 9, 2012
1 parent 64b8019 commit 16a6909
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 11 deletions.
127 changes: 127 additions & 0 deletions src/core/symbology-ng/qgsstylev2.cpp
Expand Up @@ -420,3 +420,130 @@ QStringList QgsStyleV2::symbolsWithTag( int tagid )

return symbols;
}

int QgsStyleV2::addGroup( QString groupName, int parentid )
{
sqlite3 *db = openDB( mFileName );
if ( db == NULL )
return 0;
sqlite3_stmt *ppStmt;

char *query;
QByteArray groupArray = groupName.toUtf8();
if ( parentid == 0 )
{
query = sqlite3_mprintf( "INSERT INTO symgroup VALUES (NULL, '%q', NULL);", groupArray.constData() );
}
else
{
query = sqlite3_mprintf( "INSERT INTO symgroup VALUES (NULL, '%q', %d);", groupArray.constData(), parentid );
}
int nErr = sqlite3_prepare_v2( db, query, -1, &ppStmt, NULL );
if ( nErr == SQLITE_OK )
sqlite3_step( ppStmt );
sqlite3_finalize( ppStmt );

int groupid = (int)sqlite3_last_insert_rowid( db );
sqlite3_close( db );
return groupid;
}

int QgsStyleV2::addTag( QString tagname )
{
sqlite3 *db = openDB( mFileName );
if ( db == NULL )
return 0;
sqlite3_stmt *ppStmt;

QByteArray tagArray = tagname.toUtf8();
char *query = sqlite3_mprintf( "INSERT INTO tag VALUES (NULL, '%q');", tagArray.constData() );
int nErr = sqlite3_prepare_v2( db, query, -1, &ppStmt, NULL );
if ( nErr == SQLITE_OK )
sqlite3_step( ppStmt );
sqlite3_finalize( ppStmt );

int tagid = (int)sqlite3_last_insert_rowid( db );
sqlite3_close( db );
return tagid;
}

void QgsStyleV2::rename( StyleEntity type, int id, QString newName )
{
QByteArray nameArray = newName.toUtf8();
char *query;
switch ( type )
{
case SymbolEntity : query = sqlite3_mprintf( "UPDATE symbol SET name='%q' WHERE id=%d;", nameArray.constData(), id );
break;
case GroupEntity : query = sqlite3_mprintf( "UPDATE symgroup SET name='%q' WHERE id=%d;", nameArray.constData(), id );
break;
case TagEntity : query = sqlite3_mprintf( "UPDATE tag SET name='%q' WHERE id=%d;", nameArray.constData(), id );
break;
case ColorrampEntity : query = sqlite3_mprintf( "UPDATE colorramp SET name='%q' WHERE id=%d;", nameArray.constData(), id );
break;
default : QgsDebugMsg( "Invalid Style Entity indicated" );
return;
}
if ( !runEmptyQuery( query ) )
mErrorString = "Could not rename!";
}

char* QgsStyleV2::getGroupRemoveQuery( int id )
{
int parentid;
sqlite3 * db = openDB( mFileName );
char *query = sqlite3_mprintf( "SELECT parent FROM symgroup WHERE id=%d;", id );
sqlite3_stmt *ppStmt;
int err = sqlite3_prepare_v2( db, query, -1, &ppStmt, NULL );
if ( err == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
parentid = sqlite3_column_int( ppStmt, 0 );
sqlite3_finalize( ppStmt );
sqlite3_close( db );
if ( parentid )
{
query = sqlite3_mprintf( "UPDATE symbol SET groupid=%d WHERE groupid=%d;"
"UPDATE symgroup SET parent=%d WHERE parent=%d;"
"DELETE FROM symgroup WHERE id=%d;", parentid, id, parentid, id, id );
}
else
{
query = sqlite3_mprintf( "UPDATE symbol SET groupid=NULL WHERE groupid=%d;"
"UPDATE symgroup SET parent=NULL WHERE parent=%d;"
"DELETE FROM symgroup WHERE id=%d;", id, id, id );
}
return query;
}

void QgsStyleV2::remove( StyleEntity type, int id )
{
char *query;
switch ( type )
{
case SymbolEntity : query = sqlite3_mprintf( "DELETE FROM symbol WHERE id=%d; DELETE FROM tagmap WHERE symbol_id=%d;", id, id );
break;
case GroupEntity : query = getGroupRemoveQuery( id );
break;
case TagEntity : query = sqlite3_mprintf( "DELETE FROM tag WHERE id=%d; DELETE FROM tagmap WHERE tag_id=%d;", id, id );
break;
case ColorrampEntity : query = sqlite3_mprintf( "DELETE FROM colorramp WHERE id=%d;", id );
break;
default : QgsDebugMsg( "Invalid Style Entity indicated" );
return;
}
if ( !runEmptyQuery( query ) )
mErrorString = "Could not delete entity!";

}

bool QgsStyleV2::runEmptyQuery( char *query )
{
sqlite3 *db = openDB( mFileName );
char *zErr = 0;
if ( db == NULL )
return false;
int nErr = sqlite3_exec( db, query, NULL, NULL, &zErr );
if ( nErr )
return false;
sqlite3_close( db );
return true;
}
18 changes: 15 additions & 3 deletions src/core/symbology-ng/qgsstylev2.h
Expand Up @@ -41,6 +41,8 @@ enum TagTable { TagId, TagName };
enum TagmapTable { TagmapTagId, TagmapSymbolId };
enum ColorrampTable { ColorrampId, ColorrampName, ColorrampXML };

// Enums for types
enum StyleEntity { SymbolEntity, GroupEntity, TagEntity, ColorrampEntity };

class CORE_EXPORT QgsStyleV2
{
Expand Down Expand Up @@ -80,16 +82,21 @@ class CORE_EXPORT QgsStyleV2
//! return a map of all groupid and names for the given parent
//! Returns the First order groups when empty QString is passed
QgsSymbolGroupMap groupNames( QString parent = "" );

//! returns the symbolnames of a given groupid
QStringList symbolsOfGroup( int groupid );

//! returns the tags in the DB
QgsSymbolTagMap symbolTags();

//! returns the symbol names with which have the given tag
QStringList symbolsWithTag( int tagid );
//! adds a new group and returns the group's id
int addGroup( QString groupName, int parent = 0 );
//! adds a new tag and returns the tag's id
int addTag( QString tagName );

//! rename the given entity with the specified id
void rename( StyleEntity type, int id, QString newName );
//! remove the specified entity from the db
void remove( StyleEntity type, int id );

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

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


Expand Down
146 changes: 138 additions & 8 deletions src/gui/symbology-ng/qgsstylev2managerdialog.cpp
Expand Up @@ -82,7 +82,12 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
{
groupTree->setExpanded( groupModel->indexFromItem( groupModel->item( i )), true );
}
connect( groupTree->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ), this, SLOT( groupChanged( const QModelIndex& ) ) );
connect( groupTree->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
this, SLOT( groupChanged( const QModelIndex& ) ) );
connect( groupModel, SIGNAL( itemChanged( QStandardItem* ) ), this, SLOT( groupRenamed( QStandardItem* ) ) );

connect( btnAddGroup, SIGNAL( clicked() ), this, SLOT( addGroup() ) );
connect( btnRemoveGroup, SIGNAL( clicked() ), this, SLOT( removeGroup() ) );

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

Expand Down Expand Up @@ -578,25 +583,30 @@ void QgsStyleV2ManagerDialog::populateGroups()
model->clear();

QStandardItem *allSymbols = new QStandardItem( "All Symbols" );
allSymbols->setData( QVariant( "all" ) );
allSymbols->setData( "all" );
allSymbols->setEditable( false );
model->appendRow( allSymbols );

QStandardItem *projectSymbols = new QStandardItem( "Project Symbols" );
projectSymbols->setData( "project" );
projectSymbols->setEditable( false );
model->appendRow( projectSymbols );

QStandardItem *recent = new QStandardItem( "Recently Used" );
recent->setData( QVariant( "recent" ) );
recent->setData( "recent" );
recent->setEditable( false );
model->appendRow( recent );

QStandardItem *group = new QStandardItem( "" ); //require empty name to get first order groups
group->setData( QVariant( "groups" ) );
group->setData( "groups" );
group->setEditable( false );
buildGroupTree( group );
group->setText( "Groups" );//set title later
model->appendRow( group );

QStandardItem *tag = new QStandardItem( "Smart Groups" );
tag->setData( QVariant( "tags" ) );
tag->setData( "tags" );
tag->setEditable( false );
buildTagTree( tag );
model->appendRow( tag );

Expand All @@ -609,7 +619,7 @@ void QgsStyleV2ManagerDialog::buildGroupTree( QStandardItem* &parent )
while ( i != groups.constEnd() )
{
QStandardItem *item = new QStandardItem( i.value() );
item->setData( QVariant( i.key() ) );
item->setData( i.key() );
parent->appendRow( item );
buildGroupTree( item );
++i;
Expand All @@ -618,12 +628,12 @@ void QgsStyleV2ManagerDialog::buildGroupTree( QStandardItem* &parent )

void QgsStyleV2ManagerDialog::buildTagTree( QStandardItem* &parent )
{
QgsSymbolTagMap tags;
QgsSymbolTagMap tags = mStyle->symbolTags();
QgsSymbolTagMap::const_iterator i = tags.constBegin();
while ( i != tags.constEnd() )
{
QStandardItem *item = new QStandardItem( i.value() );
item->setData( QVariant( i.key() ) );
item->setData( i.key() );
parent->appendRow( item );
++i;
}
Expand Down Expand Up @@ -685,3 +695,123 @@ void QgsStyleV2ManagerDialog::groupChanged( const QModelIndex& index )
}
}

void QgsStyleV2ManagerDialog::addGroup()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
QModelIndex parentIndex = groupTree->currentIndex();

// Violation 1: Creating sub-groups of system defined groups
QString parentData = parentIndex.data( Qt::UserRole + 1 ).toString();
if ( parentData == "all" || parentData == "recent" || parentData == "project" )
{
int err = QMessageBox::critical( this, tr( "Invalid Selection" ),
tr( "The parent group you have selected is not user editable.\n"
"Kindly select a Group or Smart Group."));
if ( err )
return;
}

// Violation 2: Creating a nested tag
if ( parentIndex.parent().data( Qt::UserRole + 1 ).toString() == "tags" )
{
int err = QMessageBox::critical( this, tr( "Operation Not Allowed" ),
tr( "Creation of nested Smart Groups are not allowed\n"
"Select the 'Smart Group' to create a new group." ) );
if ( err )
return;
}

// No violation
QStandardItem *parentItem = model->itemFromIndex( parentIndex );
QStandardItem *childItem = new QStandardItem( "New Group" );
childItem->setData( QVariant( "newgroup" ) );
parentItem->appendRow( childItem );

groupTree->setCurrentIndex( childItem->index() );
groupTree->edit( childItem->index() );
}

void QgsStyleV2ManagerDialog::removeGroup()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
QModelIndex index = groupTree->currentIndex();

// Violation: removing system groups
QString data = index.data( Qt::UserRole + 1 ).toString();
if ( data == "all" || data == "recent" || data == "project" || data == "groups" || data == "tags" )
{
int err = QMessageBox::critical( this, tr( "Invalid slection" ),
tr( "Cannot delete system defined categories.\n"
"Kindly select a group or smart group you might want to delete."));
if ( err )
return;
}

QStandardItem *parentItem = model->itemFromIndex( index.parent() );
if ( parentItem->data( Qt::UserRole + 1 ).toString() == "tags" )
{
mStyle->remove( TagEntity, index.data( Qt::UserRole + 1 ).toInt() );
}
else
{
mStyle->remove( GroupEntity, index.data( Qt::UserRole + 1 ).toInt() );
QStandardItem *item = model->itemFromIndex( index );
if ( item->hasChildren() )
{
QStandardItem *parent = item->parent();
for( int i = 0; i < item->rowCount(); i++ )
{
parent->appendRow( item->takeChild( i ) );
}
}
}
parentItem->removeRow( index.row() );
}

void QgsStyleV2ManagerDialog::groupRenamed( QStandardItem * item )
{
QString data = item->data( Qt::UserRole + 1 ).toString();
QgsDebugMsg( "Symbol group edited: data=" + data + " text=" + item->text() );
if ( data == "newgroup" )
{
int id;
if ( item->parent()->data( Qt::UserRole + 1 ).toString() == "tags" )
{
id = mStyle->addTag( item->text() );
}
else if ( item->parent()->data( Qt::UserRole + 1 ).toString() == "groups" )
{
id = mStyle->addGroup( item->text() );
}
else
{
int parentid = item->parent()->data( Qt::UserRole + 1 ).toInt();
id = mStyle->addGroup( item->text(), parentid );
}
if ( !id )
{
QMessageBox::critical( this, tr( "Error!" ),
tr( "New group could not be created.\n"
"There was a problem with your symbol database." ) );
item->parent()->removeRow( item->row() );
return;
}
else
{
item->setData( id );
}
}
else
{
int id = item->data( Qt::UserRole + 1 ).toInt();
QString name = item->text();
if ( item->parent()->data( Qt::UserRole + 1 ) == "tags" )
{
mStyle->rename( TagEntity, id, name );
}
else
{
mStyle->rename( GroupEntity, id, name );
}
}
}
3 changes: 3 additions & 0 deletions src/gui/symbology-ng/qgsstylev2managerdialog.h
Expand Up @@ -51,6 +51,9 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
void itemChanged( QStandardItem* item );

void groupChanged( const QModelIndex& );
void groupRenamed( QStandardItem * );
void addGroup();
void removeGroup();

protected:

Expand Down

0 comments on commit 16a6909

Please sign in to comment.