Browse Source

GalleryDialog improvements:
* Added "Replace PNG" function
* Added locks instead of border for system shapes

YuSanka 3 years ago
parent
commit
0cdc54b710
2 changed files with 82 additions and 3 deletions
  1. 10 0
      resources/icons/lock.svg
  2. 72 3
      src/slic3r/GUI/GalleryDialog.cpp

+ 10 - 0
resources/icons/lock.svg

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 23.0.3, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<svg version="1.0" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 viewBox="0 0 16 16" enable-background="new 0 0 16 16" xml:space="preserve">
+<g id="lock_x5F_closed">
+	<path fill="none" stroke="#ED6B21" stroke-width="2" stroke-miterlimit="10" d="M4,8V4c0,0,0-2,2-2c1,0,3,0,4,0c2,0,2,2,2,2v4"/>
+	<path fill="#ED6B21" d="M13,8H3C2.45,8,2,8.45,2,9v5c0,0.55,0.45,1,1,1h10c0.55,0,1-0.45,1-1V9C14,8.45,13.55,8,13,8z M10,12H8.91
+		c-0.21,0.58-0.76,1-1.41,1C6.67,13,6,12.33,6,11.5S6.67,10,7.5,10c0.65,0,1.2,0.42,1.41,1H10V12z"/>
+</g>
+</svg>

+ 72 - 3
src/slic3r/GUI/GalleryDialog.cpp

@@ -77,6 +77,8 @@ GalleryDialog::GalleryDialog(wxWindow* parent) :
     });
 
     wxStdDialogButtonSizer* buttons = this->CreateStdDialogButtonSizer(wxOK | wxCANCEL);
+    wxButton* ok_btn = static_cast<wxButton*>(FindWindowById(wxID_OK, this));
+    ok_btn->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(!m_selected_items.empty()); });
 
     auto add_btn = [this, buttons]( size_t pos, int& ID, wxString title, wxString tooltip,
                                     void (GalleryDialog::* method)(wxEvent&), 
@@ -147,7 +149,7 @@ static void add_border(wxImage& image)
 
     int width = image.GetWidth();
     int height = image.GetHeight();
-    int border_width = 2;
+    int border_width = 1;
 
     for (size_t x = 0; x < width; ++x) {
         for (size_t y = 0; y < height; ++y) {
@@ -165,6 +167,48 @@ static void add_border(wxImage& image)
     }
 }
 
+static void add_lock(wxImage& image) 
+{
+    wxBitmap bmp = create_scaled_bitmap("lock", nullptr, 22);
+
+    wxImage lock_image = bmp.ConvertToImage();
+    if (!lock_image.IsOk() || lock_image.GetWidth() == 0 || lock_image.GetHeight() == 0)
+        return;
+
+    int icon_sz = 16;
+    auto lock_px_data = (uint8_t*)lock_image.GetData();
+    auto lock_a_data = (uint8_t*)lock_image.GetAlpha();
+    int lock_width  = lock_image.GetWidth();
+    int lock_height = lock_image.GetHeight();
+    
+    auto px_data = (uint8_t*)image.GetData();
+    auto a_data = (uint8_t*)image.GetAlpha();
+
+    int width = image.GetWidth();
+    int height = image.GetHeight();
+
+    size_t beg_x = width - lock_width;
+    size_t beg_y = height - lock_height;
+    for (size_t x = 0; x < lock_width; ++x) {
+        for (size_t y = 0; y < lock_height; ++y) {
+            const size_t lock_idx = (x + y * lock_width);
+            if (lock_a_data && lock_a_data[lock_idx] == 0)
+                continue;
+
+            const size_t idx = (beg_x + x + (beg_y + y) * width);
+            if (a_data)
+                a_data[idx] = lock_a_data[lock_idx];
+
+            const size_t idx_rgb = (beg_x + x + (beg_y + y) * width) * 3;
+            const size_t lock_idx_rgb = (x + y * lock_width) * 3;
+            px_data[idx_rgb] = lock_px_data[lock_idx_rgb];
+            px_data[idx_rgb + 1] = lock_px_data[lock_idx_rgb + 1];
+            px_data[idx_rgb + 2] = lock_px_data[lock_idx_rgb + 2];
+        }
+    }
+//    add_border(image);
+}
+
 static void add_def_img(wxImageList* img_list, bool is_system, std::string stl_path)
 {
     wxBitmap bmp = create_scaled_bitmap("cog", nullptr, IMG_PX_CNT, true);    
@@ -172,7 +216,7 @@ static void add_def_img(wxImageList* img_list, bool is_system, std::string stl_p
     if (is_system) {
         wxImage image = bmp.ConvertToImage();
         if (image.IsOk() && image.GetWidth() != 0 && image.GetHeight() != 0) {
-            add_border(image);
+            add_lock(image);
             bmp = wxBitmap(std::move(image));
         }
     }
@@ -252,7 +296,7 @@ void GalleryDialog::load_label_icon_list()
         image.Rescale(px_cnt, px_cnt, wxIMAGE_QUALITY_BILINEAR);
 
         if (item.is_system)
-            add_border(image);
+            add_lock(image);
         wxBitmap bmp = wxBitmap(std::move(image));
         m_image_list->Add(bmp);
     }
@@ -312,6 +356,31 @@ void GalleryDialog::del_custom_shapes(wxEvent& event)
 
 void GalleryDialog::replace_custom_png(wxEvent& event)
 {
+    if (m_selected_items.size() != 1 || m_selected_items[0].is_system)
+        return;
+
+    wxFileDialog dialog(this, _L("Choose one PNG file:"),
+                        from_u8(wxGetApp().app_config->get_last_dir()), "",
+                        "PNG files (*.png)|*.png;*.PNG", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
+    if (dialog.ShowModal() != wxID_OK)
+        return;
+
+    wxArrayString input_files;
+    dialog.GetPaths(input_files);
+
+    if (input_files.IsEmpty())
+        return;
+
+    try {
+        fs::path current = fs::path(into_u8(input_files.Item(0)));
+        fs::copy_file(current, get_dir(false) / (m_selected_items[0].name + ".png"));
+    }
+    catch (fs::filesystem_error const& e) {
+        std::cerr << e.what() << '\n';
+        return;
+    }
+
+    update();
 }
 
 void GalleryDialog::select(wxListEvent& event)