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 PART_TEXT_H
19 #define PART_TEXT_H
20 #include <QtWidgets>
21 #include "customelementpart.h"
22 #include "qetapp.h"
23 class TextEditor;
24 class ElementPrimitiveDecorator;
25 /**
26 	This class represents an static text primitive which may be used to compose
27 	the drawing of an electrical element within the element editor.
28 */
29 class PartText : public QGraphicsTextItem, public CustomElementPart
30 {
31 	Q_OBJECT
32 
33 	Q_PROPERTY(qreal real_size READ realSize WRITE setRealSize)
34 	Q_PROPERTY(QColor color READ defaultTextColor WRITE setDefaultTextColor NOTIFY colorChanged)
35 	Q_PROPERTY(QString text READ toPlainText WRITE setPlainText NOTIFY plainTextChanged)
36 	Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
37 
38 	signals:
39 		void fontChanged(const QFont &font);
40 		void colorChanged(const QColor &color);
41 		void plainTextChanged(const QString &text);
42 
43 		// constructors, destructor
44 	public:
45 		PartText(QETElementEditor *, QGraphicsItem * = nullptr);
46 		~PartText() override;
47 
48 	private:
49 		PartText(const PartText &);
50 
51 		// methods
52 	public:
53 		enum { Type = UserType + 1107 };
54 		/**
55 			Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a
56 			PartText.
57 			@return the QGraphicsItem type
58 		*/
type()59 		int type() const override { return Type; }
name()60 		QString name() const override { return(QObject::tr("texte", "element part name")); }
xmlName()61 		QString xmlName() const override { return(QString("text")); }
62 		void fromXml(const QDomElement &) override;
63 		const QDomElement toXml(QDomDocument &) const override;
setRotation(qreal angle)64 		void setRotation(qreal angle) {(QGraphicsObject::setRotation(QET::correctAngle(angle)));}
65 		bool isUseless() const override;
66 		QRectF sceneGeometricRect() const override;
67 		void startUserTransformation(const QRectF &) override;
68 		void handleUserTransformation(const QRectF &, const QRectF &) override;
69 
setProperty(const char * name,const QVariant & value)70 		void setProperty(const char *name, const QVariant &value) override {QGraphicsTextItem::setProperty(name, value);}
property(const char * name)71 		QVariant property(const char *name) const override {return QGraphicsTextItem::property(name);}
72 
realSize()73 		qreal realSize() const {return real_font_size_;}
setRealSize(qreal rs)74 		void setRealSize(qreal rs) {real_font_size_ = rs;}
75 		void setDefaultTextColor(const QColor &color);
76 		void setPlainText(const QString &text);
77 		void setFont(const QFont &font);
78 
79 	public slots:
80         void adjustItemPosition(int = 0);
81         void setEditable(bool);
82         void startEdition();
83         void endEdition();
84 
85 	protected:
86         void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
87         void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
88         void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
89         void focusInEvent(QFocusEvent *) override;
90         void focusOutEvent(QFocusEvent *) override;
91         void keyPressEvent(QKeyEvent *) override;
92         void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override;
93         QVariant itemChange(GraphicsItemChange, const QVariant &) override;
94         QRectF boundingRect() const override;
95 
96 	private:
97         QPointF margin() const;
98         QString previous_text;
99         qreal real_font_size_;
100         QPointF saved_point_;
101         qreal saved_font_size_;
102         QGraphicsItem *decorator_;
103         QPointF m_origine_pos;
104 };
105 #endif
106