diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2018-05-25 17:12:07 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2018-05-31 15:50:50 +0000 |
commit | c4a21708ed186640f4db381dc800febdbc85941e (patch) | |
tree | 72c5414f12b15d6b5e932666a6f4bb72f0ba2bfa /src/gui/painting/qbrush.cpp | |
parent | c538a333db4b7526fb4d9a82c06296d2492bf000 (diff) |
Provide presets for QGradient
Similar to Qt::GlobalColor, the presets allow the user to create
brushes based on predefined gradients, quickly getting pretty pixels
on screen.
The presets are based on the linear gradients from WebGradients, a
free collection of gradients, hosted at https://webgradients.com/.
The few radial and blended gradient presets have been excluded.
Change-Id: I1ce8f2210a6045c9edb8829ab3eddcc313549127
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/gui/painting/qbrush.cpp')
-rw-r--r-- | src/gui/painting/qbrush.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp index 5c13308d94..c667454cd2 100644 --- a/src/gui/painting/qbrush.cpp +++ b/src/gui/painting/qbrush.cpp @@ -46,9 +46,13 @@ #include "qvariant.h" #include "qline.h" #include "qdebug.h" +#include <QtCore/qjsondocument.h> +#include <QtCore/qjsonarray.h> #include <QtCore/qcoreapplication.h> #include "private/qhexstring_p.h" #include <QtCore/qnumeric.h> +#include <QtCore/qfile.h> +#include <QtCore/qmutex.h> QT_BEGIN_NAMESPACE @@ -1330,6 +1334,72 @@ QGradient::QGradient() { } +/*! + \enum QGradient::Preset + \since 5.12 + + This enum specifies a set of predefined presets for QGradient, + based on the gradients from https://webgradients.com/. +*/ + +/*! + \fn QGradient::QGradient(QGradient::Preset preset) + \since 5.12 + + Constructs a gradient based on a predefined \a preset. + + The coordinate mode of the resulting gradient is + QGradient::ObjectBoundingMode, allowing the preset + to be applied to arbitrary object sizes. +*/ +QGradient::QGradient(Preset preset) + : QGradient() +{ + static QHash<int, QGradient> cachedPresets; + static QMutex cacheMutex; + QMutexLocker locker(&cacheMutex); + if (cachedPresets.contains(preset)) { + const QGradient &cachedPreset = cachedPresets.value(preset); + m_type = cachedPreset.m_type; + m_data = cachedPreset.m_data; + m_stops = cachedPreset.m_stops; + m_spread = cachedPreset.m_spread; + dummy = cachedPreset.dummy; + } else { + static QJsonDocument jsonPresets = []() { + QFile webGradients(QLatin1String(":/qgradient/webgradients.binaryjson")); + webGradients.open(QFile::ReadOnly); + return QJsonDocument::fromBinaryData(webGradients.readAll()); + }(); + + const QJsonValue presetData = jsonPresets[preset - 1]; + if (!presetData.isObject()) + return; + + m_type = LinearGradient; + setCoordinateMode(ObjectBoundingMode); + setSpread(PadSpread); + + const QJsonValue start = presetData[QLatin1Literal("start")]; + const QJsonValue end = presetData[QLatin1Literal("end")]; + m_data.linear.x1 = start[QLatin1Literal("x")].toDouble(); + m_data.linear.y1 = start[QLatin1Literal("y")].toDouble(); + m_data.linear.x2 = end[QLatin1Literal("x")].toDouble(); + m_data.linear.y2 = end[QLatin1Literal("y")].toDouble(); + + for (const QJsonValue &stop : presetData[QLatin1String("stops")].toArray()) { + setColorAt(stop[QLatin1Literal("stop")].toDouble(), + QColor(QRgb(stop[QLatin1Literal("color")].toInt()))); + } + + cachedPresets.insert(preset, *this); + } +} + +QT_END_NAMESPACE +static void initGradientPresets() { Q_INIT_RESOURCE(qmake_webgradients); } +Q_CONSTRUCTOR_FUNCTION(initGradientPresets); +QT_BEGIN_NAMESPACE /*! \enum QGradient::Type |