[locale] Apply coding style to timezonewidget/

This commit is contained in:
Adriaan de Groot 2019-12-10 17:42:58 +01:00
parent 1e04924224
commit 7354d35faf
5 changed files with 153 additions and 93 deletions

View File

@ -30,7 +30,7 @@
//### Private variables //### Private variables
//### //###
QHash<QString, QHash<QString, QList<LocaleGlobal::Locale> > > LocaleGlobal::locales; QHash< QString, QHash< QString, QList< LocaleGlobal::Locale > > > LocaleGlobal::locales;
//### //###
@ -48,13 +48,12 @@ LocaleGlobal::Location::pretty( const QString& s )
QString QString
LocaleGlobal::Location::comment() const LocaleGlobal::Location::comment() const
{ {
QTimeZone qtz = QTimeZone( QString( "%1/%2" ) QTimeZone qtz = QTimeZone( QString( "%1/%2" ).arg( region ).arg( zone ).toLatin1() );
.arg( region )
.arg( zone ).toLatin1() );
return qtz.comment(); return qtz.comment();
} }
LocaleGlobal::Location & LocaleGlobal::Location::operator=(const CalamaresUtils::Locale::TZZone& location) LocaleGlobal::Location&
LocaleGlobal::Location::operator=( const CalamaresUtils::Locale::TZZone& location )
{ {
region = location.region(); region = location.region();
zone = location.key(); zone = location.key();
@ -66,67 +65,86 @@ LocaleGlobal::Location & LocaleGlobal::Location::operator=(const CalamaresUtils:
void void
LocaleGlobal::init() { LocaleGlobal::init()
{
// TODO: Error handling // TODO: Error handling
initLocales(); initLocales();
} }
QHash< QString, QHash< QString, QList< LocaleGlobal::Locale > > > QHash< QString, QHash< QString, QList< LocaleGlobal::Locale > > >
LocaleGlobal::getLocales() { LocaleGlobal::getLocales()
{
return locales; return locales;
} }
//### //###
//### Private methods //### Private methods
//### //###
void void
LocaleGlobal::initLocales() { LocaleGlobal::initLocales()
{
locales.clear(); locales.clear();
QStringList files = QDir(LOCALESDIR).entryList(QDir::Files, QDir::Name); QStringList files = QDir( LOCALESDIR ).entryList( QDir::Files, QDir::Name );
for (int i = 0; i < files.size(); ++i) { for ( int i = 0; i < files.size(); ++i )
QString filename = files.at(i); {
QFile file(QString(LOCALESDIR) + "/" + filename); QString filename = files.at( i );
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) QFile file( QString( LOCALESDIR ) + "/" + filename );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
continue; continue;
}
QTextStream in(&file); QTextStream in( &file );
QString commentChar = "%"; QString commentChar = "%";
Locale locale; Locale locale;
QString lang, territory; QString lang, territory;
locale.locale = filename; locale.locale = filename;
while (!in.atEnd()) { while ( !in.atEnd() )
{
QString line = in.readLine().trimmed(); QString line = in.readLine().trimmed();
QStringList split = line.split(commentChar, QString::KeepEmptyParts).first().split(QRegExp(" (?=[^\"]*(\"[^\"]*\"[^\"]*)*$)"), QString::SkipEmptyParts); QStringList split = line.split( commentChar, QString::KeepEmptyParts )
.first()
.split( QRegExp( " (?=[^\"]*(\"[^\"]*\"[^\"]*)*$)" ), QString::SkipEmptyParts );
if (split.size() < 2) if ( split.size() < 2 )
{
continue; continue;
}
QString sub1 = QString(split.at(0)).remove("\""); QString sub1 = QString( split.at( 0 ) ).remove( "\"" );
QString sub2 = QString(split.at(1)).remove("\""); QString sub2 = QString( split.at( 1 ) ).remove( "\"" );
if (sub1 == "comment_char") if ( sub1 == "comment_char" )
{
commentChar = sub2; commentChar = sub2;
else if (sub1 == "title") }
else if ( sub1 == "title" )
{
locale.description = sub2; locale.description = sub2;
else if (sub1 == "territory") }
territory= sub2; else if ( sub1 == "territory" )
else if (sub1 == "language") {
territory = sub2;
}
else if ( sub1 == "language" )
{
lang = sub2; lang = sub2;
} }
}
if (lang.isEmpty() || territory.isEmpty()) if ( lang.isEmpty() || territory.isEmpty() )
{
continue; continue;
}
locales[lang][territory].append(locale); locales[ lang ][ territory ].append( locale );
} }
} }

View File

@ -24,25 +24,25 @@
#ifndef LOCALEGLOBAL_H #ifndef LOCALEGLOBAL_H
#define LOCALEGLOBAL_H #define LOCALEGLOBAL_H
#include <QString> #include "localeconst.h"
#include <QFile> #include <QDebug>
#include <QTextStream>
#include <QDir> #include <QDir>
#include <QStringList> #include <QFile>
#include <QList>
#include <QHash> #include <QHash>
#include <QList>
#include <QMap> #include <QMap>
#include <QRegExp> #include <QRegExp>
#include <QDebug> #include <QString>
#include "localeconst.h" #include <QStringList>
#include <QTextStream>
namespace CalamaresUtils namespace CalamaresUtils
{ {
namespace Locale namespace Locale
{ {
class TZZone; class TZZone;
}
} }
} // namespace CalamaresUtils
class LocaleGlobal class LocaleGlobal
{ {
@ -59,19 +59,20 @@ public:
static QString pretty( const QString& s ); static QString pretty( const QString& s );
QString comment() const; QString comment() const;
Location& operator=(const CalamaresUtils::Locale::TZZone&); Location& operator=( const CalamaresUtils::Locale::TZZone& );
}; };
static void init(); static void init();
static QHash<QString, QHash<QString, QList<LocaleGlobal::Locale> > > getLocales(); static QHash< QString, QHash< QString, QList< LocaleGlobal::Locale > > > getLocales();
private: private:
static QHash<QString, QHash<QString, QList<LocaleGlobal::Locale> > > locales; static QHash< QString, QHash< QString, QList< LocaleGlobal::Locale > > > locales;
static void initLocales(); static void initLocales();
}; };
inline QDebug& operator <<( QDebug& s, const LocaleGlobal::Location& l ) inline QDebug&
operator<<( QDebug& s, const LocaleGlobal::Location& l )
{ {
return s << l.region << '/' << l.zone << '(' << l.country << ") @N" << l.latitude << 'E' << l.longitude; return s << l.region << '/' << l.zone << '(' << l.country << ") @N" << l.latitude << 'E' << l.longitude;
} }

