2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2013-04-01 13:49:34 +00:00
|
|
|
Copyright 2006-2013 QElectroTech Team
|
2007-12-01 10:47:15 +00:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech 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 QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2007-09-26 22:57:53 +00:00
|
|
|
#include "diagramtextitem.h"
|
|
|
|
#include "diagramcommands.h"
|
2008-07-09 17:30:52 +00:00
|
|
|
#include "qetapp.h"
|
2013-04-09 11:26:53 +00:00
|
|
|
#include "richtext/richtexteditor_p.h"
|
2014-07-21 20:44:32 +00:00
|
|
|
#include "diagram.h"
|
2013-04-09 11:26:53 +00:00
|
|
|
|
2007-09-26 22:57:53 +00:00
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param parent Le QGraphicsItem parent du champ de texte
|
2010-04-18 17:59:54 +00:00
|
|
|
@param parent_diagram Le schema auquel appartient le champ de texte
|
2007-09-26 22:57:53 +00:00
|
|
|
*/
|
2010-04-18 17:59:54 +00:00
|
|
|
DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, Diagram *parent_diagram) :
|
|
|
|
QGraphicsTextItem(parent, parent_diagram),
|
2010-04-23 22:39:59 +00:00
|
|
|
previous_text_(),
|
2014-10-17 21:30:42 +00:00
|
|
|
rotation_angle_(0.0),
|
|
|
|
m_first_move (true)
|
2007-09-26 22:57:53 +00:00
|
|
|
{
|
2014-10-18 12:25:16 +00:00
|
|
|
build();
|
2007-09-26 22:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param text Le texte affiche par le champ de texte
|
2010-04-18 17:59:54 +00:00
|
|
|
@param parent Le QGraphicsItem parent du champ de texte
|
|
|
|
@param parent_diagram Le schema auquel appartient le champ de texte
|
2007-09-26 22:57:53 +00:00
|
|
|
*/
|
2010-04-18 17:59:54 +00:00
|
|
|
DiagramTextItem::DiagramTextItem(const QString &text, QGraphicsItem *parent, Diagram *parent_diagram) :
|
|
|
|
QGraphicsTextItem(text, parent, parent_diagram),
|
2010-04-23 22:39:59 +00:00
|
|
|
previous_text_(text),
|
2009-11-29 21:56:48 +00:00
|
|
|
rotation_angle_(0.0)
|
2007-09-26 22:57:53 +00:00
|
|
|
{
|
2014-10-18 12:25:16 +00:00
|
|
|
build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destructeur
|
|
|
|
DiagramTextItem::~DiagramTextItem() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DiagramTextItem::build
|
|
|
|
* Build this item with default value
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::build() {
|
2013-08-24 15:18:45 +00:00
|
|
|
//set Zvalue at 10 to be upper than the DiagramImageItem
|
|
|
|
setZValue(10);
|
2007-11-01 23:56:19 +00:00
|
|
|
setDefaultTextColor(Qt::black);
|
2009-04-12 17:21:19 +00:00
|
|
|
setFont(QETApp::diagramTextsFont());
|
2010-04-25 17:34:46 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
|
2013-12-29 18:09:25 +00:00
|
|
|
setNoEditable(false);
|
2014-11-07 22:16:40 +00:00
|
|
|
setToolTip(tr("Maintenir ctrl pour un d\351placement libre"));
|
2010-04-25 17:34:46 +00:00
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
#endif
|
2007-10-27 13:18:17 +00:00
|
|
|
connect(this, SIGNAL(lostFocus()), this, SLOT(setNonFocusable()));
|
2007-09-26 22:57:53 +00:00
|
|
|
}
|
|
|
|
|
2010-04-18 17:59:54 +00:00
|
|
|
/**
|
|
|
|
@return le Diagram auquel ce texte appartient, ou 0 si ce texte n'est
|
|
|
|
rattache a aucun schema
|
|
|
|
*/
|
2007-09-26 22:57:53 +00:00
|
|
|
Diagram *DiagramTextItem::diagram() const {
|
2010-05-02 22:21:53 +00:00
|
|
|
return(qobject_cast<Diagram *>(scene()));
|
2007-09-26 22:57:53 +00:00
|
|
|
}
|
|
|
|
|
2009-11-29 21:56:48 +00:00
|
|
|
/**
|
|
|
|
@return l'angle de rotation actuel de ce texte
|
|
|
|
*/
|
|
|
|
qreal DiagramTextItem::rotationAngle() const {
|
|
|
|
return(rotation_angle_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-13 22:12:52 +00:00
|
|
|
Permet de tourner le texte a un angle donne de maniere absolue.
|
|
|
|
Un angle de 0 degres correspond a un texte horizontal non retourne.
|
2009-11-29 21:56:48 +00:00
|
|
|
@param rotation Nouvel angle de rotation de ce texte
|
2009-12-13 22:12:52 +00:00
|
|
|
@see applyRotation
|
2009-11-29 21:56:48 +00:00
|
|
|
*/
|
|
|
|
void DiagramTextItem::setRotationAngle(const qreal &rotation) {
|
2009-12-13 22:12:52 +00:00
|
|
|
qreal applied_rotation = QET::correctAngle(rotation);
|
|
|
|
applyRotation(applied_rotation - rotation_angle_);
|
2009-11-29 21:56:48 +00:00
|
|
|
rotation_angle_ = applied_rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-13 22:12:52 +00:00
|
|
|
Permet de tourner le texte de maniere relative.
|
|
|
|
L'angle added_rotation est ajoute a l'orientation actuelle du texte.
|
2009-11-29 21:56:48 +00:00
|
|
|
@param added_rotation Angle a ajouter a la rotation actuelle
|
2009-12-13 22:12:52 +00:00
|
|
|
@see applyRotation
|
2009-11-29 21:56:48 +00:00
|
|
|
*/
|
|
|
|
void DiagramTextItem::rotateBy(const qreal &added_rotation) {
|
2009-12-13 22:12:52 +00:00
|
|
|
qreal applied_added_rotation = QET::correctAngle(added_rotation);
|
|
|
|
rotation_angle_ = QET::correctAngle(rotation_angle_ + applied_added_rotation);
|
|
|
|
applyRotation(applied_added_rotation);
|
2009-11-29 21:56:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 21:21:52 +00:00
|
|
|
/**
|
|
|
|
Traduit en coordonnees de la scene un mouvement / vecteur initialement
|
|
|
|
exprime en coordonnees locales.
|
|
|
|
@param movement Vecteur exprime en coordonnees locales
|
|
|
|
@return le meme vecteur, exprime en coordonnees de la scene
|
|
|
|
*/
|
|
|
|
QPointF DiagramTextItem::mapMovementToScene(const QPointF &movement) const {
|
|
|
|
// on definit deux points en coordonnees locales
|
|
|
|
QPointF local_origin(0.0, 0.0);
|
|
|
|
QPointF local_movement_point(movement);
|
|
|
|
|
|
|
|
// on les mappe sur la scene
|
|
|
|
QPointF scene_origin(mapToScene(local_origin));
|
|
|
|
QPointF scene_movement_point(mapToScene(local_movement_point));
|
|
|
|
|
|
|
|
// on calcule le vecteur represente par ces deux points
|
|
|
|
return(scene_movement_point - scene_origin);
|
|
|
|
}
|
|
|
|
|
2010-04-24 20:42:20 +00:00
|
|
|
/**
|
|
|
|
Traduit en coordonnees locales un mouvement / vecteur initialement
|
|
|
|
exprime en coordonnees de la scene.
|
|
|
|
@param movement Vecteur exprime en coordonnees de la scene
|
|
|
|
@return le meme vecteur, exprime en coordonnees locales
|
|
|
|
*/
|
|
|
|
QPointF DiagramTextItem::mapMovementFromScene(const QPointF &movement) const {
|
|
|
|
// on definit deux points sur la scene
|
|
|
|
QPointF scene_origin(0.0, 0.0);
|
|
|
|
QPointF scene_movement_point(movement);
|
2014-10-25 21:21:52 +00:00
|
|
|
|
2010-04-24 20:42:20 +00:00
|
|
|
// on les mappe sur ce QGraphicsItem
|
|
|
|
QPointF local_origin(mapFromScene(scene_origin));
|
|
|
|
QPointF local_movement_point(mapFromScene(scene_movement_point));
|
2014-10-25 21:21:52 +00:00
|
|
|
|
2010-04-24 20:42:20 +00:00
|
|
|
// on calcule le vecteur represente par ces deux points
|
|
|
|
return(local_movement_point - local_origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Traduit en coordonnees de l'item parent un mouvement / vecteur initialement
|
|
|
|
exprime en coordonnees locales.
|
|
|
|
@param movement Vecteur exprime en coordonnees locales
|
|
|
|
@return le meme vecteur, exprime en coordonnees du parent
|
|
|
|
*/
|
|
|
|
QPointF DiagramTextItem::mapMovementToParent(const QPointF &movement) const {
|
|
|
|
// on definit deux points en coordonnees locales
|
|
|
|
QPointF local_origin(0.0, 0.0);
|
|
|
|
QPointF local_movement_point(movement);
|
2014-10-25 21:21:52 +00:00
|
|
|
|
2010-04-24 20:42:20 +00:00
|
|
|
// on les mappe sur la scene
|
|
|
|
QPointF parent_origin(mapToParent(local_origin));
|
|
|
|
QPointF parent_movement_point(mapToParent(local_movement_point));
|
2014-10-25 21:21:52 +00:00
|
|
|
|
2010-04-24 20:42:20 +00:00
|
|
|
// on calcule le vecteur represente par ces deux points
|
|
|
|
return(parent_movement_point - parent_origin);
|
|
|
|
}
|
|
|
|
|
2014-10-25 21:21:52 +00:00
|
|
|
/**
|
|
|
|
Traduit en coordonnees locales un mouvement / vecteur initialement
|
|
|
|
exprime en coordonnees du parent.
|
|
|
|
@param movement Vecteur exprime en coordonnees du parent
|
|
|
|
@return le meme vecteur, exprime en coordonnees locales
|
|
|
|
*/
|
|
|
|
QPointF DiagramTextItem::mapMovementFromParent(const QPointF &movement) const {
|
|
|
|
// on definit deux points sur le parent
|
|
|
|
QPointF parent_origin(0.0, 0.0);
|
|
|
|
QPointF parent_movement_point(movement);
|
|
|
|
|
|
|
|
// on les mappe sur ce QGraphicsItem
|
|
|
|
QPointF local_origin(mapFromParent(parent_origin));
|
|
|
|
QPointF local_movement_point(mapFromParent(parent_movement_point));
|
|
|
|
|
|
|
|
// on calcule le vecteur represente par ces deux points
|
|
|
|
return(local_movement_point - local_origin);
|
|
|
|
}
|
|
|
|
|
2013-12-03 10:17:49 +00:00
|
|
|
void DiagramTextItem::setFontSize(int &s) {
|
|
|
|
setFont(QETApp::diagramTextsFont(s));
|
|
|
|
}
|
|
|
|
|
2010-05-08 17:03:05 +00:00
|
|
|
/**
|
|
|
|
Dessine le champ de texte.
|
|
|
|
Cette methode delegue simplement le travail a QGraphicsTextItem::paint apres
|
|
|
|
avoir desactive l'antialiasing.
|
|
|
|
@param painter Le QPainter a utiliser pour dessiner le champ de texte
|
|
|
|
@param option Les options de style pour le champ de texte
|
|
|
|
@param widget Le QWidget sur lequel on dessine
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
|
|
|
|
painter -> setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
QGraphicsTextItem::paint(painter, option, widget);
|
|
|
|
}
|
|
|
|
|
2010-04-23 22:39:59 +00:00
|
|
|
/**
|
|
|
|
Gere la prise de focus du champ de texte
|
2010-05-02 22:00:53 +00:00
|
|
|
@param e Objet decrivant la prise de focus
|
2010-04-23 22:39:59 +00:00
|
|
|
*/
|
|
|
|
void DiagramTextItem::focusInEvent(QFocusEvent *e) {
|
|
|
|
QGraphicsTextItem::focusInEvent(e);
|
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
// empeche le deplacement du texte pendant son edition
|
|
|
|
setFlag(QGraphicsItem::ItemIsMovable, false);
|
|
|
|
|
2010-04-23 22:39:59 +00:00
|
|
|
// memorise le texte avant que l'utilisateur n'y touche
|
2013-03-22 23:17:39 +00:00
|
|
|
previous_text_ = toHtml();
|
2010-04-23 22:39:59 +00:00
|
|
|
// cela permettra de determiner si l'utilisateur a modifie le texte a la fin de l'edition
|
|
|
|
}
|
|
|
|
|
2010-04-18 17:59:54 +00:00
|
|
|
/**
|
|
|
|
Gere la perte de focus du champ de texte
|
2010-05-02 22:00:53 +00:00
|
|
|
@param e Objet decrivant la perte de focus
|
2007-09-26 22:57:53 +00:00
|
|
|
*/
|
|
|
|
void DiagramTextItem::focusOutEvent(QFocusEvent *e) {
|
|
|
|
QGraphicsTextItem::focusOutEvent(e);
|
2010-04-23 22:39:59 +00:00
|
|
|
|
2008-07-09 21:14:30 +00:00
|
|
|
// signale la modification du texte si besoin
|
2010-04-23 22:39:59 +00:00
|
|
|
if (toPlainText() != previous_text_) {
|
2013-03-22 23:17:39 +00:00
|
|
|
emit(diagramTextChanged(this, previous_text_, toHtml()));
|
|
|
|
previous_text_ = toHtml();
|
2007-09-26 22:57:53 +00:00
|
|
|
}
|
2007-10-14 14:44:33 +00:00
|
|
|
|
|
|
|
// deselectionne le texte
|
|
|
|
QTextCursor cursor = textCursor();
|
|
|
|
cursor.clearSelection();
|
|
|
|
setTextCursor(cursor);
|
2007-10-27 13:18:17 +00:00
|
|
|
|
2009-12-06 23:18:47 +00:00
|
|
|
// hack a la con pour etre re-entrant
|
|
|
|
setTextInteractionFlags(Qt::NoTextInteraction);
|
2010-05-08 21:24:43 +00:00
|
|
|
|
|
|
|
// autorise de nouveau le deplacement du texte
|
|
|
|
setFlag(QGraphicsItem::ItemIsMovable, true);
|
2009-12-06 23:18:47 +00:00
|
|
|
QTimer::singleShot(0, this, SIGNAL(lostFocus()));
|
2007-10-27 13:18:17 +00:00
|
|
|
}
|
|
|
|
|
2007-12-16 13:21:28 +00:00
|
|
|
/**
|
|
|
|
Gere les double-clics sur ce champ de texte.
|
|
|
|
@param event un QGraphicsSceneMouseEvent decrivant le double-clic
|
|
|
|
*/
|
2007-10-27 13:18:17 +00:00
|
|
|
void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
|
2013-12-29 18:09:25 +00:00
|
|
|
if (!(textInteractionFlags() & Qt::TextEditable) && !no_editable) {
|
2007-10-27 13:18:17 +00:00
|
|
|
// rend le champ de texte editable
|
|
|
|
setTextInteractionFlags(Qt::TextEditorInteraction);
|
|
|
|
|
2009-12-06 23:18:47 +00:00
|
|
|
// edite le champ de texte
|
|
|
|
setFocus(Qt::MouseFocusReason);
|
2007-10-27 13:18:17 +00:00
|
|
|
} else {
|
|
|
|
QGraphicsTextItem::mouseDoubleClickEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 21:30:42 +00:00
|
|
|
/**
|
|
|
|
* @brief DiagramTextItem::mousePressEvent
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::mousePressEvent (QGraphicsSceneMouseEvent *event) {
|
|
|
|
m_first_move = true;
|
|
|
|
if (event -> modifiers() & Qt::ControlModifier) {
|
|
|
|
setSelected(!isSelected());
|
|
|
|
}
|
2014-10-18 12:25:16 +00:00
|
|
|
//Save the pos of item at the beggining of the movement
|
|
|
|
m_mouse_to_origin_movement = pos() - event->scenePos();
|
2014-10-17 21:30:42 +00:00
|
|
|
QGraphicsTextItem::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DiagramTextItem::mouseMoveEvent
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
if (textInteractionFlags() & Qt::TextEditable) QGraphicsTextItem::mouseMoveEvent(event);
|
|
|
|
|
|
|
|
else if ((flags() & QGraphicsItem::ItemIsMovable) && (event -> buttons() & Qt::LeftButton)) {
|
|
|
|
Diagram *diagram_ = diagram();
|
|
|
|
|
|
|
|
//This is first move, we signal it to parent diagram
|
|
|
|
if(diagram_ && m_first_move)
|
|
|
|
diagram_ -> beginMoveElements(this);
|
|
|
|
|
|
|
|
QPointF old_pos = pos();
|
|
|
|
|
|
|
|
//Set the actual pos
|
2014-10-18 12:25:16 +00:00
|
|
|
QPointF new_pos = event->scenePos() + m_mouse_to_origin_movement;
|
|
|
|
event->modifiers() == Qt::ControlModifier ? setPos(new_pos) : setPos(Diagram::snapToGrid(new_pos));
|
|
|
|
|
2014-10-17 21:30:42 +00:00
|
|
|
|
|
|
|
//Update the actual movement for other selected item
|
|
|
|
if (diagram_)
|
|
|
|
diagram_ -> continueMoveElements(pos() - old_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
else event -> ignore();
|
|
|
|
|
|
|
|
m_first_move = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DiagramTextItem::mouseReleaseEvent
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::mouseReleaseEvent (QGraphicsSceneMouseEvent *event) {
|
|
|
|
//Signal to diagram movement is finish
|
|
|
|
if (diagram())
|
|
|
|
diagram() -> endMoveElements();
|
|
|
|
|
|
|
|
if (!(event -> modifiers() & Qt::ControlModifier))
|
|
|
|
QGraphicsTextItem::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-12-13 22:12:52 +00:00
|
|
|
/**
|
2010-07-18 01:46:30 +00:00
|
|
|
Effectue la rotation du texte en elle-meme
|
2009-12-13 22:12:52 +00:00
|
|
|
Pour les DiagramTextItem, la rotation s'effectue autour du point (0, 0).
|
2010-07-18 01:46:30 +00:00
|
|
|
Cette methode peut toutefois etre redefinie dans des classes filles
|
2009-12-13 22:12:52 +00:00
|
|
|
@param angle Angle de la rotation a effectuer
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::applyRotation(const qreal &angle) {
|
2013-09-09 13:29:28 +00:00
|
|
|
setRotation(QET::correctAngle(rotation()+angle));
|
2009-12-13 22:12:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-27 13:18:17 +00:00
|
|
|
/// Rend le champ de texte non focusable
|
|
|
|
void DiagramTextItem::setNonFocusable() {
|
|
|
|
setFlag(QGraphicsTextItem::ItemIsFocusable, false);
|
2007-09-26 22:57:53 +00:00
|
|
|
}
|
2013-04-01 13:49:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief DiagramTextItem::setHtmlText
|
|
|
|
* @param txt
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::setHtmlText(const QString &txt) {
|
|
|
|
setHtml( txt );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Edit the text with HtmlEditor
|
|
|
|
*/
|
|
|
|
void DiagramTextItem::edit() {
|
|
|
|
//Open the HtmlEditor
|
2013-04-09 11:26:53 +00:00
|
|
|
qdesigner_internal::RichTextEditorDialog *editor = new qdesigner_internal::RichTextEditorDialog();
|
2013-04-01 13:49:34 +00:00
|
|
|
// connect the in/out
|
|
|
|
connect(editor, SIGNAL(applyEditText(const QString &)), this, SLOT(setHtmlText(const QString &)));
|
|
|
|
// load the Html txt
|
2013-04-09 11:26:53 +00:00
|
|
|
editor->setText( toHtml() );
|
2013-04-01 13:49:34 +00:00
|
|
|
// show
|
|
|
|
editor->show();
|
|
|
|
}
|
|
|
|
|