Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow showing only the current item in the Quick preview widget #312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions plugins/quickinspector/quickinspector.cpp
Expand Up @@ -60,6 +60,7 @@
#include <QQuickItem>
#include <QQuickWindow>
#include <QQuickView>
#include <QQuickItemGrabResult>

#include <QQmlContext>
#include <QQmlEngine>
Expand Down Expand Up @@ -370,6 +371,7 @@ QuickInspector::QuickInspector(ProbeInterface *probe, QObject *parent)
, m_remoteView(new RemoteViewServer(QStringLiteral("com.kdab.GammaRay.QuickRemoteView"), this))
, m_pendingRenderMode(new RenderModeRequest(this))
, m_renderMode(QuickInspectorInterface::NormalRendering)
, m_grabMode(QuickInspectorInterface::FullWindow)
{
registerMetaTypes();
registerVariantHandlers();
Expand Down Expand Up @@ -561,6 +563,7 @@ void QuickInspector::recreateOverlay()
{
ProbeGuard guard;
m_overlay = new QuickOverlay;
m_overlay->setGrabMode(m_grabMode);

connect(m_overlay.data(), &QuickOverlay::sceneChanged, m_remoteView, &RemoteViewServer::sourceChanged);
connect(m_overlay.data(), &QuickOverlay::sceneGrabbed, this, &QuickInspector::sendRenderedScene);
Expand Down Expand Up @@ -658,6 +661,16 @@ void QuickInspector::checkServerSideDecorations()
emit serverSideDecorations(m_overlay->decorationsEnabled());
}

void QuickInspector::setGrabMode(QuickInspectorInterface::GrabMode mode)
{
if (m_grabMode == mode) {
return;
}

m_grabMode = mode;
m_overlay->setGrabMode(mode);
}

void QuickInspector::setOverlaySettings(const GammaRay::QuickDecorationsSettings &settings)
{
m_overlay->setSettings(settings);
Expand Down
3 changes: 3 additions & 0 deletions plugins/quickinspector/quickinspector.h
Expand Up @@ -118,6 +118,8 @@ public slots:

void checkOverlaySettings() override;

void setGrabMode(GammaRay::QuickInspectorInterface::GrabMode mode) override;

void requestElementsAt(const QPoint &pos, GammaRay::RemoteViewInterface::RequestMode mode);
void pickElementId(const GammaRay::ObjectId& id);

Expand Down Expand Up @@ -165,6 +167,7 @@ private slots:
RemoteViewServer *m_remoteView;
RenderModeRequest *m_pendingRenderMode;
QuickInspectorInterface::RenderMode m_renderMode;
GrabMode m_grabMode;
};

class QuickInspectorFactory : public QObject,
Expand Down
5 changes: 5 additions & 0 deletions plugins/quickinspector/quickinspectorclient.cpp
Expand Up @@ -88,3 +88,8 @@ void QuickInspectorClient::checkOverlaySettings()
{
Endpoint::instance()->invokeObject(objectName(), "checkOverlaySettings");
}

void QuickInspectorClient::setGrabMode(QuickInspectorInterface::GrabMode mode)
{
Endpoint::instance()->invokeObject(objectName(), "setGrabMode", { QVariant::fromValue(mode) });
}
2 changes: 2 additions & 0 deletions plugins/quickinspector/quickinspectorclient.h
Expand Up @@ -62,6 +62,8 @@ public slots:
void setOverlaySettings(const GammaRay::QuickDecorationsSettings &settings) override;

void checkOverlaySettings() override;

void setGrabMode(GammaRay::QuickInspectorInterface::GrabMode mode) override;
};
}

Expand Down
15 changes: 15 additions & 0 deletions plugins/quickinspector/quickinspectorinterface.cpp
Expand Up @@ -63,6 +63,20 @@ QDataStream &operator>>(QDataStream &in, QuickInspectorInterface::RenderMode &va
return in;
}

QDataStream &operator<<(QDataStream &out, QuickInspectorInterface::GrabMode value)
{
out << qint32(value);
return out;
}

QDataStream &operator>>(QDataStream &in, QuickInspectorInterface::GrabMode &value)
{
qint32 t;
in >> t;
value = static_cast<QuickInspectorInterface::GrabMode>(t);
return in;
}

QuickInspectorInterface::QuickInspectorInterface(QObject *parent)
: QObject(parent)
{
Expand All @@ -72,6 +86,7 @@ QuickInspectorInterface::QuickInspectorInterface(QObject *parent)
qRegisterMetaTypeStreamOperators<QuickItemGeometry>();
qRegisterMetaTypeStreamOperators<QVector<QuickItemGeometry>>();
qRegisterMetaTypeStreamOperators<QuickDecorationsSettings>();
qRegisterMetaTypeStreamOperators<GrabMode>();
}

QuickInspectorInterface::~QuickInspectorInterface()
Expand Down
9 changes: 9 additions & 0 deletions plugins/quickinspector/quickinspectorinterface.h
Expand Up @@ -70,8 +70,14 @@ class QuickInspectorInterface : public QObject
VisualizeTraces,
};

enum GrabMode {
FullWindow,
Item
};

