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 //
0005 
0006 
0007 #include "RoutingProfileSettingsDialog.h"
0008 
0009 #include <QStandardItemModel>
0010 
0011 #include "MarbleGlobal.h"
0012 #include "MarbleDebug.h"
0013 #include "RoutingProfilesModel.h"
0014 #include "PluginManager.h"
0015 
0016 #include "ui_RoutingProfileSettingsDialog.h"
0017 
0018 namespace Marble {
0019 
0020 
0021 RoutingProfileSettingsDialog::RoutingProfileSettingsDialog( const PluginManager *pluginManager, RoutingProfilesModel *profilesModel, QWidget* parent )
0022     : QDialog( parent ),
0023     m_profilesModel ( profilesModel ), m_dialog( nullptr ), m_dialogLayout( nullptr )
0024 {
0025     m_ui = new Ui_RoutingProfileSettingsDialog();
0026     m_ui->setupUi( this );
0027     bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
0028     if ( smallScreen ) {
0029       setMinimumHeight( 480 );
0030       m_ui->services->setMinimumWidth( 280 );
0031       m_ui->buttonBox->hide();
0032     }
0033 
0034     QList<RoutingRunnerPlugin*> allPlugins = pluginManager->routingRunnerPlugins();
0035     for( RoutingRunnerPlugin* plugin: allPlugins ) {
0036         m_plugins << plugin;
0037         RoutingRunnerPlugin::ConfigWidget* configWidget = plugin->configWidget();
0038         if ( configWidget ) {
0039             m_configWidgets.insert( plugin, configWidget );
0040             m_ui->settingsStack->addWidget( configWidget );
0041         }
0042     }
0043 
0044     m_servicesModel = new QStandardItemModel;
0045     m_ui->services->setModel( m_servicesModel );
0046     connect ( m_ui->services->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(updateConfigWidget()), Qt::QueuedConnection );
0047     connect ( m_servicesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), SLOT(updateConfigWidget()) );
0048 
0049     connect ( m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()) );
0050     connect ( m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()) );
0051 
0052     connect( m_ui->configureButton, SIGNAL(clicked()), this, SLOT(openConfigDialog()) );
0053 }
0054 
0055 RoutingProfileSettingsDialog::~RoutingProfileSettingsDialog()
0056 {
0057     qDeleteAll( m_configWidgets );
0058     delete m_ui;
0059 }
0060 
0061 void RoutingProfileSettingsDialog::updateConfigWidget( )
0062 {
0063     QModelIndex current = m_ui->services->selectionModel()->currentIndex();
0064 
0065     if ( !current.isValid() ) {
0066         m_ui->settingsStack->setEnabled( false );
0067         return;
0068     }
0069 
0070     RoutingRunnerPlugin *plugin = m_plugins.at( current.row() );
0071     QWidget* configWidget = m_configWidgets[plugin];
0072     if ( configWidget ) {
0073         bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
0074         m_ui->settingsStack->setCurrentWidget( smallScreen ? m_ui->configurePage : configWidget );
0075         m_ui->descriptionLabel->setText( plugin->description() );
0076         m_ui->statusLabel->setText( plugin->statusMessage() );
0077         QStandardItem *item = m_servicesModel->invisibleRootItem()->child( current.row() );
0078         m_ui->settingsStack->setEnabled( item->checkState() == Qt::Checked );
0079     } else {
0080         m_ui->settingsStack->setEnabled( false );
0081         m_ui->noConfigDescriptionLabel->setText( plugin->description() );
0082         m_ui->noConfigStatusLabel->setText( plugin->statusMessage() );
0083         m_ui->settingsStack->setCurrentWidget( m_ui->noConfigAvailablePage );
0084     }
0085 }
0086 
0087 void RoutingProfileSettingsDialog::editProfile( int profileIndex )
0088 {
0089     QList<RoutingProfile> profiles = m_profilesModel->profiles();
0090     m_ui->name->setText( profiles.at( profileIndex ).name() );
0091 
0092     m_servicesModel->clear();
0093     for( RoutingRunnerPlugin *plugin:  m_plugins ) {
0094         QStandardItem *item = new QStandardItem( plugin->guiString() );
0095         item->setCheckable( true );
0096         if ( profiles[ profileIndex ].pluginSettings().contains( plugin->nameId() ) ) {
0097             item->setCheckState( Qt::Checked );
0098             QHash<QString, QVariant> settings = profiles[ profileIndex ].pluginSettings()[ plugin->nameId() ];
0099             mDebug() << settings;
0100             if ( m_configWidgets[plugin] ) {
0101                 m_configWidgets[plugin]->loadSettings( settings );
0102             }
0103         }
0104         m_servicesModel->invisibleRootItem()->appendRow( item );
0105     }
0106     m_ui->settingsStack->setCurrentWidget( m_ui->selectServicePage );
0107     m_ui->settingsStack->setEnabled( false );
0108 
0109     bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
0110     // Small screen profile (like maemo 5) does not have the "buttonBox"
0111     if ( exec() != QDialog::Accepted && !smallScreen ) {
0112         return;
0113     }
0114 
0115     m_profilesModel->setProfileName( profileIndex, m_ui->name->text() );
0116 
0117     QHash< QString, QHash< QString, QVariant > > pluginSettings;
0118     for ( int i=0; i < m_servicesModel->invisibleRootItem()->rowCount(); ++i) {
0119         QStandardItem *item = m_servicesModel->invisibleRootItem()->child( i );
0120         if ( item->checkState() == Qt::Checked ) {
0121             QHash<QString, QVariant> settings;
0122             if ( m_configWidgets[m_plugins[ i ]] ) {
0123                 settings = m_configWidgets[m_plugins[ i ]]->settings();
0124             }
0125             mDebug() << i << m_plugins[ i ]->nameId() << settings;
0126             pluginSettings.insert( m_plugins[ i ]->nameId(), settings );
0127         } else {
0128             pluginSettings.remove( m_plugins[ i ]->nameId() );
0129         }
0130     }
0131     m_profilesModel->setProfilePluginSettings( profileIndex, pluginSettings );
0132 }
0133 
0134 void RoutingProfileSettingsDialog::openConfigDialog()
0135 {
0136     QModelIndex current = m_ui->services->selectionModel()->currentIndex();
0137     if ( current.isValid() ) {
0138         RoutingRunnerPlugin *plugin = m_plugins.at( current.row() );
0139 
0140         if ( !m_dialog ) {
0141             m_dialog = new QDialog( this );
0142 
0143             m_dialogLayout = new QHBoxLayout();
0144             m_dialogLayout->addWidget( m_configWidgets[plugin] );
0145 
0146             m_dialog->setLayout( m_dialogLayout );
0147             m_dialog->setMinimumHeight( 480 );
0148         } else {
0149             m_dialogLayout->insertWidget( 0, m_configWidgets[plugin] );
0150         }
0151 
0152         m_configWidgets[plugin]->show();
0153         m_dialog->setWindowTitle( plugin->guiString() );
0154         m_dialog->exec();
0155         m_configWidgets[plugin]->hide();
0156         m_dialogLayout->removeWidget( m_configWidgets[plugin] );
0157     }
0158 }
0159 
0160 }
0161 
0162 #include "moc_RoutingProfileSettingsDialog.cpp"