22// Distributed under the MIT software license, see the accompanying
33// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
5+ #include < reverse_iterator.h>
56#include < wallet/wallet.h>
67
78#include < qt/receivecoinsdialog.h>
@@ -30,6 +31,9 @@ ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle *_platformStyle, QWid
3031{
3132 ui->setupUi (this );
3233
34+ m_sort_proxy = new QSortFilterProxyModel (this );
35+ m_sort_proxy->setSortRole (Qt::UserRole);
36+
3337 if (!_platformStyle->getImagesOnButtons ()) {
3438 ui->clearButton ->setIcon (QIcon ());
3539 ui->receiveButton ->setIcon (QIcon ());
@@ -57,7 +61,7 @@ ReceiveCoinsDialog::ReceiveCoinsDialog(const PlatformStyle *_platformStyle, QWid
5761 tableView->verticalHeader ()->hide ();
5862 tableView->setAlternatingRowColors (true );
5963 tableView->setSelectionBehavior (QAbstractItemView::SelectRows);
60- tableView->setSelectionMode (QAbstractItemView::ContiguousSelection );
64+ tableView->setSelectionMode (QAbstractItemView::ExtendedSelection );
6165
6266 QSettings settings;
6367 if (!tableView->horizontalHeader ()->restoreState (settings.value (" RecentRequestsViewHeaderState" ).toByteArray ())) {
@@ -75,12 +79,12 @@ void ReceiveCoinsDialog::setModel(WalletModel *_model)
7579
7680 if (_model && _model->getOptionsModel ())
7781 {
78- _model->getRecentRequestsTableModel ()->sort (RecentRequestsTableModel::Date, Qt::DescendingOrder);
7982 connect (_model->getOptionsModel (), &OptionsModel::displayUnitChanged, this , &ReceiveCoinsDialog::updateDisplayUnit);
8083 updateDisplayUnit ();
8184
8285 QTableView* tableView = ui->recentRequestsView ;
83- tableView->setModel (_model->getRecentRequestsTableModel ());
86+ tableView->setModel (m_sort_proxy);
87+ m_sort_proxy->setSourceModel (_model->getRecentRequestsTableModel ());
8488 tableView->sortByColumn (RecentRequestsTableModel::Date, Qt::DescendingOrder);
8589
8690 connect (tableView->selectionModel (),
@@ -192,12 +196,7 @@ void ReceiveCoinsDialog::on_receiveButton_clicked()
192196
193197void ReceiveCoinsDialog::on_recentRequestsView_doubleClicked (const QModelIndex &index)
194198{
195- const RecentRequestsTableModel *submodel = model->getRecentRequestsTableModel ();
196- ReceiveRequestDialog *dialog = new ReceiveRequestDialog (this );
197- dialog->setModel (model);
198- dialog->setInfo (submodel->entry (index.row ()).recipient );
199- dialog->setAttribute (Qt::WA_DeleteOnClose);
200- dialog->show ();
199+ ShowReceiveRequestDialogForItem (selectedRow ());
201200}
202201
203202void ReceiveCoinsDialog::recentRequestsView_selectionChanged (const QItemSelection &selected, const QItemSelection &deselected)
@@ -210,37 +209,55 @@ void ReceiveCoinsDialog::recentRequestsView_selectionChanged(const QItemSelectio
210209
211210void ReceiveCoinsDialog::on_showRequestButton_clicked ()
212211{
213- if (!model || !model->getRecentRequestsTableModel () || !ui->recentRequestsView ->selectionModel ())
214- return ;
215- QModelIndexList selection = ui->recentRequestsView ->selectionModel ()->selectedRows ();
212+ QModelIndexList selection = SelectedRows ();
216213
217214 for (const QModelIndex& index : selection) {
218- on_recentRequestsView_doubleClicked (index);
215+ ShowReceiveRequestDialogForItem (index);
219216 }
220217}
221218
222219void ReceiveCoinsDialog::on_removeRequestButton_clicked ()
223220{
224- if (!model || !model->getRecentRequestsTableModel () || !ui->recentRequestsView ->selectionModel ())
225- return ;
226- QModelIndexList selection = ui->recentRequestsView ->selectionModel ()->selectedRows ();
221+ QModelIndexList selection = SelectedRows ();
227222 if (selection.empty ())
228223 return ;
229- // correct for selection mode ContiguousSelection
230- QModelIndex firstIndex = selection.at (0 );
231- model->getRecentRequestsTableModel ()->removeRows (firstIndex.row (), selection.length (), firstIndex.parent ());
224+
225+ // Collect row indices in a set (sorted) and pass in reverse order to removeRows
226+ // to avoid having to keep track of changed source indices after each removal
227+ std::set<int > row_indices;
228+ for (const QModelIndex& ind : selection) {
229+ row_indices.insert (ind.row ());
230+ }
231+
232+ for (auto row_ind : reverse_iterate (row_indices)) {
233+ model->getRecentRequestsTableModel ()->removeRows (row_ind, 1 );
234+ }
232235}
233236
234237QModelIndex ReceiveCoinsDialog::selectedRow ()
235238{
236239 if (!model || !model->getRecentRequestsTableModel () || !ui->recentRequestsView ->selectionModel ())
237240 return QModelIndex ();
238241 QModelIndexList selection = ui->recentRequestsView ->selectionModel ()->selectedRows ();
239- if (selection.empty ())
242+ // Only allow single-selection in this method
243+ if (selection.size () != 1 )
240244 return QModelIndex ();
241- // correct for selection mode ContiguousSelection
242- QModelIndex firstIndex = selection.at (0 );
243- return firstIndex;
245+
246+ QModelIndex row_ind = m_sort_proxy->mapToSource (selection.at (0 ));
247+ return row_ind;
248+ }
249+
250+ QModelIndexList ReceiveCoinsDialog::SelectedRows ()
251+ {
252+ if (!model || !model->getRecentRequestsTableModel () || !ui->recentRequestsView ->selectionModel ())
253+ return QModelIndexList ();
254+ QModelIndexList selection = ui->recentRequestsView ->selectionModel ()->selectedRows ();
255+ QModelIndexList source_mapped;
256+ for (auto row : selection) {
257+ source_mapped.append (m_sort_proxy->mapToSource (row));
258+ }
259+
260+ return source_mapped;
244261}
245262
246263// copy column of selected row to clipboard
@@ -253,6 +270,19 @@ void ReceiveCoinsDialog::copyColumnToClipboard(int column)
253270 GUIUtil::setClipboard (model->getRecentRequestsTableModel ()->index (firstIndex.row (), column).data (Qt::EditRole).toString ());
254271}
255272
273+ void ReceiveCoinsDialog::ShowReceiveRequestDialogForItem (const QModelIndex& index)
274+ {
275+ if (!index.isValid ()) {
276+ return ;
277+ }
278+ const RecentRequestsTableModel *submodel = model->getRecentRequestsTableModel ();
279+ ReceiveRequestDialog *dialog = new ReceiveRequestDialog (this );
280+ dialog->setModel (model);
281+ dialog->setInfo (submodel->entry (index.row ()).recipient );
282+ dialog->setAttribute (Qt::WA_DeleteOnClose);
283+ dialog->show ();
284+ }
285+
256286// context menu
257287void ReceiveCoinsDialog::showMenu (const QPoint &point)
258288{
0 commit comments