Merge pull request #10977 from Ultimaker/CURA-8696_Show_whats_new_after_beta

CURA-8696 Show whats new after beta
This commit is contained in:
Evangelos Trantos 2021-12-01 20:40:04 +01:00 committed by GitHub
commit 95f0b692a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 61 deletions

View File

@ -1,7 +1,9 @@
# Copyright (c) 2019 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from collections import deque
import os import os
from collections import deque
from typing import TYPE_CHECKING, Optional, List, Dict, Any from typing import TYPE_CHECKING, Optional, List, Dict, Any
from PyQt5.QtCore import QUrl, Qt, pyqtSlot, pyqtProperty, pyqtSignal from PyQt5.QtCore import QUrl, Qt, pyqtSlot, pyqtProperty, pyqtSignal
@ -16,24 +18,23 @@ if TYPE_CHECKING:
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
#
# This is the Qt ListModel that contains all welcome pages data. Each page is a page that can be shown as a step in the
# welcome wizard dialog. Each item in this ListModel represents a page, which contains the following fields:
#
# - id : A unique page_id which can be used in function goToPage(page_id)
# - page_url : The QUrl to the QML file that contains the content of this page
# - next_page_id : (OPTIONAL) The next page ID to go to when this page finished. This is optional. If this is not
# provided, it will go to the page with the current index + 1
# - next_page_button_text: (OPTIONAL) The text to show for the "next" button, by default it's the translated text of
# "Next". Note that each step QML can decide whether to use this text or not, so it's not
# mandatory.
# - should_show_function : (OPTIONAL) An optional function that returns True/False indicating if this page should be
# shown. By default all pages should be shown. If a function returns False, that page will
# be skipped and its next page will be shown.
#
# Note that in any case, a page that has its "should_show_function" == False will ALWAYS be skipped.
#
class WelcomePagesModel(ListModel): class WelcomePagesModel(ListModel):
"""
This is the Qt ListModel that contains all welcome pages data. Each page is a page that can be shown as a step in
the welcome wizard dialog. Each item in this ListModel represents a page, which contains the following fields:
- id : A unique page_id which can be used in function goToPage(page_id)
- page_url : The QUrl to the QML file that contains the content of this page
- next_page_id : (OPTIONAL) The next page ID to go to when this page finished. This is optional. If this is
not provided, it will go to the page with the current index + 1
- next_page_button_text : (OPTIONAL) The text to show for the "next" button, by default it's the translated text of
"Next". Note that each step QML can decide whether to use this text or not, so it's not
mandatory.
- should_show_function : (OPTIONAL) An optional function that returns True/False indicating if this page should be
shown. By default all pages should be shown. If a function returns False, that page will
be skipped and its next page will be shown.
Note that in any case, a page that has its "should_show_function" == False will ALWAYS be skipped.
"""
IdRole = Qt.UserRole + 1 # Page ID IdRole = Qt.UserRole + 1 # Page ID
PageUrlRole = Qt.UserRole + 2 # URL to the page's QML file PageUrlRole = Qt.UserRole + 2 # URL to the page's QML file
@ -55,11 +56,11 @@ class WelcomePagesModel(ListModel):
self._default_next_button_text = self._catalog.i18nc("@action:button", "Next") self._default_next_button_text = self._catalog.i18nc("@action:button", "Next")
self._pages = [] # type: List[Dict[str, Any]] self._pages: List[Dict[str, Any]] = []
self._current_page_index = 0 self._current_page_index = 0
# Store all the previous page indices so it can go back. # Store all the previous page indices so it can go back.
self._previous_page_indices_stack = deque() # type: deque self._previous_page_indices_stack: deque = deque()
# If the welcome flow should be shown. It can show the complete flow or just the changelog depending on the # If the welcome flow should be shown. It can show the complete flow or just the changelog depending on the
# specific case. See initialize() for how this variable is set. # specific case. See initialize() for how this variable is set.
@ -72,17 +73,21 @@ class WelcomePagesModel(ListModel):
def currentPageIndex(self) -> int: def currentPageIndex(self) -> int:
return self._current_page_index return self._current_page_index
# Returns a float number in [0, 1] which indicates the current progress.
@pyqtProperty(float, notify = currentPageIndexChanged) @pyqtProperty(float, notify = currentPageIndexChanged)
def currentProgress(self) -> float: def currentProgress(self) -> float:
"""
Returns a float number in [0, 1] which indicates the current progress.
"""
if len(self._items) == 0: if len(self._items) == 0:
return 0 return 0
else: else:
return self._current_page_index / len(self._items) return self._current_page_index / len(self._items)
# Indicates if the current page is the last page.
@pyqtProperty(bool, notify = currentPageIndexChanged) @pyqtProperty(bool, notify = currentPageIndexChanged)
def isCurrentPageLast(self) -> bool: def isCurrentPageLast(self) -> bool:
"""
Indicates if the current page is the last page.
"""
return self._current_page_index == len(self._items) - 1 return self._current_page_index == len(self._items) - 1
def _setCurrentPageIndex(self, page_index: int) -> None: def _setCurrentPageIndex(self, page_index: int) -> None:
@ -91,17 +96,22 @@ class WelcomePagesModel(ListModel):
self._current_page_index = page_index self._current_page_index = page_index
self.currentPageIndexChanged.emit() self.currentPageIndexChanged.emit()
# Ends the Welcome-Pages. Put as a separate function for cases like the 'decline' in the User-Agreement.
@pyqtSlot() @pyqtSlot()
def atEnd(self) -> None: def atEnd(self) -> None:
"""
Ends the Welcome-Pages. Put as a separate function for cases like the 'decline' in the User-Agreement.
"""
self.allFinished.emit() self.allFinished.emit()
self.resetState() self.resetState()
# Goes to the next page.
# If "from_index" is given, it will look for the next page to show starting from the "from_index" page instead of
# the "self._current_page_index".
@pyqtSlot() @pyqtSlot()
def goToNextPage(self, from_index: Optional[int] = None) -> None: def goToNextPage(self, from_index: Optional[int] = None) -> None:
"""
Goes to the next page.
If "from_index" is given, it will look for the next page to show starting from the "from_index" page instead of
the "self._current_page_index".
"""
# Look for the next page that should be shown # Look for the next page that should be shown
current_index = self._current_page_index if from_index is None else from_index current_index = self._current_page_index if from_index is None else from_index
while True: while True:
@ -137,9 +147,11 @@ class WelcomePagesModel(ListModel):
# Move to the next page # Move to the next page
self._setCurrentPageIndex(next_page_index) self._setCurrentPageIndex(next_page_index)
# Goes to the previous page. If there's no previous page, do nothing.
@pyqtSlot() @pyqtSlot()
def goToPreviousPage(self) -> None: def goToPreviousPage(self) -> None:
"""
Goes to the previous page. If there's no previous page, do nothing.
"""
if len(self._previous_page_indices_stack) == 0: if len(self._previous_page_indices_stack) == 0:
Logger.log("i", "No previous page, do nothing") Logger.log("i", "No previous page, do nothing")
return return
@ -148,9 +160,9 @@ class WelcomePagesModel(ListModel):
self._current_page_index = previous_page_index self._current_page_index = previous_page_index
self.currentPageIndexChanged.emit() self.currentPageIndexChanged.emit()
# Sets the current page to the given page ID. If the page ID is not found, do nothing.
@pyqtSlot(str) @pyqtSlot(str)
def goToPage(self, page_id: str) -> None: def goToPage(self, page_id: str) -> None:
"""Sets the current page to the given page ID. If the page ID is not found, do nothing."""
page_index = self.getPageIndexById(page_id) page_index = self.getPageIndexById(page_id)
if page_index is None: if page_index is None:
# FIXME: If we cannot find the next page, we cannot do anything here. # FIXME: If we cannot find the next page, we cannot do anything here.
@ -165,18 +177,22 @@ class WelcomePagesModel(ListModel):
# Find the next page to show starting from the "page_index" # Find the next page to show starting from the "page_index"
self.goToNextPage(from_index = page_index) self.goToNextPage(from_index = page_index)
# Checks if the page with the given index should be shown by calling the "should_show_function" associated with it.
# If the function is not present, returns True (show page by default).
def _shouldPageBeShown(self, page_index: int) -> bool: def _shouldPageBeShown(self, page_index: int) -> bool:
"""
Checks if the page with the given index should be shown by calling the "should_show_function" associated with
it. If the function is not present, returns True (show page by default).
"""
next_page_item = self.getItem(page_index) next_page_item = self.getItem(page_index)
should_show_function = next_page_item.get("should_show_function", lambda: True) should_show_function = next_page_item.get("should_show_function", lambda: True)
return should_show_function() return should_show_function()
# Resets the state of the WelcomePagesModel. This functions does the following:
# - Resets current_page_index to 0
# - Clears the previous page indices stack
@pyqtSlot() @pyqtSlot()
def resetState(self) -> None: def resetState(self) -> None:
"""
Resets the state of the WelcomePagesModel. This functions does the following:
- Resets current_page_index to 0
- Clears the previous page indices stack
"""
self._current_page_index = 0 self._current_page_index = 0
self._previous_page_indices_stack.clear() self._previous_page_indices_stack.clear()
@ -188,8 +204,8 @@ class WelcomePagesModel(ListModel):
def shouldShowWelcomeFlow(self) -> bool: def shouldShowWelcomeFlow(self) -> bool:
return self._should_show_welcome_flow return self._should_show_welcome_flow
# Gets the page index with the given page ID. If the page ID doesn't exist, returns None.
def getPageIndexById(self, page_id: str) -> Optional[int]: def getPageIndexById(self, page_id: str) -> Optional[int]:
"""Gets the page index with the given page ID. If the page ID doesn't exist, returns None."""
page_idx = None page_idx = None
for idx, page_item in enumerate(self._items): for idx, page_item in enumerate(self._items):
if page_item["id"] == page_id: if page_item["id"] == page_id:
@ -197,8 +213,9 @@ class WelcomePagesModel(ListModel):
break break
return page_idx return page_idx
# Convenience function to get QUrl path to pages that's located in "resources/qml/WelcomePages". @staticmethod
def _getBuiltinWelcomePagePath(self, page_filename: str) -> "QUrl": def _getBuiltinWelcomePagePath(page_filename: str) -> QUrl:
"""Convenience function to get QUrl path to pages that's located in "resources/qml/WelcomePages"."""
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
return QUrl.fromLocalFile(Resources.getPath(CuraApplication.ResourceTypes.QmlFiles, return QUrl.fromLocalFile(Resources.getPath(CuraApplication.ResourceTypes.QmlFiles,
os.path.join("WelcomePages", page_filename))) os.path.join("WelcomePages", page_filename)))
@ -213,21 +230,22 @@ class WelcomePagesModel(ListModel):
self._initialize() self._initialize()
def _initialize(self, update_should_show_flag: bool = True) -> None: def _initialize(self, update_should_show_flag: bool = True) -> None:
show_whatsnew_only = False show_whats_new_only = False
if update_should_show_flag: if update_should_show_flag:
has_active_machine = self._application.getMachineManager().activeMachine is not None has_active_machine = self._application.getMachineManager().activeMachine is not None
has_app_just_upgraded = self._application.hasJustUpdatedFromOldVersion() has_app_just_upgraded = self._application.hasJustUpdatedFromOldVersion()
# Only show the what's new dialog if there's no machine and we have just upgraded # Only show the what's new dialog if there's no machine and we have just upgraded
show_complete_flow = not has_active_machine show_complete_flow = not has_active_machine
show_whatsnew_only = has_active_machine and has_app_just_upgraded show_whats_new_only = has_active_machine and has_app_just_upgraded
# FIXME: This is a hack. Because of the circular dependency between MachineManager, ExtruderManager, and # FIXME: This is a hack. Because of the circular dependency between MachineManager, ExtruderManager, and
# possibly some others, setting the initial active machine is not done when the MachineManager gets initialized. # possibly some others, setting the initial active machine is not done when the MachineManager gets
# So at this point, we don't know if there will be an active machine or not. It could be that the active machine # initialized. So at this point, we don't know if there will be an active machine or not. It could be that
# files are corrupted so we cannot rely on Preferences either. This makes sure that once the active machine # the active machine files are corrupted so we cannot rely on Preferences either. This makes sure that once
# gets changed, this model updates the flags, so it can decide whether to show the welcome flow or not. # the active machine gets changed, this model updates the flags, so it can decide whether to show the
should_show_welcome_flow = show_complete_flow or show_whatsnew_only # welcome flow or not.
should_show_welcome_flow = show_complete_flow or show_whats_new_only
if should_show_welcome_flow != self._should_show_welcome_flow: if should_show_welcome_flow != self._should_show_welcome_flow:
self._should_show_welcome_flow = should_show_welcome_flow self._should_show_welcome_flow = should_show_welcome_flow
self.shouldShowWelcomeFlowChanged.emit() self.shouldShowWelcomeFlowChanged.emit()
@ -274,23 +292,25 @@ class WelcomePagesModel(ListModel):
] ]
pages_to_show = all_pages_list pages_to_show = all_pages_list
if show_whatsnew_only: if show_whats_new_only:
pages_to_show = list(filter(lambda x: x["id"] == "whats_new", all_pages_list)) pages_to_show = list(filter(lambda x: x["id"] == "whats_new", all_pages_list))
self._pages = pages_to_show self._pages = pages_to_show
self.setItems(self._pages) self.setItems(self._pages)
# For convenience, inject the default "next" button text to each item if it's not present.
def setItems(self, items: List[Dict[str, Any]]) -> None: def setItems(self, items: List[Dict[str, Any]]) -> None:
# For convenience, inject the default "next" button text to each item if it's not present.
for item in items: for item in items:
if "next_page_button_text" not in item: if "next_page_button_text" not in item:
item["next_page_button_text"] = self._default_next_button_text item["next_page_button_text"] = self._default_next_button_text
super().setItems(items) super().setItems(items)
# Indicates if the machine action panel should be shown by checking if there's any first start machine actions
# available.
def shouldShowMachineActions(self) -> bool: def shouldShowMachineActions(self) -> bool:
"""
Indicates if the machine action panel should be shown by checking if there's any first start machine actions
available.
"""
global_stack = self._application.getMachineManager().activeMachine global_stack = self._application.getMachineManager().activeMachine
if global_stack is None: if global_stack is None:
return False return False
@ -312,6 +332,3 @@ class WelcomePagesModel(ListModel):
def addPage(self) -> None: def addPage(self) -> None:
pass pass
__all__ = ["WelcomePagesModel"]

