2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-07-09 16:08:51 +02:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
2020-07-09 16:08:51 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-11-25 20:51:45 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2019-11-25 20:51:45 +01:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
2019-11-25 20:51:45 +01:00
|
|
|
*/
|
|
|
|
|
2020-08-10 09:43:13 +02:00
|
|
|
/** @file Timezone data and models to go with it
|
|
|
|
*
|
|
|
|
* The TimeZoneData class holds information from zone.tab, about
|
|
|
|
* TZ names and locations (latitude and longitude) for geographic
|
|
|
|
* lookups.
|
|
|
|
*
|
|
|
|
* The RegionModel lists the regions of the world (about 12) and
|
|
|
|
* ZonesModel lists all the timezones; the RegionalZonesModel provides
|
|
|
|
* a way to restrict the view of timezones to those of a specific region.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-25 20:51:45 +01:00
|
|
|
#ifndef LOCALE_TIMEZONE_H
|
|
|
|
#define LOCALE_TIMEZONE_H
|
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
#include "DllMacro.h"
|
|
|
|
|
2020-08-05 18:01:24 +02:00
|
|
|
#include "locale/TranslatableString.h"
|
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
#include <QAbstractListModel>
|
2019-11-26 11:18:35 +01:00
|
|
|
#include <QObject>
|
2020-08-05 17:14:13 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
2020-08-05 15:11:52 +02:00
|
|
|
#include <QVariant>
|
2019-12-10 11:36:05 +01:00
|
|
|
|
2019-11-25 20:51:45 +01:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
namespace Locale
|
|
|
|
{
|
2020-08-06 10:47:47 +02:00
|
|
|
class Private;
|
2020-08-05 18:01:24 +02:00
|
|
|
class RegionalZonesModel;
|
|
|
|
class ZonesModel;
|
|
|
|
|
|
|
|
class TimeZoneData : public QObject, TranslatableString
|
|
|
|
{
|
|
|
|
friend class RegionalZonesModel;
|
|
|
|
friend class ZonesModel;
|
|
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
Q_PROPERTY( QString region READ region CONSTANT )
|
2020-08-06 14:19:27 +02:00
|
|
|
Q_PROPERTY( QString zone READ zone CONSTANT )
|
|
|
|
Q_PROPERTY( QString name READ tr CONSTANT )
|
|
|
|
Q_PROPERTY( QString countryCode READ country CONSTANT )
|
2020-08-05 18:01:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
TimeZoneData( const QString& region,
|
|
|
|
const QString& zone,
|
|
|
|
const QString& country,
|
|
|
|
double latitude,
|
|
|
|
double longitude );
|
2020-08-05 23:26:17 +02:00
|
|
|
TimeZoneData( const TimeZoneData& ) = delete;
|
|
|
|
TimeZoneData( TimeZoneData&& ) = delete;
|
|
|
|
|
2020-08-05 18:01:24 +02:00
|
|
|
QString tr() const override;
|
|
|
|
|
|
|
|
QString region() const { return m_region; }
|
2020-08-05 22:30:04 +02:00
|
|
|
QString zone() const { return key(); }
|
2020-08-05 18:01:24 +02:00
|
|
|
|
2020-08-05 23:26:17 +02:00
|
|
|
QString country() const { return m_country; }
|
|
|
|
double latitude() const { return m_latitude; }
|
|
|
|
double longitude() const { return m_longitude; }
|
|
|
|
|
2020-08-05 18:01:24 +02:00
|
|
|
private:
|
|
|
|
QString m_region;
|
|
|
|
QString m_country;
|
|
|
|
double m_latitude;
|
|
|
|
double m_longitude;
|
|
|
|
};
|
|
|
|
|
2019-11-25 20:51:45 +01:00
|
|
|
|
2020-08-05 15:11:52 +02:00
|
|
|
/** @brief The list of timezone regions
|
2020-07-09 16:08:51 +02:00
|
|
|
*
|
2020-08-05 15:11:52 +02:00
|
|
|
* The regions are a short list of global areas (Africa, America, India ..)
|
|
|
|
* which contain zones.
|
2020-07-09 16:08:51 +02:00
|
|
|
*/
|
2020-08-05 15:11:52 +02:00
|
|
|
class DLLEXPORT RegionsModel : public QAbstractListModel
|
2019-11-25 20:51:45 +01:00
|
|
|
{
|
2020-03-24 16:04:14 +01:00
|
|
|
Q_OBJECT
|
2019-11-25 20:51:45 +01:00
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
public:
|
2020-08-05 16:36:00 +02:00
|
|
|
enum Roles
|
|
|
|
{
|
2020-08-05 16:45:41 +02:00
|
|
|
NameRole = Qt::DisplayRole,
|
2020-08-05 23:26:17 +02:00
|
|
|
KeyRole = Qt::UserRole // So that currentData() will get the key
|
2020-08-05 16:36:00 +02:00
|
|
|
};
|
|
|
|
|
2020-08-05 15:17:09 +02:00
|
|
|
RegionsModel( QObject* parent = nullptr );
|
2020-08-05 15:11:52 +02:00
|
|
|
virtual ~RegionsModel() override;
|
2019-12-09 21:46:10 +01:00
|
|
|
|
|
|
|
int rowCount( const QModelIndex& parent ) const override;
|
|
|
|
QVariant data( const QModelIndex& index, int role ) const override;
|
|
|
|
|
2020-08-05 16:36:00 +02:00
|
|
|
QHash< int, QByteArray > roleNames() const override;
|
|
|
|
|
2020-08-06 14:19:27 +02:00
|
|
|
public Q_SLOTS:
|
|
|
|
/** @brief Provides a human-readable version of the region
|
|
|
|
*
|
|
|
|
* Returns @p region unchanged if there is no such region
|
|
|
|
* or no translation for the region's name.
|
|
|
|
*/
|
|
|
|
QString tr( const QString& region ) const;
|
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
private:
|
2020-08-05 15:17:09 +02:00
|
|
|
Private* m_private;
|
2019-12-09 21:46:10 +01:00
|
|
|
};
|
|
|
|
|
2020-08-05 16:21:05 +02:00
|
|
|
class DLLEXPORT ZonesModel : public QAbstractListModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2020-08-05 16:45:41 +02:00
|
|
|
enum Roles
|
|
|
|
{
|
|
|
|
NameRole = Qt::DisplayRole,
|
2020-08-05 23:26:17 +02:00
|
|
|
KeyRole = Qt::UserRole, // So that currentData() will get the key
|
|
|
|
RegionRole = Qt::UserRole + 1
|
2020-08-05 16:45:41 +02:00
|
|
|
};
|
|
|
|
|
2020-08-05 16:21:05 +02:00
|
|
|
ZonesModel( QObject* parent = nullptr );
|
|
|
|
virtual ~ZonesModel() override;
|
|
|
|
|
|
|
|
int rowCount( const QModelIndex& parent ) const override;
|
|
|
|
QVariant data( const QModelIndex& index, int role ) const override;
|
|
|
|
|
2020-08-05 16:45:41 +02:00
|
|
|
QHash< int, QByteArray > roleNames() const override;
|
|
|
|
|
2020-08-06 10:47:47 +02:00
|
|
|
/** @brief Iterator for the underlying list of zones
|
2020-08-05 23:46:14 +02:00
|
|
|
*
|
|
|
|
* Iterates over all the zones in the model. Operator * may return
|
|
|
|
* a @c nullptr when the iterator is not valid. Typical usage:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* for( auto it = model.begin(); it; ++it )
|
|
|
|
* {
|
|
|
|
* const auto* zonedata = *it;
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
friend class ZonesModel;
|
|
|
|
Iterator( const Private* m )
|
|
|
|
: m_index( 0 )
|
|
|
|
, m_p( m )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
operator bool() const;
|
|
|
|
void operator++() { ++m_index; }
|
|
|
|
const TimeZoneData* operator*() const;
|
|
|
|
int index() const { return m_index; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_index;
|
|
|
|
const Private* m_p;
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin() const { return Iterator( m_private ); }
|
2020-08-05 22:30:04 +02:00
|
|
|
|
2020-08-08 13:45:32 +02:00
|
|
|
/** @brief Look up TZ data based on an arbitrary distance function
|
|
|
|
*
|
|
|
|
* This is a generic method that can define distance in whatever
|
|
|
|
* coordinate system is wanted; returns the zone with the smallest
|
|
|
|
* distance. The @p distanceFunc must return "the distance" for
|
|
|
|
* each zone. It would be polite to return something non-negative.
|
|
|
|
*
|
|
|
|
* Note: not a slot, because the parameter isn't moc-able.
|
|
|
|
*/
|
|
|
|
const TimeZoneData* find( const std::function< double( const TimeZoneData* ) >& distanceFunc ) const;
|
|
|
|
|
2020-08-06 10:47:47 +02:00
|
|
|
public Q_SLOTS:
|
|
|
|
/** @brief Look up TZ data based on its name.
|
|
|
|
*
|
|
|
|
* Returns @c nullptr if not found.
|
|
|
|
*/
|
|
|
|
const TimeZoneData* find( const QString& region, const QString& zone ) const;
|
|
|
|
|
|
|
|
/** @brief Look up TZ data based on the location.
|
|
|
|
*
|
2020-08-08 13:45:32 +02:00
|
|
|
* Returns the nearest zone to the given lat and lon. This is a
|
|
|
|
* convenience function for calling find(), below, with a standard
|
|
|
|
* distance function based on the distance between the given
|
|
|
|
* location (lat and lon) and each zone's given location.
|
2020-08-06 10:47:47 +02:00
|
|
|
*/
|
|
|
|
const TimeZoneData* find( double latitude, double longitude ) const;
|
|
|
|
|
|
|
|
/** @brief Look up TZ data based on the location.
|
|
|
|
*
|
|
|
|
* Returns the nearest zone, or New York. This is non-const for QML
|
|
|
|
* purposes, but the object should be considered const anyway.
|
|
|
|
*/
|
|
|
|
QObject* lookup( double latitude, double longitude ) const;
|
|
|
|
|
2020-08-05 16:21:05 +02:00
|
|
|
private:
|
|
|
|
Private* m_private;
|
|
|
|
};
|
|
|
|
|
2020-08-05 17:14:13 +02:00
|
|
|
class DLLEXPORT RegionalZonesModel : public QSortFilterProxyModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY( QString region READ region WRITE setRegion NOTIFY regionChanged )
|
|
|
|
|
|
|
|
public:
|
|
|
|
RegionalZonesModel( ZonesModel* source, QObject* parent = nullptr );
|
|
|
|
~RegionalZonesModel() override;
|
|
|
|
|
|
|
|
bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const override;
|
|
|
|
|
|
|
|
QString region() const { return m_region; }
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
void setRegion( const QString& r );
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void regionChanged( const QString& );
|
|
|
|
|
|
|
|
private:
|
|
|
|
Private* m_private;
|
|
|
|
QString m_region;
|
|
|
|
};
|
|
|
|
|
2020-08-05 16:21:05 +02:00
|
|
|
|
2019-11-26 11:18:35 +01:00
|
|
|
} // namespace Locale
|
|
|
|
} // namespace CalamaresUtils
|
2019-11-25 20:51:45 +01:00
|
|
|
|
|
|
|
#endif // LOCALE_TIMEZONE_H
|