Expose Account API to QML

This is done by adding the API as an SingletonType to Cura.

CURA-5744
This commit is contained in:
Jaime van Kessel 2018-09-21 17:23:30 +02:00
parent b54383e685
commit 081b2a28fe
4 changed files with 36 additions and 5 deletions

View File

@ -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.

View File

@ -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

View File

@ -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")

View File

@ -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