⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qlocale.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        }    }#endif    return QString();}static QString macDateToString(const QDate &date, bool short_format){    CFGregorianDate macGDate;    macGDate.year = date.year();    macGDate.month = date.month();    macGDate.day = date.day();    macGDate.hour = 0;    macGDate.minute = 0;    macGDate.second = 0.0;    QCFType<CFDateRef> myDate        = CFDateCreate(0, CFGregorianDateGetAbsoluteTime(macGDate,                                                         QCFType<CFTimeZoneRef>(CFTimeZoneCopyDefault())));    QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();    CFDateFormatterStyle style = short_format ? kCFDateFormatterShortStyle : kCFDateFormatterLongStyle;    QCFType<CFDateFormatterRef> myFormatter        = CFDateFormatterCreate(kCFAllocatorDefault,                                mylocale, style,                                kCFDateFormatterNoStyle);    return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));}static QString macTimeToString(const QTime &time, bool short_format){    CFGregorianDate macGDate;    // Assume this is local time and the current date    QDate dt = QDate::currentDate();    macGDate.year = dt.year();    macGDate.month = dt.month();    macGDate.day = dt.day();    macGDate.hour = time.hour();    macGDate.minute = time.minute();    macGDate.second = time.second();    QCFType<CFDateRef> myDate        = CFDateCreate(0, CFGregorianDateGetAbsoluteTime(macGDate,                                                         QCFType<CFTimeZoneRef>(CFTimeZoneCopyDefault())));    QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();    CFDateFormatterStyle style = short_format ? kCFDateFormatterShortStyle :  kCFDateFormatterLongStyle;    QCFType<CFDateFormatterRef> myFormatter = CFDateFormatterCreate(kCFAllocatorDefault,                                                                    mylocale,                                                                    kCFDateFormatterNoStyle,                                                                    style);    return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));}static QString macToQtFormat(const QString &sys_fmt){    QString result;    int i = 0;    while (i < sys_fmt.size()) {        if (sys_fmt.at(i).unicode() == '\'') {            QString text = readEscapedFormatString(sys_fmt, &i);            if (text == QLatin1String("'"))                result += QLatin1String("''");            else                result += QLatin1Char('\'') + text + QLatin1Char('\'');            continue;        }        QChar c = sys_fmt.at(i);        int repeat = repeatCount(sys_fmt, i);        switch (c.unicode()) {            case 'G': // Qt doesn't support these :(            case 'Y':            case 'D':            case 'F':            case 'w':            case 'W':            case 'g':                break;            case 'u': // extended year - use 'y'                if (repeat < 4)                    result += QLatin1String("yy");                else                    result += QLatin1String("yyyy");                break;            case 'S': // fractional second                if (repeat < 3)                    result += QLatin1String("z");                else                    result += QLatin1String("zzz");                break;            case 'E':                if (repeat <= 3)                    result += QLatin1String("ddd");                else                    result += QLatin1String("dddd");                break;            case 'e':                if (repeat >= 2)                    result += QLatin1String("dd");                else                    result += QLatin1String("d");                break;            case 'a':                result += QLatin1String("AP");                break;            case 'k':                result += QString(repeat, QLatin1Char('H'));                break;            case 'K':                result += QString(repeat, QLatin1Char('h'));                break;            case 'z':            case 'Z':            case 'v':                result += QLatin1Char('t');                break;            default:                result += QString(repeat, c);                break;        }        i += repeat;    }    return result;}QString getMacDateFormat(CFDateFormatterStyle style){    QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();    QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault,                                                                  l, style, kCFDateFormatterNoStyle);    return macToQtFormat(QCFString::toQString(CFDateFormatterGetFormat(formatter)));}static QString getMacTimeFormat(CFDateFormatterStyle style){    QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();    QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault,                                                                  l, kCFDateFormatterNoStyle, style);    return macToQtFormat(QCFString::toQString(CFDateFormatterGetFormat(formatter)));}static QString getCFLocaleValue(CFStringRef key){    QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();    CFTypeRef value = CFLocaleGetValue(locale, key);    return QCFString::toQString(CFStringRef(static_cast<CFTypeRef>(value)));}QLocale QSystemLocale::fallbackLocale() const{    return QLocale(QString::fromUtf8(getMacLocaleName().constData()));}QVariant QSystemLocale::query(QueryType type, QVariant in = QVariant()) const{    switch(type) {//     case Name://         return getMacLocaleName();    case DecimalPoint: {        QString value = getCFLocaleValue(kCFLocaleDecimalSeparator);        return value.isEmpty() ? QVariant() : value;    }    case GroupSeparator: {        QString value = getCFLocaleValue(kCFLocaleGroupingSeparator);        return value.isEmpty() ? QVariant() : value;    }    case DateFormatLong:    case DateFormatShort:        return macToQtFormat(getMacDateFormat(type == DateFormatShort                                ? kCFDateFormatterShortStyle                                : kCFDateFormatterLongStyle));    case TimeFormatLong:    case TimeFormatShort:        return macToQtFormat(getMacTimeFormat(type == TimeFormatShort                                ? kCFDateFormatterShortStyle                                : kCFDateFormatterLongStyle));    case DayNameLong:    case DayNameShort:        return macDayName(in.toInt(), (type == DayNameShort));    case MonthNameLong:    case MonthNameShort:        return macMonthName(in.toInt(), (type == MonthNameShort));    case DateToStringShort:    case DateToStringLong:        return macDateToString(in.toDate(), (type == DateToStringShort));    case TimeToStringShort:    case TimeToStringLong:        return macTimeToString(in.toTime(), (type == TimeToStringShort));    case NegativeSign:    case ZeroDigit:    case LanguageId:    case CountryId:    default:        break;    }    return QVariant();}#else/*!    Returns a fallback locale, that will get used for everything that    is not explicitly overridden by the system locale.*/QLocale QSystemLocale::fallbackLocale() const{    return QLocale(QLatin1String(envVarLocale()));}/*!    Performs a query of the given \a type in the system locale for    customized values or conversion. If the method returns a null    QVariant, the conversion of the fallbackLocale() will be used.    \a in is unused for some of the query types.    \sa QSystemLocale::QueryType*/QVariant QSystemLocale::query(QueryType /* type */, QVariant /* in */) const{    return QVariant();}#endifstatic QSystemLocale *_systemLocale = 0;static QLocalePrivate *system_lp = 0;/******************************************************************************** Default system locale behavior*//*!    \class QSystemLocale    \brief The QSystemLocale class can be used to finetune the system locale    of the user.    \since 4.2    \ingroup i18n    \warning This class is only useful in very rare cases. Usually QLocale offers    all the functionality required for application development.    QSystemLocale allows to override the values provided by the system    locale (QLocale::system()).    \sa QLocale*//*!  \enum QSystemLocale::QueryType  Specifies the type of information queried by query(). For each value  the type of information to return from the query() method is listed.  \value LanguageId a uint specifying the language.  \value CountryId a uint specifying the country.  \value DecimalPoint a QString specifying the decimal point.  \value GroupSeparator a QString specifying the group separator.  \value ZeroDigit a QString specifying the zero digit.  \value NegativeSign a QString specifying the minus sign.  \value DateFormatLong a QString specifying the long date format  \value DateFormatShort a QString specifying the short date format  \value TimeFormatLong a QString specifying the long time format  \value TimeFormatShort a QString specifying the short time format  \value DayNameLong a QString specifying the name of a weekday. the in variant contains an integer between 1 and 7 (Monday - Friday)  \value DayNameShort a QString specifying the short name of a weekday. the in variant contains an integer between 1 and 7 (Monday - Friday)  \value MonthNameLong a QString specifying the name of a month. the in variant contains an integer between 1 and 12  \value MonthNameShort a QString specifying the short name of a month. the in variant contains an integer between 1 and 12  \value DateToStringLong converts the QDate stored in the in variant to a QString using the long date format  \value DateToStringShort converts the QDate stored in the in variant to a QString using the short date format  \value TimeToStringLong converts the QTime stored in the in variant to a QString using the long time format  \value TimeToStringShort converts the QTime stored in the in variant to a QString using the short time format*//*!  Constructs a QSystemLocale object. The constructor will automatically  install this object as the system locale and remove any earlier installed  system locales.*/QSystemLocale::QSystemLocale(){    delete ::_systemLocale;    ::_systemLocale = this;    if (system_lp)        system_lp->m_language_id = 0;}/*!  Deletes the object.*/QSystemLocale::~QSystemLocale(){    Q_ASSERT(::_systemLocale == this);    ::_systemLocale = 0;    if (system_lp)        system_lp->m_language_id = 0;}static const QSystemLocale *systemLocale(){    if (!_systemLocale)        _systemLocale = new QSystemLocale();    return _systemLocale;}void QLocalePrivate::updateSystemPrivate(){    const QSystemLocale *sys_locale = systemLocale();    if (!system_lp)        system_lp = new QLocalePrivate;    *system_lp = *sys_locale->fallbackLocale().d();    QVariant res = sys_locale->query(QSystemLocale::LanguageId, QVariant());    if (!res.isNull())        system_lp->m_language_id = res.toInt();    res = sys_locale->query(QSystemLocale::CountryId, QVariant());    if (!res.isNull())        system_lp->m_country_id = res.toInt();    res = sys_locale->query(QSystemLocale::DecimalPoint, QVariant());    if (!res.isNull())        system_lp->m_decimal = res.toString().at(0).unicode();    res = sys_locale->query(QSystemLocale::GroupSeparator, QVariant());    if (!res.isNull())        system_lp->m_group = res.toString().at(0).unicode();    res = sys_locale->query(QSystemLocale::ZeroDigit, QVariant());    if (!res.isNull())        system_lp->m_zero = res.toString().at(0).unicode();    res = sys_locale->query(QSystemLocale::NegativeSign, QVariant());    if (!res.isNull())        system_lp->m_minus = res.toString().at(0).unicode();}#endifstatic const QLocalePrivate *systemPrivate(){#ifndef QT_NO_SYSTEMLOCALE    // copy over the information from the fallback locale and modify    if (!system_lp || system_lp->m_language_id == 0)        QLocalePrivate::updateSystemPrivate();    return system_lp;#else    return locale_data;#endif}static const QLocalePrivate *defaultPrivate(){    if (!default_lp)        default_lp = systemPrivate();    return default_lp;}static QString getLocaleListData(const char *data, int index){    while (index) {        while (*data != ';')            ++data;        --index;        ++data;    }    const char *end = data;    while (*end != '\0' && *end != ';')        ++end;    return QString::fromUtf8(data, end - data);}#ifndef QT_NO_DATASTREAMQDataStream &operator<<(QDataStream &ds, const QLocale &l){    ds << l.name();    return ds;}QDataStream &operator>>(QDataStream &ds, QLocale &l){    QString s;    ds >> s;    l = QLocale(s);    return ds;}#endif/*!    \class QLocale    \brief The QLocale class converts between numbers and their    string representations in various languages.    \reentrant    \ingroup i18n    \ingroup text    \ingroup shared    \mainclass    QLocale is initialized with a language/country pair in its    constructor and offers number-to-string and string-to-number    conversion functions similar to those in QString.    Example:    \code        QLocale egyptian(QLocale::Arabic, QLocale::Egypt);        QString s1 = egyptian.toString(1.571429E+07, 'e');        QString s2 = egyptian.toString(10);        double d = egyptian.toDouble(s1);        int i = egyptian.toInt(s2);    \endcode    QLocale supports the concept of a default locale, which is    determined from the system's locale settings at application    startup. The default locale can be changed by calling the    static member setDefault(). Setting the default locale has the    following effects:    \list    \i If a QLocale object is constructed with the default constructor,       it will use the default locale's settings.    \i QString::toInt(), QString::toDouble(), etc., interpret the       string according to the default locale. If this fails, it       falls back on the "C" locale.    \i QString::arg() uses the default locale to format a number when       its position specifier in the format string contains an 'L',       e.g. "%L1".    \endlist    The following example illustrates how to use QLocale directly:    \code        QLocale::setDefault(QLocale(QLocale::Hebrew, QLocale::Israel));        QLocale hebrew; // Constructs a default QLocale        QString s1 = hebrew.toString(15714.3, 'e');        bool ok;        double d;        QLocale::setDefault(QLocale::C);        d = QString("1234,56").toDouble(&ok);   // ok == false        d = QString("1234.56").toDouble(&ok);   // ok == true, d == 1234.56        QLocale::setDefault(QLocale::German);        d = QString("1234,56").toDouble(&ok);   // ok == true, d == 1234.56        d = QString("1234.56").toDouble(&ok);   // ok == true, d == 1234.56        QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));        str = QString("%1 %L2 %L3")              .arg(12345).arg(12345).arg(12345, 0, 16);        // str == "12345 12,345 3039"    \endcode    When a language/country pair is specified in the constructor, one    of three things can happen:    \list    \i If the language/country pair is found in the database, it is used.    \i If the language is found but the country is not, or if the country       is \c AnyCountry, the language is used with the most       appropriate available country (for example, Germany for German),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -