Skip to content

Commit

Permalink
[bugfix] Bookmarks fix deleting multiple items
Browse files Browse the repository at this point in the history
Also fix sorting enabled.
  • Loading branch information
elpaso committed Dec 12, 2017
1 parent d0a8891 commit 78554dd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 40 deletions.
92 changes: 53 additions & 39 deletions src/app/qgsbookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,24 @@ 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->setSourceModel( mMergedModel );

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

connect( mModel, &QgsMergedBookmarksTableModel::layoutChanged, mProxyModel, &QgsBookmarksProxyModel::_resetModel );
connect( mMergedModel, &QgsMergedBookmarksTableModel::selectItem, this, [ = ]( const QModelIndex & index )
{
QModelIndex proxyIndex( mProxyModel->mapFromSource( index ) );
lstBookmarks->scrollTo( proxyIndex );
lstBookmarks->setCurrentIndex( proxyIndex );
} );

connect( mMergedModel, &QgsMergedBookmarksTableModel::layoutChanged, mProxyModel, &QgsBookmarksProxyModel::_resetModel );

QgsSettings settings;
lstBookmarks->header()->restoreState( settings.value( QStringLiteral( "Windows/Bookmarks/headerstate" ) ).toByteArray() );
Expand Down Expand Up @@ -143,7 +152,7 @@ void QgsBookmarks::saveWindowLocation()

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

QgsMapCanvas *canvas = QgisApp::instance()->mapCanvas();
Expand Down Expand Up @@ -176,7 +185,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 );
Expand All @@ -192,16 +201,15 @@ 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;
for ( const auto &selectedIdx : selection.indexes() )
{
if ( idx.column() == 1 )
{
rows << idx.row();
}
if ( selectedIdx.column() == 1 )
rows.push_back( selectedIdx.row() );
}

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

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

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

for ( const auto &row : rows )
{
mModel->removeRow( row - i );
i++;
mMergedModel->removeRow( row );
}
mProxyModel->_resetModel();
}

void QgsBookmarks::lstBookmarks_doubleClicked( const QModelIndex &index )
Expand Down Expand Up @@ -285,7 +295,7 @@ void QgsBookmarks::importFromXml()
QDomElement docElem = doc.documentElement();
QDomNodeList nodeList = docElem.elementsByTagName( QStringLiteral( "bookmark" ) );

Q_ASSERT( mModel );
Q_ASSERT( mMergedModel );

QString queries;

Expand Down Expand Up @@ -354,8 +364,8 @@ void QgsBookmarks::exportToXml()
QDomElement root = doc.createElement( QStringLiteral( "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
Expand All @@ -374,7 +384,7 @@ 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();
Expand Down Expand Up @@ -489,9 +499,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( QStringLiteral( "Bookmarks" ), QStringLiteral( "/count" ), QgsProject::instance()->readNumEntry( QStringLiteral( "Bookmarks" ), QStringLiteral( "/count" ) ) + count );
Q_UNUSED( row );
// append
int oldCount = QgsProject::instance()->readNumEntry( QStringLiteral( "Bookmarks" ), QStringLiteral( "/count" ) );
beginInsertRows( parent, oldCount, oldCount + count );
bool result = QgsProject::instance()->writeEntry( QStringLiteral( "Bookmarks" ), QStringLiteral( "/count" ), oldCount + count );
endInsertRows();
return result;
}
Expand Down Expand Up @@ -603,36 +615,39 @@ 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
Expand All @@ -659,19 +674,18 @@ 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 );
Q_ASSERT( qgisModel );
result = qgisModel->removeRows( row, count, parent );
qgisModel->select();
qgisModel->select(); // This updates row count in the model!
}
else
{
result = mProjectTableModel.removeRows( row - mQgisTableModel.rowCount(), count, parent );
}
endRemoveRows();
allLayoutChanged();
return result;
}

Expand Down
6 changes: 5 additions & 1 deletion src/app/qgsbookmarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,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()
{
Expand Down Expand Up @@ -156,7 +160,7 @@ class APP_EXPORT QgsBookmarks : public QgsDockWidget, private Ui::QgsBookmarksBa
private:
QSqlTableModel *mQgisModel = nullptr;
QgsProjectBookmarksTableModel *mProjectModel = nullptr;
QgsMergedBookmarksTableModel *mModel = nullptr;
QgsMergedBookmarksTableModel *mMergedModel = nullptr;
QgsBookmarksProxyModel *mProxyModel = nullptr;

void saveWindowLocation();
Expand Down

0 comments on commit 78554dd

Please sign in to comment.