Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[BACKPORT] Api update - added signlas for legend events and made them…
… available to plugin api. Patch courtesy of Etienne Tourigny <etourigny.dev@gmail.com> and closes pull request 107
  • Loading branch information
timlinux committed Mar 31, 2012
1 parent 98bc763 commit 58ebe86
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/app/legend/qgsapplegendinterface.cpp
Expand Up @@ -24,7 +24,12 @@
QgsAppLegendInterface::QgsAppLegendInterface( QgsLegend * legend )
: mLegend( legend )
{
connect( legend, SIGNAL( itemAdded( QModelIndex ) ), this, SIGNAL( itemAdded( QModelIndex ) ) );
connect( legend, SIGNAL( itemMoved( QModelIndex, QModelIndex ) ), this, SLOT( updateIndex( QModelIndex, QModelIndex ) ) );
connect( legend, SIGNAL( itemMoved( QModelIndex, QModelIndex ) ), this, SIGNAL( groupRelationsChanged( ) ) );
connect( legend, SIGNAL( itemMovedGroup( QgsLegendItem *, int ) ), this, SIGNAL( groupRelationsChanged() ) );
// connect( legend, SIGNAL( itemChanged( QTreeWidgetItem*, int ) ), this, SIGNAL( groupRelationsChanged() ) );
connect( legend, SIGNAL( itemRemoved() ), this, SIGNAL( itemRemoved() ) );
}

QgsAppLegendInterface::~QgsAppLegendInterface()
Expand Down
79 changes: 72 additions & 7 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -45,6 +45,34 @@

const int AUTOSCROLL_MARGIN = 16;

// This function finds a unique group name [prefix1, prefix2, ...] by adding an
// incremental integer to prefix. It is necessary because group names are the
// only way of identifying groups in QgsLegendInterface.
// Could add a "parent" argument and use that instead, or pass it as prefix
static QString getUniqueGroupName( QString prefix, QStringList groups )
{
QString suffix;
if ( groups.size() == 0 )
{
suffix = "1";
}
else
{
// get a list of strings that match prefix, and keep the suffix
QStringList match = groups.filter( QRegExp( QString( "^" + prefix ) ) );
match.replaceInStrings( prefix, QString( "" ) );
// find the maximum
int max = 0;
foreach( QString m, match )
{
if ( m.toInt() > max )
max = m.toInt();
}
suffix = QString( "%1" ).arg( max + 1 );
}
return prefix + suffix;
}

QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
: QTreeWidget( parent )
, mMousePressedFlag( false )
Expand Down Expand Up @@ -162,19 +190,22 @@ int QgsLegend::addGroup( QString name, bool expand, QTreeWidgetItem* parent )
blockSignals( true );

bool nameEmpty = name.isEmpty();
if ( nameEmpty )
name = tr( "group" ); // some default name if none specified

QgsLegendGroup *parentGroup = dynamic_cast<QgsLegendGroup *>( parent );
QgsLegendGroup *group;

if ( parentGroup )
{
if ( nameEmpty )
name = getUniqueGroupName( tr( "sub-group" ), groups() );
group = new QgsLegendGroup( parentGroup, name );
}
else
{
if ( nameEmpty )
name = getUniqueGroupName( tr( "group" ), groups() );
group = new QgsLegendGroup( this, name );

if ( currentItem() )
{
moveItem( group, currentItem() );
Expand All @@ -188,6 +219,9 @@ int QgsLegend::addGroup( QString name, bool expand, QTreeWidgetItem* parent )
openEditor();

blockSignals( false );

emit itemAdded( groupIndex );

return groupIndex.row();
}

Expand Down Expand Up @@ -274,6 +308,7 @@ void QgsLegend::removeLayer( QString layerId )
updateMapCanvasLayerSet();
adjustIconSize();

emit itemRemoved();
if ( invLayerRemoved )
emit invisibleLayerRemoved();
}
Expand Down Expand Up @@ -617,8 +652,8 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
theMenu.addAction( QgisApp::getThemeIcon( "/mActionZoomToLayer.png" ),
tr( "Zoom to Group" ), this, SLOT( legendLayerZoom() ) );

theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemoveLayer.png" ),
tr( "&Remove" ), this, SLOT( legendGroupRemove() ) );
// use QGisApp::removeLayer() to remove all selected layers+groups
theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemoveLayer.png" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );

theMenu.addAction( QgisApp::getThemeIcon( "/mActionSetCRS.png" ),
tr( "&Set Group CRS" ), this, SLOT( legendGroupSetCRS() ) );
Expand All @@ -628,6 +663,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
{
theMenu.addAction( tr( "Re&name" ), this, SLOT( openEditor() ) );
}

