Skip to content

Commit fd06c2b

Browse files
pool manager: don't ask for filename when creating pool items with imp
changelog: Enhacements/Pool Manager: don't ask for filename when creating pool items
1 parent 2ad36a6 commit fd06c2b

37 files changed

+376
-257
lines changed

Diff for: src/core/core.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ const std::string Core::autosave_suffix = ".autosave";
318318

319319
void Core::autosave()
320320
{
321-
save(autosave_suffix);
321+
if (get_filename().size())
322+
save(autosave_suffix);
322323
}
323324

324325
Core::Core(IPool &pool, IPool *pool_caching)

Diff for: src/core/core_decal.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,16 @@ void CoreDecal::delete_autosave()
110110
if (Glib::file_test(m_decal_filename + autosave_suffix, Glib::FILE_TEST_IS_REGULAR))
111111
Gio::File::create_for_path(m_decal_filename + autosave_suffix)->remove();
112112
}
113+
114+
void CoreDecal::set_temp_mode()
115+
{
116+
Gio::File::create_for_path(m_decal_filename)->remove();
117+
m_decal_filename.clear();
118+
}
119+
120+
void CoreDecal::set_filename(const std::string &filename)
121+
{
122+
m_decal_filename = filename;
123+
}
124+
113125
} // namespace horizon

Diff for: src/core/core_decal.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class CoreDecal : public Core, public IDocumentDecal {
2828
return decal.version;
2929
}
3030

31+
void set_temp_mode();
32+
void set_filename(const std::string &filename);
33+
3134
private:
3235
std::map<UUID, Polygon> *get_polygon_map() override;
3336
std::map<UUID, Junction> *get_junction_map() override;

Diff for: src/core/core_frame.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,15 @@ void CoreFrame::delete_autosave()
133133
if (Glib::file_test(m_frame_filename + autosave_suffix, Glib::FILE_TEST_IS_REGULAR))
134134
Gio::File::create_for_path(m_frame_filename + autosave_suffix)->remove();
135135
}
136+
137+
void CoreFrame::set_temp_mode()
138+
{
139+
Gio::File::create_for_path(m_frame_filename)->remove();
140+
m_frame_filename.clear();
141+
}
142+
143+
void CoreFrame::set_filename(const std::string &filename)
144+
{
145+
m_frame_filename = filename;
146+
}
136147
} // namespace horizon

Diff for: src/core/core_frame.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class CoreFrame : public Core, public IDocumentFrame {
2828
return frame.version;
2929
}
3030

31+
void set_temp_mode();
32+
void set_filename(const std::string &filename);
33+
3134
bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
3235
class PropertyMeta &meta) override;
3336

Diff for: src/core/core_package.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,16 @@ void CorePackage::delete_autosave()
277277
if (Glib::file_test(m_filename + autosave_suffix, Glib::FILE_TEST_IS_REGULAR))
278278
Gio::File::create_for_path(m_filename + autosave_suffix)->remove();
279279
}
280+
281+
void CorePackage::set_temp_mode()
282+
{
283+
Gio::File::create_for_path(m_filename)->remove();
284+
m_filename.clear();
285+
}
286+
287+
void CorePackage::set_filename(const std::string &filename)
288+
{
289+
m_filename = filename;
290+
}
291+
280292
} // namespace horizon

Diff for: src/core/core_package.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class CorePackage : public Core, public virtual IDocumentPackage {
5252
return package.version;
5353
}
5454

55+
void set_temp_mode();
56+
void set_filename(const std::string &filename);
57+
5558
private:
5659
std::map<UUID, Junction> *get_junction_map() override;
5760
std::map<UUID, Line> *get_line_map() override;

Diff for: src/core/core_padstack.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,15 @@ void CorePadstack::delete_autosave()
275275
Gio::File::create_for_path(m_filename + autosave_suffix)->remove();
276276
}
277277

278+
void CorePadstack::set_temp_mode()
279+
{
280+
Gio::File::create_for_path(m_filename)->remove();
281+
m_filename.clear();
282+
}
283+
284+
void CorePadstack::set_filename(const std::string &filename)
285+
{
286+
m_filename = filename;
287+
}
288+
278289
} // namespace horizon

Diff for: src/core/core_padstack.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class CorePadstack : public Core, public virtual IDocumentPadstack {
3737
return padstack.version;
3838
}
3939

40+
void set_temp_mode();
41+
void set_filename(const std::string &filename);
42+
4043
private:
4144
std::map<UUID, Polygon> *get_polygon_map() override;
4245
std::map<UUID, Hole> *get_hole_map() override;

Diff for: src/core/core_symbol.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ const std::string &CoreSymbol::get_filename() const
335335

336336
void CoreSymbol::save(const std::string &suffix)
337337
{
338+
if (m_filename.empty())
339+
throw std::runtime_error("filename is empty");
338340
s_signal_save.emit();
339341

340342
json j = sym.serialize();
@@ -348,4 +350,15 @@ void CoreSymbol::delete_autosave()
348350
Gio::File::create_for_path(m_filename + autosave_suffix)->remove();
349351
}
350352

