File indexing completed on 2024-04-14 03:46:50

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <tackat@kde.org>
0004 // SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
0005 //
0006 
0007 // Own
0008 #include "KdeMainWindow.h"
0009 
0010 // Qt
0011 #include <QAction>
0012 #include <QActionGroup>
0013 #include <QCoreApplication>
0014 #include <QCloseEvent>
0015 
0016 // KF
0017 #include <KLocalizedString>
0018 #include <KActionCollection>
0019 #include <kparts/part.h>
0020 #include <kxmlguifactory.h>
0021 
0022 // GeoData
0023 #include <GeoDataLatLonAltBox.h>
0024 #include <GeoSceneDocument.h>
0025 #include <GeoSceneHead.h>
0026 #include <GeoSceneMap.h>
0027 #include <ViewportParams.h>
0028 
0029 // Local dir
0030 #include "MarbleDebug.h"
0031 #include "ControlView.h"
0032 #include "marble_part.h"
0033 
0034 namespace Marble
0035 {
0036 
0037 MainWindow::MainWindow( const QString& marbleDataPath, QWidget *parent )
0038     : KXmlGuiWindow( parent ),
0039       m_savedSize( QSize( -1, -1 ) )
0040 {
0041     m_part = new MarblePart( this, this, QVariantList() << marbleDataPath );
0042 
0043     setCentralWidget( m_part->widget() );
0044 
0045     insertChildClient( m_part );
0046 
0047     setupGUI( Default, "marbleui.rc" );
0048     setStandardToolBarMenuEnabled( true );
0049 
0050     QList<QAction*> panelActions = m_part->controlView()->setupDockWidgets( this );
0051     QAction *allAction = panelActions.first();
0052     m_part->actionCollection()->setDefaultShortcut(allAction, allAction->shortcut());
0053     m_part->readTrackingSettings();
0054     m_part->unplugActionList( "panels_actionlist" );
0055     m_part->plugActionList( "panels_actionlist", panelActions );
0056 
0057     // View size actions
0058     m_viewSizeActsGroup = ControlView::createViewSizeActionGroup( this );
0059     connect( m_viewSizeActsGroup, SIGNAL(triggered(QAction*)), this, SLOT(changeViewSize(QAction*)) );
0060 
0061     m_part->plugActionList( "viewSize_actionlist", m_viewSizeActsGroup->actions() );
0062 
0063     // Creating the plugin menus
0064     m_part->createInfoBoxesMenu();
0065     m_part->createOnlineServicesMenu();
0066     m_part->createRenderPluginActions();
0067     m_part->createFolderList();
0068 
0069     setAutoSaveSettings();
0070 
0071     connect( marbleWidget(), SIGNAL(themeChanged(QString)),
0072             this, SLOT(updateWindowTitle()));
0073     connect( marbleWidget(), SIGNAL(themeChanged(QString)),
0074             this, SLOT(updateCenterFromTheme()));
0075     updateWindowTitle();
0076 }
0077 
0078 MainWindow::~MainWindow()
0079 {
0080     factory()->removeClient( m_part );
0081     delete m_part;
0082 }
0083 
0084 ControlView* MainWindow::marbleControl() const
0085 {
0086     return m_part->controlView();
0087 }
0088 
0089 MarbleWidget* MainWindow::marbleWidget() const
0090 {
0091     return m_part->controlView()->marbleWidget();
0092 }
0093 
0094 void MainWindow::updateWindowTitle()
0095 {
0096     GeoSceneDocument *mapTheme = marbleWidget()->mapTheme();
0097     setWindowTitle(mapTheme ? mapTheme->head()->name() : QString());
0098 }
0099 
0100 void MainWindow::updateCenterFromTheme()
0101 {
0102    //A scene provider may provide only a subset of the globe, use scene properties read from a dgml as a starting point
0103    GeoSceneDocument * theme = m_part->controlView()->marbleWidget()->mapTheme();
0104    if (theme) {
0105        const GeoSceneMap* map = theme->map();
0106        if (map) {
0107            const QVariantList coords = map->center();
0108            if (! coords.empty()) {
0109                if (coords.count() == 2) {
0110                    m_part->controlView()->marbleWidget()->centerOn( coords.at(0).toDouble(), coords.at(1).toDouble() );
0111                }
0112                else if (coords.count() == 4) {
0113                    // If the  the map theme provides a bounding box that does not contain
0114                    // the current view then we center on the bounding box of the map theme.
0115                    GeoDataLatLonBox latLonBox(coords.at(0).toDouble(), coords.at(1).toDouble(),
0116                                               coords.at(2).toDouble(), coords.at(3).toDouble(), GeoDataCoordinates::Degree);
0117                    GeoDataLatLonAltBox viewBox = m_part->controlView()->marbleWidget()->viewport()->viewLatLonAltBox();
0118                    if (!latLonBox.contains(viewBox)) {
0119                        m_part->controlView()->marbleWidget()->centerOn( latLonBox );
0120                    }
0121                    else {
0122                        mDebug() << QString("DGML theme %1 has invalid number of coordinates").arg(theme->head()->name());
0123                    }
0124                }
0125            }
0126        }
0127    }
0128 }
0129 
0130 void MainWindow::changeViewSize( QAction* action )
0131 {
0132     mDebug()<<size();
0133     mDebug()<<minimumSize()<<maximumSize();
0134     if ( action->data().type() == QVariant::Size ) {
0135         if ( m_savedSize.isEmpty() ) {
0136             m_savedSize = marbleControl()->size();
0137         }
0138         marbleControl()->setFixedSize( action->data().toSize() );
0139         adjustSize();
0140     } else {
0141         marbleControl()->setMinimumSize( QSize( 0, 0 ) );
0142         marbleControl()->setMaximumSize( QSize( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ) );
0143         marbleControl()->resize( m_savedSize );
0144         marbleControl()->setMinimumSize( m_savedSize );
0145         adjustSize();
0146         marbleControl()->setMinimumSize( QSize( 0, 0 ) );
0147         m_savedSize.setHeight( -1 );
0148     }
0149     mDebug()<<marbleControl()->size();
0150     mDebug()<<size();
0151 }
0152 
0153 void MainWindow::closeEvent( QCloseEvent *event )
0154 {
0155     QCloseEvent newEvent;
0156     QCoreApplication::sendEvent( this->marbleControl(), &newEvent );
0157 
0158     if ( newEvent.isAccepted() ) {
0159         event->accept();
0160     } else {
0161         event->ignore();
0162     }
0163 }
0164 
0165 }
0166 #include "moc_KdeMainWindow.cpp"