summaryrefslogtreecommitdiffstats
path: root/src/document/MultiViewCommandHistory.h
blob: d122d089527adcb4793ff4bc46cd21c1550337d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>

    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _RG_MULTIVIEWCOMMANDHISTORY_H_
#define _RG_MULTIVIEWCOMMANDHISTORY_H_

#include <set>
#include <stack>
#include <qobject.h>


class QString;
class KCommand;
class KActionCollection;


namespace Rosegarden
{



/**
 * The MultiViewCommandHistory class is much like KCommandHistory in
 * that it stores a list of executed commands and maintains Undo and
 * Redo actions synchronised with those commands.
 *
 * The difference is that MultiViewCommandHistory allows you to
 * associate more than one Undo and Redo action with the same command
 * history, and it keeps them all up-to-date at once.  This makes it
 * effective in systems where multiple views may be editing the same
 * data at once.
 */

class MultiViewCommandHistory : public QObject
{
    Q_OBJECT
public:

    MultiViewCommandHistory();
    virtual ~MultiViewCommandHistory();

    void clear();
    
    void attachView(KActionCollection *collection);
    void detachView(KActionCollection *collection);

    void addCommand(KCommand *command, bool execute = true);
    
    /// @return the maximum number of items in the undo history
    int undoLimit() { return m_undoLimit; }

    /// Set the maximum number of items in the undo history
    void setUndoLimit(int limit);

    /// @return the maximum number of items in the redo history
    int redoLimit() { return m_redoLimit; }

    /// Set the maximum number of items in the redo history
    void setRedoLimit(int limit);
    
public slots:
    /**
     * Remember when you saved the document.
     * Call this right after saving the document. As soon as
     * the history reaches the current index again (via some
     * undo/redo operations) it will emit @ref documentRestored
     * If you implemented undo/redo properly the document is
     * the same you saved before.
     */
    virtual void documentSaved();

protected slots:
    void slotUndo();
    void slotRedo();
    void slotUndoAboutToShow();
    void slotUndoActivated(int);
    void slotRedoAboutToShow();
    void slotRedoActivated(int);

signals:
    /**
     * This is emitted every time a command is executed
     * (whether by addCommand, undo or redo).
     * You can use this to update the GUI, for instance.
     */
    void commandExecuted(KCommand *);

    /**
     * This is emitted every time a command is executed
     * (whether by addCommand, undo or redo).
     *
     * It should be connected to the update() slot of widgets
     * which need to repaint after a command
     */
    void commandExecuted();

    /**
     * This is emitted every time we reach the index where you
     * saved the document for the last time. See @ref documentSaved
     */
    void documentRestored();

private:
    //--------------- Data members ---------------------------------

    typedef std::set<KActionCollection *> ViewSet;
    ViewSet m_views;

    typedef std::stack<KCommand *> CommandStack;
    CommandStack m_undoStack;
    CommandStack m_redoStack;

    int m_undoLimit;
    int m_redoLimit;
    int m_savedAt;

    void updateButtons();
    void updateButton(bool undo, const QString &name, CommandStack &stack);
    void updateMenu(bool undo, const QString &name, CommandStack &stack);
    void clipCommands();

    void clipStack(CommandStack &stack, int limit);
    void clearStack(CommandStack &stack);
};



}

#endif