diff options
author | Volker Hilsheimer <[email protected]> | 2023-01-09 13:55:09 +0100 |
---|---|---|
committer | Jan Arve Sæther <[email protected]> | 2023-01-31 00:20:31 +0100 |
commit | cb0bf5ad683b72d4ecb0d21dafde3a5acdaac800 (patch) | |
tree | d16f765a9d41bf178c7bcc0bccccf0fe59ac1fee /examples/widgets/tools | |
parent | 26f8ea1224e43bef9ba3f121c6d5d8e19a8756fc (diff) |
Merge "undo" and "undoframework" examples
The "undo" example didn't show anything that the "undoframework"
example doesn't, and the latter is more comprehensive and properly
documented. "undoframework" also uses QGraphicsView instead of
inventing its own diagram widget.
However, the "undo" example created a nicer UI with toolbuttons,
icons, and the undo view in a dock widget, so reuse those elements
in the "undoframework" example instead.
Update the documentation quoting tags accordingly, and clean up
a bit.
Pick-to: 6.5
Change-Id: I3c91feecbd5fe3e5900838b0b51f9fe7bd190280
Reviewed-by: Jan Arve Sæther <[email protected]>
Diffstat (limited to 'examples/widgets/tools')
34 files changed, 65 insertions, 1644 deletions
diff --git a/examples/widgets/tools/CMakeLists.txt b/examples/widgets/tools/CMakeLists.txt index 2de15d5a444..edf951fe189 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 cd8035336e4..4a9dce34129 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 f8f5dea23a3..00000000000 --- 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 b06050fd998..00000000000 --- 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<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 5fc141da21d..00000000000 --- 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 <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 1a6d6338468..00000000000 --- 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 <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 a3e5014bf03..00000000000 --- 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 <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 3bc5ed8cf01..00000000000 --- 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 4e181bb61a6..00000000000 --- 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 ed16c6e1446..00000000000 --- 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 539cb2ead93..00000000000 --- 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 c5483d14ab7..00000000000 --- 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 57e57e343ba..00000000000 --- 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 33e0d6394c7..00000000000 --- 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 57fd5e2f343..00000000000 --- 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 e2e7cc9e501..00000000000 --- 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 e355ea91bc5..00000000000 --- a/examples/widgets/tools/undo/icons/ok.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 58c3e7253bd..00000000000 --- a/examples/widgets/tools/undo/icons/red.png +++ /dev/null diff --git a/examples/widgets/tools/undo/main.cpp b/examples/widgets/tools/undo/main.cpp deleted file mode 100644 index ce28152f75a..00000000000 --- 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 <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 20d382e217d..00000000000 --- 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 <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 cda67ea1967..00000000000 --- 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 <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 91a0b437e58..00000000000 --- 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 9b7b577dd32..00000000000 --- 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 65619b8f1af..00000000000 --- 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> diff --git a/examples/widgets/tools/undoframework/CMakeLists.txt b/examples/widgets/tools/undoframework/CMakeLists.txt index 47b4e4474a4..753d4205dcb 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 Binary files differindex 3570a242ae0..3570a242ae0 100644 --- a/examples/widgets/tools/undoframework/images/cross.png +++ b/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 Binary files differindex 3a7d9795fd2..3a7d9795fd2 100644 --- a/examples/widgets/tools/undo/icons/rectangle.png +++ b/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 Binary files differindex 5591517e1ca..5591517e1ca 100644 --- a/examples/widgets/tools/undo/icons/redo.png +++ b/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 Binary files differindex 7a7b048c0a6..7a7b048c0a6 100644 --- a/examples/widgets/tools/undo/icons/remove.png +++ b/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 Binary files differindex 2969131c319..2969131c319 100644 --- a/examples/widgets/tools/undo/icons/triangle.png +++ b/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 Binary files differindex 8cf63a8ec95..8cf63a8ec95 100644 --- a/examples/widgets/tools/undo/icons/undo.png +++ b/examples/widgets/tools/undoframework/icons/undo.png diff --git a/examples/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp index 6b13d0d328b..c2a6c1cff6f 100644 --- a/examples/widgets/tools/undoframework/mainwindow.cpp +++ b/examples/widgets/tools/undoframework/mainwindow.cpp @@ -7,70 +7,75 @@ #include "commands.h" #include <QAction> +#include <QDockWidget> #include <QGraphicsView> #include <QMenu> #include <QMenuBar> #include <QMessageBox> +#include <QToolBar> #include <QUndoView> //! [0] MainWindow::MainWindow() { undoStack = new QUndoStack(this); + diagramScene = new DiagramScene(); + + const QBrush pixmapBrush(QPixmap(":/icons/cross.png").scaled(30, 30)); + diagramScene->setBackgroundBrush(pixmapBrush); + diagramScene->setSceneRect(QRect(0, 0, 500, 500)); createActions(); createMenus(); + createToolBars(); createUndoView(); - diagramScene = new DiagramScene(); - QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30)); - diagramScene->setBackgroundBrush(pixmapBrush); - diagramScene->setSceneRect(QRect(0, 0, 500, 500)); - 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 c635ed01728..5ef1dddf751 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 6321d94d8d6..298cc768bdb 100644 --- a/examples/widgets/tools/undoframework/undoframework.qrc +++ b/examples/widgets/tools/undoframework/undoframework.qrc @@ -1,6 +1,10 @@ <!DOCTYPE RCC><RCC version="1.0"> <qresource> - <file>images/cross.png</file> + <file>icons/cross.png</file> + <file>icons/rectangle.png</file> + <file>icons/redo.png</file> + <file>icons/remove.png</file> + <file>icons/triangle.png</file> + <file>icons/undo.png</file> </qresource> </RCC> - |