diff --git a/examples/widgets/doc/images/undodemo.png b/examples/widgets/doc/images/undodemo.png deleted file mode 100644 index 85c3622738..0000000000 Binary files a/examples/widgets/doc/images/undodemo.png and /dev/null differ diff --git a/examples/widgets/doc/src/undo.qdoc b/examples/widgets/doc/src/undo.qdoc deleted file mode 100644 index 3ffe10ddf3..0000000000 --- a/examples/widgets/doc/src/undo.qdoc +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example tools/undo - \title Undo Framework - \ingroup examples-widgets-tools - - \brief This example shows Qt's undo framework in action. - - \image undodemo.png - - Qt's undo framework is an implementation of the Command - pattern, which provides advanced undo/redo functionality. - - To show the abilities of the framework, we have implemented a - small diagram application in which the diagram items are geometric - primitives. You can edit the diagram in the following ways: add, - move, change the color of, and delete the items. -*/ diff --git a/examples/widgets/doc/src/undoframework.qdoc b/examples/widgets/doc/src/undoframework.qdoc index 3d275788f6..6ca2b4b138 100644 --- a/examples/widgets/doc/src/undoframework.qdoc +++ b/examples/widgets/doc/src/undoframework.qdoc @@ -69,6 +69,9 @@ \snippet tools/undoframework/mainwindow.cpp 0 In the constructor, we set up the DiagramScene and QGraphicsView. + We only want \c deleteAction to be enabled when we have selected an + item, so we connect to the \c selectionChanged() signal of the scene + to \c updateActions() slot. Here is the \c createUndoView() function: @@ -76,13 +79,11 @@ The QUndoView is a widget that display the text, which is set with the \l{QUndoCommand::}{setText()} function, for each QUndoCommand - in the undo stack in a list. + in the undo stack in a list. We put it into a docking widget. Here is the \c createActions() function: \snippet tools/undoframework/mainwindow.cpp 2 - \codeline - \snippet tools/undoframework/mainwindow.cpp 3 \dots \snippet tools/undoframework/mainwindow.cpp 5 @@ -95,46 +96,38 @@ \l{QUndoCommand::}{text()} of the undo commands. For the other actions we have implemented slots in the \c MainWindow class. - Here is the \c createMenus() function: - + \dots \snippet tools/undoframework/mainwindow.cpp 6 - \dots + Once all actions are created we update their state by calling the + same function that is connected to the \c selectionChanged signal of + the scene. + + The \c createMenus() and \c createToolBars() functions add the + actions to menus and toolbars: + \snippet tools/undoframework/mainwindow.cpp 7 \dots \snippet tools/undoframework/mainwindow.cpp 8 - - We have to use the QMenu \c aboutToShow() and \c aboutToHide() - signals since we only want \c deleteAction to be enabled when we - have selected an item. + \dots + \snippet tools/undoframework/mainwindow.cpp 9 Here is the \c itemMoved() slot: - \snippet tools/undoframework/mainwindow.cpp 9 + \snippet tools/undoframework/mainwindow.cpp 11 We simply push a MoveCommand on the stack, which calls \c redo() on it. Here is the \c deleteItem() slot: - \snippet tools/undoframework/mainwindow.cpp 10 + \snippet tools/undoframework/mainwindow.cpp 12 - An item must be selected to be deleted. We need to check if it is + An item must be selected to be deleted. We need to check if it is selected as the \c deleteAction may be enabled even if an item is not selected. This can happen as we do not catch a signal or event when an item is selected. - Here is the \c itemMenuAboutToShow() and itemMenuAboutToHide() slots: - - \snippet tools/undoframework/mainwindow.cpp 11 - \codeline - \snippet tools/undoframework/mainwindow.cpp 12 - - We implement \c itemMenuAboutToShow() and \c itemMenuAboutToHide() - to get a dynamic item menu. These slots are connected to the - \l{QMenu::}{aboutToShow()} and \l{QMenu::}{aboutToHide()} signals. - We need this to disable or enable the \c deleteAction. - Here is the \c addBox() slot: \snippet tools/undoframework/mainwindow.cpp 13 diff --git a/examples/widgets/tools/CMakeLists.txt b/examples/widgets/tools/CMakeLists.txt index 2de15d5a44..edf951fe18 100644 --- a/examples/widgets/tools/CMakeLists.txt +++ b/examples/widgets/tools/CMakeLists.txt @@ -7,7 +7,6 @@ qt_internal_add_example(regularexpression) qt_internal_add_example(settingseditor) qt_internal_add_example(styleplugin) qt_internal_add_example(treemodelcompleter) -qt_internal_add_example(undo) qt_internal_add_example(undoframework) if(QT_FEATURE_library) diff --git a/examples/widgets/tools/tools.pro b/examples/widgets/tools/tools.pro index cd8035336e..4a9dce3412 100644 --- a/examples/widgets/tools/tools.pro +++ b/examples/widgets/tools/tools.pro @@ -8,7 +8,6 @@ SUBDIRS = \ settingseditor \ styleplugin \ treemodelcompleter \ - undo \ undoframework !qtConfig(library) { diff --git a/examples/widgets/tools/undo/CMakeLists.txt b/examples/widgets/tools/undo/CMakeLists.txt deleted file mode 100644 index f8f5dea23a..0000000000 --- a/examples/widgets/tools/undo/CMakeLists.txt +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(undo LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/undo") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) - -qt_standard_project_setup() - -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 PRIVATE - Qt6::Core - Qt6::Gui - Qt6::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" -) - -qt_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 b06050fd99..0000000000 --- a/examples/widgets/tools/undo/commands.cpp +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#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(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(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 5fc141da21..0000000000 --- a/examples/widgets/tools/undo/commands.h +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef COMMANDS_H -#define COMMANDS_H - -#include -#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 1a6d633846..0000000000 --- a/examples/widgets/tools/undo/document.cpp +++ /dev/null @@ -1,401 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "document.h" -#include "commands.h" - -#include -#include -#include -#include - -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 a3e5014bf0..0000000000 --- a/examples/widgets/tools/undo/document.h +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef DOCUMENT_H -#define DOCUMENT_H - -#include - -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 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 deleted file mode 100644 index 3bc5ed8cf0..0000000000 Binary files a/examples/widgets/tools/undo/icons/background.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/blue.png b/examples/widgets/tools/undo/icons/blue.png deleted file mode 100644 index 4e181bb61a..0000000000 Binary files a/examples/widgets/tools/undo/icons/blue.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/circle.png b/examples/widgets/tools/undo/icons/circle.png deleted file mode 100644 index ed16c6e144..0000000000 Binary files a/examples/widgets/tools/undo/icons/circle.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/exit.png b/examples/widgets/tools/undo/icons/exit.png deleted file mode 100644 index 539cb2ead9..0000000000 Binary files a/examples/widgets/tools/undo/icons/exit.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/fileclose.png b/examples/widgets/tools/undo/icons/fileclose.png deleted file mode 100644 index c5483d14ab..0000000000 Binary files a/examples/widgets/tools/undo/icons/fileclose.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/filenew.png b/examples/widgets/tools/undo/icons/filenew.png deleted file mode 100644 index 57e57e343b..0000000000 Binary files a/examples/widgets/tools/undo/icons/filenew.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/fileopen.png b/examples/widgets/tools/undo/icons/fileopen.png deleted file mode 100644 index 33e0d6394c..0000000000 Binary files a/examples/widgets/tools/undo/icons/fileopen.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/filesave.png b/examples/widgets/tools/undo/icons/filesave.png deleted file mode 100644 index 57fd5e2f34..0000000000 Binary files a/examples/widgets/tools/undo/icons/filesave.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/green.png b/examples/widgets/tools/undo/icons/green.png deleted file mode 100644 index e2e7cc9e50..0000000000 Binary files a/examples/widgets/tools/undo/icons/green.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/ok.png b/examples/widgets/tools/undo/icons/ok.png deleted file mode 100644 index e355ea91bc..0000000000 Binary files a/examples/widgets/tools/undo/icons/ok.png and /dev/null differ diff --git a/examples/widgets/tools/undo/icons/red.png b/examples/widgets/tools/undo/icons/red.png deleted file mode 100644 index 58c3e7253b..0000000000 Binary files a/examples/widgets/tools/undo/icons/red.png and /dev/null differ diff --git a/examples/widgets/tools/undo/main.cpp b/examples/widgets/tools/undo/main.cpp deleted file mode 100644 index ce28152f75..0000000000 --- a/examples/widgets/tools/undo/main.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include -#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 20d382e217..0000000000 --- a/examples/widgets/tools/undo/mainwindow.cpp +++ /dev/null @@ -1,410 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "mainwindow.h" -#include "document.h" -#include "commands.h" - -#include -#include -#include -#include -#include -#include -#include - -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(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 cda67ea196..0000000000 --- a/examples/widgets/tools/undo/mainwindow.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef MAINWINDOW_H -#define MAINWINDOW_H - -#include -#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 @@ - - MainWindow - - - - 0 - 0 - 567 - 600 - - - - - 32 - 32 - - - - - - 0 - - - 0 - - - - - 0 - - - - Tab 1 - - - - - - - - - - 0 - 0 - 567 - 27 - - - - - File - - - - - - - - - - - Edit - - - - Macros - - - - - - - - - - - - - - - - - - - Help - - - - - - - - - - - - File actions - - - Qt::Horizontal - - - TopToolBarArea - - - false - - - - - - - - - - Shape actions - - - Qt::Vertical - - - LeftToolBarArea - - - false - - - - - - - - - - - - - Undo Stack - - - 2 - - - - - 0 - - - 4 - - - - - 0 - - - 6 - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Undo limit - - - - - - - - - - - - false - - - - - - - - - :/icons/fileopen.png - - - &Open - - - - - :/icons/fileclose.png - - - &Close - - - - - :/icons/filenew.png - - - &New - - - - - :/icons/filesave.png - - - &Save - - - - - :/icons/exit.png - - - E&xit - - - - - :/icons/red.png - - - Red - - - - - :/icons/green.png - - - Green - - - - - :/icons/blue.png - - - Blue - - - - - :/icons/rectangle.png - - - Add Rectangle - - - - - :/icons/circle.png - - - Add Circle - - - - - :/icons/remove.png - - - Remove Shape - - - - - Add robot - - - - - Add snowan - - - - - :/icons/triangle.png - - - addTriangle - - - - - About - - - - - About Qt - - - - - - QUndoView - QListView -
qundoview.h
-
-
- - - - -
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 @@ - - - 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 - - diff --git a/examples/widgets/tools/undoframework/CMakeLists.txt b/examples/widgets/tools/undoframework/CMakeLists.txt index 47b4e4474a..753d4205dc 100644 --- a/examples/widgets/tools/undoframework/CMakeLists.txt +++ b/examples/widgets/tools/undoframework/CMakeLists.txt @@ -35,7 +35,12 @@ target_link_libraries(undoframework PRIVATE # Resources: set(undoframework_resource_files - "images/cross.png" + "icons/cross.png" + "icons/rectangle.png" + "icons/redo.png" + "icons/remove.png" + "icons/triangle.png" + "icons/undo.png" ) qt_add_resources(undoframework "undoframework" diff --git a/examples/widgets/tools/undoframework/images/cross.png b/examples/widgets/tools/undoframework/icons/cross.png similarity index 100% rename from examples/widgets/tools/undoframework/images/cross.png rename to examples/widgets/tools/undoframework/icons/cross.png diff --git a/examples/widgets/tools/undo/icons/rectangle.png b/examples/widgets/tools/undoframework/icons/rectangle.png similarity index 100% rename from examples/widgets/tools/undo/icons/rectangle.png rename to examples/widgets/tools/undoframework/icons/rectangle.png diff --git a/examples/widgets/tools/undo/icons/redo.png b/examples/widgets/tools/undoframework/icons/redo.png similarity index 100% rename from examples/widgets/tools/undo/icons/redo.png rename to examples/widgets/tools/undoframework/icons/redo.png diff --git a/examples/widgets/tools/undo/icons/remove.png b/examples/widgets/tools/undoframework/icons/remove.png similarity index 100% rename from examples/widgets/tools/undo/icons/remove.png rename to examples/widgets/tools/undoframework/icons/remove.png diff --git a/examples/widgets/tools/undo/icons/triangle.png b/examples/widgets/tools/undoframework/icons/triangle.png similarity index 100% rename from examples/widgets/tools/undo/icons/triangle.png rename to examples/widgets/tools/undoframework/icons/triangle.png diff --git a/examples/widgets/tools/undo/icons/undo.png b/examples/widgets/tools/undoframework/icons/undo.png similarity index 100% rename from examples/widgets/tools/undo/icons/undo.png rename to examples/widgets/tools/undoframework/icons/undo.png diff --git a/examples/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp index 6b13d0d328..c2a6c1cff6 100644 --- a/examples/widgets/tools/undoframework/mainwindow.cpp +++ b/examples/widgets/tools/undoframework/mainwindow.cpp @@ -7,70 +7,75 @@ #include "commands.h" #include +#include #include #include #include #include +#include #include //! [0] MainWindow::MainWindow() { undoStack = new QUndoStack(this); - - createActions(); - createMenus(); - - createUndoView(); - diagramScene = new DiagramScene(); - QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30)); + + const QBrush pixmapBrush(QPixmap(":/icons/cross.png").scaled(30, 30)); diagramScene->setBackgroundBrush(pixmapBrush); diagramScene->setSceneRect(QRect(0, 0, 500, 500)); + createActions(); + createMenus(); + createToolBars(); + + createUndoView(); + connect(diagramScene, &DiagramScene::itemMoved, this, &MainWindow::itemMoved); + connect(diagramScene, &DiagramScene::selectionChanged, + this, &MainWindow::updateActions); setWindowTitle("Undo Framework"); QGraphicsView *view = new QGraphicsView(diagramScene); setCentralWidget(view); - resize(700, 500); + adjustSize(); } //! [0] //! [1] void MainWindow::createUndoView() { - undoView = new QUndoView(undoStack); - undoView->setWindowTitle(tr("Command List")); - undoView->show(); - undoView->setAttribute(Qt::WA_QuitOnClose, false); + QDockWidget *undoDockWidget = new QDockWidget; + undoDockWidget->setWindowTitle(tr("Command List")); + undoDockWidget->setWidget(new QUndoView(undoStack)); + addDockWidget(Qt::RightDockWidgetArea, undoDockWidget); } //! [1] //! [2] void MainWindow::createActions() { - deleteAction = new QAction(tr("&Delete Item"), this); + deleteAction = new QAction(QIcon(":/icons/remove.png"), tr("&Delete Item"), this); deleteAction->setShortcut(tr("Del")); connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteItem); -//! [2] //! [3] +//! [2] -//! [3] //! [4] - addBoxAction = new QAction(tr("Add &Box"), this); -//! [4] + addBoxAction = new QAction(QIcon(":/icons/rectangle.png"), tr("Add &Box"), this); addBoxAction->setShortcut(tr("Ctrl+O")); connect(addBoxAction, &QAction::triggered, this, &MainWindow::addBox); - addTriangleAction = new QAction(tr("Add &Triangle"), this); + addTriangleAction = new QAction(QIcon(":/icons/triangle.png"), tr("Add &Triangle"), this); addTriangleAction->setShortcut(tr("Ctrl+T")); connect(addTriangleAction, &QAction::triggered, this, &MainWindow::addTriangle); //! [5] undoAction = undoStack->createUndoAction(this, tr("&Undo")); + undoAction->setIcon(QIcon(":/icons/undo.png")); undoAction->setShortcuts(QKeySequence::Undo); redoAction = undoStack->createRedoAction(this, tr("&Redo")); + redoAction->setIcon(QIcon(":/icons/redo.png")); redoAction->setShortcuts(QKeySequence::Redo); //! [5] @@ -83,46 +88,66 @@ void MainWindow::createActions() aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B"); aboutAction->setShortcuts(aboutShortcuts); connect(aboutAction, &QAction::triggered, this, &MainWindow::about); -} //! [6] + updateActions(); +} + +void MainWindow::updateActions() +{ + deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty()); +} +//! [6] + +//! [7] void MainWindow::createMenus() { -//! [6] fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(exitAction); -//! [7] editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(undoAction); editMenu->addAction(redoAction); editMenu->addSeparator(); editMenu->addAction(deleteAction); - connect(editMenu, &QMenu::aboutToShow, - this, &MainWindow::itemMenuAboutToShow); - connect(editMenu, &QMenu::aboutToHide, - this, &MainWindow::itemMenuAboutToHide); - //! [7] + itemMenu = menuBar()->addMenu(tr("&Item")); itemMenu->addAction(addBoxAction); itemMenu->addAction(addTriangleAction); +//! [8] helpMenu = menuBar()->addMenu(tr("&About")); helpMenu->addAction(aboutAction); -//! [8] } + +void MainWindow::createToolBars() +{ + QToolBar *editToolBar = new QToolBar; + editToolBar->addAction(undoAction); + editToolBar->addAction(redoAction); + editToolBar->addSeparator(); + editToolBar->addAction(deleteAction); //! [8] + QToolBar *itemToolBar = new QToolBar; + itemToolBar->addAction(addBoxAction); + itemToolBar->addAction(addTriangleAction); //! [9] + addToolBar(editToolBar); + addToolBar(itemToolBar); +} +//! [9] + +//! [11] void MainWindow::itemMoved(DiagramItem *movedItem, const QPointF &oldPosition) { undoStack->push(new MoveCommand(movedItem, oldPosition)); } -//! [9] +//! [11] -//! [10] +//! [12] void MainWindow::deleteItem() { if (diagramScene->selectedItems().isEmpty()) @@ -131,20 +156,6 @@ void MainWindow::deleteItem() QUndoCommand *deleteCommand = new DeleteCommand(diagramScene); undoStack->push(deleteCommand); } -//! [10] - -//! [11] -void MainWindow::itemMenuAboutToHide() -{ - deleteAction->setEnabled(true); -} -//! [11] - -//! [12] -void MainWindow::itemMenuAboutToShow() -{ - deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty()); -} //! [12] //! [13] diff --git a/examples/widgets/tools/undoframework/mainwindow.h b/examples/widgets/tools/undoframework/mainwindow.h index c635ed0172..5ef1dddf75 100644 --- a/examples/widgets/tools/undoframework/mainwindow.h +++ b/examples/widgets/tools/undoframework/mainwindow.h @@ -32,12 +32,12 @@ private slots: void addBox(); void addTriangle(); void about(); - void itemMenuAboutToShow(); - void itemMenuAboutToHide(); + void updateActions(); private: void createActions(); void createMenus(); + void createToolBars(); void createUndoView(); QAction *deleteAction = nullptr; diff --git a/examples/widgets/tools/undoframework/undoframework.qrc b/examples/widgets/tools/undoframework/undoframework.qrc index 6321d94d8d..298cc768bd 100644 --- a/examples/widgets/tools/undoframework/undoframework.qrc +++ b/examples/widgets/tools/undoframework/undoframework.qrc @@ -1,6 +1,10 @@ - images/cross.png + icons/cross.png + icons/rectangle.png + icons/redo.png + icons/remove.png + icons/triangle.png + icons/undo.png -