@@ -103,15 +103,24 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
103
103
mQgisModel ->setHeaderData ( 7 , Qt::Horizontal, tr ( " SRID" ) );
104
104
105
105
mProjectModel = new QgsProjectBookmarksTableModel ( this );
106
- mModel = new QgsMergedBookmarksTableModel ( *mQgisModel , *mProjectModel , lstBookmarks, this );
106
+ mMergedModel = new QgsMergedBookmarksTableModel ( *mQgisModel , *mProjectModel , lstBookmarks, this );
107
107
108
108
mProxyModel = new QgsBookmarksProxyModel ( this );
109
- mProxyModel ->setSourceModel ( mModel );
109
+ mProxyModel ->setSourceModel ( mMergedModel );
110
110
111
111
lstBookmarks->setModel ( mProxyModel );
112
112
lstBookmarks->setItemDelegate ( new QgsDoubleSpinBoxBookmarksDelegate ( this ) );
113
+ lstBookmarks->setSortingEnabled ( true );
114
+ lstBookmarks->sortByColumn ( 1 , Qt::AscendingOrder );
113
115
114
- connect ( mModel , &QgsMergedBookmarksTableModel::layoutChanged, mProxyModel , &QgsBookmarksProxyModel::_resetModel );
116
+ connect ( mMergedModel , &QgsMergedBookmarksTableModel::selectItem, this , [ = ]( const QModelIndex & index )
117
+ {
118
+ QModelIndex proxyIndex ( mProxyModel ->mapFromSource ( index ) );
119
+ lstBookmarks->scrollTo ( proxyIndex );
120
+ lstBookmarks->setCurrentIndex ( proxyIndex );
121
+ } );
122
+
123
+ connect ( mMergedModel , &QgsMergedBookmarksTableModel::layoutChanged, mProxyModel , &QgsBookmarksProxyModel::_resetModel );
115
124
116
125
QgsSettings settings;
117
126
lstBookmarks->header ()->restoreState ( settings.value ( QStringLiteral ( " Windows/Bookmarks/headerstate" ) ).toByteArray () );
@@ -143,7 +152,7 @@ void QgsBookmarks::saveWindowLocation()
143
152
144
153
void QgsBookmarks::addClicked ()
145
154
{
146
- Q_ASSERT ( mModel );
155
+ Q_ASSERT ( mMergedModel );
147
156
Q_ASSERT ( mQgisModel );
148
157
149
158
QgsMapCanvas *canvas = QgisApp::instance ()->mapCanvas ();
@@ -176,7 +185,7 @@ void QgsBookmarks::addClicked()
176
185
{
177
186
mQgisModel ->setSort ( 0 , Qt::AscendingOrder );
178
187
mQgisModel ->select ();
179
- QModelIndex newIdx = mProxyModel ->mapFromSource ( mModel ->index ( mQgisModel ->rowCount () - 1 , 1 ) );
188
+ QModelIndex newIdx = mProxyModel ->mapFromSource ( mMergedModel ->index ( mQgisModel ->rowCount () - 1 , 1 ) );
180
189
// Edit new bookmark title
181
190
lstBookmarks->scrollTo ( newIdx );
182
191
lstBookmarks->setCurrentIndex ( newIdx );
@@ -192,16 +201,15 @@ void QgsBookmarks::addClicked()
192
201
193
202
void QgsBookmarks::deleteClicked ()
194
203
{
195
- QList<int > rows;
196
- Q_FOREACH ( const QModelIndex &idx, lstBookmarks->selectionModel ()->selectedIndexes () )
204
+ QItemSelection selection ( mProxyModel ->mapSelectionToSource ( lstBookmarks->selectionModel ()->selection () ) );
205
+ std::vector<int > rows;
206
+ for ( const auto &selectedIdx : selection.indexes () )
197
207
{
198
- if ( idx.column () == 1 )
199
- {
200
- rows << idx.row ();
201
- }
208
+ if ( selectedIdx.column () == 1 )
209
+ rows.push_back ( selectedIdx.row () );
202
210
}
203
211
204
- if ( rows.isEmpty () )
212
+ if ( rows.size () == 0 )
205
213
return ;
206
214
207
215
// make sure the user really wants to delete these bookmarks
@@ -210,12 +218,14 @@ void QgsBookmarks::deleteClicked()
210
218
QMessageBox::Ok | QMessageBox::Cancel ) )
211
219
return ;
212
220
213
- int i = 0 ;
214
- Q_FOREACH ( int row, rows )
221
+ // Remove in reverse order to keep the merged model indexes
222
+ std::sort ( rows.begin (), rows.end (), std::greater<int >() );
223
+
224
+ for ( const auto &row : rows )
215
225
{
216
- mModel ->removeRow ( row - i );
217
- i++;
226
+ mMergedModel ->removeRow ( row );
218
227
}
228
+ mProxyModel ->_resetModel ();
219
229
}
220
230
221
231
void QgsBookmarks::lstBookmarks_doubleClicked ( const QModelIndex &index )
@@ -285,7 +295,7 @@ void QgsBookmarks::importFromXml()
285
295
QDomElement docElem = doc.documentElement ();
286
296
QDomNodeList nodeList = docElem.elementsByTagName ( QStringLiteral ( " bookmark" ) );
287
297
288
- Q_ASSERT ( mModel );
298
+ Q_ASSERT ( mMergedModel );
289
299
290
300
QString queries;
291
301
@@ -354,8 +364,8 @@ void QgsBookmarks::exportToXml()
354
364
QDomElement root = doc.createElement ( QStringLiteral ( " qgis_bookmarks" ) );
355
365
doc.appendChild ( root );
356
366
357
- int rowCount = mModel ->rowCount ();
358
- int colCount = mModel ->columnCount () - 1 ; // exclude virtual "In project" column
367
+ int rowCount = mMergedModel ->rowCount ();
368
+ int colCount = mMergedModel ->columnCount () - 1 ; // exclude virtual "In project" column
359
369
360
370
QList<QString> headerList;
361
371
headerList
@@ -374,7 +384,7 @@ void QgsBookmarks::exportToXml()
374
384
root.appendChild ( bookmark );
375
385
for ( int j = 0 ; j < colCount; j++ )
376
386
{
377
- QModelIndex idx = mModel ->index ( i, j );
387
+ QModelIndex idx = mMergedModel ->index ( i, j );
378
388
if ( idx.isValid () )
379
389
{
380
390
QString value = idx.data ( Qt::DisplayRole ).toString ();
@@ -489,9 +499,11 @@ bool QgsProjectBookmarksTableModel::setData( const QModelIndex &index, const QVa
489
499
bool QgsProjectBookmarksTableModel::insertRows ( int row, int count, const QModelIndex &parent )
490
500
{
491
501
Q_UNUSED ( parent );
492
- beginInsertRows ( parent, row, row + count );
493
-
494
- bool result = QgsProject::instance ()->writeEntry ( QStringLiteral ( " Bookmarks" ), QStringLiteral ( " /count" ), QgsProject::instance ()->readNumEntry ( QStringLiteral ( " Bookmarks" ), QStringLiteral ( " /count" ) ) + count );
502
+ Q_UNUSED ( row );
503
+ // append
504
+ int oldCount = QgsProject::instance ()->readNumEntry ( QStringLiteral ( " Bookmarks" ), QStringLiteral ( " /count" ) );
505
+ beginInsertRows ( parent, oldCount, oldCount + count );
506
+ bool result = QgsProject::instance ()->writeEntry ( QStringLiteral ( " Bookmarks" ), QStringLiteral ( " /count" ), oldCount + count );
495
507
endInsertRows ();
496
508
return result;
497
509
}
@@ -603,36 +615,39 @@ QVariant QgsMergedBookmarksTableModel::data( const QModelIndex &index, int role
603
615
604
616
bool QgsMergedBookmarksTableModel::setData ( const QModelIndex &index, const QVariant &value, int role )
605
617
{
618
+ bool result = false ;
606
619
// last column triggers a move from QGIS to project bookmark
607
620
if ( index .column () == mQgisTableModel .columnCount () )
608
621
{
609
622
if ( index .row () < mQgisTableModel .rowCount () )
610
623
{
611
624
// Move from SQLite storage to project
612
625
moveBookmark ( mQgisTableModel , mProjectTableModel , index .row () );
613
- mTreeView ->scrollTo ( this ->index ( rowCount () - 1 , 1 ) );
614
- mTreeView ->setCurrentIndex ( this ->index ( rowCount () - 1 , 1 ) );
615
- mTreeView ->selectionModel ()->select ( this ->index ( rowCount () - 1 , 1 ), QItemSelectionModel::Rows );
626
+ emit selectItem ( this ->index ( rowCount () - 1 , 1 ) );
616
627
}
617
628
else
618
629
{
619
630
// Move from project to SQLite storage
620
631
moveBookmark ( mProjectTableModel , mQgisTableModel , index .row () - mQgisTableModel .rowCount () );
621
- mTreeView ->scrollTo ( this ->index ( mQgisTableModel .rowCount () - 1 , 1 ) );
622
- mTreeView ->setCurrentIndex ( this ->index ( mQgisTableModel .rowCount () - 1 , 1 ) );
623
- mTreeView ->selectionModel ()->select ( this ->index ( mQgisTableModel .rowCount () - 1 , 1 ), QItemSelectionModel::Rows );
632
+ emit selectItem ( this ->index ( mQgisTableModel .rowCount () - 1 , 1 ) );
624
633
}
625
- return true ;
626
- }
627
-
628
- if ( index .row () < mQgisTableModel .rowCount () )
629
- {
630
- return mQgisTableModel .setData ( index , value, role );
634
+ result = true ;
631
635
}
632
636
else
633
637
{
634
- return mProjectTableModel .setData ( this ->index ( index .row () - mQgisTableModel .rowCount (), index .column () ), value, role );
638
+
639
+ if ( index .row () < mQgisTableModel .rowCount () )
640
+ {
641
+ result = mQgisTableModel .setData ( index , value, role );
642
+ }
643
+ else
644
+ {
645
+ result = mProjectTableModel .setData ( this ->index ( index .row () - mQgisTableModel .rowCount (), index .column () ), value, role );
646
+ }
635
647
}
648
+ if ( result )
649
+ emit dataChanged ( index , index );
650
+ return result;
636
651
}
637
652
638
653
Qt::ItemFlags QgsMergedBookmarksTableModel::flags ( const QModelIndex &index ) const
@@ -659,19 +674,18 @@ bool QgsMergedBookmarksTableModel::removeRows( int row, int count, const QModelI
659
674
{
660
675
Q_ASSERT ( count == 1 );
661
676
bool result;
662
- beginRemoveRows ( parent, row, row + count );
663
677
if ( row < mQgisTableModel .rowCount () )
664
678
{
665
679
QSqlTableModel *qgisModel = static_cast <QSqlTableModel *>( &mQgisTableModel );
666
680
Q_ASSERT ( qgisModel );
667
681
result = qgisModel->removeRows ( row, count, parent );
668
- qgisModel->select ();
682
+ qgisModel->select (); // This updates row count in the model!
669
683
}
670
684
else
671
685
{
672
686
result = mProjectTableModel .removeRows ( row - mQgisTableModel .rowCount (), count, parent );
673
687
}
674
- endRemoveRows ();
688
+ allLayoutChanged ();
675
689
return result;
676
690
}
677
691
0 commit comments