BulkExportDialog: Added question dialog for the case, when already existed bed name is used

+ Use wxFlexGridSizer instead of nested wxBoxSizers and Layout() calls
This commit is contained in:
YuSanka 2024-12-12 23:05:54 +01:00 committed by Lukas Matena
parent cbd85c7528
commit 0de63d0db8
2 changed files with 42 additions and 28 deletions

View File

@ -18,7 +18,7 @@
#include "GUI.hpp" #include "GUI.hpp"
#include "GUI_App.hpp" #include "GUI_App.hpp"
#include "format.hpp" #include "format.hpp"
#include "Tab.hpp" #include "MsgDialog.hpp"
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
@ -27,7 +27,7 @@ namespace GUI {
constexpr auto BORDER_W = 10; constexpr auto BORDER_W = 10;
void BulkExportDialog::Item::init_input_name_ctrl(wxBoxSizer* row_sizer, const std::string &path) void BulkExportDialog::Item::init_input_name_ctrl(wxFlexGridSizer* row_sizer, const std::string &path)
{ {
#ifdef _WIN32 #ifdef _WIN32
const long style = wxBORDER_SIMPLE; const long style = wxBORDER_SIMPLE;
@ -39,10 +39,10 @@ void BulkExportDialog::Item::init_input_name_ctrl(wxBoxSizer* row_sizer, const s
m_text_ctrl->Bind(wxEVT_TEXT, [this](wxCommandEvent&) { update(); }); m_text_ctrl->Bind(wxEVT_TEXT, [this](wxCommandEvent&) { update(); });
m_text_ctrl->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& event) { event.Enable(selected); }); m_text_ctrl->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& event) { event.Enable(selected); });
row_sizer->Add(m_text_ctrl, 1, wxEXPAND, BORDER_W); row_sizer->Add(m_text_ctrl, 1, wxEXPAND);
} }
void BulkExportDialog::Item::init_selection_ctrl(wxBoxSizer* row_sizer, int bed_index) void BulkExportDialog::Item::init_selection_ctrl(wxFlexGridSizer* row_sizer, int bed_index)
{ {
m_checkbox = new ::CheckBox(m_parent, std::to_string(bed_index + 1)); m_checkbox = new ::CheckBox(m_parent, std::to_string(bed_index + 1));
m_checkbox->SetFont(wxGetApp().bold_font()); m_checkbox->SetFont(wxGetApp().bold_font());
@ -51,13 +51,13 @@ void BulkExportDialog::Item::init_selection_ctrl(wxBoxSizer* row_sizer, int bed_
this->selected = event.IsChecked(); this->selected = event.IsChecked();
}); });
row_sizer->Add(m_checkbox, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, BORDER_W); row_sizer->Add(m_checkbox, 0, wxALIGN_CENTER_VERTICAL);
m_checkbox->SetValue(this->selected); m_checkbox->SetValue(this->selected);
} }
BulkExportDialog::Item::Item( BulkExportDialog::Item::Item(
wxWindow *parent, wxWindow *parent,
wxBoxSizer *sizer, wxFlexGridSizer*sizer,
const fs::path &path, const fs::path &path,
const int bed_index, const int bed_index,
Validator validator Validator validator
@ -69,12 +69,9 @@ BulkExportDialog::Item::Item(
m_validator(std::move(validator)), m_validator(std::move(validator)),
m_directory(path.parent_path()) m_directory(path.parent_path())
{ {
wxBoxSizer* row_sizer = new wxBoxSizer(wxHORIZONTAL); init_selection_ctrl(sizer, bed_index);
init_selection_ctrl(row_sizer, bed_index); init_input_name_ctrl(sizer, path.filename().string());
init_input_name_ctrl(row_sizer, path.filename().string()); sizer->Add(m_valid_bmp, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, BORDER_W);
row_sizer->Add(m_valid_bmp, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, BORDER_W);
sizer->Add(row_sizer,0, wxEXPAND | wxTOP, BORDER_W);
m_valid_bmp->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& event) { event.Show(selected); }); m_valid_bmp->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& event) { event.Show(selected); });
update(); update();
@ -177,8 +174,6 @@ void BulkExportDialog::Item::update()
m_status = status; m_status = status;
update_valid_bmp(); update_valid_bmp();
m_parent->Layout();
} }
std::string get_bmp_name(const BulkExportDialog::ItemStatus status) { std::string get_bmp_name(const BulkExportDialog::ItemStatus status) {
@ -214,7 +209,7 @@ BulkExportDialog::BulkExportDialog(const std::vector<std::pair<int, fs::path>> &
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
m_sizer = new wxBoxSizer(wxVERTICAL); m_sizer = new wxFlexGridSizer(paths.size(), 3, wxSize(0.5*BORDER_W, BORDER_W));
for (const auto&[bed_index, path] : paths) { for (const auto&[bed_index, path] : paths) {
AddItem(path, bed_index); AddItem(path, bed_index);
@ -223,6 +218,7 @@ BulkExportDialog::BulkExportDialog(const std::vector<std::pair<int, fs::path>> &
// Add dialog's buttons // Add dialog's buttons
wxStdDialogButtonSizer* btns = this->CreateStdDialogButtonSizer(wxOK | wxCANCEL); wxStdDialogButtonSizer* btns = this->CreateStdDialogButtonSizer(wxOK | wxCANCEL);
wxButton* btnOK = static_cast<wxButton*>(this->FindWindowById(wxID_OK, this)); wxButton* btnOK = static_cast<wxButton*>(this->FindWindowById(wxID_OK, this));
btnOK->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { accept(); });
btnOK->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(enable_ok_btn()); }); btnOK->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(enable_ok_btn()); });
topSizer->Add(m_sizer, 0, wxEXPAND | wxALL, BORDER_W); topSizer->Add(m_sizer, 0, wxEXPAND | wxALL, BORDER_W);
@ -244,6 +240,19 @@ void BulkExportDialog::AddItem(const fs::path& path, int bed_index)
m_items.push_back(std::make_unique<Item>(this, m_sizer, path, bed_index, PathValidator{m_items})); m_items.push_back(std::make_unique<Item>(this, m_sizer, path, bed_index, PathValidator{m_items}));
} }
void BulkExportDialog::accept()
{
if (has_warnings()) {
MessageDialog dialog(nullptr,
_L("Some of the selected files already exists. Do you want to replace them?"),
_L("Export Beds"), wxYES_NO | wxICON_QUESTION);
if (dialog.ShowModal() == wxID_NO)
return;
}
EndModal(wxID_OK);
}
bool BulkExportDialog::enable_ok_btn() const bool BulkExportDialog::enable_ok_btn() const
{ {
for (const auto &item : m_items) for (const auto &item : m_items)
@ -261,13 +270,6 @@ bool BulkExportDialog::enable_ok_btn() const
return !all_unselected; return !all_unselected;
} }
bool BulkExportDialog::Layout()
{
const bool ret = DPIDialog::Layout();
this->Fit();
return ret;
}
std::vector<std::pair<int, std::optional<fs::path>>> BulkExportDialog::get_paths() const { std::vector<std::pair<int, std::optional<fs::path>>> BulkExportDialog::get_paths() const {
std::vector<std::pair<int, std::optional<fs::path>>> result; std::vector<std::pair<int, std::optional<fs::path>>> result;
std::transform( std::transform(
@ -284,6 +286,15 @@ std::vector<std::pair<int, std::optional<fs::path>>> BulkExportDialog::get_paths
return result; return result;
} }
bool BulkExportDialog::has_warnings() const
{
for (const auto& item : m_items)
if (item->selected && item->is_warning()) {
return true;
}
return false;
}
void BulkExportDialog::on_dpi_changed(const wxRect&) void BulkExportDialog::on_dpi_changed(const wxRect&)
{ {
const int& em = em_unit(); const int& em = em_unit();

View File

@ -17,6 +17,7 @@ class wxString;
class wxStaticText; class wxStaticText;
class wxTextCtrl; class wxTextCtrl;
class wxStaticBitmap; class wxStaticBitmap;
class wxFlexGridSizer;
namespace Slic3r { namespace Slic3r {
class Print; class Print;
@ -39,7 +40,7 @@ public:
>; >;
Item( Item(
wxWindow *parent, wxWindow *parent,
wxBoxSizer *sizer, wxFlexGridSizer *sizer,
const boost::filesystem::path &path, const boost::filesystem::path &path,
const int bed_index, const int bed_index,
Validator validator Validator validator
@ -53,7 +54,8 @@ public:
// directly to its address. // directly to its address.
void update_valid_bmp(); void update_valid_bmp();
bool is_valid() const { return m_status != ItemStatus::NoValid; } bool is_valid() const { return m_status != ItemStatus::NoValid; }
bool is_warning() const { return m_status == ItemStatus::Warning; }
boost::filesystem::path path; boost::filesystem::path path;
int bed_index{}; int bed_index{};
@ -68,21 +70,21 @@ public:
Validator m_validator; Validator m_validator;
boost::filesystem::path m_directory{}; boost::filesystem::path m_directory{};
void init_input_name_ctrl(wxBoxSizer *row_sizer, const std::string &path); void init_input_name_ctrl(wxFlexGridSizer*row_sizer, const std::string &path);
void init_selection_ctrl(wxBoxSizer *row_sizer, int bed_index); void init_selection_ctrl(wxFlexGridSizer*row_sizer, int bed_index);
void update(); void update();
}; };
private: private:
// This must be a unique ptr, because Item does not have copy nor move constructors. // This must be a unique ptr, because Item does not have copy nor move constructors.
std::vector<std::unique_ptr<Item>> m_items; std::vector<std::unique_ptr<Item>> m_items;
wxBoxSizer *m_sizer{nullptr}; wxFlexGridSizer*m_sizer{nullptr};
public: public:
BulkExportDialog(const std::vector<std::pair<int, boost::filesystem::path>> &paths); BulkExportDialog(const std::vector<std::pair<int, boost::filesystem::path>> &paths);
bool Layout() override;
std::vector<std::pair<int, std::optional<boost::filesystem::path>>> get_paths() const; std::vector<std::pair<int, std::optional<boost::filesystem::path>>> get_paths() const;
bool has_warnings() const;
protected: protected:
void on_dpi_changed(const wxRect &) override; void on_dpi_changed(const wxRect &) override;
@ -90,6 +92,7 @@ protected:
private: private:
void AddItem(const boost::filesystem::path &path, int bed_index); void AddItem(const boost::filesystem::path &path, int bed_index);
void accept();
bool enable_ok_btn() const; bool enable_ok_btn() const;
}; };