diff options
Diffstat (limited to 'src/imports')
-rw-r--r-- | src/imports/imports.pro | 3 | ||||
-rw-r--r-- | src/imports/undo/plugin.cpp | 68 | ||||
-rw-r--r-- | src/imports/undo/qmldir | 1 | ||||
-rw-r--r-- | src/imports/undo/uiquickundocommands.cpp | 147 | ||||
-rw-r--r-- | src/imports/undo/uiquickundocommands_p.h | 134 | ||||
-rw-r--r-- | src/imports/undo/uiquickundostack.cpp | 100 | ||||
-rw-r--r-- | src/imports/undo/uiquickundostack_p.h | 84 | ||||
-rw-r--r-- | src/imports/undo/undo.pro | 24 |
8 files changed, 561 insertions, 0 deletions
diff --git a/src/imports/imports.pro b/src/imports/imports.pro new file mode 100644 index 0000000..6abb48b --- /dev/null +++ b/src/imports/imports.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS += undo diff --git a/src/imports/undo/plugin.cpp b/src/imports/undo/plugin.cpp new file mode 100644 index 0000000..0345e23 --- /dev/null +++ b/src/imports/undo/plugin.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the UiHelpers playground module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 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 the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtQml/qqmlextensionplugin.h> + +#include "uiquickundostack_p.h" +#include "uiquickundocommands_p.h" + + +class QmlUndoFrameworkPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + +public: + virtual void registerTypes(const char* uri); +}; + +void QmlUndoFrameworkPlugin::registerTypes(const char* uri) +{ + Q_ASSERT(QLatin1String(uri) == QLatin1String("Playground.UiHelpers.UndoFramework")); + + qmlRegisterType<UiQuickUndoStack>(uri, 1, 0, "UndoStack"); + qmlRegisterUncreatableType<UiQuickBaseUndoCommand>(uri, 1, 0, "", ""); + qmlRegisterType<UiQuickUndoCommand>(uri, 1, 0, "UndoCommand"); + qmlRegisterType<UiQuickUndoPropertyCommand>(uri, 1, 0, "UndoPropertyCommand"); +} + +#include "plugin.moc" + +Q_EXPORT_PLUGIN2(qmlundoframeworkplugin, QT_PREPEND_NAMESPACE(QmlUndoFrameworkPlugin)) diff --git a/src/imports/undo/qmldir b/src/imports/undo/qmldir new file mode 100644 index 0000000..e04cfa3 --- /dev/null +++ b/src/imports/undo/qmldir @@ -0,0 +1 @@ +plugin qmlundoframeworkplugin diff --git a/src/imports/undo/uiquickundocommands.cpp b/src/imports/undo/uiquickundocommands.cpp new file mode 100644 index 0000000..8130f9f --- /dev/null +++ b/src/imports/undo/uiquickundocommands.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the UiHelpers playground module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 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 the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "uiquickundocommands_p.h" + + +UiQuickBaseUndoCommand::UiQuickBaseUndoCommand(QObject *parent) + : QObject(parent) +{ +} + +UiQuickBaseUndoCommand::~UiQuickBaseUndoCommand() +{ +} + +UiQuickUndoCommand::UiQuickUndoCommand(QObject *parent) + : UiQuickBaseUndoCommand(parent) +{ +} + +UiQuickUndoCommand::~UiQuickUndoCommand() +{ +} + +UndoCommand::UndoCommand(QObject* target, UiQuickUndoCommand *qmlObject) + : UiUndoCommand() + , m_target(target) + , m_qmlObject(qmlObject) +{ +} + +UndoCommand::~UndoCommand() +{ + emit m_qmlObject->commandDestroyed(m_target); +} + +void UndoCommand::undo() +{ + emit m_qmlObject->undo(m_target); +} + +void UndoCommand::redo() +{ + emit m_qmlObject->redo(m_target); +} + +// --------------------------------- // + +UiQuickUndoPropertyCommand::UiQuickUndoPropertyCommand(QObject *parent) + : UiQuickBaseUndoCommand(parent) + , m_properties(QVariantList()) +{ +} + +UiQuickUndoPropertyCommand::~UiQuickUndoPropertyCommand() +{ +} + +QVariantList UiQuickUndoPropertyCommand::properties() const +{ + return m_properties; +} + +void UiQuickUndoPropertyCommand::setProperties(const QVariantList& prop) +{ + m_properties = prop; + emit propertiesChanged(); +} + +UndoPropertyCommand::UndoPropertyCommand(QObject* t, UiQuickUndoPropertyCommand *q) + : UiUndoCommand() + , m_undoState(TargetState()) + , m_redoState(TargetState()) + , m_target(t) + , m_qmlObject(q) +{ + saveState(m_undoState); +} + +UndoPropertyCommand::~UndoPropertyCommand() +{ +} + +void UndoPropertyCommand::saveState(TargetState& state) +{ + foreach (const QVariant& var, m_qmlObject->properties()) { + QByteArray propertyName = var.toByteArray(); + state.append(qMakePair(propertyName, m_target->property(propertyName.data()))); + } +} + +void UndoPropertyCommand::applyState(TargetState& state) +{ + foreach (PropertyState pair, state) + m_target->setProperty(pair.first, pair.second); +} + +void UndoPropertyCommand::undo() +{ + applyState(m_undoState); +} + +void UndoPropertyCommand::redo() +{ + if (m_redoState.empty()) + saveState(m_redoState); + else + applyState(m_redoState); +} diff --git a/src/imports/undo/uiquickundocommands_p.h b/src/imports/undo/uiquickundocommands_p.h new file mode 100644 index 0000000..f5d46c2 --- /dev/null +++ b/src/imports/undo/uiquickundocommands_p.h @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the UiHelpers playground module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 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 the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef UIQUICKUNDOCOMMAND_H +#define UIQUICKUNDOCOMMAND_H + +#include <UiHelpers/UiUndoStack> +#include <QVariant> + +QT_USE_NAMESPACE_UIHELPERS; + +class UiQuickBaseUndoCommand : public QObject +{ + Q_OBJECT + +public: + UiQuickBaseUndoCommand(QObject *parent = 0); + ~UiQuickBaseUndoCommand(); +}; + +// ------- // + +class UiQuickUndoCommand : public UiQuickBaseUndoCommand +{ + Q_OBJECT + +public: + UiQuickUndoCommand(QObject *parent = 0); + ~UiQuickUndoCommand(); + +signals: + void undo(QObject *target); + void redo(QObject *target); + void commandDestroyed(QObject *target); +}; + +class UndoCommand : public UiUndoCommand +{ +public: + UndoCommand(QObject* target, UiQuickUndoCommand *m_qmlObject); + ~UndoCommand(); + + void undo(); + void redo(); + +private: + QObject *m_target; + UiQuickUndoCommand *m_qmlObject; +}; + +// -----------------// + +class UiQuickUndoPropertyCommand : public UiQuickBaseUndoCommand +{ + Q_OBJECT + + Q_PROPERTY(QVariantList properties READ properties WRITE setProperties NOTIFY propertiesChanged) + +public: + UiQuickUndoPropertyCommand(QObject *parent = 0); + ~UiQuickUndoPropertyCommand(); + + QVariantList properties() const; + void setProperties(const QVariantList& prop); + +signals: + void propertiesChanged(); + +private: + QVariantList m_properties; +}; + +typedef QPair<QByteArray, QVariant> PropertyState; +typedef QList<PropertyState> TargetState; + +class UndoPropertyCommand : public UiUndoCommand +{ + +public: + UndoPropertyCommand(QObject*, UiQuickUndoPropertyCommand*); + ~UndoPropertyCommand(); + + void undo(); + void redo(); + +private: + void saveState(TargetState& state); + void applyState(TargetState& state); + + TargetState m_undoState; + TargetState m_redoState; + QObject *m_target; + UiQuickUndoPropertyCommand *m_qmlObject; +}; + +#endif diff --git a/src/imports/undo/uiquickundostack.cpp b/src/imports/undo/uiquickundostack.cpp new file mode 100644 index 0000000..97adf9f --- /dev/null +++ b/src/imports/undo/uiquickundostack.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the UiHelpers playground module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 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 the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "uiquickundostack_p.h" +#include "uiquickundocommands_p.h" + +UiQuickUndoStack::UiQuickUndoStack(QObject *parent) + : QObject(parent) + , m_stack(new UndoStack(this)) +{ +} + +UiQuickUndoStack::~UiQuickUndoStack() +{ +} + +UndoStack::UndoStack(QObject *parent) + : UiUndoStack(parent) + , currentCommand(0) +{ +} + +UndoStack::~UndoStack() +{ +} + +void UiQuickUndoStack::push(UiQuickBaseUndoCommand *cmd, QObject *target) +{ + if (!cmd || !target) + return; // XXX: notify error + + m_stack->commit(); + + UiQuickUndoPropertyCommand *upc = qobject_cast<UiQuickUndoPropertyCommand *>(cmd); + if (upc) { + m_stack->currentCommand = new UndoPropertyCommand(target, upc); + } else { + UiQuickUndoCommand *uc = qobject_cast<UiQuickUndoCommand *>(cmd); + m_stack->push(new UndoCommand(target, uc)); + } +} + +void UiQuickUndoStack::undo() +{ + m_stack->commit(); + m_stack->undo(); +} + +void UiQuickUndoStack::redo() +{ + m_stack->commit(); + m_stack->redo(); +} + +void UndoStack::commit() +{ + if (!currentCommand) + return; + + push(currentCommand); + currentCommand = 0; +} diff --git a/src/imports/undo/uiquickundostack_p.h b/src/imports/undo/uiquickundostack_p.h new file mode 100644 index 0000000..f7b35d5 --- /dev/null +++ b/src/imports/undo/uiquickundostack_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the UiHelpers playground module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 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 the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef UIQUICKUNDOSTACK_H +#define UIQUICKUNDOSTACK_H + +#include <QtQml/QQmlComponent> +#include <UiHelpers/UiUndoStack> + + +QT_USE_NAMESPACE_UIHELPERS; + +class UndoStack; +class UiQuickBaseUndoCommand; + +class UiQuickUndoStack : public QObject +{ + Q_OBJECT + +public: + UiQuickUndoStack(QObject *parent = 0); + ~UiQuickUndoStack(); + +public slots: + void push(UiQuickBaseUndoCommand *cmd, QObject *target); + void undo(); + void redo(); + +private: + UndoStack *m_stack; +}; + +class UndoStack : public UiUndoStack +{ + Q_OBJECT + +public: + UndoStack(QObject *parent = 0); + ~UndoStack(); + + void commit(); + + UiUndoCommand *currentCommand; +}; + +#endif diff --git a/src/imports/undo/undo.pro b/src/imports/undo/undo.pro new file mode 100644 index 0000000..b9289e7 --- /dev/null +++ b/src/imports/undo/undo.pro @@ -0,0 +1,24 @@ +TEMPLATE = lib +TARGET = qmlundoframeworkplugin +TARGETPATH = Playground/UiHelpers/UndoFramework + +QT += qml uihelpers + +CONFIG += qt plugin + +SOURCES += plugin.cpp + +SOURCES += uiquickundocommands.cpp \ + uiquickundostack.cpp + +HEADERS += uiquickundocommands_p.h \ + uiquickundostack_p.h + + +DESTDIR = $$QT.qml.imports/$$TARGETPATH +target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH + +qmldir.files += $$PWD/qmldir +qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH + +INSTALLS += target qmldir |