calamares/src/modules/locale/LocalePage.cpp

239 lines
7.6 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
2019-01-08 22:30:12 +01:00
* Copyright 2017-2019, Adriaan de Groot <groot@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calamares is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LocalePage.h"
#include "Config.h"
2020-07-21 17:44:44 +02:00
#include "LCLocaleDialog.h"
#include "timezonewidget/timezonewidget.h"
#include "utils/CalamaresUtilsGui.h"
#include "utils/Logger.h"
#include "utils/RAII.h"
#include "utils/Retranslator.h"
#include <QBoxLayout>
#include <QComboBox>
#include <QLabel>
#include <QPointer>
2019-09-07 15:48:22 +02:00
#include <QPushButton>
LocalePage::LocalePage( Config* config, QWidget* parent )
2016-07-27 13:35:03 +02:00
: QWidget( parent )
, m_config( config )
, m_blockTzWidgetSet( false )
{
QBoxLayout* mainLayout = new QVBoxLayout;
QBoxLayout* tzwLayout = new QHBoxLayout;
m_tzWidget = new TimeZoneWidget( config->timezoneData(), this );
tzwLayout->addStretch();
tzwLayout->addWidget( m_tzWidget );
tzwLayout->addStretch();
// Adjust for margins and spacing in this page
m_tzWidget->setMinimumHeight( m_tzWidget->minimumHeight() + 12 ); // 2 * spacing
QBoxLayout* zoneAndRegionLayout = new QHBoxLayout;
m_regionLabel = new QLabel( this );
zoneAndRegionLayout->addWidget( m_regionLabel );
m_regionCombo = new QComboBox( this );
zoneAndRegionLayout->addWidget( m_regionCombo );
m_regionCombo->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
m_regionLabel->setBuddy( m_regionCombo );
zoneAndRegionLayout->addSpacing( 20 );
m_zoneLabel = new QLabel( this );
zoneAndRegionLayout->addWidget( m_zoneLabel );
m_zoneCombo = new QComboBox( this );
m_zoneCombo->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
zoneAndRegionLayout->addWidget( m_zoneCombo );
m_zoneLabel->setBuddy( m_zoneCombo );
QBoxLayout* localeLayout = new QHBoxLayout;
m_localeLabel = new QLabel( this );
m_localeLabel->setWordWrap( true );
m_localeLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
localeLayout->addWidget( m_localeLabel );
m_localeChangeButton = new QPushButton( this );
m_localeChangeButton->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
localeLayout->addWidget( m_localeChangeButton );
QBoxLayout* formatsLayout = new QHBoxLayout;
m_formatsLabel = new QLabel( this );
m_formatsLabel->setWordWrap( true );
m_formatsLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
formatsLayout->addWidget( m_formatsLabel );
m_formatsChangeButton = new QPushButton( this );
m_formatsChangeButton->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
formatsLayout->addWidget( m_formatsChangeButton );
mainLayout->addLayout( tzwLayout );
mainLayout->addStretch();
mainLayout->addLayout( zoneAndRegionLayout );
mainLayout->addStretch();
mainLayout->addLayout( localeLayout );
mainLayout->addLayout( formatsLayout );
setMinimumWidth( m_tzWidget->width() );
setLayout( mainLayout );
// Set up the location before connecting signals, to avoid a signal
// storm as various parts interact.
m_regionCombo->setModel( m_config->regionModel() );
locationChanged( m_config->currentLocation() ); // doesn't inform TZ widget
m_tzWidget->setCurrentLocation( m_config->currentLocation() );
connect( config, &Config::currentLCStatusChanged, m_formatsLabel, &QLabel::setText );
connect( config, &Config::currentLanguageStatusChanged, m_localeLabel, &QLabel::setText );
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_localeChangeButton, &QPushButton::clicked, this, &LocalePage::changeLocale );
connect( m_formatsChangeButton, &QPushButton::clicked, this, &LocalePage::changeFormats );
CALAMARES_RETRANSLATE_SLOT( &LocalePage::updateLocaleLabels )
}
LocalePage::~LocalePage() {}
void
LocalePage::updateLocaleLabels()
{
m_regionLabel->setText( tr( "Region:" ) );
m_zoneLabel->setText( tr( "Zone:" ) );
m_localeChangeButton->setText( tr( "&Change..." ) );
m_formatsChangeButton->setText( tr( "&Change..." ) );
m_localeLabel->setText( m_config->currentLanguageStatus() );
m_formatsLabel->setText( m_config->currentLCStatus() );
}
2014-07-08 18:25:54 +02:00
2014-11-26 18:52:44 +01:00
void
LocalePage::onActivate()
{
m_regionCombo->setFocus();
updateLocaleLabels();
}
void
LocalePage::regionChanged( int currentIndex )
{
using namespace CalamaresUtils::Locale;
Q_UNUSED( currentIndex )
QString selectedRegion = m_regionCombo->currentData().toString();
TZRegion* region = m_config->timezoneData().find< TZRegion >( selectedRegion );
if ( !region )
{
return;
}
{
cSignalBlocker b( m_zoneCombo );
m_zoneCombo->setModel( new CStringListModel( region->zones() ) );
}
m_zoneCombo->currentIndexChanged( m_zoneCombo->currentIndex() );
}
void
LocalePage::zoneChanged( int currentIndex )
{
Q_UNUSED( currentIndex )
if ( !m_blockTzWidgetSet )
{
m_config->setCurrentLocation( m_regionCombo->currentData().toString(), m_zoneCombo->currentData().toString() );
}
}
void
LocalePage::locationChanged( const CalamaresUtils::Locale::TZZone* location )
{
if ( !location )
{
return;
}
cBoolSetter< true > b( m_blockTzWidgetSet );
// Set region index
int index = m_regionCombo->findData( location->region() );
if ( index < 0 )
2019-09-07 15:48:22 +02:00
{
return;
2019-09-07 15:48:22 +02:00
}
m_regionCombo->setCurrentIndex( index );
// Set zone index
index = m_zoneCombo->findData( location->zone() );
if ( index < 0 )
2019-09-07 15:48:22 +02:00
{
return;
2019-09-07 15:48:22 +02:00
}
m_zoneCombo->setCurrentIndex( index );
}
void
LocalePage::changeLocale()
{
QPointer< LCLocaleDialog > dlg(
new LCLocaleDialog( m_config->localeConfiguration().language(), m_config->supportedLocales(), this ) );
dlg->exec();
if ( dlg && dlg->result() == QDialog::Accepted && !dlg->selectedLCLocale().isEmpty() )
{
m_config->setLanguageExplicitly( dlg->selectedLCLocale() );
updateLocaleLabels();
}
delete dlg;
}
void
LocalePage::changeFormats()
{
QPointer< LCLocaleDialog > dlg(
new LCLocaleDialog( m_config->localeConfiguration().lc_numeric, m_config->supportedLocales(), this ) );
dlg->exec();
if ( dlg && dlg->result() == QDialog::Accepted && !dlg->selectedLCLocale().isEmpty() )
{
m_config->setLCLocaleExplicitly( dlg->selectedLCLocale() );
updateLocaleLabels();
}
delete dlg;
}