5.15.9 original files

This commit is contained in:
kleuter 2022-04-26 11:26:05 +02:00
parent f2ec2ddc38
commit baea518e88
4 changed files with 8030 additions and 0 deletions

View File

@ -0,0 +1,569 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#include "qoperatingsystemversion.h"
#if !defined(Q_OS_DARWIN) && !defined(Q_OS_WIN)
#include "qoperatingsystemversion_p.h"
#endif
#if defined(Q_OS_DARWIN)
#include <QtCore/private/qcore_mac_p.h>
#endif
#include <qversionnumber.h>
#include <qdebug.h>
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
#include <private/qjni_p.h>
#endif
QT_BEGIN_NAMESPACE
/*!
\class QOperatingSystemVersion
\inmodule QtCore
\since 5.9
\brief The QOperatingSystemVersion class provides information about the
operating system version.
Unlike other version functions in QSysInfo, QOperatingSystemVersion provides
access to the full version number that \a developers typically use to vary
behavior or determine whether to enable APIs or features based on the
operating system version (as opposed to the kernel version number or
marketing version).
This class is also a complete replacement for QSysInfo::macVersion and
QSysInfo::windowsVersion, additionally providing access to the third (micro)
version number component.
Presently, Android, Apple Platforms (iOS, macOS, tvOS, and watchOS),
and Windows are supported.
The \a majorVersion(), \a minorVersion(), and \a microVersion() functions
return the parts of the operating system version number based on:
\table
\header
\li Platforms
\li Value
\row
\li Android
\li result of parsing
\l{https://developer.android.com/reference/android/os/Build.VERSION.html#RELEASE}{android.os.Build.VERSION.RELEASE}
using QVersionNumber, with a fallback to
\l{https://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT}{android.os.Build.VERSION.SDK_INT}
to determine the major and minor version component if the former
fails
\row
\li Apple Platforms
\li majorVersion, minorVersion, and patchVersion from
\l{https://developer.apple.com/reference/foundation/nsprocessinfo/1410906-operatingsystemversion?language=objc}{NSProcessInfo.operatingSystemVersion}
\row
\li Windows
\li dwMajorVersion, dwMinorVersion, and dwBuildNumber from
\l{https://msdn.microsoft.com/en-us/library/mt723418.aspx}{RtlGetVersion} -
note that this function ALWAYS return the version number of the
underlying operating system, as opposed to the shim underneath
GetVersionEx that hides the real version number if the
application is not manifested for that version of the OS
\endtable
Because QOperatingSystemVersion stores both a version number and an OS type, the OS type
can be taken into account when performing comparisons. For example, on a macOS system running
macOS Sierra (v10.12), the following expression will return \c false even though the
major version number component of the object on the left hand side of the expression (10) is
greater than that of the object on the right (9):
\snippet code/src_corelib_global_qoperatingsystemversion.cpp 0
This allows expressions for multiple operating systems to be joined with a logical OR operator
and still work as expected. For example:
\snippet code/src_corelib_global_qoperatingsystemversion.cpp 1
A more naive comparison algorithm might incorrectly return true on all versions of macOS,
including Mac OS 9. This behavior is achieved by overloading the comparison operators to return
\c false whenever the OS types of the QOperatingSystemVersion instances being compared do not
match. Be aware that due to this it can be the case \c x >= y and \c x < y are BOTH \c false
for the same instances of \c x and \c y.
*/
/*!
\enum QOperatingSystemVersion::OSType
This enum provides symbolic names for the various operating
system families supported by QOperatingSystemVersion.
\value Android The Google Android operating system.
\value IOS The Apple iOS operating system.
\value MacOS The Apple macOS operating system.
\value TvOS The Apple tvOS operating system.
\value WatchOS The Apple watchOS operating system.
\value Windows The Microsoft Windows operating system.
\value Unknown An unknown or unsupported operating system.
*/
/*!
\fn QOperatingSystemVersion::QOperatingSystemVersion(OSType osType, int vmajor, int vminor = -1, int vmicro = -1)
Constructs a QOperatingSystemVersion consisting of the OS type \a osType, and
major, minor, and micro version numbers \a vmajor, \a vminor and \a vmicro, respectively.
*/
/*!
\fn QOperatingSystemVersion QOperatingSystemVersion::current()
Returns a QOperatingSystemVersion indicating the current OS and its version number.
\sa currentType()
*/
#if !defined(Q_OS_DARWIN) && !defined(Q_OS_WIN)
QOperatingSystemVersion QOperatingSystemVersion::current()
{
QOperatingSystemVersion version;
version.m_os = currentType();
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
#ifndef QT_BOOTSTRAPPED
const QVersionNumber v = QVersionNumber::fromString(QJNIObjectPrivate::getStaticObjectField(
"android/os/Build$VERSION", "RELEASE", "Ljava/lang/String;").toString());
if (!v.isNull()) {
version.m_major = v.majorVersion();
version.m_minor = v.minorVersion();
version.m_micro = v.microVersion();
return version;
}
#endif
version.m_major = -1;
version.m_minor = -1;
static const struct {
uint major : 4;
uint minor : 4;
} versions[] = {
{ 1, 0 }, // API level 1
{ 1, 1 }, // API level 2
{ 1, 5 }, // API level 3
{ 1, 6 }, // API level 4
{ 2, 0 }, // API level 5
{ 2, 0 }, // API level 6
{ 2, 1 }, // API level 7
{ 2, 2 }, // API level 8
{ 2, 3 }, // API level 9
{ 2, 3 }, // API level 10
{ 3, 0 }, // API level 11
{ 3, 1 }, // API level 12
{ 3, 2 }, // API level 13
{ 4, 0 }, // API level 14
{ 4, 0 }, // API level 15
{ 4, 1 }, // API level 16
{ 4, 2 }, // API level 17
{ 4, 3 }, // API level 18
{ 4, 4 }, // API level 19
{ 4, 4 }, // API level 20
{ 5, 0 }, // API level 21
{ 5, 1 }, // API level 22
{ 6, 0 }, // API level 23
{ 7, 0 }, // API level 24
{ 7, 1 }, // API level 25
{ 8, 0 }, // API level 26
};
// This will give us at least the first 2 version components
const size_t versionIdx = size_t(QJNIObjectPrivate::getStaticField<jint>(
"android/os/Build$VERSION", "SDK_INT")) - 1;
if (versionIdx < sizeof(versions) / sizeof(versions[0])) {
version.m_major = versions[versionIdx].major;
version.m_minor = versions[versionIdx].minor;
}
// API level 6 was exactly version 2.0.1
version.m_micro = versionIdx == 5 ? 1 : -1;
#else
version.m_major = -1;
version.m_minor = -1;
version.m_micro = -1;
#endif
return version;
}
#endif
static inline int compareVersionComponents(int lhs, int rhs)
{
return lhs >= 0 && rhs >= 0 ? lhs - rhs : 0;
}
int QOperatingSystemVersion::compare(const QOperatingSystemVersion &v1,
const QOperatingSystemVersion &v2)
{
if (v1.m_major == v2.m_major) {
if (v1.m_minor == v2.m_minor) {
return compareVersionComponents(v1.m_micro, v2.m_micro);
}
return compareVersionComponents(v1.m_minor, v2.m_minor);
}
return compareVersionComponents(v1.m_major, v2.m_major);
}
/*!
\fn int QOperatingSystemVersion::majorVersion() const
Returns the major version number, that is, the first segment of the
operating system's version number.
See the main class documentation for what the major version number is on a given
operating system.
-1 indicates an unknown or absent version number component.
\sa minorVersion(), microVersion()
*/
/*!
\fn int QOperatingSystemVersion::minorVersion() const
Returns the minor version number, that is, the second segment of the
operating system's version number.
See the main class documentation for what the minor version number is on a given
operating system.
-1 indicates an unknown or absent version number component.
\sa majorVersion(), microVersion()
*/
/*!
\fn int QOperatingSystemVersion::microVersion() const
Returns the micro version number, that is, the third segment of the
operating system's version number.
See the main class documentation for what the micro version number is on a given
operating system.
-1 indicates an unknown or absent version number component.
\sa majorVersion(), minorVersion()
*/
/*!
\fn int QOperatingSystemVersion::segmentCount() const
Returns the number of integers stored in the version number.
*/
/*!
\fn QOperatingSystemVersion::OSType QOperatingSystemVersion::type() const
Returns the OS type identified by the QOperatingSystemVersion.
\sa name()
*/
/*!
\fn QOperatingSystemVersion::OSType QOperatingSystemVersion::currentType()
Returns the current OS type without constructing a QOperatingSystemVersion instance.
\since 5.10
\sa current()
*/
/*!
\fn QString QOperatingSystemVersion::name() const
Returns a string representation of the OS type identified by the QOperatingSystemVersion.
\sa type()
*/
QString QOperatingSystemVersion::name() const
{
switch (type()) {
case QOperatingSystemVersion::Windows:
return QStringLiteral("Windows");
case QOperatingSystemVersion::MacOS: {
if (majorVersion() < 10)
return QStringLiteral("Mac OS");
if (majorVersion() == 10 && minorVersion() < 8)
return QStringLiteral("Mac OS X");
if (majorVersion() == 10 && minorVersion() < 12)
return QStringLiteral("OS X");
return QStringLiteral("macOS");
}
case QOperatingSystemVersion::IOS: {
if (majorVersion() < 4)
return QStringLiteral("iPhone OS");
return QStringLiteral("iOS");
}
case QOperatingSystemVersion::TvOS:
return QStringLiteral("tvOS");
case QOperatingSystemVersion::WatchOS:
return QStringLiteral("watchOS");
case QOperatingSystemVersion::Android:
return QStringLiteral("Android");
case QOperatingSystemVersion::Unknown:
default:
return QString();
}
}
/*!
\fn bool QOperatingSystemVersion::isAnyOfType(std::initializer_list<OSType> types) const
Returns whether the OS type identified by the QOperatingSystemVersion
matches any of the OS types in \a types.
*/
bool QOperatingSystemVersion::isAnyOfType(std::initializer_list<OSType> types) const
{
for (const auto &t : qAsConst(types)) {
if (type() == t)
return true;
}
return false;
}
/*!
\variable QOperatingSystemVersion::Windows7
\brief a version corresponding to Windows 7 (version 6.1).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::Windows7 =
QOperatingSystemVersion(QOperatingSystemVersion::Windows, 6, 1);
/*!
\variable QOperatingSystemVersion::Windows8
\brief a version corresponding to Windows 8 (version 6.2).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::Windows8 =
QOperatingSystemVersion(QOperatingSystemVersion::Windows, 6, 2);
/*!
\variable QOperatingSystemVersion::Windows8_1
\brief a version corresponding to Windows 8.1 (version 6.3).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::Windows8_1 =
QOperatingSystemVersion(QOperatingSystemVersion::Windows, 6, 3);
/*!
\variable QOperatingSystemVersion::Windows10
\brief a version corresponding to Windows 10 (version 10.0).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::Windows10 =
QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10);
/*!
\variable QOperatingSystemVersion::OSXMavericks
\brief a version corresponding to OS X Mavericks (version 10.9).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::OSXMavericks =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 9);
/*!
\variable QOperatingSystemVersion::OSXYosemite
\brief a version corresponding to OS X Yosemite (version 10.10).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::OSXYosemite =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 10);
/*!
\variable QOperatingSystemVersion::OSXElCapitan
\brief a version corresponding to OS X El Capitan (version 10.11).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::OSXElCapitan =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 11);
/*!
\variable QOperatingSystemVersion::MacOSSierra
\brief a version corresponding to macOS Sierra (version 10.12).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::MacOSSierra =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 12);
/*!
\variable QOperatingSystemVersion::MacOSHighSierra
\brief a version corresponding to macOS High Sierra (version 10.13).
\since 5.9.1
*/
const QOperatingSystemVersion QOperatingSystemVersion::MacOSHighSierra =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 13);
/*!
\variable QOperatingSystemVersion::MacOSMojave
\brief a version corresponding to macOS Mojave (version 10.14).
\since 5.11.2
*/
const QOperatingSystemVersion QOperatingSystemVersion::MacOSMojave =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 14);
/*!
\variable QOperatingSystemVersion::MacOSCatalina
\brief a version corresponding to macOS Catalina (version 10.15).
\since 5.12.5
*/
const QOperatingSystemVersion QOperatingSystemVersion::MacOSCatalina =
QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 15);
/*!
\variable QOperatingSystemVersion::MacOSBigSur
\brief a version corresponding to macOS Big Sur
The actual version number depends on whether the application was built
using the Xcode 12 SDK. If it was, the version number corresponds
to macOS 11.0. If not it will correspond to macOS 10.16.
By comparing QOperatingSystemVersion::current() to this constant
you will always end up comparing to the right version number.
\since 6.0
*/
const QOperatingSystemVersion QOperatingSystemVersion::MacOSBigSur = [] {
#if defined(Q_OS_DARWIN)
if (QMacVersion::buildSDK(QMacVersion::ApplicationBinary) >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 16))
return QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 11, 0);
else
#endif
return QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 16);
}();
/*!
\variable QOperatingSystemVersion::AndroidJellyBean
\brief a version corresponding to Android Jelly Bean (version 4.1, API level 16).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidJellyBean =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 4, 1);
/*!
\variable QOperatingSystemVersion::AndroidJellyBean_MR1
\brief a version corresponding to Android Jelly Bean, maintenance release 1
(version 4.2, API level 17).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidJellyBean_MR1 =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 4, 2);
/*!
\variable QOperatingSystemVersion::AndroidJellyBean_MR2
\brief a version corresponding to Android Jelly Bean, maintenance release 2
(version 4.3, API level 18).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidJellyBean_MR2 =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 4, 3);
/*!
\variable QOperatingSystemVersion::AndroidKitKat
\brief a version corresponding to Android KitKat (versions 4.4 & 4.4W, API levels 19 & 20).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidKitKat =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 4, 4);
/*!
\variable QOperatingSystemVersion::AndroidLollipop
\brief a version corresponding to Android Lollipop (version 5.0, API level 21).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidLollipop =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 5, 0);
/*!
\variable QOperatingSystemVersion::AndroidLollipop_MR1
\brief a version corresponding to Android Lollipop, maintenance release 1
(version 5.1, API level 22).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidLollipop_MR1 =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 5, 1);
/*!
\variable QOperatingSystemVersion::AndroidMarshmallow
\brief a version corresponding to Android Marshmallow (version 6.0, API level 23).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidMarshmallow =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 6, 0);
/*!
\variable QOperatingSystemVersion::AndroidNougat
\brief a version corresponding to Android Nougat (version 7.0, API level 24).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidNougat =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 7, 0);
/*!
\variable QOperatingSystemVersion::AndroidNougat_MR1
\brief a version corresponding to Android Nougat, maintenance release 1
(version 7.0, API level 25).
\since 5.9
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidNougat_MR1 =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 7, 1);
/*!
\variable QOperatingSystemVersion::AndroidOreo
\brief a version corresponding to Android Oreo (version 8.0, API level 26).
\since 5.9.2
*/
const QOperatingSystemVersion QOperatingSystemVersion::AndroidOreo =
QOperatingSystemVersion(QOperatingSystemVersion::Android, 8, 0);
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const QOperatingSystemVersion &ov)
{
QDebugStateSaver saver(debug);
debug.nospace();
debug << "QOperatingSystemVersion(" << ov.name()
<< ", " << ov.majorVersion() << '.' << ov.minorVersion()
<< '.' << ov.microVersion() << ')';
return debug;
}
#endif // !QT_NO_DEBUG_STREAM
QT_END_NAMESPACE

