1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "undocommands_p.h"
35 #include <QtCore/QModelIndex>
37 namespace SharedTools {
39 ViewCommand::ViewCommand(ResourceView *view)
43 ViewCommand::~ViewCommand()
46 ModelIndexViewCommand::ModelIndexViewCommand(ResourceView *view)
50 ModelIndexViewCommand::~ModelIndexViewCommand()
53 void ModelIndexViewCommand::storeIndex(const QModelIndex &index)
55 if (m_view->isPrefix(index)) {
56 m_prefixArrayIndex = index.row();
57 m_fileArrayIndex = -1;
59 m_fileArrayIndex = index.row();
60 m_prefixArrayIndex = m_view->model()->parent(index).row();
64 QModelIndex ModelIndexViewCommand::makeIndex() const
66 const QModelIndex prefixModelIndex
67 = m_view->model()->index(m_prefixArrayIndex, 0, QModelIndex());
68 if (m_fileArrayIndex != -1) {
70 const QModelIndex fileModelIndex
71 = m_view->model()->index(m_fileArrayIndex, 0, prefixModelIndex);
72 return fileModelIndex;
75 return prefixModelIndex;
81 ModifyPropertyCommand::ModifyPropertyCommand(ResourceView *view, const QModelIndex &nodeIndex,
82 ResourceView::NodeProperty property, const int mergeId, const QString &before,
84 : ModelIndexViewCommand(view), m_property(property), m_before(before), m_after(after),
87 storeIndex(nodeIndex);
90 bool ModifyPropertyCommand::mergeWith(const QUndoCommand * command)
92 if (command->id() != id() || m_property != static_cast<const ModifyPropertyCommand *>(command)->m_property)
94 // Choose older command (this) and forgot the other
98 void ModifyPropertyCommand::undo()
102 // Save current text in m_after for redo()
103 m_after = m_view->getCurrentValue(m_property);
105 // Reset text to m_before
106 m_view->changeValue(makeIndex(), m_property, m_before);
109 void ModifyPropertyCommand::redo()
111 // Prevent execution from within QUndoStack::push
112 if (m_after.isNull())
115 // Bring back text before undo
117 m_view->changeValue(makeIndex(), m_property, m_after);
120 RemoveEntryCommand::RemoveEntryCommand(ResourceView *view, const QModelIndex &index)
121 : ModelIndexViewCommand(view), m_entry(0), m_isExpanded(true)
126 RemoveEntryCommand::~RemoveEntryCommand()
131 void RemoveEntryCommand::redo()
134 const QModelIndex index = makeIndex();
135 m_isExpanded = m_view->isExpanded(index);
136 m_entry = m_view->removeEntry(index);
139 void RemoveEntryCommand::undo()
143 Q_ASSERT(m_view != 0);
144 const QModelIndex index = makeIndex();
145 m_view->setExpanded(index, m_isExpanded);
146 m_view->setCurrentIndex(index);
151 void RemoveEntryCommand::freeEntry()
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)
163 void AddFilesCommand::redo()
165 m_view->addFiles(m_prefixIndex, m_fileNames, m_cursorFileIndex, m_firstFile, m_lastFile);
168 void AddFilesCommand::undo()
170 m_view->removeFiles(m_prefixIndex, m_firstFile, m_lastFile);
173 AddEmptyPrefixCommand::AddEmptyPrefixCommand(ResourceView *view)
177 void AddEmptyPrefixCommand::redo()
179 m_prefixArrayIndex = m_view->addPrefix().row();
182 void AddEmptyPrefixCommand::undo()
184 const QModelIndex prefixModelIndex = m_view->model()->index(
185 m_prefixArrayIndex, 0, QModelIndex());
186 delete m_view->removeEntry(prefixModelIndex);
189 } // namespace SharedTools