1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2011-2012 Florian Eßer <f.esser@rwth-aachen.de>
4 // SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
5 // SPDX-FileCopyrightText: 2013 Roman Karlstetter <roman.karlstetter@googlemail.com>
6 //
7 #include "ElevationProfileContextMenu.h"
8 
9 #include "ElevationProfileDataSource.h"
10 #include "ElevationProfileFloatItem.h"
11 #include "MarbleDebug.h"
12 
13 namespace Marble {
14 
ElevationProfileContextMenu(ElevationProfileFloatItem * floatItem)15 ElevationProfileContextMenu::ElevationProfileContextMenu(ElevationProfileFloatItem *floatItem):
16     QObject(floatItem),
17     m_floatItem(floatItem),
18     m_sourceGrp(nullptr),
19     m_contextMenu(nullptr),
20     m_trackMapper(nullptr)
21 {
22 }
23 
getMenu()24 QMenu *ElevationProfileContextMenu::getMenu()
25 {
26     if (!m_contextMenu) {
27         m_contextMenu = m_floatItem->contextMenu();
28 
29         for ( QAction *action: m_contextMenu->actions() ) {
30             if ( action->text() == tr( "&Configure..." ) ) {
31                 m_contextMenu->removeAction( action );
32                 break;
33             }
34         }
35 
36         QAction *zoomToViewportAction = m_contextMenu->addAction( tr("&Zoom to viewport"), m_floatItem,
37                                 SLOT(toggleZoomToViewport()) );
38         zoomToViewportAction->setCheckable( true );
39         zoomToViewportAction->setChecked( m_floatItem->m_zoomToViewport );
40         m_contextMenu->addSeparator();
41 
42         m_sourceGrp = new QActionGroup(this);
43         m_trackMapper = new QSignalMapper(this);
44         updateContextMenuEntries();
45     }
46     return m_contextMenu;
47 }
48 
updateContextMenuEntries()49 void ElevationProfileContextMenu::updateContextMenuEntries()
50 {
51     // context menu has not yet been initialized, this functions is called as last step of initialization
52     if (!m_contextMenu) {
53         return;
54     }
55 
56     // completely rebuild selection, TODO could be possibly improved to only add/remove items incrementally
57     for (QAction* action: m_selectionActions) {
58         m_contextMenu->removeAction( action );
59     }
60 
61     // clear state
62     qDeleteAll(m_selectionActions);
63     m_selectionActions.clear();
64 
65     // add route data source (if available)
66     if ( m_floatItem->m_routeDataSource.isDataAvailable()) {
67         QAction *route = new QAction( tr( "Route" ), m_contextMenu );
68         route->setActionGroup(m_sourceGrp);
69         route->setCheckable(true);
70         route->setChecked(m_floatItem->m_activeDataSource == &m_floatItem->m_routeDataSource);
71         connect( route, SIGNAL(triggered()), m_floatItem, SLOT(switchToRouteDataSource()) );
72         m_selectionActions.append(route);
73     }
74 
75     // add tracks (if available)
76     if ( m_floatItem->m_trackDataSource.isDataAvailable()) {
77         QStringList sources = m_floatItem->m_trackDataSource.sourceDescriptions();
78         for (int i = 0; i<sources.size(); ++i) {
79             QAction *track = new QAction( tr("Track: ") + sources[i], m_contextMenu);
80             connect(track, SIGNAL(triggered()), m_trackMapper, SLOT(map()));
81             track->setCheckable(true);
82             track->setChecked(m_floatItem->m_activeDataSource == &m_floatItem->m_trackDataSource && m_floatItem->m_trackDataSource.currentSourceIndex() == i);
83             track->setActionGroup(m_sourceGrp);
84             m_selectionActions.append(track);
85             m_trackMapper->setMapping(track, i);
86         }
87         connect(m_trackMapper, SIGNAL(mapped(int)), m_floatItem, SLOT(switchToTrackDataSource(int)));
88     }
89 
90     // no route or track available, add disabled action to inform user about it
91     if ( m_selectionActions.isEmpty() ) {
92         QAction *disabledInformationAction = new QAction( tr( "Create a route or load a track from file to view its elevation profile." ), m_contextMenu);
93         disabledInformationAction->setEnabled(false);
94         m_selectionActions.append(disabledInformationAction);
95     }
96 
97     for (QAction *action: m_selectionActions) {
98         m_contextMenu->addAction(action);
99     }
100 }
101 
102 }
103 
104 #include "moc_ElevationProfileContextMenu.cpp"
105