Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "undomanager.hpp"
10 : #include "copykeycommand.hpp"
11 : #include "cutkeycommand.hpp"
12 : #include "deletekeycommand.hpp"
13 : #include "editkeycommand.hpp"
14 : #include "importconfigurationcommand.hpp"
15 : #include "newkeycommand.hpp"
16 : #include <QUndoStack>
17 :
18 0 : UndoManager::UndoManager (QObject * parentManager) : QObject (parentManager), m_undoStack (new QUndoStack (this)), m_clipboardEmpty (true)
19 : {
20 0 : connect (m_undoStack, SIGNAL (canRedoChanged (bool)), this, SIGNAL (canRedoChanged ()));
21 0 : connect (m_undoStack, SIGNAL (canUndoChanged (bool)), this, SIGNAL (canUndoChanged ()));
22 0 : connect (m_undoStack, SIGNAL (redoTextChanged (QString)), this, SIGNAL (redoTextChanged ()));
23 0 : connect (m_undoStack, SIGNAL (undoTextChanged (QString)), this, SIGNAL (undoTextChanged ()));
24 :
25 0 : m_clipboard = QApplication::clipboard ();
26 0 : }
27 :
28 0 : bool UndoManager::canUndo () const
29 : {
30 0 : return m_undoStack->canUndo ();
31 : }
32 :
33 0 : bool UndoManager::canRedo () const
34 : {
35 0 : return m_undoStack->canRedo ();
36 : }
37 :
38 0 : void UndoManager::createEditKeyCommand (TreeViewModel * model, int idx, DataContainer * data)
39 : {
40 0 : m_undoStack->push (new EditKeyCommand (model, idx, data));
41 0 : }
42 :
43 0 : void UndoManager::createDeleteKeyCommand (const QString & type, TreeViewModel * model, int idx)
44 : {
45 0 : m_undoStack->push (new DeleteKeyCommand (type, model, idx));
46 0 : }
47 :
48 0 : void UndoManager::createNewKeyCommand (TreeViewModel * model, int idx, DataContainer * data, bool isBelow)
49 : {
50 0 : m_undoStack->push (new NewKeyCommand (model, idx, data, isBelow));
51 0 : }
52 :
53 0 : void UndoManager::createCopyKeyCommand (TreeViewModel * model, int idx)
54 : {
55 0 : m_undoStack->push (new CopyKeyCommand (m_clipboardType, qvariant_cast<ConfigNodePtr> (m_clipboard->property ("source")),
56 0 : model->model ().at (idx)));
57 0 : }
58 :
59 0 : void UndoManager::createCutKeyCommand (TreeViewModel * model, int idx)
60 : {
61 0 : m_undoStack->push (new CutKeyCommand (m_clipboardType, qvariant_cast<ConfigNodePtr> (m_clipboard->property ("source")),
62 0 : model->model ().at (idx), m_clipboard->property ("index").toInt ()));
63 0 : }
64 :
65 0 : void UndoManager::createImportConfigurationCommand (TreeViewModel * model, int idx, DataContainer * data)
66 : {
67 0 : m_undoStack->push (new ImportConfigurationCommand (model, idx, data));
68 0 : }
69 :
70 0 : void UndoManager::setClean ()
71 : {
72 0 : m_undoStack->setClean ();
73 0 : }
74 :
75 0 : bool UndoManager::isClean () const
76 : {
77 0 : return m_undoStack->isClean ();
78 : }
79 :
80 0 : bool UndoManager::canPaste () const
81 : {
82 0 : return !m_clipboardEmpty;
83 : }
84 :
85 0 : int UndoManager::index () const
86 : {
87 0 : return m_undoStack->index ();
88 : }
89 :
90 0 : int UndoManager::cleanIndex () const
91 : {
92 0 : return m_undoStack->cleanIndex ();
93 : }
94 :
95 0 : int UndoManager::count () const
96 : {
97 0 : return m_undoStack->count ();
98 : }
99 :
100 0 : void UndoManager::setIndex (int idx)
101 : {
102 0 : m_undoStack->setIndex (idx);
103 0 : }
104 :
105 0 : void UndoManager::undo ()
106 : {
107 0 : m_undoStack->undo ();
108 0 : }
109 :
110 0 : void UndoManager::redo ()
111 : {
112 0 : m_undoStack->redo ();
113 0 : }
114 :
115 0 : QString UndoManager::undoText () const
116 : {
117 0 : return m_undoStack->undoText ();
118 : }
119 :
120 0 : QString UndoManager::clipboardType () const
121 : {
122 0 : return m_clipboardType;
123 : }
124 :
125 0 : void UndoManager::putToClipboard (const QString & type, TreeViewModel * model, int idx)
126 : {
127 0 : m_clipboardType = type;
128 :
129 0 : m_clipboard->clear ();
130 :
131 0 : m_clipboard->setProperty ("source", QVariant::fromValue (model->model ().at (idx)));
132 0 : m_clipboard->setProperty ("index", QVariant::fromValue (idx));
133 :
134 0 : m_clipboardEmpty = false;
135 :
136 0 : emit clipboardTypeChanged ();
137 0 : emit canPasteChanged ();
138 0 : }
139 :
140 0 : QString UndoManager::redoText () const
141 : {
142 0 : return m_undoStack->redoText ();
143 0 : }
|