OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / shared / qrceditor / undocommands.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "undocommands_p.h"
34
35 #include <QtCore/QModelIndex>
36
37 namespace SharedTools {
38
39 ViewCommand::ViewCommand(ResourceView *view)
40         : m_view(view)
41 { }
42
43 ViewCommand::~ViewCommand()
44 { }
45
46 ModelIndexViewCommand::ModelIndexViewCommand(ResourceView *view)
47         : ViewCommand(view)
48 { }
49
50 ModelIndexViewCommand::~ModelIndexViewCommand()
51 { }
52
53 void ModelIndexViewCommand::storeIndex(const QModelIndex &index)
54 {
55     if (m_view->isPrefix(index)) {
56         m_prefixArrayIndex = index.row();
57         m_fileArrayIndex = -1;
58     } else {
59         m_fileArrayIndex = index.row();
60         m_prefixArrayIndex = m_view->model()->parent(index).row();
61     }
62 }
63
64 QModelIndex ModelIndexViewCommand::makeIndex() const
65 {
66     const QModelIndex prefixModelIndex
67             = m_view->model()->index(m_prefixArrayIndex, 0, QModelIndex());
68     if (m_fileArrayIndex != -1) {
69         // File node
70         const QModelIndex fileModelIndex
71                 = m_view->model()->index(m_fileArrayIndex, 0, prefixModelIndex);
72         return fileModelIndex;
73     } else {
74         // Prefix node
75         return prefixModelIndex;
76     }
77 }
78
79
80
81 ModifyPropertyCommand::ModifyPropertyCommand(ResourceView *view, const QModelIndex &nodeIndex,
82         ResourceView::NodeProperty property, const int mergeId, const QString &before,
83         const QString &after)
84         : ModelIndexViewCommand(view), m_property(property), m_before(before), m_after(after),
85         m_mergeId(mergeId)
86 {
87     storeIndex(nodeIndex);
88 }
89
90 bool ModifyPropertyCommand::mergeWith(const QUndoCommand * command)
91 {
92     if (command->id() != id() || m_property != static_cast<const ModifyPropertyCommand *>(command)->m_property)
93         return false;
94     // Choose older command (this) and forgot the other
95     return true;
96 }
97
98 void ModifyPropertyCommand::undo()
99 {
100     Q_ASSERT(m_view);
101
102     // Save current text in m_after for redo()
103     m_after = m_view->getCurrentValue(m_property);
104
105     // Reset text to m_before
106     m_view->changeValue(makeIndex(), m_property, m_before);
107 }
108
109 void ModifyPropertyCommand::redo()
110 {
111     // Prevent execution from within QUndoStack::push
112     if (m_after.isNull())
113         return;
114
115     // Bring back text before undo
116     Q_ASSERT(m_view);
117     m_view->changeValue(makeIndex(), m_property, m_after);
118 }
119
120 RemoveEntryCommand::RemoveEntryCommand(ResourceView *view, const QModelIndex &index)
121         : ModelIndexViewCommand(view), m_entry(0), m_isExpanded(true)
122 {
123     storeIndex(index);
124 }
125
126 RemoveEntryCommand::~RemoveEntryCommand()
127 {
128     freeEntry();
129 }
130
131 void RemoveEntryCommand::redo()
132 {
133     freeEntry();
134     const QModelIndex index = makeIndex();
135     m_isExpanded = m_view->isExpanded(index);
136     m_entry = m_view->removeEntry(index);
137 }
138
139 void RemoveEntryCommand::undo()
140 {
141     if (m_entry != 0) {
142         m_entry->restore();
143         Q_ASSERT(m_view != 0);
144         const QModelIndex index = makeIndex();
145         m_view->setExpanded(index, m_isExpanded);
146         m_view->setCurrentIndex(index);
147         freeEntry();
148     }
149 }
150
151 void RemoveEntryCommand::freeEntry()
152 {
153     delete m_entry;
154     m_entry = 0;
155 }
156
157 AddFilesCommand::AddFilesCommand(ResourceView *view, int prefixIndex, int cursorFileIndex,
158         const QStringList &fileNames)
159         : ViewCommand(view), m_prefixIndex(prefixIndex), m_cursorFileIndex(cursorFileIndex),
160         m_fileNames(fileNames)
161 { }
162
163 void AddFilesCommand::redo()
164 {
165     m_view->addFiles(m_prefixIndex, m_fileNames, m_cursorFileIndex, m_firstFile, m_lastFile);
166 }
167
168 void AddFilesCommand::undo()
169 {
170     m_view->removeFiles(m_prefixIndex, m_firstFile, m_lastFile);
171 }
172
173 AddEmptyPrefixCommand::AddEmptyPrefixCommand(ResourceView *view)
174         : ViewCommand(view)
175 { }
176
177 void AddEmptyPrefixCommand::redo()
178 {
179     m_prefixArrayIndex = m_view->addPrefix().row();
180 }
181
182 void AddEmptyPrefixCommand::undo()
183 {
184     const QModelIndex prefixModelIndex = m_view->model()->index(
185             m_prefixArrayIndex, 0, QModelIndex());
186     delete m_view->removeEntry(prefixModelIndex);
187 }
188
189 } // namespace SharedTools