[locale] Refactor loading of timezone images

- Move the image-loading to a separate class
This commit is contained in:
Adriaan de Groot 2020-04-14 14:44:24 +02:00
parent 4b65600c08
commit 340c462062
5 changed files with 126 additions and 19 deletions

View File

@ -20,6 +20,7 @@ calamares_add_plugin( locale
LocaleViewStep.cpp
SetTimezoneJob.cpp
timezonewidget/timezonewidget.cpp
timezonewidget/TimeZoneImage.cpp
UI
RESOURCES
locale.qrc

View File

@ -0,0 +1,68 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2020, 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 "TimeZoneImage.h"
#include "utils/Logger.h"
#include <QDir>
static const char* zoneNames[] =
{ "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" } ;
TimeZoneImageList::TimeZoneImageList()
{
}
TimeZoneImageList TimeZoneImageList::fromQRC()
{
TimeZoneImageList l;
for ( const auto* zoneName : zoneNames )
{
l.append( QImage( QStringLiteral( ":/images/timezone_" ) + zoneName + ".png" ) );
l.last().setText( QStringLiteral( "zone" ), zoneName );
}
return l;
}
TimeZoneImageList TimeZoneImageList::fromDirectory(const QString& dirName)
{
TimeZoneImageList l;
QDir dir( dirName );
if ( !dir.exists() )
{
cWarning() << "TimeZone images directory" << dirName << "does not exist.";
return l;
}
for ( const auto* zoneName : zoneNames )
{
l.append( QImage( dir.filePath( QStringLiteral("timezone_" ) + zoneName + ".png" ) ) );
l.last().setText( QStringLiteral( "zone" ), zoneName );
}
return l;
}
int TimeZoneImageList::zoneCount()
{
return sizeof(zoneNames) / sizeof(zoneNames[0]);
}

View File

@ -0,0 +1,53 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2020, 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/>.
*/
#ifndef TIMEZONEIMAGE_H
#define TIMEZONEIMAGE_H
#include <QImage>
#include <QList>
using TimeZoneImage = QImage;
/** @brief All the timezone images
*
* There's one fixed list of timezone images that can be loaded
* from the QRC, or from the source directory.
*/
class TimeZoneImageList : public QList< TimeZoneImage >
{
private:
TimeZoneImageList();
public:
/** @brief loads all the images from QRC.
*
* The images are assumed to be compiled into the Qt resource
* system and are loaded from there.
*/
static TimeZoneImageList fromQRC();
/** @brief loads all the images from a specified directory.
*
* No error is returned if files are missing.
*/
static TimeZoneImageList fromDirectory( const QString& dirName );
/// @brief The **expected** number of zones in the list.
static int zoneCount();
};
#endif

View File

@ -42,6 +42,7 @@ constexpr static double MATH_PI = 3.14159265;
TimeZoneWidget::TimeZoneWidget( QWidget* parent )
: QWidget( parent )
, timeZoneImages( TimeZoneImageList::fromQRC() )
{
setMouseTracking( false );
setCursor( Qt::PointingHandCursor );
@ -64,22 +65,6 @@ TimeZoneWidget::TimeZoneWidget( QWidget* parent )
// Set size
setMinimumSize( background.size() );
setMaximumSize( background.size() );
// Zone images
for ( const auto* zoneName :
{ "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" } )
{
timeZoneImages.append( QImage( QStringLiteral( ":/images/timezone_" ) + zoneName + ".png" ) );
#ifdef DEBUG_TIMEZONES
if ( timeZoneImages.last().size() != background.size() )
{
cWarning() << "Timezone image size mismatch" << zoneName << timeZoneImages.last().size();
}
timeZoneImages.last().setText( ZONE_NAME, zoneName );
#endif
}
}

View File

@ -24,13 +24,13 @@
#ifndef TIMEZONEWIDGET_H
#define TIMEZONEWIDGET_H
#include "TimeZoneImage.h"
#include "locale/TimeZone.h"
#include <QFile>
#include <QFont>
#include <QFontMetrics>
#include <QImage>
#include <QList>
#include <QMouseEvent>
#include <QPainter>
#include <QStringList>
@ -56,7 +56,7 @@ signals:
private:
QFont font;
QImage background, pin, currentZoneImage;
QList< QImage > timeZoneImages;
TimeZoneImageList timeZoneImages;
const TZZone* m_currentLocation = nullptr; // Not owned by me
QPoint getLocationPosition( const TZZone* l ) { return getLocationPosition( l->longitude(), l->latitude() ); }