Skip to content

Commit 1d04756

Browse files
committed
using undo and redo of base class
1 parent efc0783 commit 1d04756

File tree

2 files changed

+28
-77
lines changed

2 files changed

+28
-77
lines changed

src/commands.cpp

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
3-
* Copyright (c) 2022-2025 Jochen Meidow, Fraunhofer IOSB
3+
* Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -17,7 +17,6 @@
1717
*/
1818

1919
#include "commands.h"
20-
#include "global.h"
2120
#include "mainscene.h"
2221
#include "state.h"
2322

@@ -39,32 +38,37 @@ Undo::Undo( QUndoCommand *parent) : QUndoCommand(parent)
3938
}
4039

4140

42-
AddStroke::AddStroke( State *curr,
43-
std::unique_ptr<State> &p,
44-
std::unique_ptr<State> &n,
45-
QUndoCommand *parent) : Undo(parent)
41+
void Undo::undo()
4642
{
47-
// qDebug() << Q_FUNC_INFO;
48-
setText( QStringLiteral("add stroke") );
49-
current_state_ = curr; // set pointer
50-
prev_state_ = std::move(p);
51-
next_state_ = std::move(n);
43+
//qDebug() << Q_FUNC_INFO;
44+
45+
*current_state_ = *prev_state_; // set
46+
scene()->removeAllItems();
47+
scene()->addGraphicItems( prev_state_.get() );
5248
}
5349

54-
void AddStroke::redo()
50+
51+
void Undo::redo()
5552
{
56-
// qDebug() << Q_FUNC_INFO;
53+
//qDebug() << Q_FUNC_INFO;
54+
5755
*current_state_ = *next_state_; // copy content
5856
scene()->removeAllItems();
5957
scene()->addGraphicItems( next_state_.get() );
6058
}
6159

62-
void AddStroke::undo()
60+
61+
AddStroke::AddStroke( State *curr,
62+
std::unique_ptr<State> &p,
63+
std::unique_ptr<State> &n,
64+
QUndoCommand *parent) : Undo(parent)
6365
{
6466
// qDebug() << Q_FUNC_INFO;
65-
*current_state_ = *prev_state_; // set
66-
scene()->removeAllItems();
67-
scene()->addGraphicItems( prev_state_.get() );
67+
setText( QStringLiteral("add stroke") );
68+
69+
current_state_ = curr; // set pointer
70+
prev_state_ = std::move(p);
71+
next_state_ = std::move(n);
6872
}
6973

7074

@@ -73,6 +77,7 @@ DeleteSelection::DeleteSelection( State *st,
7377
std::unique_ptr<State> & n,
7478
QUndoCommand *parent) : Undo(parent)
7579
{
80+
// qDebug() << Q_FUNC_INFO ;
7681
setText( QStringLiteral("delete selected item%1")
7782
.arg( scene()->selectedItems().size()==1 ? "" : "s") );
7883

@@ -81,20 +86,6 @@ DeleteSelection::DeleteSelection( State *st,
8186
next_state_ = std::move(n);
8287
}
8388

84-
void DeleteSelection::redo()
85-
{
86-
*current_state_ = *next_state_;
87-
scene()->removeAllItems();
88-
scene()->addGraphicItems( next_state_.get() );
89-
}
90-
91-
void DeleteSelection::undo()
92-
{
93-
*current_state_ = *prev_state_;
94-
scene()->removeAllItems();
95-
scene()->addGraphicItems( prev_state_.get() );
96-
}
97-
9889

9990
TabulaRasa::TabulaRasa( State *st, QUndoCommand *parent ) : Undo(parent)
10091
{
@@ -103,25 +94,9 @@ TabulaRasa::TabulaRasa( State *st, QUndoCommand *parent ) : Undo(parent)
10394
current_state_ = st; // pointer
10495
prev_state_ = std::make_unique<State>(*st); // copy
10596
next_state_ = std::make_unique<State>();
106-
//next_state_ = std::make_unique<State>(*st);
107-
//next_state_->clearAll();
10897
}
10998

11099

111-
void TabulaRasa::redo()
112-
{
113-
// qDebug() << Q_FUNC_INFO;
114-
*current_state_ = *next_state_;
115-
scene()->removeAllItems();
116-
}
117-
118-
void TabulaRasa::undo()
119-
{
120-
// qDebug() << Q_FUNC_INFO;
121-
*current_state_ = *prev_state_;
122-
scene()->addGraphicItems( prev_state_.get() );
123-
}
124-
125100
ReplaceStateWithFileContent::ReplaceStateWithFileContent( const QString & fileName,
126101
State *curr,
127102
std::unique_ptr<State> & p,
@@ -133,25 +108,8 @@ ReplaceStateWithFileContent::ReplaceStateWithFileContent( const QString & fileNa
133108
setText( fileName );
134109

135110
current_state_ = curr; // pointer
136-
//prev_state_ = std::make_unique<State>(*Old); // copy
137111
prev_state_ = std::move(p); // copy
138112
next_state_ = std::move(n);
139113
}
140114

141-
void ReplaceStateWithFileContent::redo()
142-
{
143-
// qDebug() << Q_FUNC_INFO;
144-
*current_state_ = *next_state_;
145-
scene()->removeAllItems();
146-
scene()->addGraphicItems( next_state_.get() );
147-
}
148-
149-
void ReplaceStateWithFileContent::undo()
150-
{
151-
// qDebug() << Q_FUNC_INFO;
152-
*current_state_ = *prev_state_;
153-
scene()->removeAllItems();
154-
scene()->addGraphicItems( prev_state_.get() );
155-
}
156-
157115
} // namespace Cmd

src/commands.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
3-
* Copyright (c) 2022-2025 Jochen Meidow, Fraunhofer IOSB
3+
* Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -50,14 +50,17 @@ class Undo : public QUndoCommand
5050

5151
protected:
5252
explicit Undo( QUndoCommand *parent); //!< Standard constructor
53-
~Undo() = default;
53+
~Undo() override = default;
5454

5555
State *current_state_{}; //!< Pointer to current state
5656
std::unique_ptr<State> next_state_; //!< Pointer to next state (redo)
5757
std::unique_ptr<State> prev_state_; //!< Pointer to previous state (undo)
5858

5959
private:
6060
static GUI::MainScene *s_scene;
61+
62+
void redo() override;
63+
void undo() override;
6164
};
6265

6366

@@ -77,8 +80,6 @@ class AddStroke : public Undo
7780
~AddStroke() override = default;
7881

7982
private:
80-
void redo() override;
81-
void undo() override;
8283
};
8384

8485

@@ -91,9 +92,6 @@ class DeleteSelection : public Undo
9192
std::unique_ptr<State> &p,
9293
std::unique_ptr<State> &n,
9394
QUndoCommand *parent);
94-
private:
95-
void redo() override;
96-
void undo() override;
9795
};
9896

9997

@@ -103,11 +101,9 @@ class TabulaRasa : public Undo
103101
public:
104102
//! Value constructor
105103
TabulaRasa( State *st, QUndoCommand *parent);
106-
private:
107-
void redo() override;
108-
void undo() override;
109104
};
110105

106+
111107
//! Command 'replace view with file content'
112108
class ReplaceStateWithFileContent : public Undo
113109
{
@@ -118,9 +114,6 @@ class ReplaceStateWithFileContent : public Undo
118114
std::unique_ptr<State> &p,
119115
std::unique_ptr<State> &n,
120116
QUndoCommand *parent);
121-
private:
122-
void redo() override;
123-
void undo() override;
124117
};
125118

126119
} // namespace Cmd

0 commit comments

Comments
 (0)