| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 2007 Licq developers |
|---|
| 5 | * |
|---|
| 6 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * Licq is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with Licq; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "timezoneedit.h" |
|---|
| 22 | |
|---|
| 23 | #include <QRegExp> |
|---|
| 24 | |
|---|
| 25 | #include <licq_user.h> |
|---|
| 26 | |
|---|
| 27 | using namespace LicqQtGui; |
|---|
| 28 | /* TRANSLATOR LicqQtGui::TimeZoneEdit */ |
|---|
| 29 | |
|---|
| 30 | TimeZoneEdit::TimeZoneEdit(QWidget* parent) |
|---|
| 31 | : QSpinBox(parent) |
|---|
| 32 | { |
|---|
| 33 | setMinimum(-24); |
|---|
| 34 | setMaximum(24); |
|---|
| 35 | |
|---|
| 36 | // The world is round so let timezones wrap |
|---|
| 37 | setWrapping(true); |
|---|
| 38 | |
|---|
| 39 | // Plus and minus seems more fitting than up and down |
|---|
| 40 | setButtonSymbols(QSpinBox::PlusMinus); |
|---|
| 41 | |
|---|
| 42 | // Allow the value to be undefined as well. This will replace the lowest value (-24) |
|---|
| 43 | setSpecialValueText(tr("Unknown")); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void TimeZoneEdit::setData(char data) |
|---|
| 47 | { |
|---|
| 48 | // The spinbox uses the lowest value to mark the undefined state but the constant is some other value so we need to change it |
|---|
| 49 | // For all defined values, the sign is inverted |
|---|
| 50 | setValue(data == TIMEZONE_UNKNOWN ? undefinedValue : static_cast<int>(-data)); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | char TimeZoneEdit::data() const |
|---|
| 54 | { |
|---|
| 55 | int v = value(); |
|---|
| 56 | if (v == undefinedValue) |
|---|
| 57 | return TIMEZONE_UNKNOWN; |
|---|
| 58 | return static_cast<char>(-v); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | QValidator::State TimeZoneEdit::validate(QString& input, int& /* pos */) const |
|---|
| 62 | { |
|---|
| 63 | // First check for the undefined value |
|---|
| 64 | if (input == specialValueText()) |
|---|
| 65 | return QValidator::Acceptable; |
|---|
| 66 | if (specialValueText().startsWith(input)) |
|---|
| 67 | return QValidator::Intermediate; |
|---|
| 68 | |
|---|
| 69 | // Check if this is a complete valid timezone |
|---|
| 70 | QRegExp rxValid("^GMT[\\+\\-](1[012]|\\d)[03]0$"); |
|---|
| 71 | if (rxValid.indexIn(input) > -1) |
|---|
| 72 | return QValidator::Acceptable; |
|---|
| 73 | |
|---|
| 74 | // Check if this is anything close to a timezone |
|---|
| 75 | QRegExp rxPossible("^G?M?T?[\\+\\-]?\\d*$"); |
|---|
| 76 | if (rxPossible.indexIn(input) > -1) |
|---|
| 77 | return QValidator::Intermediate; |
|---|
| 78 | |
|---|
| 79 | return QValidator::Invalid; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | QString TimeZoneEdit::textFromValue(int v) const |
|---|
| 83 | { |
|---|
| 84 | // The internal value in the spinbox is 30min intervals so convert it to something more readable |
|---|
| 85 | return QString("GMT%1%2%3").arg(v < 0 ? "-" : "+").arg(abs(v) / 2).arg(v % 2 ? "30" : "00"); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | int TimeZoneEdit::valueFromText(const QString& text) const |
|---|
| 89 | { |
|---|
| 90 | // The user entered something so now we must try and convert it back to the internal int |
|---|
| 91 | QRegExp rx("^GMT(\\+|-)(\\d+)(0|3)0$"); |
|---|
| 92 | if (rx.indexIn(text) == -1) |
|---|
| 93 | return undefinedValue; |
|---|
| 94 | |
|---|
| 95 | int ret = rx.cap(2).toInt() * 2; |
|---|
| 96 | if (rx.cap(3) == "3") |
|---|
| 97 | ret++; |
|---|
| 98 | if (rx.cap(1) == "-") |
|---|
| 99 | ret = -ret; |
|---|
| 100 | return ret; |
|---|
| 101 | } |
|---|