Q_ENUMS(RenderMode)
Q_DECLARE_FLAGS(Features, Feature)
Q_ENUMS(PreviewMode)

explicit QuickInspectorInterface(QObject *parent = nullptr);
~QuickInspectorInterface();
Expand All @@ -92,6 +98,8 @@ public slots:

virtual void checkOverlaySettings() = 0;

virtual void setGrabMode(GrabMode mode) = 0;

signals:
void features(GammaRay::QuickInspectorInterface::Features features);
void serverSideDecorations(bool enabled);
Expand All @@ -101,6 +109,7 @@ public slots:

Q_DECLARE_METATYPE(GammaRay::QuickInspectorInterface::Features)
Q_DECLARE_METATYPE(GammaRay::QuickInspectorInterface::RenderMode)
Q_DECLARE_METATYPE(GammaRay::QuickInspectorInterface::GrabMode)
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(GammaRay::QuickInspectorInterface,
"com.kdab.GammaRay.QuickInspectorInterface/1.0")
Expand Down
58 changes: 46 additions & 12 deletions plugins/quickinspector/quickoverlay.cpp
Expand Up @@ -37,6 +37,7 @@
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLPaintDevice>
#include <QQuickItemGrabResult>

#include <private/qquickanchors_p.h>
#include <private/qquickitem_p.h>
Expand Down Expand Up @@ -196,6 +197,7 @@ QuickOverlay::QuickOverlay()
, m_currentToplevelItem(nullptr)
, m_isGrabbingMode(false)
, m_decorationsEnabled(true)
, m_grabMode(QuickInspectorInterface::FullWindow)
{
const QMetaObject *mo = metaObject();
m_sceneGrabbed = mo->method(mo->indexOfSignal(QMetaObject::normalizedSignature("sceneGrabbed(GammaRay::GrabbedFrame)")));
Expand Down Expand Up @@ -431,24 +433,25 @@ void QuickOverlay::windowAfterRendering()

m_grabbedFrame.transform.reset();

if (m_grabMode == QuickInspectorInterface::FullWindow) {
#ifdef ENABLE_GL_READPIXELS
if (m_grabbedFrame.image.size() != QSize(w, h))
m_grabbedFrame.image = QImage(w, h, QImage::Format_RGBA8888);
if (m_grabbedFrame.image.size() != QSize(w, h))
m_grabbedFrame.image = QImage(w, h, QImage::Format_RGBA8888);

QOpenGLFunctions *glFuncs = QOpenGLContext::currentContext()->functions();
glFuncs->glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, m_grabbedFrame.image.bits());
QOpenGLFunctions *glFuncs = QOpenGLContext::currentContext()->functions();
glFuncs->glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, m_grabbedFrame.image.bits());

// set transform to flip the read texture later, when displayed
m_grabbedFrame.transform.scale(1.0, -1.0);
m_grabbedFrame.transform.translate(intersect.x() * m_renderInfo.dpr , -intersect.y() * m_renderInfo.dpr - h);
// set transform to flip the read texture later, when displayed
m_grabbedFrame.transform.scale(1.0, -1.0);
m_grabbedFrame.transform.translate(intersect.x() * m_renderInfo.dpr , -intersect.y() * m_renderInfo.dpr - h);
#else
m_grabbedFrame.image = qt_gl_read_framebuffer(m_renderInfo.windowSize * m_renderInfo.dpr, false, QOpenGLContext::currentContext());
m_grabbedFrame.image = qt_gl_read_framebuffer(m_renderInfo.windowSize * m_renderInfo.dpr, false, QOpenGLContext::currentContext());
#endif
m_grabbedFrame.image.setDevicePixelRatio(m_renderInfo.dpr);

m_grabbedFrame.image.setDevicePixelRatio(m_renderInfo.dpr);

if (!m_grabbedFrame.image.isNull()) {
m_sceneGrabbed.invoke(this, Qt::QueuedConnection, Q_ARG(GammaRay::GrabbedFrame, m_grabbedFrame));
if (!m_grabbedFrame.image.isNull()) {
m_sceneGrabbed.invoke(this, Qt::QueuedConnection, Q_ARG(GammaRay::GrabbedFrame, m_grabbedFrame));
}
}
}

Expand Down Expand Up @@ -547,6 +550,28 @@ void QuickOverlay::updateOverlay()

m_window->update();
}

if (m_isGrabbingMode && m_currentItem.item() && m_grabMode == QuickInspectorInterface::Item) {
QSharedPointer<QQuickItemGrabResult> reply = m_currentItem.item()->grabToImage();

// The lambda needs to be 'mutable' in order to call reply.clear(), otherwise the
// QQuickItemGrabResult will never be deleted.
connect(reply.data(), &QQuickItemGrabResult::ready, this, [this, reply]() mutable {
m_isGrabbingMode = false;

// We don't set the pixel ratio here, as the image is not scaled up
m_grabbedFrame.transform.reset();
m_grabbedFrame.itemsGeometry.clear();
m_grabbedFrame.image = reply->image();
m_grabbedFrame.itemsGeometryRect = reply->image().rect();
m_grabbedFrame.itemsGeometry << initFromItem(m_currentItem.item());

m_sceneGrabbed.invoke(this, Qt::QueuedConnection, Q_ARG(GammaRay::GrabbedFrame, m_grabbedFrame));
reply.clear();
}, Qt::QueuedConnection);
} else if (m_currentToplevelItem) {
m_currentToplevelItem->window()->update();
}
}

