Skip to content

Commit

Permalink
Scaled Geometry class implementation
Browse files Browse the repository at this point in the history
This patch implements the draw() function in the GeometryPointShapeScaled
class, and introduce a new abstract method drawShape().
A derived class must simply draw into the provided QPainter object that is
already translated, scaled and rotated accordingly to the parameters of
the ancestor object.
  • Loading branch information
studiofuga authored and maum committed Sep 2, 2019
1 parent 0e63f18 commit 67f0acc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
35 changes: 3 additions & 32 deletions QMapControl/src/QMapControl/GeometryPointImageScaled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,9 @@ namespace qmapcontrol
setSizePx(m_image->size(), update_shape);
}

void GeometryPointImageScaled::draw(QPainter& painter, const RectWorldCoord& backbuffer_rect_coord, const int& controller_zoom)
void GeometryPointImageScaled::drawShape(QPainter &painter, const RectWorldPx &rect)
{
// Check the geometry is visible.
if(isVisible(controller_zoom))
{
// Check if the bounding boxes intersect.
const RectWorldCoord pixmap_rect_coord(boundingBox(controller_zoom));
if(backbuffer_rect_coord.rawRect().intersects(pixmap_rect_coord.rawRect()))
{
// Calculate the pixmap rect to draw within.
const RectWorldPx pixmap_rect_px(projection::get().toPointWorldPx(pixmap_rect_coord.topLeftCoord(), controller_zoom), projection::get().toPointWorldPx(pixmap_rect_coord.bottomRightCoord(), controller_zoom));

// Translate to center point with required rotation.
painter.translate(pixmap_rect_px.centerPx().rawPoint());
painter.rotate(rotation());

// Draw the pixmap.
painter.drawPixmap(-pixmap_rect_px.rawRect().width() / 2.0, -pixmap_rect_px.rawRect().height() / 2.0, image());

// Un-translate.
painter.rotate(-rotation());
painter.translate(-pixmap_rect_px.centerPx().rawPoint());

// Do we have a meta-data value and should we display it at this zoom?
if(controller_zoom >= m_metadata_displayed_zoom_minimum && metadata(m_metadata_displayed_key).isNull() == false)
{
/// @todo calculate correct alignment for metadata displayed offset.

// Draw the text next to the point with an offset.
painter.drawText(pixmap_rect_px.rawRect().topRight() + PointPx(m_metadata_displayed_alignment_offset_px, -m_metadata_displayed_alignment_offset_px).rawPoint(), metadata(m_metadata_displayed_key).toString());
}
}
}
painter.drawPixmap(-rect.rawRect().width() / 2.0, -rect.rawRect().height() / 2.0, image());
}

}
11 changes: 5 additions & 6 deletions QMapControl/src/QMapControl/GeometryPointImageScaled.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ namespace qmapcontrol
*/
void setImage(const QPixmap& new_image, const bool& update_shape = true);

public:
protected:
/*!
* Draws the geometry to a pixmap using the provided painter.
* @param painter The painter that will draw to the pixmap.
* @param backbuffer_rect_coord Only draw geometries that are contained in the backbuffer rect (world coordinates).
* @param controller_zoom The current controller zoom.
* \brief drawShape Draws the shape in a transformed painter according to zoom/translate/rotate status
* \param painter
* \param rect The rect in pixel coordinates
*/
void draw(QPainter& painter, const RectWorldCoord& backbuffer_rect_coord, const int& controller_zoom) final;
virtual void drawShape(QPainter &painter, const RectWorldPx &rect);

private:
/// The image pixmap to draw.
Expand Down
39 changes: 39 additions & 0 deletions QMapControl/src/QMapControl/GeometryPointShapeScaled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,45 @@ namespace qmapcontrol
return RectWorldCoord(projection::get().toPointWorldCoord(top_left_point_px, controller_zoom), projection::get().toPointWorldCoord(bottom_right_point_px, controller_zoom));
}

void GeometryPointShapeScaled::draw(QPainter &painter, const RectWorldCoord &backbuffer_rect_coord, const int &controller_zoom)
{
// Check the geometry is visible.
if(isVisible(controller_zoom))
{
// Check if the bounding boxes intersect.
const RectWorldCoord pixmap_rect_coord(boundingBox(controller_zoom));
if(backbuffer_rect_coord.rawRect().intersects(pixmap_rect_coord.rawRect()))
{
// Calculate the pixmap rect to draw within.
const RectWorldPx pixmap_rect_px(projection::get().toPointWorldPx(pixmap_rect_coord.topLeftCoord(), controller_zoom), projection::get().toPointWorldPx(pixmap_rect_coord.bottomRightCoord(), controller_zoom));


qreal scale = pow(2.0, controller_zoom - baseZoom());

// Translate to center point with required rotation.
painter.translate(pixmap_rect_px.centerPx().rawPoint());
painter.rotate(rotation());
painter.scale(scale, scale);

drawShape(painter, pixmap_rect_px);

// Un-translate.
painter.scale(1.0/scale, 1.0/scale);
painter.rotate(-rotation());
painter.translate(-pixmap_rect_px.centerPx().rawPoint());

// Do we have a meta-data value and should we display it at this zoom?
if(controller_zoom >= m_metadata_displayed_zoom_minimum && metadata(m_metadata_displayed_key).isNull() == false)
{
/// @todo calculate correct alignment for metadata displayed offset.

// Draw the text next to the point with an offset.
painter.drawText(pixmap_rect_px.rawRect().topRight() + PointPx(m_metadata_displayed_alignment_offset_px, -m_metadata_displayed_alignment_offset_px).rawPoint(), metadata(m_metadata_displayed_key).toString());
}
}
}
}

const QSizeF GeometryPointShapeScaled::calculateGeometrySizePx(const int& controller_zoom) const
{
// Get the object size (default to base size).
Expand Down
17 changes: 17 additions & 0 deletions QMapControl/src/QMapControl/GeometryPointShapeScaled.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ namespace qmapcontrol
*/
RectWorldCoord boundingBox(const int& controller_zoom) const final;

/*!
* Draws the geometry to a pixmap using the provided painter.
* @param painter The painter that will draw to the pixmap.
* @param backbuffer_rect_coord Only draw geometries that are contained in the backbuffer rect (world coordinates).
* @param controller_zoom The current controller zoom.
*/
void draw(QPainter& painter, const RectWorldCoord& backbuffer_rect_coord, const int& controller_zoom);

private:
/*!
* Calculates the shape size in pixels for the given controller zoom.
Expand All @@ -143,6 +151,15 @@ namespace qmapcontrol
*/
const QSizeF calculateGeometrySizePx(const int& controller_zoom) const;

protected:
/*!
* \brief drawShape Draws the shape in a transformed painter according to zoom/translate/rotate status
* A derived class must implement this method
* \param painter
* \param rect The rect in pixel coordinates
*/
virtual void drawShape(QPainter &painter, const RectWorldPx &rect) = 0;

private:
/// The base zoom level.
int m_base_zoom;
Expand Down

0 comments on commit 67f0acc

Please sign in to comment.