|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>ImageStone: PCL_interface_undo.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a></div>
<h1>PCL_interface_undo.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> * Copyright (C) =USTC= Fu Li</span>
00003 <span class="comment"> *</span>
00004 <span class="comment"> * Author : Fu Li</span>
00005 <span class="comment"> * Create : 2005-3-6</span>
00006 <span class="comment"> * Home : http://www.crazy-bit.com/</span>
00007 <span class="comment"> * Mail : crazybit@263.net</span>
00008 <span class="comment"> * History : </span>
00009 <span class="comment"> */</span>
00010 <span class="preprocessor">#ifndef __FOO_PCL_INTERFACE_UNDO_MANAGER_H__</span>
00011 <span class="preprocessor"></span><span class="preprocessor">#define __FOO_PCL_INTERFACE_UNDO_MANAGER_H__</span>
00012 <span class="preprocessor"></span><span class="preprocessor">#include <assert.h></span>
00013 <span class="preprocessor">#include <deque></span>
00014
00015 <span class="keyword">template</span><<span class="keyword">class</span> T> <span class="keyword">class </span><a class="code" href="class_p_c_l___interface___undo.html">PCL_Interface_Undo</a> ;
00016
00017 <span class="comment">//=============================================================================</span>
00021 <span class="comment"></span><span class="keyword">template</span><<span class="keyword">class</span> T>
<a name="l00022"></a><a class="code" href="class_p_c_l___interface___undo.html">00022</a> <span class="keyword">class </span><a class="code" href="class_p_c_l___interface___undo.html">PCL_Interface_Undo</a>
00023 {
00024 <span class="keyword">public</span>:
00030 <span class="keywordtype">bool</span> IsUndoEnable()<span class="keyword"> const </span>{<span class="keywordflow">return</span> !m_UndoList.empty();}
00031 <span class="keywordtype">bool</span> IsRedoEnable()<span class="keyword"> const </span>{<span class="keywordflow">return</span> !m_RedoList.empty();}
00032 <span class="keywordtype">void</span> Undo()
00033 {
00034 <span class="keywordflow">if</span> (IsUndoEnable())
00035 {
00036 T * p = m_UndoList.back() ; <span class="comment">// get the latest undo-command</span>
00037 this-><a class="code" href="class_p_c_l___interface___undo.html#b0">PCL_Implement_Undo</a> (p) ;
00038 m_UndoList.pop_back() ;
00039 m_RedoList.push_front(p) ;
00040 }
00041 }
00042 <span class="keywordtype">void</span> Redo()
00043 {
00044 <span class="keywordflow">if</span> (IsRedoEnable())
00045 {
00046 T * p = m_RedoList.front() ; <span class="comment">// get the first redo-command</span>
00047 this-><a class="code" href="class_p_c_l___interface___undo.html#b1">PCL_Implement_Redo</a> (p) ;
00048 m_RedoList.pop_front() ;
00049 m_UndoList.push_back(p) ;
00050 }
00051 }
00053
00059 <span class="keywordtype">void</span> ClearRedoList()
00060 {
00061 <span class="keywordflow">while</span> (IsRedoEnable())
00062 {
00063 T * p = m_RedoList.back() ;
00064 m_RedoList.pop_back() ;
00065 <span class="keyword">delete</span> p ;
00066 }
00067 }
00068 <span class="keywordtype">void</span> ClearUndoList()
00069 {
00070 <span class="keywordflow">while</span> (IsUndoEnable())
00071 {
00072 T * p = m_UndoList.back() ;
00073 m_UndoList.pop_back() ;
00074 <span class="keyword">delete</span> p ;
00075 }
00076 }
00078
00084 <span class="keywordtype">int</span> GetUndoLevel()<span class="keyword"> const </span>{<span class="keywordflow">return</span> m_nUndoLevel;}
<a name="l00086"></a><a class="code" href="class_p_c_l___interface___undo.html#z57_1">00086</a> <span class="keywordtype">void</span> <a class="code" href="class_p_c_l___interface___undo.html#z57_1">SetUndoLevel</a> (<span class="keywordtype">int</span> nLevel)
00087 {
00088 <span class="keywordflow">if</span> (nLevel < 0)
00089 {
00090 assert(<span class="keyword">false</span>); <span class="keywordflow">return</span>;
00091 }
00092
00093 m_nUndoLevel = nLevel ; <span class="comment">// >=0</span>
00094 <span class="comment">// calculate current number of command in list</span>
00095 <span class="keywordtype">int</span> nRemove = (int)(m_UndoList.size() + m_RedoList.size()) - m_nUndoLevel ;
00096 <span class="keywordflow">if</span> (nRemove <= 0)
00097 return ;
00098
00099 <span class="comment">// first : delete command from Redo-list back</span>
00100 <span class="keywordflow">while</span> (!m_RedoList.empty() && (nRemove > 0))
00101 {
00102 T * p = m_RedoList.back() ;
00103 m_RedoList.pop_back() ;
00104 <span class="keyword">delete</span> p ; nRemove-- ;
00105 }
00106
00107 <span class="comment">// then : delete command from Undo-list front</span>
00108 <span class="keywordflow">while</span> (!m_UndoList.empty() && (nRemove > 0))
00109 {
00110 T * p = m_UndoList.front() ;
00111 m_UndoList.pop_front() ;
00112 <span class="keyword">delete</span> p ; nRemove-- ;
00113 }
00114 }
00116
00117 <span class="keyword">protected</span>:
00120 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="class_p_c_l___interface___undo.html#b0">PCL_Implement_Undo</a> (T* pCmd) =0 ;
00123 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="class_p_c_l___interface___undo.html#b1">PCL_Implement_Redo</a> (T* pCmd) =0 ;
00124
<a name="l00130"></a><a class="code" href="class_p_c_l___interface___undo.html#b2">00130</a> <span class="keywordtype">bool</span> <a class="code" href="class_p_c_l___interface___undo.html#b2">AddCommand</a> (T* pCmd)
00131 {
00132 <span class="keywordflow">if</span> ((m_nUndoLevel <= 0) || !pCmd)
00133 {
00134 assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> <span class="keyword">false</span>;
00135 }
00136
00137 <span class="comment">// clear redo-list, ennnnnnn. this behavior like a bintree.</span>
00138 ClearRedoList() ;
00139
00140 <span class="comment">// single command</span>
00141 assert ((<span class="keywordtype">int</span>)m_UndoList.size() <= m_nUndoLevel) ;
00142 <span class="keywordflow">if</span> ((<span class="keywordtype">int</span>)m_UndoList.size() >= m_nUndoLevel) <span class="comment">// never '>'</span>
00143 {
00144 T * p = m_UndoList.front() ;
00145 m_UndoList.pop_front() ;
00146 <span class="keyword">delete</span> p ;
00147 }
00148
00149 <span class="comment">// add to undo list</span>
00150 m_UndoList.push_back (pCmd) ;
00151 <span class="keywordflow">return</span> true ;
00152 }
00153
00154 T* GetLatestUndoCommand()<span class="keyword"> const </span>{<span class="keywordflow">return</span> IsUndoEnable() ? m_UndoList.back() : 0;}
00155
00161 <a class="code" href="class_p_c_l___interface___undo.html">PCL_Interface_Undo</a> (<span class="keywordtype">int</span> nUndoLevel = 20) : m_nUndoLevel(nUndoLevel) {}
00162 <span class="keyword">virtual</span> ~<a class="code" href="class_p_c_l___interface___undo.html">PCL_Interface_Undo</a>()
00163 {
00164 ClearRedoList() ;
00165 ClearUndoList() ;
00166 }
00168
00169 <span class="keyword">private</span>:
00170 std::deque<T*> m_UndoList ;
00171 std::deque<T*> m_RedoList ;
00172 <span class="comment">// the level of undo operation. the ZERO means disable undo</span>
00173 <span class="keywordtype">int</span> m_nUndoLevel ;
00174 };
00175
00176 <span class="comment">//=============================================================================</span>
00177 <span class="comment">// inline implement</span>
00178 <span class="comment">//=============================================================================</span>
00179
00180 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Wed Mar 8 11:12:46 2006 for ImageStone by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
</body>
</html>
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
graduate from University of Science and Technology of China at 2002.
Now I work at www.phoxo.com.