Skip to content

Commit

Permalink
Add QtQuick Controls 2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
fieldOfView committed Mar 21, 2022
1 parent d5c800d commit 42d55b8
Show file tree
Hide file tree
Showing 14 changed files with 1,598 additions and 191 deletions.
19 changes: 14 additions & 5 deletions DiscoverOctoPrintAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from UM.i18n import i18nCatalog
from UM.Logger import Logger
from UM.Version import Version
from UM.Settings.DefinitionContainer import DefinitionContainer
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from UM.Settings.ContainerRegistry import ContainerRegistry
Expand All @@ -12,7 +13,6 @@
from cura.Settings.CuraStackBuilder import CuraStackBuilder

from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QUrl, QObject, QTimer
from PyQt5.QtQml import QQmlComponent, QQmlContext
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtNetwork import (
QNetworkRequest,
Expand Down Expand Up @@ -45,13 +45,22 @@ def __init__(self, parent: QObject = None) -> None:
"DiscoverOctoPrintAction", catalog.i18nc("@action", "Connect OctoPrint")
)

self._qml_url = os.path.join("qml", "DiscoverOctoPrintAction.qml")

self._application = CuraApplication.getInstance()
self._network_plugin = None # type: Optional[OctoPrintOutputDevicePlugin]

# QNetwork manager needs to be created in advance. If we don't it can happen that it doesn't correctly
# hook itself into the event loop, which results in events never being fired / done.
try:
use_controls1 = False
if self._application.getAPIVersion() < Version(8) and self._application.getVersion() != "master":
use_controls1 = True
except AttributeError:
# UM.Application.getAPIVersion was added for API > 6 (Cura 4)
use_controls1 = True
qml_folder = "qml" if not use_controls1 else "qml_controls1"

self._qml_url = os.path.join(qml_folder, "DiscoverOctoPrintAction.qml")

# QNetwork manager needs to be created in advance. If we don't it can happen that it doesn't correctly
# hook itself into the event loop, which results in events never being fired / done.
self._network_manager = QNetworkAccessManager()
self._network_manager.finished.connect(self._onRequestFinished)

Expand Down
34 changes: 22 additions & 12 deletions OctoPrintOutputDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ def __init__(
plugin_version = "Unknown"
Logger.logException("w", "Could not get version information for the plugin")

application = CuraApplication.getInstance()
self._user_agent = "%s/%s %s/%s" % (
CuraApplication.getInstance().getApplicationName(),
CuraApplication.getInstance().getVersion(),
application.getApplicationName(),
application.getVersion(),
"OctoPrintPlugin",
plugin_version,
) # NetworkedPrinterOutputDevice defines this as string, so we encode this later
Expand Down Expand Up @@ -182,23 +183,32 @@ def __init__(
basic_auth_password,
)

use_controls1 = False
try:
major_api_version = CuraApplication.getInstance().getAPIVersion().getMajor()
if application.getAPIVersion() < Version(8) and application.getVersion() != "master":
use_controls1 = True

major_api_version = application.getAPIVersion().getMajor()
except AttributeError:
# UM.Application.getAPIVersion was added for API > 6 (Cura 4)
# Since this plugin version is only compatible with Cura 3.5 and newer, it is safe to assume API 5
major_api_version = 5
use_controls1 = True

if major_api_version <= 5:
# In Cura 3.x, the monitor item only shows the camera stream
self._monitor_view_qml_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "qml", "MonitorItem3x.qml"
)
else:
qml_folder = "qml" if not use_controls1 else "qml_controls1"
if not use_controls1:
# In Cura 5.x, the monitor item can only contain QtQuick Controls 2 items
qml_file = "MonitorItem.qml"
elif major_api_version > 5:
# In Cura 4.x, the monitor item shows the camera stream as well as the monitor sidebar
self._monitor_view_qml_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "qml", "MonitorItem4x.qml"
)
qml_file = "MonitorItem4x.qml"
else:
# In Cura 3.x, the monitor item only shows the camera stream
qml_file = "MonitorItem3x.qml"

self._monitor_view_qml_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), qml_folder, qml_file
)

name = self._id
matches = re.search(r"^\"(.*)\"\._octoprint\._tcp.local$", name)
Expand Down
14 changes: 13 additions & 1 deletion UploadOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# OctoPrintPlugin is released under the terms of the AGPLv3 or higher.

from UM.Application import Application
from UM.Version import Version
from UM.Util import parseBool

from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, pyqtSlot
Expand All @@ -24,6 +25,17 @@ def __init__(self) -> None:
self._auto_select = False
self._auto_print = False

use_controls1 = False
try:
use_controls1 = False
if self._application.getAPIVersion() < Version(8) and self._application.getVersion() != "master":
use_controls1 = True
except AttributeError:
# UM.Application.getAPIVersion was added for API > 6 (Cura 4)
use_controls1 = True
self._qml_folder = "qml" if not use_controls1 else "qml_controls1"


def configure(self, global_container_stack, file_name) -> None:
self.setAutoPrint(
parseBool(
Expand All @@ -45,7 +57,7 @@ def setProceedCallback(self, callback: Callable) -> None:

def showOptionsDialog(self) -> None:
path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "qml", "UploadOptions.qml"
os.path.dirname(os.path.abspath(__file__)), self._qml_folder, "UploadOptions.qml"
)

self._settings_dialog = self._application.createQmlComponent(
Expand Down
Loading

0 comments on commit 42d55b8

Please sign in to comment.