1 /* SPDX-FileCopyrightText: 2021 Tobias Leupold <tobias.leupold@gmx.de>
2
3 SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-KDE-Accepted-GPL
4 */
5
6 // Local includes
7 #include "TracksListView.h"
8 #include "GeoDataModel.h"
9
10 // KDE includes
11 #include <KLocalizedString>
12
13 // Qt includes
14 #include <QMenu>
15 #include <QAction>
16
TracksListView(GeoDataModel * model,QWidget * parent)17 TracksListView::TracksListView(GeoDataModel *model, QWidget *parent) : QListView(parent)
18 {
19 viewport()->setAcceptDrops(true);
20 setDropIndicatorShown(true);
21 setDragDropMode(QAbstractItemView::DropOnly);
22
23 setModel(model);
24 setSelectionMode(QAbstractItemView::ExtendedSelection);
25 setContextMenuPolicy(Qt::CustomContextMenu);
26
27 connect(this, &QAbstractItemView::clicked, this, &TracksListView::trackSelected);
28
29 connect(selectionModel(), &QItemSelectionModel::selectionChanged,
30 this, &TracksListView::checkSelection);
31
32 m_contextMenu = new QMenu(this);
33
34 m_remove = m_contextMenu->addAction(i18np("Remove track", "Remove tracks", 1));
35 connect(m_remove, &QAction::triggered, this, &TracksListView::removeTracks);
36
37 connect(this, &QListView::customContextMenuRequested, this, &TracksListView::showContextMenu);
38 }
39
currentChanged(const QModelIndex & current,const QModelIndex &)40 void TracksListView::currentChanged(const QModelIndex ¤t, const QModelIndex &)
41 {
42 if (current.isValid()) {
43 emit trackSelected(current);
44 scrollTo(current);
45 }
46 }
47
showContextMenu(const QPoint & point)48 void TracksListView::showContextMenu(const QPoint &point)
49 {
50 const auto selected = selectedIndexes();
51 const int allSelected = selected.count();
52
53 m_remove->setEnabled(allSelected > 0);
54 m_remove->setText(i18np("Remove track", "Remove tracks", allSelected));
55
56 m_contextMenu->exec(mapToGlobal(point));
57 }
58
selectedTracks() const59 QVector<int> TracksListView::selectedTracks() const
60 {
61 QVector<int> selection;
62 const auto selected = selectedIndexes();
63 for (const auto &index : selected) {
64 selection.append(index.row());
65 }
66 return selection;
67 }
68
checkSelection()69 void TracksListView::checkSelection()
70 {
71 const auto selected = selectedIndexes();
72 emit updateTrackWalker(selected.count() == 1 ? selected.first().row() : -1);
73 }
74