353+
void CoreSymbol::set_temp_mode()
354+
{
355+
Gio::File::create_for_path(m_filename)->remove();
356+
m_filename.clear();
357+
}
358+
359+
void CoreSymbol::set_filename(const std::string &filename)
360+
{
361+
m_filename = filename;
362+
}
363+
351364
} // namespace horizon

Diff for: src/core/core_symbol.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class CoreSymbol : public Core, public IDocumentSymbol {
6060
return sym.version;
6161
}
6262

63+
void set_temp_mode();
64+
void set_filename(const std::string &filename);
6365

6466
private:
6567
std::map<UUID, Text> *get_text_map() override;

Diff for: src/imp/imp.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ bool ImpBase::handle_close(const GdkEventAny *ev)
227227
case 2:
228228
if (force_end_tool()) {
229229
trigger_action(ActionID::SAVE);
230+
if (core->get_needs_save())
231+
return true; // saving didn't work, keep open
230232
core->delete_autosave();
231233
return false; // close
232234
}
@@ -528,7 +530,12 @@ void ImpBase::run(int argc, char *argv[])
528530
connect_action(ActionID::SELECTION_FILTER, [this](const auto &a) { selection_filter_dialog->present(); });
529531
connect_action(ActionID::SAVE, [this](const auto &a) {
530532
if (!read_only) {
533+
if (core->get_filename().empty()) {
534+
if (!set_filename())
535+
return;
536+
}
531537
core->save();
538+
save_button->set_label("Save");
532539
json j;
533540
this->get_save_meta(j);
534541
if (!j.is_null()) {
@@ -678,7 +685,9 @@ void ImpBase::run(int argc, char *argv[])
678685
set_action_sensitive(ActionID::SELECT_GRID, grids_window->has_grids());
679686
}
680687

681-
auto save_button = create_action_button(ActionID::SAVE);
688+
save_button = create_action_button(ActionID::SAVE);
689+
if (temp_mode)
690+
save_button->set_label("Save as…");
682691
save_button->show();
683692
main_window->header->pack_start(*save_button);
684693
core->signal_needs_save().connect([this](bool v) {
@@ -1905,4 +1914,16 @@ void ImpBase::update_cursor(ToolID tool_id)
19051914
cr->paint();
19061915
win->set_cursor(Gdk::Cursor::create(win->get_display(), surf, 0, 0));
19071916
}
1917+
1918+
bool ImpBase::set_filename()
1919+
{
1920+
throw std::runtime_error("set_filename not implemented");
1921+
return false;
1922+
}
1923+
1924+
void ImpBase::set_suggested_filename(const std::string &s)
1925+
{
1926+
suggested_filename = s;
1927+
}
1928+
19081929
} // namespace horizon

Diff for: src/imp/imp.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class ImpBase {
4646
{
4747
}
4848
void set_read_only(bool v);
49+
void set_suggested_filename(const std::string &s);
50+
enum class TempMode { YES, NO };
4951

5052
class SelectionFilterInfo {
5153
public:
@@ -261,6 +263,9 @@ class ImpBase {
261263
throw std::runtime_error("not implemented");
262264
}
263265

266+
bool temp_mode = false;
267+
std::string suggested_filename;
268+
264269
private:
265270
void fix_cursor_pos();
266271
Glib::RefPtr<Gio::FileMonitor> preferences_monitor;
@@ -354,5 +359,8 @@ class ImpBase {
354359
unsigned int saved_version = 0;
355360

356361
class MSDTuningWindow *msd_tuning_window = nullptr;
362+
363+
Gtk::Button *save_button = nullptr;
364+
virtual bool set_filename();
357365
};
358366
} // namespace horizon

Diff for: src/imp/imp_decal.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
#include "widgets/action_button.hpp"
77
#include "util/util.hpp"
88
#include "widgets/layer_box.hpp"
9+
#include "util/util.hpp"
10+
#include "util/gtk_util.hpp"
911

1012
namespace horizon {
11-
ImpDecal::ImpDecal(const std::string &decal_filename, const std::string &pool_path)
13+
ImpDecal::ImpDecal(const std::string &decal_filename, const std::string &pool_path, TempMode tmp_mode)
1214
: ImpLayer(pool_path), core_decal(decal_filename, *pool), decal(core_decal.get_decal())
1315
{
1416
core = &core_decal;
1517
core_decal.signal_tool_changed().connect(sigc::mem_fun(*this, &ImpBase::handle_tool_change));
1618
view_angle = 0;
19+
if (tmp_mode == TempMode::YES) {
20+
core_decal.set_temp_mode();
21+
temp_mode = true;
22+
}
1723
}
1824

1925
void ImpDecal::canvas_update()
@@ -90,4 +96,22 @@ void ImpDecal::load_default_layers()
9096
"layer_display_decal.json"));
9197
}
9298

99+
bool ImpDecal::set_filename()
100+
{
101+
GtkFileChooserNative *native = gtk_file_chooser_native_new("Save Decal", GTK_WINDOW(main_window->gobj()),
102+
GTK_FILE_CHOOSER_ACTION_SAVE, "_Save", "_Cancel");
103+
auto chooser = Glib::wrap(GTK_FILE_CHOOSER(native));
104+
chooser->set_do_overwrite_confirmation(true);
105+
chooser->set_current_folder(Glib::build_filename(pool->get_base_path(), "decals"));
106+
chooser->set_current_name(name_entry->get_text() + ".json");
107+
108+
std::string filename;
109+
auto success = run_native_filechooser_with_retry(chooser, "Error saving decal", [this, chooser, &filename] {
110+
filename = append_dot_json(chooser->get_filename());
111+
pool->check_filename_throw(ObjectType::DECAL, filename);
112+
core_decal.set_filename(filename);
113+
});
114+
return success;
115+
}
116+
93117
}; // namespace horizon

Diff for: src/imp/imp_decal.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace horizon {
66
class ImpDecal : public ImpLayer {
77
public:
8-
ImpDecal(const std::string &decal_filename, const std::string &pool_path);
8+
ImpDecal(const std::string &decal_filename, const std::string &pool_path, TempMode temp_mode);
99

1010
protected:
1111
void construct() override;
@@ -27,5 +27,7 @@ class ImpDecal : public ImpLayer {
2727
Gtk::Entry *name_entry = nullptr;
2828

2929
void update_header();
30+
31+
bool set_filename() override;
3032
};
3133
} // namespace horizon

Diff for: src/imp/imp_frame.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
#include "header_button.hpp"
44
#include "core/tool_id.hpp"
55
#include "widgets/action_button.hpp"
6+
#include "util/gtk_util.hpp"
7+
#include "util/util.hpp"
68

79
namespace horizon {
8-
ImpFrame::ImpFrame(const std::string &frame_filename, const std::string &pool_path)
10+
ImpFrame::ImpFrame(const std::string &frame_filename, const std::string &pool_path, TempMode tmp_mode)
911
: ImpBase(pool_path), core_frame(frame_filename, *pool.get()), frame(core_frame.get_frame())
1012
{
1113
core = &core_frame;
1214
core_frame.signal_tool_changed().connect(sigc::mem_fun(*this, &ImpBase::handle_tool_change));
15+
if (tmp_mode == TempMode::YES) {
16+
core_frame.set_temp_mode();
17+
temp_mode = true;
18+
}
1319
}
1420

1521
void ImpFrame::canvas_update()
@@ -29,7 +35,7 @@ void ImpFrame::construct()
2935

3036
name_entry = header_button->add_entry("Name");
3137
name_entry->set_text(frame.name);
32-
name_entry->set_width_chars(std::min<int>(frame.name.size(), 20));
38+
name_entry->set_width_chars(std::max<int>(frame.name.size(), 20));
3339
name_entry->signal_changed().connect([this] { core_frame.set_needs_save(); });
3440
name_entry->signal_activate().connect(sigc::mem_fun(*this, &ImpFrame::update_header));
3541

@@ -52,4 +58,22 @@ void ImpFrame::update_header()
5258
set_window_title(name_entry->get_text());
5359
}
5460

61+
bool ImpFrame::set_filename()
62+
{
63+
GtkFileChooserNative *native = gtk_file_chooser_native_new("Save Frame", GTK_WINDOW(main_window->gobj()),
64+
GTK_FILE_CHOOSER_ACTION_SAVE, "_Save", "_Cancel");
65+
auto chooser = Glib::wrap(GTK_FILE_CHOOSER(native));
66+
chooser->set_do_overwrite_confirmation(true);
67+
chooser->set_current_folder(Glib::build_filename(pool->get_base_path(), "frames"));
68+
chooser->set_current_name(name_entry->get_text() + ".json");
69+
70+
std::string filename;
71+
auto success = run_native_filechooser_with_retry(chooser, "Error saving frame", [this, chooser, &filename] {
72+
filename = append_dot_json(chooser->get_filename());
73+
pool->check_filename_throw(ObjectType::FRAME, filename);
74+
core_frame.set_filename(filename);
75+
});
76+
return success;
77+
}
78+
5579
} // namespace horizon

Diff for: src/imp/imp_frame.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace horizon {
66
class ImpFrame : public ImpBase {
77
public:
8-
ImpFrame(const std::string &frame_filename, const std::string &pool_path);
8+
ImpFrame(const std::string &frame_filename, const std::string &pool_path, TempMode temp_mode);
99

1010
protected:
1111
void construct() override;
@@ -24,5 +24,7 @@ class ImpFrame : public ImpBase {
2424
Gtk::Entry *name_entry = nullptr;
2525

2626
void update_header();
27+
28+
bool set_filename() override;
2729
};
2830
} // namespace horizon

0 commit comments

Comments
 (0)