File: template_image.h

package info (click to toggle)
openorienteering-mapper 0.6.7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,556 kB
  • ctags: 8,993
  • sloc: cpp: 84,560; sh: 3,530; ansic: 1,917; java: 145; xml: 135; sed: 43; makefile: 30
file content (176 lines) | stat: -rw-r--r-- 5,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 *    Copyright 2012, 2013 Thomas Schöps
 *
 *    This file is part of OpenOrienteering.
 *
 *    OpenOrienteering is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OpenOrienteering is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef _OPENORIENTEERING_TEMPLATE_IMAGE_H_
#define _OPENORIENTEERING_TEMPLATE_IMAGE_H_

#include "template.h"

#include <QDialog>
#include <QImage>

QT_BEGIN_NAMESPACE
class QCheckBox;
class QRadioButton;
class QXmlStreamReader;
class QXmlStreamWriter;
QT_END_NAMESPACE

class Georeferencing;

/**
 * Template showing a raster image.
 * Can be georeferenced or non-georeferenced.
 */
class TemplateImage : public Template
{
Q_OBJECT
public:
	enum GeoreferencingType
	{
		Georeferencing_None = 0,
		Georeferencing_WorldFile,
		Georeferencing_GeoTiff
	};
	
	/**
	 * Returns the filename extensions supported by this template class.
	 */
	static const std::vector<QByteArray>& supportedExtensions();
	
	TemplateImage(const QString& path, Map* map);
    virtual ~TemplateImage();
	virtual const char* getTemplateType() const {return "TemplateImage";}
	virtual bool isRasterGraphics() const {return true;}

	virtual bool saveTemplateFile() const;
	virtual bool loadTypeSpecificTemplateConfiguration(QIODevice* stream, int version);
	virtual void saveTypeSpecificTemplateConfiguration(QXmlStreamWriter& xml) const;
	virtual bool loadTypeSpecificTemplateConfiguration(QXmlStreamReader& xml);

	virtual bool loadTemplateFileImpl(bool configuring);
	virtual bool postLoadConfiguration(QWidget* dialog_parent, bool& out_center_in_view);
	virtual void unloadTemplateFileImpl();
	
    virtual void drawTemplate(QPainter* painter, QRectF& clip_rect, double scale, bool on_screen, float opacity) const;
	virtual QRectF getTemplateExtent() const;
	virtual bool canBeDrawnOnto() const {return true;}

	/**
	 * Calculates the image's center of gravity in template coordinates by
	 * iterating over all pixels, leaving out the pixels with background_color.
	 */
	QPointF calcCenterOfGravity(QRgb background_color);
	
	/** Returns the internal QImage. */
	inline const QImage& getImage() const {return image;}
	
	/**
	 * Returns which georeferencing method (if any) is available.
	 * (This does not mean that the image is in georeferenced mode)
	 */
	inline GeoreferencingType getAvailableGeoreferencing() const {return available_georef;}
	
public slots:
	void updateGeoreferencing();
	
protected:
	/** Holds a pixel-to-world transform loaded from a world file. */
	struct WorldFile
	{
		bool loaded;
		QTransform pixel_to_world;
		
		/// Creates an unloaded world file
		WorldFile();
		
		/// Tries to load the given path as world file.
		/// Returns true on success and sets loaded to true or false.
		bool load(const QString& path);
		
		/// Tries to find and load a world file for the given image path.
		bool tryToLoadForImage(const QString& image_path);
	};
	
	/** Information about an undo step for the paint-on-template functionality. */
	struct DrawOnImageUndoStep
	{
		/** Copy of previous image part */
		QImage image;
		
		/** X position of image part origin */
		int x;
		
		/** Y position of image part origin */
		int y;
	};
	
	virtual Template* duplicateImpl() const;
	virtual void drawOntoTemplateImpl(MapCoordF* coords, int num_coords, QColor color, float width);
	virtual void drawOntoTemplateUndo(bool redo);
	void addUndoStep(const DrawOnImageUndoStep& new_step);
	void calculateGeoreferencing();
	void updatePosFromGeoreferencing();

	QImage image;
	
	std::vector< DrawOnImageUndoStep > undo_steps;
	/// Current index in undo_steps, where 0 means before the first item.
	int undo_index;
	
	GeoreferencingType available_georef;
	QScopedPointer<Georeferencing> georef;
	// Temporary storage for crs spec. Use georef instead.
	QString temp_crs_spec;
};

/**
 * Initial setting dialog when opening a raster image as template,
 * asking for how to position the image.
 * 
 * \todo Move this class to separate files.
 */
class TemplateImageOpenDialog : public QDialog
{
Q_OBJECT
public:
	TemplateImageOpenDialog(TemplateImage* templ, QWidget* parent);
	
	double getMpp() const;
	bool isGeorefRadioChecked() const;
	
protected slots:
	void radioClicked();
	void setOpenEnabled();
	void doAccept();
	
private:
	QRadioButton* georef_radio;
	QRadioButton* mpp_radio;
	QRadioButton* dpi_radio;
	QLineEdit* mpp_edit;
	QLineEdit* dpi_edit;
	QLineEdit* scale_edit;
	QPushButton* open_button;
	
	TemplateImage* templ;
};

#endif