Skip to content

Commit

Permalink
pool manager: add 'move to other pool'
Browse files Browse the repository at this point in the history
  • Loading branch information
carrotIndustries committed Apr 7, 2021
1 parent b720230 commit 13f4efb
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ SRC_POOL_PRJ_MGR = \
src/pool-prj-mgr/pools_window/pool_status_provider.cpp\
src/pool-prj-mgr/pools_window/pool_merge_box.cpp\
src/pool-prj-mgr/pools_window/pool_download_window.cpp\
src/pool-prj-mgr/pool-mgr/move_window.cpp\

SRC_PGM_TEST = \
src/pgm-test/pgm-test.cpp
Expand Down
1 change: 1 addition & 0 deletions imp.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<file>pool-prj-mgr/pool-mgr/github_login_window.ui</file>
<file>pool-prj-mgr/pool-mgr/import_kicad_package_window.ui</file>
<file>pool-prj-mgr/pool-mgr/pool_cache_box.ui</file>
<file>pool-prj-mgr/pool-mgr/move_window.ui</file>
<file>pool-prj-mgr/prj-mgr/part_browser/part_browser.ui</file>
<file>pool-prj-mgr/welcome.ui</file>
<file>pool-prj-mgr/output_window.ui</file>
Expand Down
168 changes: 168 additions & 0 deletions src/pool-prj-mgr/pool-mgr/move_window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include "move_window.hpp"
#include "pool/pool.hpp"
#include "util/gtk_util.hpp"
#include "common/object_descr.hpp"
#include "widgets/location_entry.hpp"
#include "pool/pool_manager.hpp"

namespace horizon {
MoveWindow *MoveWindow::create(Pool &pool, ObjectType type, const UUID &uu)
{
MoveWindow *w;
Glib::RefPtr<Gtk::Builder> x = Gtk::Builder::create();
x->add_from_resource("/org/horizon-eda/horizon/pool-prj-mgr/pool-mgr/move_window.ui");
x->get_widget_derived("window", w, pool, type, uu);

return w;
}

class MoveItemRow : public Gtk::Box {
public:
MoveItemRow(MoveWindow &p, SQLite::Query &q)
: Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 10), parent(p), filename(q.get<std::string>(0))
{
property_margin() = 5;
set_margin_end(10);
const auto type = q.get<ObjectType>(1);
const auto name = q.get<std::string>(2);
cb_item = Gtk::manage(new Gtk::CheckButton());
{
auto box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 5));
{
auto la = Gtk::manage(new Gtk::Label(object_descriptions.at(type).name));
la->set_xalign(0);
parent.sg_type->add_widget(*la);
box->pack_start(*la, false, false, 0);
}
{
auto la = Gtk::manage(new Gtk::Label(name));
la->set_xalign(0);
box->pack_start(*la, false, false, 0);
}
cb_item->add(*box);
}
cb_item->show_all();
cb_item->set_active(true);
parent.sg_item->add_widget(*cb_item);
pack_start(*cb_item, false, false, 0);


{
auto la = Gtk::manage(new Gtk::Label(filename));
la->set_xalign(0);
la->show();
parent.sg_src->add_widget(*la);
pack_start(*la, false, false, 0);
}

entry = Gtk::manage(new LocationEntry);
entry->show();
entry->set_rel_filename(filename);
parent.sg_dest->add_widget(*entry);
pack_start(*entry, true, true, 0);
}

MoveWindow &parent;
Gtk::CheckButton *cb_item;
LocationEntry *entry;
const std::string filename;
};


MoveWindow::MoveWindow(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, Pool &apool, ObjectType type,
const UUID &uu)
: Gtk::Window(cobject), pool(apool), window_state_store(this, "move-pool-item")
{
GET_OBJECT(sg_item);
GET_OBJECT(sg_src);
GET_OBJECT(sg_dest);
sg_type = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);

