QElectroTech  0.70
diagramtextitem.h
Go to the documentation of this file.
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 DIAGRAM_TEXT_ITEM_H
19 #define DIAGRAM_TEXT_ITEM_H
20 
21 #include <QGraphicsTextItem>
22 #include <QFont>
23 
24 class Diagram;
25 class QDomElement;
26 class QDomDocument;
27 
33 class DiagramTextItem : public QGraphicsTextItem
34 {
35  Q_OBJECT
36 
37  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
38  Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
39  Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText)
40  Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
41 
42 
43  signals:
44  void colorChanged(QColor color);
45  void alignmentChanged(Qt::Alignment alignment);
46  void textEdited(const QString &old_str, const QString &new_str);
47  void fontChanged(QFont font);
48 
49  public:
50  DiagramTextItem(QGraphicsItem * = nullptr);
51  DiagramTextItem(const QString &, QGraphicsItem * = nullptr);
52 
53  private:
54  void build();
55 
56  public:
57  enum { Type = UserType + 1004 };
58  int type() const override { return Type; }
59 
60  Diagram *diagram() const;
61  virtual void fromXml(const QDomElement &) = 0;
62  virtual QDomElement toXml(QDomDocument &) const;
63  void edit();
64 
65  QPointF mapMovementToScene (const QPointF &) const;
66  QPointF mapMovementFromScene (const QPointF &) const;
67  QPointF mapMovementToParent (const QPointF &) const;
68  QPointF mapMovementFromParent (const QPointF &) const;
69 
70  void setFont(const QFont &font);
71 
72  void setColor(const QColor& color);
73  QColor color() const;
74 
75  void setNoEditable(bool e = true) {m_no_editable = e;}
76 
77  void setAlignment(const Qt::Alignment &alignment);
78  Qt::Alignment alignment() const;
79  bool m_block_alignment = false;
80 
81  QRectF frameRect() const;
82 
83  void setHtml(const QString &text);
84  void setPlainText(const QString &text);
85  bool isHtml() const;
86 
87  protected:
88  void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;
89  void focusInEvent(QFocusEvent *) override;
90  void focusOutEvent(QFocusEvent *) override;
91 
92  void mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event) override;
93  void mousePressEvent (QGraphicsSceneMouseEvent *event) override;
94  void mouseMoveEvent (QGraphicsSceneMouseEvent *event) override;
95  void mouseReleaseEvent (QGraphicsSceneMouseEvent *event) override;
96 
97  void hoverEnterEvent(QGraphicsSceneHoverEvent *) override;
98  void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override;
99  void hoverMoveEvent(QGraphicsSceneHoverEvent *) override;
100 
101  virtual void applyRotation(const qreal &);
102  void prepareAlignment();
103  void finishAlignment();
104 
105 
106  protected:
107  bool m_mouse_hover = false,
108  m_first_move = true,
110  m_is_html = false;
111 
114 
116 
117  private:
119  Qt::Alignment m_alignment = (Qt::AlignTop | Qt::AlignLeft);
120 };
121 #endif
void fontChanged(QFont font)
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
Qt::Alignment alignment() const
void setHtml(const QString &text)
void textEdited(const QString &old_str, const QString &new_str)
void setColor(const QColor &color)
void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override
bool isHtml() const
QPointF mapMovementFromScene(const QPointF &) const
void finishAlignment()
DiagramTextItem::finishAlignment Call this function after changing the bouding rect of this text to s...
void colorChanged(QColor color)
void focusInEvent(QFocusEvent *) override
DiagramTextItem::focusInEvent.
void build()
DiagramTextItem::build Build this item with default value.
virtual QDomElement toXml(QDomDocument &) const
DiagramTextItem::toXml This method do nothing and return an empty DomElement This is used to be inher...
void prepareAlignment()
DiagramTextItem::prepareAlignment Call this function before changing the bounding rect of this text...
QPointF mapMovementToParent(const QPointF &) const
void setPlainText(const QString &text)
void edit()
Edit the text with HtmlEditor.
void hoverEnterEvent(QGraphicsSceneHoverEvent *) override
QString m_previous_html_text
void setAlignment(const Qt::Alignment &alignment)
virtual void applyRotation(const qreal &)
QPointF m_mouse_to_origin_movement
void alignmentChanged(Qt::Alignment alignment)
QPointF mapMovementToScene(const QPointF &) const
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
DiagramTextItem::mousePressEvent.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
DiagramTextItem::mouseReleaseEvent.
QPointF mapMovementFromParent(const QPointF &) const
Qt::Alignment m_alignment
virtual void fromXml(const QDomElement &)=0
void focusOutEvent(QFocusEvent *) override
DiagramTextItem::focusOutEvent.
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
DiagramTextItem::paint Draw this text field. This method draw the text by calling QGraphicsTextItem::...
void setFont(const QFont &font)
void hoverMoveEvent(QGraphicsSceneHoverEvent *) override
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
DiagramTextItem::mouseMoveEvent.
QRectF frameRect() const
DiagramTextItem::frameRect.
QColor color() const
void setNoEditable(bool e=true)
int type() const override
Diagram * diagram() const
DiagramTextItem::diagram.