1 /*
2 	Copyright 2006-2019 The QElectroTech Team
3 	This file is part of QElectroTech.
4 
5 	QElectroTech is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	QElectroTech is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef CUSTOM_ELEMENT_GRAPHIC_PART_H
19 #define CUSTOM_ELEMENT_GRAPHIC_PART_H
20 
21 #include <QGraphicsObject>
22 #include "customelementpart.h"
23 
24 class QETElementEditor;
25 class QPainter;
26 
27 
28 /**
29  * @brief The CustomElementGraphicPart class
30  * This class is the base for all home-made primitive like line, rectangle, ellipse etc....
31  * It provides methods and enums to manage style attributes available for primitive (color, pen style, etc...)
32  */
33 class CustomElementGraphicPart : public QGraphicsObject, public CustomElementPart
34 {
35 		#define SHADOWS_HEIGHT 4.0
36 
37 		Q_OBJECT
38 
39 		Q_PROPERTY(LineStyle line_style   READ lineStyle   WRITE setLineStyle)
40 		Q_PROPERTY(LineWeight line_weight READ lineWeight  WRITE setLineWeight)
41 		Q_PROPERTY(Filling filling        READ filling     WRITE setFilling)
42 		Q_PROPERTY(Color color            READ color       WRITE setColor)
43 		Q_PROPERTY(bool antialias         READ antialiased WRITE setAntialiased)
44 
45 	public:
46 			//Line style
47 		enum LineStyle {NormalStyle, DashedStyle, DottedStyle, DashdottedStyle};
48 		Q_ENUM (LineStyle)
49 
50 			//Line weight : invisible, 0px, 1px, 2px, 5px
51 		enum LineWeight {NoneWeight, ThinWeight, NormalWeight, UltraWeight, BigWeight};
52 		Q_ENUM (LineWeight)
53 
54 			//Filling color of the part : NoneFilling -> No filling (i.e. transparent)
55 		enum Filling { NoneFilling, BlackFilling, WhiteFilling, GreenFilling, RedFilling, BlueFilling, GrayFilling, BrunFilling, YellowFilling, CyanFilling, MagentaFilling, LightgrayFilling, OrangeFilling, PurpleFilling, HorFilling, VerFilling, BdiagFilling, FdiagFilling};
56 		Q_ENUM (Filling)
57 
58 			//Line color
59 		enum Color {BlackColor, WhiteColor, GreenColor, RedColor, BlueColor, GrayColor, BrunColor, YellowColor, CyanColor, MagentaColor, LightgrayColor, OrangeColor, PurpleColor, NoneColor};
60 		Q_ENUM (Color)
61 
62 		// constructors, destructor
63 	public:
64 
65 		CustomElementGraphicPart(QETElementEditor *editor, QGraphicsItem *parent = nullptr);
66 		~CustomElementGraphicPart() override;
67 
68 		static void drawCross (const QPointF &center, QPainter *painter);
69 
70 			//Getter and setter
lineStyle()71 		LineStyle lineStyle    () const {return _linestyle;}
72 		void      setLineStyle (const LineStyle ls);
73 
lineWeight()74 		LineWeight lineWeight    () const {return _lineweight;}
75 		void       setLineWeight (const LineWeight lw);
76 		qreal      penWeight     () const;
77 
filling()78 		Filling filling   () const {return _filling;}
79 		void    setFilling(const Filling f);
80 
color()81 		Color color   () const {return _color;}
82 		void  setColor(const Color c);
83 
antialiased()84 		bool antialiased   () const {return _antialiased;}
85 		void setAntialiased(const bool b);
86 			//End of getter and setter
87 
88 
89 			//Rediriged to QObject Q_PROPERTY system
setProperty(const char * name,const QVariant & value)90 		void     setProperty (const char *name, const QVariant &value) override {QObject::setProperty(name, value);}
property(const char * name)91 		QVariant property    (const char *name) const override                  {return QObject::property(name);}
92 
93 		virtual QPainterPath shadowShape ()const = 0;
94 
95 	protected:
96 		void stylesToXml  (QDomElement &) const;
97 		void stylesFromXml(const QDomElement &);
98 		void resetStyles  ();
99 		void applyStylesToQPainter(QPainter &) const;
100 		void drawShadowShape (QPainter *painter);
101 
102 		QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
103 		void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
104 		void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
105 
106 		void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
107 		void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
108 		void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
109 
110 		// attributes
111 		bool m_hovered;
112 	private:
113 		LineStyle _linestyle;
114 		LineWeight _lineweight;
115 		Filling _filling ;
116 		Color _color;
117 		bool _antialiased;
118         QPointF m_origin_pos;
119 };
120 
121 typedef CustomElementGraphicPart CEGP;
122 #endif
123