void QuickOverlay::itemParentChanged(QQuickItem *parent)
Expand Down Expand Up @@ -622,3 +647,12 @@ void QuickOverlay::requestGrabWindow(const QRectF &userViewport)
m_userViewport = userViewport;
m_setIsGrabbingMode.invoke(this, Qt::QueuedConnection, Q_ARG(bool, true));
}

void QuickOverlay::setGrabMode(QuickInspectorInterface::GrabMode mode)
{
if (m_grabMode == mode)
return;

m_grabMode = mode;
updateOverlay();
}
5 changes: 5 additions & 0 deletions plugins/quickinspector/quickoverlay.h
Expand Up @@ -34,6 +34,8 @@
#include <QQuickItem>

#include "quickdecorationsdrawer.h"
#include "quickitemgeometry.h"
#include "quickinspectorinterface.h"

QT_BEGIN_NAMESPACE
class QQuickWindow;
Expand Down Expand Up @@ -103,6 +105,8 @@ class QuickOverlay : public QObject

void requestGrabWindow(const QRectF &userViewport);

void setGrabMode(QuickInspectorInterface::GrabMode grabMode);

signals:
void sceneChanged();
void sceneGrabbed(const GammaRay::GrabbedFrame &frame);
Expand Down Expand Up @@ -150,6 +154,7 @@ class QuickOverlay : public QObject
QSize windowSize;
GraphicsApi graphicsApi;
} m_renderInfo;
QuickInspectorInterface::GrabMode m_grabMode;

private slots:
void setIsGrabbingMode(bool isGrabbingMode);
Expand Down
12 changes: 12 additions & 0 deletions plugins/quickinspector/quickscenecontrolwidget.cpp
Expand Up @@ -63,6 +63,7 @@ QuickSceneControlWidget::QuickSceneControlWidget(QuickInspectorInterface *inspec
, m_gridSettingsWidget(new GridSettingsWidget) // Owned by the QWidgetAction
, m_legendTool(new QuickOverlayLegend(this))
, m_inspectorInterface(inspector)
, m_grabMode(QuickInspectorInterface::FullWindow)
{
m_layout = new QVBoxLayout(this);
m_layout->setContentsMargins(QMargins());
Expand Down Expand Up @@ -172,6 +173,17 @@ QuickSceneControlWidget::QuickSceneControlWidget(QuickInspectorInterface *inspec

m_toolBar->addSeparator();

//TODO add icon
auto previewItem = new QAction(QIcon(""), tr("Preview only the selected item"), this);
previewItem->setCheckable(true);
connect(previewItem, &QAction::toggled, this, [this](bool checked) {
m_grabMode = checked ? QuickInspectorInterface::Item : QuickInspectorInterface::FullWindow;
m_inspectorInterface->setGrabMode(m_grabMode);
});
m_toolBar->addAction(previewItem);

m_toolBar->addSeparator();

foreach (auto action, m_previewWidget->interactionModeActions()->actions()) {
m_toolBar->addAction(action);
}
Expand Down
3 changes: 3 additions & 0 deletions plugins/quickinspector/quickscenecontrolwidget.h
Expand Up @@ -71,6 +71,8 @@ class QuickSceneControlWidget : public QWidget

QuickScenePreviewWidget *previewWidget();

QuickInspectorInterface::GrabMode grabMode() const { return m_grabMode; }

signals:
void stateChanged();

Expand Down Expand Up @@ -103,6 +105,7 @@ private Q_SLOTS:
QuickOverlayLegend *m_legendTool;

QuickInspectorInterface *m_inspectorInterface;
QuickInspectorInterface::GrabMode m_grabMode;
};
} // namespace GammaRay

Expand Down
5 changes: 5 additions & 0 deletions plugins/quickinspector/quickscenepreviewwidget.cpp
Expand Up @@ -39,6 +39,7 @@ static const qint32 QuickScenePreviewWidgetStateVersion = 4;

QT_BEGIN_NAMESPACE
GAMMARAY_ENUM_STREAM_OPERATORS(GammaRay::QuickInspectorInterface::RenderMode)
GAMMARAY_ENUM_STREAM_OPERATORS(GammaRay::QuickInspectorInterface::GrabMode)
QT_END_NAMESPACE

QuickScenePreviewWidget::QuickScenePreviewWidget(QuickInspectorInterface *inspector,
Expand Down Expand Up @@ -177,6 +178,10 @@ void QuickScenePreviewWidget::resizeEvent(QResizeEvent *e)

void QuickScenePreviewWidget::drawDecoration(QPainter *p)
{
if (m_control->grabMode() == QuickInspectorInterface::Item) {
return;
}

// Scaling and translations on QuickItemGeometry will be done on demand

if (frame().data().userType() == qMetaTypeId<QuickItemGeometry>()) {
Expand Down