UBGraphicsAristo.h 4.19 KB
Newer Older
Yimgo's avatar
Yimgo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef UBGRAPHICSARISTO_H_
#define UBGRAPHICSARISTO_H_

#include "core/UB.h"
#include "domain/UBItem.h"
21
#include "domain/UBGraphicsScene.h"
Yimgo's avatar
Yimgo committed
22 23
#include "tools/UBAbstractDrawRuler.h"

24
#include <QtGlobal>
25 26 27 28 29 30
#include <QBrush>
#include <QCursor>
#include <QGraphicsPathItem>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSvgItem>
31
#include <QObject>
32 33 34 35 36 37 38 39 40
#include <QPainter>
#include <QPainterPath>
#include <QPointF>
#include <QRectF>
#include <QStyleOptionGraphicsItem>
#include <QTransform>
#include <QWidget>

class UBGraphicsAristo : public UBAbstractDrawRuler, public QGraphicsPathItem, public UBItem
Yimgo's avatar
Yimgo committed
41
{
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
Q_OBJECT

public:
    UBGraphicsAristo();
    virtual ~UBGraphicsAristo();

    enum {
        Type = UBGraphicsItemType::AristoItemType 
    };

    enum Tool {
        None,
        Move,
        Resize,
        Rotate,
        Close,
        MoveMarker,
        HorizontalFlip
    };

    enum Orientation
    {
        Bottom = 0,
        Top,
        Undefined
    };
    
    void setOrientation(Orientation orientation);
    void setBoundingRect(QRectF boundingRect);        

    virtual UBItem* deepCopy() const;
    virtual void copyItemParameters(UBItem *copy) const;

    virtual void StartLine(const QPointF& scenePos, qreal width);
    virtual void DrawLine(const QPointF& position, qreal width);
    virtual void EndLine();

    virtual int type() const
    {
        return Type;
    }
    UBGraphicsScene* scene() const;

protected:
    virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget);

    virtual void rotateAroundCenter(qreal angle);
    virtual void resize(qreal factor);

    virtual QPointF rotationCenter() const;

    virtual QRectF closeButtonRect() const;
    QRectF hFlipRect() const;
    QRectF markerButtonRect() const;
    QRectF resizeButtonRect () const;        
    QRectF rotateRect() const;

    QCursor flipCursor() const;        
    QCursor markerCursor() const;
    QCursor resizeCursor() const;

    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);

private:
    Tool toolFromPos(QPointF pos);

    QTransform calculateRotationTransform();
    void rotateAroundCenter(QTransform& transform, QPointF center);

    void calculatePoints();
    QPainterPath determinePath();
    void setItemsPos();
    void makeGeometryChange();

    QBrush fillBrush() const;
    void paintGraduations(QPainter *painter);
    void paintMarker(QPainter *painter);
    void paintProtractorGraduations(QPainter* painter);
    void paintRulerGraduations(QPainter *painter);

    inline qreal radius () const
    {
        return sqrt(((B.x() - A.x())*(B.x() - A.x()))+((B.y() - A.y())*(B.y() - A.y()))) * 9 / 16 - 20;
    }        

    bool mMarking;
    bool mResizing;
    bool mRotating;

    Orientation mOrientation;

Guillaume Burel's avatar
Guillaume Burel committed
138 139
    qreal mRotatedAngle;
    qreal mMarkerAngle;
140 141 142 143 144 145 146 147 148 149 150 151 152 153
    qreal mStartAngle;

    qreal mSpan;

    QGraphicsSvgItem* mHFlipSvgItem;
    QGraphicsSvgItem* mMarkerSvgItem;
    QGraphicsSvgItem* mResizeSvgItem;
    QGraphicsSvgItem* mRotateSvgItem;      

    QPointF A, B, C;
    
    static const int sArcAngleMargin = 5;
    static const Orientation sDefaultOrientation;        
    static const QRectF sDefaultRect;
Yimgo's avatar
Yimgo committed
154 155 156
};

#endif /* UBGRAPHICSARISTO_H_ */