Skip to content

Commit d8ba3ff

Browse files
Core: store what has been done in history
1 parent 5677df4 commit d8ba3ff

26 files changed

+147
-100
lines changed

src/core/core.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ToolResponse Core::tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInt
6363
Logger::Domain::CORE, e.what());
6464
return ToolResponse::end();
6565
}
66+
tool_id_current = tool_id;
6667
maybe_end_tool(r);
6768

6869
return r;
@@ -82,15 +83,17 @@ void Core::maybe_end_tool(const ToolResponse &r)
8283
s_signal_tool_changed.emit(ToolID::NONE);
8384
if (r.result == ToolResponse::Result::COMMIT) {
8485
set_needs_save(true);
85-
rebuild_internal(false);
86+
const auto comment = action_catalog.at(tool_id_current).name;
87+
rebuild_internal(false, comment);
8688
}
8789
else if (r.result == ToolResponse::Result::REVERT) {
8890
history_load(history_current);
89-
rebuild_internal(true);
91+
rebuild_internal(true, "undo");
9092
}
9193
else if (r.result == ToolResponse::Result::END) { // did nothing
9294
// do nothing
9395
}
96+
tool_id_current = ToolID::NONE;
9497
}
9598
}
9699

@@ -138,19 +141,19 @@ ToolResponse Core::tool_update(const ToolArgs &args)
138141
return ToolResponse();
139142
}
140143

141-
void Core::rebuild()
144+
void Core::rebuild(const std::string &comment)
142145
{
143-
rebuild_internal(false);
146+
rebuild_internal(false, comment);
144147
}
145148

146-
void Core::rebuild_finish(bool from_undo)
149+
void Core::rebuild_finish(bool from_undo, const std::string &comment)
147150
{
148151
if (!from_undo) {
149152
while (history_current + 1 != (int)history.size()) {
150153
history.pop_back();
151154
}
152155
assert(history_current + 1 == (int)history.size());
153-
history_push();
156+
history_push(comment);
154157
history_current++;
155158
history_trim();
156159
}
@@ -181,6 +184,28 @@ void Core::redo()
181184
set_needs_save();
182185
}
183186

187+
static const std::string empty_string;
188+
189+
const std::string &Core::get_undo_comment() const
190+
{
191+
if (can_undo()) {
192+
return history.at(history_current)->comment;
193+
}
194+
else {
195+
return empty_string;
196+
}
197+
}
198+
199+
const std::string &Core::get_redo_comment() const
200+
{
201+
if (can_redo()) {
202+
return history.at(history_current + 1)->comment;
203+
}
204+
else {
205+
return empty_string;
206+
}
207+
}
208+
184209
void Core::history_clear()
185210
{
186211
history.clear();
@@ -218,7 +243,7 @@ void Core::set_property_commit()
218243
{
219244
if (!property_transaction)
220245
throw std::runtime_error("no transaction in progress");
221-
rebuild();
246+
rebuild("edit property");
222247
set_needs_save(true);
223248
property_transaction = false;
224249
}
@@ -278,7 +303,8 @@ void Core::autosave()
278303
save(autosave_suffix);
279304
}
280305

281-
Core::Core(IPool &pool, IPool *pool_caching) : m_pool(pool), m_pool_caching(pool_caching ? *pool_caching : pool)
306+
Core::Core(IPool &pool, IPool *pool_caching)
307+
: m_pool(pool), m_pool_caching(pool_caching ? *pool_caching : pool), tool_id_current(ToolID::NONE)
282308
{
283309
}
284310

