Commit 76e8ce64 authored by Volker Krause's avatar Volker Krause

Port time labels to QTimeZone.

parent 6e3b5d35
......@@ -27,11 +27,10 @@
#include "timelabelszone.h"
#include "timescaleconfigdialog.h"
#include <QIcon>
#include <KLocalizedString>
#include <KTimeZone>
#include <QFrame>
#include <QIcon>
#include <QMenu>
#include <QPainter>
#include <QPointer>
......@@ -39,12 +38,11 @@
using namespace EventViews;
TimeLabels::TimeLabels(const KDateTime::Spec &spec, int rows,
TimeLabelsZone *parent, Qt::WindowFlags f)
: QFrame(parent, f)
TimeLabels::TimeLabels(const QTimeZone &zone, int rows, TimeLabelsZone *parent, Qt::WindowFlags f) :
QFrame(parent, f),
mTimezone(zone)
{
mTimeLabelsZone = parent;
mSpec = spec;
mRows = rows;
mMiniWidth = 0;
......@@ -59,8 +57,8 @@ TimeLabels::TimeLabels(const KDateTime::Spec &spec, int rows,
colorMousePos();
mAgenda = Q_NULLPTR;
if (mSpec.isValid()) {
setToolTip(i18n("Timezone:") + i18n(mSpec.timeZone().name().toUtf8()));
if (mTimezone.isValid()) {
setToolTip(i18n("Timezone:") + i18n(mTimezone.id()));
}
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
......@@ -177,10 +175,10 @@ void TimeLabels::paintEvent(QPaintEvent *)
const int cy = -y();// y() returns a negative value.
const int beginning =
!mSpec.isValid() ?
!mTimezone.isValid() ?
0 :
(mSpec.timeZone().currentOffset() -
mTimeLabelsZone->preferences()->timeSpec().timeZone().currentOffset()) / (60 * 60);
(mTimezone.offsetFromUtc(QDateTime::currentDateTimeUtc()) -
mTimeLabelsZone->preferences()->timeZone().offsetFromUtc(QDateTime::currentDateTimeUtc())) / (60 * 60);
// bug: the parameters cx and cw are the areas that need to be
// redrawn, not the area of the widget. unfortunately, this
......@@ -296,10 +294,10 @@ void TimeLabels::contextMenuEvent(QContextMenuEvent *event)
popup.addAction(QIcon::fromTheme(QStringLiteral("document-properties")), i18n("&Add Timezones..."));
QAction *removeTimeZone =
popup.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")),
i18n("&Remove Timezone %1", i18n(mSpec.timeZone().name().toUtf8())));
if (!mSpec.isValid() ||
i18n("&Remove Timezone %1", i18n(mTimezone.id())));
if (!mTimezone.isValid() ||
!mTimeLabelsZone->preferences()->timeScaleTimezones().count() ||
mSpec == mTimeLabelsZone->preferences()->timeSpec()) {
mTimezone == mTimeLabelsZone->preferences()->timeZone()) {
removeTimeZone->setEnabled(false);
}
......@@ -313,7 +311,7 @@ void TimeLabels::contextMenuEvent(QContextMenuEvent *event)
delete dialog;
} else if (activatedAction == removeTimeZone) {
QStringList list = mTimeLabelsZone->preferences()->timeScaleTimezones();
list.removeAll(mSpec.timeZone().name());
list.removeAll(QString::fromUtf8(mTimezone.id()));
mTimeLabelsZone->preferences()->setTimeScaleTimezones(list);
mTimeLabelsZone->preferences()->writeConfig();
mTimeLabelsZone->reset();
......@@ -322,41 +320,41 @@ void TimeLabels::contextMenuEvent(QContextMenuEvent *event)
}
}
KDateTime::Spec TimeLabels::timeSpec()
QTimeZone TimeLabels::timeZone() const
{
return mSpec;
return mTimezone;
}
QString TimeLabels::header() const
{
return i18n(mSpec.timeZone().name().toUtf8());
return i18n(mTimezone.id());
}
QString TimeLabels::headerToolTip() const
{
KTimeZone tz = mSpec.timeZone();
QString toolTip;
toolTip += QLatin1String("<qt>");
toolTip += i18n("<b>%1</b>", i18n(tz.name().toUtf8()));
toolTip += i18n("<b>%1</b>", i18n(mTimezone.id()));
toolTip += QLatin1String("<hr>");
//TODO: Once string freeze is lifted, add UTC offset here
if (!tz.countryCode().isEmpty()) {
toolTip += i18n("<i>Country Code:</i> %1", tz.countryCode());
if (mTimezone.country() != QLocale::AnyCountry) {
toolTip += i18n("<i>Country:</i> %1", QLocale::countryToString(mTimezone.country()));
toolTip += QLatin1String("<br/>");
}
if (!tz.abbreviations().isEmpty()) {
toolTip += i18n("<i>Abbreviations:</i>") + QLatin1String("</i>");
toolTip += QLatin1String("&nbsp;");
foreach (const QByteArray &a, tz.abbreviations()) {
toolTip += QString::fromLocal8Bit(a);
toolTip += QLatin1String(",&nbsp;");
auto abbreviations = QStringLiteral("&nbsp;");
foreach (const auto &transition, mTimezone.transitions(QDateTime::currentDateTime(), QDateTime::currentDateTime().addYears(1))) {
abbreviations += transition.abbreviation;
abbreviations += QLatin1String(",&nbsp;");
}
toolTip.chop(7);
abbreviations.chop(7);
if (!abbreviations.isEmpty()) {
toolTip += i18n("<i>Abbreviations:</i>");
toolTip += abbreviations;
toolTip += QLatin1String("<br/>");
}
if (!tz.comment().isEmpty()) {
toolTip += i18n("<i>Comment:</i> %1", tz.comment());
if (!mTimezone.comment().isEmpty()) {
toolTip += i18n("<i>Comment:</i> %1", mTimezone.comment());
}
toolTip += QLatin1String("</qt>");
......
......@@ -24,7 +24,7 @@
#ifndef EVENTVIEWS_TIMELABELS_H
#define EVENTVIEWS_TIMELABELS_H
#include <KDateTime>
#include <QTimeZone>
#include <QFrame>
......@@ -43,8 +43,7 @@ class TimeLabels : public QFrame
public:
typedef QList<TimeLabels *> List;
TimeLabels(const KDateTime::Spec &spec, int rows,
TimeLabelsZone *parent = Q_NULLPTR, Qt::WindowFlags f = Q_NULLPTR);
TimeLabels(const QTimeZone &zone, int rows, TimeLabelsZone *parent = Q_NULLPTR, Qt::WindowFlags f = Q_NULLPTR);
/** updates widget's internal state */
void updateConfig();
......@@ -58,8 +57,8 @@ public:
/** */
void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
/** Returns time spec of this label */
KDateTime::Spec timeSpec();
/** Returns the time zone of this label */
QTimeZone timeZone() const;
/**
Return string which can be used as a header for the time label.
......@@ -86,7 +85,7 @@ private Q_SLOTS:
private:
void colorMousePos();
KDateTime::Spec mSpec;
QTimeZone mTimezone;
int mRows;
double mCellHeight;
int mMiniWidth;
......
......@@ -25,8 +25,6 @@
#include "prefs.h"
#include "timelabels.h"
#include <KSystemTimeZone>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
......@@ -64,13 +62,13 @@ void TimeLabelsZone::reset()
void TimeLabelsZone::init()
{
QStringList seenTimeZones(mPrefs->timeSpec().timeZone().name());
QStringList seenTimeZones(QString::fromUtf8(mPrefs->timeZone().id()));
addTimeLabels(mPrefs->timeSpec());
addTimeLabels(mPrefs->timeZone());
foreach (const QString &zoneStr, mPrefs->timeScaleTimezones()) {
if (!seenTimeZones.contains(zoneStr)) {
KTimeZone zone = KSystemTimeZones::zone(zoneStr);
auto zone = QTimeZone(zoneStr.toUtf8());
if (zone.isValid()) {
addTimeLabels(zone);
seenTimeZones += zoneStr;
......@@ -79,10 +77,10 @@ void TimeLabelsZone::init()
}
}
void TimeLabelsZone::addTimeLabels(const KDateTime::Spec &spec)
void TimeLabelsZone::addTimeLabels(const QTimeZone &zone)
{
QScrollArea *area = new QScrollArea(this);
TimeLabels *labels = new TimeLabels(spec, 24, this);
TimeLabels *labels = new TimeLabels(zone, 24, this);
mTimeLabelsList.prepend(area);
area->setWidgetResizable(true);
area->setWidget(labels);
......
......@@ -28,6 +28,7 @@
class QHBoxLayout;
class QScrollArea;
class QTimeZone;
namespace EventViews
{
......@@ -44,10 +45,10 @@ class TimeLabelsZone : public QWidget
public:
explicit TimeLabelsZone(QWidget *parent, const PrefsPtr &preferences, Agenda *agenda = Q_NULLPTR);
/** Add a new time label with the given spec.
If spec is not valid, use the display timespec.
/** Add a new time label with the given time zone.
If @p zone is not valid, use the display time zone.
*/
void addTimeLabels(const KDateTime::Spec &spec);
void addTimeLabels(const QTimeZone &zone);
/**
Returns the best width for each TimeLabels widget
......
......@@ -22,11 +22,11 @@
#include "timescaleconfigdialog.h"
#include "prefs.h"
#include <KSystemTimeZone>
#include <KLocalizedString>
#include <QIcon>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QTimeZone>
#include <QVBoxLayout>
using namespace EventViews;
......@@ -48,13 +48,14 @@ enum {
TimeZoneNameRole = Qt::UserRole
};
typedef QPair<QString, QString> TimeZoneNamePair;
typedef QPair<QString, QByteArray> TimeZoneNamePair;
//TODO: move to KCalCore::Stringify
static QString tzUTCOffsetStr(const KTimeZone &tz)
static QString tzUTCOffsetStr(const QTimeZone &tz)
{
int utcOffsetHrs = tz.currentOffset() / 3600; // in hours
int utcOffsetMins = (tz.currentOffset() % 3600) / 60; // in minutes
auto currentOffset = tz.offsetFromUtc(QDateTime::currentDateTimeUtc());
int utcOffsetHrs = currentOffset / 3600; // in hours
int utcOffsetMins = (currentOffset % 3600) / 60; // in minutes
QString utcStr;
if (utcOffsetMins > 0) {
utcStr = utcOffsetHrs >= 0 ?
......@@ -70,11 +71,12 @@ static QString tzUTCOffsetStr(const KTimeZone &tz)
}
//TODO: move to KCalCore::Stringify
static QString tzWithUTC(KTimeZones::ZoneMap::ConstIterator it)
static QString tzWithUTC(const QByteArray &zoneId)
{
auto tz = QTimeZone(zoneId);
return
QStringLiteral("%1 (UTC%2)").
arg(i18n(it.key().toUtf8()), tzUTCOffsetStr(it.value()));
arg(i18n(zoneId), tzUTCOffsetStr(tz));
}
TimeScaleConfigDialog::TimeScaleConfigDialog(const PrefsPtr &preferences, QWidget *parent)
......@@ -97,18 +99,18 @@ TimeScaleConfigDialog::TimeScaleConfigDialog(const PrefsPtr &preferences, QWidge
mainLayout->addWidget(mainwidget);
mainLayout->addWidget(buttonBox);
QStringList shownTimeZones(d->mPreferences->timeSpec().timeZone().name());
QStringList shownTimeZones(QString::fromUtf8(d->mPreferences->timeZone().id()));
shownTimeZones += d->mPreferences->timeScaleTimezones();
shownTimeZones.removeDuplicates();
QList<TimeZoneNamePair> availList, selList;
const KTimeZones::ZoneMap timezones = KSystemTimeZones::zones();
for (KTimeZones::ZoneMap::ConstIterator it = timezones.begin(); it != timezones.end(); ++it) {
const auto zoneIds = QTimeZone::availableTimeZoneIds();
foreach (const auto &zoneId, zoneIds) {
// do not list timezones already shown
if (!shownTimeZones.contains(it.key())) {
availList.append(TimeZoneNamePair(tzWithUTC(it), it.key()));
if (!shownTimeZones.contains(QString::fromUtf8(zoneId))) {
availList.append(TimeZoneNamePair(tzWithUTC(zoneId), zoneId));
} else {
selList.append(TimeZoneNamePair(tzWithUTC(it), it.key()));
selList.append(TimeZoneNamePair(tzWithUTC(zoneId), zoneId));
}
}
qSort(availList.begin(), availList.end());
......
......@@ -858,6 +858,11 @@ KDateTime::Spec Prefs::timeSpec() const
return KSystemTimeZones::local();
}
QTimeZone Prefs::timeZone() const
{
return QTimeZone(timeSpec().timeZone().name().toUtf8());
}
void Prefs::setTimeSpec(const KDateTime::Spec &spec)
{
d->mBaseConfig.mTimeSpec = spec;
......
......@@ -30,6 +30,7 @@
#include <KConfigSkeleton>
#include <KDateTime>
#include <QTimeZone>
namespace EventViews
{
......@@ -155,6 +156,7 @@ public:
void setTimeSpec(const KDateTime::Spec &spec);
KDateTime::Spec timeSpec() const;
QTimeZone timeZone() const;
QStringList timeScaleTimezones() const;
void setTimeScaleTimezones(const QStringList &list);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment