Skip to content
Permalink
Browse files

[composer] Fix polylines would be removed if they had < 3 points

when deleting nodes (2 node lines should be allowed)
  • Loading branch information
nyalldawson committed Apr 1, 2016
1 parent 90b6f46 commit 38fa2980ad83d4d3590a5aa475e478cd6f6a9ec2
@@ -87,6 +87,9 @@ class QgsComposerNodesItem: QgsComposerItem
/** Method called in addNode. */
virtual bool _addNode( const int nodeIndex, const QPointF &newPoint, const double radius ) = 0;

/** Method called in removeNode. */
virtual bool _removeNode( const int nodeIndex ) = 0;

/** Method called in paint. */
virtual void _draw( QPainter *painter ) = 0;

@@ -23,10 +23,10 @@ class QgsComposerPolygon: QgsComposerNodesItem

protected:

/** Add the point newPoint at the given position according to some
* criteres. */
bool _addNode( const int indexPoint, const QPointF &newPoint, const double radius );

bool _removeNode( const int indexPoint );

/** Draw points for the current shape. */
void _draw( QPainter *painter );

@@ -23,10 +23,10 @@ class QgsComposerPolyline: QgsComposerNodesItem

protected:

/** Add the point newPoint at the given position according to some
* criteres. */
bool _addNode( const int indexPoint, const QPointF &newPoint, const double radius );

bool _removeNode( const int indexPoint );

/** Draw points for the current shape. */
void _draw( QPainter *painter );

@@ -255,27 +255,9 @@ bool QgsComposerNodesItem::nodePosition( const int index, QPointF &position )

bool QgsComposerNodesItem::removeNode( const int index )
{
bool rc( false );

if ( index >= 0 && index < mPolygon.size() )
{
mPolygon.remove( index );

if ( mPolygon.size() < 3 )
mPolygon.clear();
else
{
int newSelectNode = index;
if ( index == mPolygon.size() )
newSelectNode = 0;
setSelectedNode( newSelectNode );
}

bool rc = _removeNode( index );
if ( rc )
updateSceneRect();

rc = true;
}

return rc;
}

@@ -128,6 +128,9 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
/** Method called in addNode. */
virtual bool _addNode( const int nodeIndex, const QPointF &newNode, const double radius ) = 0;

/** Method called in removeNode. */
virtual bool _removeNode( const int nodeIndex ) = 0;

/** Method called in paint. */
virtual void _draw( QPainter *painter ) = 0;

@@ -120,3 +120,23 @@ void QgsComposerPolygon::_writeXMLStyle( QDomDocument &doc, QDomElement &elmt )
doc );
elmt.appendChild( pe );
}

bool QgsComposerPolygon::_removeNode( const int index )
{
if ( index < 0 || index >= mPolygon.size() )
return false;

mPolygon.remove( index );

if ( mPolygon.size() < 3 )
mPolygon.clear();
else
{
int newSelectNode = index;
if ( index == mPolygon.size() )
newSelectNode = 0;
setSelectedNode( newSelectNode );
}

return true;
}
@@ -69,6 +69,8 @@ class CORE_EXPORT QgsComposerPolygon: public QgsComposerNodesItem
* criteres. */
bool _addNode( const int indexPoint, const QPointF &newPoint, const double radius ) override;

bool _removeNode( const int nodeIndex ) override;

/** Draw nodes for the current shape. */
void _draw( QPainter *painter ) override;

@@ -59,6 +59,26 @@ bool QgsComposerPolyline::_addNode( const int indexPoint,
return true;
}

bool QgsComposerPolyline::_removeNode( const int index )
{
if ( index < 0 || index >= mPolygon.size() )
return false;

mPolygon.remove( index );

if ( mPolygon.size() < 2 )
mPolygon.clear();
else
{
int newSelectNode = index;
if ( index >= mPolygon.size() )
newSelectNode = mPolygon.size() - 1;
setSelectedNode( newSelectNode );
}

return true;
}

void QgsComposerPolyline::createDefaultPolylineStyleSymbol()
{
QgsStringMap properties;
@@ -68,6 +68,8 @@ class CORE_EXPORT QgsComposerPolyline: public QgsComposerNodesItem
* criteres. */
bool _addNode( const int indexPoint, const QPointF &newPoint, const double radius ) override;

bool _removeNode( const int nodeIndex ) override;

/** Draw nodes for the current shape. */
void _draw( QPainter *painter ) override;

@@ -1577,27 +1577,24 @@ void QgsComposerView::deleteSelectedItems()
if ( mNodesItemIndex != -1 )
{
composition()->beginCommand( mNodesItem, tr( "Remove item node" ) );
bool rc = mNodesItem->removeNode( mNodesItemIndex );
composition()->endCommand();

bool nodeDeleted = true;
if ( rc )
if ( mNodesItem->removeNode( mNodesItemIndex ) )
{
mNodesItemIndex = mNodesItem->selectedNode();

if ( mNodesItemIndex != -1 )
composition()->endCommand();
if ( mNodesItem->nodesSize() > 0 )
{
nodeDeleted = false;
setSelectedNode( mNodesItem, mNodesItemIndex );
mNodesItemIndex = mNodesItem->selectedNode();
// setSelectedNode( mNodesItem, mNodesItemIndex );
}
else
{
mNodesItemIndex = -1;
mNodesItem = nullptr;
}
scene()->update();
}

if ( nodeDeleted )
else
{
scene()->update();

mNodesItemIndex = -1;
mNodesItem = nullptr;
composition()->cancelCommand();
}
}
}
@@ -139,15 +139,6 @@ def testRemoveNode(self):
assert myTestResult, myMessage

self.assertEqual(self.mComposerPolygon.nodesSize(), 4)
rc = self.mComposerPolygon.removeNode(3)
self.assertEqual(rc, True)
self.assertEqual(self.mComposerPolygon.nodesSize(), 3)

checker = QgsCompositionChecker(
'composerpolygon_removednode', self.mComposition)
checker.setControlPathPrefix("composer_polygon")
myTestResult, myMessage = checker.testComposition()
assert myTestResult, myMessage

def testAddNode(self):
"""Test addNode method"""

0 comments on commit 38fa298

Please sign in to comment.
You can’t perform that action at this time.