UBGraphicsTriangle.h 5.23 KB
Newer Older
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10
 * (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.
11
 *
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14 15 16 17 18 19 20 21 22 23 24
 */

#ifndef UBGRAPHICSTRIANGLE_H_
#define UBGRAPHICSTRIANGLE_H_

#include <QtGui>
#include <QtSvg>
#include <QGraphicsPolygonItem>

#include "core/UB.h"
#include "domain/UBItem.h"
25
#include "tools/UBAbstractDrawRuler.h"
26 27 28


class UBGraphicsScene;
29
class UBItem;
30

31
class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonItem, public UBItem
32
{
33
    Q_OBJECT
34 35 36 37

    public:
        UBGraphicsTriangle();
        virtual ~UBGraphicsTriangle();
38

39
        enum { Type = UBGraphicsItemType::TriangleItemType };
40 41 42 43 44 45 46

        virtual int type() const
        {
            return Type;
        }


47
        virtual UBItem* deepCopy(void) const;
48
        virtual void copyItemParameters(UBItem *copy) const;
49

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

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        enum UBGraphicsTriangleOrientation
        {
                BottomLeft = 0,
                BottomRight,
                TopLeft,
                TopRight
        };

        static UBGraphicsTriangleOrientation orientationFromStr(QStringRef& str)
        {
            if (str == "BottomLeft") return BottomLeft;
            if (str == "BottomRight") return BottomRight;
            if (str == "TopLeft") return TopLeft;
            if (str == "TopRight") return TopRight;
            return sDefaultOrientation;
        }
70 71 72 73 74 75 76 77 78 79 80
        static QString orientationToStr(UBGraphicsTriangleOrientation orientation)
        {
            QString result;
            if (orientation == 0) result = "BottomLeft";
            else if (orientation == 1) result = "BottomRight";
            else if (orientation == 2) result = "TopLeft";
            else if (orientation == 3) result = "TopRight";

            return result;
        }

81 82 83 84 85 86
        void setRect(const QRectF &rect, UBGraphicsTriangleOrientation orientation)
        {
            setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation);
        }
        void setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation);
        void setOrientation(UBGraphicsTriangleOrientation orientation);
87
        UBGraphicsTriangleOrientation getOrientation() const {return mOrientation;}
88 89 90 91 92
        QRectF rect() const {return boundingRect();}

        UBGraphicsScene* scene() const;

    protected:
unknown's avatar
unknown committed
93

94 95
        void updateResizeCursor();

96
        virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget);
97
        virtual QPainterPath shape() const;
98

99
        virtual void rotateAroundCenter(qreal angle);
100

101
        virtual QPointF    rotationCenter() const;
102

103 104 105 106 107 108
        virtual QRectF    closeButtonRect() const;
        QPolygonF resize1Polygon() const;
        QPolygonF resize2Polygon() const;
        QRectF    hFlipRect() const;
        QRectF    vFlipRect() const;
        QRectF    rotateRect() const;
unknown's avatar
unknown committed
109

110 111
        QCursor    resizeCursor1() const;
        QCursor    resizeCursor2() const;
112

113
        QCursor    flipCursor() const;
unknown's avatar
unknown committed
114

115
        virtual void    mousePressEvent(QGraphicsSceneMouseEvent *event);
unknown's avatar
unknown committed
116
        virtual void    mouseMoveEvent(QGraphicsSceneMouseEvent *event);
117 118 119 120
        virtual void    mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
        virtual void    hoverEnterEvent(QGraphicsSceneHoverEvent *event);
        virtual void    hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
        virtual void    hoverMoveEvent(QGraphicsSceneHoverEvent *event);
121

122
    private:
123

124 125 126
        QCursor mResizeCursor1;
        QCursor mResizeCursor2;

unknown's avatar
unknown committed
127 128
        QTransform calculateRotationTransform();
        qreal angle;
129
        void rotateAroundCenter(QTransform& transform, QPointF center);
unknown's avatar
unknown committed
130

131 132 133 134
        bool mResizing1;
        bool mResizing2;
        bool mRotating;
        QRect lastRect;
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
135 136

        // Coordinates are transformed....
137
        QPoint lastPos;
unknown's avatar
unknown committed
138

139 140 141
        QGraphicsSvgItem* mHFlipSvgItem;
        QGraphicsSvgItem* mVFlipSvgItem;
        QGraphicsSvgItem* mRotateSvgItem;
unknown's avatar
unknown committed
142

143 144
        static const QRect sDefaultRect;
        static const UBGraphicsTriangleOrientation sDefaultOrientation;
145

146
        void paintGraduations(QPainter *painter);
147 148


149
        UBGraphicsTriangleOrientation mOrientation;
unknown's avatar
unknown committed
150

151 152 153 154 155 156
        QPointF A1, B1, C1, A2, B2, C2; // coordinates of points in ext and int triangles
        qreal C;
        qreal W1, H1; // Neccessary for filling
        QPointF CC; // Hyp. fillining gradient - top point
        void calculatePoints(const QRectF& rect);

157 158 159 160
        static const int d = 70; // width of triangle border
        static const int sArrowLength = 30;
        static const int sMinWidth = 380;
        static const int sMinHeight = 200;
161 162 163
};

#endif /* UBGRAPHICSTRIANGLE_H_ */