AnnotationObject.qt.H

00001 #ifndef ANNOTATIONOBJECT_QT_H
00002 #define ANNOTATIONOBJECT_QT_H
00003 
00004 #include <QtGui/QtGui>
00005 #include "NeovisionII/NeoAnnotate/AnimationModel.qt.H"
00006 #include "NeovisionII/NeoAnnotate/ObjectAnimation.qt.H"
00007 
00008 
00009 class AnnotationObject;
00010 class FrameRange;
00011 //class ObjectAnimation;
00012 
00013 class AnnotationObjectVertex : public QObject, public QGraphicsItem
00014 {
00015   Q_OBJECT
00016 
00017   public:
00018     //! Create a new vertex to be a part of a parent Annotation Object
00019     AnnotationObjectVertex(AnnotationObject *parent, int frameNum, FrameRange frameRange, QPointF initialPos);
00020 
00021     //! Get the bounding rectangle of this vertex
00022     QRectF boundingRect() const;
00023 
00024     //! Paint this vertex on the screen
00025     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
00026 
00027     ObjectAnimation * getAnimation() { return itsAnimation; }
00028 
00029     ObjectAnimation::FrameState getFrameState(int fnum) { return itsAnimation->getFrameState(fnum); }
00030 
00031     //Make the object's brush and pen opaque
00032     void makeOpaque();
00033 
00034     //Make the object's brush and pen transparent
00035     void makeTransparent();
00036 
00037     //! Set a new keyframe
00038     void setKeyframe(int frameNum, QPointF pos, bool visible);
00039 
00040     //! Clear all keypoints from the animation
00041     void clearAnimation();
00042 
00043   signals:
00044     void animationChanged();
00045 
00046   public slots:
00047     //! Recompute our position and visibility for the given frame and redraw
00048     void frameChanged(int);
00049 
00050   protected:
00051     //! Overloaded to redraw the parent whenever the vertex is moved
00052     QVariant itemChange(GraphicsItemChange change, const QVariant &value);
00053 
00054     //! Highlight this vertex when the mouse moves over
00055     void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
00056 
00057     //! Dehighlight this vertex when the mouse leaves
00058     void hoverLeaveEvent(QGraphicsSceneHoverEvent * event);
00059 
00060     //! Create a new keyframe with the new position of the object when moved by the mouse
00061     //! and select this object
00062     void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
00063 
00064     //! Select the object when the user clicks on it
00065     void mousePressEvent(QGraphicsSceneMouseEvent * event);
00066 
00067   private:
00068     const QRectF getVertexRect(void) const;
00069 
00070     const QRectF itsVertexSize;   //!< Denotes how large to draw vertices
00071 
00072     AnnotationObject * itsParent; //!< A reference to the parent object
00073                                   //!< of which this vertex is a part
00074 
00075     QPen   itsPen;                //!< The pen to outline this vertex
00076 
00077     QBrush itsBrush;              //!< The brush to fill this vertex
00078 
00079     QColor itsColor;              //!< The color of this vertex
00080 
00081     ObjectAnimation * itsAnimation;
00082 
00083     int itsCurrentFrame;
00084 };
00085 
00086 
00087 class AnnotationObject : public QObject, public QGraphicsItem
00088 {
00089   Q_OBJECT
00090 
00091   public:
00092     //! Create a new Annotation Object with a default description and type
00093     AnnotationObject(int frameNum, FrameRange frameRange, QPointF initialPos,
00094         QString description="None", int type=0,
00095         QGraphicsItem * parent = 0);
00096 
00097     //! Destroy this Annotation Object, and destroy all of it's vertices
00098     ~AnnotationObject();
00099 
00100     //! Insert a new vertex into the store at the given index
00101     void addVertex(int index, AnnotationObjectVertex * v);
00102 
00103     //! Add a vertex to this object at the given point
00104     //* This function will find the nearest line on this object's polygon, and will
00105     //  insert the new vertex between the two endpoints of that line */
00106     void insertVertexAtPoint(QPointF point);
00107 
00108     //! Remove the top most vertex of this object at the given point
00109     void removeVertex(QPointF point);
00110 
00111     //! Highlight this object
00112     void showSelected();
00113 
00114     //! Dehighlight this object
00115     void showDeselected();
00116 
00117     //! Get the bounding rectangle of this object, given by the bounding rect of it's polygon
00118     QRectF boundingRect() const;
00119 
00120     //! Draw this object on the screen
00121     /*! The paint function goes clockwise through each of the vertices in itsVertices, and constructs
00122      *  a QPolygon from their positions. This polygon is then drawn on the screen using a fill brush
00123      *  with itsColor, and a thick white line pen.*/
00124     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
00125 
00126     //! Get the Unique ID of this object.
00127     /*! This ID is auto-incremented by a static counter for each created object. */
00128     int     getId()    const { return itsId;    }
00129 
00130     //! Get the user specified description of this object
00131     QString getDescription()  const { return itsDescription;  }
00132 
00133     //! Get the user specified type of this object
00134     int getType()  const { return itsType;  }
00135 
00136     //! Get the randomly assigned color of this object
00137     QColor  getColor() const { return itsColor; }
00138 
00139     //! Compute the center point of this object
00140     QPointF getCenter() const;
00141 
00142     //! Set the description of this object
00143     void setDescription(QString description) { itsDescription = description; setToolTip(description); update(); }
00144 
00145     //! Set the type of this object
00146     void setType(int type) { itsType = type; update(); }
00147 
00148     //! Get the interpolated frame state for a given frame
00149     ObjectAnimation::FrameState getFrameState(int fnum) { return itsAnimation->getFrameState(fnum); }
00150 
00151     //! Get a pointer to our member animation
00152     ObjectAnimation * getAnimation() { return itsAnimation; }
00153 
00154     //! Get a pointer to the vertices structure
00155     QList<AnnotationObjectVertex*>* getVertices() { return &itsVertices; }
00156 
00157     //! Get a pointer to the animation model
00158     AnimationModel* getAnimationModel();
00159 
00160     //! Ask our ObjectAnimation member to construct a context menu
00161     void constructAnimationContextMenu(QPoint globalPos, int row, int column);
00162 
00163     int getCurrentFrameNum() { return itsCurrentFrame; }
00164 
00165     //! Tell the animation view that a vertex has been selected
00166     /*! This will then highlight the proper row in the view so that users can
00167         have some feedback about what they're editing*/
00168     void selectVertex(AnnotationObjectVertex *vertex);
00169 
00170     //! Return the actual shape of the polygon for hit detection
00171     QPainterPath shape() const;
00172 
00173     //! Allow vertices to prepare the object for a geometry change
00174     /*! When vertices are moved, they need to tell their parent object
00175      *  that the bounding box has been modified so things can be drawn
00176      *  correclty. This is called at the end of a vertex' mouseReleaseEvent
00177      *  to save a bit on processing power. */
00178     void pubPrepareGeometryChange() { this->prepareGeometryChange(); }
00179 
00180     //! Store the index of the last click in the animation model - used for drag and drop
00181     void setLastClick(const QModelIndex & index) {itsAnimationModel.setLastClick(index);}
00182 
00183     //! Set the user selectable opacity for this object
00184     void setOpacity(int opacity) { itsOpacity = opacity; update(); }
00185 
00186     //! Render this object's keyframed/relative animation out to an absolute structure.
00187     /*! The returned result of this method is a vector, which contains an entry for each
00188      *  vertex of the object.  Each of these entries is a Map, which maps framenumbers to
00189      *  absolute vertex positions at that framenumber. Some vertices may not have entries for
00190      *  every frame because they were made invisible.*/
00191     QVector<QMap<int, QPointF > > renderAnimation();
00192 
00193     //! Get all of the interpolated vertex states for a given frame number
00194     std::map<int, ObjectAnimation::FrameState> getVertexStates(int fnum);
00195 
00196     //! Reset all of the objects vertices to these values
00197     void setVertices(std::map<int, ObjectAnimation::FrameState> VertexFrames);
00198 
00199     //! Set a new keyframe
00200     void setKeyframe(int frameNum, QPointF pos, bool visible);
00201 
00202     //! Force the id of this object - this should really only be done when loading from XML
00203     void forceId(int id);
00204 
00205     //! Get a vertex by it's index
00206     AnnotationObjectVertex* getVertexById(int id) { return itsVertices[id]; }
00207 
00208     //! Clear all keypoints from the animation
00209     void clearAnimation();
00210   signals:
00211     //! Let the manager know that we have been selected by the user
00212     void objectSelected(int);
00213 
00214     //! Let the view know which row has been selected
00215     void rowSelected(int);
00216 
00217     //! Let the model know that the animation state has changed
00218     void animationChanged(int row, int frame);
00219 
00220 
00221   public slots:
00222     //! Recompute our position and visibility for the given frame and redraw
00223     void frameChanged(int);
00224 
00225     //! Recompute our position and visibility because our animation was changed
00226     void updateAnimation();
00227 
00228   protected:
00229     //! Create a new keyframe with the new position of the object when moved by the mouse
00230     //! and select this object
00231     void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
00232 
00233     //! Select the object when the user clicks on it
00234     void mousePressEvent(QGraphicsSceneMouseEvent * event);
00235 
00236   private:
00237     //! Recalculate itsPoly based on the currently visible vertices
00238     void recalculatePoly();
00239 
00240     static int theirIdCount;       //!< Count of # objects created for unique ids
00241 
00242     int itsId;               //!< The unique id of this object
00243 
00244     QString itsDescription;               //!< The user assigned description of this object
00245 
00246     int itsType;                   //!< The user assigned type of this object
00247 
00248     QPolygonF itsPoly;             //!< The polygon generated from the list of vertices
00249 
00250     QBrush itsBrush;               //!< The brush for drawing this object
00251 
00252     QPen itsPen;                   //!< The pen for outlining this object
00253 
00254     QColor itsColor;               //!< The color of this object
00255 
00256     int itsCurrentFrame;           //!< The current frame number of the animation
00257 
00258     ObjectAnimation *itsAnimation; //!< The object containing all of our animation information
00259 
00260     int itsOpacity;                //!< The user setable opacity
00261 
00262     bool itsSelected;              //!< Whether or not this object is selected by the user
00263 
00264     AnimationModel itsAnimationModel;
00265 
00266     QList<AnnotationObjectVertex*> itsVertices; //!< The list of vertices
00267                                                 /*!< Vertices in this list are
00268                                                      in strictly clockwise ordering*/
00269 };
00270 
00271 //! A simple struct to hold the state of an annotation object at a single frame
00272 /*! This struct is just used to store detailed info about annotation objects 
00273  *  when rendering animations out to XML or wherever. An std::map is necessary
00274  *  for the VertexFrame storage, as it will ensure that the frames are stored
00275  *  in order of ID so that they are removed and inserted in the same order.*/
00276 struct AnnotationObjectFrame
00277 {
00278   QString  ObjectDescription;
00279   int      ObjectType;
00280   int      ObjectId;
00281   ObjectAnimation::FrameState ObjectFrameState;
00282   std::map<int, ObjectAnimation::FrameState> VertexFrames;
00283 };
00284 
00285 
00286 #endif //ANNOTATIONOBJECT_QT_H
00287 
00288 
Generated on Sun May 8 08:41:02 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3