~ubuntu-branches/ubuntu/vivid/notecase/vivid

« back to all changes in this revision

Viewing changes to src/lib/DocActionManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Vijay(Vijay)
  • Date: 2007-06-14 00:13:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070614001348-z9e2vbgtenb9nhoo
Tags: 1.5.6-0ubuntu1
* New Upstream release 
*  The libgnomevfs2-dev is also added to Build-Depends 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
6
 
// File: Object that manages document changes history list (Undo/Redo framework)
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
#include "DocActionManager.h"
10
 
 
11
 
DocActionManager::DocActionManager(int nMaxListSize)
12
 
{
13
 
        m_nMaxListSize = nMaxListSize;
14
 
        m_nPos = 0;
15
 
}
16
 
 
17
 
DocActionManager::~DocActionManager()
18
 
{
19
 
        Clear();
20
 
}
21
 
 
22
 
void DocActionManager::Clear()
23
 
{
24
 
        //delete all action objects
25
 
        for(unsigned int i=0; i<m_lstHistory.size(); i++)
26
 
                delete m_lstHistory[i];
27
 
        m_lstHistory.clear();   //clear pointer list
28
 
        m_nPos = 0;
29
 
}
30
 
 
31
 
bool DocActionManager::CanUndo()
32
 
{
33
 
        return (m_lstHistory.size() > 0) && (m_nPos > 0);
34
 
}
35
 
 
36
 
bool DocActionManager::CanRedo()
37
 
{
38
 
        return (m_lstHistory.size() > 0) && ((m_lstHistory.size() - m_nPos) > 0);
39
 
}
40
 
 
41
 
void DocActionManager::AddAction(DocActionBase *pAction)
42
 
{
43
 
        //each new action destroys redo part of the list
44
 
        ClearRedoList();
45
 
        
46
 
        //push new undo action into the list
47
 
        m_lstHistory.push_back(pAction);
48
 
        m_nPos ++;
49
 
        
50
 
        //check if we overgrown allowed list size
51
 
        if(m_lstHistory.size() > m_nMaxListSize)
52
 
        {
53
 
                //delete oldest entry
54
 
                delete m_lstHistory[0];
55
 
                m_lstHistory.erase(m_lstHistory.begin(), m_lstHistory.begin()+1);
56
 
                m_nPos --;
57
 
        }
58
 
}
59
 
 
60
 
void DocActionManager::Undo()
61
 
{
62
 
        if(CanUndo())
63
 
        {
64
 
                m_nPos --;
65
 
                m_lstHistory[m_nPos]->Undo();
66
 
        }
67
 
}
68
 
 
69
 
void DocActionManager::Redo()
70
 
{
71
 
        if(CanRedo())
72
 
        {
73
 
                m_lstHistory[m_nPos]->Redo();
74
 
                m_nPos ++;
75
 
        }
76
 
}
77
 
 
78
 
void DocActionManager::ClearRedoList()
79
 
{
80
 
        // clear redo part of the list
81
 
        if(CanRedo()){
82
 
                for(unsigned int i=m_nPos; i<m_lstHistory.size(); i++)
83
 
                        delete m_lstHistory[i];
84
 
                m_lstHistory.erase(m_lstHistory.begin()+m_nPos, m_lstHistory.end());
85
 
        }
86
 
}
87
 
 
88
 
// returns a pointer to the specified action.
89
 
DocActionBase* DocActionManager::GetAction(unsigned int nPos) const
90
 
{
91
 
        // make sure that index is valid
92
 
        if(nPos < 0 || nPos > m_lstHistory.size()) return NULL;
93
 
 
94
 
        return m_lstHistory[nPos];
95
 
}
96
 
 
97
 
// returns a pointer to the current action.
98
 
DocActionBase* DocActionManager::GetCurrentAction() const
99
 
{
100
 
        // make sure that current undo action exists
101
 
        if(m_nPos < 1 || m_nPos > m_lstHistory.size()) return NULL;
102
 
 
103
 
        return m_lstHistory[m_nPos - 1];
104
 
}
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: Object that manages document changes history list (Undo/Redo framework)
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
#include "DocActionManager.h"
 
10
 
 
11
DocActionManager::DocActionManager(int nMaxListSize)
 
12
{
 
13
        m_nMaxListSize = nMaxListSize;
 
14
        m_nPos = 0;
 
15
}
 
16
 
 
17
DocActionManager::~DocActionManager()
 
18
{
 
19
        Clear();
 
20
}
 
21
 
 
22
void DocActionManager::Clear()
 
23
{
 
24
        //delete all action objects
 
25
        for(unsigned int i=0; i<m_lstHistory.size(); i++)
 
26
                delete m_lstHistory[i];
 
27
        m_lstHistory.clear();   //clear pointer list
 
28
        m_nPos = 0;
 
29
}
 
30
 
 
31
bool DocActionManager::CanUndo()
 
32
{
 
33
        return (m_lstHistory.size() > 0) && (m_nPos > 0);
 
34
}
 
35
 
 
36
bool DocActionManager::CanRedo()
 
37
{
 
38
        return (m_lstHistory.size() > 0) && ((m_lstHistory.size() - m_nPos) > 0);
 
39
}
 
40
 
 
41
void DocActionManager::AddAction(DocActionBase *pAction)
 
42
{
 
43
        //each new action destroys redo part of the list
 
44
        ClearRedoList();
 
45
        
 
46
        //push new undo action into the list
 
47
        m_lstHistory.push_back(pAction);
 
48
        m_nPos ++;
 
49
        
 
50
        //check if we overgrown allowed list size
 
51
        if(m_lstHistory.size() > m_nMaxListSize)
 
52
        {
 
53
                //delete oldest entry
 
54
                delete m_lstHistory[0];
 
55
                m_lstHistory.erase(m_lstHistory.begin(), m_lstHistory.begin()+1);
 
56
                m_nPos --;
 
57
        }
 
58
}
 
59
 
 
60
void DocActionManager::Undo()
 
61
{
 
62
        if(CanUndo())
 
63
        {
 
64
                m_nPos --;
 
65
                m_lstHistory[m_nPos]->Undo();
 
66
        }
 
67
}
 
68
 
 
69
void DocActionManager::Redo()
 
70
{
 
71
        if(CanRedo())
 
72
        {
 
73
                m_lstHistory[m_nPos]->Exec();
 
74
                m_nPos ++;
 
75
        }
 
76
}
 
77
 
 
78
void DocActionManager::ClearRedoList()
 
79
{
 
80
        // clear redo part of the list
 
81
        if(CanRedo()){
 
82
                for(unsigned int i=m_nPos; i<m_lstHistory.size(); i++)
 
83
                        delete m_lstHistory[i];
 
84
                m_lstHistory.erase(m_lstHistory.begin()+m_nPos, m_lstHistory.end());
 
85
        }
 
86
}
 
87
 
 
88
// returns a pointer to the specified action.
 
89
DocActionBase* DocActionManager::GetAction(unsigned int nPos) const
 
90
{
 
91
        // make sure that index is valid
 
92
        if(nPos < 0 || nPos > m_lstHistory.size()) return NULL;
 
93
 
 
94
        return m_lstHistory[nPos];
 
95
}
 
96
 
 
97
// returns a pointer to the current action.
 
98
DocActionBase* DocActionManager::GetCurrentAction() const
 
99
{
 
100
        // make sure that current undo action exists
 
101
        if(m_nPos < 1 || m_nPos > m_lstHistory.size()) return NULL;
 
102
 
 
103
        return m_lstHistory[m_nPos - 1];
 
104
}