File indexing completed on 2024-10-06 07:02:30

0001 /***************************************************************************
0002                           kimearea.h  -  description
0003                              -------------------
0004     begin                : Thu Jun 14 2001
0005     copyright            : (C) 2001 by Jan Schäfer
0006     email                : janschaefer@users.sourceforge.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef KIMEAREA_H
0019 #define KIMEAREA_H
0020 
0021 #include <QHash>
0022 #include <QHashIterator>
0023 #include <QList>
0024 #include <QPixmap>
0025 #include <QPoint>
0026 #include <QRect>
0027 #include <QTreeWidgetItem>
0028 
0029 #include <klocalizedstring.h>
0030 
0031 class QPainter;
0032 class QPolygon;
0033 class QBitmap;
0034 
0035 
0036 typedef QHash<QString,QString> AttributeMap;
0037 typedef QHashIterator<QString,QString> AttributeIterator;
0038 
0039 #define SELSIZE 9
0040 
0041 
0042 
0043 class SelectionPoint 
0044 {
0045 
0046 public:
0047   enum State {
0048     Normal,
0049     HighLighted,
0050     AboutToRemove,
0051     Inactive
0052   };
0053 
0054 
0055   SelectionPoint(QPoint,QCursor);
0056   virtual ~SelectionPoint();
0057 
0058   void setState(SelectionPoint::State s);
0059   State getState() const;
0060 
0061   QPoint getPoint() const;
0062   void setPoint(QPoint);
0063   void translate(int,int);
0064 
0065   QRect getRect() const;
0066 
0067   void draw(QPainter*, double);
0068 
0069   QCursor cursor();
0070   void setCursor(QCursor);
0071 
0072 private:
0073   QPoint point;
0074   State state;
0075   QCursor _cursor;
0076   
0077 };
0078 
0079 typedef QList<SelectionPoint*> SelectionPointList;
0080 
0081 
0082 class Area
0083 {
0084 public:
0085   enum ShapeType { 
0086     None, 
0087     Rectangle, 
0088     Circle, 
0089     Polygon, 
0090     Default, 
0091     Selection 
0092   };
0093 
0094   static bool highlightArea;
0095   static bool showAlt;
0096 
0097 protected:
0098     QRect _rect;
0099     ShapeType _type;
0100     QString _name;
0101     QString _href;
0102     QString _alt;
0103     QString _target;
0104     AttributeMap _attributes;
0105     bool _isSelected;
0106     bool _finished;
0107     bool _isMoving;
0108     int currentHighlighted;
0109     QTreeWidgetItem* _listViewItem;
0110 
0111     QPolygon _coords;
0112     SelectionPointList _selectionPoints;
0113 
0114     void setPenAndBrush(QPainter* p);
0115     void drawAlt(QPainter*);
0116     QString getHTMLAttributes() const;
0117     void deleteSelectionPoints();
0118 
0119 public:
0120     Area();
0121     virtual ~Area();
0122 
0123     virtual Area* clone() const;
0124     // Default implementation; is specified by subclasses
0125     virtual bool contains(const QPoint &) const;
0126     // Default implementation; is specified by subclasses
0127     virtual QString coordsToString() const;
0128     virtual void draw(QPainter*);
0129 
0130     virtual QBitmap getMask() const;
0131     virtual QString getHTMLCode() const;
0132 
0133     virtual void moveBy(int, int);
0134     virtual void moveTo(int, int);
0135     virtual void moveSelectionPoint(SelectionPoint*, const QPoint &);
0136 
0137     virtual SelectionPoint* onSelectionPoint(const QPoint &,double zoom) const;
0138     virtual bool removeSelectionPoint(SelectionPoint* );
0139     virtual const SelectionPointList & selectionPoints() const { return _selectionPoints; }
0140     virtual void resetSelectionPointState();
0141     virtual void setSelectionPointStates(SelectionPoint::State st);
0142 
0143     virtual QRect rect() const;
0144 
0145     virtual QRect selectionRect() const;
0146     virtual void setArea(const Area &);
0147     virtual bool setCoords(const QString &);
0148     /** finished drawing only important for polygon */
0149     virtual void setFinished(bool b, bool removeLast = true); 
0150     virtual void setRect(const QRect &);
0151   virtual void setMoving(bool b);
0152   virtual bool isMoving() const;
0153     // Default implementation; is specified by subclasses
0154     virtual QString typeString() const { return ""; }
0155     virtual ShapeType type() const;
0156 
0157     virtual void updateSelectionPoints() {};
0158 
0159     // Only interesting for Polygons
0160     virtual void simplifyCoords() {};
0161     virtual int addCoord(const QPoint &);
0162     virtual void insertCoord(int, const QPoint &);
0163     virtual void removeCoord(int);
0164     virtual void moveCoord(int,const QPoint &);
0165     virtual QPolygon coords() const;
0166 
0167     virtual void highlightSelectionPoint(int);
0168 
0169     virtual QString attribute(const QString &) const;
0170     virtual void setAttribute(const QString &, const QString &);
0171     virtual AttributeIterator attributeIterator() const;
0172 
0173     QPixmap cutOut(const QImage &) ;
0174     void setListViewItem(QTreeWidgetItem*);
0175     void deleteListViewItem();
0176     QTreeWidgetItem* listViewItem() const;
0177     void setName(const QString &);
0178     QString name() const;
0179     void setSelected(bool b);
0180     bool isSelected() const;
0181     bool finished() const;
0182     int countSelectionPoints() const;
0183 
0184 };
0185 
0186 
0187 
0188 inline QTreeWidgetItem* Area::listViewItem() const {
0189     return _listViewItem;
0190 }
0191 
0192 inline void Area::setName(const QString & name) {
0193     _name=name;
0194 }
0195 
0196 inline QString Area::name() const {
0197     return _name;
0198 }
0199 
0200 inline bool Area::isMoving() const {
0201   return _isMoving;
0202 }
0203 
0204 
0205 inline bool Area::isSelected() const {
0206     return _isSelected;
0207 }
0208 
0209 
0210 inline bool Area::finished() const {
0211     return _finished;
0212 }
0213 
0214 /**
0215  *  Represents a Rectangle Area
0216  **/
0217 class RectArea : public Area
0218 {
0219     public:
0220         RectArea();
0221         ~RectArea() override;
0222 
0223         Area* clone() const override;
0224         bool contains(const QPoint & p) const override;
0225         QString coordsToString() const override;
0226         void draw(QPainter*) override;
0227         void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p) override;
0228         bool setCoords(const QString & s) override;
0229         QString typeString() const override { return i18n("Rectangle"); }
0230         QBitmap getMask() const override;
0231             QString getHTMLCode() const override;
0232         void updateSelectionPoints() override;
0233 };
0234 
0235 
0236 /**
0237  *  Represents a Circle Area
0238  **/
0239 class CircleArea : public Area
0240 {
0241     public:
0242         CircleArea();
0243         ~CircleArea() override;
0244 
0245         Area* clone() const override;
0246         bool contains(const QPoint & p) const override;
0247         QString coordsToString() const override;
0248         void draw(QPainter*) override;
0249         void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p) override;
0250         bool setCoords(const QString & s) override;
0251         void setRect(const QRect & r) override;
0252         QString typeString() const override { return i18n("Circle"); }
0253         QBitmap getMask() const override;
0254         QString getHTMLCode() const override;
0255         void updateSelectionPoints() override;
0256 
0257 };
0258 
0259 /**
0260  *  Represents a Rectangle Area
0261  **/
0262 class PolyArea :public Area
0263 {
0264     public:
0265         PolyArea();
0266         ~PolyArea() override;
0267 
0268         Area* clone() const override;
0269         bool contains(const QPoint & p) const override;
0270         QString coordsToString() const override;
0271         void draw(QPainter*) override;
0272         void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p) override;
0273         void simplifyCoords() override;
0274     int addCoord(const QPoint & p) override;
0275         bool setCoords(const QString & s) override;
0276         QRect selectionRect() const override;
0277         void setFinished(bool b, bool removeLast) override;
0278         QString typeString() const override { return i18n("Polygon"); }
0279         QBitmap getMask() const override;
0280         QString getHTMLCode() const override;
0281         void updateSelectionPoints() override;
0282 
0283   private:
0284    static int distance(const QPoint &p1, const QPoint &p2);
0285    static bool isBetween(const QPoint &p, const QPoint &p1, const QPoint &p2);
0286 
0287 };
0288 
0289 /**
0290  *  Represents the default Area
0291  **/
0292 class DefaultArea :public Area
0293 {
0294  public:
0295   DefaultArea();
0296   ~DefaultArea() override;
0297 
0298   Area* clone() const override;
0299   // the default area isn't drawn
0300   void draw(QPainter*) override;
0301   QString typeString() const override { 
0302     return i18n("Default"); 
0303   }
0304   QString getHTMLCode() const override;
0305 
0306 };
0307 
0308 
0309 typedef QList<Area*> AreaList;
0310 typedef QListIterator<Area*> AreaListIterator;
0311 
0312 /**
0313  *  This class represents a selection of areas
0314  *  all operations performed on this class
0315  *  will be performed on the selected Areas
0316  *  the only actions that can be used is the
0317  *  move action
0318  **/
0319 class AreaSelection : public Area {
0320 public :
0321   AreaSelection();
0322   ~AreaSelection() override;
0323 
0324   /**
0325    * New Methods
0326    */
0327 
0328   // Adding automatically selects the area
0329   void add(Area *a);
0330 
0331   // Removing automatically deselects the area
0332   void remove(Area *a);
0333 
0334   // Removes all areas from the list and deselects them
0335   void reset();
0336 
0337   int count() const;
0338 
0339   AreaList getAreaList() const;
0340   AreaListIterator getAreaListIterator() const;
0341   void setAreaList( const AreaList & areas );
0342 
0343   bool isEmpty() const;
0344 
0345   /**
0346    * Overridden Methods of the Area class
0347    */
0348   bool contains(const QPoint & p) const override;
0349 
0350   /**
0351    *
0352    **/
0353   SelectionPoint* onSelectionPoint(const QPoint & p, double zoom) const override;
0354 
0355   /**
0356    * Only if one Area is selected the moveSelectionPoint method
0357    * of that Area will be called
0358    **/
0359   void moveSelectionPoint(SelectionPoint* selectionPoint, const QPoint & p) override;
0360 
0361   const SelectionPointList & selectionPoints() const override;
0362 
0363   /**
0364    * All Areas will be moved by dx and dy
0365    **/
0366   void moveBy(int dx, int dy) override;
0367 
0368   /**
0369    * Calls for every selected Area the setArea with the
0370    * corresponding Area in the copy Selection.
0371    * IMPORTANT : works only if the copy Area is an AreaSelection
0372    * and have the same number of Areas
0373    **/
0374   void setArea(const Area & copy) override;
0375   virtual void setAreaSelection(const AreaSelection & copy);
0376 
0377   /**
0378    * If only one Area is selected the setRect method of that Area
0379    * will be called
0380    **/
0381   void setRect(const QRect & r) override;
0382   QRect rect() const override;
0383 
0384 
0385   QString typeString() const override;
0386   ShapeType type() const override;
0387 
0388   // The selection is only a container
0389   // so it is never drawn
0390   void draw(QPainter*) override;
0391 
0392 
0393   /**
0394    * A deep copy of the Areas
0395    **/
0396   Area* clone() const override;
0397 
0398   void resetSelectionPointState() override;
0399   void setSelectionPointStates(SelectionPoint::State st) override;
0400   void updateSelectionPointStates();
0401 
0402   void updateSelectionPoints() override;
0403   int addCoord(const QPoint & p) override;
0404   void insertCoord(int pos, const QPoint & p) override;
0405   void removeCoord(int pos) override;
0406   bool removeSelectionPoint(SelectionPoint *) override;
0407   void moveCoord(int pos,const QPoint & p) override;
0408   QPolygon coords() const override;
0409   void highlightSelectionPoint(int) override;
0410 
0411   QRect selectionRect() const override;
0412 
0413   QString attribute(const QString & name) const override;
0414   void setAttribute(const QString & name, const QString & value) override;
0415   AttributeIterator attributeIterator() const override;
0416 
0417   void setMoving(bool b) override;
0418   bool isMoving() const override;
0419 
0420 
0421   bool allAreasWithin(const QRect & r) const;
0422 
0423   // makes the cache invalid
0424   void invalidate();
0425 
0426 private :
0427   AreaList *_areas;
0428 
0429   // The selectionRect and the rect are cached
0430   // so even in const functions they must be changeable
0431   mutable QRect _cachedSelectionRect;
0432   mutable QRect _cachedRect;
0433   mutable bool _selectionCacheValid;
0434   mutable bool _rectCacheValid;
0435 
0436 };
0437 
0438 
0439 #endif
0440 
0441