Trong khung hoàn tác Qt, tất cả các hành động mà người dùng thực hiện được triển khai trong các lớp kế thừa
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]6. Một lớp lệnh hoàn tác biết cách thực hiện cả
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]7 - hoặc chỉ thực hiện lần đầu tiên - và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]8 một hành động. Đối với mỗi hành động mà người dùng thực hiện, một lệnh được đặt trên một
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]9. Vì ngăn xếp chứa tất cả các lệnh được thực thi [xếp chồng lên nhau theo thứ tự thời gian] trên tài liệu nên nó có thể cuộn trạng thái của tài liệu tới và lui bằng cách hoàn tác và làm lại các lệnh của nó. Xem tài liệu tổng quan để biết phần giới thiệu cấp cao về khung hoàn tác
Ví dụ hoàn tác thực hiện một ứng dụng sơ đồ đơn giản. Có thể thêm và xóa các mục có dạng hình hộp hoặc hình chữ nhật và di chuyển các mục bằng cách kéo chúng bằng chuột. Ngăn xếp hoàn tác được hiển thị trong một
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]0, là một danh sách trong đó các lệnh được hiển thị dưới dạng các mục danh sách. Hoàn tác và làm lại có sẵn thông qua menu chỉnh sửa. Người dùng cũng có thể chọn một lệnh từ chế độ xem hoàn tác
Chúng tôi sử dụng khung chế độ xem đồ họa để triển khai sơ đồ. Chúng tôi chỉ xử lý mã liên quan một cách ngắn gọn vì khung có các ví dụ của riêng nó [e. g. , Ví dụ về cảnh sơ đồ ].
Ví dụ bao gồm các lớp sau
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]1 là cửa sổ chính và sắp xếp các widget của ví dụ. Nó tạo các lệnh dựa trên đầu vào của người dùng và giữ chúng trên ngăn xếp lệnh def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]2 thêm một mục vào cảnh def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]3 xóa một mục khỏi hiện trường def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]4 khi một mục được di chuyển, MoveCommand sẽ ghi lại các vị trí bắt đầu và kết thúc của việc di chuyển, và nó sẽ di chuyển mục theo những điều này khidef createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]7 vàdef createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]8 được gọi def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]7 kế thừadef createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]8 và phát ra tín hiệu chodef createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]9 khi một mục được di chuyển def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]40 kế thừadef createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]41 và đại diện cho một mục trong sơ đồ
Định nghĩa lớp MainWindow#
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]6
Lớp
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]1 duy trì ngăn xếp hoàn tác, tôi. e. , nó tạo ra các
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]6 và đẩy và bật chúng ra khỏi ngăn xếp khi nhận được tín hiệu
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]44 từ
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]45 và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]46
Triển khai lớp MainWindow#
Chúng ta sẽ bắt đầu với việc xem xét hàm tạo
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]2
Trong hàm tạo, chúng tôi thiết lập DiagramScene và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]47
Đây là hàm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]48
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]0 là một tiện ích hiển thị văn bản, được đặt bằng hàm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]70, cho mỗi
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]6 trong ngăn xếp hoàn tác trong danh sách
Đây là hàm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]72
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]
Hàm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]72 thiết lập tất cả các hành động ví dụ theo cách được hiển thị ở trên. Các phương thức
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]74 và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]75 giúp chúng ta tạo các hành động bị vô hiệu hóa và được kích hoạt dựa trên trạng thái của ngăn xếp. Ngoài ra, văn bản của hành động sẽ được cập nhật tự động dựa trên
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]76 của các lệnh hoàn tác. Đối với các hành động khác, chúng tôi đã triển khai các vị trí trong lớp
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]1
Đây là hàm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]78
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]4
Chúng tôi phải sử dụng các tín hiệu
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]79
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]90 và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]91 vì chúng tôi chỉ muốn bật
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]92 khi chúng tôi đã chọn một mục
Đây là khe cắm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]93
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]7
Chúng tôi chỉ cần đẩy một MoveCommand trên ngăn xếp, lệnh này gọi
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]7 trên đó
Đây là khe cắm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]95
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]9
Một mục phải được chọn để xóa. Chúng tôi cần kiểm tra xem nó có được chọn hay không vì
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]92 có thể được bật ngay cả khi một mục không được chọn. Điều này có thể xảy ra do chúng tôi không bắt được tín hiệu hoặc sự kiện khi một mục được chọn
Đây là vị trí của
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]97 và itemMenuAboutToHide[]
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]9
Chúng tôi triển khai
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]97 và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]99 để có menu mục động. Các khe cắm này được kết nối với tín hiệu
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]90 và
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]91. Chúng tôi cần điều này để vô hiệu hóa hoặc kích hoạt
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]92
Đây là khe cắm
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]93
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]6
Hàm
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]93 tạo AddCommand và đẩy nó vào ngăn xếp hoàn tác
Đây là
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]95 sot
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]9
Hàm
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]95 tạo AddCommand và đẩy nó vào ngăn xếp hoàn tác
Đây là việc thực hiện của
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]97
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]20
Vị trí giới thiệu được kích hoạt bởi
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]98 và hiển thị hộp giới thiệu chẳng hạn
AddCommand Lớp Định nghĩa #
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]21
Lớp
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]2 thêm các mục đồ họa DiagramItem vào DiagramScene
Thực hiện lớp AddCommand#
Chúng tôi bắt đầu với hàm tạo
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]22
Đầu tiên chúng ta tạo DiagramItem để thêm vào DiagramScene. Hàm
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]70 cho phép chúng tôi đặt một
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]61 mô tả lệnh. Chúng tôi sử dụng điều này để nhận các tin nhắn tùy chỉnh trong
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]0 và trong menu của cửa sổ chính
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]23
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]8 xóa vật phẩm khỏi hiện trường
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]24
Chúng tôi đặt vị trí của mục vì chúng tôi không làm điều này trong hàm tạo
XóaĐịnh nghĩa lớp lệnh#
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]25
Lớp DeleteCommand thực hiện chức năng xóa một mục khỏi hiện trường
Xóa thực hiện lớp lệnh #
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]26
Chúng tôi biết rằng phải có một mục được chọn vì không thể tạo DeleteCommand trừ khi mục cần xóa được chọn và chỉ có thể chọn một mục bất kỳ lúc nào. Mục này phải được bỏ chọn nếu nó được chèn lại vào cảnh
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]27
Mục này chỉ đơn giản là được lắp lại vào cảnh
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]28
Vật phẩm được lấy ra khỏi hiện trường
Định nghĩa lớp MoveCommand#
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]29
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]64 được triển khai lại để thực hiện các bước di chuyển liên tiếp của một mục MoveCommand, i. e, mục sẽ được di chuyển trở lại vị trí bắt đầu của lần di chuyển đầu tiên
Triển khai lớp MoveCommand#
Hàm tạo của MoveCommand trông như thế này
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]0
Chúng tôi lưu cả vị trí cũ và mới để hoàn tác và làm lại tương ứng
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]1
Chúng tôi chỉ cần đặt các mục ở vị trí cũ và cập nhật cảnh
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]2
Chúng tôi đặt mục vào vị trí mới của nó
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]3
Bất cứ khi nào MoveCommand được tạo, chức năng này được gọi để kiểm tra xem nó có nên được hợp nhất với lệnh trước đó không. Đó là đối tượng lệnh trước đó được giữ trên ngăn xếp. Hàm trả về true nếu lệnh được hợp nhất;
Trước tiên, chúng tôi kiểm tra xem đó có phải là cùng một mục đã được di chuyển hai lần hay không, trong trường hợp đó, chúng tôi hợp nhất các lệnh. Chúng tôi cập nhật vị trí của vật phẩm để nó sẽ chiếm vị trí cuối cùng trong chuỗi di chuyển khi hoàn tác
Định nghĩa lớp DiagramScene#
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]4
DiagramScene thực hiện chức năng di chuyển một DiagramItem bằng chuột. Nó phát ra tín hiệu khi di chuyển xong. Điều này bị bắt bởi
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]1, khiến MoveCommands. Chúng tôi không kiểm tra việc triển khai DiagramScene vì nó chỉ giải quyết các vấn đề về khung đồ họa
``chính[]``
Hàm số#
Hàm
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]66 của chương trình trông như thế này
def createUndoView[self]: undoView = QUndoView[undoStack] undoView.setWindowTitle[tr["Command List"]] undoView.show[] undoView.setAttribute[Qt.WA_QuitOnClose, False]5
Chúng tôi vẽ một lưới trong nền của DiagramScene, vì vậy chúng tôi sử dụng tệp tài nguyên. Phần còn lại của hàm tạo
def createActions[self]: deleteAction = QAction[tr["Delete Item"], self] deleteAction.setShortcut[tr["Del"]] deleteAction.triggered.connect[self.deleteItem] ... undoAction = undoStack.createUndoAction[self, tr["Undo"]] undoAction.setShortcuts[QKeySequence.Undo] redoAction = undoStack.createRedoAction[self, tr["Redo"]] redoAction.setShortcuts[QKeySequence.Redo]1 và hiển thị nó dưới dạng cửa sổ cấp cao nhất