Skip to content

Commit

Permalink
Set angle in vertexModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenD98 committed Nov 5, 2024
1 parent ab0f1d0 commit b11c935
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
25 changes: 22 additions & 3 deletions src/core/vertexmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,11 @@ void VertexModel::next()
}
}

QgsPoint VertexModel::getPoint( int index )
{
return mVertices[index].point;
}

void VertexModel::addVertexNearestToPosition( const QgsPoint &mapPoint )
{
double closestDistance = std::numeric_limits<double>::max();
Expand Down Expand Up @@ -709,7 +714,7 @@ double VertexModel::calculateAngle( const QgsPoint &a, const QgsPoint &b, const
double angle = std::acos( cosTheta );

// Convert to degrees
return angle * ( 180.0 / M_PI );
return std::abs( angle * ( 180.0 / M_PI ) - 180 );
}

void VertexModel::setCurrentPoint( const QgsPoint &point )
Expand Down Expand Up @@ -737,8 +742,9 @@ void VertexModel::setCurrentPoint( const QgsPoint &point )

double angle = calculateAngle( mVertices[startPoint].point, mVertices[mCurrentIndex].point, mVertices[endPoint].point );

qDebug() << "angle = " << angle;
setAngleFonud( angle > 88 && angle < 92 );
qDebug() << "--------- angle = " << angle;

setAngleFonud( angle > snapToCommonAngleDegrees() - 1 && angle < snapToCommonAngleDegrees() + 1 );

if ( mMapSettings && vertex.point.distance( point ) / mMapSettings->mapSettings().mapUnitsPerPixel() < 1 )
return;
Expand Down Expand Up @@ -1098,3 +1104,16 @@ void VertexModel::updateAngleFound()
setAngleFonud( false );
}
}

int VertexModel::snapToCommonAngleDegrees() const
{
return mSnapToCommonAngleDegrees;
}

void VertexModel::setSnapToCommonAngleDegrees( int snapToCommonAngleDegrees )
{
if ( mSnapToCommonAngleDegrees == snapToCommonAngleDegrees )
return;
mSnapToCommonAngleDegrees = snapToCommonAngleDegrees;
emit snapToCommonAngleDegreesChanged();
}
13 changes: 12 additions & 1 deletion src/core/vertexmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class QFIELD_CORE_EXPORT VertexModel : public QAbstractListModel
*/
Q_PROPERTY( int currentVertexIndex READ currentVertexIndex WRITE setCurrentVertexIndex NOTIFY currentVertexIndexChanged )

Q_PROPERTY( int snapToCommonAngleDegrees READ snapToCommonAngleDegrees WRITE setSnapToCommonAngleDegrees NOTIFY snapToCommonAngleDegreesChanged )


/**
* The geometry in layer coordinates
*/
Expand Down Expand Up @@ -190,6 +193,8 @@ class QFIELD_CORE_EXPORT VertexModel : public QAbstractListModel
//! next vertex or segment
Q_INVOKABLE void next();

Q_INVOKABLE QgsPoint getPoint( int index );

//! Selects the vertex at the given screen \a point within a given \a threshold
Q_INVOKABLE void selectVertexAtPosition( const QPointF &point, double threshold, bool autoInsert = true );

Expand Down Expand Up @@ -251,7 +256,7 @@ class QFIELD_CORE_EXPORT VertexModel : public QAbstractListModel
QVector<QgsPoint> verticesDeleted() const { return mVerticesDeleted; }

//! Returns a list of vertices
QList<Vertex> vertices() const;
Q_INVOKABLE QList<Vertex> vertices() const;

QHash<int, QByteArray> roleNames() const override;

Expand All @@ -270,6 +275,9 @@ class QFIELD_CORE_EXPORT VertexModel : public QAbstractListModel
bool canUndo();

double calculateAngle( const QgsPoint &a, const QgsPoint &b, const QgsPoint &c );
int snapToCommonAngleDegrees() const;
void setSnapToCommonAngleDegrees( int snapToCommonAngleDegrees );

signals:
//! \copydoc editingMode
void editingModeChanged();
Expand Down Expand Up @@ -307,6 +315,8 @@ class QFIELD_CORE_EXPORT VertexModel : public QAbstractListModel
//! Emitted when the history has been modified
void historyChanged();

void snapToCommonAngleDegreesChanged();