//
// Option to group layers, if the selection is more than one
//
Expand Down Expand Up @@ -868,6 +904,8 @@ void QgsLegend::addLayer( QgsMapLayer * layer )
}
//make the QTreeWidget item up-to-date
doItemsLayout();

emit itemAdded( indexFromItem( llayer ) );
}

void QgsLegend::setLayerVisible( QgsMapLayer * layer, bool visible )
Expand Down Expand Up @@ -1099,6 +1137,8 @@ void QgsLegend::removeGroup( QgsLegendGroup *lg )

delete lg;

emit itemRemoved();

adjustIconSize();
}

Expand Down Expand Up @@ -1132,6 +1172,8 @@ void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
return;

insertItem( layer, group );

emit itemMovedGroup( dynamic_cast<QgsLegendItem*>( layer ), groupIndex );
}

void QgsLegend::legendLayerShowInOverview()
Expand Down Expand Up @@ -1766,11 +1808,14 @@ void QgsLegend::insertItem( QTreeWidgetItem* move, QTreeWidgetItem* into )
}
intoItem->receive( movedItem );
movedItem->restoreAppearanceSettings();//apply the settings again
emit itemMovedGroup( movedItem, indexFromItem( intoItem ).row() );
}
}

void QgsLegend::moveItem( QTreeWidgetItem* move, QTreeWidgetItem* after )
{
QModelIndex oldIndex = indexFromItem( move );

QgsDebugMsgLevel( QString( "Moving layer : %1 (%2)" ).arg( move->text( 0 ) ).arg( move->type() ), 3 );
if ( after )
{
Expand Down Expand Up @@ -1809,6 +1854,8 @@ void QgsLegend::moveItem( QTreeWidgetItem* move, QTreeWidgetItem* after )
}

static_cast<QgsLegendItem*>( move )->restoreAppearanceSettings();//apply the settings again

emit itemMoved( oldIndex, indexFromItem( move ) );
}

void QgsLegend::removeItem( QTreeWidgetItem* item )
Expand Down Expand Up @@ -2435,24 +2482,42 @@ void QgsLegend::groupSelectedLayers()

if ( parent )
{
group = new QgsLegendGroup( parent, tr( "sub-group" ) );
group = new QgsLegendGroup( parent,
getUniqueGroupName( tr( "sub-group" ), groups() ) );
}
else
{
group = new QgsLegendGroup( this, tr( "group" ) );
group = new QgsLegendGroup( this,
getUniqueGroupName( tr( "group" ), groups() ) );
}

// save old indexes so we can notify changes
QList< QModelIndex > oldIndexes;
QList< QTreeWidgetItem* > selected;

foreach( QTreeWidgetItem * item, selectedItems() )
{
QgsLegendLayer* layer = dynamic_cast<QgsLegendLayer *>( item );
if ( layer )
{
insertItem( item, group );
oldIndexes.append( indexFromItem( item ) );
selected.append( item );
}
}
foreach( QTreeWidgetItem * item, selected )
{
insertItem( item, group );
}

editItem( group, 0 );

blockSignals( false );

// notify that group was added and that items were moved
emit itemAdded( indexFromItem( group ) );
for ( int i = 0; i < selected.size(); i++ )
{
emit itemMoved( oldIndexes[i], indexFromItem( selected[i] ) );
}
}

3 changes: 3 additions & 0 deletions src/app/legend/qgslegend.h
Expand Up @@ -515,7 +515,10 @@ class QgsLegend : public QTreeWidget
void updateGroupCheckStates( QTreeWidgetItem *item );

signals:
void itemAdded( QModelIndex index );
void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );
void itemMovedGroup( QgsLegendItem *item, int newGroupIndex ); // should we add oldGroup?
void itemRemoved( ); // should we add an argument?

void zOrderChanged();
void invisibleLayerRemoved();
Expand Down
21 changes: 10 additions & 11 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -252,7 +252,7 @@ bool QgsCoordinateReferenceSystem::loadFromDb( QString db, QString expression, Q
is_geo integer NOT NULL);
*/

QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name||':'||auth_id,is_geo from tbl_srs where " + expression + "=" + quotedValue( value );
QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name||':'||auth_id,is_geo from tbl_srs where " + expression + "=" + quotedValue( value ) + " order by deprecated";
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
// XXX Need to free memory from the error msg if one is set
if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
Expand Down Expand Up @@ -435,7 +435,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
* - if the above does not match perform a whole text search on proj4 string (if not null)
*/
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4String.trimmed() ) );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4String.trimmed() ) + " order by deprecated" );
if ( myRecord.empty() )
{
// Ticket #722 - aaronr
Expand Down Expand Up @@ -470,7 +470,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
myStart2 = myLat2RegExp.indexIn( theProj4String, myStart2 );
theProj4StringModified.replace( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN, lat1Str );
QgsDebugMsg( "trying proj4string match with swapped lat_1,lat_2" );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4StringModified.trimmed() ) );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4StringModified.trimmed() ) + " order by deprecated" );
}
}

Expand Down Expand Up @@ -503,13 +503,13 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String

if ( !datum.isEmpty() )
{
myRecord = getRecord( sql + delim + datum );
myRecord = getRecord( sql + delim + datum + " order by deprecated" );
}

if ( myRecord.empty() )
{
// datum might have disappeared in definition - retry without it
myRecord = getRecord( sql );
myRecord = getRecord( sql + " order by deprecated" );
}
}

Expand Down Expand Up @@ -549,7 +549,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
if ( mIsValidFlag )
{
// but the proj.4 parsed string might already be in our database
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( toProj4() ) );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( toProj4() ) + " order by deprecated" );
if ( myRecord.empty() )
{
// It's not, so try to add it
Expand All @@ -559,7 +559,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
if ( mIsValidFlag )
{
// but validate that it's there afterwards
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( toProj4() ) );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( toProj4() ) + " order by deprecated" );
}
}

Expand Down Expand Up @@ -913,7 +913,7 @@ long QgsCoordinateReferenceSystem::findMatchingProj()
int myResult;

// Set up the query to retrieve the projection information needed to populate the list
QString mySql = QString( "select srs_id,parameters from tbl_srs where projection_acronym=%1 and ellipsoid_acronym=%2" )
QString mySql = QString( "select srs_id,parameters from tbl_srs where projection_acronym=%1 and ellipsoid_acronym=%2 order by deprecated" )
.arg( quotedValue( mProjectionAcronym ) )
.arg( quotedValue( mEllipsoidAcronym ) );
// Get the full path name to the sqlite3 spatial reference database.
Expand Down Expand Up @@ -1186,8 +1186,7 @@ QString QgsCoordinateReferenceSystem::proj4FromSrsId( const int theSrsId )

QString myDatabaseFileName;
QString myProjString;
QString mySql = "select parameters from tbl_srs where srs_id = ";
mySql += QString::number( theSrsId );
QString mySql = QString( "select parameters from tbl_srs where srs_id = %1 order by deprecated" ).arg( theSrsId );

QgsDebugMsg( "mySrsId = " + QString::number( theSrsId ) );
QgsDebugMsg( "USER_CRS_START_ID = " + QString::number( USER_CRS_START_ID ) );
Expand Down Expand Up @@ -1417,7 +1416,7 @@ int QgsCoordinateReferenceSystem::syncDb()

const char *tail;
sqlite3_stmt *select;
QString sql = "select auth_name,auth_id,parameters from tbl_srs WHERE auth_name IS NOT NULL AND auth_id IS NOT NULL";
QString sql = "select auth_name,auth_id,parameters from tbl_srs WHERE auth_name IS NOT NULL AND auth_id IS NOT NULL order by deprecated";
if ( sqlite3_prepare( database, sql.toAscii(), sql.size(), &select, &tail ) != SQLITE_OK )
{
qCritical( "Could not prepare: %s [%s]\n", sql.toAscii().constData(), sqlite3_errmsg( database ) );
Expand Down
11 changes: 11 additions & 0 deletions src/gui/qgslegendinterface.h
Expand Up @@ -20,6 +20,7 @@
#include <QObject>
#include <QPair>
#include <QStringList>
#include <QModelIndex>

class QgsMapLayer;
class QTreeWidgetItem;
Expand Down Expand Up @@ -74,9 +75,19 @@ class GUI_EXPORT QgsLegendInterface : public QObject
virtual bool isLayerVisible( QgsMapLayer * ml ) = 0;

signals:

//! emitted when a group index has changed
void groupIndexChanged( int oldIndex, int newIndex );

/* //! emitted when group relations have changed */
void groupRelationsChanged( );

/* //! emitted when an item (group/layer) is added */
void itemAdded( QModelIndex index );

/* //! emitted when an item (group/layer) is removed */
void itemRemoved( );

public slots:

//! Add a new group
Expand Down

0 comments on commit 58ebe86

Please sign in to comment.