From 081b2a28fe6a3b6d8136ec6e8bc67745262e9ede Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Fri, 21 Sep 2018 17:23:30 +0200 Subject: [PATCH] Expose Account API to QML This is done by adding the API as an SingletonType to Cura. CURA-5744 --- cura/API/Account.py | 18 +++++++++++++++++- cura/API/__init__.py | 10 ++++++++-- cura/CuraApplication.py | 10 ++++++++++ resources/qml/Cura.qml | 3 +-- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/cura/API/Account.py b/cura/API/Account.py index 377464f438..7ccd995be3 100644 --- a/cura/API/Account.py +++ b/cura/API/Account.py @@ -2,7 +2,7 @@ # Cura is released under the terms of the LGPLv3 or higher. from typing import Tuple, Optional, Dict -from PyQt5.QtCore.QObject import QObject, pyqtSignal, pyqtSlot, pyqtProperty +from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty from UM.Message import Message from cura.OAuth2.AuthorizationService import AuthorizationService @@ -66,12 +66,27 @@ class Account(QObject): self._logged_in = logged_in self.loginStateChanged.emit() + @pyqtSlot() def login(self) -> None: if self._logged_in: # Nothing to do, user already logged in. return self._authorization_service.startAuthorizationFlow() + @pyqtProperty(str, notify=loginStateChanged) + def userName(self): + user_profile = self._authorization_service.getUserProfile() + if not user_profile: + return None + return user_profile.username + + @pyqtProperty(str, notify = loginStateChanged) + def profileImageUrl(self): + user_profile = self._authorization_service.getUserProfile() + if not user_profile: + return None + return user_profile.profile_image_url + # Get the profile of the logged in user # @returns None if no user is logged in, a dict containing user_id, username and profile_image_url @pyqtProperty("QVariantMap", notify = loginStateChanged) @@ -81,6 +96,7 @@ class Account(QObject): return None return user_profile.__dict__ + @pyqtSlot() def logout(self) -> None: if not self._logged_in: return # Nothing to do, user isn't logged in. diff --git a/cura/API/__init__.py b/cura/API/__init__.py index d6d9092219..54f5c1f8b0 100644 --- a/cura/API/__init__.py +++ b/cura/API/__init__.py @@ -1,5 +1,7 @@ # Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. +from PyQt5.QtCore import QObject, pyqtProperty + from UM.PluginRegistry import PluginRegistry from cura.API.Backups import Backups from cura.API.Interface import Interface @@ -12,7 +14,7 @@ from cura.API.Account import Account # this API provides a version-safe interface with proper deprecation warnings # etc. Usage of any other methods than the ones provided in this API can cause # plug-ins to be unstable. -class CuraAPI: +class CuraAPI(QObject): # For now we use the same API version to be consistent. VERSION = PluginRegistry.APIVersion @@ -23,4 +25,8 @@ class CuraAPI: # Interface API interface = Interface() - account = Account() + _account = Account() + + @pyqtProperty(QObject, constant = True) + def account(self) -> Account: + return CuraAPI._account diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index a94814502e..cd0cfb95d6 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -204,6 +204,7 @@ class CuraApplication(QtApplication): self._quality_profile_drop_down_menu_model = None self._custom_quality_profile_drop_down_menu_model = None + self._cura_API = None self._physics = None self._volume = None @@ -894,6 +895,12 @@ class CuraApplication(QtApplication): self._custom_quality_profile_drop_down_menu_model = CustomQualityProfilesDropDownMenuModel(self) return self._custom_quality_profile_drop_down_menu_model + def getCuraAPI(self, *args, **kwargs): + if self._cura_API is None: + from cura.API import CuraAPI + self._cura_API = CuraAPI() + return self._cura_API + ## Registers objects for the QML engine to use. # # \param engine The QML engine. @@ -942,6 +949,9 @@ class CuraApplication(QtApplication): qmlRegisterSingletonType(ContainerManager, "Cura", 1, 0, "ContainerManager", ContainerManager.getInstance) qmlRegisterType(SidebarCustomMenuItemsModel, "Cura", 1, 0, "SidebarCustomMenuItemsModel") + from cura.API import CuraAPI + qmlRegisterSingletonType(CuraAPI, "Cura", 1, 1, "API", self.getCuraAPI) + # As of Qt5.7, it is necessary to get rid of any ".." in the path for the singleton to work. actions_url = QUrl.fromLocalFile(os.path.abspath(Resources.getPath(CuraApplication.ResourceTypes.QmlFiles, "Actions.qml"))) qmlRegisterSingletonType(actions_url, "Cura", 1, 0, "Actions") diff --git a/resources/qml/Cura.qml b/resources/qml/Cura.qml index 07154a0729..b3367471ad 100644 --- a/resources/qml/Cura.qml +++ b/resources/qml/Cura.qml @@ -8,7 +8,7 @@ import QtQuick.Layouts 1.1 import QtQuick.Dialogs 1.2 import UM 1.3 as UM -import Cura 1.0 as Cura +import Cura 1.1 as Cura import "Menus" @@ -21,7 +21,6 @@ UM.MainWindow property bool showPrintMonitor: false backgroundColor: UM.Theme.getColor("viewport_background") - // This connection is here to support legacy printer output devices that use the showPrintMonitor signal on Application to switch to the monitor stage // It should be phased out in newer plugin versions. Connections