View File

@ -0,0 +1,158 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#include <QtCore/qglobal.h>
#ifndef QOPERATINGSYSTEMVERSION_H
#define QOPERATINGSYSTEMVERSION_H
QT_BEGIN_NAMESPACE
class QString;
class QVersionNumber;
class Q_CORE_EXPORT QOperatingSystemVersion
{
public:
enum OSType {
Unknown = 0,
Windows,
MacOS,
IOS,
TvOS,
WatchOS,
Android
};
static const QOperatingSystemVersion Windows7;
static const QOperatingSystemVersion Windows8;
static const QOperatingSystemVersion Windows8_1;
static const QOperatingSystemVersion Windows10;
static const QOperatingSystemVersion OSXMavericks;
static const QOperatingSystemVersion OSXYosemite;
static const QOperatingSystemVersion OSXElCapitan;
static const QOperatingSystemVersion MacOSSierra;
static const QOperatingSystemVersion MacOSHighSierra;
static const QOperatingSystemVersion MacOSMojave;
static const QOperatingSystemVersion MacOSCatalina;
static const QOperatingSystemVersion MacOSBigSur;
static const QOperatingSystemVersion AndroidJellyBean;
static const QOperatingSystemVersion AndroidJellyBean_MR1;
static const QOperatingSystemVersion AndroidJellyBean_MR2;
static const QOperatingSystemVersion AndroidKitKat;
static const QOperatingSystemVersion AndroidLollipop;
static const QOperatingSystemVersion AndroidLollipop_MR1;
static const QOperatingSystemVersion AndroidMarshmallow;
static const QOperatingSystemVersion AndroidNougat;
static const QOperatingSystemVersion AndroidNougat_MR1;
static const QOperatingSystemVersion AndroidOreo;
Q_DECL_CONSTEXPR QOperatingSystemVersion(OSType osType,
int vmajor, int vminor = -1, int vmicro = -1)
: m_os(osType),
m_major(qMax(-1, vmajor)),
m_minor(vmajor < 0 ? -1 : qMax(-1, vminor)),
m_micro(vmajor < 0 || vminor < 0 ? -1 : qMax(-1, vmicro))
{ }
static QOperatingSystemVersion current();
static Q_DECL_CONSTEXPR OSType currentType()
{
#if defined(Q_OS_WIN)
return Windows;
#elif defined(Q_OS_MACOS)
return MacOS;
#elif defined(Q_OS_IOS)
return IOS;
#elif defined(Q_OS_TVOS)
return TvOS;
#elif defined(Q_OS_WATCHOS)
return WatchOS;
#elif defined(Q_OS_ANDROID)
return Android;
#else
return Unknown;
#endif
}
Q_DECL_CONSTEXPR int majorVersion() const { return m_major; }
Q_DECL_CONSTEXPR int minorVersion() const { return m_minor; }
Q_DECL_CONSTEXPR int microVersion() const { return m_micro; }
Q_DECL_CONSTEXPR int segmentCount() const
{ return m_micro >= 0 ? 3 : m_minor >= 0 ? 2 : m_major >= 0 ? 1 : 0; }
bool isAnyOfType(std::initializer_list<OSType> types) const;
Q_DECL_CONSTEXPR OSType type() const { return m_os; }
QString name() const;
friend bool operator>(const QOperatingSystemVersion &lhs, const QOperatingSystemVersion &rhs)
{ return lhs.type() == rhs.type() && QOperatingSystemVersion::compare(lhs, rhs) > 0; }
friend bool operator>=(const QOperatingSystemVersion &lhs, const QOperatingSystemVersion &rhs)
{ return lhs.type() == rhs.type() && QOperatingSystemVersion::compare(lhs, rhs) >= 0; }
friend bool operator<(const QOperatingSystemVersion &lhs, const QOperatingSystemVersion &rhs)
{ return lhs.type() == rhs.type() && QOperatingSystemVersion::compare(lhs, rhs) < 0; }
friend bool operator<=(const QOperatingSystemVersion &lhs, const QOperatingSystemVersion &rhs)
{ return lhs.type() == rhs.type() && QOperatingSystemVersion::compare(lhs, rhs) <= 0; }
private:
QOperatingSystemVersion() = default;
OSType m_os;
int m_major;
int m_minor;
int m_micro;
static int compare(const QOperatingSystemVersion &v1, const QOperatingSystemVersion &v2);
};
Q_DECLARE_TYPEINFO(QOperatingSystemVersion, QT_VERSION < QT_VERSION_CHECK(6, 0, 0) ? Q_RELOCATABLE_TYPE : Q_PRIMITIVE_TYPE);
#ifndef QT_NO_DEBUG_STREAM
class QDebug;
Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QOperatingSystemVersion &ov);
#endif
QT_END_NAMESPACE
#endif // QOPERATINGSYSTEMVERSION_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,552 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#include "qslider.h"
#ifndef QT_NO_ACCESSIBILITY
#include "qaccessible.h"
#endif
#include "qapplication.h"
#include "qevent.h"
#include "qpainter.h"
#include "qstyle.h"
#include "qstyleoption.h"
#include "private/qapplication_p.h"
#include "private/qabstractslider_p.h"
#include "qdebug.h"
QT_BEGIN_NAMESPACE
class QSliderPrivate : public QAbstractSliderPrivate
{
Q_DECLARE_PUBLIC(QSlider)
public:
QStyle::SubControl pressedControl;
int tickInterval;
QSlider::TickPosition tickPosition;
int clickOffset;
void init();
void resetLayoutItemMargins();
int pixelPosToRangeValue(int pos) const;
inline int pick(const QPoint &pt) const;
QStyle::SubControl newHoverControl(const QPoint &pos);
bool updateHoverControl(const QPoint &pos);
QStyle::SubControl hoverControl;
QRect hoverRect;
};
void QSliderPrivate::init()
{
Q_Q(QSlider);
pressedControl = QStyle::SC_None;
tickInterval = 0;
tickPosition = QSlider::NoTicks;
hoverControl = QStyle::SC_None;
q->setFocusPolicy(Qt::FocusPolicy(q->style()->styleHint(QStyle::SH_Button_FocusPolicy)));
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::Slider);
if (orientation == Qt::Vertical)
sp.transpose();
q->setSizePolicy(sp);
q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
resetLayoutItemMargins();
}
void QSliderPrivate::resetLayoutItemMargins()
{
Q_Q(QSlider);
QStyleOptionSlider opt;
q->initStyleOption(&opt);
setLayoutItemMargins(QStyle::SE_SliderLayoutItem, &opt);
}
int QSliderPrivate::pixelPosToRangeValue(int pos) const
{
Q_Q(const QSlider);
QStyleOptionSlider opt;
q->initStyleOption(&opt);
QRect gr = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, q);
QRect sr = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, q);
int sliderMin, sliderMax, sliderLength;
if (orientation == Qt::Horizontal) {
sliderLength = sr.width();
sliderMin = gr.x();
sliderMax = gr.right() - sliderLength + 1;
} else {
sliderLength = sr.height();
sliderMin = gr.y();
sliderMax = gr.bottom() - sliderLength + 1;
}
return QStyle::sliderValueFromPosition(minimum, maximum, pos - sliderMin,
sliderMax - sliderMin, opt.upsideDown);
}
inline int QSliderPrivate::pick(const QPoint &pt) const
{
return orientation == Qt::Horizontal ? pt.x() : pt.y();
}
/*!
Initialize \a option with the values from this QSlider. This method
is useful for subclasses when they need a QStyleOptionSlider, but don't want
to fill in all the information themselves.
\sa QStyleOption::initFrom()
*/
void QSlider::initStyleOption(QStyleOptionSlider *option) const
{
if (!option)
return;
Q_D(const QSlider);
option->initFrom(this);
option->subControls = QStyle::SC_None;
option->activeSubControls = QStyle::SC_None;
option->orientation = d->orientation;
option->maximum = d->maximum;
option->minimum = d->minimum;
option->tickPosition = (QSlider::TickPosition)d->tickPosition;
option->tickInterval = d->tickInterval;
option->upsideDown = (d->orientation == Qt::Horizontal) ?
(d->invertedAppearance != (option->direction == Qt::RightToLeft))
: (!d->invertedAppearance);
option->direction = Qt::LeftToRight; // we use the upsideDown option instead
option->sliderPosition = d->position;
option->sliderValue = d->value;
option->singleStep = d->singleStep;
option->pageStep = d->pageStep;
if (d->orientation == Qt::Horizontal)
option->state |= QStyle::State_Horizontal;
}
bool QSliderPrivate::updateHoverControl(const QPoint &pos)
{
Q_Q(QSlider);
QRect lastHoverRect = hoverRect;
QStyle::SubControl lastHoverControl = hoverControl;
bool doesHover = q->testAttribute(Qt::WA_Hover);
if (lastHoverControl != newHoverControl(pos) && doesHover) {
q->update(lastHoverRect);
q->update(hoverRect);
return true;
}
return !doesHover;
}
QStyle::SubControl QSliderPrivate::newHoverControl(const QPoint &pos)
{
Q_Q(QSlider);
QStyleOptionSlider opt;
q->initStyleOption(&opt);
opt.subControls = QStyle::SC_All;
QRect handleRect = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, q);
QRect grooveRect = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, q);
QRect tickmarksRect = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderTickmarks, q);
if (handleRect.contains(pos)) {
hoverRect = handleRect;
hoverControl = QStyle::SC_SliderHandle;
} else if (grooveRect.contains(pos)) {
hoverRect = grooveRect;
hoverControl = QStyle::SC_SliderGroove;
} else if (tickmarksRect.contains(pos)) {
hoverRect = tickmarksRect;
hoverControl = QStyle::SC_SliderTickmarks;
} else {
hoverRect = QRect();
hoverControl = QStyle::SC_None;
}
return hoverControl;
}
/*!
\class QSlider
\brief The QSlider widget provides a vertical or horizontal slider.
\ingroup basicwidgets
\inmodule QtWidgets
\image windows-slider.png
The slider is the classic widget for controlling a bounded value.
It lets the user move a slider handle along a horizontal or vertical
groove and translates the handle's position into an integer value
within the legal range.
QSlider has very few of its own functions; most of the functionality is in
QAbstractSlider. The most useful functions are setValue() to set
the slider directly to some value; triggerAction() to simulate
the effects of clicking (useful for shortcut keys);
setSingleStep(), setPageStep() to set the steps; and setMinimum()
and setMaximum() to define the range of the scroll bar.
QSlider provides methods for controlling tickmarks. You can use
setTickPosition() to indicate where you want the tickmarks to be,
setTickInterval() to indicate how many of them you want. the
currently set tick position and interval can be queried using the
tickPosition() and tickInterval() functions, respectively.
QSlider inherits a comprehensive set of signals:
\table
\header \li Signal \li Description
\row \li \l valueChanged()
\li Emitted when the slider's value has changed. The tracking()
determines whether this signal is emitted during user
interaction.
\row \li \l sliderPressed()
\li Emitted when the user starts to drag the slider.
\row \li \l sliderMoved()
\li Emitted when the user drags the slider.
\row \li \l sliderReleased()
\li Emitted when the user releases the slider.
\endtable
QSlider only provides integer ranges. Note that although
QSlider handles very large numbers, it becomes difficult for users
to use a slider accurately for very large ranges.
A slider accepts focus on Tab and provides both a mouse wheel and a
keyboard interface. The keyboard interface is the following:
\list
\li Left/Right move a horizontal slider by one single step.
\li Up/Down move a vertical slider by one single step.
\li PageUp moves up one page.
\li PageDown moves down one page.
\li Home moves to the start (mininum).
\li End moves to the end (maximum).
\endlist
\sa QScrollBar, QSpinBox, QDial, {fowler}{GUI Design Handbook: Slider}, {Sliders Example}
*/
/*!
\enum QSlider::TickPosition
This enum specifies where the tick marks are to be drawn relative
to the slider's groove and the handle the user moves.
\value NoTicks Do not draw any tick marks.
\value TicksBothSides Draw tick marks on both sides of the groove.
\value TicksAbove Draw tick marks above the (horizontal) slider
\value TicksBelow Draw tick marks below the (horizontal) slider
\value TicksLeft Draw tick marks to the left of the (vertical) slider
\value TicksRight Draw tick marks to the right of the (vertical) slider
*/
/*!
Constructs a vertical slider with the given \a parent.
*/
QSlider::QSlider(QWidget *parent)
: QSlider(Qt::Vertical, parent)
{
}
/*!
Constructs a slider with the given \a parent. The \a orientation
parameter determines whether the slider is horizontal or vertical;
the valid values are Qt::Vertical and Qt::Horizontal.
*/
QSlider::QSlider(Qt::Orientation orientation, QWidget *parent)
: QAbstractSlider(*new QSliderPrivate, parent)
{
d_func()->orientation = orientation;
d_func()->init();
}
/*!
Destroys this slider.
*/
QSlider::~QSlider()
{
}
/*!
\reimp
*/
void QSlider::paintEvent(QPaintEvent *)
{
Q_D(QSlider);
QPainter p(this);
QStyleOptionSlider opt;
initStyleOption(&opt);
opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
if (d->tickPosition != NoTicks)
opt.subControls |= QStyle::SC_SliderTickmarks;
if (d->pressedControl) {
opt.activeSubControls = d->pressedControl;
opt.state |= QStyle::State_Sunken;
} else {
opt.activeSubControls = d->hoverControl;
}
style()->drawComplexControl(QStyle::CC_Slider, &opt, &p, this);
}
/*!
\reimp
*/
bool QSlider::event(QEvent *event)
{
Q_D(QSlider);
switch(event->type()) {
case QEvent::HoverEnter:
case QEvent::HoverLeave:
case QEvent::HoverMove:
if (const QHoverEvent *he = static_cast<const QHoverEvent *>(event))
d->updateHoverControl(he->pos());
break;
case QEvent::StyleChange:
case QEvent::MacSizeChange:
d->resetLayoutItemMargins();
break;
default:
break;
}
return QAbstractSlider::event(event);
}
/*!
\reimp
*/
void QSlider::mousePressEvent(QMouseEvent *ev)
{
Q_D(QSlider);
if (d->maximum == d->minimum || (ev->buttons() ^ ev->button())) {
ev->ignore();
return;
}
#ifdef QT_KEYPAD_NAVIGATION
if (QApplicationPrivate::keypadNavigationEnabled())
setEditFocus(true);
#endif
ev->accept();
if ((ev->button() & style()->styleHint(QStyle::SH_Slider_AbsoluteSetButtons)) == ev->button()) {
QStyleOptionSlider opt;
initStyleOption(&opt);
const QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
const QPoint center = sliderRect.center() - sliderRect.topLeft();
// to take half of the slider off for the setSliderPosition call we use the center - topLeft
setSliderPosition(d->pixelPosToRangeValue(d->pick(ev->pos() - center)));
triggerAction(SliderMove);
setRepeatAction(SliderNoAction);
d->pressedControl = QStyle::SC_SliderHandle;
update();
} else if ((ev->button() & style()->styleHint(QStyle::SH_Slider_PageSetButtons)) == ev->button()) {
QStyleOptionSlider opt;
initStyleOption(&opt);
d->pressedControl = style()->hitTestComplexControl(QStyle::CC_Slider,
&opt, ev->pos(), this);
SliderAction action = SliderNoAction;
if (d->pressedControl == QStyle::SC_SliderGroove) {
const QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
int pressValue = d->pixelPosToRangeValue(d->pick(ev->pos() - sliderRect.center() + sliderRect.topLeft()));
d->pressValue = pressValue;
if (pressValue > d->value)
action = SliderPageStepAdd;
else if (pressValue < d->value)
action = SliderPageStepSub;
if (action) {
triggerAction(action);
setRepeatAction(action);
}
}
} else {
ev->ignore();
return;
}
if (d->pressedControl == QStyle::SC_SliderHandle) {
QStyleOptionSlider opt;
initStyleOption(&opt);
setRepeatAction(SliderNoAction);
QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
d->clickOffset = d->pick(ev->pos() - sr.topLeft());
update(sr);
setSliderDown(true);
}
}
/*!
\reimp
*/
void QSlider::mouseMoveEvent(QMouseEvent *ev)
{
Q_D(QSlider);
if (d->pressedControl != QStyle::SC_SliderHandle) {
ev->ignore();
return;
}
ev->accept();
int newPosition = d->pixelPosToRangeValue(d->pick(ev->pos()) - d->clickOffset);
QStyleOptionSlider opt;
initStyleOption(&opt);
setSliderPosition(newPosition);
}
/*!
\reimp
*/
void QSlider::mouseReleaseEvent(QMouseEvent *ev)
{
Q_D(QSlider);
if (d->pressedControl == QStyle::SC_None || ev->buttons()) {
ev->ignore();
return;
}
ev->accept();
QStyle::SubControl oldPressed = QStyle::SubControl(d->pressedControl);
d->pressedControl = QStyle::SC_None;
setRepeatAction(SliderNoAction);
if (oldPressed == QStyle::SC_SliderHandle)
setSliderDown(false);
QStyleOptionSlider opt;
initStyleOption(&opt);
opt.subControls = oldPressed;
update(style()->subControlRect(QStyle::CC_Slider, &opt, oldPressed, this));
}
/*!
\reimp
*/
QSize QSlider::sizeHint() const
{
Q_D(const QSlider);
ensurePolished();
const int SliderLength = 84, TickSpace = 5;
QStyleOptionSlider opt;
initStyleOption(&opt);
int thick = style()->pixelMetric(QStyle::PM_SliderThickness, &opt, this);
if (d->tickPosition & TicksAbove)
thick += TickSpace;
if (d->tickPosition & TicksBelow)
thick += TickSpace;
int w = thick, h = SliderLength;
if (d->orientation == Qt::Horizontal) {
w = SliderLength;
h = thick;
}
return style()->sizeFromContents(QStyle::CT_Slider, &opt, QSize(w, h), this).expandedTo(QApplication::globalStrut());
}
/*!
\reimp
*/
QSize QSlider::minimumSizeHint() const
{
Q_D(const QSlider);
QSize s = sizeHint();
QStyleOptionSlider opt;
initStyleOption(&opt);
int length = style()->pixelMetric(QStyle::PM_SliderLength, &opt, this);
if (d->orientation == Qt::Horizontal)
s.setWidth(length);
else
s.setHeight(length);
return s;
}
/*!
\property QSlider::tickPosition
\brief the tickmark position for this slider
The valid values are described by the QSlider::TickPosition enum.
The default value is \l QSlider::NoTicks.
\sa tickInterval
*/
void QSlider::setTickPosition(TickPosition position)
{
Q_D(QSlider);
d->tickPosition = position;
d->resetLayoutItemMargins();
update();
updateGeometry();
}
QSlider::TickPosition QSlider::tickPosition() const
{
return d_func()->tickPosition;
}
/*!
\property QSlider::tickInterval
\brief the interval between tickmarks
This is a value interval, not a pixel interval. If it is 0, the
slider will choose between singleStep and pageStep.
The default value is 0.
\sa tickPosition, singleStep, pageStep
*/
void QSlider::setTickInterval(int ts)
{
d_func()->tickInterval = qMax(0, ts);
update();
}
int QSlider::tickInterval() const
{
return d_func()->tickInterval;
}
Q_WIDGETS_EXPORT QStyleOptionSlider qt_qsliderStyleOption(QSlider *slider)
{
QStyleOptionSlider sliderOption;
slider->initStyleOption(&sliderOption);
return sliderOption;
}
QT_END_NAMESPACE
#include "moc_qslider.cpp"