Skip to content

Commit

Permalink
[TD]fix fail on undo of cosmetic delete
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed Mar 27, 2024
1 parent ef16eca commit 10b9229
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 17 deletions.
12 changes: 5 additions & 7 deletions src/Mod/TechDraw/App/CosmeticExtension.cpp
Expand Up @@ -349,7 +349,7 @@ TechDraw::CosmeticEdge* CosmeticExtension::getCosmeticEdge(const std::string& ta
/// used when selecting
TechDraw::CosmeticEdge* CosmeticExtension::getCosmeticEdgeBySelection(const std::string& name) const
{
// Base::Console().Message("CEx::getCEBySelection(%s)\n", name.c_str());
// Base::Console().Message("CEx::getCEBySelection(%s)\n", name.c_str());
App::DocumentObject* extObj = const_cast<App::DocumentObject*> (getExtendedObject());
TechDraw::DrawViewPart* dvp = dynamic_cast<TechDraw::DrawViewPart*>(extObj);
if (!dvp) {
Expand All @@ -367,7 +367,7 @@ TechDraw::CosmeticEdge* CosmeticExtension::getCosmeticEdgeBySelection(const std:
/// find the cosmetic edge corresponding to the input parameter (the 5 in Edge5)
TechDraw::CosmeticEdge* CosmeticExtension::getCosmeticEdgeBySelection(int i) const
{
// Base::Console().Message("CEx::getCEBySelection(%d)\n", i);
// Base::Console().Message("CEx::getCEBySelection(%d)\n", i);
std::stringstream edgeName;
edgeName << "Edge" << i;
return getCosmeticEdgeBySelection(edgeName.str());
Expand All @@ -376,7 +376,7 @@ TechDraw::CosmeticEdge* CosmeticExtension::getCosmeticEdgeBySelection(int i) con
/// remove the cosmetic edge with the given tag from the list property
void CosmeticExtension::removeCosmeticEdge(const std::string& delTag)
{
// Base::Console().Message("DVP::removeCE(%s)\n", delTag.c_str());
// Base::Console().Message("DVP::removeCE(%s)\n", delTag.c_str());
std::vector<CosmeticEdge*> cEdges = CosmeticEdges.getValues();
std::vector<CosmeticEdge*> newEdges;
for (auto& ce: cEdges) {

Check warning on line 382 in src/Mod/TechDraw/App/CosmeticExtension.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Missing space around colon in range-based for loop [whitespace/forcolon] [2]
Expand Down Expand Up @@ -420,7 +420,6 @@ int CosmeticExtension::add1CLToGE(const std::string& tag)
return -1;
}
TechDraw::BaseGeomPtr scaledGeom = cl->scaledAndRotatedGeometry(getOwner());
// TechDraw::BaseGeomPtr scaledGeom = cl->scaledGeometry(getOwner());
int iGE = getOwner()->getGeometryObject()->addCenterLine(scaledGeom, tag);

return iGE;
Expand All @@ -429,7 +428,7 @@ int CosmeticExtension::add1CLToGE(const std::string& tag)
//update Edge geometry with current CL's
void CosmeticExtension::refreshCLGeoms()
{
// Base::Console().Message("CE::refreshCLGeoms()\n");
// Base::Console().Message("CE::refreshCLGeoms()\n");
std::vector<TechDraw::BaseGeomPtr> gEdges = getOwner()->getEdgeGeometry();
std::vector<TechDraw::BaseGeomPtr> newGEdges;
for (auto& ge : gEdges) {
Expand All @@ -447,7 +446,6 @@ void CosmeticExtension::addCenterLinesToGeom()
// Base::Console().Message("CE::addCenterLinesToGeom()\n");
const std::vector<TechDraw::CenterLine*> lines = CenterLines.getValues();
for (auto& cl : lines) {
// TechDraw::BaseGeomPtr scaledGeom = cl->scaledGeometry(getOwner());
TechDraw::BaseGeomPtr scaledGeom = cl->scaledAndRotatedGeometry(getOwner());
if (!scaledGeom) {
Base::Console().Error("CE::addCenterLinesToGeom - scaledGeometry is null\n");
Expand Down Expand Up @@ -536,7 +534,7 @@ TechDraw::CenterLine* CosmeticExtension::getCenterLineBySelection(int i) const

void CosmeticExtension::removeCenterLine(const std::string& delTag)
{
// Base::Console().Message("DVP::removeCL(%s)\n", delTag.c_str());
// Base::Console().Message("DVP::removeCL(%s)\n", delTag.c_str());
std::vector<CenterLine*> cLines = CenterLines.getValues();
std::vector<CenterLine*> newLines;
for (auto& cl: cLines) {

Check warning on line 540 in src/Mod/TechDraw/App/CosmeticExtension.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Missing space around colon in range-based for loop [whitespace/forcolon] [2]
Expand Down
7 changes: 4 additions & 3 deletions src/Mod/TechDraw/Gui/QGIEdge.cpp
Expand Up @@ -21,9 +21,8 @@
***************************************************************************/

#include "PreCompiled.h"
#ifndef _PreComp_
# include <cassert>

#ifndef _PreComp_
# include <QPainterPath>
# include <QPainterPathStroker>
#endif
Expand All @@ -34,7 +33,6 @@
#include <Base/Parameter.h>
#include <Gui/Control.h>
#include <Mod/TechDraw/App/DrawUtil.h>
#include <Mod/TechDraw/App/DrawViewPart.h>

#include "QGIEdge.h"
#include "PreferencesGui.h"
Expand All @@ -50,6 +48,9 @@ QGIEdge::QGIEdge(int index) :
isHiddenEdge(false),
isSmoothEdge(false)
{
setFlag(QGraphicsItem::ItemIsFocusable, true); // to get key press events
setFlag(QGraphicsItem::ItemIsSelectable, true);

m_width = 1.0;
setCosmetic(isCosmetic);
setFill(Qt::NoBrush);
Expand Down
4 changes: 4 additions & 0 deletions src/Mod/TechDraw/Gui/QGIEdge.h
Expand Up @@ -55,6 +55,9 @@ class TechDrawGuiExport QGIEdge : public QGIPrimPath

void setLinePen(QPen isoPen);

void setSource(int source) { m_source = source; }
int getSource() const { return m_source;}


protected:

Expand All @@ -71,6 +74,7 @@ class TechDrawGuiExport QGIEdge : public QGIPrimPath
Qt::PenStyle getHiddenStyle();

private:
int m_source{0};
};

}
Expand Down
4 changes: 4 additions & 0 deletions src/Mod/TechDraw/Gui/QGIPrimPath.cpp
Expand Up @@ -53,6 +53,8 @@ QGIPrimPath::QGIPrimPath():
setCacheMode(QGraphicsItem::NoCache);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemIsFocusable, true); // to get key press events

setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setAcceptHoverEvents(true);
Expand Down Expand Up @@ -86,6 +88,7 @@ QVariant QGIPrimPath::itemChange(GraphicsItemChange change, const QVariant &valu
if (change == ItemSelectedHasChanged && scene()) {
if(isSelected()) {
setPrettySel();
setFocus();
} else {
setPrettyNormal();
}
Expand All @@ -98,6 +101,7 @@ void QGIPrimPath::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
if (!isSelected()) {
setPrettyPre();
}
setFocus();
QGraphicsPathItem::hoverEnterEvent(event);
}

Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/QGIView.cpp
Expand Up @@ -250,6 +250,7 @@ void QGIView::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
// TODO don't like this but only solution at the minute (MLP)
if (isSelected()) {
m_colCurrent = getSelectColor();
setFocus();
} else {
m_colCurrent = getPreColor();
}
Expand Down
80 changes: 77 additions & 3 deletions src/Mod/TechDraw/Gui/QGIViewPart.cpp
Expand Up @@ -25,6 +25,7 @@
#include <cmath>

#include <QPainterPath>
#include <QKeyEvent>
#include <qmath.h>
#endif// #ifndef _PreComp_

Expand All @@ -33,6 +34,7 @@
#include <Base/Console.h>
#include <Base/Parameter.h>
#include <Base/Vector3D.h>
#include <Gui/Selection.h>
#include <Mod/TechDraw/App/CenterLine.h>
#include <Mod/TechDraw/App/Cosmetic.h>
#include <Mod/TechDraw/App/DrawComplexSection.h>
Expand Down Expand Up @@ -83,6 +85,7 @@ QGIViewPart::QGIViewPart()
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);

showSection = false;
m_pathBuilder = new PathBuilder(this);
Expand All @@ -107,6 +110,77 @@ QVariant QGIViewPart::itemChange(GraphicsItemChange change, const QVariant& valu
return QGIView::itemChange(change, value);
}

bool QGIViewPart::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
{
// Base::Console().Message("QGIVP::sceneEventFilter - event: %d watchedtype: %d\n",
// event->type(), watched->type() - QGraphicsItem::UserType);
if (event->type() == QEvent::ShortcutOverride) {
// if we accept this event, we should get a regular keystroke event next
// which will be processed by QGVPage/QGVNavStyle keypress logic, but not forwarded to
// Std_Delete
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Delete) {
bool success = removeSelectedCosmetic();
if (success) {
updateView(true);
event->accept();
return true;
}
}
}

return QGraphicsItem::sceneEventFilter(watched, event);
}

//! called when a DEL shortcut event is received. If a cosmetic edge or vertex is
//! selected, remove it from the view.
bool QGIViewPart::removeSelectedCosmetic() const
{
// Base::Console().Message("QGIVP::removeSelectedCosmetic()\n");
char* defaultDocument{nullptr};
std::vector<Gui::SelectionObject> selectionAll = Gui::Selection().getSelectionEx(
defaultDocument, TechDraw::DrawViewPart::getClassTypeId(), Gui::ResolveMode::NoResolve);
if (selectionAll.empty()) {
return false;
}
Gui::SelectionObject firstSelection = selectionAll.front();
App::DocumentObject* firstObject = selectionAll.front().getObject();
std::vector<std::string> subElements = selectionAll.front().getSubNames();
if (subElements.empty()) {
return false;
}
auto dvp = static_cast<TechDraw::DrawViewPart*>(firstObject);
auto subelement = subElements.front();
std::string geomName = DU::getGeomTypeFromName(subelement);
int index = DU::getIndexFromName(subelement);
if (geomName == "Edge") {
TechDraw::BaseGeomPtr base = dvp->getGeomByIndex(index);
if (!base || base->getCosmeticTag().empty()) {
return false;
}
if (base->source() == COSMETICEDGE) {
dvp->removeCosmeticEdge(base->getCosmeticTag());
dvp->refreshCEGeoms();
} else if (base->source() == CENTERLINE) {
dvp->removeCenterLine(base->getCosmeticTag());
dvp->refreshCLGeoms();
} else {
Base::Console().Message("QGIVP::removeSelectedCosmetic - not a CE or a CL\n");
return false;
}
} else if (geomName == "Vertex") {
VertexPtr vert = dvp->getProjVertexByIndex(index);
if (!vert || vert->getCosmeticTag().empty() ) {
return false;
}
dvp->removeCosmeticVertex(vert->getCosmeticTag());
dvp->refreshCVGeoms();
}

return true;
}


//obs?
void QGIViewPart::tidy()
{
Expand All @@ -133,7 +207,7 @@ QPainterPath QGIViewPart::drawPainterPath(TechDraw::BaseGeomPtr baseGeom) const

void QGIViewPart::updateView(bool update)
{
// Base::Console().Message("QGIVP::updateView() - %s\n", getViewObject()->getNameInDocument());
// Base::Console().Message("QGIVP::updateView() - %s\n", getViewObject()->getNameInDocument());
auto viewPart(dynamic_cast<TechDraw::DrawViewPart*>(getViewObject()));
if (!viewPart)
return;
Expand Down Expand Up @@ -305,6 +379,7 @@ void QGIViewPart::drawAllEdges()
item = new QGIEdge(iEdge);
addToGroup(item); //item is created at scene(0, 0), not group(0, 0)
item->setPath(drawPainterPath(*itGeom));
item->setSource((*itGeom)->source());

item->setNormalColor(PreferencesGui::getAccessibleQColor(PreferencesGui::normalQColor()));
if ((*itGeom)->getCosmetic()) {
Expand Down Expand Up @@ -1025,6 +1100,7 @@ QGIViewPart::faceIsGeomHatched(int i, std::vector<TechDraw::DrawGeomHatch*> geom
}



void QGIViewPart::dumpPath(const char* text, QPainterPath path)
{
QPainterPath::Element elem;
Expand Down Expand Up @@ -1066,8 +1142,6 @@ void QGIViewPart::paint(QPainter* painter, const QStyleOptionGraphicsItem* optio
QGIView::paint(painter, &myOption, widget);
}



//QGIViewPart derived classes do not need a rotate view method as rotation is handled on App side.
void QGIViewPart::rotateView() {}

Expand Down
3 changes: 3 additions & 0 deletions src/Mod/TechDraw/Gui/QGIViewPart.h
Expand Up @@ -63,6 +63,7 @@ class TechDrawGuiExport QGIViewPart : public QGIView
void paint( QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget = nullptr ) override;
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;


void toggleCache(bool state) override;
Expand Down Expand Up @@ -115,6 +116,8 @@ class TechDrawGuiExport QGIViewPart : public QGIView

virtual QGraphicsItem *getQGISubItemByName(const std::string &subName) const;

virtual bool removeSelectedCosmetic() const;

protected:
QPainterPath drawPainterPath(TechDraw::BaseGeomPtr baseGeom) const;
void drawViewPart();
Expand Down
5 changes: 5 additions & 0 deletions src/Mod/TechDraw/Gui/QGSPage.cpp
Expand Up @@ -393,6 +393,8 @@ QGIView* QGSPage::addViewPart(TechDraw::DrawViewPart* partFeat)
viewPart->setViewPartFeature(partFeat);

addQView(viewPart);
// we need to install an event filter for any views derived from DrawViewPart
viewPart->installSceneEventFilter(viewPart);
return viewPart;
}

Expand All @@ -403,6 +405,7 @@ QGIView* QGSPage::addViewSection(DrawViewSection* sectionFeat)
viewSection->setViewPartFeature(sectionFeat);

addQView(viewSection);
viewSection->installSceneEventFilter(viewSection);
return viewSection;
}

Expand All @@ -413,6 +416,8 @@ QGIView* QGSPage::addProjectionGroup(TechDraw::DrawProjGroup* projGroupFeat)

qview->setViewFeature(projGroupFeat);
addQView(qview);
qview->installSceneEventFilter(qview);

return qview;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Mod/TechDraw/Gui/QGVNavStyle.cpp
Expand Up @@ -109,7 +109,7 @@ void QGVNavStyle::handleFocusOutEvent(QFocusEvent* event)

void QGVNavStyle::handleKeyPressEvent(QKeyEvent* event)
{
// Base::Console().Message("QGNS::handleKeyPressEvent(%d)\n", event->key());
// Base::Console().Message("QGNS::handleKeyPressEvent(%d)\n", event->key());
if (event->modifiers().testFlag(Qt::ControlModifier)) {
switch (event->key()) {
case Qt::Key_Plus: {
Expand Down Expand Up @@ -165,11 +165,11 @@ void QGVNavStyle::handleKeyPressEvent(QKeyEvent* event)
}
}
}
event->ignore();
}

void QGVNavStyle::handleKeyReleaseEvent(QKeyEvent* event)
{
// Q_UNUSED(event);
if (event->modifiers().testFlag(Qt::NoModifier)) {
switch (event->key()) {
case Qt::Key_Shift: {
Expand Down Expand Up @@ -214,7 +214,7 @@ void QGVNavStyle::handleLeaveEvent(QEvent* event)

void QGVNavStyle::handleMousePressEvent(QMouseEvent* event)
{
// Base::Console().Message("QGVNS::handleMousePressEvent()\n");
// Base::Console().Message("QGVNS::handleMousePressEvent()\n");
if (!panningActive && (event->button() == Qt::MiddleButton)) {
startPan(event->pos());
event->accept();
Expand Down
1 change: 0 additions & 1 deletion src/Mod/TechDraw/Gui/QGVNavStyleTouchpad.cpp
Expand Up @@ -46,7 +46,6 @@ QGVNavStyleTouchpad::~QGVNavStyleTouchpad()

void QGVNavStyleTouchpad::handleKeyPressEvent(QKeyEvent *event)
{
// Q_UNUSED(event)
if (event->key() == Qt::Key_PageUp) {
zoomIn();
event->accept();
Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/ViewProviderPage.cpp
Expand Up @@ -340,6 +340,7 @@ void ViewProviderPage::createMDIViewPage()
m_mdiView->setWindowIcon(Gui::BitmapFactory().pixmap("TechDraw_TreePage"));
Gui::getMainWindow()->addWindow(m_mdiView);
Gui::getMainWindow()->setActiveWindow(m_mdiView);
m_graphicsView->setFocus();
}

//NOTE: removing MDIViewPage (parent) destroys QGVPage (eventually)
Expand Down

0 comments on commit 10b9229

Please sign in to comment.