From f1d363479575614bce8607dd9822a0ddac368af7 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 3 Jun 2020 15:35:40 +0200 Subject: Native style: improve debug possibilities Add more debug options, which is very helpful when working with QStyle. And use an enum to store it, to use less memory. Change-Id: I03fe3a5c827735641ce952daa797ab53a31f937f Reviewed-by: Mitch Curtis --- src/imports/nativestyle/items/qquickstyleitem.cpp | 72 ++++++++++++++++++----- src/imports/nativestyle/items/qquickstyleitem.h | 25 ++++++-- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/src/imports/nativestyle/items/qquickstyleitem.cpp b/src/imports/nativestyle/items/qquickstyleitem.cpp index 37ec9889..cb8df2eb 100644 --- a/src/imports/nativestyle/items/qquickstyleitem.cpp +++ b/src/imports/nativestyle/items/qquickstyleitem.cpp @@ -118,7 +118,7 @@ QSGNode *QQuickStyleItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePa node->setBounds(boundingRect()); #ifdef QT_DEBUG - if (m_debugNinePatchImage) { + if (m_debugFlags.testFlag(ShowUnscaled)) { const qreal scale = window()->devicePixelRatio(); const QSizeF ninePatchImageSize = m_paintedImage.rect().size() / scale; node->setBounds(QRectF(QPointF(), ninePatchImageSize)); @@ -248,11 +248,22 @@ void QQuickStyleItem::paintControlToImage() paintEvent(&painter); #ifdef QT_DEBUG - if (m_debug) { - painter.fillRect(m_paintedImage.rect(), QColor(rand() % 255, rand() % 255, rand() % 255, 50)); - if (m_debugNinePatchImage) { + if (m_debugFlags != NoDebug) { + painter.setPen(QColor(255, 0, 0, 255)); + if (m_debugFlags.testFlag(ShowImageRect)) + painter.drawRect(QRect(QPoint(0, 0), m_paintedImage.size() / scale)); + if (m_debugFlags.testFlag(ShowLayoutRect)) + painter.drawRect(m_styleItemGeometry.layoutRect); + if (m_debugFlags.testFlag(ShowContentRect)) + painter.drawRect(m_styleItemGeometry.contentRect); + if (m_debugFlags.testFlag(ShowInputContentSize)) { + 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(ShowUnscaled)) { const QPoint center = m_paintedImage.rect().center() / scale; - painter.setPen(Qt::red); painter.drawLine(center.x(), 0, center.x(), m_paintedImage.rect().height()); painter.drawLine(0, center.y(), m_paintedImage.rect().width(), center.y()); } @@ -278,17 +289,50 @@ 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_USE_NINEPATCH_IMAGE") == QStringLiteral("false")) - m_useNinePatchImage = false; - if (qEnvironmentVariable("QQC2_DEBUG") == QStringLiteral("true")) { - // Set the object name of any QML item to "debug" to print out - // extra information about that item. Optionally add "image" - // to draw the nine patch image (with markers) instead of the - // scaled image. + 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. Note that + // some of the options cannot be shown unless we switch off using + // nine patch image scaling. const QString name = m_control->objectName(); if (name.startsWith(QLatin1String("debug"))) { - m_debug = true; - m_debugNinePatchImage = name.contains(QStringLiteral("image")); + if (name.contains(QStringLiteral("output"))) { + m_debugFlags.setFlag(PrintOutput); + qDebug() << "debug: setting PrintOutput"; + } + if (name.contains(QStringLiteral("imagerect"))) { + m_debugFlags.setFlag(ShowImageRect); + qDebug() << "debug: setting ShowImageRect"; + } + if (name.contains(QStringLiteral("contentrect"))) { + m_debugFlags.setFlag(ShowContentRect); + m_useNinePatchImage = false; + qDebug() << "debug: setting ShowContentRect"; + qDebug() << "debug: setting useNinePatchImage to false"; + } + if (name.contains(QStringLiteral("layoutrect"))) { + m_debugFlags.setFlag(ShowLayoutRect); + m_useNinePatchImage = false; + qDebug() << "debug: setting ShowLayoutRect"; + qDebug() << "debug: setting useNinePatchImage to false"; + } + if (name.contains(QStringLiteral("inputcontentsize"))) { + m_debugFlags.setFlag(ShowInputContentSize); + m_useNinePatchImage = false; + qDebug() << "debug: setting ShowInputContentSize"; + qDebug() << "debug: setting useNinePatchImage to false"; + } + if (name.contains(QStringLiteral("dontuseninepatchimage"))) { + m_useNinePatchImage = false; + qDebug() << "debug: setting useNinePatchImage to false"; + } + if (name.contains(QStringLiteral("unscaled"))) { + m_debugFlags.setFlag(ShowUnscaled); + qDebug() << "debug: setting ShowUnscaled"; + } + if (m_debugFlags == NoDebug) + qDebug() << "debug options: output, imagerect, contentrect" + << "layoutrect, unscaled, inputcontentsize, dontuseninepatchimage"; } } #endif diff --git a/src/imports/nativestyle/items/qquickstyleitem.h b/src/imports/nativestyle/items/qquickstyleitem.h index a85e1c4d..5165c735 100644 --- a/src/imports/nativestyle/items/qquickstyleitem.h +++ b/src/imports/nativestyle/items/qquickstyleitem.h @@ -56,8 +56,8 @@ #endif #ifdef QT_DEBUG -#define qqc2Debug() if (m_debug) qDebug() << __FUNCTION__ << ":" -#define qqc2DebugHeading(HEADING) if (m_debug) qDebug() << "--------" << HEADING << "--------" +#define qqc2Debug() if (m_debugFlags.testFlag(PrintOutput)) qDebug() << __FUNCTION__ << ":" +#define qqc2DebugHeading(HEADING) if (m_debugFlags.testFlag(PrintOutput)) qDebug() << "--------" << HEADING << "--------" #else #define qqc2Debug() if (false) qDebug() #define qqc2DebugHeading(HEADING) if (false) qDebug() @@ -174,6 +174,19 @@ public: }; Q_DECLARE_FLAGS(DirtyFlags, DirtyFlag) +#ifdef QT_DEBUG + enum DebugFlag { + NoDebug = 0x00, + PrintOutput = 0x01, + ShowImageRect = 0x02, + ShowContentRect = 0x04, + ShowLayoutRect = 0x08, + ShowUnscaled = 0x10, + ShowInputContentSize = 0x20 + }; + Q_DECLARE_FLAGS(DebugFlags, DebugFlag) +#endif + QQuickStyleItem(); ~QQuickStyleItem() override; @@ -222,8 +235,7 @@ protected: } #ifdef QT_DEBUG - bool m_debug = false; - bool m_debugNinePatchImage = false; + DebugFlags m_debugFlags = NoDebug; #endif private: @@ -245,6 +257,11 @@ private: }; 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 -- cgit v1.2.3