qt 6.5.1 original

This commit is contained in:
kleuter
2023-10-29 23:33:08 +01:00
parent 71d22ab6b0
commit 85d238dfda
21202 changed files with 5499099 additions and 0 deletions

View File

@ -0,0 +1,57 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(undoframework LANGUAGES CXX)
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/undoframework")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
qt_standard_project_setup()
qt_add_executable(undoframework
commands.cpp commands.h
diagramitem.cpp diagramitem.h
diagramscene.cpp diagramscene.h
main.cpp
mainwindow.cpp mainwindow.h
)
set_target_properties(undoframework PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
target_link_libraries(undoframework PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Widgets
)
# Resources:
set(undoframework_resource_files
"icons/cross.png"
"icons/rectangle.png"
"icons/redo.png"
"icons/remove.png"
"icons/triangle.png"
"icons/undo.png"
)
qt_add_resources(undoframework "undoframework"
PREFIX
"/"
FILES
${undoframework_resource_files}
)
install(TARGETS undoframework
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

View File

@ -0,0 +1,127 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "commands.h"
#include "diagramitem.h"
#include <QGraphicsScene>
//! [0]
MoveCommand::MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
QUndoCommand *parent)
: QUndoCommand(parent), myDiagramItem(diagramItem)
, myOldPos(oldPos), newPos(diagramItem->pos())
{
}
//! [0]
//! [1]
bool MoveCommand::mergeWith(const QUndoCommand *command)
{
const MoveCommand *moveCommand = static_cast<const MoveCommand *>(command);
DiagramItem *item = moveCommand->myDiagramItem;
if (myDiagramItem != item)
return false;
newPos = item->pos();
setText(QObject::tr("Move %1")
.arg(createCommandString(myDiagramItem, newPos)));
return true;
}
//! [1]
//! [2]
void MoveCommand::undo()
{
myDiagramItem->setPos(myOldPos);
myDiagramItem->scene()->update();
setText(QObject::tr("Move %1")
.arg(createCommandString(myDiagramItem, newPos)));
}
//! [2]
//! [3]
void MoveCommand::redo()
{
myDiagramItem->setPos(newPos);
setText(QObject::tr("Move %1")
.arg(createCommandString(myDiagramItem, newPos)));
}
//! [3]
//! [4]
DeleteCommand::DeleteCommand(QGraphicsScene *scene, QUndoCommand *parent)
: QUndoCommand(parent), myGraphicsScene(scene)
{
QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
list.first()->setSelected(false);
myDiagramItem = static_cast<DiagramItem *>(list.first());
setText(QObject::tr("Delete %1")
.arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
}
//! [4]
//! [5]
void DeleteCommand::undo()
{
myGraphicsScene->addItem(myDiagramItem);
myGraphicsScene->update();
}
//! [5]
//! [6]
void DeleteCommand::redo()
{
myGraphicsScene->removeItem(myDiagramItem);
}
//! [6]
//! [7]
AddCommand::AddCommand(DiagramItem::DiagramType addType,
QGraphicsScene *scene, QUndoCommand *parent)
: QUndoCommand(parent), myGraphicsScene(scene)
{
static int itemCount = 0;
myDiagramItem = new DiagramItem(addType);
initialPosition = QPointF((itemCount * 15) % int(scene->width()),
(itemCount * 15) % int(scene->height()));
scene->update();
++itemCount;
setText(QObject::tr("Add %1")
.arg(createCommandString(myDiagramItem, initialPosition)));
}
//! [7]
AddCommand::~AddCommand()
{
if (!myDiagramItem->scene())
delete myDiagramItem;
}
//! [8]
void AddCommand::undo()
{
myGraphicsScene->removeItem(myDiagramItem);
myGraphicsScene->update();
}
//! [8]
//! [9]
void AddCommand::redo()
{
myGraphicsScene->addItem(myDiagramItem);
myDiagramItem->setPos(initialPosition);
myGraphicsScene->clearSelection();
myGraphicsScene->update();
}
//! [9]
QString createCommandString(DiagramItem *item, const QPointF &pos)
{
return QObject::tr("%1 at (%2, %3)")
.arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle")
.arg(pos.x()).arg(pos.y());
}

View File

@ -0,0 +1,67 @@
// 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 "diagramitem.h"
//! [0]
class MoveCommand : public QUndoCommand
{
public:
enum { Id = 1234 };
MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
bool mergeWith(const QUndoCommand *command) override;
int id() const override { return Id; }
private:
DiagramItem *myDiagramItem;
QPointF myOldPos;
QPointF newPos;
};
//! [0]
//! [1]
class DeleteCommand : public QUndoCommand
{
public:
explicit DeleteCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
private:
DiagramItem *myDiagramItem;
QGraphicsScene *myGraphicsScene;
};
//! [1]
//! [2]
class AddCommand : public QUndoCommand
{
public:
AddCommand(DiagramItem::DiagramType addType, QGraphicsScene *graphicsScene,
QUndoCommand *parent = nullptr);
~AddCommand();
void undo() override;
void redo() override;
private:
DiagramItem *myDiagramItem;
QGraphicsScene *myGraphicsScene;
QPointF initialPosition;
};
//! [2]
QString createCommandString(DiagramItem *item, const QPointF &point);
#endif

View File

@ -0,0 +1,29 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "diagramitem.h"
#include <QBrush>
#include <QRandomGenerator>
DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item)
: QGraphicsPolygonItem(item)
{
if (diagramType == Box) {
boxPolygon << QPointF(0, 0) << QPointF(0, 30) << QPointF(30, 30)
<< QPointF(30, 0) << QPointF(0, 0);
setPolygon(boxPolygon);
} else {
trianglePolygon << QPointF(15, 0) << QPointF(30, 30) << QPointF(0, 30)
<< QPointF(15, 0);
setPolygon(trianglePolygon);
}
QColor color(QRandomGenerator::global()->bounded(256),
QRandomGenerator::global()->bounded(256),
QRandomGenerator::global()->bounded(256));
QBrush brush(color);
setBrush(brush);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
}

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef DIAGRAMITEM_H
#define DIAGRAMITEM_H
#include <QGraphicsPolygonItem>
QT_BEGIN_NAMESPACE
class QGraphicsItem;
class QGraphicsScene;
class QGraphicsSceneMouseEvent;
class QPointF;
QT_END_NAMESPACE
class DiagramItem : public QGraphicsPolygonItem
{
public:
enum { Type = UserType + 1 };
enum DiagramType { Box, Triangle };
explicit DiagramItem(DiagramType diagramType, QGraphicsItem *item = nullptr);
DiagramType diagramType() const
{
return polygon() == boxPolygon ? Box : Triangle;
}
int type() const override { return Type; }
private:
QPolygonF boxPolygon;
QPolygonF trianglePolygon;
};
#endif

View File

@ -0,0 +1,36 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "diagramscene.h"
#include "diagramitem.h"
#include <QGraphicsSceneMouseEvent>
DiagramScene::DiagramScene(QObject *parent)
: QGraphicsScene(parent)
{}
void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPointF mousePos(event->buttonDownScenePos(Qt::LeftButton).x(),
event->buttonDownScenePos(Qt::LeftButton).y());
const QList<QGraphicsItem *> itemList = items(mousePos);
movingItem = itemList.isEmpty() ? nullptr : itemList.first();
if (movingItem != nullptr && event->button() == Qt::LeftButton)
oldPos = movingItem->pos();
clearSelection();
QGraphicsScene::mousePressEvent(event);
}
void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (movingItem != nullptr && event->button() == Qt::LeftButton) {
if (oldPos != movingItem->pos())
emit itemMoved(qgraphicsitem_cast<DiagramItem *>(movingItem),
oldPos);
movingItem = nullptr;
}
QGraphicsScene::mouseReleaseEvent(event);
}

