2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2010-01-03 16:25:37 +00:00
|
|
|
Copyright 2006-2010 Xavier Guerrin
|
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-08-18 04:42:39 +00:00
|
|
|
#include "parttextfield.h"
|
|
|
|
#include "textfieldeditor.h"
|
2008-01-29 21:22:13 +00:00
|
|
|
#include "editorcommands.h"
|
2007-12-21 18:20:18 +00:00
|
|
|
#include "qetapp.h"
|
2007-08-18 04:42:39 +00:00
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param editor L'editeur d'element concerne
|
|
|
|
@param parent Le QGraphicsItem parent de ce champ de texte
|
|
|
|
@param scene La scene sur laquelle figure ce champ de texte
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
PartTextField::PartTextField(QETElementEditor *editor, QGraphicsItem *parent, QGraphicsScene *scene) :
|
|
|
|
QGraphicsTextItem(parent, scene),
|
|
|
|
CustomElementPart(editor),
|
2009-12-20 01:59:44 +00:00
|
|
|
follow_parent_rotations(true),
|
|
|
|
rotation_angle_(0.0)
|
2007-08-25 03:43:05 +00:00
|
|
|
{
|
2007-11-01 23:56:19 +00:00
|
|
|
setDefaultTextColor(Qt::black);
|
2009-06-26 19:59:49 +00:00
|
|
|
setFont(QETApp::diagramTextsFont());
|
2007-08-18 04:42:39 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
2010-01-20 00:16:55 +00:00
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
#endif
|
2009-04-03 19:30:25 +00:00
|
|
|
setPlainText(QObject::tr("_", "default text when adding a textfield in the element editor"));
|
2010-01-01 14:41:15 +00:00
|
|
|
|
|
|
|
// ajuste la position du champ de texte lorsqu'on lui ajoute/retire des lignes
|
|
|
|
connect(document(), SIGNAL(blockCountChanged(int)), this, SLOT(adjustItemPosition(int)));
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/// Destructeur
|
2007-08-18 04:42:39 +00:00
|
|
|
PartTextField::~PartTextField() {
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Importe les proprietes d'un champ de texte depuis un element XML
|
|
|
|
@param xml_element Element XML a lire
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
void PartTextField::fromXml(const QDomElement &xml_element) {
|
|
|
|
bool ok;
|
|
|
|
int font_size = xml_element.attribute("size").toInt(&ok);
|
|
|
|
if (!ok || font_size < 1) font_size = 20;
|
|
|
|
|
2009-04-12 17:21:19 +00:00
|
|
|
setFont(QETApp::diagramTextsFont(font_size));
|
2007-08-18 04:42:39 +00:00
|
|
|
setPlainText(xml_element.attribute("text"));
|
2009-12-20 01:59:44 +00:00
|
|
|
|
|
|
|
qreal default_rotation_angle = 0.0;
|
|
|
|
if (QET::attributeIsAReal(xml_element, "rotation", &default_rotation_angle)) {
|
|
|
|
setRotationAngle(default_rotation_angle);
|
|
|
|
}
|
|
|
|
|
2007-08-18 04:42:39 +00:00
|
|
|
setPos(
|
|
|
|
xml_element.attribute("x").toDouble(),
|
|
|
|
xml_element.attribute("y").toDouble()
|
|
|
|
);
|
2010-01-01 14:41:15 +00:00
|
|
|
known_position_ = pos();
|
2007-08-18 04:42:39 +00:00
|
|
|
|
2007-08-25 03:43:05 +00:00
|
|
|
follow_parent_rotations = (xml_element.attribute("rotate") == "true");
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Exporte le champ de texte en XML
|
|
|
|
@param xml_document Document XML a utiliser pour creer l'element XML
|
|
|
|
@return un element XML decrivant le champ de texte
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
const QDomElement PartTextField::toXml(QDomDocument &xml_document) const {
|
|
|
|
QDomElement xml_element = xml_document.createElement("input");
|
2009-08-25 23:31:31 +00:00
|
|
|
xml_element.setAttribute("x", QString("%1").arg(pos().x()));
|
|
|
|
xml_element.setAttribute("y", QString("%1").arg(pos().y()));
|
2007-08-18 04:42:39 +00:00
|
|
|
xml_element.setAttribute("text", toPlainText());
|
|
|
|
xml_element.setAttribute("size", font().pointSize());
|
2009-12-20 01:59:44 +00:00
|
|
|
// angle de rotation du champ de texte
|
|
|
|
if (rotationAngle()) {
|
|
|
|
xml_element.setAttribute("rotation", QString("%1").arg(rotationAngle()));
|
|
|
|
}
|
|
|
|
// suivi (ou non) des rotations de l'element parent par le champ de texte
|
|
|
|
if (follow_parent_rotations) {
|
|
|
|
xml_element.setAttribute("rotate", "true");
|
|
|
|
}
|
2007-08-18 04:42:39 +00:00
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-25 23:31:31 +00:00
|
|
|
Retourne la position du texte, l'origine etant le milieu du bord gauche du
|
|
|
|
champ
|
2007-08-18 04:42:39 +00:00
|
|
|
@return la position du texte
|
|
|
|
*/
|
|
|
|
QPointF PartTextField::pos() const {
|
2010-01-01 14:41:15 +00:00
|
|
|
return(mapToScene(margin()));
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Specifie la position du champ de texte
|
2009-12-27 22:46:35 +00:00
|
|
|
@param new_pos Nouvelle position
|
2007-12-05 21:16:01 +00:00
|
|
|
*/
|
2009-12-27 22:46:35 +00:00
|
|
|
void PartTextField::setPos(const QPointF &new_pos) {
|
2010-01-01 14:41:15 +00:00
|
|
|
// annule toute transformation (rotation notamment)
|
|
|
|
resetTransform();
|
|
|
|
|
|
|
|
// effectue le positionnement en lui-meme
|
|
|
|
QPointF m = margin();
|
|
|
|
QGraphicsTextItem::setPos(new_pos - m);
|
|
|
|
|
|
|
|
// applique a nouveau la rotation du champ de texte
|
|
|
|
setTransform(QTransform().translate(m.x(), m.y()).rotate(rotation_angle_).translate(-m.x(), -m.y()));
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Specifie la position du champ de texte
|
|
|
|
@param x abscisse de la nouvelle position
|
|
|
|
@param y ordonnee de la nouvelle position
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
void PartTextField::setPos(qreal x, qreal y) {
|
2010-01-01 14:41:15 +00:00
|
|
|
PartTextField::setPos(QPointF(x, y));
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-20 01:59:44 +00:00
|
|
|
/**
|
|
|
|
@return l'angle de rotation de ce champ de texte
|
|
|
|
*/
|
|
|
|
qreal PartTextField::rotationAngle() const {
|
|
|
|
return(rotation_angle_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param angle Le nouvel angle de rotation de ce champ de texte
|
|
|
|
*/
|
|
|
|
void PartTextField::setRotationAngle(const qreal &angle) {
|
2010-01-01 14:41:15 +00:00
|
|
|
qreal applied_rotation = QET::correctAngle(angle);
|
2009-12-20 01:59:44 +00:00
|
|
|
|
2010-01-01 14:41:15 +00:00
|
|
|
QPointF t = margin();
|
|
|
|
translate(t.x(), t.y());
|
|
|
|
rotate(applied_rotation - rotation_angle_);
|
|
|
|
rotation_angle_ = applied_rotation;
|
|
|
|
translate(-t.x(), -t.y());
|
2009-12-20 01:59:44 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return true si le champ de texte suit les rotation de l'element, false
|
|
|
|
sinon
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
bool PartTextField::followParentRotations() {
|
|
|
|
return(follow_parent_rotations);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@param fpr true pour que le champ de texte suive les rotation de
|
|
|
|
l'element, false sinon
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
void PartTextField::setFollowParentRotations(bool fpr) {
|
|
|
|
follow_parent_rotations = fpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-27 22:46:35 +00:00
|
|
|
@return le decalage entre l'origine du QGraphicsItem et l'origine du champ de
|
|
|
|
texte.
|
2007-08-18 04:42:39 +00:00
|
|
|
*/
|
|
|
|
QPointF PartTextField::margin() const {
|
2009-12-27 22:46:35 +00:00
|
|
|
return(QPointF(0.0, boundingRect().bottom() / 2.0));
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Permet a l'element texte de redevenir deplacable a la fin de l'edition de texte
|
|
|
|
@param e Le QFocusEvent decrivant la perte de focus
|
|
|
|
*/
|
|
|
|
void PartTextField::focusOutEvent(QFocusEvent *e) {
|
|
|
|
QGraphicsTextItem::focusOutEvent(e);
|
2008-01-29 21:22:13 +00:00
|
|
|
if (previous_text != toPlainText()) {
|
|
|
|
undoStack().push(
|
|
|
|
new ChangePartCommand(
|
2009-04-03 19:30:25 +00:00
|
|
|
TextFieldEditor::tr("contenu") + " " + name(),
|
2008-01-29 21:22:13 +00:00
|
|
|
this,
|
|
|
|
"text",
|
|
|
|
previous_text,
|
|
|
|
toPlainText()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
previous_text = toPlainText();
|
|
|
|
}
|
2009-04-03 19:30:25 +00:00
|
|
|
|
|
|
|
// deselectionne le texte
|
|
|
|
QTextCursor qtc = textCursor();
|
|
|
|
qtc.clearSelection();
|
|
|
|
setTextCursor(qtc);
|
|
|
|
|
|
|
|
setTextInteractionFlags(Qt::NoTextInteraction);
|
2010-01-20 00:16:55 +00:00
|
|
|
setFlag(QGraphicsItem::ItemIsFocusable, false);
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Permet a l'element texte de devenir editable lorsqu'on double-clique dessus
|
|
|
|
@param e Le QGraphicsSceneMouseEvent qui decrit le double-clic
|
|
|
|
*/
|
|
|
|
void PartTextField::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e) {
|
2010-01-20 00:16:55 +00:00
|
|
|
setFlag(QGraphicsItem::ItemIsFocusable, true);
|
2007-08-18 04:42:39 +00:00
|
|
|
setTextInteractionFlags(Qt::TextEditorInteraction);
|
2008-01-29 21:22:13 +00:00
|
|
|
previous_text = toPlainText();
|
2007-08-18 04:42:39 +00:00
|
|
|
QGraphicsTextItem::mouseDoubleClickEvent(e);
|
|
|
|
setFocus(Qt::MouseFocusReason);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Specifie la valeur d'une propriete donnee du champ de texte
|
|
|
|
@param property propriete a modifier. Valeurs acceptees :
|
|
|
|
* x : abscisse de la position
|
|
|
|
* y : ordonnee de la position
|
|
|
|
* size : taille du texte
|
|
|
|
* text : texte
|
|
|
|
* rotate : suivi de la rotation de l'element parent
|
|
|
|
@param value Valeur a attribuer a la propriete
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
void PartTextField::setProperty(const QString &property, const QVariant &value) {
|
|
|
|
if (property == "x") {
|
|
|
|
if (!value.canConvert(QVariant::Double)) return;
|
|
|
|
setPos(value.toDouble(), pos().y());
|
|
|
|
} else if (property == "y") {
|
|
|
|
if (!value.canConvert(QVariant::Double)) return;
|
|
|
|
setPos(pos().x(), value.toDouble());
|
|
|
|
} else if (property == "size") {
|
|
|
|
if (!value.canConvert(QVariant::Int)) return;
|
2009-04-12 17:21:19 +00:00
|
|
|
setFont(QETApp::diagramTextsFont(value.toInt()));
|
2010-01-17 16:09:46 +00:00
|
|
|
adjustItemPosition(0);
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "text") {
|
|
|
|
setPlainText(value.toString());
|
2009-12-20 01:59:44 +00:00
|
|
|
} else if (property == "rotation angle") {
|
|
|
|
setRotationAngle(value.toDouble());
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "rotate") {
|
|
|
|
follow_parent_rotations = value.toBool();
|
|
|
|
}
|
2009-04-03 19:30:25 +00:00
|
|
|
update();
|
2007-08-25 03:43:05 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Permet d'acceder a la valeur d'une propriete donnee du champ de texte
|
|
|
|
@param property propriete lue. Valeurs acceptees :
|
|
|
|
* x : abscisse de la position
|
|
|
|
* y : ordonnee de la position
|
|
|
|
* size : taille du texte
|
|
|
|
* text : texte
|
|
|
|
* rotate : suivi de la rotation de l'element parent
|
|
|
|
@return La valeur de la propriete property
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
QVariant PartTextField::property(const QString &property) {
|
|
|
|
if (property == "x") {
|
2009-12-27 22:46:35 +00:00
|
|
|
return(pos().x());
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "y") {
|
2009-12-27 22:46:35 +00:00
|
|
|
return(pos().y());
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "size") {
|
|
|
|
return(font().pointSize());
|
|
|
|
} else if (property == "text") {
|
|
|
|
return(toPlainText());
|
2009-12-20 01:59:44 +00:00
|
|
|
} else if (property == "rotation angle") {
|
|
|
|
return(rotation_angle_);
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "rotate") {
|
|
|
|
return(follow_parent_rotations);
|
|
|
|
}
|
|
|
|
return(QVariant());
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Gere les changements intervenant sur cette partie
|
|
|
|
@param change Type de changement
|
|
|
|
@param value Valeur numerique relative au changement
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
QVariant PartTextField::itemChange(GraphicsItemChange change, const QVariant &value) {
|
2010-01-01 21:31:57 +00:00
|
|
|
if (change == QGraphicsItem::ItemPositionHasChanged || change == QGraphicsItem::ItemSceneHasChanged) {
|
2010-01-01 20:04:15 +00:00
|
|
|
// memorise la nouvelle position "officielle" du champ de texte
|
|
|
|
// cette information servira a le recentrer en cas d'ajout / retrait de lignes
|
|
|
|
known_position_ = pos();
|
2010-02-18 00:42:41 +00:00
|
|
|
updateCurrentPartEditor();
|
2010-01-01 21:31:57 +00:00
|
|
|
} else if (change == QGraphicsItem::ItemSelectedHasChanged) {
|
2010-01-01 20:04:15 +00:00
|
|
|
if (value.toBool() == true) {
|
2010-02-18 00:42:41 +00:00
|
|
|
updateCurrentPartEditor();
|
2007-08-18 04:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(QGraphicsTextItem::itemChange(change, value));
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return le rectangle delimitant cette partie.
|
|
|
|
*/
|
2007-08-18 04:42:39 +00:00
|
|
|
QRectF PartTextField::boundingRect() const {
|
|
|
|
QRectF r = QGraphicsTextItem::boundingRect();
|
2009-08-25 23:31:31 +00:00
|
|
|
r.adjust(0.0, -1.1, 0.0, 0.0);
|
2007-08-18 04:42:39 +00:00
|
|
|
return(r);
|
|
|
|
}
|
2007-12-15 21:57:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
@return true si cette partie n'est pas pertinente et ne merite pas d'etre
|
|
|
|
conservee / enregistree.
|
|
|
|
Un champ de texte est toujours pertinent ; cette fonction renvoie donc
|
|
|
|
toujours false
|
|
|
|
*/
|
|
|
|
bool PartTextField::isUseless() const {
|
|
|
|
return(false);
|
|
|
|
}
|
2009-08-25 23:25:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Dessine le texte statique.
|
|
|
|
@param painter QPainter a utiliser pour effectuer le rendu
|
|
|
|
@param qsogi Pptions de dessin
|
|
|
|
@param widget Widget sur lequel on dessine (facultatif)
|
|
|
|
*/
|
2010-05-02 22:00:53 +00:00
|
|
|
void PartTextField::paint(QPainter *painter, const QStyleOptionGraphicsItem *qsogi, QWidget *widget) {
|
|
|
|
QGraphicsTextItem::paint(painter, qsogi, widget);
|
2009-08-25 23:25:28 +00:00
|
|
|
|
|
|
|
#ifdef QET_DEBUG_EDITOR_TEXTS
|
|
|
|
painter -> setPen(Qt::blue);
|
|
|
|
painter -> drawRect(boundingRect());
|
|
|
|
|
|
|
|
painter -> setPen(Qt::red);
|
|
|
|
drawPoint(painter, QPointF(0, 0));
|
|
|
|
|
2009-12-27 22:46:35 +00:00
|
|
|
painter -> setPen(QColor("#800000"));
|
2009-08-25 23:25:28 +00:00
|
|
|
drawPoint(painter, mapFromScene(pos()));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-01 14:41:15 +00:00
|
|
|
/**
|
|
|
|
Cette methode s'assure que la position du champ de texte est coherente
|
|
|
|
en repositionnant son origine (c-a-d le milieu du bord gauche du champ de
|
|
|
|
texte) a la position originale. Cela est notamment utile lorsque le champ
|
|
|
|
de texte est agrandi ou retreci verticalement (ajout ou retrait de lignes).
|
2010-01-17 16:09:46 +00:00
|
|
|
@param new_block_count Nombre de blocs dans le PartTextField
|
2010-01-01 14:41:15 +00:00
|
|
|
*/
|
|
|
|
void PartTextField::adjustItemPosition(int new_block_count) {
|
|
|
|
Q_UNUSED(new_block_count);
|
|
|
|
setPos(known_position_);
|
|
|
|
}
|
|
|
|
|
2009-08-25 23:25:28 +00:00
|
|
|
#ifdef QET_DEBUG_EDITOR_TEXTS
|
|
|
|
/**
|
|
|
|
Dessine deux petites fleches pour mettre un point en valeur
|
|
|
|
@param painter QPainter a utiliser pour effectuer le rendu
|
|
|
|
@param point Point a dessiner
|
|
|
|
*/
|
|
|
|
void PartTextField::drawPoint(QPainter *painter, const QPointF &point) {
|
|
|
|
qreal px = point.x();
|
|
|
|
qreal py = point.y();
|
|
|
|
qreal size_1 = 5.0;
|
|
|
|
qreal size_2 = 1.0;
|
|
|
|
painter -> drawLine(px, py, px + size_1, py);
|
|
|
|
painter -> drawLine(px + size_1 - size_2, py - size_2, px + size_1, py);
|
|
|
|
painter -> drawLine(px + size_1 - size_2, py + size_2, px + size_1, py);
|
|
|
|
painter -> drawLine(px, py, px, py + size_1);
|
|
|
|
painter -> drawLine(px, py + size_1, px - size_2, py + size_1 - size_2);
|
|
|
|
painter -> drawLine(px, py + size_1, px + size_2, py + size_1 - size_2);
|
|
|
|
}
|
|
|
|
#endif
|