Skip to content

Commit

Permalink
refresh the list properly
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmizuno committed Jan 8, 2018
1 parent 4eb4548 commit 3c82c09
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/app/qgsbookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )

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() );

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

void QgsBookmarks::exportToXml()
Expand Down Expand Up @@ -423,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 )
{
Expand All @@ -449,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() )
{
Expand Down Expand Up @@ -510,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( 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 @@ -626,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
Expand All @@ -682,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 );
Expand All @@ -694,7 +709,7 @@ bool QgsMergedBookmarksTableModel::removeRows( int row, int count, const QModelI
{
result = mProjectTableModel.removeRows( row - mQgisTableModel.rowCount(), count, parent );
}
endRemoveRows();
allLayoutChanged();
return result;
}

Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsbookmarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -155,6 +159,8 @@ class APP_EXPORT QgsBookmarks : public QgsDockWidget, private Ui::QgsBookmarksBa

void lstBookmarks_doubleClicked( const QModelIndex & );

void scrollToIndex( const QModelIndex & );

private:
QSqlTableModel *mQgisModel;
QgsProjectBookmarksTableModel *mProjectModel;
Expand Down

0 comments on commit 3c82c09

Please sign in to comment.