diff --git a/src/Mod/Sketcher/Gui/CMakeLists.txt b/src/Mod/Sketcher/Gui/CMakeLists.txt index 86393223c8..a60de0553a 100644 --- a/src/Mod/Sketcher/Gui/CMakeLists.txt +++ b/src/Mod/Sketcher/Gui/CMakeLists.txt @@ -114,6 +114,8 @@ SET(SketcherGui_SRCS TaskSketcherValidation.ui TaskSketcherValidation.cpp TaskSketcherValidation.h + ShortcutListener.h + ShortcutListener.cpp ViewProviderSketch.cpp ViewProviderSketch.h DrawSketchHandler.cpp diff --git a/src/Mod/Sketcher/Gui/ShortcutListener.cpp b/src/Mod/Sketcher/Gui/ShortcutListener.cpp new file mode 100644 index 0000000000..4e99296517 --- /dev/null +++ b/src/Mod/Sketcher/Gui/ShortcutListener.cpp @@ -0,0 +1,60 @@ +/*************************************************************************** + * Copyright (c) 2018 Abdullah Tahiri +#endif + +#include "ShortcutListener.h" +#include "ViewProviderSketch.h" + +using namespace SketcherGui; + +ShortcutListener::ShortcutListener(ViewProviderSketch * vp):QObject() +{ + pViewProvider = vp; +} +ShortcutListener::~ShortcutListener() +{ +} + +bool ShortcutListener::eventFilter(QObject *obj, QEvent *event) { + if (event->type() == QEvent::ShortcutOverride) { + QKeyEvent * kevent = static_cast(event); + if (kevent->modifiers() == Qt::NoModifier || + kevent->modifiers() == Qt::ShiftModifier || + kevent->modifiers() == Qt::KeypadModifier) { + switch (kevent->key()) { + case Qt::Key_Delete: + kevent->accept(); + pViewProvider->deleteSelected(); // this takes a list of objects + return true; + default: + break; + } + } + } + return QObject::eventFilter(obj, event); +} diff --git a/src/Mod/Sketcher/Gui/ShortcutListener.h b/src/Mod/Sketcher/Gui/ShortcutListener.h new file mode 100644 index 0000000000..3b649852d0 --- /dev/null +++ b/src/Mod/Sketcher/Gui/ShortcutListener.h @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (c) 2018 Abdullah Tahiri + + +namespace SketcherGui { + +class ViewProviderSketch; + + +class ShortcutListener: public QObject +{ + //Q_OBJECT + +public: + ShortcutListener(ViewProviderSketch * vp); + ~ShortcutListener(); + +protected: + bool eventFilter(QObject *obj, QEvent *event); + + ViewProviderSketch * pViewProvider; +}; + +} // namespace PartGui + + +#endif // SKETCHERGUI_SHORTCUTLISTENER_H + diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index 0080398f20..23bcaa5fa4 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -69,6 +69,7 @@ # include # include # include +# include #endif #ifndef _PreComp_ @@ -281,7 +282,8 @@ ViewProviderSketch::ViewProviderSketch() Mode(STATUS_NONE), visibleInformationChanged(true), combrepscalehyst(0), - isShownVirtualSpace(false) + isShownVirtualSpace(false), + listener(0) { ADD_PROPERTY_TYPE(Autoconstraints,(true),"Auto Constraints",(App::PropertyType)(App::Prop_None),"Create auto constraints"); ADD_PROPERTY_TYPE(TempoVis,(Py::None()),"Visibility automation",(App::PropertyType)(App::Prop_None),"Object that handles hiding and showing other objects when entering/leaving sketch."); @@ -5689,6 +5691,12 @@ bool ViewProviderSketch::setEdit(int ModNum) getSketchObject()->getSolvedSketch().RecalculateInitialSolutionWhileMovingPoint = hGrp2->GetBool("RecalculateInitialSolutionWhileDragging",true); + + // intercept del key press from main app + listener = new ShortcutListener(this); + + Gui::getMainWindow()->installEventFilter(listener); + return true; } @@ -5972,6 +5980,11 @@ void ViewProviderSketch::unsetEdit(int ModNum) ShowGrid.setValue(false); TightGrid.setValue(true); + if(listener) { + Gui::getMainWindow()->removeEventFilter(listener); + delete listener; + } + if (edit) { if (edit->sketchHandler) deactivateHandler(); @@ -6224,6 +6237,31 @@ Sketcher::SketchObject *ViewProviderSketch::getSketchObject(void) const return dynamic_cast(pcObject); } +void ViewProviderSketch::deleteSelected() +{ + std::vector selection; + selection = Gui::Selection().getSelectionEx(0, Sketcher::SketchObject::getClassTypeId()); + + // only one sketch with its subelements are allowed to be selected + if (selection.size() != 1) { + Base::Console().Warning("Delete: Selection not restricted to one sketch and its subelements"); + return; + } + + // get the needed lists and objects + const std::vector &SubNames = selection[0].getSubNames(); + + if(SubNames.size()>0) { + App::Document* doc = getSketchObject()->getDocument(); + + doc->openTransaction("delete sketch geometry"); + + onDelete(SubNames); + + doc->commitTransaction(); + } +} + bool ViewProviderSketch::onDelete(const std::vector &subList) { if (edit) { @@ -6349,8 +6387,20 @@ bool ViewProviderSketch::onDelete(const std::vector &subList) // This function is generally called from StdCmdDelete::activated // Since 2015-05-03 that function includes a recompute at the end. - /*this->drawConstraintIcons(); - this->updateColor();*/ + // Since December 2018, the function is no longer called from StdCmdDelete::activated, + // as there is an event filter installed that intercepts the del key event. So now we do + // need to tidy up after ourselves again. + + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); + bool autoRecompute = hGrp->GetBool("AutoRecompute",false); + + if (autoRecompute) { + Gui::Command::updateActive(); + } + else { + this->drawConstraintIcons(); + this->updateColor(); + } // if in edit not delete the object return false; diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.h b/src/Mod/Sketcher/Gui/ViewProviderSketch.h index 8baa3d372a..f70c1cddca 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.h +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.h @@ -36,6 +36,7 @@ #include #include #include +#include "ShortcutListener.h" class TopoDS_Shape; @@ -123,7 +124,7 @@ public: /// Show/Hide nodes from information layer void showRestoreInformationLayer(); - + /** @name handler control */ //@{ /// sets an DrawSketchHandler in control @@ -224,11 +225,13 @@ public: virtual bool mouseButtonPressed(int Button, bool pressed, const SbVec2s& cursorPos, const Gui::View3DInventorViewer* viewer); //@} + void deleteSelected(); + /// updates the visibility of the virtual space void updateVirtualSpace(void); void setIsShownVirtualSpace(bool isshownvirtualspace); bool getIsShownVirtualSpace(void) const; - + friend class DrawSketchHandler; friend struct ::EditData; @@ -240,7 +243,7 @@ public: boost::signals2::signal signalSolved; /// signals if the elements list has changed boost::signals2::signal signalElementsChanged; - + protected: virtual bool setEdit(int ModNum); virtual void unsetEdit(int ModNum); @@ -263,14 +266,14 @@ protected: EditData *edit; /// build up the visual of the constraints void rebuildConstraintsVisual(void); - + void slotUndoDocument(const Gui::Document&); void slotRedoDocument(const Gui::Document&); - + protected: boost::signals2::connection connectUndoDocument; boost::signals2::connection connectRedoDocument; - + /// Return display string for constraint including hiding units if //requested. QString getPresentationString(const Sketcher::Constraint *constraint); @@ -308,7 +311,7 @@ protected: /// Pointer to SoInfo object where we store the constraint IDs that the icon refers to SoInfo *infoPtr; - + /// Angle to rotate an icon double iconRotation; }; @@ -363,7 +366,7 @@ protected: void addSelectPoint(int SelectPoint); void removeSelectPoint(int SelectPoint); void clearSelectPoints(void); - + // modes while sketching SketchMode Mode; @@ -416,9 +419,11 @@ protected: // information layer variables bool visibleInformationChanged; double combrepscalehyst; - + // Virtual space variables bool isShownVirtualSpace; // indicates whether the present virtual space view is the Real Space or the Virtual Space (virtual space 1 or 2) + + ShortcutListener* listener; }; } // namespace PartGui