File indexing completed on 2024-04-28 03:49:30

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com>
0004 // SPDX-FileCopyrightText: 2011-2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0005 //
0006 
0007 #include "RoutingProfilesWidget.h"
0008 #include "ui_RoutingSettingsWidget.h"
0009 #include "MarbleWidget.h"
0010 
0011 #include <QStandardItemModel>
0012 #include "MarbleModel.h"
0013 #include "PluginManager.h"
0014 #include "RoutingRunnerPlugin.h"
0015 #include "MarbleDebug.h"
0016 #include "RoutingProfilesModel.h"
0017 #include "RoutingManager.h"
0018 #include "RoutingProfileSettingsDialog.h"
0019 
0020 namespace Marble
0021 {
0022 
0023 class Q_DECL_HIDDEN RoutingProfilesWidget::Private
0024 {
0025  public:
0026     Private( MarbleModel *marbleModel, RoutingProfilesWidget *parent );
0027 
0028     void add();
0029     void configure();
0030     void remove();
0031     void moveUp();
0032     void moveDown();
0033     void updateButtons();
0034 
0035     RoutingProfilesWidget *const q;
0036     const PluginManager *const m_pluginManager;
0037     RoutingProfilesModel *const m_profilesModel;
0038     Ui_RoutingSettingsWidget m_ui;
0039 };
0040 
0041 RoutingProfilesWidget::Private::Private( MarbleModel *marbleModel, RoutingProfilesWidget *parent ) :
0042     q( parent ),
0043     m_pluginManager( marbleModel->pluginManager() ),
0044     m_profilesModel( marbleModel->routingManager()->profilesModel() )
0045 {
0046 }
0047 
0048 RoutingProfilesWidget::RoutingProfilesWidget( MarbleModel *marbleModel )
0049     : QWidget( nullptr ),
0050       d( new Private( marbleModel, this ) )
0051 {
0052     d->m_ui.setupUi( this );
0053     d->m_ui.profilesList->setModel( d->m_profilesModel );
0054 
0055     connect( d->m_ui.addButton, SIGNAL(clicked(bool)), SLOT(add()) );
0056     connect( d->m_ui.removeButton, SIGNAL(clicked(bool)), SLOT(remove()) );
0057     connect( d->m_ui.configureButton, SIGNAL(clicked(bool)), SLOT(configure()) );
0058     connect( d->m_ui.moveUpButton, SIGNAL(clicked(bool)), SLOT(moveUp()) );
0059     connect( d->m_ui.moveDownButton, SIGNAL(clicked(bool)), SLOT(moveDown()) );
0060     connect( d->m_ui.profilesList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), SLOT(updateButtons()), Qt::QueuedConnection );
0061     connect( d->m_ui.profilesList, SIGNAL(doubleClicked(QModelIndex)), SLOT(configure()) );
0062 
0063     connect( d->m_profilesModel, SIGNAL(layoutChanged()), SLOT(updateButtons()) );
0064 }
0065 
0066 RoutingProfilesWidget::~RoutingProfilesWidget()
0067 {
0068     delete d;
0069 }
0070 
0071 void RoutingProfilesWidget::Private::add()
0072 {
0073     m_profilesModel->addProfile( tr( "New Profile" ) );
0074 
0075     int profileIndex = m_profilesModel->rowCount() - 1;
0076     m_ui.profilesList->selectionModel()->select( m_profilesModel->index( profileIndex, 0 ), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent );
0077 
0078     RoutingProfileSettingsDialog dialog( m_pluginManager, m_profilesModel, q );
0079     dialog.editProfile( profileIndex );
0080 }
0081 
0082 void RoutingProfilesWidget::Private::remove()
0083 {
0084     if ( m_ui.profilesList->selectionModel()->selectedRows().isEmpty() ) {
0085         return;
0086     }
0087     m_profilesModel->removeRows( m_ui.profilesList->selectionModel()->selectedRows().first().row(), 1 );
0088 }
0089 
0090 void RoutingProfilesWidget::Private::configure()
0091 {
0092     if ( m_ui.profilesList->selectionModel()->selectedRows().isEmpty() ) {
0093         return;
0094     }
0095 
0096     int profileIndex = m_ui.profilesList->selectionModel()->selectedRows().first().row();
0097 
0098     RoutingProfileSettingsDialog dialog( m_pluginManager, m_profilesModel, q );
0099     dialog.editProfile( profileIndex );
0100 }
0101 
0102 void RoutingProfilesWidget::Private::moveUp()
0103 {
0104     if ( m_ui.profilesList->selectionModel()->selectedRows().isEmpty() ) {
0105         return;
0106     }
0107     m_profilesModel->moveUp( m_ui.profilesList->selectionModel()->selectedRows().first().row() );
0108 }
0109 
0110 void RoutingProfilesWidget::Private::moveDown()
0111 {
0112     if ( m_ui.profilesList->selectionModel()->selectedRows().isEmpty() ) {
0113         return;
0114     }
0115     m_profilesModel->moveDown( m_ui.profilesList->selectionModel()->selectedRows().first().row() );
0116 }
0117 
0118 void RoutingProfilesWidget::Private::updateButtons()
0119 {
0120     QModelIndex current;
0121     if ( !m_ui.profilesList->selectionModel()->selectedRows().isEmpty() ) {
0122         current = m_ui.profilesList->selectionModel()->selectedRows().first();
0123     }
0124     m_ui.configureButton->setEnabled( current.isValid() );
0125     m_ui.removeButton->setEnabled( current.isValid() );
0126     m_ui.moveUpButton->setEnabled( current.isValid() && current.row() > 0 );
0127     m_ui.moveDownButton->setEnabled( current.isValid() && current.row()+1 < m_profilesModel->rowCount()  );
0128 }
0129 
0130 }
0131 
0132 #include "moc_RoutingProfilesWidget.cpp"