View File

@ -23,8 +23,8 @@
#include <cmath> #include <cmath>
#include "utils/Logger.h"
#include "locale/TimeZone.h" #include "locale/TimeZone.h"
#include "utils/Logger.h"
#include "timezonewidget.h" #include "timezonewidget.h"
@ -38,8 +38,8 @@ constexpr static double MATH_PI = 3.14159265;
constexpr static QLatin1String ZONE_NAME( "zone" ); constexpr static QLatin1String ZONE_NAME( "zone" );
#endif #endif
TimeZoneWidget::TimeZoneWidget( QWidget* parent ) : TimeZoneWidget::TimeZoneWidget( QWidget* parent )
QWidget( parent ) : QWidget( parent )
{ {
setMouseTracking( false ); setMouseTracking( false );
setCursor( Qt::PointingHandCursor ); setCursor( Qt::PointingHandCursor );
@ -60,7 +60,8 @@ TimeZoneWidget::TimeZoneWidget( QWidget* parent ) :
QStringList zones = QString( ZONES ).split( " ", QString::SkipEmptyParts ); QStringList zones = QString( ZONES ).split( " ", QString::SkipEmptyParts );
for ( int i = 0; i < zones.size(); ++i ) for ( int i = 0; i < zones.size(); ++i )
{ {
timeZoneImages.append( QImage( ":/images/timezone_" + zones.at( i ) + ".png" ).scaled( X_SIZE, Y_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) ); timeZoneImages.append( QImage( ":/images/timezone_" + zones.at( i ) + ".png" )
.scaled( X_SIZE, Y_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
#ifdef DEBUG_TIMEZONES #ifdef DEBUG_TIMEZONES
timeZoneImages.last().setText( ZONE_NAME, zones.at( i ) ); timeZoneImages.last().setText( ZONE_NAME, zones.at( i ) );
#endif #endif
@ -68,17 +69,18 @@ TimeZoneWidget::TimeZoneWidget( QWidget* parent ) :
} }
void TimeZoneWidget::setCurrentLocation( QString regionName, QString zoneName ) void
TimeZoneWidget::setCurrentLocation( QString regionName, QString zoneName )
{ {
using namespace CalamaresUtils::Locale; using namespace CalamaresUtils::Locale;
const auto& regions = TZRegion::fromZoneTab(); const auto& regions = TZRegion::fromZoneTab();
auto *region = regions.find<TZRegion>( regionName ); auto* region = regions.find< TZRegion >( regionName );
if ( !region ) if ( !region )
{ {
return; return;
} }
auto *zone = region->zones().find< TZZone >(zoneName); auto* zone = region->zones().find< TZZone >( zoneName );
if ( zone ) if ( zone )
{ {
setCurrentLocation( zone ); setCurrentLocation( zone );
@ -86,7 +88,8 @@ void TimeZoneWidget::setCurrentLocation( QString regionName, QString zoneName )
} }
void TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone *location ) void
TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone* location )
{ {
currentLocation = *location; currentLocation = *location;
@ -104,7 +107,7 @@ void TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone *l
for ( int i = 0; i < timeZoneImages.size(); ++i ) for ( int i = 0; i < timeZoneImages.size(); ++i )
{ {
QImage zone = timeZoneImages[i]; QImage zone = timeZoneImages[ i ];
// If not transparent set as current // If not transparent set as current
if ( zone.pixel( pos ) != RGB_TRANSPARENT ) if ( zone.pixel( pos ) != RGB_TRANSPARENT )
@ -119,7 +122,9 @@ void TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone *l
cDebug() << Logger::SubEntry << "First zone found" << i << zone.text( ZONE_NAME ); cDebug() << Logger::SubEntry << "First zone found" << i << zone.text( ZONE_NAME );
} }
else else
{
cDebug() << Logger::SubEntry << "Also in zone" << i << zone.text( ZONE_NAME ); cDebug() << Logger::SubEntry << "Also in zone" << i << zone.text( ZONE_NAME );
}
#else #else
currentZoneImage = zone; currentZoneImage = zone;
break; break;
@ -132,13 +137,13 @@ void TimeZoneWidget::setCurrentLocation( const CalamaresUtils::Locale::TZZone *l
} }
//### //###
//### Private //### Private
//### //###
QPoint TimeZoneWidget::getLocationPosition( double longitude, double latitude ) QPoint
TimeZoneWidget::getLocationPosition( double longitude, double latitude )
{ {
const int width = this->width(); const int width = this->width();
const int height = this->height(); const int height = this->height();
@ -152,39 +157,64 @@ QPoint TimeZoneWidget::getLocationPosition( double longitude, double latitude )
// of the different cities / regions looks ok -- at least Thule ends up in the right // of the different cities / regions looks ok -- at least Thule ends up in the right
// country, and Inuvik isn't in the ocean. // country, and Inuvik isn't in the ocean.
if ( latitude > 70.0 ) if ( latitude > 70.0 )
{
y -= sin( MATH_PI * ( latitude - 70.0 ) / 56.0 ) * MAP_Y_OFFSET * height * 0.8; y -= sin( MATH_PI * ( latitude - 70.0 ) / 56.0 ) * MAP_Y_OFFSET * height * 0.8;
}
if ( latitude > 74.0 ) if ( latitude > 74.0 )
{
y += 4; y += 4;
}
if ( latitude > 69.0 ) if ( latitude > 69.0 )
{
y -= 2; y -= 2;
}
if ( latitude > 59.0 ) if ( latitude > 59.0 )
{
y -= 4 * int( ( latitude - 54.0 ) / 5.0 ); y -= 4 * int( ( latitude - 54.0 ) / 5.0 );
}
if ( latitude > 54.0 ) if ( latitude > 54.0 )
{
y -= 2; y -= 2;
}
if ( latitude > 49.0 ) if ( latitude > 49.0 )
y -= int ( (latitude - 44.0) / 5.0 ); {
y -= int( ( latitude - 44.0 ) / 5.0 );
}
// Far south, some stretching occurs as well, but it is less pronounced. // Far south, some stretching occurs as well, but it is less pronounced.
// Move down by 1 pixel per 5 degrees past 10 south // Move down by 1 pixel per 5 degrees past 10 south
if ( latitude < 0 ) if ( latitude < 0 )
y += int( (-latitude) / 5.0 ); {
y += int( ( -latitude ) / 5.0 );
}
// Antarctica isn't shown on the map, but you could try clicking there // Antarctica isn't shown on the map, but you could try clicking there
if ( latitude < -60 ) if ( latitude < -60 )
{
y = height - 1; y = height - 1;
}
if ( x < 0 ) if ( x < 0 )
x = width+x; {
x = width + x;
}
if ( x >= width ) if ( x >= width )
{
x -= width; x -= width;
}
if ( y < 0 ) if ( y < 0 )
y = height+y; {
y = height + y;
}
if ( y >= height ) if ( y >= height )
{
y -= height; y -= height;
}
return QPoint( int(x), int(y) ); return QPoint( int( x ), int( y ) );
} }
void TimeZoneWidget::paintEvent( QPaintEvent* ) void
TimeZoneWidget::paintEvent( QPaintEvent* )
{ {
const int width = this->width(); const int width = this->width();
const int height = this->height(); const int height = this->height();
@ -203,18 +233,20 @@ void TimeZoneWidget::paintEvent( QPaintEvent* )
#ifdef DEBUG_TIMEZONES #ifdef DEBUG_TIMEZONES
QPoint point = getLocationPosition( currentLocation.longitude, currentLocation.latitude ); QPoint point = getLocationPosition( currentLocation.longitude, currentLocation.latitude );
// Draw latitude lines // Draw latitude lines
for ( int y_lat = -50; y_lat < 80 ; y_lat+=5 ) for ( int y_lat = -50; y_lat < 80; y_lat += 5 )
{ {
QPen p( y_lat ? Qt::black : Qt::red ); QPen p( y_lat ? Qt::black : Qt::red );
p.setWidth( 0 ); p.setWidth( 0 );
painter.setPen( p ); painter.setPen( p );
QPoint latLine0( getLocationPosition( 0, y_lat ) ); QPoint latLine0( getLocationPosition( 0, y_lat ) );
int llx = latLine0.x() + ((y_lat & 1) ? -10 : 0); int llx = latLine0.x() + ( ( y_lat & 1 ) ? -10 : 0 );
int lly = latLine0.y(); int lly = latLine0.y();
for ( int c = 0 ; c < width ; ++c ) for ( int c = 0; c < width; ++c )
{
painter.drawPoint( c, lly ); painter.drawPoint( c, lly );
} }
}
// Just a dot in the selected location, no label // Just a dot in the selected location, no label
painter.setPen( Qt::red ); painter.setPen( Qt::red );
painter.drawPoint( point ); painter.drawPoint( point );
@ -222,22 +254,30 @@ void TimeZoneWidget::paintEvent( QPaintEvent* )
// Draw pin at current location // Draw pin at current location
QPoint point = getLocationPosition( currentLocation.longitude, currentLocation.latitude ); QPoint point = getLocationPosition( currentLocation.longitude, currentLocation.latitude );
painter.drawImage( point.x() - pin.width()/2, point.y() - pin.height()/2, pin ); painter.drawImage( point.x() - pin.width() / 2, point.y() - pin.height() / 2, pin );
// Draw text and box // Draw text and box
const int textWidth = fontMetrics.horizontalAdvance( LocaleGlobal::Location::pretty( currentLocation.zone ) ); const int textWidth = fontMetrics.horizontalAdvance( LocaleGlobal::Location::pretty( currentLocation.zone ) );
const int textHeight = fontMetrics.height(); const int textHeight = fontMetrics.height();
QRect rect = QRect( point.x() - textWidth/2 - 5, point.y() - textHeight - 8, textWidth + 10, textHeight - 2 ); QRect rect = QRect( point.x() - textWidth / 2 - 5, point.y() - textHeight - 8, textWidth + 10, textHeight - 2 );
if ( rect.x() <= 5 ) if ( rect.x() <= 5 )
{
rect.moveLeft( 5 ); rect.moveLeft( 5 );
if ( rect.right() >= width-5 ) }
if ( rect.right() >= width - 5 )
{
rect.moveRight( width - 5 ); rect.moveRight( width - 5 );
}
if ( rect.y() <= 5 ) if ( rect.y() <= 5 )
{
rect.moveTop( 5 ); rect.moveTop( 5 );
if ( rect.y() >= height-5 ) }
rect.moveBottom( height-5 ); if ( rect.y() >= height - 5 )
{
rect.moveBottom( height - 5 );
}
painter.setPen( QPen() ); // no pen painter.setPen( QPen() ); // no pen
painter.setBrush( QColor( 40, 40, 40 ) ); painter.setBrush( QColor( 40, 40, 40 ) );
@ -250,11 +290,13 @@ void TimeZoneWidget::paintEvent( QPaintEvent* )
} }
void
void TimeZoneWidget::mousePressEvent( QMouseEvent* event ) TimeZoneWidget::mousePressEvent( QMouseEvent* event )
{ {
if ( event->button() != Qt::LeftButton ) if ( event->button() != Qt::LeftButton )
{
return; return;
}
// Set nearest location // Set nearest location
int nX = 999999, mX = event->pos().x(); int nX = 999999, mX = event->pos().x();
@ -264,12 +306,12 @@ void TimeZoneWidget::mousePressEvent( QMouseEvent* event )
const TZZone* closest = nullptr; const TZZone* closest = nullptr;
for ( const auto* region_p : TZRegion::fromZoneTab() ) for ( const auto* region_p : TZRegion::fromZoneTab() )
{ {
const auto* region = dynamic_cast<const TZRegion*>(region_p); const auto* region = dynamic_cast< const TZRegion* >( region_p );
if ( region ) if ( region )
{ {
for ( const auto* zone_p : region->zones() ) for ( const auto* zone_p : region->zones() )
{ {
const auto* zone = dynamic_cast<const TZZone*>(zone_p); const auto* zone = dynamic_cast< const TZZone* >( zone_p );
if ( zone ) if ( zone )
{ {
QPoint locPos = getLocationPosition( zone->longitude(), zone->latitude() ); QPoint locPos = getLocationPosition( zone->longitude(), zone->latitude() );

View File

@ -28,20 +28,22 @@
#include "locale/TimeZone.h" #include "locale/TimeZone.h"
#include <QWidget>
#include <QPainter>
#include <QImage>
#include <QFile> #include <QFile>
#include <QTextStream>
#include <QList>
#include <QStringList>
#include <QMouseEvent>
#include <QFontMetrics>
#include <QFont> #include <QFont>
#include <QFontMetrics>
#include <QImage>
#include <QList>
#include <QMouseEvent>
#include <QPainter>
#include <QStringList>
#include <QTextStream>
#include <QWidget>
#define RGB_TRANSPARENT 0 #define RGB_TRANSPARENT 0
#define ZONES "0.0 1.0 2.0 3.0 3.5 4.0 4.5 5.0 5.5 5.75 6.0 6.5 7.0 8.0 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.75 13.0 -1.0 -2.0 -3.0 -3.5 -4.0 -4.5 -5.0 -5.5 -6.0 -7.0 -8.0 -9.0 -9.5 -10.0 -11.0" #define ZONES \
"0.0 1.0 2.0 3.0 3.5 4.0 4.5 5.0 5.5 5.75 6.0 6.5 7.0 8.0 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.75 13.0 -1.0 -2.0 " \
"-3.0 -3.5 -4.0 -4.5 -5.0 -5.5 -6.0 -7.0 -8.0 -9.0 -9.5 -10.0 -11.0"
#define X_SIZE 780 #define X_SIZE 780
#define Y_SIZE 340 #define Y_SIZE 340
@ -52,12 +54,9 @@ class TimeZoneWidget : public QWidget
public: public:
explicit TimeZoneWidget( QWidget* parent = nullptr ); explicit TimeZoneWidget( QWidget* parent = nullptr );
LocaleGlobal::Location getCurrentLocation() LocaleGlobal::Location getCurrentLocation() { return currentLocation; }
{
return currentLocation;
}
void setCurrentLocation( QString region, QString zone ); void setCurrentLocation( QString region, QString zone );
void setCurrentLocation( const CalamaresUtils::Locale::TZZone *location ); void setCurrentLocation( const CalamaresUtils::Locale::TZZone* location );
signals: signals:
void locationChanged( LocaleGlobal::Location location ); void locationChanged( LocaleGlobal::Location location );
@ -65,7 +64,7 @@ signals:
private: private:
QFont font; QFont font;
QImage background, pin, currentZoneImage; QImage background, pin, currentZoneImage;
QList<QImage> timeZoneImages; QList< QImage > timeZoneImages;
LocaleGlobal::Location currentLocation; LocaleGlobal::Location currentLocation;
QPoint getLocationPosition( const LocaleGlobal::Location& l ) QPoint getLocationPosition( const LocaleGlobal::Location& l )