Skip to content

Commit

Permalink
Added null pointer check for asQuickItem()
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuprynowicz committed Jul 17, 2023
1 parent 72137f5 commit 1e8d032
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions libraries/ui/src/QmlWindowClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,19 @@ void QmlWindowClass::qmlToScript(const QVariant& message) {

void QmlWindowClass::sendToQml(const QVariant& message) {
// Forward messages received from the script on to QML
QMetaObject::invokeMethod(asQuickItem(), "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message));
if (asQuickItem()) {
QMetaObject::invokeMethod(asQuickItem(), "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message));
} else {
qDebug() << "QmlWindowClass::sendToQml: asQuickItem() returned NULL";
}
}

void QmlWindowClass::clearDebugWindow() {
QMetaObject::invokeMethod(asQuickItem(), "clearDebugWindow", Qt::QueuedConnection);
if (asQuickItem()) {
QMetaObject::invokeMethod(asQuickItem(), "clearDebugWindow", Qt::QueuedConnection);
} else {
qDebug() << "QmlWindowClass::clearDebugWindow: asQuickItem() returned NULL";
}
}

void QmlWindowClass::emitScriptEvent(const QVariant& scriptMessage) {
Expand All @@ -192,9 +200,17 @@ void QmlWindowClass::emitWebEvent(const QVariant& webMessage) {
const QString LOWER_KEYBOARD = "_LOWER_KEYBOARD";
QString messageString = webMessage.type() == QVariant::String ? webMessage.toString() : "";
if (messageString.left(RAISE_KEYBOARD.length()) == RAISE_KEYBOARD) {
setKeyboardRaised(asQuickItem(), true, messageString == RAISE_KEYBOARD_NUMERIC);
if (asQuickItem()) {
setKeyboardRaised(asQuickItem(), true, messageString == RAISE_KEYBOARD_NUMERIC);
} else {
qDebug() << "QmlWindowClass::emitWebEvent: asQuickItem() returned NULL";
}
} else if (messageString == LOWER_KEYBOARD) {
setKeyboardRaised(asQuickItem(), false);
if (asQuickItem()) {
setKeyboardRaised(asQuickItem(), false);
} else {
qDebug() << "QmlWindowClass::emitWebEvent: asQuickItem() returned NULL";
}
} else {
emit webEventReceived(webMessage);
}
Expand Down Expand Up @@ -234,7 +250,12 @@ void QmlWindowClass::setVisible(bool visible) {
}

QQuickItem* targetWindow = asQuickItem();
targetWindow->setProperty(OFFSCREEN_VISIBILITY_PROPERTY, visible);
//TODO: all asQuickItem() need to be guarded like this
if (targetWindow) {
targetWindow->setProperty(OFFSCREEN_VISIBILITY_PROPERTY, visible);
} else {
qDebug() << "QmlWindowClass::setVisible: asQuickItem() returned NULL";
}
}

bool QmlWindowClass::isVisible() {
Expand All @@ -249,7 +270,11 @@ bool QmlWindowClass::isVisible() {
return false;
}

return asQuickItem()->isVisible();
if (asQuickItem()) {
return asQuickItem()->isVisible();
} else {
qDebug() << "QmlWindowClass::isVisible: asQuickItem() returned NULL";
}
}

glm::vec2 QmlWindowClass::getPosition() {
Expand All @@ -263,17 +288,21 @@ glm::vec2 QmlWindowClass::getPosition() {
return {};
}

return toGlm(asQuickItem()->position());
if (asQuickItem()) {
return toGlm(asQuickItem()->position());
} else {
qDebug() << "QmlWindowClass::getPosition: asQuickItem() returned NULL";
return glm::vec2(0.0f, 0.0f);
}
}


void QmlWindowClass::setPosition(const glm::vec2& position) {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "setPosition", Q_ARG(const glm::vec2&, position));
return;
}

if (!_qmlWindow.isNull()) {
if (asQuickItem()) {
asQuickItem()->setPosition(QPointF(position.x, position.y));
}
}
Expand All @@ -298,7 +327,12 @@ glm::vec2 QmlWindowClass::getSize() {
return {};
}
QQuickItem* targetWindow = asQuickItem();
return vec2(targetWindow->width(), targetWindow->height());
if (targetWindow) {
return vec2(targetWindow->width(), targetWindow->height());
} else {
qDebug() << "QmlWindowClass::getSize: asQuickItem() returned NULL";
return vec2(0.0f, 0.0f);
}
}

void QmlWindowClass::setSize(const glm::vec2& size) {
Expand All @@ -307,7 +341,7 @@ void QmlWindowClass::setSize(const glm::vec2& size) {
return;
}

if (!_qmlWindow.isNull()) {
if (asQuickItem()) {
asQuickItem()->setSize(QSizeF(size.x, size.y));
}
}
Expand All @@ -322,7 +356,7 @@ void QmlWindowClass::setTitle(const QString& title) {
return;
}

if (!_qmlWindow.isNull()) {
if (asQuickItem()) {
asQuickItem()->setProperty(TITLE_PROPERTY, title);
}
}
Expand Down Expand Up @@ -353,7 +387,7 @@ void QmlWindowClass::raise() {
return;
}

if (_qmlWindow) {
if (asQuickItem()) {
QMetaObject::invokeMethod(asQuickItem(), "raise", Qt::DirectConnection);
}
}

0 comments on commit 1e8d032

Please sign in to comment.