From f3167bb84b6ffefe619f6521a5932929f2677eff Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 20 Mar 2017 15:35:22 +0100 Subject: [PATCH] Add dialog for opening a project file CURA-3495 --- cura/CuraApplication.py | 4 +- .../qml/AskOpenAsProjectOrModelsDialog.qml | 125 ++++++++++++++++++ resources/qml/Cura.qml | 24 +++- resources/qml/Preferences/GeneralPage.qml | 65 +++++++++ 4 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 resources/qml/AskOpenAsProjectOrModelsDialog.qml diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 3e6f4c0ae3..c5f17c071a 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -4,6 +4,7 @@ from PyQt5.QtNetwork import QLocalServer from PyQt5.QtNetwork import QLocalSocket from UM.Qt.QtApplication import QtApplication +from UM.FileHandler.ReadFileJob import ReadFileJob from UM.Scene.SceneNode import SceneNode from UM.Scene.Camera import Camera from UM.Math.Vector import Vector @@ -247,6 +248,7 @@ class CuraApplication(QtApplication): Preferences.getInstance().addPreference("cura/dialog_on_project_save", True) Preferences.getInstance().addPreference("cura/asked_dialog_on_project_save", False) Preferences.getInstance().addPreference("cura/choice_on_profile_override", "always_ask") + Preferences.getInstance().addPreference("cura/choice_on_open_project", "always_ask") Preferences.getInstance().addPreference("cura/currency", "€") Preferences.getInstance().addPreference("cura/material_settings", "{}") @@ -1122,7 +1124,7 @@ class CuraApplication(QtApplication): fileLoaded = pyqtSignal(str) def _onJobFinished(self, job): - if type(job) is not ReadMeshJob or not job.getResult(): + if (not isinstance(job, ReadMeshJob) and not isinstance(job, ReadFileJob)) or not job.getResult(): return f = QUrl.fromLocalFile(job.getFileName()) diff --git a/resources/qml/AskOpenAsProjectOrModelsDialog.qml b/resources/qml/AskOpenAsProjectOrModelsDialog.qml new file mode 100644 index 0000000000..1e04c0f5a9 --- /dev/null +++ b/resources/qml/AskOpenAsProjectOrModelsDialog.qml @@ -0,0 +1,125 @@ +// Copyright (c) 2015 Ultimaker B.V. +// Cura is released under the terms of the AGPLv3 or higher. + +import QtQuick 2.2 +import QtQuick.Controls 1.1 +import QtQuick.Controls.Styles 1.1 +import QtQuick.Layouts 1.1 +import QtQuick.Dialogs 1.1 + +import UM 1.3 as UM +import Cura 1.0 as Cura + + +UM.Dialog +{ + // This dialog asks the user whether he/she wants to open a project file as a project or import models. + id: base + + title: catalog.i18nc("@title:window", "Open project file") + width: 420 + height: 140 + + maximumHeight: height + maximumWidth: width + minimumHeight: height + minimumWidth: width + + modality: UM.Application.platform == "linux" ? Qt.NonModal : Qt.WindowModal; + + property var fileUrl + + function loadProjectFile(projectFile) + { + UM.WorkspaceFileHandler.readLocalFile(projectFile); + + var meshName = backgroundItem.getMeshName(projectFile.toString()); + backgroundItem.hasMesh(decodeURIComponent(meshName)); + } + + function loadModelFiles(fileUrls) + { + for (var i in fileUrls) + Printer.readLocalFile(fileUrls[i]); + + var meshName = backgroundItem.getMeshName(fileUrls[0].toString()); + backgroundItem.hasMesh(decodeURIComponent(meshName)); + } + + onVisibleChanged: + { + if (visible) + { + var rememberMyChoice = UM.Preferences.getValue("cura/choice_on_open_project") != "always_ask"; + rememberChoiceCheckBox.checked = rememberMyChoice; + } + } + + Column + { + anchors.fill: parent + anchors.margins: UM.Theme.getSize("default_margin").width + anchors.left: parent.left + anchors.right: parent.right + spacing: UM.Theme.getSize("default_margin").width + + Label + { + text: catalog.i18nc("@text:window", "This is a Cura project file. Would you like to open it as a project\nor import the models from it?") + anchors.margins: UM.Theme.getSize("default_margin").width + wrapMode: Text.WordWrap + } + + CheckBox + { + id: rememberChoiceCheckBox + text: catalog.i18nc("@text:window", "Remember my choice") + anchors.margins: UM.Theme.getSize("default_margin").width + checked: UM.Preferences.getValue("cura/choice_on_open_project") != "always_ask" + } + + // Buttons + Item + { + anchors.right: parent.right + anchors.left: parent.left + height: childrenRect.height + + Button + { + id: openAsProjectButton + text: catalog.i18nc("@action:button", "Open as project"); + anchors.right: importModelsButton.left + anchors.rightMargin: UM.Theme.getSize("default_margin").width + isDefault: true + onClicked: + { + // update preference + if (rememberChoiceCheckBox.checked) + UM.Preferences.setValue("cura/choice_on_open_project", "open_as_project"); + + // load this file as project + base.hide(); + loadProjectFile(base.fileUrl); + } + } + + Button + { + id: importModelsButton + text: catalog.i18nc("@action:button", "Import models"); + anchors.right: parent.right + onClicked: + { + // update preference + if (rememberChoiceCheckBox.checked) + UM.Preferences.setValue("cura/choice_on_open_project", "open_as_model"); + + // load models from this project file + base.hide(); + loadModelFiles([base.fileUrl]); + } + } + } + } +} diff --git a/resources/qml/Cura.qml b/resources/qml/Cura.qml index 469c563064..3e5ecf03fe 100755 --- a/resources/qml/Cura.qml +++ b/resources/qml/Cura.qml @@ -773,9 +773,26 @@ UM.MainWindow } if (hasProjectFile) - openFilesIncludingProjectsDialog.loadProjectFile(projectFileUrlList[0]); + { + var projectFile = projectFileUrlList[0]; + + // check preference + var choice = UM.Preferences.getValue("cura/choice_on_open_project"); + if (choice == "open_as_project") + openFilesIncludingProjectsDialog.loadProjectFile(projectFile); + else if (choice == "open_as_model") + openFilesIncludingProjectsDialog.loadModelFiles([projectFile]); + else // always ask + { + // ask whether to open as project or as models + askOpenAsProjectOrModelsDialog.fileUrl = projectFile; + askOpenAsProjectOrModelsDialog.show(); + } + } else + { openFilesIncludingProjectsDialog.loadModelFiles(fileUrls); + } } } @@ -790,6 +807,11 @@ UM.MainWindow id: openFilesIncludingProjectsDialog } + AskOpenAsProjectOrModelsDialog + { + id: askOpenAsProjectOrModelsDialog + } + EngineLog { id: engineLog; diff --git a/resources/qml/Preferences/GeneralPage.qml b/resources/qml/Preferences/GeneralPage.qml index 18b3665290..a1d5098d14 100755 --- a/resources/qml/Preferences/GeneralPage.qml +++ b/resources/qml/Preferences/GeneralPage.qml @@ -37,6 +37,18 @@ UM.PreferencesPage } } + function setDefaultOpenProjectOption(code) + { + for (var i = 0; i < choiceOnOpenProjectDropDownButton.model.count; ++i) + { + if (choiceOnOpenProjectDropDownButton.model.get(i).code == code) + { + choiceOnOpenProjectDropDownButton.currentIndex = i + break; + } + } + } + function reset() { UM.Preferences.resetPreference("general/language") @@ -65,6 +77,9 @@ UM.PreferencesPage UM.Preferences.resetPreference("cura/choice_on_profile_override") setDefaultDiscardOrKeepProfile(UM.Preferences.getValue("cura/choice_on_profile_override")) + UM.Preferences.resetPreference("cura/choice_on_open_project") + setDefaultOpenProjectOption(UM.Preferences.getValue("cura/choice_on_open_project")) + if (plugins.find("id", "SliceInfoPlugin") > -1) { UM.Preferences.resetPreference("info/send_slice_info") sendDataCheckbox.checked = boolCheck(UM.Preferences.getValue("info/send_slice_info")) @@ -389,6 +404,56 @@ UM.PreferencesPage } } + UM.TooltipArea { + width: childrenRect.width + height: childrenRect.height + text: catalog.i18nc("@info:tooltip", "Default behavior when opening a project file") + + Column + { + spacing: 4 + + Label + { + text: catalog.i18nc("@window:text", "Default behavior when opening a project file: ") + } + + ComboBox + { + id: choiceOnOpenProjectDropDownButton + width: 200 + + model: ListModel + { + id: openProjectOptionModel + + Component.onCompleted: { + append({ text: catalog.i18nc("@option:openProject", "Always ask"), code: "always_ask" }) + append({ text: catalog.i18nc("@option:openProject", "Always open as a project"), code: "open_as_project" }) + append({ text: catalog.i18nc("@option:openProject", "Always import models"), code: "open_as_model" }) + } + } + + currentIndex: + { + var index = 0; + var currentChoice = UM.Preferences.getValue("cura/choice_on_open_project"); + for (var i = 0; i < model.count; ++i) + { + if (model.get(i).code == currentChoice) + { + index = i; + break; + } + } + return index; + } + + onActivated: UM.Preferences.setValue("cura/choice_on_open_project", model.get(index).code) + } + } + } + Item { //: Spacer