View File

@ -0,0 +1,37 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef DIAGRAMSCENE_H
#define DIAGRAMSCENE_H
#include <QObject>
#include <QGraphicsScene>
class DiagramItem;
QT_BEGIN_NAMESPACE
class QGraphicsSceneDragDropEvent;
class QGraphicsViewItem;
QT_END_NAMESPACE
//! [0]
class DiagramScene : public QGraphicsScene
{
Q_OBJECT
public:
DiagramScene(QObject *parent = nullptr);
signals:
void itemMoved(DiagramItem *movedItem, const QPointF &movedFromPosition);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private:
QGraphicsItem *movingItem = nullptr;
QPointF oldPos;
};
//! [0]
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 356 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 690 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 985 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 850 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 962 B

View File

@ -0,0 +1,20 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QApplication>
#include "mainwindow.h"
//! [0]
int main(int argv, char *args[])
{
Q_INIT_RESOURCE(undoframework);
QApplication app(argv, args);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
//! [0]

View File

@ -0,0 +1,185 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "mainwindow.h"
#include "diagramscene.h"
#include "diagramitem.h"
#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();
connect(diagramScene, &DiagramScene::itemMoved,
this, &MainWindow::itemMoved);
connect(diagramScene, &DiagramScene::selectionChanged,
this, &MainWindow::updateActions);
setWindowTitle("Undo Framework");
QGraphicsView *view = new QGraphicsView(diagramScene);
setCentralWidget(view);
adjustSize();
}
//! [0]
//! [1]
void MainWindow::createUndoView()
{
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(QIcon(":/icons/remove.png"), tr("&Delete Item"), this);
deleteAction->setShortcut(tr("Del"));
connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteItem);
//! [2]
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(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]
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
connect(exitAction, &QAction::triggered, this, &QWidget::close);
aboutAction = new QAction(tr("&About"), this);
QList<QKeySequence> aboutShortcuts;
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()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(exitAction);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(undoAction);
editMenu->addAction(redoAction);
editMenu->addSeparator();
editMenu->addAction(deleteAction);
//! [7]
itemMenu = menuBar()->addMenu(tr("&Item"));
itemMenu->addAction(addBoxAction);
itemMenu->addAction(addTriangleAction);
//! [8]
helpMenu = menuBar()->addMenu(tr("&About"));
helpMenu->addAction(aboutAction);
}
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));
}
//! [11]
//! [12]
void MainWindow::deleteItem()
{
if (diagramScene->selectedItems().isEmpty())
return;
QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
undoStack->push(deleteCommand);
}
//! [12]
//! [13]
void MainWindow::addBox()
{
QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
undoStack->push(addCommand);
}
//! [13]
//! [14]
void MainWindow::addTriangle()
{
QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
diagramScene);
undoStack->push(addCommand);
}
//! [14]
//! [15]
void MainWindow::about()
{
QMessageBox::about(this, tr("About Undo"),
tr("The <b>Undo</b> example demonstrates how to "
"use Qt's undo framework."));
}
//! [15]

