Add project info editor (#3754)

* Add button to add model info

* Initial impl of project info editor

* Add sidebar item to edit project info

* Add license selector

* Fix use of deprecated apis

* Fix license combox dark mode

* Add back button on project info editor screen

---------

Co-authored-by: SoftFever <softfeverever@gmail.com>
This commit is contained in:
Noisyfox 2024-01-21 22:13:37 +08:00 committed by GitHub
parent ead78a98f9
commit 122c5cedd8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 292 additions and 189 deletions

View file

@ -62,6 +62,11 @@ ProjectPanel::ProjectPanel(wxWindow *parent, wxWindowID id, const wxPoint &pos,
Bind(EVT_PROJECT_RELOAD, &ProjectPanel::on_reload, this);
m_auxiliary = new AuxiliaryPanel(this);
m_auxiliary->Hide();
main_sizer->Add(m_auxiliary, wxSizerFlags().Expand().Proportion(1));
Bind(EVT_AUXILIARY_DONE, [this](wxCommandEvent& e) { update_model_data();});
SetSizer(main_sizer);
Layout();
Fit();
@ -91,38 +96,39 @@ void ProjectPanel::on_reload(wxCommandEvent& evt)
std::string model_author;
std::string cover_file;
std::string description;
std::map<std::string, std::vector<json>> files;
std::string p_name;
std::string p_author;
std::string p_description;
std::string p_cover_file;
std::map<std::string, std::vector<json>> files;
Model model = wxGetApp().plater()->model();
license = model.model_info->license;
model_name = model.model_info->model_name;
cover_file = model.model_info->cover_file;
description = model.model_info->description;
update_type = model.model_info->origin;
auto model_info = model.model_info;
if (model_info != nullptr) {
license = model_info->license;
model_name = model_info->model_name;
cover_file = model_info->cover_file;
description = model_info->description;
update_type = model_info->origin;
try {
if (!model_info->copyright.empty()) {
json copy_right = json::parse(model_info->copyright);
try {
if (!model.model_info->copyright.empty()) {
json copy_right = json::parse(model.model_info->copyright);
if (copy_right.is_array()) {
for (auto it = copy_right.begin(); it != copy_right.end(); it++) {
if ((*it).contains("author")) {
model_author = (*it)["author"].get<std::string>();
if (copy_right.is_array()) {
for (auto it = copy_right.begin(); it != copy_right.end(); it++) {
if ((*it).contains("author")) {
model_author = (*it)["author"].get<std::string>();
}
}
}
}
} catch (...) {
;
}
}
catch (...) {
;
}
if (model_author.empty() && model.design_info != nullptr)
model_author = model.design_info->Designer;
@ -134,12 +140,44 @@ void ProjectPanel::on_reload(wxCommandEvent& evt)
p_author = model.profile_info->ProfileUserName;
}
//file info
// file info
std::string file_path = encode_path(wxGetApp().plater()->model().get_auxiliary_file_temp_path().c_str());
if (!file_path.empty()) {
files = Reload(file_path);
wxGetApp().CallAfter([this, file_path, files] { m_auxiliary->Reload(file_path, files); });
} else {
clear_model_info();
return;
}
else {
bool has_content = false;
for (const string& v : {
update_type,
license,
model_name,
model_author,
cover_file,
description,
p_name,
p_author,
p_description,
p_cover_file,
}) {
if (!v.empty()) {
has_content = true;
break;
}
}
if (!has_content) {
for (const auto & file : files) {
if (!file.second.empty()) {
has_content = true;
break;
}
}
}
if (!has_content) {
// Nothing to show, just return
clear_model_info();
return;
}
@ -180,6 +218,7 @@ void ProjectPanel::on_reload(wxCommandEvent& evt)
void ProjectPanel::msw_rescale()
{
m_auxiliary->msw_rescale();
}
void ProjectPanel::on_size(wxSizeEvent &event)
@ -215,6 +254,9 @@ void ProjectPanel::OnScriptMessage(wxWebViewEvent& evt)
else if (strCmd == "request_3mf_info") {
m_web_init_completed = true;
}
else if (strCmd == "edit_project_info") {
show_info_editor(true);
}
else if (strCmd == "debug_info") {
//wxString msg = j["msg"];
//OutputDebugString(wxString::Format("Model_Web: msg = %s \r\n", msg));
@ -227,14 +269,24 @@ void ProjectPanel::OnScriptMessage(wxWebViewEvent& evt)
}
}
void ProjectPanel::show_info_editor(bool show)
{
m_browser->Show(!show);
m_auxiliary->Show(show);
Layout();
}
void ProjectPanel::update_model_data()
{
Model model = wxGetApp().plater()->model();
show_info_editor(false);
clear_model_info();
m_auxiliary->init_auxiliary();
//basics info
if (model.model_info == nullptr)
return;
//if (model.model_info == nullptr)
// return;
auto event = wxCommandEvent(EVT_PROJECT_RELOAD);
event.SetEventObject(this);
@ -258,7 +310,6 @@ std::map<std::string, std::vector<json>> ProjectPanel::Reload(wxString aux_path)
{
std::vector<fs::path> dir_cache;
fs::directory_iterator iter_end;
wxString m_root_dir;
std::map<std::string, std::vector<json>> m_paths_list;
const static std::array<wxString, 5> s_default_folders = {
@ -276,24 +327,16 @@ std::map<std::string, std::vector<json>> ProjectPanel::Reload(wxString aux_path)
fs::path new_aux_path(aux_path.ToStdWstring());
try {
fs::remove_all(fs::path(m_root_dir.ToStdWstring()));
}
catch (...) {
BOOST_LOG_TRIVIAL(error) << "Failed removing the auxiliary directory" << m_root_dir.c_str();
}
m_root_dir = aux_path;
// Check new path. If not exist, create a new one.
if (!fs::exists(new_aux_path)) {
fs::create_directory(new_aux_path);
// Create default folders if they are not loaded
for (auto folder : s_default_folders) {
wxString folder_path = aux_path + "/" + folder;
if (fs::exists(folder_path.ToStdWstring())) continue;
fs::create_directory(folder_path.ToStdWstring());
}
return m_paths_list;
}
// Create default folders if they are not loaded
for (auto folder : s_default_folders) {
wxString folder_path = aux_path + "/" + folder;
if (fs::exists(folder_path.ToStdWstring())) continue;
fs::create_directory(folder_path.ToStdWstring());
}
// Load from new path
@ -320,7 +363,8 @@ std::map<std::string, std::vector<json>> ProjectPanel::Reload(wxString aux_path)
wxString file_name = encode_path(file_path.c_str());
wxStat(file_name, &strucStat);
wxFileOffset filelen = strucStat.st_size;
pfile_obj["_filepath"] = file_path;
pfile_obj["filename"] = wxGetApp().url_encode(file_path_obj.filename().string().c_str());
pfile_obj["size"] = formatBytes((unsigned long)filelen);