diff options
Diffstat (limited to 'src/imports/nativestyle/items')
27 files changed, 2652 insertions, 0 deletions
diff --git a/src/imports/nativestyle/items/items.pri b/src/imports/nativestyle/items/items.pri new file mode 100644 index 00000000..f572cf24 --- /dev/null +++ b/src/imports/nativestyle/items/items.pri @@ -0,0 +1,31 @@ +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/qquickstyleitem.h \ + $$PWD/qquickstyleitembutton.h \ + $$PWD/qquickstyleitemgroupbox.h \ + $$PWD/qquickstyleitemcheckbox.h \ + $$PWD/qquickstyleitemradiobutton.h \ + $$PWD/qquickstyleitemslider.h \ + $$PWD/qquickstyleitemspinbox.h \ + $$PWD/qquickstyleitemtextfield.h \ + $$PWD/qquickstyleitemframe.h \ + $$PWD/qquickstyleitemcombobox.h \ + $$PWD/qquickstyleitemscrollbar.h \ + $$PWD/qquickstyleitemprogressbar.h \ + $$PWD/qquickstyleitemdial.h \ + +SOURCES += \ + $$PWD/qquickstyleitem.cpp \ + $$PWD/qquickstyleitembutton.cpp \ + $$PWD/qquickstyleitemgroupbox.cpp \ + $$PWD/qquickstyleitemcheckbox.cpp \ + $$PWD/qquickstyleitemradiobutton.cpp \ + $$PWD/qquickstyleitemslider.cpp \ + $$PWD/qquickstyleitemspinbox.cpp \ + $$PWD/qquickstyleitemtextfield.cpp \ + $$PWD/qquickstyleitemframe.cpp \ + $$PWD/qquickstyleitemcombobox.cpp \ + $$PWD/qquickstyleitemscrollbar.cpp \ + $$PWD/qquickstyleitemprogressbar.cpp \ + $$PWD/qquickstyleitemdial.cpp \ diff --git a/src/imports/nativestyle/items/qquickstyleitem.cpp b/src/imports/nativestyle/items/qquickstyleitem.cpp new file mode 100644 index 00000000..1fd933ea --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitem.cpp @@ -0,0 +1,431 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitem.h" + +#include <QtCore/qscopedvaluerollback.h> + +#include <QtQuick/qsgninepatchnode.h> +#include <QtQuick/private/qquickwindow_p.h> +#include <QtQuick/qquickwindow.h> + +#include <QtQuickTemplates2/private/qquickcontrol_p.h> +#include <QtQuickTemplates2/private/qquickbutton_p.h> + +#include <QtQml/qqml.h> + +#include "qquickstyleitembutton.h" +#include "qquickstylehelper_p.h" + +QT_BEGIN_NAMESPACE + +QDebug operator<<(QDebug debug, const QQuickStyleMargins &padding) +{ + QDebugStateSaver saver(debug); + debug.nospace(); + debug << "StyleMargins("; + debug << padding.left() << ", "; + debug << padding.top() << ", "; + debug << padding.right() << ", "; + debug << padding.bottom(); + debug << ')'; + return debug; +} + +QDebug operator<<(QDebug debug, const StyleItemGeometry &cg) +{ + QDebugStateSaver saver(debug); + debug.nospace(); + debug << "StyleItemGeometry("; + debug << "implicitSize:" << cg.implicitSize << ", "; + debug << "contentRect:" << cg.contentRect << ", "; + debug << "layoutRect:" << cg.layoutRect << ", "; + debug << "minimumSize:" << cg.minimumSize << ", "; + debug << "9patchMargins:" << cg.ninePatchMargins; + debug << ')'; + return debug; +} + +QQuickStyleItem::QQuickStyleItem() +{ + setFlag(QQuickItem::ItemHasContents); +} + +QQuickStyleItem::~QQuickStyleItem() +{ +} + +void QQuickStyleItem::connectToControl() +{ + connect(m_control, &QQuickStyleItem::enabledChanged, this, &QQuickStyleItem::markImageDirty); + connect(m_control, &QQuickItem::activeFocusChanged, this, &QQuickStyleItem::markImageDirty); + connect(m_control, &QQuickStyleItem::windowChanged, this, &QQuickStyleItem::markImageDirty); + connect(window(), &QQuickWindow::activeChanged, this, &QQuickStyleItem::markImageDirty); +} + +void QQuickStyleItem::markImageDirty() +{ + m_dirty.setFlag(DirtyFlag::Image); + polish(); +} + +void QQuickStyleItem::markGeometryDirty() +{ + m_dirty.setFlag(DirtyFlag::Geometry); + polish(); +} + +QSGNode *QQuickStyleItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) +{ + QSGNinePatchNode *node = static_cast<QSGNinePatchNode *>(oldNode); + if (!node) + node = window()->createNinePatchNode(); + + auto texture = window()->createTextureFromImage(m_paintedImage, QQuickWindow::TextureCanUseAtlas); + + QRectF bounds = boundingRect(); + const qreal scale = window()->devicePixelRatio(); + const QSizeF ninePatchImageSize = m_paintedImage.rect().size() / scale; +#ifdef QT_DEBUG + if (m_debugFlags.testFlag(Unscaled)) { + bounds = QRectF(QPointF(), ninePatchImageSize); + qqc2Debug() << "Setting paint node bounds to size of image:" << bounds; + } +#endif + + QMargins padding = m_useNinePatchImage ? m_styleItemGeometry.ninePatchMargins : QMargins(0, 0, 0, 0); + if (padding.right() == -1) { + // Special case: a right padding of -1 means that + // the image should not scale horizontally. + bounds.setWidth(ninePatchImageSize.width()); + padding.setLeft(0); + padding.setRight(0); + } else if (boundingRect().width() < m_styleItemGeometry.minimumSize.width()) { + // If the item size is smaller that the image, using nine-patch scaling + // ends up wrapping it. In that case we scale the whole image instead. + padding.setLeft(0); + padding.setRight(0); + } + if (padding.bottom() == -1) { + bounds.setHeight(ninePatchImageSize.height()); + padding.setTop(0); + padding.setBottom(0); + } else if (boundingRect().height() < m_styleItemGeometry.minimumSize.height()) { + padding.setTop(0); + padding.setBottom(0); + } + + node->setBounds(bounds); + node->setTexture(texture); + node->setDevicePixelRatio(window()->devicePixelRatio()); + node->setPadding(padding.left(), padding.top(), padding.right(), padding.bottom()); + node->update(); + + return node; +} + +QStyle::State QQuickStyleItem::controlSize(QQuickItem *item) +{ + // TODO: add proper API for small and mini + if (item->metaObject()->indexOfProperty("qqc2_style_small") != -1) + return QStyle::State_Small; + if (item->metaObject()->indexOfProperty("qqc2_style_mini") != -1) + return QStyle::State_Mini; + return QStyle::State_None; +} + +void QQuickStyleItem::initStyleOptionBase(QStyleOption &styleOption) +{ + Q_ASSERT(m_control); + + styleOption.control = const_cast<QQuickItem *>(control<QQuickItem>()); + styleOption.window = window(); + styleOption.palette = QQuickItemPrivate::get(m_control)->palette()->toQPalette(); + styleOption.rect = QRect(QPoint(0, 0), m_styleItemGeometry.minimumSize); + + styleOption.state = QStyle::State_None; + styleOption.state |= controlSize(styleOption.control); + + // Note: not all controls inherit from QQuickControl (e.g QQuickTextField) + if (const auto quickControl = dynamic_cast<QQuickControl *>(m_control.data())) + styleOption.direction = quickControl->isMirrored() ? Qt::RightToLeft : Qt::LeftToRight; + + if (window()) { + if (styleOption.window->isActive()) + styleOption.state |= QStyle::State_Active; + if (m_control->isEnabled()) + styleOption.state |= QStyle::State_Enabled; + if (m_control->hasActiveFocus()) + styleOption.state |= QStyle::State_HasFocus; + if (m_control->isUnderMouse()) + styleOption.state |= QStyle::State_MouseOver; + // Should this depend on the focusReason (e.g. only TabFocus) ? + styleOption.state |= QStyle::State_KeyboardFocusChange; + } + + qqc2Debug() << styleOption; +} + +void QQuickStyleItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + QQuickItem::geometryChange(newGeometry, oldGeometry); + + // Ensure that we only schedule a new geometry update + // and polish if this geometry change was caused by + // something else than us already updating geometry. + if (!m_polishing) + markGeometryDirty(); +} + +void QQuickStyleItem::updateGeometry() +{ + qqc2DebugHeading("GEOMETRY"); + m_dirty.setFlag(DirtyFlag::Geometry, false); + + const QQuickStyleMargins oldContentPadding = contentPadding(); + const QQuickStyleMargins oldLayoutMargins = layoutMargins(); + + m_styleItemGeometry = calculateGeometry(); + +#ifdef QT_DEBUG + if (m_styleItemGeometry.minimumSize.isEmpty()) + qmlWarning(this) << "minimumSize is empty!"; +#endif + + if (m_styleItemGeometry.implicitSize.isEmpty()) { + // If the item has no contents (or its size is + // empty), we just use the minimum size as implicit size. + m_styleItemGeometry.implicitSize = m_styleItemGeometry.minimumSize; + qqc2Debug() << "implicitSize is empty, using minimumSize instead"; + } + + if (contentPadding() != oldContentPadding) + emit contentPaddingChanged(); + if (layoutMargins() != oldLayoutMargins) + emit layoutMarginsChanged(); + + setImplicitSize(m_styleItemGeometry.implicitSize.width(), m_styleItemGeometry.implicitSize.height()); + + if (!m_useNinePatchImage) + m_styleItemGeometry.minimumSize = size().toSize(); + + qqc2Debug() << m_styleItemGeometry + << "bounding rect:" << boundingRect() + << "layout margins:" << layoutMargins() + << "content padding:" << contentPadding() + << "input content size:" << m_contentSize; +} + +void QQuickStyleItem::paintControlToImage() +{ + qqc2DebugHeading("PAINT"); + if (m_styleItemGeometry.minimumSize.isEmpty()) + return; + + m_dirty.setFlag(DirtyFlag::Image, false); + const qreal scale = window()->devicePixelRatio(); + m_paintedImage = QImage(m_styleItemGeometry.minimumSize * scale, QImage::Format_ARGB32_Premultiplied); + m_paintedImage.setDevicePixelRatio(scale); + m_paintedImage.fill(Qt::transparent); + + QPainter painter(&m_paintedImage); + paintEvent(&painter); + +#ifdef QT_DEBUG + if (m_debugFlags != NoDebug) { + painter.setPen(QColor(255, 0, 0, 255)); + if (m_debugFlags.testFlag(ImageRect)) + painter.drawRect(QRect(QPoint(0, 0), m_paintedImage.size() / scale)); + if (m_debugFlags.testFlag(LayoutRect)) { + const auto m = layoutMargins(); + QRect rect = QRect(QPoint(0, 0), m_paintedImage.size() / scale); + rect.adjust(m.left(), m.top(), -m.right(), -m.bottom()); + painter.drawRect(rect); + } + if (m_debugFlags.testFlag(ContentRect)) { + const auto p = contentPadding(); + QRect rect = QRect(QPoint(0, 0), m_paintedImage.size() / scale); + rect.adjust(p.left(), p.top(), -p.right(), -p.bottom()); + painter.drawRect(rect); + } + if (m_debugFlags.testFlag(InputContentSize)) { + const int offset = 2; + const QPoint p = m_styleItemGeometry.contentRect.topLeft(); + painter.drawLine(p.x() - offset, p.y() - offset, p.x() + m_contentSize.width(), p.y() - offset); + painter.drawLine(p.x() - offset, p.y() - offset, p.x() - offset, p.y() + m_contentSize.height()); + } + if (m_debugFlags.testFlag(NinePatchMargins)) { + const QMargins m = m_styleItemGeometry.ninePatchMargins; + const int w = int(m_paintedImage.rect().width() / scale); + const int h = int(m_paintedImage.rect().height() / scale); + if (m.right() != -1) { + painter.drawLine(m.left(), 0, m.left(), h); + painter.drawLine(w - m.right(), 0, w - m.right(), h); + } + if (m.bottom() != -1) { + painter.drawLine(0, m.top(), w, m.top()); + painter.drawLine(0, h - m.bottom(), w, h - m.bottom()); + } + } + } +#endif + + update(); +} + +void QQuickStyleItem::updatePolish() +{ + QScopedValueRollback<bool> guard(m_polishing, true); + + const bool dirtyGeometry = m_dirty & DirtyFlag::Geometry; + const bool dirtyImage = (m_dirty & DirtyFlag::Image) || (!m_useNinePatchImage && dirtyGeometry); + + if (dirtyGeometry) + updateGeometry(); + if (dirtyImage) + paintControlToImage(); +} + +void QQuickStyleItem::componentComplete() +{ + Q_ASSERT_X(m_control, Q_FUNC_INFO, "You need to assign a value to property 'control'"); + +#ifdef QT_DEBUG + if (!qEnvironmentVariable("QQC2_NATIVESTYLE_DEBUG").isEmpty()) { + // Set objectName to "debug" pluss one or more options separated + // by space to show extra information about this item. + +#define QQC2_DEBUG_FLAG(FLAG) \ + if (name.contains(QString(QLatin1String(#FLAG)).toLower())) m_debugFlags |= FLAG + + const QString name = m_control->objectName().toLower(); + if (name.startsWith(QString(QLatin1String("debug")).toLower())) { + QQC2_DEBUG_FLAG(Output); + QQC2_DEBUG_FLAG(ImageRect); + QQC2_DEBUG_FLAG(ContentRect); + QQC2_DEBUG_FLAG(LayoutRect); + QQC2_DEBUG_FLAG(InputContentSize); + QQC2_DEBUG_FLAG(DontUseNinePatchImage); + QQC2_DEBUG_FLAG(NinePatchMargins); + QQC2_DEBUG_FLAG(Unscaled); + + if (m_debugFlags & (DontUseNinePatchImage + | InputContentSize + | ContentRect + | LayoutRect + | NinePatchMargins)) { + // Some rects will not fit inside the drawn image unless + // we switch off (nine patch) image scaling. + m_debugFlags |= DontUseNinePatchImage; + m_useNinePatchImage = false; + } + + if (m_debugFlags != NoDebug) + qDebug() << "debug options set:" << m_debugFlags; + else + qDebug() << "available debug options:" << DebugFlags(0xFFFF); + } + } +#endif + + QQuickItem::componentComplete(); + connectToControl(); + polish(); +} + +qreal QQuickStyleItem::contentWidth() +{ + return m_contentSize.width(); +} + +void QQuickStyleItem::setContentWidth(qreal contentWidth) +{ + if (qFuzzyCompare(m_contentSize.width(), contentWidth)) + return; + + m_contentSize.setWidth(contentWidth); + markGeometryDirty(); +} + +qreal QQuickStyleItem::contentHeight() +{ + return m_contentSize.height(); +} + +void QQuickStyleItem::setContentHeight(qreal contentHeight) +{ + if (qFuzzyCompare(m_contentSize.height(), contentHeight)) + return; + + m_contentSize.setHeight(contentHeight); + markGeometryDirty(); +} + +QQuickStyleMargins QQuickStyleItem::contentPadding() const +{ + const QRect outerRect(QPoint(0, 0), m_styleItemGeometry.implicitSize); + return QQuickStyleMargins(outerRect, m_styleItemGeometry.contentRect); +} + +QQuickStyleMargins QQuickStyleItem::layoutMargins() const +{ + // ### TODO: layoutRect is currently not being used for anything. But + // eventually this information will be needed by layouts to align the controls + // correctly. This because the images drawn by QStyle are usually a bit bigger + // than the control(frame) itself, to e.g make room for shadow effects + // or focus rects/glow. And this will differ from control to control. The + // layoutRect will then inform where the frame of the control is. + const QRect outerRect(QPoint(0, 0), m_styleItemGeometry.implicitSize); + return QQuickStyleMargins(outerRect, m_styleItemGeometry.layoutRect); +} + +QFont QQuickStyleItem::styleFont(QQuickItem *control) +{ + Q_ASSERT(control); + // Note: This function should be treated as if it was static + // (meaning, don't assume anything in this object to be initialized). + // Resolving the font/font size should be done early on from QML, before we get + // around to calculate geometry and paint. Otherwise we typically need to do it + // all over again when/if the font changes. In practice this means that other + // items in QML that uses a style font, and at the same time, affects our input + // contentSize, cannot wait for this item to be fully constructed before it + // gets the font. So we need to resolve it here and now, even if this + // object might be in a half initialized state (hence also the control + // argument, instead of relying on m_control to be set). + return QGuiApplication::font(); +} + +QT_END_NAMESPACE diff --git a/src/imports/nativestyle/items/qquickstyleitem.h b/src/imports/nativestyle/items/qquickstyleitem.h new file mode 100644 index 00000000..ad315b96 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitem.h @@ -0,0 +1,274 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEM_H +#define QQUICKSTYLEITEM_H + +#include <QtCore/qdebug.h> +#include <QtQml/qqml.h> +#include <QtQml/qqmlinfo.h> +#include <QtQuick/private/qquickitem_p.h> +#include <QtQuickTemplates2/private/qquickcontrol_p.h> + +#include "qquicknativestyle.h" +#include "qquickstyle.h" +#include "qquickstyleoption.h" + +// Work-around for now, to avoid creator getting confused +// about missing macros. Should eventually be defined +// in qt declarative somewhere I assume. +#ifndef QML_NAMED_ELEMENT +#define QML_NAMED_ELEMENT(NAME) +#define QML_UNCREATABLE(NAME) +#endif + +#ifdef QT_DEBUG +#define qqc2Debug() if (m_debugFlags.testFlag(Output)) qDebug() << __FUNCTION__ << ":" +#define qqc2DebugHeading(HEADING) if (m_debugFlags.testFlag(Output)) qDebug() << "--------" << HEADING << "--------" +#else +#define qqc2Debug() if (false) qDebug() +#define qqc2DebugHeading(HEADING) if (false) qDebug() +#endif + +QT_BEGIN_NAMESPACE + +using namespace QQC2; + +class QQuickStyleMargins +{ + Q_GADGET + + Q_PROPERTY(int left READ left()) + Q_PROPERTY(int top READ top()) + Q_PROPERTY(int right READ right()) + Q_PROPERTY(int bottom READ bottom()) + + QML_NAMED_ELEMENT(StyleMargins) + +public: + QQuickStyleMargins() {} + QQuickStyleMargins(const QQuickStyleMargins &other) : m_margins(other.m_margins) {} + QQuickStyleMargins(const QMargins &margins) : m_margins(margins) {} + QQuickStyleMargins(const QRect &outer, const QRect &inner) + { + const int left = inner.left() - outer.left(); + const int top = inner.top() - outer.top(); + const int right = outer.right() - inner.right(); + const int bottom = outer.bottom() - inner.bottom(); + m_margins = QMargins(left, top, right, bottom); + } + + inline void operator=(const QQuickStyleMargins &other) { m_margins = other.m_margins; } + inline bool operator==(const QQuickStyleMargins &other) const { return other.m_margins == m_margins; } + inline bool operator!=(const QQuickStyleMargins &other) const { return other.m_margins != m_margins; } + + inline int left() const { return m_margins.left(); } + inline int right() const { return m_margins.right(); } + inline int top() const { return m_margins.top(); } + inline int bottom() const { return m_margins.bottom(); } + + QMargins m_margins; +}; + +QDebug operator<<(QDebug debug, const QQuickStyleMargins &padding); + +struct StyleItemGeometry +{ + /* + A QQuickItem is responsible for drawing a control, or a part of it. + + 'minimumSize' should be the minimum possible size that the item can + have _without_ taking content size into consideration (and still render + correctly). This will also be the size of the image that the item is drawn + to, unless QQuickStyleItem::useNinePatchImage is set to false. In that + case, the size of the image will be set to the size of the item instead + (which is set from QML, and will typically be the same as the size of the control). + The default way to calculate minimumSize is to call style()->sizeFromContents() + with an empty content size. This is not always well supported by the legacy QStyle + implementations, which means that you might e.g get an empty size in return). + For those cases, the correct solution is to go into the specific platform style + and change it so that it returns a valid size also for this special case. + + 'implicitSize' should reflect the preferred size of the item, taking the + given content size (as set from QML) into account. But not all controls + have contents (slider), and for many controls, the content/label is instead + placed outside the item/background image (radiobutton). In both cases, the + size of the item will not include the content size, and implicitSize can + usually be set equal to minimumSize instead. + + 'contentRect' should be the free space where the contents can be placed. Note that + this rect doesn't need to have the same size as the contentSize provided as input + to the style item. Instead, QStyle can typically calculate a rect that is bigger, to + e.g center the contents inside the control. + + 'layoutRect' can be set to shift the position of the whole control so + that aligns correctly with the other controls. This is important for + controls that draws e.g shadows or focus rings. Such adornments should + be painted, but not be included when aligning the controls. + */ + + QSize minimumSize; + QSize implicitSize; + QRect contentRect; + QRect layoutRect; + QMargins ninePatchMargins; +}; + +QDebug operator<<(QDebug debug, const StyleItemGeometry &cg); + +class QQuickStyleItem : public QQuickItem +{ + Q_OBJECT + + // Input + Q_PROPERTY(QQuickItem *control MEMBER m_control NOTIFY controlChanged) + Q_PROPERTY(qreal contentWidth READ contentWidth WRITE setContentWidth) + Q_PROPERTY(qreal contentHeight READ contentHeight WRITE setContentHeight) + Q_PROPERTY(bool useNinePatchImage MEMBER m_useNinePatchImage) + + // Output + Q_PROPERTY(QQuickStyleMargins contentPadding READ contentPadding() NOTIFY contentPaddingChanged) + Q_PROPERTY(QQuickStyleMargins layoutMargins READ layoutMargins() NOTIFY layoutMarginsChanged) + + QML_NAMED_ELEMENT(StyleItem) + QML_UNCREATABLE("StyleItem is an abstract base class.") + +public: + enum DirtyFlag { + Nothing = 0, + Geometry, + Image, + Everything = 255 + }; + Q_DECLARE_FLAGS(DirtyFlags, DirtyFlag) + +#ifdef QT_DEBUG + enum DebugFlag { + NoDebug = 0x00, + Output = 0x01, + ImageRect = 0x02, + ContentRect = 0x04, + LayoutRect = 0x08, + Unscaled = 0x10, + InputContentSize = 0x20, + DontUseNinePatchImage = 0x40, + NinePatchMargins = 0x80 + }; + Q_FLAG(DebugFlag) + Q_DECLARE_FLAGS(DebugFlags, DebugFlag) +#endif + + QQuickStyleItem(); + ~QQuickStyleItem() override; + + qreal contentWidth(); + void setContentWidth(qreal contentWidth); + qreal contentHeight(); + void setContentHeight(qreal contentHeight); + + QQuickStyleMargins contentPadding() const; + QQuickStyleMargins layoutMargins() const; + + Q_INVOKABLE virtual QFont styleFont(QQuickItem *control); + + void markGeometryDirty(); + void markImageDirty(); + +signals: + void controlChanged(); + void contentPaddingChanged(); + void layoutMarginsChanged(); + void fontChanged(); + +protected: + void componentComplete() override; + QSGNode *updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData) override; + void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; + void updatePolish() override; + + virtual void connectToControl(); + virtual void paintEvent(QPainter *painter) = 0; + virtual StyleItemGeometry calculateGeometry() = 0; + + static QStyle::State controlSize(QQuickItem *item); + void initStyleOptionBase(QStyleOption &styleOption); + + inline QSize contentSize() { return m_contentSize.toSize(); } + inline static QStyle *style() { return QQuickNativeStyle::style(); } + + template <class T> inline const T* control() const { +#ifdef QT_DEBUG + if (!dynamic_cast<T *>(m_control.data())) { + qmlWarning(this) << "control property is not of correct type"; + Q_UNREACHABLE(); + } +#endif + return static_cast<T *>(m_control.data()); + } + +#ifdef QT_DEBUG + DebugFlags m_debugFlags = NoDebug; +#endif + +private: + inline void updateGeometry(); + inline void paintControlToImage(); + +private: + QPointer<QQuickItem> m_control; + QImage m_paintedImage; + StyleItemGeometry m_styleItemGeometry; + QSizeF m_contentSize; + + DirtyFlags m_dirty = Everything; + bool m_useNinePatchImage = true; + bool m_polishing = false; + +private: + friend class QtQuickControls2MacOSStylePlugin; +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickStyleItem::DirtyFlags) + +#ifdef QT_DEBUG +Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickStyleItem::DebugFlags) +#endif + +QML_DECLARE_TYPE(QQuickStyleItem) + +QT_END_NAMESPACE + +#endif // QQUICKSTYLEITEM_H diff --git a/src/imports/nativestyle/items/qquickstyleitembutton.cpp b/src/imports/nativestyle/items/qquickstyleitembutton.cpp new file mode 100644 index 00000000..8e45cc69 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitembutton.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitembutton.h" + +QFont QQuickStyleItemButton::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_PushButtonLabel, controlSize(control)); +} + +void QQuickStyleItemButton::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto button = control<QQuickButton>(); + connect(button, &QQuickButton::downChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemButton::calculateGeometry() +{ + QStyleOptionButton styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_PushButton, &styleOption, QSize(0, 0)); + geometry.implicitSize = style()->sizeFromContents(QStyle::CT_PushButton, &styleOption, contentSize()); + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_PushButtonContents, &styleOption); + geometry.layoutRect = style()->subElementRect(QStyle::SE_PushButtonLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_PushButtonBevel, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemButton::paintEvent(QPainter *painter) +{ + QStyleOptionButton styleOption; + initStyleOption(styleOption); + style()->drawControl(QStyle::CE_PushButtonBevel, &styleOption, painter); +} + +void QQuickStyleItemButton::initStyleOption(QStyleOptionButton &styleOption) +{ + initStyleOptionBase(styleOption); + auto button = control<QQuickButton>(); + + if (button->isDown()) + styleOption.state |= QStyle::State_Sunken; + if (!button->isFlat() && !button->isDown()) + styleOption.state |= QStyle::State_Raised; + if (button->isHighlighted() || button->isChecked()) + styleOption.state |= QStyle::State_On; + if (button->isFlat()) + styleOption.features |= QStyleOptionButton::Flat; +} diff --git a/src/imports/nativestyle/items/qquickstyleitembutton.h b/src/imports/nativestyle/items/qquickstyleitembutton.h new file mode 100644 index 00000000..e23601a7 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitembutton.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMBUTTON_H +#define QQUICKSTYLEITEMBUTTON_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickbutton_p.h> + +class QQuickStyleItemButton : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(Button) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionButton &styleOption); +}; + +#endif // QQUICKSTYLEITEMBUTTON_H diff --git a/src/imports/nativestyle/items/qquickstyleitemcheckbox.cpp b/src/imports/nativestyle/items/qquickstyleitemcheckbox.cpp new file mode 100644 index 00000000..e5ee9133 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemcheckbox.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemcheckbox.h" + +QFont QQuickStyleItemCheckBox::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_RadioButtonLabel, controlSize(control)); +} + +void QQuickStyleItemCheckBox::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto checkbox = control<QQuickCheckBox>(); + connect(checkbox, &QQuickCheckBox::downChanged, this, &QQuickStyleItem::markImageDirty); + connect(checkbox, &QQuickCheckBox::checkStateChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemCheckBox::calculateGeometry() +{ + QStyleOptionButton styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_CheckBox, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_CheckBoxContents, &styleOption); + geometry.layoutRect = style()->subElementRect(QStyle::SE_CheckBoxLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_CheckBox, &styleOption, geometry.minimumSize); + + // Spacing seems to already be baked into SE_CheckBoxContents, so ignore until needed + //const int space = style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &styleOption); + + return geometry; +} + +void QQuickStyleItemCheckBox::paintEvent(QPainter *painter) +{ + QStyleOptionButton styleOption; + initStyleOption(styleOption); + style()->drawControl(QStyle::CE_CheckBox, &styleOption, painter); +} + +void QQuickStyleItemCheckBox::initStyleOption(QStyleOptionButton &styleOption) +{ + initStyleOptionBase(styleOption); + auto checkbox = control<QQuickCheckBox>(); + + styleOption.state |= checkbox->isDown() ? QStyle::State_Sunken : QStyle::State_Raised; + if (checkbox->isTristate() && checkbox->checkState() == Qt::PartiallyChecked) + styleOption.state |= QStyle::State_NoChange; + else + styleOption.state |= checkbox->isChecked() ? QStyle::State_On : QStyle::State_Off; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemcheckbox.h b/src/imports/nativestyle/items/qquickstyleitemcheckbox.h new file mode 100644 index 00000000..a8ccda2c --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemcheckbox.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMCHECKBOX_H +#define QQUICKSTYLEITEMCHECKBOX_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickcheckbox_p.h> + +class QQuickStyleItemCheckBox : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(CheckBox) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionButton &styleOption); +}; + +#endif // QQUICKSTYLEITEMCHECKBOX_H diff --git a/src/imports/nativestyle/items/qquickstyleitemcombobox.cpp b/src/imports/nativestyle/items/qquickstyleitemcombobox.cpp new file mode 100644 index 00000000..c9b7fca1 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemcombobox.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemcombobox.h" + +QFont QQuickStyleItemComboBox::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_PushButtonLabel, controlSize(control)); +} + +void QQuickStyleItemComboBox::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto comboBox = control<QQuickComboBox>(); + connect(comboBox, &QQuickComboBox::downChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemComboBox::calculateGeometry() +{ + QStyleOptionComboBox styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ComboBox, &styleOption, QSize(0, 0)); + geometry.implicitSize = style()->sizeFromContents(QStyle::CT_ComboBox, &styleOption, contentSize()); + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subControlRect(QStyle::CC_ComboBox, &styleOption, QStyle::SC_ComboBoxEditField); + geometry.layoutRect = style()->subElementRect(QStyle::SE_ComboBoxLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_ComboBox, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemComboBox::paintEvent(QPainter *painter) +{ + QStyleOptionComboBox styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_ComboBox, &styleOption, painter); +} + +void QQuickStyleItemComboBox::initStyleOption(QStyleOptionComboBox &styleOption) +{ + initStyleOptionBase(styleOption); + auto comboBox = control<QQuickComboBox>(); + + styleOption.subControls = QStyle::SC_ComboBoxFrame | QStyle::SC_ComboBoxArrow;// | QStyle::SC_ComboBoxEditField | QStyle::SC_ComboBoxArrow; + styleOption.frame = true; + styleOption.state |= QStyle::State_Selected; + styleOption.editable = comboBox->isEditable(); + + if (comboBox->isDown()) + styleOption.state |= QStyle::State_Sunken; + if (!comboBox->isFlat() && !comboBox->isDown()) + styleOption.state |= QStyle::State_Raised; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemcombobox.h b/src/imports/nativestyle/items/qquickstyleitemcombobox.h new file mode 100644 index 00000000..f446b191 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemcombobox.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMCOMBOBOX_H +#define QQUICKSTYLEITEMCOMBOBOX_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickcombobox_p.h> + +class QQuickStyleItemComboBox : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(ComboBox) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionComboBox &styleOption); +}; + +#endif // QQUICKSTYLEITEMCOMBOBOX_H diff --git a/src/imports/nativestyle/items/qquickstyleitemdial.cpp b/src/imports/nativestyle/items/qquickstyleitemdial.cpp new file mode 100644 index 00000000..f214fb2e --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemdial.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemdial.h" + +QFont QQuickStyleItemDial::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemDial::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto dial = control<QQuickDial>(); + connect(dial, &QQuickDial::fromChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::toChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::positionChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::valueChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::stepSizeChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::pressedChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemDial::calculateGeometry() +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_Dial, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + geometry.layoutRect = style()->subElementRect(QStyle::SE_SliderLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_Dial, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemDial::paintEvent(QPainter *painter) +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_Dial, &styleOption, painter); +} + +void QQuickStyleItemDial::initStyleOption(QStyleOptionSlider &styleOption) +{ + initStyleOptionBase(styleOption); + auto dial = control<QQuickDial>(); + + styleOption.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; + styleOption.activeSubControls = QStyle::SC_None; + styleOption.tickInterval = dial->stepSize(); + styleOption.dialWrapping = dial->wrap(); + styleOption.upsideDown = true; + + if (dial->isPressed()) + styleOption.state |= QStyle::State_Sunken; + + if (dial->stepSize() == 0) { + styleOption.minimum = 0; + styleOption.maximum = 10000; + styleOption.sliderPosition = dial->position() * styleOption.maximum; + } else { + styleOption.minimum = dial->from(); + styleOption.maximum = dial->to(); + styleOption.sliderPosition = dial->value(); + } + + // TODO: add proper API for tickmarks + const int index = dial->metaObject()->indexOfProperty("qqc2_style_tickPosition"); + if (index != -1) { + const int tickPosition = dial->metaObject()->property(index).read(dial).toInt(); + styleOption.tickPosition = QStyleOptionSlider::TickPosition(tickPosition); + if (styleOption.tickPosition != QStyleOptionSlider::NoTicks) + styleOption.subControls |= QStyle::SC_DialTickmarks; + } + +} diff --git a/src/imports/nativestyle/items/qquickstyleitemdial.h b/src/imports/nativestyle/items/qquickstyleitemdial.h new file mode 100644 index 00000000..3eefa734 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemdial.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMDIAL_H +#define QQUICKSTYLEITEMDIAL_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickdial_p.h> + +class QQuickStyleItemDial : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(Dial) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionSlider &styleOption); +}; + +#endif // QQUICKSTYLEITEMDIAL_H diff --git a/src/imports/nativestyle/items/qquickstyleitemframe.cpp b/src/imports/nativestyle/items/qquickstyleitemframe.cpp new file mode 100644 index 00000000..42a6c231 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemframe.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemframe.h" + +StyleItemGeometry QQuickStyleItemFrame::calculateGeometry() +{ + QStyleOptionFrame styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_Frame, &styleOption, QSize(0, 0)); + geometry.implicitSize = contentSize(); + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_FrameContents, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_ShapedFrame, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemFrame::paintEvent(QPainter *painter) +{ + QStyleOptionFrame styleOption; + initStyleOption(styleOption); + style()->drawControl(QStyle::CE_ShapedFrame, &styleOption, painter); +} + +void QQuickStyleItemFrame::initStyleOption(QStyleOptionFrame &styleOption) +{ + initStyleOptionBase(styleOption); + styleOption.lineWidth = 1; + styleOption.frameShape = QStyleOptionFrame::StyledPanel; + styleOption.features = QStyleOptionFrame::Flat; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemframe.h b/src/imports/nativestyle/items/qquickstyleitemframe.h new file mode 100644 index 00000000..70063ed6 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemframe.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMFRAME_H +#define QQUICKSTYLEITEMFRAME_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickframe_p.h> + +class QQuickStyleItemFrame : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(Frame) + +protected: + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionFrame &styleOption); +}; + +#endif // QQUICKSTYLEITEMFRAME_H diff --git a/src/imports/nativestyle/items/qquickstyleitemgroupbox.cpp b/src/imports/nativestyle/items/qquickstyleitemgroupbox.cpp new file mode 100644 index 00000000..d62b6f23 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemgroupbox.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemgroupbox.h" + +QFont QQuickStyleItemGroupBox::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_HeaderLabel, controlSize(control)); +} + +StyleItemGeometry QQuickStyleItemGroupBox::calculateGeometry() +{ + QStyleOptionGroupBox styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_GroupBox, &styleOption, QSize(0, 0)); + + if (!control<QQuickGroupBox>()->title().isEmpty()) { + // We don't draw the title, but we need to take + // it into calculation for the control size + styleOption.text = QStringLiteral(" "); + styleOption.subControls |= QStyle::SC_GroupBoxLabel; + } + + geometry.implicitSize = style()->sizeFromContents(QStyle::CT_GroupBox, &styleOption, contentSize()); + styleOption.rect.setSize(geometry.implicitSize); + geometry.contentRect = style()->subControlRect(QStyle::CC_GroupBox, &styleOption, QStyle::SC_GroupBoxContents); + geometry.layoutRect = style()->subElementRect(QStyle::SE_GroupBoxLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_GroupBox, &styleOption, geometry.minimumSize); + + const QQuickStyleMargins oldGroupBoxPadding = m_groupBoxPadding; + const QRect frame = style()->subControlRect(QStyle::CC_GroupBox, &styleOption, QStyle::SC_GroupBoxFrame); + m_groupBoxPadding = QQuickStyleMargins(QRect(QPoint(), geometry.implicitSize), frame); + if (m_groupBoxPadding != oldGroupBoxPadding) + emit groupBoxPaddingChanged(); + + const QPointF oldLabelPos = m_labelPos; + m_labelPos = style()->subControlRect(QStyle::CC_GroupBox, &styleOption, QStyle::SC_GroupBoxLabel).topLeft(); + if (m_labelPos != oldLabelPos) + emit labelPosChanged(); + return geometry; +} + +void QQuickStyleItemGroupBox::paintEvent(QPainter *painter) +{ + QStyleOptionGroupBox styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_GroupBox, &styleOption, painter); +} + +void QQuickStyleItemGroupBox::initStyleOption(QStyleOptionGroupBox &styleOption) +{ + initStyleOptionBase(styleOption); + styleOption.subControls = QStyle::SC_GroupBoxFrame; + styleOption.lineWidth = 1; +} + +QQuickStyleMargins QQuickStyleItemGroupBox::groupBoxPadding() const +{ + return m_groupBoxPadding; +} + +QPointF QQuickStyleItemGroupBox::labelPos() const +{ + return m_labelPos; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemgroupbox.h b/src/imports/nativestyle/items/qquickstyleitemgroupbox.h new file mode 100644 index 00000000..efb6d4f6 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemgroupbox.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMGROUPBOX_H +#define QQUICKSTYLEITEMGROUPBOX_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickgroupbox_p.h> + +class QQuickStyleItemGroupBox : public QQuickStyleItem +{ + Q_OBJECT + Q_PROPERTY(QQuickStyleMargins groupBoxPadding READ groupBoxPadding NOTIFY groupBoxPaddingChanged) + Q_PROPERTY(QPointF labelPos READ labelPos NOTIFY labelPosChanged) + QML_NAMED_ELEMENT(GroupBox) + +public: + QQuickStyleMargins groupBoxPadding() const; + QPointF labelPos() const; + QFont styleFont(QQuickItem *control) override; + +signals: + void groupBoxPaddingChanged(); + void labelPosChanged(); + +protected: + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + QQuickStyleMargins m_groupBoxPadding; + QPointF m_labelPos; + + void initStyleOption(QStyleOptionGroupBox &styleOption); +}; + +#endif // QQUICKSTYLEITEMGROUPBOX_H diff --git a/src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp b/src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp new file mode 100644 index 00000000..3429b5ac --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemprogressbar.h" + +QFont QQuickStyleItemProgressBar::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemProgressBar::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto progressBar = control<QQuickProgressBar>(); + connect(progressBar, &QQuickProgressBar::fromChanged, this, &QQuickStyleItem::markImageDirty); + connect(progressBar, &QQuickProgressBar::toChanged, this, &QQuickStyleItem::markImageDirty); + connect(progressBar, &QQuickProgressBar::positionChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemProgressBar::calculateGeometry() +{ + QStyleOptionProgressBar styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ProgressBar, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_ProgressBarContents, &styleOption); + geometry.layoutRect = style()->subElementRect(QStyle::SE_ProgressBarLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_ProgressBar, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemProgressBar::paintEvent(QPainter *painter) +{ + QStyleOptionProgressBar styleOption; + initStyleOption(styleOption); + if (m_subControl == Groove) { + styleOption.rect = style()->subElementRect(QStyle::SE_ProgressBarGroove, &styleOption); + style()->drawControl(QStyle::CE_ProgressBarGroove, &styleOption, painter); + } else { + styleOption.rect = style()->subElementRect(QStyle::SE_ProgressBarContents, &styleOption); + style()->drawControl(QStyle::CE_ProgressBarContents, &styleOption, painter); + } +} + +void QQuickStyleItemProgressBar::initStyleOption(QStyleOptionProgressBar &styleOption) +{ + initStyleOptionBase(styleOption); + auto progressBar = control<QQuickProgressBar>(); + + styleOption.state = QStyle::State_Horizontal; + + if (progressBar->isIndeterminate()) { + styleOption.minimum = 0; + styleOption.maximum = 0; + } else { + styleOption.minimum = progressBar->from(); + styleOption.maximum = progressBar->to(); + styleOption.progress = progressBar->value(); + } +} diff --git a/src/imports/nativestyle/items/qquickstyleitemprogressbar.h b/src/imports/nativestyle/items/qquickstyleitemprogressbar.h new file mode 100644 index 00000000..f6281f0f --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemprogressbar.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMPROGRESSBAR_H +#define QQUICKSTYLEITEMPROGRESSBAR_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickprogressbar_p.h> + +class QQuickStyleItemProgressBar : public QQuickStyleItem +{ + Q_OBJECT + + Q_PROPERTY(SubControl subControl MEMBER m_subControl) + + QML_NAMED_ELEMENT(ProgressBar) + +public: + enum SubControl { + Groove = 1, + Contents, + }; + Q_ENUM(SubControl) + + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionProgressBar &styleOption); + +private: + SubControl m_subControl = Groove; +}; + +#endif // QQUICKSTYLEITEMPROGRESSBAR_H diff --git a/src/imports/nativestyle/items/qquickstyleitemradiobutton.cpp b/src/imports/nativestyle/items/qquickstyleitemradiobutton.cpp new file mode 100644 index 00000000..c8bb251f --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemradiobutton.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemradiobutton.h" + +QFont QQuickStyleItemRadioButton::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_RadioButtonLabel, controlSize(control)); +} + +void QQuickStyleItemRadioButton::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto checkbox = control<QQuickRadioButton>(); + connect(checkbox, &QQuickRadioButton::downChanged, this, &QQuickStyleItem::markImageDirty); + connect(checkbox, &QQuickRadioButton::checkedChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemRadioButton::calculateGeometry() +{ + QStyleOptionButton styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_RadioButton, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_RadioButtonContents, &styleOption); + geometry.layoutRect = style()->subElementRect(QStyle::SE_RadioButtonLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_RadioButton, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemRadioButton::paintEvent(QPainter *painter) +{ + QStyleOptionButton styleOption; + initStyleOption(styleOption); + style()->drawControl(QStyle::CE_RadioButton, &styleOption, painter); +} + +void QQuickStyleItemRadioButton::initStyleOption(QStyleOptionButton &styleOption) +{ + initStyleOptionBase(styleOption); + auto checkbox = control<QQuickRadioButton>(); + + styleOption.state |= checkbox->isDown() ? QStyle::State_Sunken : QStyle::State_Raised; + styleOption.state |= checkbox->isChecked() ? QStyle::State_On : QStyle::State_Off; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemradiobutton.h b/src/imports/nativestyle/items/qquickstyleitemradiobutton.h new file mode 100644 index 00000000..5ee9097b --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemradiobutton.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMRADIOBUTTON_H +#define QQUICKSTYLEITEMRADIOBUTTON_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickradiobutton_p.h> + +class QQuickStyleItemRadioButton : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(RadioButton) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionButton &styleOption); +}; + +#endif // QQUICKSTYLEITEMRADIOBUTTON_H diff --git a/src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp b/src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp new file mode 100644 index 00000000..72f850a8 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemscrollbar.h" + +QFont QQuickStyleItemScrollBar::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemScrollBar::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto scrollBar = control<QQuickScrollBar>(); + connect(scrollBar, &QQuickScrollBar::orientationChanged, this, &QQuickStyleItem::markImageDirty); + connect(scrollBar, &QQuickScrollBar::pressedChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemScrollBar::calculateGeometry() +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ScrollBar, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.layoutRect = style()->subElementRect(QStyle::SE_ScrollBarLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_ScrollBar, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemScrollBar::paintEvent(QPainter *painter) +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_ScrollBar, &styleOption, painter); +} + +void QQuickStyleItemScrollBar::initStyleOption(QStyleOptionSlider &styleOption) +{ + initStyleOptionBase(styleOption); + auto scrollBar = control<QQuickScrollBar>(); + + styleOption.subControls = m_subControl == Groove ? QStyle::SC_ScrollBarGroove : QStyle::SC_ScrollBarSlider; + styleOption.activeSubControls = QStyle::SC_None; + styleOption.orientation = scrollBar->orientation(); + + if (scrollBar->isPressed()) + styleOption.state |= QStyle::State_Sunken; + +#ifdef Q_OS_MACOS + if (m_overrideState != None) { + // In ScrollBar.qml we fade between two versions of + // the handle, depending on if it's hovered or not + if (m_overrideState & AlwaysHovered) + styleOption.state |= QStyle::State_Sunken; + else if (m_overrideState & NeverHovered) + styleOption.state &= ~QStyle::State_Sunken; + } +#endif + + // The following values will let the handle fill 100% of the + // groove / imageSize. But when the handle is resized by + // QQuickScrollBar, it will end up with the correct size visually. + styleOption.pageStep = 1000; + styleOption.minimum = 0; + styleOption.maximum = 1; + styleOption.sliderValue = 0; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemscrollbar.h b/src/imports/nativestyle/items/qquickstyleitemscrollbar.h new file mode 100644 index 00000000..db241ad8 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemscrollbar.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMSCROLLBAR_H +#define QQUICKSTYLEITEMSCROLLBAR_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickscrollbar_p.h> + +class QQuickStyleItemScrollBar : public QQuickStyleItem +{ + Q_OBJECT + + Q_PROPERTY(SubControl subControl MEMBER m_subControl) + +#ifdef Q_OS_MACOS + Q_PROPERTY(OverrideState overrideState MEMBER m_overrideState) +#endif + + QML_NAMED_ELEMENT(ScrollBar) + +public: + enum SubControl { + Groove = 1, + Handle, + }; + Q_ENUM(SubControl) + +#ifdef Q_OS_MACOS + enum OverrideState { + None = 0, + AlwaysHovered, + NeverHovered, + }; + Q_ENUM(OverrideState) +#endif + + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionSlider &styleOption); + +private: + SubControl m_subControl = Groove; + +#ifdef Q_OS_MACOS + OverrideState m_overrideState = None; +#endif +}; + +#endif // QQUICKSTYLEITEMSCROLLBAR_H diff --git a/src/imports/nativestyle/items/qquickstyleitemslider.cpp b/src/imports/nativestyle/items/qquickstyleitemslider.cpp new file mode 100644 index 00000000..5ca282cc --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemslider.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemslider.h" + +QFont QQuickStyleItemSlider::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemSlider::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto slider = control<QQuickSlider>(); + connect(slider, &QQuickSlider::fromChanged, this, &QQuickStyleItem::markImageDirty); + connect(slider, &QQuickSlider::toChanged, this, &QQuickStyleItem::markImageDirty); + connect(slider, &QQuickSlider::positionChanged, this, &QQuickStyleItem::markImageDirty); + connect(slider, &QQuickSlider::valueChanged, this, &QQuickStyleItem::markImageDirty); + connect(slider, &QQuickSlider::stepSizeChanged, this, &QQuickStyleItem::markImageDirty); + connect(slider, &QQuickSlider::pressedChanged, this, &QQuickStyleItem::markImageDirty); + connect(slider, &QQuickSlider::orientationChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemSlider::calculateGeometry() +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_Slider, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + geometry.layoutRect = style()->subElementRect(QStyle::SE_SliderLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_Slider, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemSlider::paintEvent(QPainter *painter) +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_Slider, &styleOption, painter); +} + +void QQuickStyleItemSlider::initStyleOption(QStyleOptionSlider &styleOption) +{ + initStyleOptionBase(styleOption); + auto slider = control<QQuickSlider>(); + + styleOption.subControls = m_subControl == Groove ? QStyle::SC_SliderGroove : QStyle::SC_SliderHandle; + styleOption.activeSubControls = QStyle::SC_None; + styleOption.orientation = slider->orientation(); + styleOption.tickInterval = slider->stepSize(); + + if (slider->isPressed()) + styleOption.state |= QStyle::State_Sunken; + + if (slider->stepSize() == 0) { + styleOption.minimum = 0; + styleOption.maximum = 10000; + styleOption.sliderPosition = slider->position() * styleOption.maximum; + } else { + styleOption.minimum = slider->from(); + styleOption.maximum = slider->to(); + styleOption.sliderPosition = slider->value(); + + // TODO: add proper API for tickmarks + const int index = slider->metaObject()->indexOfProperty("qqc2_style_tickPosition"); + if (index != -1) { + const int tickPosition = slider->metaObject()->property(index).read(slider).toInt(); + styleOption.tickPosition = QStyleOptionSlider::TickPosition(tickPosition); + if (styleOption.tickPosition != QStyleOptionSlider::NoTicks) + styleOption.subControls |= QStyle::SC_SliderTickmarks; + } + } +} diff --git a/src/imports/nativestyle/items/qquickstyleitemslider.h b/src/imports/nativestyle/items/qquickstyleitemslider.h new file mode 100644 index 00000000..1108930b --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemslider.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMSLIDER_H +#define QQUICKSTYLEITEMSLIDER_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickslider_p.h> + +class QQuickStyleItemSlider : public QQuickStyleItem +{ + Q_OBJECT + + Q_PROPERTY(SubControl subControl MEMBER m_subControl) + + QML_NAMED_ELEMENT(Slider) + +public: + enum SubControl { + Groove = 1, + Handle, + }; + Q_ENUM(SubControl) + + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionSlider &styleOption); + +private: + SubControl m_subControl = Groove; +}; + +#endif // QQUICKSTYLEITEMSLIDER_H diff --git a/src/imports/nativestyle/items/qquickstyleitemspinbox.cpp b/src/imports/nativestyle/items/qquickstyleitemspinbox.cpp new file mode 100644 index 00000000..a2c4f1d1 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemspinbox.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemspinbox.h" + +QFont QQuickStyleItemSpinBox::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ComboBoxLabel, controlSize(control)); +} + +void QQuickStyleItemSpinBox::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto spinbox = control<QQuickSpinBox>(); + connect(spinbox->up(), &QQuickSpinButton::pressedChanged, this, &QQuickStyleItem::markImageDirty); + connect(spinbox->down(), &QQuickSpinButton::pressedChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemSpinBox::calculateGeometry() +{ + QStyleOptionSpinBox styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_SpinBox, &styleOption, QSize(0, 0)); + + if (styleOption.subControls == QStyle::SC_SpinBoxFrame) { + geometry.implicitSize = style()->sizeFromContents(QStyle::CT_SpinBox, &styleOption, contentSize()); + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subControlRect(QStyle::CC_SpinBox, &styleOption, QStyle::SC_SpinBoxEditField); + geometry.layoutRect = style()->subElementRect(QStyle::SE_SpinBoxLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_SpinBox, &styleOption, geometry.minimumSize); + } else { + geometry.implicitSize = geometry.minimumSize; + } + + return geometry; +} + +void QQuickStyleItemSpinBox::paintEvent(QPainter *painter) +{ + QStyleOptionSpinBox styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_SpinBox, &styleOption, painter); +} + +void QQuickStyleItemSpinBox::initStyleOption(QStyleOptionSpinBox &styleOption) +{ + initStyleOptionBase(styleOption); + auto spinbox = control<QQuickSpinBox>(); + + switch (m_subControl) { + case Frame: + styleOption.subControls = QStyle::SC_SpinBoxFrame; + styleOption.frame = true; + break; + case Up: + styleOption.subControls = QStyle::SC_SpinBoxUp; + break; + case Down: + styleOption.subControls = QStyle::SC_SpinBoxDown; + break; + } + + if (spinbox->up()->isPressed()) { + styleOption.activeSubControls = QStyle::SC_SpinBoxUp; + styleOption.state |= QStyle::State_Sunken; + } else if (spinbox->down()->isPressed()) { + styleOption.activeSubControls = QStyle::SC_SpinBoxDown; + styleOption.state |= QStyle::State_Sunken; + } + + styleOption.buttonSymbols = QStyleOptionSpinBox::UpDownArrows; + styleOption.stepEnabled = QStyleOptionSpinBox::StepEnabled; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemspinbox.h b/src/imports/nativestyle/items/qquickstyleitemspinbox.h new file mode 100644 index 00000000..101d4b2a --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemspinbox.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMSPINBOX_H +#define QQUICKSTYLEITEMSPINBOX_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickspinbox_p.h> + +class QQuickStyleItemSpinBox : public QQuickStyleItem +{ + Q_OBJECT + + Q_PROPERTY(SubControl subControl MEMBER m_subControl) + + QML_NAMED_ELEMENT(SpinBox) + +public: + enum SubControl { + Frame = 1, + Up, + Down, + }; + Q_ENUM(SubControl) + + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionSpinBox &styleOption); + +private: + SubControl m_subControl = Frame; +}; + +#endif // QQUICKSTYLEITEMSPINBOX_H diff --git a/src/imports/nativestyle/items/qquickstyleitemtextfield.cpp b/src/imports/nativestyle/items/qquickstyleitemtextfield.cpp new file mode 100644 index 00000000..1a262033 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemtextfield.cpp @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemtextfield.h" + +QFont QQuickStyleItemTextField::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ComboBoxLabel, controlSize(control)); +} + +void QQuickStyleItemTextField::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto textField = control<QQuickTextField>(); + connect(textField, &QQuickTextField::readOnlyChanged, this, &QQuickStyleItem::markImageDirty); + connect(textField, &QQuickTextField::focusChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemTextField::calculateGeometry() +{ + QStyleOptionFrame styleOption; + initStyleOption(styleOption); + StyleItemGeometry geometry; + + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_LineEdit, &styleOption, QSize(0, 0)); + geometry.implicitSize = style()->sizeFromContents(QStyle::CT_LineEdit, &styleOption, contentSize()); + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_LineEditContents, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_ShapedFrame, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemTextField::paintEvent(QPainter *painter) +{ + QStyleOptionFrame styleOption; + initStyleOption(styleOption); + style()->drawPrimitive(QStyle::PE_PanelLineEdit, &styleOption, painter); +} + +void QQuickStyleItemTextField::initStyleOption(QStyleOptionFrame &styleOption) +{ + initStyleOptionBase(styleOption); + auto textField = control<QQuickTextField>(); + + styleOption.lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &styleOption); + styleOption.midLineWidth = 0; + styleOption.state |= QStyle::State_Sunken; + if (textField->isReadOnly()) + styleOption.state |= QStyle::State_ReadOnly; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemtextfield.h b/src/imports/nativestyle/items/qquickstyleitemtextfield.h new file mode 100644 index 00000000..57aa8885 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemtextfield.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMTEXTFIELD_H +#define QQUICKSTYLEITEMTEXTFIELD_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquicktextfield_p.h> + +class QQuickStyleItemTextField : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(TextField) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionFrame &styleOption); +}; + +#endif // QQUICKSTYLEITEMTEXTFIELD_H |