timescaleconfigdialog.cpp 7.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
  Copyright (c) 2007 Bruno Virlet <bruno@virlet.org>

  This program 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 2 of the License, or
  (at your option) any later version.

  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

  As a special exception, permission is given to link this program
  with any edition of Qt, and distribute the resulting executable,
  without including the source code for Qt in the source distribution.
*/
#include "timescaleconfigdialog.h"
#include "prefs.h"

#include <KLocalizedString>
#include <QIcon>
#include <QDialogButtonBox>
#include <QPushButton>
Volker Krause's avatar
Volker Krause committed
29
#include <QTimeZone>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <QVBoxLayout>

using namespace EventViews;

class Q_DECL_HIDDEN TimeScaleConfigDialog::Private
{
public:
    Private(TimeScaleConfigDialog *parent, const PrefsPtr &preferences)
        : q(parent), mPreferences(preferences)
    {
    }

public:
    TimeScaleConfigDialog *const q;
    PrefsPtr mPreferences;
};

enum {
    TimeZoneNameRole = Qt::UserRole
};

Volker Krause's avatar
Volker Krause committed
51
typedef QPair<QString, QByteArray> TimeZoneNamePair;
52 53

//TODO: move to KCalCore::Stringify
Volker Krause's avatar
Volker Krause committed
54
static QString tzUTCOffsetStr(const QTimeZone &tz)
55
{
Volker Krause's avatar
Volker Krause committed
56 57 58
    auto currentOffset = tz.offsetFromUtc(QDateTime::currentDateTimeUtc());
    int utcOffsetHrs = currentOffset / 3600;  // in hours
    int utcOffsetMins = (currentOffset % 3600) / 60;    // in minutes
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    QString utcStr;
    if (utcOffsetMins > 0) {
        utcStr = utcOffsetHrs >= 0 ?
                 QStringLiteral("+%1:%2").arg(utcOffsetHrs).arg(utcOffsetMins) :
                 QStringLiteral("%1:%2").arg(utcOffsetHrs).arg(utcOffsetMins);

    } else {
        utcStr = utcOffsetHrs >= 0 ?
                 QStringLiteral("+%1").arg(utcOffsetHrs) :
                 QStringLiteral("%1").arg(utcOffsetHrs);
    }
    return utcStr;
}

//TODO: move to KCalCore::Stringify
Volker Krause's avatar
Volker Krause committed
74
static QString tzWithUTC(const QByteArray &zoneId)
75
{
Volker Krause's avatar
Volker Krause committed
76
    auto tz = QTimeZone(zoneId);
77 78
    return
        QStringLiteral("%1 (UTC%2)").
Volker Krause's avatar
Volker Krause committed
79
        arg(i18n(zoneId), tzUTCOffsetStr(tz));
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

TimeScaleConfigDialog::TimeScaleConfigDialog(const PrefsPtr &preferences, QWidget *parent)
    : QDialog(parent), d(new Private(this, preferences))
{
    setWindowTitle(i18n("Timezone"));
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &TimeScaleConfigDialog::reject);
    okButton->setDefault(true);
    setModal(true);

    QWidget *mainwidget = new QWidget(this);
    setupUi(mainwidget);

    mainLayout->addWidget(mainwidget);
    mainLayout->addWidget(buttonBox);

Volker Krause's avatar
Volker Krause committed
102
    QStringList shownTimeZones(QString::fromUtf8(d->mPreferences->timeZone().id()));
103 104 105 106
    shownTimeZones += d->mPreferences->timeScaleTimezones();
    shownTimeZones.removeDuplicates();

    QList<TimeZoneNamePair> availList, selList;
Volker Krause's avatar
Volker Krause committed
107 108
    const auto zoneIds = QTimeZone::availableTimeZoneIds();
    foreach (const auto &zoneId, zoneIds) {
109
        // do not list timezones already shown
Volker Krause's avatar
Volker Krause committed
110 111
        if (!shownTimeZones.contains(QString::fromUtf8(zoneId))) {
            availList.append(TimeZoneNamePair(tzWithUTC(zoneId), zoneId));
112
        } else {
Volker Krause's avatar
Volker Krause committed
113
            selList.append(TimeZoneNamePair(tzWithUTC(zoneId), zoneId));
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        }
    }
    qSort(availList.begin(), availList.end());

    Q_FOREACH (const TimeZoneNamePair &item, availList) {
        zoneCombo->addItem(item.first, item.second);
    }
    zoneCombo->setCurrentIndex(0);

    addButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
    removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
    upButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
    downButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));

    connect(addButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::add);
    connect(removeButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::remove);
    connect(upButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::up);
    connect(downButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::down);

    connect(okButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::okClicked);
    connect(buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &TimeScaleConfigDialog::reject);

    Q_FOREACH (const TimeZoneNamePair &item, selList) {
        QListWidgetItem *widgetItem = new QListWidgetItem(item.first);
        widgetItem->setData(TimeZoneNameRole, item.second);
        listWidget->addItem(widgetItem);
    }
    slotUpdateButton();
}

TimeScaleConfigDialog::~TimeScaleConfigDialog()
{
    delete d;
}

void TimeScaleConfigDialog::slotUpdateButton()
{
    removeButton->setEnabled(listWidget->count() > 0);
}

void TimeScaleConfigDialog::okClicked()
{
    d->mPreferences->setTimeScaleTimezones(zones());
    d->mPreferences->writeConfig();
    accept();
}

void TimeScaleConfigDialog::add()
{
    // Do not add duplicates
    if (zoneCombo->currentIndex() >= 0) {
        for (int i = 0; i < listWidget->count(); ++i) {
            if (listWidget->item(i)->data(TimeZoneNameRole).toString() == zoneCombo->itemData(zoneCombo->currentIndex(), TimeZoneNameRole).toString()) {
                return;
            }
        }

        QListWidgetItem *item = new QListWidgetItem(zoneCombo->currentText());
        item->setData(TimeZoneNameRole, zoneCombo->itemData(zoneCombo->currentIndex(), TimeZoneNameRole).toString());
        listWidget->addItem(item);
        zoneCombo->removeItem(zoneCombo->currentIndex());
    }
    slotUpdateButton();
}

void TimeScaleConfigDialog::remove()
{
    zoneCombo->insertItem(0, listWidget->currentItem()->text(), zoneCombo->itemData(zoneCombo->currentIndex(), TimeZoneNameRole).toString());
    delete listWidget->takeItem(listWidget->currentRow());
    slotUpdateButton();
}

void TimeScaleConfigDialog::up()
{
    int row = listWidget->currentRow();
    QListWidgetItem *item = listWidget->takeItem(row);
    listWidget->insertItem(qMax(row - 1, 0), item);
    listWidget->setCurrentRow(qMax(row - 1, 0));
}

void TimeScaleConfigDialog::down()
{
    int row = listWidget->currentRow();
    QListWidgetItem *item = listWidget->takeItem(row);
    listWidget->insertItem(qMin(row + 1, listWidget->count()), item);
    listWidget->setCurrentRow(qMin(row + 1, listWidget->count() - 1));
}

QStringList TimeScaleConfigDialog::zones() const
{
    QStringList list;
    const int count = listWidget->count();
    list.reserve(count);
    for (int i = 0; i < count; ++i) {
        list << listWidget->item(i)->data(TimeZoneNameRole).toString();
    }
    return list;
}