View File

@ -0,0 +1,62 @@
// 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>
QT_BEGIN_NAMESPACE
class QAction;
class QToolBar;
class QMenu;
class QUndoStack;
class QUndoView;
QT_END_NAMESPACE
class DiagramScene;
class DiagramItem;
//! [0]
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
public slots:
void itemMoved(DiagramItem *movedDiagram, const QPointF &moveStartPosition);
private slots:
void deleteItem();
void addBox();
void addTriangle();
void about();
void updateActions();
private:
void createActions();
void createMenus();
void createToolBars();
void createUndoView();
QAction *deleteAction = nullptr;
QAction *addBoxAction = nullptr;
QAction *addTriangleAction = nullptr;
QAction *undoAction = nullptr;
QAction *redoAction = nullptr;
QAction *exitAction = nullptr;
QAction *aboutAction = nullptr;
QMenu *fileMenu = nullptr;
QMenu *editMenu = nullptr;
QMenu *itemMenu = nullptr;
QMenu *helpMenu = nullptr;
DiagramScene *diagramScene = nullptr;
QUndoStack *undoStack = nullptr;
QUndoView *undoView = nullptr;
};
//! [0]
#endif

View File

@ -0,0 +1,17 @@
QT += widgets
requires(qtConfig(undoview))
HEADERS = commands.h \
diagramitem.h \
diagramscene.h \
mainwindow.h
SOURCES = commands.cpp \
diagramitem.cpp \
diagramscene.cpp \
main.cpp \
mainwindow.cpp
RESOURCES = undoframework.qrc
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tools/undoframework
INSTALLS += target

View File

@ -0,0 +1,10 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<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>