Files
qt-creator/shared/proparser/procommandmanager.cpp

167 lines
3.6 KiB
C++
Raw Normal View History

2008-12-02 12:01:29 +01:00
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
2008-12-02 12:01:29 +01:00
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
2008-12-02 12:01:29 +01:00
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
2008-12-02 12:01:29 +01:00
#include "procommandmanager.h"
2008-12-09 15:25:01 +01:00
#include <utils/qtcassert.h>
2008-12-02 12:01:29 +01:00
using namespace Qt4ProjectManager::Internal;
ProCommandGroup::ProCommandGroup(const QString &name)
: m_name(name)
{ }
2008-12-02 12:01:29 +01:00
ProCommandGroup::~ProCommandGroup()
{
qDeleteAll(m_commands);
}
void ProCommandGroup::appendCommand(ProCommand *cmd)
{
m_commands.append(cmd);
}
void ProCommandGroup::undo()
{
for (int i = m_commands.count(); i > 0; --i)
2008-12-02 12:01:29 +01:00
m_commands[i-1]->undo();
}
void ProCommandGroup::redo()
{
for (int i = 0; i < m_commands.count(); ++i)
2008-12-02 12:01:29 +01:00
m_commands[i]->redo();
}
ProCommandManager::ProCommandManager(QObject *parent)
: QObject(parent)
{
m_savepoint = 0;
m_pos = 0;
m_group = 0;
}
ProCommandManager::~ProCommandManager()
{
qDeleteAll(m_groups);
}
void ProCommandManager::beginGroup(const QString &name)
{
2008-12-09 15:25:01 +01:00
QTC_ASSERT(!m_group, return);
2008-12-02 12:01:29 +01:00
if (m_pos != m_groups.count()) {
int removecount = m_groups.count() - m_pos;
for (int i = 0; i < removecount; ++i)
2008-12-02 12:01:29 +01:00
delete m_groups.takeLast();
m_pos = m_groups.count();
}
m_group = new ProCommandGroup(name);
}
bool ProCommandManager::hasGroup() const
{
return m_group != 0;
2008-12-02 12:01:29 +01:00
}
void ProCommandManager::endGroup()
{
2008-12-09 15:25:01 +01:00
QTC_ASSERT(m_group, return);
2008-12-02 12:01:29 +01:00
m_groups.append(m_group);
m_pos = m_groups.count();
m_group = 0;
emit modified();
}
bool ProCommandManager::command(ProCommand *cmd)
{
2008-12-09 15:25:01 +01:00
QTC_ASSERT(m_group, return false);
2008-12-02 12:01:29 +01:00
if (cmd->redo()) {
m_group->appendCommand(cmd);
return true;
}
return false;
}
void ProCommandManager::undo()
{
if (canUndo()) {
--m_pos;
m_groups[m_pos]->undo();
}
emit modified();
}
void ProCommandManager::redo()
{
if (canRedo()) {
m_groups[m_pos]->redo();
++m_pos;
}
emit modified();
}
bool ProCommandManager::isDirty() const
{
if (m_groups.isEmpty())
return false;
if (m_pos != 0 && m_groups.at(m_pos - 1) == m_savepoint)
return false;
return true;
}
void ProCommandManager::notifySave()
{
if (m_pos > 0)
m_savepoint = m_groups.at(m_pos - 1);
}
bool ProCommandManager::canUndo() const
{
return !m_groups.isEmpty() && m_pos > 0;
2008-12-02 12:01:29 +01:00
}
bool ProCommandManager::canRedo() const
{
return m_groups.count() > m_pos;
2008-12-02 12:01:29 +01:00
}