Skip to content

Instantly share code, notes, and snippets.

@torarnv
Created January 23, 2023 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torarnv/eed751d8f68b7e2caa3bf42d40db6282 to your computer and use it in GitHub Desktop.
Save torarnv/eed751d8f68b7e2caa3bf42d40db6282 to your computer and use it in GitHub Desktop.
JUCE QtComponentPeer
class QtComponentPeer : public QObject, public ComponentPeer
{
Q_OBJECT
public:
QtComponentPeer(juce::Component& c, int f, void* viewToAttachTo)
: QObject(), ComponentPeer(c, f)
{
// FIXME: Set up m_window and reparent into QWindow::fromWinId(viewToAttachTo)
m_window->installEventFilter(this);
}
~QtComponentPeer() override
{
}
void* getNativeHandle() const override
{
return reinterpret_cast<void*>(m_window->winId());
}
void setVisible(bool shouldBeVisible) override
{
m_window->setVisible(shouldBeVisible);
}
void setTitle(const String& title) override
{
m_window->setTitle(QString::fromStdString(title.toStdString()));
}
void setBounds(const Rectangle<int>& r, bool isNowFullScreen) override
{
Q_UNUSED(isNowFullScreen);
QRect geometry(r.getX(), r.getY(), r.getWidth(), r.getHeight());
m_window->setGeometry(geometry);
}
bool eventFilter(QObject *watched, QEvent *event) override
{
Q_ASSERT(watched == m_window);
switch (event->type()) {
case QEvent::Resize:
case QEvent::Move:
handleMovedOrResized();
break;
default:
break;
}
return false;
}
Rectangle<int> getBounds() const override
{
auto r = m_window->geometry();
return { r.x(), r.y(), r.width(), r.height() };
}
juce::Point<float> localToGlobal(juce::Point<float> relativePosition) override
{
QPointF local(relativePosition.getX(), relativePosition.getY());
QPointF global = m_window->mapToGlobal(local);
return { static_cast<float>(global.x()), static_cast<float>(global.y()) };
}
juce::Point<float> globalToLocal(juce::Point<float> screenPosition) override
{
QPointF global(screenPosition.getX(), screenPosition.getY());
QPointF local = m_window->mapFromGlobal(global);
return { static_cast<float>(local.x()), static_cast<float>(local.y()) };
}
void setMinimised(bool shouldBeMinimised) override
{
auto windowStates = m_window->windowStates();
windowStates.setFlag(Qt::WindowMinimized, shouldBeMinimised);
m_window->setWindowStates(windowStates);
}
bool isMinimised() const override
{
return m_window->windowStates().testFlag(Qt::WindowMinimized);
}
void setFullScreen(bool shouldBeFullScreen) override
{
auto windowStates = m_window->windowStates();
windowStates.setFlag(Qt::WindowFullScreen, shouldBeFullScreen);
m_window->setWindowStates(windowStates);
}
bool isFullScreen() const override
{
return m_window->windowStates().testFlag(Qt::WindowFullScreen);
}
bool contains(juce::Point<int> localPos, bool trueIfInAChildWindow) const override
{
Q_UNUSED(trueIfInAChildWindow);
QPointF pos(localPos.getX(), localPos.getY());
QRectF bounds(QPoint(), m_window->size());
return bounds.contains(pos);
}
BorderSize<int> getFrameSize() const override
{
auto margins = m_window->frameMargins();
return { margins.top(), margins.left(), margins.bottom(), margins.right() };
}
OptionalBorderSize getFrameSizeIfPresent() const override
{
return OptionalBorderSize{getFrameSize()};
}
void toFront(bool takeKeyboardFocus) override
{
m_window->raise();
if (takeKeyboardFocus)
m_window->requestActivate();
}
bool isFocused() const override
{
return m_window->isActive();
}
void grabFocus() override
{
m_window->requestActivate();
}
void repaint(const Rectangle<int>& area) override
{
Q_UNUSED(area;)
m_window->requestUpdate();
}
void performAnyPendingRepaintsNow() override
{
QEvent request(QEvent::UpdateRequest);
QCoreApplication::sendEvent(m_window, &request);
}
void setAlpha(float newAlpha) override
{
m_window->setOpacity(newAlpha);
}
// Unimplemented
void toBehind(ComponentPeer*) override { assert(false); }
bool setAlwaysOnTop(bool) override { assert(false); return {}; }
void setIcon(const Image&) override { assert(false); }
StringArray getAvailableRenderingEngines() override { assert(false); return {}; }
void textInputRequired(juce::Point<int>, TextInputTarget&) override { assert(false); }
private:
QWindow *m_window = nullptr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment