Undo/Redo for Qt Tree Model
eryar@163.com
Abstract. Qt contains a set of item view classes that use a model/view architecture to manage the relationship between data and the way it is presented to the user. The separation of functionality introduced by this architecture gives developers greater flexibility to customize the presentation of items, and provides a standard model interface to allow a wide range of data sources to be used with existing item views. Model 3D aided design software such as AVEVA Plant/PDMS, Marine use the architecture to manage the design data source. The article demonstrate the Undo/Redo on the Qt Tree model.
Key Words. Model/View, MVC pattern, Undo/Redo, Tree Model
1. Introduction
現代稍微大型一點的軟件,要處理的數據量通常會比較大。這時就需要有一個唯一的數據源,且會對這個數據源中的數據進行增、刪、改的操作。如果沒有統一的數據源,數據會隨意地被創建和刪除,且創建和刪除的用戶界面也不統一,不利于軟件管理。基于唯一的數據源,并在這個基礎上提供統一的增刪改接口,不僅有利于軟件數據管理,還有利于事務的處理,即Undo/Redo功能。若引入腳本語言,如Tcl或Python,甚至可實現腳本命令對數據的操作,為程序增加二次開發功能。
Figure 1. AVEVA Plant/PDMS Software
如上圖1所示為英國劍橋大學CADCENTRE出品的AVEVA Plant/PDMS軟件。PDMS使用了統一的數據源,左邊的導航樹及右邊的三維模型都是這個數據源的一種展示方式,可以在導航樹上創建及刪除數據;也可以在三維視圖中進行交互,方便地創建及修改模型。這種方式與GoF描述的Observer觀察者模式相似,即:
定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時,所有依賴于它的對象都得到通知并被自動更新。
對應上面的軟件,即對于唯一的數據源這個對象,導航樹及三維視圖都依賴于他。當數據源這個對象中有數據的增刪改時,導航樹及三維視圖都會得到通知并自動更新了。
圖1右下角的CommandWindow中可以輸入命令,即PML,也可以對數據源進行修改。引入Observer模式,即可實現這些功能。
Qt中包含了一系列的Model/View類,這些類都是基于MVC架構的,這種將數據與視圖分開的處理,使同一個數據源可以不同的視圖中進行展示,且不需要修改數據源的結構。Qt中基于Command命令模式實現了一個Undo/Redo的框架,將Undo/Redo框架應用于Model/View的類,即可得到與AVEVA Plant/PDMS中類似的功能。
本文主要對Qt中的model/view及undo/redo框架進行介紹,及如何將Undo/Redo框架應用到model/view中去,實現對以層次方式組織數據的樹模型進行undo/redo,進而實現一個三維CAD工廠設計的原型。
2.Tree Model
軟件中使用層次方式來組織數據,可護展性好。如OpenCASCADE中的OCAF框架的數據也是樹形方式組織:
Figure 2.1 OpenCASCADE OCAF data tree
每個Label中可以存放屬性,還包含子Label,這樣就提供了一個通用的程序框架,靈活使用,可以用一種統一的方式來存放數據。有一個缺點就是每個Label中的屬性類型只能包含一種,這種方式相對于AVEVA的自定義屬性UDA來說,就顯得很不方便了,需要提前對數據的存儲進行規劃,擴展起來稍有不便。
AVEVA中對每個數據Element都提供了自定義屬性(User Definable Attribute),每種類型的屬性(數值型、字符串型等)都可以包含任意數量,沒有限制。所以可擴展性更好。
Qt中基于MVC模式的model/view架構提供了一些預定義的模型及視圖,如列表模型及對應的視圖,樹模型及樹視圖等。在model/view架構中,為model提供了統一的訪問數據的方式,即Model Index。對于列表model,通過QModelIndex::row()即可訪問。對于表格model,需要QModelIndex::row()和QModelIndex::column()。對于樹model,除了上述兩個函數,還需要提供QModelIndex::parent()來對父節點進行訪問。如下圖2.2所示:
Figure 2.2 Schematic view of Qt’s Models
若需要對列表及表格model應用Undo框架,還是很方便的,只要記住數據操作的對應的row或row及column即可。若要對樹形model進行undo,原理也是一樣的,即在redo中生成數據在undo中恢復數據。
3.Key Points
將undo框架與model/view結合有兩種方式:
v 通過在undo/redo命令中調用QAbstractItemModel的API來改變底層數據;
v 自定義model使其創建命令并推送到undo棧Stack中去;
第一種方式看起來要容易些,且可與任意model結合。The first approach seems simpler, as it works with any model and gives the undo framework total control --- you can create as many different types of commands as you want and each of them can call a number of methods from the model API in their undo() and redo() routines.
當model中有delete操作時,會將index內部的數據刪除。對于這種情況,需要注意。Qt給出了兩個解決辦法:
There are two things to remember. The first is that, if you delete items, rows or columns, you should save the data of all the items getting deleted(including children) somewhere so that you can later revert the deletion. The second is that it would be nice to be able to set meaningful descriptions of commands pushed on the stack. To do that you can provide methods in the model, that are called each time a command is pushed onto the stack, which should each return a description based on the parameters of the command being executed.
基于上述思想,實現了Tree Model與Undo Framework結合的一個小例子:
Figure 3. Undo/Redo and Tree Model
4.Conclusion
Qt中基于Command命令模式的Undo框架,需要對數據操作及恢復仔細考量。尤其對于有刪除數據的操作,若以QModelIndex作為參考依據,則會出錯。對于此Qt提供了兩個解決思路。
基于Qt的model/view及undo框架,也可以方便實現OpenCASCADE中的OCAF框架許多類似的功能,且Qt的代碼可讀性更高。若要快速開發程序,可以考慮使用Qt的model/view框架。
感謝曉天的建議。
5. References
1. Using Undo/Redo with Item Views. http://doc.qt.digia.com/qq/qq25-undo.html
2. OpenCASCADE. OCAF User’s Guide. 2014
3. GoF. Design Patterns-Element of Reusable Object-Oriented Software. 1995