| 1 |
#include "duplicate_entity.hpp" |
| 2 |
#include "duplicate_unit.hpp" |
| 3 |
#include "duplicate_part.hpp" |
| 4 |
#include "duplicate_window.hpp" |
| 5 |
#include "pool/pool.hpp" |
| 6 |
#include "pool/part.hpp" |
| 7 |
#include "pool/unit.hpp" |
| 8 |
#include "pool/symbol.hpp" |
| 9 |
#include "util/gtk_util.hpp" |
| 10 |
#include "util/util.hpp" |
| 11 |
#include "widgets/location_entry.hpp" |
| 12 |
#include "nlohmann/json.hpp" |
| 13 |
#include "util/pool_completion.hpp" |
| 14 |
|
| 15 |
namespace horizon { |
| 16 |
class DuplicatePackageWidget : public Gtk::Box, public Changeable { |
| 17 |
public: |
| 18 |
DuplicatePackageWidget(Pool &p, const UUID &pkg_uuid) |
| 19 |
: Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10), pool(p), pkg(*pool.get_package(pkg_uuid)) |
| 20 |
{ |
| 21 |
auto explain_label = Gtk::manage(new Gtk::Label); |
| 22 |
explain_label->get_style_context()->add_class("dim-label"); |
| 23 |
explain_label->set_xalign(0); |
| 24 |
explain_label->set_text("The new part will reference the new package"); |
| 25 |
explain_label->show(); |
| 26 |
explain_label->set_margin_left(20); |
| 27 |
|
| 28 |
auto cb = Gtk::manage(new Gtk::CheckButton); |
| 29 |
auto la = Gtk::manage(new Gtk::Label); |
| 30 |
la->set_markup("<b>Package: " + pkg.name + "</b>"); |
| 31 |
cb->add(*la); |
| 32 |
cb->show_all(); |
| 33 |
cb->set_active(true); |
| 34 |
cb->signal_toggled().connect([this, cb, explain_label] { |
| 35 |
if (cb->get_active()) { |
| 36 |
explain_label->set_text("The new part will reference the new package"); |
| 37 |
} |
| 38 |
else { |
| 39 |
explain_label->set_text("The new part will reference the existing package"); |
| 40 |
} |
| 41 |
grid->set_visible(cb->get_active()); |
| 42 |
}); |
| 43 |
|
| 44 |
pack_start(*cb, false, false, 0); |
| 45 |
pack_start(*explain_label, false, false, 0); |
| 46 |
|
| 47 |
|
| 48 |
grid = Gtk::manage(new Gtk::Grid); |
| 49 |
grid->set_row_spacing(10); |
| 50 |
grid->set_column_spacing(10); |
| 51 |
grid->set_margin_left(20); |
| 52 |
int top = 0; |
| 53 |
|
| 54 |
name_entry = Gtk::manage(new Gtk::Entry); |
| 55 |
name_entry->set_text(pkg.name + " (Copy)"); |
| 56 |
name_entry->set_hexpand(true); |
| 57 |
grid_attach_label_and_widget(grid, "Name", name_entry, top); |
| 58 |
|
| 59 |
location_entry = Gtk::manage(new LocationEntry(pool.get_base_path())); |
| 60 |
location_entry->set_rel_filename(Glib::path_get_dirname(pool.get_rel_filename(ObjectType::PACKAGE, pkg.uuid)) |
| 61 |
+ "-copy"); |
| 62 |
location_entry->signal_changed().connect([this] { s_signal_changed.emit(); }); |
| 63 |
grid_attach_label_and_widget(grid, "Filename", location_entry, top); |
| 64 |
|
| 65 |
grid->show_all(); |
| 66 |
|
| 67 |
|
| 68 |
pack_start(*grid, true, true, 0); |
| 69 |
} |
| 70 |
|
| 71 |
UUID duplicate(std::vector<std::string> *filenames) |
| 72 |
{ |
| 73 |
if (grid->get_visible()) { |
| 74 |
return DuplicatePartWidget::duplicate_package(pool, pkg.uuid, location_entry->get_filename(), |
| 75 |
name_entry->get_text(), filenames); |
| 76 |
} |
| 77 |
else { |
| 78 |
return pkg.uuid; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
bool check_valid() |
| 83 |
{ |
| 84 |
if (!grid->get_visible()) |
| 85 |
return true; |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
private: |
| 91 |
Pool &pool; |
| 92 |
const Package &pkg; |
| 93 |
Gtk::Entry *name_entry = nullptr; |
| 94 |
class LocationEntry *location_entry = nullptr; |
| 95 |
Gtk::Grid *grid = nullptr; |
| 96 |
}; |
| 97 |
|
| 98 |
DuplicatePartWidget::DuplicatePartWidget(Pool &p, const UUID &part_uuid, Gtk::Box *ubox) |
| 99 |
: Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10), pool(p), part(*pool.get_part(part_uuid)) |
| 100 |
{ |
| 101 |
auto la = Gtk::manage(new Gtk::Label); |
| 102 |
la->set_markup("<b>Part: " + part.get_MPN() + "</b>"); |
| 103 |
la->set_xalign(0); |
| 104 |
la->show(); |
| 105 |
pack_start(*la, false, false, 0); |
| 106 |
|
| 107 |
grid = Gtk::manage(new Gtk::Grid); |
| 108 |
grid->set_row_spacing(10); |
| 109 |
grid->set_column_spacing(10); |
| 110 |
int top = 0; |
| 111 |
|
| 112 |
mpn_entry = Gtk::manage(new Gtk::Entry); |
| 113 |
mpn_entry->set_text(part.get_MPN() + " (Copy)"); |
| 114 |
mpn_entry->set_hexpand(true); |
| 115 |
grid_attach_label_and_widget(grid, "MPN", mpn_entry, top); |
| 116 |
|
| 117 |
manufacturer_entry = Gtk::manage(new Gtk::Entry); |
| 118 |
manufacturer_entry->set_text(part.get_manufacturer()); |
| 119 |
manufacturer_entry->set_hexpand(true); |
| 120 |
manufacturer_entry->set_completion(create_pool_manufacturer_completion(pool)); |
| 121 |
|
| 122 |
grid_attach_label_and_widget(grid, "Manufacturer", manufacturer_entry, top); |
| 123 |
|
| 124 |
location_entry = Gtk::manage(new LocationEntry(pool.get_base_path())); |
| 125 |
location_entry->set_append_json(true); |
| 126 |
location_entry->set_rel_filename( |
| 127 |
DuplicateUnitWidget::insert_filename(pool.get_rel_filename(ObjectType::PART, part.uuid), "-copy")); |
| 128 |
location_entry->signal_changed().connect([this] { s_signal_changed.emit(); }); |
| 129 |
grid_attach_label_and_widget(grid, "Filename", location_entry, top); |
| 130 |
|
| 131 |
grid->show_all(); |
| 132 |
grid->set_margin_bottom(10); |
| 133 |
|
| 134 |
pack_start(*grid, false, false, 0); |
| 135 |
|
| 136 |
dpw = Gtk::manage(new DuplicatePackageWidget(pool, part.package->uuid)); |
| 137 |
dpw->signal_changed().connect([this] { s_signal_changed.emit(); }); |
| 138 |
pack_start(*dpw, false, false, 0); |
| 139 |
dpw->show(); |
| 140 |
|
| 141 |
dew = Gtk::manage(new DuplicateEntityWidget(pool, part.entity->uuid, ubox, true)); |
| 142 |
dew->signal_changed().connect([this] { s_signal_changed.emit(); }); |
| 143 |
pack_start(*dew, false, false, 0); |
| 144 |
dew->show(); |
| 145 |
} |
| 146 |
|
| 147 |
UUID DuplicatePartWidget::duplicate(std::vector<std::string> *filenames) |
| 148 |
{ |
| 149 |
Part new_part(part); |
| 150 |
new_part.uuid = UUID::random(); |
| 151 |
new_part.attributes.at(Part::Attribute::MPN).second = mpn_entry->get_text(); |
| 152 |
new_part.attributes.at(Part::Attribute::MANUFACTURER).second = manufacturer_entry->get_text(); |
| 153 |
auto new_part_json = new_part.serialize(); |
| 154 |
|
| 155 |
auto entity_uuid = dew->duplicate(filenames); |
| 156 |
new_part_json["entity"] = (std::string)entity_uuid; |
| 157 |
|
| 158 |
auto pkg_uuid = dpw->duplicate(filenames); |
| 159 |
new_part_json["package"] = (std::string)pkg_uuid; |
| 160 |
|
| 161 |
auto part_filename = location_entry->get_filename(); |
| 162 |
if (filenames) |
| 163 |
filenames->push_back(part_filename); |
| 164 |
ensure_parent_dir(part_filename); |
| 165 |
save_json_to_file(part_filename, new_part_json); |
| 166 |
|
| 167 |
return new_part.uuid; |
| 168 |
} |
| 169 |
|
| 170 |
UUID DuplicatePartWidget::duplicate_package(Pool &pool, const UUID &uu, const std::string &new_dir, |
| 171 |
const std::string &new_name, std::vector<std::string> *filenames) |
| 172 |
{ |
| 173 |
auto padstack_dir = Glib::build_filename(new_dir, "padstacks"); |
| 174 |
auto fi = Gio::File::create_for_path(padstack_dir); |
| 175 |
fi->make_directory_with_parents(); |
| 176 |
Package pkg(*pool.get_package(uu)); |
| 177 |
pkg.uuid = UUID::random(); |
| 178 |
pkg.name = new_name; |
| 179 |
auto pkg_json = pkg.serialize(); |
| 180 |
SQLite::Query q(pool.db, "SELECT uuid FROM padstacks WHERE package=?"); |
| 181 |
q.bind(1, uu); |
| 182 |
while (q.step()) { |
| 183 |
UUID padstack_uuid(q.get<std::string>(0)); |
| 184 |
auto padstack_filename = pool.get_filename(ObjectType::PADSTACK, padstack_uuid); |
| 185 |
auto padstack_basename = Glib::path_get_basename(padstack_filename); |
| 186 |
Padstack padstack(*pool.get_padstack(padstack_uuid)); |
| 187 |
padstack.uuid = UUID::random(); |
| 188 |
for (const auto &it : pkg.pads) { |
| 189 |
if (it.second.pool_padstack->uuid == padstack_uuid) { |
| 190 |
pkg_json["pads"][(std::string)it.first]["padstack"] = (std::string)padstack.uuid; |
| 191 |
} |
| 192 |
} |
| 193 |
auto new_padstack_filename = Glib::build_filename(padstack_dir, padstack_basename); |
| 194 |
ensure_parent_dir(new_padstack_filename); |
| 195 |
save_json_to_file(new_padstack_filename, padstack.serialize()); |
| 196 |
if (filenames) |
| 197 |
filenames->push_back(new_padstack_filename); |
| 198 |
} |
| 199 |
std::string new_pkg_filename = Glib::build_filename(new_dir, "package.json"); |
| 200 |
if (filenames) |
| 201 |
filenames->push_back(new_pkg_filename); |
| 202 |
ensure_parent_dir(new_pkg_filename); |
| 203 |
save_json_to_file(new_pkg_filename, pkg_json); |
| 204 |
return pkg.uuid; |
| 205 |
} |
| 206 |
|
| 207 |
bool DuplicatePartWidget::check_valid() |
| 208 |
{ |
| 209 |
bool valid = true; |
| 210 |
valid = location_entry->check_ends_json() && valid; |
| 211 |
valid = dpw->check_valid() && valid; |
| 212 |
valid = dew->check_valid() && valid; |
| 213 |
return valid; |
| 214 |
} |
| 215 |
|
| 216 |
} // namespace horizon |