From 3c3cb76bf88fedbdd54a2615da37240c84783abb Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 21 Apr 2018 00:40:31 +0200 Subject: [PATCH] fix some crashers --- libnymea-common/types/actiontypes.cpp | 1 + libnymea-common/types/eventtype.cpp | 4 + libnymea-common/types/eventtype.h | 2 +- libnymea-common/types/eventtypes.cpp | 1 + libnymea-common/types/interfaces.cpp | 4 +- libnymea-common/types/paramtypes.cpp | 1 + libnymea-common/types/state.cpp | 1 - mea/jsonrpc/jsonrpcclient.cpp | 12 +- mea/jsonrpc/jsonrpcclient.h | 8 +- mea/ui/customviews/GenericTypeLogView.qml | 2 +- mea/ui/customviews/MediaControllerView.qml | 132 +++++++-------------- 11 files changed, 69 insertions(+), 99 deletions(-) diff --git a/libnymea-common/types/actiontypes.cpp b/libnymea-common/types/actiontypes.cpp index e956ffe0..afd01f87 100644 --- a/libnymea-common/types/actiontypes.cpp +++ b/libnymea-common/types/actiontypes.cpp @@ -73,6 +73,7 @@ QVariant ActionTypes::data(const QModelIndex &index, int role) const void ActionTypes::addActionType(ActionType *actionType) { + actionType->setParent(this); beginInsertRows(QModelIndex(), m_actionTypes.count(), m_actionTypes.count()); //qDebug() << "ActionTypes: loaded actionType" << actionType->name(); m_actionTypes.append(actionType); diff --git a/libnymea-common/types/eventtype.cpp b/libnymea-common/types/eventtype.cpp index 3c7c9513..b4a3b77c 100644 --- a/libnymea-common/types/eventtype.cpp +++ b/libnymea-common/types/eventtype.cpp @@ -74,6 +74,10 @@ ParamTypes *EventType::paramTypes() const void EventType::setParamTypes(ParamTypes *paramTypes) { + if (m_paramTypes && m_paramTypes->parent() == this) { + m_paramTypes->deleteLater(); + } + paramTypes->setParent(this); m_paramTypes = paramTypes; } diff --git a/libnymea-common/types/eventtype.h b/libnymea-common/types/eventtype.h index ec8b445b..9979b8cc 100644 --- a/libnymea-common/types/eventtype.h +++ b/libnymea-common/types/eventtype.h @@ -60,7 +60,7 @@ private: QString m_name; QString m_displayName; int m_index; - ParamTypes *m_paramTypes; + ParamTypes *m_paramTypes = nullptr; }; #endif // EVENTTYPE_H diff --git a/libnymea-common/types/eventtypes.cpp b/libnymea-common/types/eventtypes.cpp index 66970d58..7fbb8945 100644 --- a/libnymea-common/types/eventtypes.cpp +++ b/libnymea-common/types/eventtypes.cpp @@ -71,6 +71,7 @@ QVariant EventTypes::data(const QModelIndex &index, int role) const void EventTypes::addEventType(EventType *eventType) { + eventType->setParent(this); beginInsertRows(QModelIndex(), m_eventTypes.count(), m_eventTypes.count()); //qDebug() << "EventTypes: loaded eventType" << eventType->name(); m_eventTypes.append(eventType); diff --git a/libnymea-common/types/interfaces.cpp b/libnymea-common/types/interfaces.cpp index 2e170966..aef768a3 100644 --- a/libnymea-common/types/interfaces.cpp +++ b/libnymea-common/types/interfaces.cpp @@ -15,7 +15,7 @@ Interfaces::Interfaces(QObject *parent) : QAbstractListModel(parent) ParamType* pt = nullptr; ParamTypes *pts = nullptr; - iface = new Interface("battery", "Battery powered devices"); + iface = new Interface("battery", "Battery powered devices", this); et = new EventType(); pts = new ParamTypes(et); et->setParamTypes(pts); @@ -43,7 +43,7 @@ Interfaces::Interfaces(QObject *parent) : QAbstractListModel(parent) m_list.append(iface); - iface = new Interface("notification", "Notification services"); + iface = new Interface("notification", "Notification services", this); at = new ActionType(); pts = new ParamTypes(at); at->setParamTypes(pts); diff --git a/libnymea-common/types/paramtypes.cpp b/libnymea-common/types/paramtypes.cpp index cf499330..42b33b95 100644 --- a/libnymea-common/types/paramtypes.cpp +++ b/libnymea-common/types/paramtypes.cpp @@ -100,6 +100,7 @@ QVariant ParamTypes::data(const QModelIndex &index, int role) const void ParamTypes::addParamType(ParamType *paramType) { + paramType->setParent(this); beginInsertRows(QModelIndex(), m_paramTypes.count(), m_paramTypes.count()); //qDebug() << "ParamTypes: loaded paramType" << paramType->name(); m_paramTypes.append(paramType); diff --git a/libnymea-common/types/state.cpp b/libnymea-common/types/state.cpp index d5814507..6fa681f3 100644 --- a/libnymea-common/types/state.cpp +++ b/libnymea-common/types/state.cpp @@ -44,7 +44,6 @@ QUuid State::stateTypeId() const QVariant State::value() const { - qDebug() << "returning value:" << m_value; return m_value; } diff --git a/mea/jsonrpc/jsonrpcclient.cpp b/mea/jsonrpc/jsonrpcclient.cpp index db4b78f2..f1b1aba6 100644 --- a/mea/jsonrpc/jsonrpcclient.cpp +++ b/mea/jsonrpc/jsonrpcclient.cpp @@ -309,6 +309,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) int commandId = dataMap.value("id").toInt(); JsonRpcReply *reply = m_replies.take(commandId); if (reply) { + reply->deleteLater(); // qDebug() << QString("JsonRpc: got response for %1.%2: %3").arg(reply->nameSpace(), reply->method(), QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Indented))) << reply->callback() << reply->callback(); if (dataMap.value("status").toString() == "unauthorized") { @@ -322,7 +323,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) emit authenticationRequiredChanged(); } - if (reply->caller() != nullptr && !reply->callback().isEmpty()) { + if (!reply->caller().isNull() && !reply->callback().isEmpty()) { QMetaObject::invokeMethod(reply->caller(), reply->callback().toLatin1().data(), Q_ARG(QVariantMap, dataMap)); } @@ -346,8 +347,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) } } -JsonRpcReply::JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params, QObject *caller, const QString &callback): - QObject(caller), +JsonRpcReply::JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params, QPointer caller, const QString &callback): m_commandId(commandId), m_nameSpace(nameSpace), m_method(method), @@ -357,6 +357,10 @@ JsonRpcReply::JsonRpcReply(int commandId, QString nameSpace, QString method, QVa { } +JsonRpcReply::~JsonRpcReply() +{ +} + int JsonRpcReply::commandId() const { return m_commandId; @@ -388,7 +392,7 @@ QVariantMap JsonRpcReply::requestMap() return request; } -QObject* JsonRpcReply::caller() const +QPointer JsonRpcReply::caller() const { return m_caller; } diff --git a/mea/jsonrpc/jsonrpcclient.h b/mea/jsonrpc/jsonrpcclient.h index 06a994a5..f8ced86b 100644 --- a/mea/jsonrpc/jsonrpcclient.h +++ b/mea/jsonrpc/jsonrpcclient.h @@ -23,6 +23,7 @@ #include #include +#include #include "nymeaconnection.h" #include "jsonhandler.h" @@ -115,7 +116,8 @@ class JsonRpcReply : public QObject { Q_OBJECT public: - explicit JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params = QVariantMap(), QObject *caller = 0, const QString &callback = QString()); + explicit JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params = QVariantMap(), QPointer caller = QPointer(), const QString &callback = QString()); + ~JsonRpcReply(); int commandId() const; QString nameSpace() const; @@ -123,7 +125,7 @@ public: QVariantMap params() const; QVariantMap requestMap(); - QObject *caller() const; + QPointer caller() const; QString callback() const; private: @@ -132,7 +134,7 @@ private: QString m_method; QVariantMap m_params; - QObject *m_caller; + QPointer m_caller; QString m_callback; }; diff --git a/mea/ui/customviews/GenericTypeLogView.qml b/mea/ui/customviews/GenericTypeLogView.qml index b5006173..be291101 100644 --- a/mea/ui/customviews/GenericTypeLogView.qml +++ b/mea/ui/customviews/GenericTypeLogView.qml @@ -15,7 +15,7 @@ Item { signal addRuleClicked(var value) - readonly property var deviceClass: Engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId) + readonly property var deviceClass: device ? Engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId) : null LogsModel { id: logs diff --git a/mea/ui/customviews/MediaControllerView.qml b/mea/ui/customviews/MediaControllerView.qml index bd89f311..07e0fce3 100644 --- a/mea/ui/customviews/MediaControllerView.qml +++ b/mea/ui/customviews/MediaControllerView.qml @@ -15,106 +15,64 @@ CustomViewBase { } property var playbackState: device.states.getState(deviceClass.stateTypes.findByName("playbackStatus").id) + property var playbackStateValue: playbackState.value + onPlaybackStateValueChanged: populateControls() + Component.onCompleted: populateControls() + + function populateControls() { + print("generating controls") + controlsModel.clear(); + controlsModel.append({image: "../images/media-skip-backward.svg", action: "skipBack"}) + controlsModel.append({image: "../images/media-seek-backward.svg", action: "rewind"}) + controlsModel.append({image: "../images/media-playback-stop.svg", action: "stop"}) + if (playbackState.value === "PAUSED" || playbackState.value === "STOPPED") { + controlsModel.append({image: "../images/media-playback-start.svg", action: "play"}) + } + if (playbackState.value === "PLAYING") { + controlsModel.append({image: "../images/media-playback-pause.svg", action: "pause"}) + } + + controlsModel.append({image: "../images/media-seek-forward.svg", action: "fastForward"}) + controlsModel.append({image: "../images/media-skip-forward.svg", action: "skipNext"}) + } ColumnLayout { id: column anchors { left: parent.left; right: parent.right } - RowLayout { + Row { + id: controlsRow Layout.fillWidth: true - Item { Layout.fillWidth: true; height: 1 } + property int iconSize: Math.max(app.iconSize * 2, column.width / (controlsModel.count + 0)) - property int iconSize: Math.min(root.width / 6, app.iconSize * 2) +// Item { +// width: Math.max(app.iconSize * 2, column.width / (controlsModel.count + 2)) +// height: 1 +// } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-skip-backward.svg" + + Repeater { + model: ListModel { + id: controlsModel } - onClicked: { - executeAction("skipBack") - } - } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-seek-backward.svg" - } - onClicked: { - executeAction("rewind") - } - } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-playback-stop.svg" - } - onClicked: { - executeAction("stop") - } - } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-playback-start.svg" - } - visible: playbackState.value == "PAUSED" || playbackState.value == "STOPPED" - onClicked: { - executeAction("play") - } - } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-playback-pause.svg" - } - visible: playbackState.value == "PLAYING" - onClicked: { - executeAction("pause") + delegate: AbstractButton { + + height: app.iconSize * 2 + width: controlsRow.iconSize + ColorIcon { + height: parent.height + width: height + name: model.image + anchors.horizontalCenter: parent.horizontalCenter + } + onClicked: { + executeAction(model.action) + } } } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-seek-forward.svg" - } - onClicked: { - executeAction("fastForward") - } - } - AbstractButton { - Layout.fillWidth: true - height: Math.min(app.iconSize * 2) - ColorIcon { - height: parent.height - width: height - name: "../images/media-skip-forward.svg" - } - onClicked: { - executeAction("skipNext") - } - } - Item { Layout.fillWidth: true; height: 1 } +// Item { Layout.fillWidth: true; height: 1 } } }