SQLite::Query q(pool.get_db(),
"WITH RECURSIVE "
"deps(typex, uuidx) AS "
"( SELECT $type, $uuid UNION "
"SELECT dep_type, dep_uuid FROM dependencies, deps "
"WHERE dependencies.type = deps.typex AND dependencies.uuid = deps.uuidx) "
", deps_sym(typey, uuidy) AS (SELECT * FROM deps UNION SELECT 'symbol', symbols.uuid FROM "
"symbols INNER JOIN deps ON (symbols.unit = uuidx AND typex = 'unit')) "
", where_used(typex, uuidx) AS ( SELECT $type, $uuid UNION "
"SELECT type, uuid FROM dependencies, where_used "
"WHERE dependencies.dep_type = where_used.typex "
"AND dependencies.dep_uuid = where_used.uuidx) "

"SELECT filename, type, name FROM deps_sym LEFT JOIN all_items_view "
"ON(all_items_view.type =deps_sym.typey AND all_items_view.uuid = deps_sym.uuidy) "
"WHERE all_items_view.pool_uuid = $pool "

"UNION SELECT model_filename, 'model_3d', '' FROM models INNER JOIN deps "
"ON (deps.typex = 'package' AND deps.uuidx = models.package_uuid) "
"LEFT JOIN packages ON (models.package_uuid = packages.uuid) "
"WHERE packages.pool_uuid = $pool "

"UNION SELECT filename, type, name FROM where_used "
"LEFT JOIN all_items_view "
"ON (where_used.typex = all_items_view.type "
"AND where_used.uuidx = all_items_view.uuid) "
"WHERE all_items_view.pool_uuid = $pool");
q.bind("$type", type);
q.bind("$uuid", uu);
q.bind("$pool", pool.get_pool_info().uuid);
GET_WIDGET(listbox);

while (q.step()) {
auto w = Gtk::manage(new MoveItemRow(*this, q));
w->show();
listbox->append(*w);
}

GET_WIDGET(pool_combo);
for (const auto &[path, it] : PoolManager::get().get_pools()) {
if (path != pool.get_base_path()) {
pool_combo->append(path, it.name + " (" + path + ")");
}
}
pool_combo->signal_changed().connect([this] {
const std::string path = pool_combo->get_active_id();
for (auto ch : listbox->get_children()) {
if (auto row = dynamic_cast<Gtk::ListBoxRow *>(ch)) {
if (auto w = dynamic_cast<MoveItemRow *>(row->get_child())) {
w->entry->set_relative_to(path);
}
}
}
});
pool_combo->set_active(0);

{
Gtk::Button *button_move;
GET_WIDGET(button_move);
button_move->signal_clicked().connect(sigc::mem_fun(*this, &MoveWindow::do_move));
}
}

void MoveWindow::do_move()
{
for (auto ch : listbox->get_children()) {
if (auto row = dynamic_cast<Gtk::ListBoxRow *>(ch)) {
if (auto w = dynamic_cast<MoveItemRow *>(row->get_child())) {
if (w->cb_item->get_active()) {
auto src = Gio::File::create_for_path(Glib::build_filename(pool.get_base_path(), w->filename));
auto dest = Gio::File::create_for_path(w->entry->get_filename());
try {
dest->get_parent()->make_directory_with_parents();
}
catch (Gio::Error &e) {
if (e.code() != Gio::Error::EXISTS)
throw;
}
src->move(dest);
}
}
}
}
moved = true;
hide();
}

} // namespace horizon
34 changes: 34 additions & 0 deletions src/pool-prj-mgr/pool-mgr/move_window.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include <gtkmm.h>
#include "util/window_state_store.hpp"
#include "common/common.hpp"
#include "util/uuid.hpp"

namespace horizon {
class MoveWindow : public Gtk::Window {
public:
friend class MoveItemRow;
MoveWindow(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &x, class Pool &pool, ObjectType type,
const UUID &uu);
static MoveWindow *create(class Pool &pool, ObjectType type, const UUID &uu);
bool get_moved() const
{
return moved;
}

private:
void do_move();
class Pool &pool;
Gtk::ComboBoxText *pool_combo = nullptr;
Gtk::ListBox *listbox = nullptr;
bool moved = false;


Glib::RefPtr<Gtk::SizeGroup> sg_item;
Glib::RefPtr<Gtk::SizeGroup> sg_type;
Glib::RefPtr<Gtk::SizeGroup> sg_src;
Glib::RefPtr<Gtk::SizeGroup> sg_dest;

WindowStateStore window_state_store;
};
} // namespace horizon
Loading

0 comments on commit 13f4efb

Please sign in to comment.