diff options
Diffstat (limited to 'examples/widgets/tools/undo')
27 files changed, 0 insertions, 1928 deletions
diff --git a/examples/widgets/tools/undo/CMakeLists.txt b/examples/widgets/tools/undo/CMakeLists.txt deleted file mode 100644 index 8156e12bcd..0000000000 --- a/examples/widgets/tools/undo/CMakeLists.txt +++ /dev/null @@ -1,70 +0,0 @@ -# Generated from undo.pro. - -cmake_minimum_required(VERSION 3.14) -project(undo LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/undo") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) - -qt_add_executable(undo - commands.cpp commands.h - document.cpp document.h - main.cpp - mainwindow.cpp mainwindow.h mainwindow.ui -) -set_target_properties(undo PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) -target_link_libraries(undo PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets -) - - -# Resources: -set(undo_resource_files - "icons/background.png" - "icons/blue.png" - "icons/circle.png" - "icons/exit.png" - "icons/fileclose.png" - "icons/filenew.png" - "icons/fileopen.png" - "icons/filesave.png" - "icons/green.png" - "icons/ok.png" - "icons/rectangle.png" - "icons/red.png" - "icons/redo.png" - "icons/remove.png" - "icons/triangle.png" - "icons/undo.png" -) - -qt6_add_resources(undo "undo" - PREFIX - "/" - FILES - ${undo_resource_files} -) - -install(TARGETS undo - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/tools/undo/commands.cpp b/examples/widgets/tools/undo/commands.cpp deleted file mode 100644 index 97204f9d2f..0000000000 --- a/examples/widgets/tools/undo/commands.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "commands.h" - -static constexpr int setShapeRectCommandId = 1; -static constexpr int setShapeColorCommandId = 2; - -/****************************************************************************** -** AddShapeCommand -*/ - -AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent) - : QUndoCommand(parent), m_doc(doc), m_shape(shape) -{ -} - -void AddShapeCommand::undo() -{ - m_doc->deleteShape(m_shapeName); -} - -void AddShapeCommand::redo() -{ - // A shape only gets a name when it is inserted into a document - m_shapeName = m_doc->addShape(m_shape); - setText(QObject::tr("Add %1").arg(m_shapeName)); -} - -/****************************************************************************** -** RemoveShapeCommand -*/ - -RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName, - QUndoCommand *parent) - : QUndoCommand(parent), m_doc(doc), m_shape(doc->shape(shapeName)) - , m_shapeName(shapeName) -{ - setText(QObject::tr("Remove %1").arg(shapeName)); -} - -void RemoveShapeCommand::undo() -{ - m_shapeName = m_doc->addShape(m_shape); -} - -void RemoveShapeCommand::redo() -{ - m_doc->deleteShape(m_shapeName); -} - -/****************************************************************************** -** SetShapeColorCommand -*/ - -SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName, - const QColor &color, QUndoCommand *parent) - : QUndoCommand(parent), m_doc(doc), m_shapeName(shapeName) - , m_oldColor(doc->shape(shapeName).color()), m_newColor(color) -{ - setText(QObject::tr("Set %1's color").arg(shapeName)); -} - -void SetShapeColorCommand::undo() -{ - m_doc->setShapeColor(m_shapeName, m_oldColor); -} - -void SetShapeColorCommand::redo() -{ - m_doc->setShapeColor(m_shapeName, m_newColor); -} - -bool SetShapeColorCommand::mergeWith(const QUndoCommand *command) -{ - if (command->id() != setShapeColorCommandId) - return false; - - const SetShapeColorCommand *other = static_cast<const SetShapeColorCommand*>(command); - if (m_shapeName != other->m_shapeName) - return false; - - m_newColor = other->m_newColor; - return true; -} - -int SetShapeColorCommand::id() const -{ - return setShapeColorCommandId; -} - -/****************************************************************************** -** SetShapeRectCommand -*/ - -SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName, - const QRect &rect, QUndoCommand *parent) - : QUndoCommand(parent), m_doc(doc), m_shapeName(shapeName) - , m_oldRect(doc->shape(shapeName).rect()), m_newRect(rect) -{ - setText(QObject::tr("Change %1's geometry").arg(shapeName)); -} - -void SetShapeRectCommand::undo() -{ - m_doc->setShapeRect(m_shapeName, m_oldRect); -} - -void SetShapeRectCommand::redo() -{ - m_doc->setShapeRect(m_shapeName, m_newRect); -} - -bool SetShapeRectCommand::mergeWith(const QUndoCommand *command) -{ - if (command->id() != setShapeRectCommandId) - return false; - - const SetShapeRectCommand *other = static_cast<const SetShapeRectCommand*>(command); - if (m_shapeName != other->m_shapeName) - return false; - - m_newRect = other->m_newRect; - return true; -} - -int SetShapeRectCommand::id() const -{ - return setShapeRectCommandId; -} diff --git a/examples/widgets/tools/undo/commands.h b/examples/widgets/tools/undo/commands.h deleted file mode 100644 index ccb550fdd4..0000000000 --- a/examples/widgets/tools/undo/commands.h +++ /dev/null @@ -1,123 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef COMMANDS_H -#define COMMANDS_H - -#include <QUndoCommand> -#include "document.h" - -class AddShapeCommand : public QUndoCommand -{ -public: - AddShapeCommand(Document *doc, const Shape &shape, - QUndoCommand *parent = nullptr); - void undo() override; - void redo() override; - -private: - Document *m_doc; - Shape m_shape; - QString m_shapeName; -}; - -class RemoveShapeCommand : public QUndoCommand -{ -public: - RemoveShapeCommand(Document *doc, const QString &shapeName, - QUndoCommand *parent = nullptr); - void undo() override; - void redo() override; - -private: - Document *m_doc; - Shape m_shape; - QString m_shapeName; -}; - -class SetShapeColorCommand : public QUndoCommand -{ -public: - SetShapeColorCommand(Document *doc, const QString &shapeName, - const QColor &color, QUndoCommand *parent = nullptr); - - void undo() override; - void redo() override; - - bool mergeWith(const QUndoCommand *command) override; - int id() const override; - -private: - Document *m_doc; - QString m_shapeName; - QColor m_oldColor; - QColor m_newColor; -}; - -class SetShapeRectCommand : public QUndoCommand -{ -public: - SetShapeRectCommand(Document *doc, const QString &shapeName, - const QRect &rect, QUndoCommand *parent = nullptr); - - void undo() override; - void redo() override; - - bool mergeWith(const QUndoCommand *command) override; - int id() const override; - -private: - Document *m_doc; - QString m_shapeName; - QRect m_oldRect; - QRect m_newRect; -}; - -#endif // COMMANDS_H diff --git a/examples/widgets/tools/undo/document.cpp b/examples/widgets/tools/undo/document.cpp deleted file mode 100644 index 339a63abae..0000000000 --- a/examples/widgets/tools/undo/document.cpp +++ /dev/null @@ -1,448 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "document.h" -#include "commands.h" - -#include <QPainter> -#include <QPaintEvent> -#include <QTextStream> -#include <QUndoStack> - -static constexpr int resizeHandleWidth = 6; - -/****************************************************************************** -** Shape -*/ - -const QSize Shape::minSize(80, 50); - -Shape::Shape(Type type, const QColor &color, const QRect &rect) - : m_type(type), m_rect(rect), m_color(color) -{ -} - -Shape::Type Shape::type() const -{ - return m_type; -} - -QRect Shape::rect() const -{ - return m_rect; -} - -QColor Shape::color() const -{ - return m_color; -} - -QString Shape::name() const -{ - return m_name; -} - -QRect Shape::resizeHandle() const -{ - QPoint br = m_rect.bottomRight(); - return QRect(br - QPoint(resizeHandleWidth, resizeHandleWidth), br); -} - -QString Shape::typeToString(Type type) -{ - switch (type) { - case Rectangle: - return QLatin1String("Rectangle"); - case Circle: - return QLatin1String("Circle"); - case Triangle: - return QLatin1String("Triangle"); - } - - return QString(); -} - -Shape::Type Shape::stringToType(const QString &s, bool *ok) -{ - if (ok != nullptr) - *ok = true; - - if (s == QLatin1String("Rectangle")) - return Rectangle; - if (s == QLatin1String("Circle")) - return Circle; - if (s == QLatin1String("Triangle")) - return Triangle; - - if (ok != nullptr) - *ok = false; - return Rectangle; -} - -/****************************************************************************** -** Document -*/ - -Document::Document(QWidget *parent) - : QWidget(parent), m_undoStack(new QUndoStack(this)) -{ - setAutoFillBackground(true); - setBackgroundRole(QPalette::Base); - - QPalette pal = palette(); - pal.setBrush(QPalette::Base, QPixmap(":/icons/background.png")); - pal.setColor(QPalette::HighlightedText, Qt::red); - setPalette(pal); -} - -QString Document::addShape(const Shape &shape) -{ - QString name = Shape::typeToString(shape.type()); - name = uniqueName(name); - - m_shapeList.append(shape); - m_shapeList[m_shapeList.count() - 1].m_name = name; - setCurrentShape(m_shapeList.count() - 1); - - return name; -} - -void Document::deleteShape(const QString &shapeName) -{ - int index = indexOf(shapeName); - if (index == -1) - return; - - update(m_shapeList.at(index).rect()); - - m_shapeList.removeAt(index); - - if (index <= m_currentIndex) { - m_currentIndex = -1; - if (index == m_shapeList.count()) - --index; - setCurrentShape(index); - } -} - -Shape Document::shape(const QString &shapeName) const -{ - int index = indexOf(shapeName); - if (index == -1) - return Shape(); - return m_shapeList.at(index); -} - -void Document::setShapeRect(const QString &shapeName, const QRect &rect) -{ - int index = indexOf(shapeName); - if (index == -1) - return; - - Shape &shape = m_shapeList[index]; - - update(shape.rect()); - update(rect); - - shape.m_rect = rect; -} - -void Document::setShapeColor(const QString &shapeName, const QColor &color) -{ - - int index = indexOf(shapeName); - if (index == -1) - return; - - Shape &shape = m_shapeList[index]; - shape.m_color = color; - - update(shape.rect()); -} - -QUndoStack *Document::undoStack() const -{ - return m_undoStack; -} - -bool Document::load(QTextStream &stream) -{ - m_shapeList.clear(); - - while (!stream.atEnd()) { - QString shapeType, shapeName, colorName; - int left, top, width, height; - stream >> shapeType >> shapeName >> colorName >> left >> top >> width >> height; - if (stream.status() != QTextStream::Ok) - return false; - bool ok; - Shape::Type type = Shape::stringToType(shapeType, &ok); - if (!ok) - return false; - QColor color(colorName); - if (!color.isValid()) - return false; - - Shape shape(type); - shape.m_name = shapeName; - shape.m_color = color; - shape.m_rect = QRect(left, top, width, height); - - m_shapeList.append(shape); - } - - m_currentIndex = m_shapeList.isEmpty() ? -1 : 0; - - return true; -} - -void Document::save(QTextStream &stream) -{ - for (int i = 0; i < m_shapeList.count(); ++i) { - const Shape &shape = m_shapeList.at(i); - QRect r = shape.rect(); - stream << Shape::typeToString(shape.type()) << QLatin1Char(' ') - << shape.name() << QLatin1Char(' ') - << shape.color().name() << QLatin1Char(' ') - << r.left() << QLatin1Char(' ') - << r.top() << QLatin1Char(' ') - << r.width() << QLatin1Char(' ') - << r.height(); - if (i != m_shapeList.count() - 1) - stream << QLatin1Char('\n'); - } - m_undoStack->setClean(); -} - -QString Document::fileName() const -{ - return m_fileName; -} - -void Document::setFileName(const QString &fileName) -{ - m_fileName = fileName; -} - -int Document::indexAt(const QPoint &pos) const -{ - for (int i = m_shapeList.count() - 1; i >= 0; --i) { - if (m_shapeList.at(i).rect().contains(pos)) - return i; - } - return -1; -} - -void Document::mousePressEvent(QMouseEvent *event) -{ - event->accept(); - int index = indexAt(event->position().toPoint());; - if (index != -1) { - setCurrentShape(index); - - const Shape &shape = m_shapeList.at(index); - m_resizeHandlePressed = shape.resizeHandle().contains(event->position().toPoint()); - - if (m_resizeHandlePressed) - m_mousePressOffset = shape.rect().bottomRight() - event->position().toPoint(); - else - m_mousePressOffset = event->position().toPoint() - shape.rect().topLeft(); - } - m_mousePressIndex = index; -} - -void Document::mouseReleaseEvent(QMouseEvent *event) -{ - event->accept(); - m_mousePressIndex = -1; -} - -void Document::mouseMoveEvent(QMouseEvent *event) -{ - event->accept(); - - if (m_mousePressIndex == -1) - return; - - const Shape &shape = m_shapeList.at(m_mousePressIndex); - - QRect rect; - if (m_resizeHandlePressed) { - rect = QRect(shape.rect().topLeft(), event->position().toPoint() + m_mousePressOffset); - } else { - rect = shape.rect(); - rect.moveTopLeft(event->position().toPoint() - m_mousePressOffset); - } - - QSize size = rect.size().expandedTo(Shape::minSize); - rect.setSize(size); - - m_undoStack->push(new SetShapeRectCommand(this, shape.name(), rect)); -} - -static QGradient gradient(const QColor &color, const QRect &rect) -{ - QColor c = color; - c.setAlpha(160); - QLinearGradient result(rect.topLeft(), rect.bottomRight()); - result.setColorAt(0, c.darker(150)); - result.setColorAt(0.5, c.lighter(200)); - result.setColorAt(1, c.darker(150)); - return result; -} - -static QPolygon triangle(const QRect &rect) -{ - QPolygon result(3); - result.setPoint(0, rect.center().x(), rect.top()); - result.setPoint(1, rect.right(), rect.bottom()); - result.setPoint(2, rect.left(), rect.bottom()); - return result; -} - -void Document::paintEvent(QPaintEvent *event) -{ - QRegion paintRegion = event->region(); - QPainter painter(this); - QPalette pal = palette(); - - for (int i = 0; i < m_shapeList.count(); ++i) { - const Shape &shape = m_shapeList.at(i); - - if (!paintRegion.contains(shape.rect())) - continue; - - QPen pen = pal.text().color(); - pen.setWidth(i == m_currentIndex ? 2 : 1); - painter.setPen(pen); - painter.setBrush(gradient(shape.color(), shape.rect())); - - QRect rect = shape.rect(); - rect.adjust(1, 1, -resizeHandleWidth/2, -resizeHandleWidth/2); - - // paint the shape - switch (shape.type()) { - case Shape::Rectangle: - painter.drawRect(rect); - break; - case Shape::Circle: - painter.setRenderHint(QPainter::Antialiasing); - painter.drawEllipse(rect); - painter.setRenderHint(QPainter::Antialiasing, false); - break; - case Shape::Triangle: - painter.setRenderHint(QPainter::Antialiasing); - painter.drawPolygon(triangle(rect)); - painter.setRenderHint(QPainter::Antialiasing, false); - break; - } - - // paint the resize handle - painter.setPen(pal.text().color()); - painter.setBrush(Qt::white); - painter.drawRect(shape.resizeHandle().adjusted(0, 0, -1, -1)); - - // paint the shape name - painter.setBrush(pal.text()); - if (shape.type() == Shape::Triangle) - rect.adjust(0, rect.height()/2, 0, 0); - painter.drawText(rect, Qt::AlignCenter, shape.name()); - } -} - -void Document::setCurrentShape(int index) -{ - QString currentName; - - if (m_currentIndex != -1) - update(m_shapeList.at(m_currentIndex).rect()); - - m_currentIndex = index; - - if (m_currentIndex != -1) { - const Shape ¤t = m_shapeList.at(m_currentIndex); - update(current.rect()); - currentName = current.name(); - } - - emit currentShapeChanged(currentName); -} - -int Document::indexOf(const QString &shapeName) const -{ - for (int i = 0; i < m_shapeList.count(); ++i) { - if (m_shapeList.at(i).name() == shapeName) - return i; - } - return -1; -} - -QString Document::uniqueName(const QString &name) const -{ - QString unique; - - for (int i = 0; ; ++i) { - unique = name; - if (i > 0) - unique += QString::number(i); - if (indexOf(unique) == -1) - break; - } - - return unique; -} - -QString Document::currentShapeName() const -{ - if (m_currentIndex == -1) - return QString(); - return m_shapeList.at(m_currentIndex).name(); -} - diff --git a/examples/widgets/tools/undo/document.h b/examples/widgets/tools/undo/document.h deleted file mode 100644 index 6e728ca7a9..0000000000 --- a/examples/widgets/tools/undo/document.h +++ /dev/null @@ -1,133 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef DOCUMENT_H -#define DOCUMENT_H - -#include <QWidget> - -QT_FORWARD_DECLARE_CLASS(QUndoStack) -QT_FORWARD_DECLARE_CLASS(QTextStream) - -class Shape -{ -public: - enum Type { Rectangle, Circle, Triangle }; - - explicit Shape(Type type = Rectangle, const QColor &color = Qt::red, const QRect &rect = QRect()); - - Type type() const; - QString name() const; - QRect rect() const; - QRect resizeHandle() const; - QColor color() const; - - static QString typeToString(Type type); - static Type stringToType(const QString &s, bool *ok = nullptr); - - static const QSize minSize; - -private: - Type m_type; - QRect m_rect; - QColor m_color; - QString m_name; - - friend class Document; -}; - -class Document : public QWidget -{ - Q_OBJECT - -public: - Document(QWidget *parent = nullptr); - - QString addShape(const Shape &shape); - void deleteShape(const QString &shapeName); - Shape shape(const QString &shapeName) const; - QString currentShapeName() const; - - void setShapeRect(const QString &shapeName, const QRect &rect); - void setShapeColor(const QString &shapeName, const QColor &color); - - bool load(QTextStream &stream); - void save(QTextStream &stream); - - QString fileName() const; - void setFileName(const QString &fileName); - - QUndoStack *undoStack() const; - -signals: - void currentShapeChanged(const QString &shapeName); - -protected: - void paintEvent(QPaintEvent *event) override; - void mousePressEvent(QMouseEvent *event) override; - void mouseReleaseEvent(QMouseEvent *event) override; - void mouseMoveEvent(QMouseEvent *event) override; - -private: - void setCurrentShape(int index); - int indexOf(const QString &shapeName) const; - int indexAt(const QPoint &pos) const; - QString uniqueName(const QString &name) const; - - QList<Shape> m_shapeList; - QPoint m_mousePressOffset; - QString m_fileName; - QUndoStack *m_undoStack = nullptr; - int m_currentIndex = -1; - int m_mousePressIndex = -1; - bool m_resizeHandlePressed = false; -}; - -#endif // DOCUMENT_H diff --git a/examples/widgets/tools/undo/icons/background.png b/examples/widgets/tools/undo/icons/background.png Binary files differdeleted file mode 100644 index 3bc5ed8cf0..0000000000 --- a/examples/widgets/tools/undo/icons/background.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/blue.png b/examples/widgets/tools/undo/icons/blue.png Binary files differdeleted file mode 100644 index 4e181bb61a..0000000000 --- a/examples/widgets/tools/undo/icons/blue.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/circle.png b/examples/widgets/tools/undo/icons/circle.png Binary files differdeleted file mode 100644 index ed16c6e144..0000000000 --- a/examples/widgets/tools/undo/icons/circle.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/exit.png b/examples/widgets/tools/undo/icons/exit.png Binary files differdeleted file mode 100644 index 539cb2ead9..0000000000 --- a/examples/widgets/tools/undo/icons/exit.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/fileclose.png b/examples/widgets/tools/undo/icons/fileclose.png Binary files differdeleted file mode 100644 index c5483d14ab..0000000000 --- a/examples/widgets/tools/undo/icons/fileclose.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/filenew.png b/examples/widgets/tools/undo/icons/filenew.png Binary files differdeleted file mode 100644 index 57e57e343b..0000000000 --- a/examples/widgets/tools/undo/icons/filenew.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/fileopen.png b/examples/widgets/tools/undo/icons/fileopen.png Binary files differdeleted file mode 100644 index 33e0d6394c..0000000000 --- a/examples/widgets/tools/undo/icons/fileopen.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/filesave.png b/examples/widgets/tools/undo/icons/filesave.png Binary files differdeleted file mode 100644 index 57fd5e2f34..0000000000 --- a/examples/widgets/tools/undo/icons/filesave.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/green.png b/examples/widgets/tools/undo/icons/green.png Binary files differdeleted file mode 100644 index e2e7cc9e50..0000000000 --- a/examples/widgets/tools/undo/icons/green.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/ok.png b/examples/widgets/tools/undo/icons/ok.png Binary files differdeleted file mode 100644 index e355ea91bc..0000000000 --- a/examples/widgets/tools/undo/icons/ok.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/rectangle.png b/examples/widgets/tools/undo/icons/rectangle.png Binary files differdeleted file mode 100644 index 3a7d9795fd..0000000000 --- a/examples/widgets/tools/undo/icons/rectangle.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/red.png b/examples/widgets/tools/undo/icons/red.png Binary files differdeleted file mode 100644 index 58c3e7253b..0000000000 --- a/examples/widgets/tools/undo/icons/red.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/redo.png b/examples/widgets/tools/undo/icons/redo.png Binary files differdeleted file mode 100644 index 5591517e1c..0000000000 --- a/examples/widgets/tools/undo/icons/redo.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/remove.png b/examples/widgets/tools/undo/icons/remove.png Binary files differdeleted file mode 100644 index 7a7b048c0a..0000000000 --- a/examples/widgets/tools/undo/icons/remove.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/triangle.png b/examples/widgets/tools/undo/icons/triangle.png Binary files differdeleted file mode 100644 index 2969131c31..0000000000 --- a/examples/widgets/tools/undo/icons/triangle.png +++ /dev/null diff --git a/examples/widgets/tools/undo/icons/undo.png b/examples/widgets/tools/undo/icons/undo.png Binary files differdeleted file mode 100644 index 8cf63a8ec9..0000000000 --- a/examples/widgets/tools/undo/icons/undo.png +++ /dev/null diff --git a/examples/widgets/tools/undo/main.cpp b/examples/widgets/tools/undo/main.cpp deleted file mode 100644 index a5ec1b1b83..0000000000 --- a/examples/widgets/tools/undo/main.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QApplication> -#include "mainwindow.h" - -int main(int argc, char **argv) -{ - Q_INIT_RESOURCE(undo); - - QApplication app(argc, argv); - - MainWindow win; - win.resize(800, 600); - win.show(); - - return app.exec(); -} diff --git a/examples/widgets/tools/undo/mainwindow.cpp b/examples/widgets/tools/undo/mainwindow.cpp deleted file mode 100644 index 5623a09ad2..0000000000 --- a/examples/widgets/tools/undo/mainwindow.cpp +++ /dev/null @@ -1,457 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "mainwindow.h" -#include "document.h" -#include "commands.h" - -#include <QUndoGroup> -#include <QUndoStack> -#include <QFileDialog> -#include <QMessageBox> -#include <QRandomGenerator> -#include <QTextStream> -#include <QToolButton> - -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) -{ - setupUi(this); - - QWidget *w = documentTabs->widget(0); - documentTabs->removeTab(0); - delete w; - - connect(actionOpen, &QAction::triggered, this, &MainWindow::openDocument); - connect(actionClose, &QAction::triggered, this, &MainWindow::closeDocument); - connect(actionNew, &QAction::triggered, this, &MainWindow::newDocument); - connect(actionSave, &QAction::triggered, this, &MainWindow::saveDocument); - connect(actionExit, &QAction::triggered, this, &QWidget::close); - connect(actionRed, &QAction::triggered, this, &MainWindow::setShapeColor); - connect(actionGreen, &QAction::triggered, this, &MainWindow::setShapeColor); - connect(actionBlue, &QAction::triggered, this, &MainWindow::setShapeColor); - connect(actionAddCircle, &QAction::triggered, this, &MainWindow::addShape); - connect(actionAddRectangle, &QAction::triggered, this, &MainWindow::addShape); - connect(actionAddTriangle, &QAction::triggered, this, &MainWindow::addShape); - connect(actionRemoveShape, &QAction::triggered, this, &MainWindow::removeShape); - connect(actionAddRobot, &QAction::triggered, this, &MainWindow::addRobot); - connect(actionAddSnowman, &QAction::triggered, this, &MainWindow::addSnowman); - connect(actionAbout, &QAction::triggered, this, &MainWindow::about); - connect(actionAboutQt, &QAction::triggered, this, &MainWindow::aboutQt); - - connect(undoLimit, &QSpinBox::valueChanged, this, &MainWindow::updateActions); - connect(documentTabs, &QTabWidget::currentChanged, this, &MainWindow::updateActions); - - actionOpen->setShortcut(QString("Ctrl+O")); - actionClose->setShortcut(QString("Ctrl+W")); - actionNew->setShortcut(QString("Ctrl+N")); - actionSave->setShortcut(QString("Ctrl+S")); - actionExit->setShortcut(QString("Ctrl+Q")); - actionRemoveShape->setShortcut(QString("Del")); - actionRed->setShortcut(QString("Alt+R")); - actionGreen->setShortcut(QString("Alt+G")); - actionBlue->setShortcut(QString("Alt+B")); - actionAddCircle->setShortcut(QString("Alt+C")); - actionAddRectangle->setShortcut(QString("Alt+L")); - actionAddTriangle->setShortcut(QString("Alt+T")); - - m_undoGroup = new QUndoGroup(this); - undoView->setGroup(m_undoGroup); - undoView->setCleanIcon(QIcon(":/icons/ok.png")); - - QAction *undoAction = m_undoGroup->createUndoAction(this); - QAction *redoAction = m_undoGroup->createRedoAction(this); - undoAction->setIcon(QIcon(":/icons/undo.png")); - redoAction->setIcon(QIcon(":/icons/redo.png")); - menuShape->insertAction(menuShape->actions().at(0), undoAction); - menuShape->insertAction(undoAction, redoAction); - - toolBar->addAction(undoAction); - toolBar->addAction(redoAction); - - newDocument(); - updateActions(); -}; - -void MainWindow::updateActions() -{ - Document *doc = currentDocument(); - m_undoGroup->setActiveStack(doc == nullptr ? nullptr : doc->undoStack()); - QString shapeName = doc == nullptr ? QString() : doc->currentShapeName(); - - actionAddRobot->setEnabled(doc != nullptr); - actionAddSnowman->setEnabled(doc != nullptr); - actionAddCircle->setEnabled(doc != nullptr); - actionAddRectangle->setEnabled(doc != nullptr); - actionAddTriangle->setEnabled(doc != nullptr); - actionClose->setEnabled(doc != nullptr); - actionSave->setEnabled(doc != nullptr && !doc->undoStack()->isClean()); - undoLimit->setEnabled(doc != nullptr && doc->undoStack()->count() == 0); - - if (shapeName.isEmpty()) { - actionRed->setEnabled(false); - actionGreen->setEnabled(false); - actionBlue->setEnabled(false); - actionRemoveShape->setEnabled(false); - } else { - Shape shape = doc->shape(shapeName); - actionRed->setEnabled(shape.color() != Qt::red); - actionGreen->setEnabled(shape.color() != Qt::green); - actionBlue->setEnabled(shape.color() != Qt::blue); - actionRemoveShape->setEnabled(true); - } - - if (doc != nullptr) { - int index = documentTabs->indexOf(doc); - Q_ASSERT(index != -1); - static const QIcon unsavedIcon(":/icons/filesave.png"); - documentTabs->setTabIcon(index, doc->undoStack()->isClean() ? QIcon() : unsavedIcon); - - if (doc->undoStack()->count() == 0) - doc->undoStack()->setUndoLimit(undoLimit->value()); - } -} - -void MainWindow::openDocument() -{ - QString fileName = QFileDialog::getOpenFileName(this); - if (fileName.isEmpty()) - return; - - QFile file(fileName); - if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::warning(this, - tr("File error"), - tr("Failed to open\n%1").arg(fileName)); - return; - } - QTextStream stream(&file); - - Document *doc = new Document(); - if (!doc->load(stream)) { - QMessageBox::warning(this, - tr("Parse error"), - tr("Failed to parse\n%1").arg(fileName)); - delete doc; - return; - } - - doc->setFileName(fileName); - addDocument(doc); -} - -QString MainWindow::fixedWindowTitle(const Document *doc) const -{ - QString title = doc->fileName(); - - if (title.isEmpty()) - title = tr("Unnamed"); - else - title = QFileInfo(title).fileName(); - - QString result; - - for (int i = 0; ; ++i) { - result = title; - if (i > 0) - result += QString::number(i); - - bool unique = true; - for (int j = 0; j < documentTabs->count(); ++j) { - const QWidget *widget = documentTabs->widget(j); - if (widget == doc) - continue; - if (result == documentTabs->tabText(j)) { - unique = false; - break; - } - } - - if (unique) - break; - } - - return result; -} - -void MainWindow::addDocument(Document *doc) -{ - if (documentTabs->indexOf(doc) != -1) - return; - m_undoGroup->addStack(doc->undoStack()); - documentTabs->addTab(doc, fixedWindowTitle(doc)); - connect(doc, &Document::currentShapeChanged, this, &MainWindow::updateActions); - connect(doc->undoStack(), &QUndoStack::indexChanged, this, &MainWindow::updateActions); - connect(doc->undoStack(), &QUndoStack::cleanChanged, this, &MainWindow::updateActions); - - setCurrentDocument(doc); -} - -void MainWindow::setCurrentDocument(Document *doc) -{ - documentTabs->setCurrentWidget(doc); -} - -Document *MainWindow::currentDocument() const -{ - return qobject_cast<Document*>(documentTabs->currentWidget()); -} - -void MainWindow::removeDocument(Document *doc) -{ - int index = documentTabs->indexOf(doc); - if (index == -1) - return; - - documentTabs->removeTab(index); - m_undoGroup->removeStack(doc->undoStack()); - disconnect(doc, &Document::currentShapeChanged, this, &MainWindow::updateActions); - disconnect(doc->undoStack(), &QUndoStack::indexChanged, this, &MainWindow::updateActions); - disconnect(doc->undoStack(), &QUndoStack::cleanChanged, this, &MainWindow::updateActions); - - if (documentTabs->count() == 0) { - newDocument(); - updateActions(); - } -} - -void MainWindow::saveDocument() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - for (;;) { - QString fileName = doc->fileName(); - - if (fileName.isEmpty()) - fileName = QFileDialog::getSaveFileName(this); - if (fileName.isEmpty()) - break; - - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly)) { - QMessageBox::warning(this, - tr("File error"), - tr("Failed to open\n%1").arg(fileName)); - doc->setFileName(QString()); - } else { - QTextStream stream(&file); - doc->save(stream); - doc->setFileName(fileName); - - int index = documentTabs->indexOf(doc); - Q_ASSERT(index != -1); - documentTabs->setTabText(index, fixedWindowTitle(doc)); - - break; - } - } -} - -void MainWindow::closeDocument() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - if (!doc->undoStack()->isClean()) { - int button - = QMessageBox::warning(this, - tr("Unsaved changes"), - tr("Would you like to save this document?"), - QMessageBox::Yes, QMessageBox::No); - if (button == QMessageBox::Yes) - saveDocument(); - } - - removeDocument(doc); - delete doc; -} - -void MainWindow::newDocument() -{ - addDocument(new Document()); -} - -static QColor randomColor() -{ - int r = QRandomGenerator::global()->bounded(3); - switch (r) { - case 0: - return Qt::red; - case 1: - return Qt::green; - default: - break; - } - return Qt::blue; -} - -static QRect randomRect(const QSize &s) -{ - QSize min = Shape::minSize; - - int left = qRound((s.width() - min.width()) * (QRandomGenerator::global()->bounded(1.0))); - int top = qRound((s.height() - min.height()) * (QRandomGenerator::global()->bounded(1.0))); - int width = qRound((s.width() - left - min.width()) * (QRandomGenerator::global()->bounded(1.0))) + min.width(); - int height = qRound((s.height() - top - min.height()) * (QRandomGenerator::global()->bounded(1.0))) + min.height(); - - return QRect(left, top, width, height); -} - -void MainWindow::addShape() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - Shape::Type type; - - if (sender() == actionAddCircle) - type = Shape::Circle; - else if (sender() == actionAddRectangle) - type = Shape::Rectangle; - else if (sender() == actionAddTriangle) - type = Shape::Triangle; - else return; - - Shape newShape(type, randomColor(), randomRect(doc->size())); - doc->undoStack()->push(new AddShapeCommand(doc, newShape)); -} - -void MainWindow::removeShape() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - QString shapeName = doc->currentShapeName(); - if (shapeName.isEmpty()) - return; - - doc->undoStack()->push(new RemoveShapeCommand(doc, shapeName)); -} - -void MainWindow::setShapeColor() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - QString shapeName = doc->currentShapeName(); - if (shapeName.isEmpty()) - return; - - QColor color; - - if (sender() == actionRed) - color = Qt::red; - else if (sender() == actionGreen) - color = Qt::green; - else if (sender() == actionBlue) - color = Qt::blue; - else - return; - - if (color == doc->shape(shapeName).color()) - return; - - doc->undoStack()->push(new SetShapeColorCommand(doc, shapeName, color)); -} - -void MainWindow::addSnowman() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - // Create a macro command using beginMacro() and endMacro() - - doc->undoStack()->beginMacro(tr("Add snowman")); - doc->undoStack()->push(new AddShapeCommand(doc, - Shape(Shape::Circle, Qt::blue, QRect(51, 30, 97, 95)))); - doc->undoStack()->push(new AddShapeCommand(doc, - Shape(Shape::Circle, Qt::blue, QRect(27, 123, 150, 133)))); - doc->undoStack()->push(new AddShapeCommand(doc, - Shape(Shape::Circle, Qt::blue, QRect(11, 253, 188, 146)))); - doc->undoStack()->endMacro(); -} - -void MainWindow::addRobot() -{ - Document *doc = currentDocument(); - if (doc == nullptr) - return; - - // Compose a macro command by explicitly adding children to a parent command - - QUndoCommand *parent = new QUndoCommand(tr("Add robot")); - - new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(115, 15, 81, 70)), parent); - new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(82, 89, 148, 188)), parent); - new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(76, 280, 80, 165)), parent); - new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(163, 280, 80, 164)), parent); - new AddShapeCommand(doc, Shape(Shape::Circle, Qt::blue, QRect(116, 25, 80, 50)), parent); - new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(232, 92, 80, 127)), parent); - new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(2, 92, 80, 125)), parent); - - doc->undoStack()->push(parent); -} - -void MainWindow::about() -{ - QMessageBox::about(this, tr("About Undo"), tr("The Undo demonstration shows how to use the Qt Undo framework.")); -} - -void MainWindow::aboutQt() -{ - QMessageBox::aboutQt(this, tr("About Qt")); -} diff --git a/examples/widgets/tools/undo/mainwindow.h b/examples/widgets/tools/undo/mainwindow.h deleted file mode 100644 index 87ee3084fb..0000000000 --- a/examples/widgets/tools/undo/mainwindow.h +++ /dev/null @@ -1,96 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef MAINWINDOW_H -#define MAINWINDOW_H - -#include <QMainWindow> -#include "ui_mainwindow.h" - -class Document; - -class MainWindow : public QMainWindow, public Ui::MainWindow -{ - Q_OBJECT - -public: - MainWindow(QWidget *parent = nullptr); - - void addDocument(Document *doc); - void removeDocument(Document *doc); - void setCurrentDocument(Document *doc); - Document *currentDocument() const; - -public slots: - void openDocument(); - void saveDocument(); - void closeDocument(); - void newDocument(); - - void addShape(); - void removeShape(); - void setShapeColor(); - - void addSnowman(); - void addRobot(); - - void about(); - void aboutQt(); - -private slots: - void updateActions(); - -private: - QUndoGroup *m_undoGroup; - - QString fixedWindowTitle(const Document *doc) const; -}; - -#endif // MAINWINDOW_H diff --git a/examples/widgets/tools/undo/mainwindow.ui b/examples/widgets/tools/undo/mainwindow.ui deleted file mode 100644 index 91a0b437e5..0000000000 --- a/examples/widgets/tools/undo/mainwindow.ui +++ /dev/null @@ -1,322 +0,0 @@ -<ui version="4.0" > - <class>MainWindow</class> - <widget class="QMainWindow" name="MainWindow" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>567</width> - <height>600</height> - </rect> - </property> - <property name="iconSize" > - <size> - <width>32</width> - <height>32</height> - </size> - </property> - <widget class="QWidget" name="centralwidget" > - <layout class="QVBoxLayout" > - <property name="margin" > - <number>0</number> - </property> - <property name="spacing" > - <number>0</number> - </property> - <item> - <widget class="QTabWidget" name="documentTabs" > - <property name="currentIndex" > - <number>0</number> - </property> - <widget class="QWidget" name="tab" > - <attribute name="title" > - <string>Tab 1</string> - </attribute> - </widget> - </widget> - </item> - </layout> - </widget> - <widget class="QMenuBar" name="menubar" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>567</width> - <height>27</height> - </rect> - </property> - <widget class="QMenu" name="menuFile" > - <property name="title" > - <string>File</string> - </property> - <addaction name="actionNew" /> - <addaction name="actionOpen" /> - <addaction name="actionSave" /> - <addaction name="actionClose" /> - <addaction name="separator" /> - <addaction name="actionExit" /> - </widget> - <widget class="QMenu" name="menuShape" > - <property name="title" > - <string>Edit</string> - </property> - <widget class="QMenu" name="menuMacros" > - <property name="title" > - <string>Macros</string> - </property> - <addaction name="actionAddRobot" /> - <addaction name="actionAddSnowman" /> - </widget> - <addaction name="separator" /> - <addaction name="actionAddCircle" /> - <addaction name="actionAddRectangle" /> - <addaction name="actionAddTriangle" /> - <addaction name="actionRemoveShape" /> - <addaction name="separator" /> - <addaction name="actionRed" /> - <addaction name="actionGreen" /> - <addaction name="actionBlue" /> - <addaction name="separator" /> - <addaction name="menuMacros" /> - </widget> - <widget class="QMenu" name="menuHelp" > - <property name="title" > - <string>Help</string> - </property> - <addaction name="actionAbout" /> - <addaction name="actionAboutQt" /> - </widget> - <addaction name="menuFile" /> - <addaction name="menuShape" /> - <addaction name="menuHelp" /> - </widget> - <widget class="QStatusBar" name="statusbar" /> - <widget class="QToolBar" name="toolBar" > - <property name="windowTitle" > - <string>File actions</string> - </property> - <property name="orientation" > - <enum>Qt::Horizontal</enum> - </property> - <attribute name="toolBarArea" > - <enum>TopToolBarArea</enum> - </attribute> - <attribute name="toolBarBreak" > - <bool>false</bool> - </attribute> - <addaction name="actionNew" /> - <addaction name="actionOpen" /> - <addaction name="actionSave" /> - <addaction name="actionClose" /> - <addaction name="separator" /> - </widget> - <widget class="QToolBar" name="shapeToolBar" > - <property name="windowTitle" > - <string>Shape actions</string> - </property> - <property name="orientation" > - <enum>Qt::Vertical</enum> - </property> - <attribute name="toolBarArea" > - <enum>LeftToolBarArea</enum> - </attribute> - <attribute name="toolBarBreak" > - <bool>false</bool> - </attribute> - <addaction name="actionAddRectangle" /> - <addaction name="actionAddCircle" /> - <addaction name="actionAddTriangle" /> - <addaction name="actionRemoveShape" /> - <addaction name="separator" /> - <addaction name="actionRed" /> - <addaction name="actionGreen" /> - <addaction name="actionBlue" /> - </widget> - <widget class="QDockWidget" name="dockWidget" > - <property name="windowTitle" > - <string>Undo Stack</string> - </property> - <attribute name="dockWidgetArea" > - <number>2</number> - </attribute> - <widget class="QWidget" name="dockWidgetContents" > - <layout class="QVBoxLayout" > - <property name="margin" > - <number>0</number> - </property> - <property name="spacing" > - <number>4</number> - </property> - <item> - <layout class="QHBoxLayout" > - <property name="margin" > - <number>0</number> - </property> - <property name="spacing" > - <number>6</number> - </property> - <item> - <spacer> - <property name="orientation" > - <enum>Qt::Horizontal</enum> - </property> - <property name="sizeHint" > - <size> - <width>40</width> - <height>20</height> - </size> - </property> - </spacer> - </item> - <item> - <widget class="QLabel" name="label" > - <property name="text" > - <string>Undo limit</string> - </property> - </widget> - </item> - <item> - <widget class="QSpinBox" name="undoLimit" /> - </item> - </layout> - </item> - <item> - <widget class="QUndoView" name="undoView" > - <property name="alternatingRowColors" > - <bool>false</bool> - </property> - </widget> - </item> - </layout> - </widget> - </widget> - <action name="actionOpen" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/fileopen.png</iconset> - </property> - <property name="text" > - <string>&Open</string> - </property> - </action> - <action name="actionClose" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/fileclose.png</iconset> - </property> - <property name="text" > - <string>&Close</string> - </property> - </action> - <action name="actionNew" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/filenew.png</iconset> - </property> - <property name="text" > - <string>&New</string> - </property> - </action> - <action name="actionSave" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/filesave.png</iconset> - </property> - <property name="text" > - <string>&Save</string> - </property> - </action> - <action name="actionExit" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/exit.png</iconset> - </property> - <property name="text" > - <string>E&xit</string> - </property> - </action> - <action name="actionRed" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/red.png</iconset> - </property> - <property name="text" > - <string>Red</string> - </property> - </action> - <action name="actionGreen" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/green.png</iconset> - </property> - <property name="text" > - <string>Green</string> - </property> - </action> - <action name="actionBlue" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/blue.png</iconset> - </property> - <property name="text" > - <string>Blue</string> - </property> - </action> - <action name="actionAddRectangle" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/rectangle.png</iconset> - </property> - <property name="text" > - <string>Add Rectangle</string> - </property> - </action> - <action name="actionAddCircle" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/circle.png</iconset> - </property> - <property name="text" > - <string>Add Circle</string> - </property> - </action> - <action name="actionRemoveShape" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/remove.png</iconset> - </property> - <property name="text" > - <string>Remove Shape</string> - </property> - </action> - <action name="actionAddRobot" > - <property name="text" > - <string>Add robot</string> - </property> - </action> - <action name="actionAddSnowman" > - <property name="text" > - <string>Add snowan</string> - </property> - </action> - <action name="actionAddTriangle" > - <property name="icon" > - <iconset resource="undo.qrc" >:/icons/triangle.png</iconset> - </property> - <property name="text" > - <string>addTriangle</string> - </property> - </action> - <action name="actionAbout" > - <property name="text" > - <string>About</string> - </property> - </action> - <action name="actionAboutQt" > - <property name="text" > - <string>About Qt</string> - </property> - </action> - </widget> - <customwidgets> - <customwidget> - <class>QUndoView</class> - <extends>QListView</extends> - <header>qundoview.h</header> - </customwidget> - </customwidgets> - <resources> - <include location="undo.qrc" /> - </resources> - <connections/> -</ui> diff --git a/examples/widgets/tools/undo/undo.pro b/examples/widgets/tools/undo/undo.pro deleted file mode 100644 index 9b7b577dd3..0000000000 --- a/examples/widgets/tools/undo/undo.pro +++ /dev/null @@ -1,17 +0,0 @@ -QT += widgets -requires(qtConfig(undoview)) - -SOURCES += main.cpp mainwindow.cpp commands.cpp document.cpp -HEADERS += mainwindow.h commands.h document.h -FORMS += mainwindow.ui - -build_all:!build_pass { - CONFIG -= build_all - CONFIG += release -} - -RESOURCES += undo.qrc - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tools/undo -INSTALLS += target diff --git a/examples/widgets/tools/undo/undo.qrc b/examples/widgets/tools/undo/undo.qrc deleted file mode 100644 index 65619b8f1a..0000000000 --- a/examples/widgets/tools/undo/undo.qrc +++ /dev/null @@ -1,20 +0,0 @@ -<RCC> - <qresource prefix="/" > - <file>icons/background.png</file> - <file>icons/blue.png</file> - <file>icons/circle.png</file> - <file>icons/exit.png</file> - <file>icons/fileclose.png</file> - <file>icons/filenew.png</file> - <file>icons/fileopen.png</file> - <file>icons/filesave.png</file> - <file>icons/green.png</file> - <file>icons/ok.png</file> - <file>icons/rectangle.png</file> - <file>icons/red.png</file> - <file>icons/redo.png</file> - <file>icons/remove.png</file> - <file>icons/triangle.png</file> - <file>icons/undo.png</file> - </qresource> -</RCC> |