From eb9802022e3ccad2537956d1334332c5a5f662b4 Mon Sep 17 00:00:00 2001 From: supermerill Date: Mon, 29 Jun 2020 15:27:36 +0200 Subject: [PATCH] freepyscad: add comment/uncomment shortcut (ctrl+K/U) --- src/slic3r/GUI/FreeCADDialog.cpp | 63 ++++++++++++++++++++++++++++++++ src/slic3r/GUI/FreeCADDialog.hpp | 2 + 2 files changed, 65 insertions(+) diff --git a/src/slic3r/GUI/FreeCADDialog.cpp b/src/slic3r/GUI/FreeCADDialog.cpp index e993bc1f2..b41c1229e 100644 --- a/src/slic3r/GUI/FreeCADDialog.cpp +++ b/src/slic3r/GUI/FreeCADDialog.cpp @@ -528,11 +528,69 @@ void FreeCADDialog::on_char_add(wxStyledTextEvent& event) { stc->InsertText(current_pos, "\""); } } +void FreeCADDialog::comment(bool is_switch) { + //if selction, do that for every line + std::vector lines; + if (m_text->LineFromPosition(m_text->GetSelectionStart()) < m_text->LineFromPosition(m_text->GetSelectionEnd()) ) { + for(int i= m_text->LineFromPosition(m_text->GetSelectionStart()); i <= m_text->LineFromPosition(m_text->GetSelectionEnd()); i++) + lines.push_back(i); + } else { + lines.push_back(m_text->GetCurrentLine()); + } + + //get last \n pos or 0 + int saved_pos = m_text->GetCurrentPos(); + int begin = m_text->GetSelectionStart(); + int end = m_text->GetSelectionEnd(); + + for (int l : lines) { + int commentpos = m_text->PositionFromLine(l); + //skip ' ' and '\t' + if(is_switch) + while (m_text->GetCharAt(commentpos) == ' ' || m_text->GetCharAt(commentpos) == '\t') + commentpos++; + if (m_text->GetCharAt(commentpos) == '#' && is_switch) { + m_text->SetTargetStart(commentpos); + m_text->SetTargetEnd(commentpos + 1); + m_text->ReplaceTarget(""); + if (commentpos < begin) { + begin--; + } + end--; + saved_pos--; + } else { + m_text->InsertText(commentpos, "#"); + if (commentpos < begin) { + begin++; + } + end++; + saved_pos++; + } + } + + m_text->SetCurrentPos(saved_pos); //TODO +- some if # added/removed + if (begin < end) { + m_text->SetSelectionStart(begin); + m_text->SetSelectionEnd(end); + } +} + +void FreeCADDialog::on_char_type(wxKeyEvent &event) { + //std::cout << "on_char_type " << event.GetUnicodeKey() <<", " << event.GetModifiers() << "\n"; + if (event.GetUnicodeKey() == 'Q' && event.GetModifiers() == wxMOD_CONTROL) { + comment(true); + } else if (event.GetUnicodeKey() == 'K' && event.GetModifiers() == wxMOD_CONTROL) { + comment(false); + } else { + event.Skip(true); + } +} // note: this works on KEY, not on CHAR, so be sure the key is the right one for all keyboard layout. // space, back, del are ok but no ascii char void FreeCADDialog::on_key_type(wxKeyEvent& event) { + //std::cout << "on_key_type " << event.GetUnicodeKey() << " ? "<< int('Q') <<", "<< event.GetKeyCode() << ", " << event.GetModifiers() << "\n"; if (event.GetKeyCode() == WXK_SPACE && event.GetModifiers() == wxMOD_CONTROL) { //get word, if any @@ -603,6 +661,10 @@ void FreeCADDialog::on_key_type(wxKeyEvent& event) event.Skip(true); } else if (event.GetKeyCode() == WXK_ESCAPE && m_text != nullptr && m_text->AutoCompActive()) { m_text->AutoCompCancel(); + }else if (event.GetUnicodeKey() == 'Q' && event.GetModifiers() == wxMOD_CONTROL) { + comment(true); + } else if (event.GetUnicodeKey() == 'K' && event.GetModifiers() == wxMOD_CONTROL) { + comment(false); } else { event.Skip(true); } @@ -633,6 +695,7 @@ void FreeCADDialog::createSTC() m_text->Bind(wxEVT_STC_MODIFIED, &FreeCADDialog::on_word_change_for_autocomplete, this); m_text->Bind(wxEVT_STC_CHARADDED, &FreeCADDialog::on_char_add, this); m_text->Bind(wxEVT_KEY_DOWN, &FreeCADDialog::on_key_type, this); + m_text->Bind(wxEVT_CHAR, &FreeCADDialog::on_char_type, this); m_text->Bind(wxEVT_STC_AUTOCOMP_COMPLETED, &FreeCADDialog::on_autocomp_complete, this); m_text->Connect(wxID_ANY, wxEVT_KEY_DOWN, diff --git a/src/slic3r/GUI/FreeCADDialog.hpp b/src/slic3r/GUI/FreeCADDialog.hpp index f93fac5df..05b06bb4c 100644 --- a/src/slic3r/GUI/FreeCADDialog.hpp +++ b/src/slic3r/GUI/FreeCADDialog.hpp @@ -68,10 +68,12 @@ protected: void on_word_change_for_autocomplete(wxStyledTextEvent& event); void on_char_add(wxStyledTextEvent& event); void on_key_type(wxKeyEvent& event); + void on_char_type(wxKeyEvent& event); void on_autocomp_complete(wxStyledTextEvent& event); bool write_text_in_file(const wxString &towrite, const boost::filesystem::path &file); bool load_text_from_file(const boost::filesystem::path &file); void test_update_script_file(std::string &json); + void comment(bool is_switch); wxStyledTextCtrl* m_text; wxTextCtrl* m_errors;