Skip to content

Commit 65390b5

Browse files
committed
undo framework: protected members now private
1 parent 703b837 commit 65390b5

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

src/commands.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ namespace Cmd {
3232

3333
GUI::MainScene * Undo::s_scene = nullptr;
3434

35-
Undo::Undo( QUndoCommand *parent, State *st)
36-
: QUndoCommand(parent), current_state_(st)
35+
Undo::Undo( QUndoCommand *parent,
36+
std::unique_ptr<State> &p,
37+
std::unique_ptr<State> &n, State *st)
38+
: QUndoCommand(parent),
39+
next_state_(std::move(n)),
40+
prev_state_(std::move(p)),
41+
current_state_(st)
3742
{
3843
// qDebug() << Q_FUNC_INFO;
3944
}
@@ -70,42 +75,37 @@ void Undo::redo()
7075
AddStroke::AddStroke( State *curr,
7176
std::unique_ptr<State> &p,
7277
std::unique_ptr<State> &n,
73-
QUndoCommand *parent) : Undo(parent, curr)
78+
QUndoCommand *parent) : Undo(parent, p, n, curr)
7479
{
7580
// qDebug() << Q_FUNC_INFO;
76-
setText( QStringLiteral("add stroke") );
77-
78-
prev_state_ = std::move(p);
79-
next_state_ = std::move(n);
81+
setText( "add stroke" );
8082
}
8183

8284

8385
DeleteSelection::DeleteSelection( State *st,
8486
std::unique_ptr<State> & p,
8587
std::unique_ptr<State> & n,
86-
QUndoCommand *parent) : Undo(parent, st)
88+
QUndoCommand *parent) : Undo(parent, p,n, st)
8789
{
8890
// qDebug() << Q_FUNC_INFO ;
8991
auto *scn = scene();
9092
if (scn==nullptr) {
9193
return;
9294
}
9395

94-
const QString suffix = scn->selectedItems().size()==1 ? QStringLiteral("") : QStringLiteral("s");
95-
setText( QStringLiteral("delete selected item%1").arg(suffix) );
96-
97-
prev_state_ = std::move(p);
98-
next_state_ = std::move(n);
96+
setText( QString( scn->selectedItems().size()==1 ?
97+
"delete selected item" : "delete selected items") );
9998
}
10099

101100

102-
TabulaRasa::TabulaRasa( State *st, QUndoCommand *parent ) : Undo(parent, st)
101+
TabulaRasa::TabulaRasa( State *st,
102+
std::unique_ptr<State> &p,
103+
std::unique_ptr<State> &n,
104+
QUndoCommand *parent )
105+
: Undo(parent, p,n, st)
103106
{
104107
// qDebug() << Q_FUNC_INFO ;
105-
setText( QStringLiteral("clear all") );
106-
107-
prev_state_ = std::make_unique<State>(*st); // copy
108-
next_state_ = std::make_unique<State>();
108+
setText( "clear all" );
109109
}
110110

111111

@@ -114,13 +114,10 @@ ReplaceStateWithFileContent::ReplaceStateWithFileContent( const QString & fileNa
114114
std::unique_ptr<State> & p,
115115
std::unique_ptr<State> & n,
116116
QUndoCommand *parent)
117-
: Undo(parent, curr)
117+
: Undo(parent, p,n, curr)
118118
{
119119
// qDebug() << Q_FUNC_INFO;
120120
setText( fileName );
121-
122-
prev_state_ = std::move(p); // copy
123-
next_state_ = std::move(n);
124121
}
125122

126123
} // namespace Cmd

src/commands.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ class Undo : public QUndoCommand
4949
static GUI::MainScene * scene() { return s_scene; }
5050

5151
protected:
52-
explicit Undo(QUndoCommand *parent, State *st); //!< Standard constructor
52+
explicit Undo(QUndoCommand *parent,
53+
std::unique_ptr<State> &p,
54+
std::unique_ptr<State> &n,
55+
State *st); //!< Standard constructor
5356
~Undo() override = default;
5457

58+
private:
5559
std::unique_ptr<State> next_state_; //!< Pointer to next state (redo)
5660
std::unique_ptr<State> prev_state_; //!< Pointer to previous state (undo)
57-
58-
private:
5961
State *current_state_{}; //!< Pointer to current state
6062

6163
static GUI::MainScene *s_scene;
@@ -99,7 +101,10 @@ class TabulaRasa : public Undo
99101
{
100102
public:
101103
//! Value constructor
102-
TabulaRasa( State *st, QUndoCommand *parent);
104+
TabulaRasa( State *st,
105+
std::unique_ptr<State> &p,
106+
std::unique_ptr<State> &n,
107+
QUndoCommand *parent);
103108
};
104109

105110

src/mainwindow.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,12 @@ void MainWindow::readBinaryFile( const QString & fileName)
821821

822822
void MainWindow::slotCmdTabulaRasa()
823823
{
824+
825+
std::unique_ptr<State> p = std::make_unique<State>(curr_state);
826+
std::unique_ptr<State> n = std::make_unique<State>();
827+
824828
m_undoStack->push(
825-
new Cmd::TabulaRasa( &curr_state, nullptr )
829+
new Cmd::TabulaRasa( &curr_state, p, n, nullptr )
826830
);
827831

828832
setWindowModified( true );
@@ -875,7 +879,7 @@ void MainWindow::slotCmdDeleteSelection()
875879
if ( next_state_->reduce() )
876880
{
877881
std::unique_ptr<State> prev_state_
878-
= std::make_unique<State>(curr_state); // copy
882+
= std::make_unique<State>(curr_state);
879883

880884
m_undoStack->push(
881885
new Cmd::DeleteSelection( &curr_state,

0 commit comments

Comments
 (0)