private:
void refreshGeometry();
//! Add the candidates of new vertices (extending or segment)
Expand Down Expand Up @@ -359,6 +369,7 @@ class QFIELD_CORE_EXPORT VertexModel : public QAbstractListModel
bool mHistoryTraversing = false;

friend class VertexModelTest;
int mSnapToCommonAngleDegrees;
};

Q_DECLARE_METATYPE( VertexModel::Vertex );
Expand Down
37 changes: 32 additions & 5 deletions src/qml/CoordinateLocator.qml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,24 @@ Item {
// Get the current crosshair location in screen coordinates. If `undefined`, then we use the center of the screen as input point.
const location = sourceLocation === undefined ? Qt.point(locator.width / 2, locator.height / 2) : sourceLocation;
if (snapToCommonAngleButton.isSnapToCommonAngleEnabled && geometryEditingVertexModel.angleFonud) {
vertexSnapToCommonAngleLines.endCoordX1 = 0;
vertexSnapToCommonAngleLines.endCoordY1 = 0;
vertexSnapToCommonAngleLines.endCoordX2 = 200;
vertexSnapToCommonAngleLines.endCoordY2 = 0;
const vertexCount = geometryEditingVertexModel.vertexCount;
const currentIndex = geometryEditingVertexModel.currentVertexIndex;
let startPoint = currentIndex - 2;
if (startPoint < 0) {
startPoint = vertexCount + startPoint;
}
let endPoint = currentIndex + 2;
if (endPoint >= vertexCount) {
endPoint = endPoint - vertexCount;
}
const start = mapSettings.coordinateToScreen(geometryEditingVertexModel.currentPoint);
const p1 = mapSettings.coordinateToScreen(geometryEditingVertexModel.getPoint(startPoint));
const p2 = mapSettings.coordinateToScreen(geometryEditingVertexModel.getPoint(endPoint));
const intersections = getIntersectionPoints(start, p1, p2, 1000, 1000);
vertexSnapToCommonAngleLines.endCoordX1 = intersections.x1 || 0;
vertexSnapToCommonAngleLines.endCoordY1 = intersections.y1 || 0;
vertexSnapToCommonAngleLines.endCoordX2 = intersections.x2 || 0;
vertexSnapToCommonAngleLines.endCoordY2 = intersections.y2 || 0;
} else if (snapToCommonAngleButton.isSnapToCommonAngleEnabled) {
locator.commonAngleInDegrees = getCommonAngleInDegrees(location, locator.rubberbandModel, snapToCommonAngleButton.snapToCommonAngleDegrees, snapToCommonAngleButton.isSnapToCommonAngleRelative);
const coords = calculateSnapToAngleLineEndCoords(snappedPoint, locator.commonAngleInDegrees, snapToCommonAngleButton.isSnapToCommonAngleRelative, 1000);
Expand Down Expand Up @@ -295,7 +309,7 @@ Item {
property double endCoordX2: 0
property double endCoordY2: 0

visible: (endCoordX1 + endCoordY1 + endCoordX2 + endCoordY2) > 0
visible: (endCoordX1 + endCoordY1 + endCoordX2 + endCoordY2) != 0
width: parent.width
height: parent.height
anchors.centerIn: parent
Expand Down Expand Up @@ -495,4 +509,17 @@ Item {
"y": y2
};
}

function getIntersectionPoints(centeralPoint, pointB, pointC) {
const xDiffAB = centeralPoint.x - pointB.x;
const yDiffAB = centeralPoint.y - pointB.y;
const xDiffAC = centeralPoint.x - pointC.x;
const yDiffAC = centeralPoint.y - pointC.y;
return {
"x1": centeralPoint.x - (10 * xDiffAB),
"y1": centeralPoint.y - (10 * yDiffAB),
"x2": centeralPoint.x - (10 * xDiffAC),
"y2": centeralPoint.y - (10 * yDiffAC)
};
}
}
1 change: 1 addition & 0 deletions src/qml/qgismobileapp.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4000,6 +4000,7 @@ ApplicationWindow {
currentPoint: coordinateLocator.currentCoordinate
mapSettings: mapCanvas.mapSettings
isHovering: mapCanvasMap.hovered
snapToCommonAngleDegrees: snapToCommonAngleButton.snapToCommonAngleDegrees
}

ScreenLocker {
Expand Down

0 comments on commit b11c935

Please sign in to comment.