File indexing completed on 2024-04-28 03:50:15

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011-2012 Florian Eßer <f.esser@rwth-aachen.de>
0004 // SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0005 // SPDX-FileCopyrightText: 2013 Roman Karlstetter <roman.karlstetter@googlemail.com>
0006 //
0007 #include "ElevationProfileContextMenu.h"
0008 
0009 #include "ElevationProfileDataSource.h"
0010 #include "ElevationProfileFloatItem.h"
0011 #include "MarbleDebug.h"
0012 
0013 namespace Marble {
0014 
0015 ElevationProfileContextMenu::ElevationProfileContextMenu(ElevationProfileFloatItem *floatItem):
0016     QObject(floatItem),
0017     m_floatItem(floatItem),
0018     m_sourceGrp(nullptr),
0019     m_contextMenu(nullptr),
0020     m_trackMapper(nullptr)
0021 {
0022 }
0023 
0024 QMenu *ElevationProfileContextMenu::getMenu()
0025 {
0026     if (!m_contextMenu) {
0027         m_contextMenu = m_floatItem->contextMenu();
0028 
0029         for ( QAction *action: m_contextMenu->actions() ) {
0030             if ( action->text() == tr( "&Configure..." ) ) {
0031                 m_contextMenu->removeAction( action );
0032                 break;
0033             }
0034         }
0035 
0036         QAction *zoomToViewportAction = m_contextMenu->addAction( tr("&Zoom to viewport"), m_floatItem,
0037                                 SLOT(toggleZoomToViewport()) );
0038         zoomToViewportAction->setCheckable( true );
0039         zoomToViewportAction->setChecked( m_floatItem->m_zoomToViewport );
0040         m_contextMenu->addSeparator();
0041 
0042         m_sourceGrp = new QActionGroup(this);
0043         m_trackMapper = new QSignalMapper(this);
0044         updateContextMenuEntries();
0045     }
0046     return m_contextMenu;
0047 }
0048 
0049 void ElevationProfileContextMenu::updateContextMenuEntries()
0050 {
0051     // context menu has not yet been initialized, this functions is called as last step of initialization
0052     if (!m_contextMenu) {
0053         return;
0054     }
0055 
0056     // completely rebuild selection, TODO could be possibly improved to only add/remove items incrementally
0057     for (QAction* action: m_selectionActions) {
0058         m_contextMenu->removeAction( action );
0059     }
0060 
0061     // clear state
0062     qDeleteAll(m_selectionActions);
0063     m_selectionActions.clear();
0064 
0065     // add route data source (if available)
0066     if ( m_floatItem->m_routeDataSource.isDataAvailable()) {
0067         QAction *route = new QAction( tr( "Route" ), m_contextMenu );
0068         route->setActionGroup(m_sourceGrp);
0069         route->setCheckable(true);
0070         route->setChecked(m_floatItem->m_activeDataSource == &m_floatItem->m_routeDataSource);
0071         connect( route, SIGNAL(triggered()), m_floatItem, SLOT(switchToRouteDataSource()) );
0072         m_selectionActions.append(route);
0073     }
0074 
0075     // add tracks (if available)
0076     if ( m_floatItem->m_trackDataSource.isDataAvailable()) {
0077         QStringList sources = m_floatItem->m_trackDataSource.sourceDescriptions();
0078         for (int i = 0; i<sources.size(); ++i) {
0079             QAction *track = new QAction( tr("Track: ") + sources[i], m_contextMenu);
0080             connect(track, SIGNAL(triggered()), m_trackMapper, SLOT(map()));
0081             track->setCheckable(true);
0082             track->setChecked(m_floatItem->m_activeDataSource == &m_floatItem->m_trackDataSource && m_floatItem->m_trackDataSource.currentSourceIndex() == i);
0083             track->setActionGroup(m_sourceGrp);
0084             m_selectionActions.append(track);
0085             m_trackMapper->setMapping(track, i);
0086         }
0087         connect(m_trackMapper, SIGNAL(mapped(int)), m_floatItem, SLOT(switchToTrackDataSource(int)));
0088     }
0089 
0090     // no route or track available, add disabled action to inform user about it
0091     if ( m_selectionActions.isEmpty() ) {
0092         QAction *disabledInformationAction = new QAction( tr( "Create a route or load a track from file to view its elevation profile." ), m_contextMenu);
0093         disabledInformationAction->setEnabled(false);
0094         m_selectionActions.append(disabledInformationAction);
0095     }
0096 
0097     for (QAction *action: m_selectionActions) {
0098         m_contextMenu->addAction(action);
0099     }
0100 }
0101 
0102 }
0103 
0104 #include "moc_ElevationProfileContextMenu.cpp"