View File

@ -1,27 +1,39 @@
# Copyright (c) 2021 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from .WelcomePagesModel import WelcomePagesModel
import os import os
from typing import Optional, Dict, List, Tuple from typing import Optional, Dict, List, Tuple, TYPE_CHECKING
from PyQt5.QtCore import pyqtProperty, pyqtSlot from PyQt5.QtCore import pyqtProperty, pyqtSlot
from UM.Logger import Logger from UM.Logger import Logger
from UM.Resources import Resources from UM.Resources import Resources
# from cura.UI.WelcomePagesModel import WelcomePagesModel
# This Qt ListModel is more or less the same the WelcomePagesModel, except that this model is only for showing the
# "what's new" page. This is also used in the "Help" menu to show the changes log. if TYPE_CHECKING:
# from PyQt5.QtCore import QObject
from cura.CuraApplication import CuraApplication
class WhatsNewPagesModel(WelcomePagesModel): class WhatsNewPagesModel(WelcomePagesModel):
"""
This Qt ListModel is more or less the same the WelcomePagesModel, except that this model is only for showing the
"what's new" page. This is also used in the "Help" menu to show the changes log.
"""
image_formats = [".png", ".jpg", ".jpeg", ".gif", ".svg"] image_formats = [".png", ".jpg", ".jpeg", ".gif", ".svg"]
text_formats = [".txt", ".htm", ".html"] text_formats = [".txt", ".htm", ".html"]
image_key = "image" image_key = "image"
text_key = "text" text_key = "text"
def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
super().__init__(application, parent)
self._subpages: List[Dict[str, Optional[str]]] = []
@staticmethod @staticmethod
def _collectOrdinalFiles(resource_type: int, include: List[str]) -> Tuple[Dict[int, str], int]: def _collectOrdinalFiles(resource_type: int, include: List[str]) -> Tuple[Dict[int, str], int]:
result = {} #type: Dict[int, str] result = {} # type: Dict[int, str]
highest = -1 highest = -1
try: try:
folder_path = Resources.getPath(resource_type, "whats_new") folder_path = Resources.getPath(resource_type, "whats_new")
@ -65,7 +77,7 @@ class WhatsNewPagesModel(WelcomePagesModel):
texts, max_text = WhatsNewPagesModel._collectOrdinalFiles(Resources.Texts, WhatsNewPagesModel.text_formats) texts, max_text = WhatsNewPagesModel._collectOrdinalFiles(Resources.Texts, WhatsNewPagesModel.text_formats)
highest = max(max_image, max_text) highest = max(max_image, max_text)
self._subpages = [] #type: List[Dict[str, Optional[str]]] self._subpages = []
for n in range(0, highest + 1): for n in range(0, highest + 1):
self._subpages.append({ self._subpages.append({
WhatsNewPagesModel.image_key: None if n not in images else images[n], WhatsNewPagesModel.image_key: None if n not in images else images[n],
@ -93,5 +105,3 @@ class WhatsNewPagesModel(WelcomePagesModel):
def getSubpageText(self, page: int) -> str: def getSubpageText(self, page: int) -> str:
result = self._getSubpageItem(page, WhatsNewPagesModel.text_key) result = self._getSubpageItem(page, WhatsNewPagesModel.text_key)
return result if result else "* * *" return result if result else "* * *"
__all__ = ["WhatsNewPagesModel"]