Debian Codegraph @

src/pool-prj-mgr/pool-mgr/duplicate/duplicate_window.cpp

raw
1 #include "duplicate_window.hpp"
2 #include "duplicate_unit.hpp"
3 #include "duplicate_entity.hpp"
4 #include "duplicate_part.hpp"
5 #include "common/object_descr.hpp"
6
7 namespace horizon {
8 DuplicateWindow::DuplicateWindow(class Pool &p, ObjectType ty, const UUID &uu) : Gtk::Window(), pool(p)
9 {
10 set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG);
11 auto hb = Gtk::manage(new Gtk::HeaderBar());
12 set_titlebar(*hb);
13
14 duplicate_button = Gtk::manage(new Gtk::Button("Duplicate"));
15 hb->pack_start(*duplicate_button);
16
17 hb->show_all();
18 hb->set_show_close_button(true);
19
20 auto box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 20));
21 box->property_margin() = 20;
22 box->show();
23 add(*box);
24
25 set_title("Duplicate " + object_descriptions.at(ty).name);
26
27 if (ty == ObjectType::UNIT) {
28 auto w = Gtk::manage(new DuplicateUnitWidget(pool, uu, false));
29 box->pack_start(*w, true, true, 0);
30 w->show();
31 duplicate_widget = w;
32 }
33 else if (ty == ObjectType::ENTITY) {
34 auto ubox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10));
35 auto w = Gtk::manage(new DuplicateEntityWidget(pool, uu, ubox, false));
36 box->pack_start(*w, true, true, 0);
37 box->pack_start(*ubox, true, true, 0);
38 w->show();
39 ubox->show();
40 duplicate_widget = w;
41 }
42 else if (ty == ObjectType::PART) {
43 auto ubox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10));
44 auto w = Gtk::manage(new DuplicatePartWidget(pool, uu, ubox));
45 box->pack_start(*w, true, true, 0);
46 box->pack_start(*ubox, true, true, 0);
47 w->show();
48 ubox->show();
49 duplicate_widget = w;
50 }
51 duplicate_button->signal_clicked().connect(sigc::mem_fun(*this, &DuplicateWindow::handle_duplicate));
52 duplicate_widget->signal_changed().connect([this] { check_valid(); });
53 check_valid();
54 }
55
56 std::vector<std::string> DuplicateWindow::get_filenames() const
57 {
58 return filenames;
59 }
60
61 void DuplicateWindow::check_valid()
62 {
63 duplicate_button->set_sensitive(duplicate_widget->check_valid());
64 }
65
66 void DuplicateWindow::handle_duplicate()
67 {
68 if (!duplicate_widget)
69 return;
70 std::string error_str;
71 try {
72 filenames.clear();
73 duplicate_widget->duplicate(&filenames);
74 }
75 catch (const std::exception &e) {
76 error_str = e.what();
77 }
78 catch (const Gio::Error &e) {
79 error_str = "io error: " + e.what();
80 }
81 catch (...) {
82 error_str = "unknown exception";
83 }
84 if (error_str.size()) {
85 filenames.clear();
86 Gtk::MessageDialog md(*this, "Error duplicating", false /* use_markup */, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
87 md.set_secondary_text(error_str);
88 md.run();
89 }
90 close();
91 }
92 } // namespace horizon