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