Skip to content
Permalink
Browse files

Merge pull request #6032 from stevenmizuno/followup_17673

Follow up #17673 Spatial Bookmarks fixes backport to 2.18
  • Loading branch information
elpaso committed Jan 11, 2018
2 parents 028bca4 + fe73791 commit 697509e889c7ea4de570777e8e3db80b139403ff
Showing with 77 additions and 40 deletions.
  1. +70 −39 src/app/qgsbookmarks.cpp
  2. +7 −1 src/app/qgsbookmarks.h
@@ -41,7 +41,7 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
: QgsDockWidget( parent )
, mQgisModel( nullptr )
, mProjectModel( nullptr )
, mModel( nullptr )
, mMergedModel( nullptr )
, mProxyModel( nullptr )
{
setupUi( this );
@@ -109,15 +109,20 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
mQgisModel->setHeaderData( 7, Qt::Horizontal, tr( "SRID" ) );

mProjectModel = new QgsProjectBookmarksTableModel( this );
mModel = new QgsMergedBookmarksTableModel( *mQgisModel, *mProjectModel, lstBookmarks, this );
mMergedModel = new QgsMergedBookmarksTableModel( *mQgisModel, *mProjectModel, lstBookmarks, this );

mProxyModel = new QgsBookmarksProxyModel( this );
mProxyModel->setSourceModel( mModel );
mProxyModel = new QgsBookmarksProxyModel( );
mProxyModel->setSourceModel( mMergedModel );
mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );

lstBookmarks->setModel( mProxyModel );
lstBookmarks->setItemDelegate( new QgsDoubleSpinBoxBookmarksDelegate( this ) );
lstBookmarks->setSortingEnabled( true );
lstBookmarks->sortByColumn( 1, Qt::AscendingOrder );

connect( mModel, SIGNAL( layoutChanged() ), mProxyModel, SLOT( _resetModel() ) );
connect( mMergedModel, SIGNAL( layoutChanged() ), mProxyModel, SLOT( _resetModel() ) );

connect( mMergedModel, SIGNAL( &QgsMergedBookmarksTableModel::selectItem( const QModelIndex &index ) ), this, SLOT( scrollToIndex( const QModelIndex &index ) ) );

QSettings settings;
lstBookmarks->header()->restoreState( settings.value( "Windows/Bookmarks/headerstate" ).toByteArray() );
@@ -130,6 +135,7 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
QgsBookmarks::~QgsBookmarks()
{
delete mQgisModel;
delete mProxyModel;
QSqlDatabase::removeDatabase( "bookmarks" );
saveWindowLocation();
}
@@ -149,7 +155,7 @@ void QgsBookmarks::saveWindowLocation()

void QgsBookmarks::addClicked()
{
Q_ASSERT( mModel );
Q_ASSERT( mMergedModel );
Q_ASSERT( mQgisModel );

QgsMapCanvas *canvas = QgisApp::instance()->mapCanvas();
@@ -182,7 +188,7 @@ void QgsBookmarks::addClicked()
{
mQgisModel->setSort( 0, Qt::AscendingOrder );
mQgisModel->select();
QModelIndex newIdx = mProxyModel->mapFromSource( mModel->index( mQgisModel->rowCount() - 1, 1 ) );
QModelIndex newIdx = mProxyModel->mapFromSource( mMergedModel->index( mQgisModel->rowCount() - 1, 1 ) );
// Edit new bookmark title
lstBookmarks->scrollTo( newIdx );
lstBookmarks->setCurrentIndex( newIdx );
@@ -198,16 +204,17 @@ void QgsBookmarks::addClicked()

void QgsBookmarks::deleteClicked()
{
QList<int> rows;
Q_FOREACH ( const QModelIndex &idx, lstBookmarks->selectionModel()->selectedIndexes() )
QItemSelection selection( mProxyModel->mapSelectionToSource( lstBookmarks->selectionModel()->selection() ) );
std::vector<int> rows;
Q_FOREACH ( const QModelIndex &idx, selection.indexes() )
{
if ( idx.column() == 1 )
{
rows << idx.row();
rows.push_back( idx.row() );
}
}

if ( rows.isEmpty() )
if ( rows.size() == 0 )
return;

// make sure the user really wants to delete these bookmarks
@@ -216,12 +223,14 @@ void QgsBookmarks::deleteClicked()
QMessageBox::Ok | QMessageBox::Cancel ) )
return;

int i = 0;
// Remove in reverse order to keep the merged model indexes
std::sort( rows.begin(), rows.end(), std::greater<int>() );

Q_FOREACH ( int row, rows )
{
mModel->removeRow( row - i );
i++;
mMergedModel->removeRow( row );
}
mProxyModel->_resetModel();
}

