1 /*
2  *    Copyright 2013 Thomas Schöps
3  *    Copyright 2014, 2017 Kai Pastor
4  *
5  *    This file is part of OpenOrienteering.
6  *
7  *    OpenOrienteering is free software: you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation, either version 3 of the License, or
10  *    (at your option) any later version.
11  *
12  *    OpenOrienteering is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #ifndef OPENORIENTEERING_FILL_TOOL_H
23 #define OPENORIENTEERING_FILL_TOOL_H
24 
25 #include <vector>
26 
27 #include <QImage>
28 #include <QObject>
29 // IWYU pragma: no_include <QRectF>
30 #include <QString>
31 #include <QTransform>
32 
33 #include "tool_base.h"
34 
35 class QAction;
36 class QPainter;
37 class QPoint;
38 class QRectF;
39 
40 namespace OpenOrienteering {
41 
42 class Map;
43 class MapEditorController;
44 class RenderConfig;
45 class Symbol;
46 
47 
48 /**
49  * Tool to fill bounded areas with PathObjects.
50  */
51 class FillTool : public MapEditorToolBase
52 {
53 Q_OBJECT
54 public:
55 	FillTool(MapEditorController* editor, QAction* tool_action);
56 	~FillTool() override;
57 
58 protected slots:
59 	void setDrawingSymbol(const OpenOrienteering::Symbol* symbol);
60 
61 protected:
62 	void updateStatusText() override;
63 	void objectSelectionChangedImpl() override;
64 
65 	void clickPress() override;
66 
67 	/**
68 	 * Tries to apply the fill tool at the current click position,
69 	 * rasterizing the given extent of the map.
70 	 * Returns -1 for abort, 0 for unsuccessful, 1 for successful.
71 	 */
72 	int fill(const QRectF& extent);
73 
74 	/**
75 	 * Rasterizes an area of the current map part with the given extent into an image.
76 	 *
77 	 * The pixels encodes object IDs (with alpha = 255). The background is white.
78 	 * Returns the image and the used map-to-image transform.
79 	 */
80 	QImage rasterizeMap(const QRectF& extent, QTransform& out_transform);
81 
82 	/**
83 	 * Helper method for rasterizeMap().
84 	 */
85 	void drawObjectIDs(Map* map, QPainter* painter, const RenderConfig& config);
86 
87 	/**
88 	 * Constructs the boundary around an area of free pixels in the given image.
89 	 *
90 	 * The discovered boundary is stored as a vector of pixel positions.
91 	 * The tracing starts from the free_pixel/boundary_pixel pair, where
92 	 * free_pixel references a free (transparent) pixel next to boundary pixel
93 	 * which reference an obstructed pixel of area to be traced.
94 	 *
95 	 * Returns:
96 	 * -1 if running out of the image borders,
97 	 *  0 if the tracing fails because the start is not included in the shape,
98 	 *  1 if the tracing succeeds.
99 	 */
100 	int traceBoundary(const QImage& image, const QPoint& free_pixel, const QPoint& boundary_pixel, std::vector<QPoint>& out_boundary);
101 
102 	/**
103 	 * Creates a fill object for the given image, boundary vector (of pixel positions) and transform.
104 	 * Returns false if the creation fails.
105 	 */
106 	bool fillBoundary(const QImage& image, const std::vector<QPoint>& boundary, const QTransform& image_to_map);
107 
108 	const Symbol* drawing_symbol;
109 };
110 
111 
112 }  // namespace OpenOrienteering
113 
114 #endif
115