1 | /*************************************************************************** |
2 | kimearea.h - description |
3 | ------------------- |
4 | begin : Thu Jun 14 2001 |
5 | copyright : (C) 2001 by Jan Schäfer |
6 | email : janschaefer@users.sourceforge.net |
7 | ***************************************************************************/ |
8 | |
9 | /*************************************************************************** |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | ***************************************************************************/ |
17 | |
18 | #ifndef KIMEAREA_H |
19 | #define KIMEAREA_H |
20 | |
21 | #include <qrect.h> |
22 | #include <qpoint.h> |
23 | #include <QList> |
24 | |
25 | #include <QPixmap> |
26 | #include <QLinkedList> |
27 | #include <QHashIterator> |
28 | #include <QTreeWidgetItem> |
29 | #include <klocale.h> |
30 | #include <qhash.h> |
31 | #include "kdeversion.h" |
32 | |
33 | class QPainter; |
34 | class QPolygon; |
35 | class QBitmap; |
36 | |
37 | |
38 | typedef QHash<QString,QString> AttributeMap; |
39 | typedef QHashIterator<QString,QString> AttributeIterator; |
40 | |
41 | #define SELSIZE 9 |
42 | |
43 | |
44 | |
45 | class SelectionPoint |
46 | { |
47 | |
48 | public: |
49 | enum State { |
50 | Normal, |
51 | HighLighted, |
52 | AboutToRemove, |
53 | Inactive |
54 | }; |
55 | |
56 | |
57 | SelectionPoint(QPoint,QCursor); |
58 | virtual ~SelectionPoint(); |
59 | |
60 | void setState(SelectionPoint::State s); |
61 | State getState() const; |
62 | |
63 | QPoint getPoint() const; |
64 | void setPoint(QPoint); |
65 | void translate(int,int); |
66 | |
67 | QRect getRect() const; |
68 | |
69 | void draw(QPainter*, double); |
70 | |
71 | QCursor cursor(); |
72 | void setCursor(QCursor); |
73 | |
74 | private: |
75 | QPoint point; |
76 | State state; |
77 | QCursor _cursor; |
78 | |
79 | }; |
80 | |
81 | typedef QList<SelectionPoint*> SelectionPointList; |
82 | |
83 | |
84 | class Area |
85 | { |
86 | public: |
87 | enum ShapeType { |
88 | None, |
89 | Rectangle, |
90 | Circle, |
91 | Polygon, |
92 | Default, |
93 | Selection |
94 | }; |
95 | |
96 | static bool highlightArea; |
97 | static bool showAlt; |
98 | |
99 | protected: |
100 | QRect _rect; |
101 | ShapeType _type; |
102 | QString _name; |
103 | QString _href; |
104 | QString _alt; |
105 | QString _target; |
106 | AttributeMap _attributes; |
107 | bool _isSelected; |
108 | bool _finished; |
109 | bool _isMoving; |
110 | int currentHighlighted; |
111 | QTreeWidgetItem* _listViewItem; |
112 | |
113 | QPolygon _coords; |
114 | SelectionPointList _selectionPoints; |
115 | |
116 | void setPenAndBrush(QPainter* p); |
117 | void drawAlt(QPainter*); |
118 | QString getHTMLAttributes() const; |
119 | void deleteSelectionPoints(); |
120 | |
121 | public: |
122 | Area(); |
123 | virtual ~Area(); |
124 | |
125 | virtual Area* clone() const; |
126 | // Default implementation; is specified by subclasses |
127 | virtual bool contains(const QPoint &) const; |
128 | // Default implementation; is specified by subclasses |
129 | virtual QString coordsToString() const; |
130 | virtual void draw(QPainter*); |
131 | |
132 | virtual QBitmap getMask() const; |
133 | virtual QString getHTMLCode() const; |
134 | |
135 | virtual void moveBy(int, int); |
136 | virtual void moveTo(int, int); |
137 | virtual void moveSelectionPoint(SelectionPoint*, const QPoint &); |
138 | |
139 | virtual SelectionPoint* onSelectionPoint(const QPoint &,double zoom) const; |
140 | virtual bool removeSelectionPoint(SelectionPoint* ); |
141 | virtual const SelectionPointList & selectionPoints() const { return _selectionPoints; } |
142 | virtual void resetSelectionPointState(); |
143 | virtual void setSelectionPointStates(SelectionPoint::State st); |
144 | |
145 | virtual QRect rect() const; |
146 | |
147 | virtual QRect selectionRect() const; |
148 | virtual void setArea(const Area &); |
149 | virtual bool setCoords(const QString &); |
150 | /** finished drawing only important for polygon */ |
151 | virtual void setFinished(bool b, bool removeLast = true); |
152 | virtual void setRect(const QRect &); |
153 | virtual void setMoving(bool b); |
154 | virtual bool isMoving() const; |
155 | // Default implementation; is specified by subclasses |
156 | virtual QString typeString() const { return "" ; } |
157 | virtual ShapeType type() const; |
158 | |
159 | virtual void updateSelectionPoints() {}; |
160 | |
161 | // Only interesting for Polygons |
162 | virtual void simplifyCoords() {}; |
163 | virtual int addCoord(const QPoint &); |
164 | virtual void insertCoord(int, const QPoint &); |
165 | virtual void removeCoord(int); |
166 | virtual void moveCoord(int,const QPoint &); |
167 | virtual QPolygon coords() const; |
168 | |
169 | virtual void highlightSelectionPoint(int); |
170 | |
171 | virtual QString attribute(const QString &) const; |
172 | virtual void setAttribute(const QString &, const QString &); |
173 | virtual AttributeIterator attributeIterator() const; |
174 | |
175 | QPixmap cutOut(const QImage &) ; |
176 | void setListViewItem(QTreeWidgetItem*); |
177 | void deleteListViewItem(); |
178 | QTreeWidgetItem* listViewItem() const; |
179 | void setName(const QString &); |
180 | QString name() const; |
181 | void setSelected(bool b); |
182 | bool isSelected() const; |
183 | bool finished() const; |
184 | int countSelectionPoints() const; |
185 | |
186 | }; |
187 | |
188 | |
189 | |
190 | inline QTreeWidgetItem* Area::listViewItem() const { |
191 | return _listViewItem; |
192 | } |
193 | |
194 | inline void Area::setName(const QString & name) { |
195 | _name=name; |
196 | } |
197 | |
198 | inline QString Area::name() const { |
199 | return _name; |
200 | } |
201 | |
202 | inline bool Area::isMoving() const { |
203 | return _isMoving; |
204 | } |
205 | |
206 | |
207 | inline bool Area::isSelected() const { |
208 | return _isSelected; |
209 | } |
210 | |
211 | |
212 | inline bool Area::finished() const { |
213 | return _finished; |
214 | } |
215 | |
216 | /** |
217 | * Represents a Rectangle Area |
218 | **/ |
219 | class RectArea : public Area |
220 | { |
221 | public: |
222 | RectArea(); |
223 | virtual ~RectArea(); |
224 | |
225 | virtual Area* clone() const; |
226 | virtual bool contains(const QPoint & p) const; |
227 | virtual QString coordsToString() const; |
228 | virtual void draw(QPainter*); |
229 | virtual void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p); |
230 | virtual bool setCoords(const QString & s); |
231 | virtual QString typeString() const { return i18n("Rectangle" ); } |
232 | virtual QBitmap getMask() const; |
233 | virtual QString getHTMLCode() const; |
234 | virtual void updateSelectionPoints(); |
235 | }; |
236 | |
237 | |
238 | /** |
239 | * Represents a Circle Area |
240 | **/ |
241 | class CircleArea : public Area |
242 | { |
243 | public: |
244 | CircleArea(); |
245 | virtual ~CircleArea(); |
246 | |
247 | virtual Area* clone() const; |
248 | virtual bool contains(const QPoint & p) const; |
249 | virtual QString coordsToString() const; |
250 | virtual void draw(QPainter*); |
251 | virtual void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p); |
252 | virtual bool setCoords(const QString & s); |
253 | virtual void setRect(const QRect & r); |
254 | virtual QString typeString() const { return i18n("Circle" ); } |
255 | virtual QBitmap getMask() const; |
256 | virtual QString getHTMLCode() const; |
257 | virtual void updateSelectionPoints(); |
258 | |
259 | }; |
260 | |
261 | /** |
262 | * Represents a Rectangle Area |
263 | **/ |
264 | class PolyArea :public Area |
265 | { |
266 | public: |
267 | PolyArea(); |
268 | virtual ~PolyArea(); |
269 | |
270 | virtual Area* clone() const; |
271 | virtual bool contains(const QPoint & p) const; |
272 | virtual QString coordsToString() const; |
273 | virtual void draw(QPainter*); |
274 | virtual void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p); |
275 | virtual void simplifyCoords(); |
276 | virtual int addCoord(const QPoint & p); |
277 | virtual bool setCoords(const QString & s); |
278 | virtual QRect selectionRect() const; |
279 | virtual void setFinished(bool b, bool removeLast); |
280 | virtual QString typeString() const { return i18n("Polygon" ); } |
281 | virtual QBitmap getMask() const; |
282 | virtual QString getHTMLCode() const; |
283 | virtual void updateSelectionPoints(); |
284 | |
285 | private: |
286 | static int distance(const QPoint &p1, const QPoint &p2); |
287 | static bool isBetween(const QPoint &p, const QPoint &p1, const QPoint &p2); |
288 | |
289 | }; |
290 | |
291 | /** |
292 | * Represents the default Area |
293 | **/ |
294 | class DefaultArea :public Area |
295 | { |
296 | public: |
297 | DefaultArea(); |
298 | virtual ~DefaultArea(); |
299 | |
300 | virtual Area* clone() const; |
301 | // the default area isn't drawn |
302 | virtual void draw(QPainter*); |
303 | virtual QString typeString() const { |
304 | return i18n("Default" ); |
305 | } |
306 | virtual QString getHTMLCode() const; |
307 | |
308 | }; |
309 | |
310 | |
311 | typedef QList<Area*> AreaList; |
312 | typedef QListIterator<Area*> AreaListIterator; |
313 | |
314 | /** |
315 | * This class represents a selection of areas |
316 | * all operations performed on this class |
317 | * will be performed on the selected Areas |
318 | * the only actions that can be used is the |
319 | * move action |
320 | **/ |
321 | class AreaSelection : public Area { |
322 | public : |
323 | AreaSelection(); |
324 | virtual ~AreaSelection(); |
325 | |
326 | /** |
327 | * New Methods |
328 | */ |
329 | |
330 | // Adding automatically selects the area |
331 | void add(Area *a); |
332 | |
333 | // Removing automatically deselects the area |
334 | void remove(Area *a); |
335 | |
336 | // Removes all areas from the list and deselects them |
337 | void reset(); |
338 | |
339 | int count() const; |
340 | |
341 | AreaList getAreaList() const; |
342 | AreaListIterator getAreaListIterator() const; |
343 | void setAreaList( const AreaList & areas ); |
344 | |
345 | bool isEmpty() const; |
346 | |
347 | /** |
348 | * Overiden Methods of the Area class |
349 | */ |
350 | virtual bool contains(const QPoint & p) const; |
351 | |
352 | /** |
353 | * |
354 | **/ |
355 | virtual SelectionPoint* onSelectionPoint(const QPoint & p, double zoom) const; |
356 | |
357 | /** |
358 | * Only if one Area is selected the moveSelectionPoint method |
359 | * of that Area will be called |
360 | **/ |
361 | virtual void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p); |
362 | |
363 | virtual const SelectionPointList & selectionPoints() const; |
364 | |
365 | /** |
366 | * All Areas will be moved by dx and dy |
367 | **/ |
368 | virtual void moveBy(int dx, int dy); |
369 | |
370 | /** |
371 | * Calls for every selected Area the setArea with the |
372 | * corresponding Area in the copy Selection. |
373 | * IMPORTANT : works only if the copy Area is an AreaSelection |
374 | * and have the same number of Areas |
375 | **/ |
376 | virtual void setArea(const Area & copy); |
377 | virtual void setAreaSelection(const AreaSelection & copy); |
378 | |
379 | /** |
380 | * If only one Area is selected the setRect method of that Area |
381 | * will be called |
382 | **/ |
383 | virtual void setRect(const QRect & r); |
384 | virtual QRect rect() const; |
385 | |
386 | |
387 | virtual QString typeString() const; |
388 | virtual ShapeType type() const; |
389 | |
390 | // The selection is only a container |
391 | // so it is never drawn |
392 | virtual void draw(QPainter*); |
393 | |
394 | |
395 | /** |
396 | * A deep copy of the Areas |
397 | **/ |
398 | virtual Area* clone() const; |
399 | |
400 | virtual void resetSelectionPointState(); |
401 | virtual void setSelectionPointStates(SelectionPoint::State st); |
402 | void updateSelectionPointStates(); |
403 | |
404 | virtual void updateSelectionPoints(); |
405 | virtual int addCoord(const QPoint & p); |
406 | virtual void insertCoord(int pos, const QPoint & p); |
407 | virtual void removeCoord(int pos); |
408 | virtual bool removeSelectionPoint(SelectionPoint *); |
409 | virtual void moveCoord(int pos,const QPoint & p); |
410 | virtual QPolygon coords() const; |
411 | virtual void highlightSelectionPoint(int); |
412 | |
413 | virtual QRect selectionRect() const; |
414 | |
415 | virtual QString attribute(const QString & name) const; |
416 | virtual void setAttribute(const QString & name, const QString & value); |
417 | virtual AttributeIterator attributeIterator() const; |
418 | |
419 | virtual void setMoving(bool b); |
420 | virtual bool isMoving() const; |
421 | |
422 | |
423 | bool allAreasWithin(const QRect & r) const; |
424 | |
425 | // makes the cache invalid |
426 | void invalidate(); |
427 | |
428 | private : |
429 | AreaList *_areas; |
430 | |
431 | // The selectionRect and the rect are cached |
432 | // so even in const functions they must be changeable |
433 | mutable QRect _cachedSelectionRect; |
434 | mutable QRect _cachedRect; |
435 | mutable bool _selectionCacheValid; |
436 | mutable bool _rectCacheValid; |
437 | |
438 | }; |
439 | |
440 | |
441 | #endif |
442 | |
443 | |
444 | |