void QgsBookmarks::lstBookmarks_doubleClicked( const QModelIndex &index )
@@ -290,7 +299,7 @@ void QgsBookmarks::importFromXml()
QDomElement docElem = doc.documentElement();
QDomNodeList nodeList = docElem.elementsByTagName( "bookmark" );

Q_ASSERT( mModel );
Q_ASSERT( mMergedModel );

QString queries;

@@ -335,6 +344,7 @@ void QgsBookmarks::importFromXml()
}
mQgisModel->setSort( 0, Qt::AscendingOrder );
mQgisModel->select();
mProxyModel->_resetModel();
}

void QgsBookmarks::exportToXml()
@@ -359,8 +369,8 @@ void QgsBookmarks::exportToXml()
QDomElement root = doc.createElement( "qgis_bookmarks" );
doc.appendChild( root );

int rowCount = mModel->rowCount();
int colCount = mModel->columnCount() - 1; // exclude virtual "In project" column
int rowCount = mMergedModel->rowCount();
int colCount = mMergedModel->columnCount() - 1; // exclude virtual "In project" column

QList<QString> headerList;
headerList
@@ -379,12 +389,21 @@ void QgsBookmarks::exportToXml()
root.appendChild( bookmark );
for ( int j = 0; j < colCount; j++ )
{
QModelIndex idx = mModel->index( i, j );
QModelIndex idx = mMergedModel->index( i, j );
if ( idx.isValid() )
{
QString value = idx.data( Qt::DisplayRole ).toString();
QDomText idText = doc.createTextNode( value );
QString header = headerList.at( j );
// If it's the EPSG code, convert it to internal srid
if ( header == "sr_id" )
{
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsByOgcWmsCrs( value );
if ( crs.isValid() )
value = QString::number( crs.srsid() );
else
value = QString();
}
QDomText idText = doc.createTextNode( value );
QDomElement id = doc.createElement( header );
id.appendChild( idText );
bookmark.appendChild( id );
@@ -407,6 +426,13 @@ void QgsBookmarks::exportToXml()
settings.setValue( "Windows/Bookmarks/LastUsedDirectory", QFileInfo( fileName ).path() );
}

void QgsBookmarks::scrollToIndex( const QModelIndex & index )
{
QModelIndex proxyIndex( mProxyModel->mapFromSource( index ) );
lstBookmarks->scrollTo( proxyIndex );
lstBookmarks->setCurrentIndex( proxyIndex );
}

QgsProjectBookmarksTableModel::QgsProjectBookmarksTableModel( QObject *parent )
: QAbstractTableModel( parent )
{
@@ -433,8 +459,10 @@ int QgsProjectBookmarksTableModel::columnCount( const QModelIndex &parent ) cons

QVariant QgsProjectBookmarksTableModel::data( const QModelIndex& index, int role ) const
{
Q_UNUSED( role );
Q_ASSERT( role == Qt::DisplayRole );
if ( role != Qt::DisplayRole && role != Qt::EditRole )
{
return QVariant();
}

switch ( index.column() )
{
@@ -494,9 +522,11 @@ bool QgsProjectBookmarksTableModel::setData( const QModelIndex& index, const QVa
bool QgsProjectBookmarksTableModel::insertRows( int row, int count, const QModelIndex &parent )
{
Q_UNUSED( parent );
beginInsertRows( parent, row, row + count );

bool result = QgsProject::instance()->writeEntry( "Bookmarks" , "/count", QgsProject::instance()->readNumEntry( "Bookmarks", "/count" ) + count );
Q_UNUSED( row );
// append
int oldCount = QgsProject::instance()->readNumEntry( "Bookmarks", "/count" );
beginInsertRows( parent, oldCount, oldCount + count );
bool result = QgsProject::instance()->writeEntry( "Bookmarks", "/count", oldCount + count );
endInsertRows();
return result;
}
@@ -610,36 +640,38 @@ QVariant QgsMergedBookmarksTableModel::data( const QModelIndex &index, int role

bool QgsMergedBookmarksTableModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
bool result = false;
// last column triggers a move from QGIS to project bookmark
if ( index.column() == mQgisTableModel.columnCount() )
{
if ( index.row() < mQgisTableModel.rowCount() )
{
// Move from SQLite storage to project
moveBookmark( mQgisTableModel, mProjectTableModel, index.row() );
mTreeView->scrollTo( this->index( rowCount() - 1, 1 ) );
mTreeView->setCurrentIndex( this->index( rowCount() - 1, 1 ) );
mTreeView->selectionModel()->select( this->index( rowCount() - 1, 1 ), QItemSelectionModel::Rows );
emit selectItem( this->index( rowCount() - 1, 1 ) );
}
else
{
//Move from project to SQLite storage
moveBookmark( mProjectTableModel, mQgisTableModel, index.row() - mQgisTableModel.rowCount() );
mTreeView->scrollTo( this->index( mQgisTableModel.rowCount() - 1, 1 ) );
mTreeView->setCurrentIndex( this->index( mQgisTableModel.rowCount() - 1, 1 ) );
mTreeView->selectionModel()->select( this->index( mQgisTableModel.rowCount() - 1, 1 ), QItemSelectionModel::Rows );
emit selectItem( this->index( mQgisTableModel.rowCount() - 1, 1 ) );
}
return true;
}

if ( index.row() < mQgisTableModel.rowCount() )
{
return mQgisTableModel.setData( index, value, role );
result = true;
}
else
{
return mProjectTableModel.setData( this->index( index.row() - mQgisTableModel.rowCount(), index.column() ), value, role );
if ( index.row() < mQgisTableModel.rowCount() )
{
result = mQgisTableModel.setData( index, value, role );
}
else
{
result = mProjectTableModel.setData( this->index( index.row() - mQgisTableModel.rowCount(), index.column() ), value, role );
}
}
if ( result )
emit dataChanged( index, index );
return result;
}

Qt::ItemFlags QgsMergedBookmarksTableModel::flags( const QModelIndex &index ) const
@@ -666,7 +698,6 @@ bool QgsMergedBookmarksTableModel::removeRows( int row, int count, const QModelI
{
Q_ASSERT( count == 1 );
bool result;
beginRemoveRows( parent, row, row + count );
if ( row < mQgisTableModel.rowCount() )
{
QSqlTableModel *qgisModel = static_cast<QSqlTableModel *>( &mQgisTableModel );
@@ -678,7 +709,7 @@ bool QgsMergedBookmarksTableModel::removeRows( int row, int count, const QModelI
{
result = mProjectTableModel.removeRows( row - mQgisTableModel.rowCount(), count, parent );
}
endRemoveRows();
allLayoutChanged();
return result;
}

@@ -105,6 +105,10 @@ class QgsMergedBookmarksTableModel: public QAbstractTableModel
bool projectAvailable() const;
void moveBookmark( QAbstractTableModel& modelFrom, QAbstractTableModel& modelTo, int row );

signals:

void selectItem( const QModelIndex &index );

private slots:
void allLayoutChanged()
{
@@ -155,10 +159,12 @@ class APP_EXPORT QgsBookmarks : public QgsDockWidget, private Ui::QgsBookmarksBa

void lstBookmarks_doubleClicked( const QModelIndex & );

void scrollToIndex( const QModelIndex & );

private:
QSqlTableModel *mQgisModel;
QgsProjectBookmarksTableModel *mProjectModel;
QgsMergedBookmarksTableModel *mModel;
QgsMergedBookmarksTableModel *mMergedModel;
QgsBookmarksProxyModel *mProxyModel;

void saveWindowLocation();

0 comments on commit 697509e

Please sign in to comment.
You can’t perform that action at this time.