New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds option to remove an edge from polygon #1685

Merged
merged 6 commits into from Aug 15, 2017
Next

Adds option to remove an edge from polygon

  • Loading branch information...
ketanhwr committed Aug 13, 2017
commit e83b340d7cd2cfd23f4a99e0df2367f6b65815e6
@@ -61,3 +61,14 @@ void ChangePolygon::redo()
{
mMapDocument->mapObjectModel()->setObjectPolygon(mMapObject, mNewPolygon);
}
TogglePolygonPolyline::TogglePolygonPolyline(MapObject *mapObject)
: mMapObject(mapObject)
{
setText(QCoreApplication::translate("Undo Commands", "Toggle Polygon/Polyline"));
}
void TogglePolygonPolyline::toggle()
{
mMapObject->setShape((mMapObject->shape() == MapObject::Polygon) ? MapObject::Polyline : MapObject::Polygon);
}
View
@@ -60,5 +60,19 @@ class ChangePolygon : public QUndoCommand
QPolygonF mNewPolygon;
};
class TogglePolygonPolyline : public QUndoCommand
{
public:
TogglePolygonPolyline(MapObject *MapObject);
void undo() override { toggle(); }
void redo() override { toggle(); }
private:
void toggle();
MapObject *mMapObject;
};
} // namespace Internal
} // namespace Tiled
@@ -618,6 +618,24 @@ void EditPolygonTool::showHandleContextMenu(PointHandle *clickedHandle,
connect(joinNodesAction, SIGNAL(triggered()), SLOT(joinNodes()));
connect(splitSegmentsAction, SIGNAL(triggered()), SLOT(splitSegments()));
const auto firstHandle = *mSelectedHandles.begin();
const auto secondHandle = *(mSelectedHandles.begin() + 1);

This comment has been minimized.

@bjorn

bjorn Aug 15, 2017

Owner

Please get the second handle only after checking if there is one.

MapObject *mapObject = firstHandle->mapObjectItem()->mapObject();
if (mapObject->shape() == MapObject::Polygon) {
QAction *deleteEdge = menu.addAction(tr("Delete Edge"));
bool enabled = false;
if (mSelectedHandles.size() == 2) {
int indexDifference = std::abs(firstHandle->pointIndex() - secondHandle->pointIndex());
if (indexDifference == 1 || indexDifference == mapObject->polygon().size() - 1)

This comment has been minimized.

@bjorn

bjorn Aug 15, 2017

Owner

Don't forget to also check that both points belong to the same object.

enabled = true;
}
deleteEdge->setEnabled(enabled);
connect(deleteEdge, SIGNAL(triggered()), SLOT(deleteEdge()));
}
menu.exec(screenPos);
}
@@ -870,3 +888,38 @@ void EditPolygonTool::splitSegments()
if (macroStarted)
undoStack->endMacro();
}
void EditPolygonTool::deleteEdge()
{
if (mSelectedHandles.size() != 2)
return;
const auto &firstHandle = *mSelectedHandles.begin();
const auto &secondHandle = *(mSelectedHandles.begin() + 1);
const MapObjectItem *item = firstHandle->mapObjectItem();
MapObject *mapObject = item->mapObject();
QPolygonF polygon = mapObject->polygon();
QPolygonF newPolygon(polygon);
int indexDifference = std::abs(firstHandle->pointIndex() - secondHandle->pointIndex());
if (indexDifference != polygon.size() - 1) {
int maxIndex = std::max(firstHandle->pointIndex(), secondHandle->pointIndex());
for (int i = maxIndex; i < polygon.size(); ++i)
newPolygon[i - maxIndex] = polygon[i];
for (int i = 0; i < maxIndex; ++i)
newPolygon[polygon.size() - maxIndex + i] = polygon[i];
}
setSelectedHandles(QSet<PointHandle*>());
mapDocument()->mapObjectModel()->setObjectPolygon(mapObject, newPolygon);

This comment has been minimized.

@bjorn

bjorn Aug 15, 2017

Owner

Instead of setting it here, you could use the ChangePolygon constructor that takes both the old and the new polygon.

mapDocument()->undoStack()->beginMacro(tr("Delete Edge"));
mapDocument()->undoStack()->push(new ChangePolygon(mapDocument(), mapObject, polygon));
mapDocument()->undoStack()->push(new TogglePolygonPolyline(mapObject));
mapDocument()->undoStack()->endMacro();
}
@@ -68,6 +68,7 @@ private slots:
void joinNodes();
void splitSegments();
void deleteEdge();
private:
enum Mode {
ProTip! Use n and p to navigate between commits in a pull request.