From 726f88218525b7fa91dca8410b372d2d15be99b4 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 20 Jul 2020 23:06:12 +0200 Subject: [PATCH] [locale] Move current-location to Config --- src/modules/locale/Config.cpp | 25 ++++++++++++++++++ src/modules/locale/Config.h | 22 ++++++++++++++++ src/modules/locale/LocalePage.cpp | 26 ++++++++----------- .../locale/timezonewidget/timezonewidget.cpp | 20 +++----------- .../locale/timezonewidget/timezonewidget.h | 10 ++----- 5 files changed, 63 insertions(+), 40 deletions(-) diff --git a/src/modules/locale/Config.cpp b/src/modules/locale/Config.cpp index 2e0eb8173..5a97339a8 100644 --- a/src/modules/locale/Config.cpp +++ b/src/modules/locale/Config.cpp @@ -163,6 +163,31 @@ Config::timezoneData() const return ::timezoneData(); } +void +Config::setCurrentLocation( const QString& regionName, const QString& zoneName ) +{ + using namespace CalamaresUtils::Locale; + auto* region = timezoneData().find< TZRegion >( regionName ); + auto* zone = region ? region->zones().find< TZZone >( zoneName ) : nullptr; + if ( zone ) + { + setCurrentLocation( zone ); + } + else + { + setCurrentLocation( QStringLiteral("America"), QStringLiteral("New_York") ); + } +} + +void +Config::setCurrentLocation( const CalamaresUtils::Locale::TZZone* location ) +{ + if ( location != m_currentLocation ) + { + m_currentLocation = location; + emit currentLocationChanged( m_currentLocation ); + } +} void Config::setConfigurationMap( const QVariantMap& configurationMap ) diff --git a/src/modules/locale/Config.h b/src/modules/locale/Config.h index 8e1f29cf7..44e7c9142 100644 --- a/src/modules/locale/Config.h +++ b/src/modules/locale/Config.h @@ -35,6 +35,7 @@ class Config : public QObject Q_PROPERTY( CalamaresUtils::Locale::CStringListModel* zonesModel READ zonesModel CONSTANT FINAL ) Q_PROPERTY( CalamaresUtils::Locale::CStringListModel* regionModel READ regionModel CONSTANT FINAL ) Q_PROPERTY( const CalamaresUtils::Locale::CStringPairList& timezoneData READ timezoneData CONSTANT FINAL ) + Q_PROPERTY( const CalamaresUtils::Locale::TZZone* currentLocation READ currentLocation WRITE setCurrentLocation NOTIFY currentLocationChanged ) public: Config( QObject* parent = nullptr ); @@ -50,6 +51,23 @@ public Q_SLOTS: // Underlying data for the models const CalamaresUtils::Locale::CStringPairList& timezoneData() const; + /** @brief Sets a location by name + * + * @p region should be "America" or the like, while @p zone + * names a zone within that region. + */ + void setCurrentLocation( const QString& region, const QString& zone ); + /** @brief Sets a location by pointer + * + * Pointer should be within the same model as the widget uses. + */ + void setCurrentLocation( const CalamaresUtils::Locale::TZZone* location ); + + const CalamaresUtils::Locale::TZZone* currentLocation() const { return m_currentLocation; } + +signals: + void currentLocationChanged( const CalamaresUtils::Locale::TZZone* location ); + private: /// A list of supported locale identifiers (e.g. "en_US.UTF-8") QStringList m_localeGenLines; @@ -58,6 +76,10 @@ private: std::unique_ptr< CalamaresUtils::Locale::CStringListModel > m_regionModel; /// The zones for the current region (e.g. America/New_York) std::unique_ptr< CalamaresUtils::Locale::CStringListModel > m_zonesModel; + + /// The location, points into the timezone data + const CalamaresUtils::Locale::TZZone* m_currentLocation = nullptr; + }; diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index b3fa8e8e9..d4d24afef 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -104,9 +104,13 @@ LocalePage::LocalePage( Config* config, QWidget* parent ) setMinimumWidth( m_tzWidget->width() ); setLayout( mainLayout ); + connect( config, &Config::currentLocationChanged, m_tzWidget, &TimeZoneWidget::setCurrentLocation ); + connect( config, &Config::currentLocationChanged, this, &LocalePage::locationChanged ); + connect( m_tzWidget, &TimeZoneWidget::locationChanged, config, QOverload< const CalamaresUtils::Locale::TZZone* >::of( &Config::setCurrentLocation ) ); + connect( m_regionCombo, QOverload< int >::of( &QComboBox::currentIndexChanged ), this, &LocalePage::regionChanged ); connect( m_zoneCombo, QOverload< int >::of( &QComboBox::currentIndexChanged ), this, &LocalePage::zoneChanged ); - connect( m_tzWidget, &TimeZoneWidget::locationChanged, this, &LocalePage::locationChanged ); + connect( m_localeChangeButton, &QPushButton::clicked, this, &LocalePage::changeLocale ); connect( m_formatsChangeButton, &QPushButton::clicked, this, &LocalePage::changeFormats ); @@ -140,16 +144,7 @@ LocalePage::init( const QString& initialRegion, const QString& initialZone ) m_regionCombo->setModel( m_config->regionModel() ); m_regionCombo->currentIndexChanged( m_regionCombo->currentIndex() ); - auto* region = CalamaresUtils::Locale::TZRegion::fromZoneTab().find< TZRegion >( initialRegion ); - if ( region && region->zones().find< TZZone >( initialZone ) ) - { - m_tzWidget->setCurrentLocation( initialRegion, initialZone ); - } - else - { - m_tzWidget->setCurrentLocation( "America", "New_York" ); - } - + m_config->setCurrentLocation( initialRegion, initialZone ); updateGlobalStorage(); } @@ -209,7 +204,7 @@ LocaleConfiguration LocalePage::guessLocaleConfiguration() const { return LocaleConfiguration::fromLanguageAndLocation( - QLocale().name(), m_config->supportedLocales(), m_tzWidget->currentLocation()->country() ); + QLocale().name(), m_config->supportedLocales(), m_config->currentLocation()->country() ); } @@ -227,7 +222,7 @@ LocalePage::updateGlobalStorage() { auto* gs = Calamares::JobQueue::instance()->globalStorage(); - const auto* location = m_tzWidget->currentLocation(); + const auto* location = m_config->currentLocation(); bool locationChanged = ( location->region() != gs->value( "locationRegion" ) ) || ( location->zone() != gs->value( "locationZone" ) ); @@ -296,9 +291,10 @@ LocalePage::zoneChanged( int currentIndex ) { Q_UNUSED( currentIndex ) if ( !m_blockTzWidgetSet ) - m_tzWidget->setCurrentLocation( m_regionCombo->currentData().toString(), + { + m_config->setCurrentLocation( m_regionCombo->currentData().toString(), m_zoneCombo->currentData().toString() ); - + } updateGlobalStorage(); } diff --git a/src/modules/locale/timezonewidget/timezonewidget.cpp b/src/modules/locale/timezonewidget/timezonewidget.cpp index 149ad6590..df1142e17 100644 --- a/src/modules/locale/timezonewidget/timezonewidget.cpp +++ b/src/modules/locale/timezonewidget/timezonewidget.cpp @@ -65,26 +65,13 @@ TimeZoneWidget::TimeZoneWidget( const CalamaresUtils::Locale::CStringPairList& z void -TimeZoneWidget::setCurrentLocation( QString regionName, QString zoneName ) +TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone* location ) { - using namespace CalamaresUtils::Locale; - auto* region = m_zonesData.find< TZRegion >( regionName ); - if ( !region ) + if ( location == m_currentLocation ) { return; } - auto* zone = region->zones().find< TZZone >( zoneName ); - if ( zone ) - { - setCurrentLocation( zone ); - } -} - - -void -TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone* location ) -{ m_currentLocation = location; // Set zone @@ -100,7 +87,6 @@ TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone* locati // Repaint widget repaint(); - emit locationChanged( m_currentLocation ); } @@ -229,6 +215,6 @@ TimeZoneWidget::mousePressEvent( QMouseEvent* event ) // Set zone image and repaint widget setCurrentLocation( closest ); // Emit signal - emit locationChanged( m_currentLocation ); + emit locationChanged( closest ); } } diff --git a/src/modules/locale/timezonewidget/timezonewidget.h b/src/modules/locale/timezonewidget/timezonewidget.h index c070b4cd4..6bb94c0dd 100644 --- a/src/modules/locale/timezonewidget/timezonewidget.h +++ b/src/modules/locale/timezonewidget/timezonewidget.h @@ -55,21 +55,15 @@ public: explicit TimeZoneWidget( const CalamaresUtils::Locale::CStringPairList& zones, QWidget* parent = nullptr ); - /** @brief Sets a location by name - * - * @p region should be "America" or the like, while @p zone - * names a zone within that region. - */ - void setCurrentLocation( QString region, QString zone ); +public Q_SLOTS: /** @brief Sets a location by pointer * * Pointer should be within the same model as the widget uses. */ void setCurrentLocation( const TZZone* location ); - const TZZone* currentLocation() { return m_currentLocation; } - signals: + /** @brief The location has changed by mouse click */ void locationChanged( const TZZone* location ); private: