1 /**********************************************************************************************
2     Copyright (C) 2009 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #ifndef CGARMINTYP_H
20 #define CGARMINTYP_H
21 
22 #include <QtGui>
23 
24 class CGarminTyp
25 {
26     Q_DECLARE_TR_FUNCTIONS(CGarminTyp)
27 public:
28     CGarminTyp() = default;
29     virtual ~CGarminTyp() = default;
30 
31     enum label_type_e
32     {
33         eStandard  = 0
34         , eNone      = 1
35         , eSmall     = 2
36         , eNormal    = 3
37         , eLarge     = 4
38     };
39 
40     struct polyline_property
41     {
polyline_propertypolyline_property42         polyline_property()
43             : type(0)
44             , penLineDay(Qt::magenta, 3)
45             , penLineNight(Qt::magenta, 3)
46             , hasBorder(false)
47             , penBorderDay(Qt::NoPen)
48             , penBorderNight(Qt::NoPen)
49             , hasPixmap(false)
50             , labelType(eStandard)
51             , colorLabelDay(Qt::black)
52             , colorLabelNight(Qt::black)
53             , known(false)
54 
55         {
56         }
57 
polyline_propertypolyline_property58         polyline_property(quint16 type, const QPen& penLineDay, const QPen& penLineNight, const QPen& penBorderDay, const QPen& penBorderNight)
59             : type(type)
60             , penLineDay(penLineDay)
61             , penLineNight(penLineNight)
62             , hasBorder(true)
63             , penBorderDay(penBorderDay)
64             , penBorderNight(penBorderNight)
65             , hasPixmap(false)
66             , labelType(eStandard)
67             , colorLabelDay(Qt::black)
68             , colorLabelNight(Qt::black)
69             , known(true)
70         {
71         }
72 
polyline_propertypolyline_property73         polyline_property(quint16 type, const QColor& color, int width, Qt::PenStyle style)
74             : type(type)
75             , penLineDay(QPen(color, width, style))
76             , penLineNight(penLineDay)
77             , hasBorder(false)
78             , penBorderDay(Qt::NoPen)
79             , penBorderNight(Qt::NoPen)
80             , hasPixmap(false)
81             , labelType(eStandard)
82             , colorLabelDay(Qt::black)
83             , colorLabelNight(Qt::black)
84             , known(true)
85         {
86         }
87 
88         quint16 type;
89 
90         QPen penLineDay;
91         QPen penLineNight;
92 
93         bool hasBorder;
94         QPen penBorderDay;
95         QPen penBorderNight;
96 
97         bool hasPixmap;
98         QImage imgDay;
99         QImage imgNight;
100 
101         QMap<int, QString> strings;
102         label_type_e labelType;
103         QColor colorLabelDay;
104         QColor colorLabelNight;
105 
106         bool known;
107     };
108 
109     struct polygon_property
110     {
polygon_propertypolygon_property111         polygon_property()
112             : type(0)
113             , pen(Qt::magenta)
114             , brushDay(Qt::magenta, Qt::BDiagPattern)
115             , brushNight(Qt::magenta, Qt::BDiagPattern)
116             , labelType(eStandard)
117             , colorLabelDay(Qt::black)
118             , colorLabelNight(Qt::black)
119             , known(false)
120         {
121         }
122 
polygon_propertypolygon_property123         polygon_property(quint16 type, const Qt::PenStyle pensty, const QColor& brushColor, Qt::BrushStyle pattern)
124             : type(type)
125             , pen(pensty)
126             , brushDay(brushColor, pattern)
127             , brushNight(brushColor.darker(150), pattern)
128             , labelType(eStandard)
129             , colorLabelDay(Qt::black)
130             , colorLabelNight(Qt::black)
131             , known(true)
132         {
133             pen.setWidth(1);
134         }
135 
polygon_propertypolygon_property136         polygon_property(quint16 type, const QColor& penColor, const QColor& brushColor, Qt::BrushStyle pattern)
137             : type(type)
138             , pen(penColor, 1)
139             , brushDay(brushColor, pattern)
140             , brushNight(brushColor.darker(150), pattern)
141             , labelType(eStandard)
142             , colorLabelDay(Qt::black)
143             , colorLabelNight(Qt::black)
144             , known(true)
145         {
146         }
147 
148         quint16 type;
149         QPen pen;
150         QBrush brushDay;
151         QBrush brushNight;
152 
153         QMap<int, QString> strings;
154         label_type_e labelType;
155         QColor colorLabelDay;
156         QColor colorLabelNight;
157         bool known;
158     };
159 
160     struct point_property
161     {
point_propertypoint_property162         point_property() : labelType(eStandard)
163         {
164         }
165         QImage imgDay;
166         QImage imgNight;
167 
168         QMap<int, QString> strings;
169         label_type_e labelType;
170         QColor colorLabelDay;
171         QColor colorLabelNight;
172     };
173 
174     /// decode typ file
175     /**
176         This pure virtual function has to be implemented in every subclass. It should
177         be the only public function needed. The typ file is read and it's content is
178         stored in the passed map/list objects.
179 
180         @param in input data stream
181         @param polygons reference to polygon properties map
182         @param polylines reference to polyline properties map
183         @param drawOrder reference to list of polygon draw orders
184         @param points reference to point properties map
185 
186      */
187     bool decode(const QByteArray& array, QMap<quint32, polygon_property>& polygons, QMap<quint32, polyline_property>& polylines, QList<quint32>& drawOrder, QMap<quint32, point_property>& points);
188 
getLanguages()189     QSet<quint8> getLanguages()
190     {
191         return languages;
192     }
193 
getFid()194     quint16 getFid()
195     {
196         return fid;
197     }
198 
getPid()199     quint16 getPid()
200     {
201         return pid;
202     }
203 
204 protected:
205     virtual bool parseHeader(QDataStream& in);
206     virtual bool parseDrawOrder(QDataStream& in, QList<quint32>& drawOrder);
207     virtual bool parsePolygon(QDataStream& in, QMap<quint32, polygon_property>& polygons);
208     virtual bool parsePolyline(QDataStream& in, QMap<quint32, polyline_property>& polylines);
209     virtual bool parsePoint(QDataStream& in, QMap<quint32, point_property>& points);
210 
211     QTextCodec* getCodec(quint16 codepage);
212     void decodeBitmap(QDataStream& in, QImage& img, int w, int h, int bpp);
213     bool decodeBppAndBytes(int ncolors, int w, int flags, int& bpp, int& bytes);
214     bool decodeColorTable(QDataStream& in, QImage& img, int ncolors, int maxcolor, bool hasAlpha);
215 
216 
217     struct typ_section_t
218     {
typ_section_ttyp_section_t219         typ_section_t() : dataOffset(0), dataLength(0), arrayOffset(0), arrayModulo(0), arraySize(0)
220         {
221         }
222         quint32 dataOffset;
223         quint32 dataLength;
224         quint32 arrayOffset;
225         quint16 arrayModulo;
226         quint32 arraySize;
227     };
228 
229     quint16 version = 0;
230     quint16 codepage = 0;
231     quint16 year = 0;
232     quint8 month = 0;
233     quint8 day = 0;
234     quint8 hour = 0;
235     quint8 minutes = 0;
236     quint8 seconds = 0;
237 
238     quint16 fid = 0;
239     quint16 pid = 0;
240 
241     typ_section_t sectPoints;
242     typ_section_t sectPolylines;
243     typ_section_t sectPolygons;
244     typ_section_t sectOrder;
245 
246     QSet<quint8> languages;
247 };
248 #endif                           //CGARMINTYP_H
249