src/core/core.hpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Core : public virtual Document {
7070
* Expands the non-working document.
7171
* And copies the non-working document to the working document.
7272
*/
73-
void rebuild();
73+
void rebuild(const std::string &comment);
7474
ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
7575
ToolResponse tool_update(const ToolArgs &args);
7676
std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
@@ -84,6 +84,9 @@ class Core : public virtual Document {
8484
bool can_undo() const;
8585
bool can_redo() const;
8686

87+
const std::string &get_undo_comment() const;
88+
const std::string &get_redo_comment() const;
89+
8790
inline bool tool_is_active()
8891
{
8992
return tool != nullptr;
@@ -179,6 +182,7 @@ class Core : public virtual Document {
179182
class IPool &m_pool;
180183
class IPool &m_pool_caching;
181184

185+
ToolID tool_id_current;
182186
std::unique_ptr<ToolBase> tool = nullptr;
183187
type_signal_tool_changed s_signal_tool_changed;
184188
type_signal_rebuilt s_signal_rebuilt;
@@ -191,20 +195,21 @@ class Core : public virtual Document {
191195
bool needs_save = false;
192196
void set_needs_save(bool v);
193197

194-
void rebuild_finish(bool from_undo);
198+
void rebuild_finish(bool from_undo, const std::string &comment);
195199

196200
class HistoryItem {
197201
public:
198-
// Symbol sym;
199-
// HistoryItem(const Symbol &s): sym(s) {}
200-
std::string comment;
202+
HistoryItem(const std::string &c) : comment(c)
203+
{
204+
}
205+
const std::string comment;
201206
virtual ~HistoryItem()
202207
{
203208
}
204209
};
205210
std::deque<std::unique_ptr<HistoryItem>> history;
206211
int history_current = -1;
207-
virtual void history_push() = 0;
212+
virtual void history_push(const std::string &comment) = 0;
208213
virtual void history_load(unsigned int i) = 0;
209214
void history_clear();
210215
void history_trim();
@@ -223,6 +228,6 @@ class Core : public virtual Document {
223228
std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
224229
std::set<SelectableRef> tool_selection;
225230
void maybe_end_tool(const ToolResponse &r);
226-
virtual void rebuild_internal(bool from_undo) = 0;
231+
virtual void rebuild_internal(bool from_undo, const std::string &comment) = 0;
227232
};
228233
} // namespace horizon

src/core/core_board.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CoreBoard::CoreBoard(const std::string &board_filename, const std::string &block
5353
m_blocks_filename(blocks_filename), m_pictures_dir(pictures_dir)
5454
{
5555
brd->load_pictures(pictures_dir);
56-
rebuild();
56+
rebuild("init");
5757
}
5858

5959
void CoreBoard::reload_netlist()
@@ -98,7 +98,7 @@ void CoreBoard::reload_netlist()
9898
brd->update_refs();
9999
rules.cleanup(&*block);
100100
brd->expand_flags = Board::EXPAND_PROPAGATE_NETS | Board::EXPAND_ALL_AIRWIRES | Board::EXPAND_PACKAGES;
101-
rebuild();
101+
rebuild("reload netlist");
102102
}
103103

104104
bool CoreBoard::get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, PropertyValue &value)
@@ -552,7 +552,7 @@ bool CoreBoard::set_property(ObjectType type, const UUID &uu, ObjectProperty::ID
552552
return false;
553553
}
554554
if (!property_transaction) {
555-
rebuild_internal(false);
555+
rebuild_internal(false, "edit properties");
556556
set_needs_save(true);
557557
}
558558
return true;
@@ -621,11 +621,11 @@ bool CoreBoard::get_property_meta(ObjectType type, const UUID &uu, ObjectPropert
621621
}
622622
}
623623

624-
void CoreBoard::rebuild_internal(bool from_undo)
624+
void CoreBoard::rebuild_internal(bool from_undo, const std::string &comment)
625625
{
626626
clock_t begin = clock();
627627
brd->expand_some();
628-
rebuild_finish(from_undo);
628+
rebuild_finish(from_undo, comment);
629629
clock_t end = clock();
630630
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
631631
std::cout << "rebuild took " << elapsed_secs << std::endl;
@@ -666,13 +666,14 @@ void CoreBoard::update_rules()
666666
brd->rules = rules;
667667
}
668668

669-
CoreBoard::HistoryItem::HistoryItem(const Block &b, const Board &r) : block(b), brd(shallow_copy, r)
669+
CoreBoard::HistoryItem::HistoryItem(const Block &b, const Board &r, const std::string &cm)
670+
: Core::HistoryItem(cm), block(b), brd(shallow_copy, r)
670671
{
671672
}
672673

673-
void CoreBoard::history_push()
674+
void CoreBoard::history_push(const std::string &comment)
674675
{
675-
history.push_back(std::make_unique<CoreBoard::HistoryItem>(*block, *brd));
676+
history.push_back(std::make_unique<CoreBoard::HistoryItem>(*block, *brd, comment));
676677
}
677678

678679
void CoreBoard::history_load(unsigned int i)
@@ -698,7 +699,7 @@ void CoreBoard::reload_pool()
698699
brd.emplace(brd->uuid, brd_j, *block, m_pool_caching);
699700
keeper.restore(brd->pictures);
700701
history_clear();
701-
rebuild();
702+
rebuild("reload pool");
702703
}
703704

704705
json CoreBoard::get_meta()

src/core/core_board.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ class CoreBoard : public Core, public DocumentBoard {
9393

9494
class HistoryItem : public Core::HistoryItem {
9595
public:
96-
HistoryItem(const Block &b, const Board &r);
96+
HistoryItem(const Block &b, const Board &r, const std::string &comment);
9797
Block block;
9898
Board brd;
9999
};
100-
void rebuild_internal(bool from_undo) override;
101-
void history_push() override;
100+
void rebuild_internal(bool from_undo, const std::string &comment) override;
101+
void history_push(const std::string &comment) override;
102102
void history_load(unsigned int i) override;
103103
void save(const std::string &suffix) override;
104104
void delete_autosave() override;

src/core/core_decal.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace horizon {
88
CoreDecal::CoreDecal(const std::string &decal_filename, IPool &pool)
99
: Core(pool, nullptr), decal(Decal::new_from_file(decal_filename)), m_decal_filename(decal_filename)
1010
{
11-
rebuild();
11+
rebuild("init");
1212
}
1313

1414

@@ -52,9 +52,9 @@ std::map<UUID, Arc> *CoreDecal::get_arc_map()
5252
return &decal.arcs;
5353
}
5454

55-
void CoreDecal::rebuild_internal(bool from_undo)
55+
void CoreDecal::rebuild_internal(bool from_undo, const std::string &comment)
5656
{
57-
rebuild_finish(from_undo);
57+
rebuild_finish(from_undo, comment);
5858
}
5959

6060
LayerProvider &CoreDecal::get_layer_provider()
@@ -72,13 +72,13 @@ Decal &CoreDecal::get_decal()
7272
return decal;
7373
}
7474

75-
CoreDecal::HistoryItem::HistoryItem(const Decal &dec) : decal(dec)
75+
CoreDecal::HistoryItem::HistoryItem(const Decal &dec, const std::string &cm) : Core::HistoryItem(cm), decal(dec)
7676
{
7777
}
7878

79-
void CoreDecal::history_push()
79+
void CoreDecal::history_push(const std::string &comment)
8080
{
81-
history.push_back(std::make_unique<CoreDecal::HistoryItem>(decal));
81+
history.push_back(std::make_unique<CoreDecal::HistoryItem>(decal, comment));
8282
}
8383

8484
void CoreDecal::history_load(unsigned int i)

src/core/core_decal.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class CoreDecal : public Core, public IDocumentDecal {
4141

4242
class HistoryItem : public Core::HistoryItem {
4343
public:
44-
HistoryItem(const Decal &r);
44+
HistoryItem(const Decal &r, const std::string &cm);
4545
Decal decal;
4646
};
47-
void rebuild_internal(bool from_undo) override;
48-
void history_push() override;
47+
void rebuild_internal(bool from_undo, const std::string &comment) override;
48+
void history_push(const std::string &comment) override;
4949
void history_load(unsigned int i) override;
5050
void save(const std::string &suffix) override;
5151
void delete_autosave() override;

src/core/core_frame.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace horizon {
99
CoreFrame::CoreFrame(const std::string &frame_filename, IPool &pool)
1010
: Core(pool, nullptr), frame(Frame::new_from_file(frame_filename)), m_frame_filename(frame_filename)
1111
{
12-
rebuild();
12+
rebuild("init");
1313
}
1414

1515

@@ -73,10 +73,10 @@ bool CoreFrame::get_property_meta(ObjectType type, const UUID &uu, ObjectPropert
7373
return false;
7474
}
7575

76-
void CoreFrame::rebuild_internal(bool from_undo)
76+
void CoreFrame::rebuild_internal(bool from_undo, const std::string &comment)
7777
{
7878
frame.expand();
79-
rebuild_finish(from_undo);
79+
rebuild_finish(from_undo, comment);
8080
}
8181

8282
LayerProvider &CoreFrame::get_layer_provider()
@@ -94,13 +94,13 @@ Frame &CoreFrame::get_frame()
9494
return frame;
9595
}
9696

97-
CoreFrame::HistoryItem::HistoryItem(const Frame &fr) : frame(fr)
97+
CoreFrame::HistoryItem::HistoryItem(const Frame &fr, const std::string &cm) : Core::HistoryItem(cm), frame(fr)
9898
{
9999
}
100100

101-
void CoreFrame::history_push()
101+
void CoreFrame::history_push(const std::string &comment)
102102
{
103-
history.push_back(std::make_unique<CoreFrame::HistoryItem>(frame));
103+
history.push_back(std::make_unique<CoreFrame::HistoryItem>(frame, comment));
104104
}
105105

106106
void CoreFrame::history_load(unsigned int i)

src/core/core_frame.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class CoreFrame : public Core, public IDocumentFrame {
4444

4545
class HistoryItem : public Core::HistoryItem {
4646
public:
47-
HistoryItem(const Frame &r);
47+
HistoryItem(const Frame &r, const std::string &comment);
4848
Frame frame;
4949
};
50-
void rebuild_internal(bool from_undo) override;
51-
void history_push() override;
50+
void rebuild_internal(bool from_undo, const std::string &comment) override;
51+
void history_push(const std::string &comment) override;
5252
void history_load(unsigned int i) override;
5353
void save(const std::string &suffix) override;
5454
void delete_autosave() override;

0 commit comments

Comments
 (0)