2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2012-01-01 22:51:51 +00:00
|
|
|
Copyright 2006-2012 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-06-30 17:41:07 +00:00
|
|
|
#include "parttext.h"
|
|
|
|
#include "texteditor.h"
|
2008-01-29 21:22:13 +00:00
|
|
|
#include "editorcommands.h"
|
2013-02-08 22:05:15 +00:00
|
|
|
#include "elementprimitivedecorator.h"
|
2007-08-25 03:43:05 +00:00
|
|
|
#include "elementscene.h"
|
2007-12-21 18:20:18 +00:00
|
|
|
#include "qetapp.h"
|
2007-12-05 21:16:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param editor L'editeur d'element concerne
|
|
|
|
@param parent Le QGraphicsItem parent de ce texte statique
|
|
|
|
@param scene La scene sur laquelle figure ce texte statique
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
PartText::PartText(QETElementEditor *editor, QGraphicsItem *parent, ElementScene *scene) :
|
|
|
|
QGraphicsTextItem(parent, scene),
|
2013-02-08 22:05:15 +00:00
|
|
|
CustomElementPart(editor),
|
|
|
|
previous_text(),
|
|
|
|
decorator_(0)
|
2007-08-25 03:43:05 +00:00
|
|
|
{
|
2009-08-31 21:26:08 +00:00
|
|
|
#if QT_VERSION >= 0x040500
|
2009-08-25 23:31:31 +00:00
|
|
|
document() -> setDocumentMargin(1.0);
|
2009-08-31 21:26:08 +00:00
|
|
|
#endif
|
2007-11-01 23:56:19 +00:00
|
|
|
setDefaultTextColor(Qt::black);
|
2009-06-26 19:59:49 +00:00
|
|
|
setFont(QETApp::diagramTextsFont());
|
2013-02-08 22:05:15 +00:00
|
|
|
real_font_size_ = font().pointSize();
|
|
|
|
setFlags(QGraphicsItem::ItemIsSelectable);
|
2010-01-20 00:16:55 +00:00
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
#endif
|
2013-02-08 22:05:15 +00:00
|
|
|
setAcceptHoverEvents(true);
|
2012-03-31 22:48:40 +00:00
|
|
|
setDefaultTextColor(Qt::black);
|
2009-04-03 19:30:25 +00:00
|
|
|
setPlainText(QObject::tr("T", "default text when adding a text in the element editor"));
|
2010-07-25 15:29:58 +00:00
|
|
|
|
|
|
|
adjustItemPosition(1);
|
2012-05-27 15:18:21 +00:00
|
|
|
// adjust textfield position after line additions/deletions
|
2010-07-25 15:29:58 +00:00
|
|
|
connect(document(), SIGNAL(blockCountChanged(int)), this, SLOT(adjustItemPosition(int)));
|
|
|
|
connect(document(), SIGNAL(contentsChanged()), this, SLOT(adjustItemPosition()));
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/// Destructeur
|
2007-06-30 17:41:07 +00:00
|
|
|
PartText::~PartText() {
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Importe les proprietes d'un texte statique depuis un element XML
|
|
|
|
@param xml_element Element XML a lire
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
void PartText::fromXml(const QDomElement &xml_element) {
|
|
|
|
bool ok;
|
|
|
|
int font_size = xml_element.attribute("size").toInt(&ok);
|
|
|
|
if (!ok || font_size < 1) font_size = 20;
|
|
|
|
|
2012-03-31 22:48:40 +00:00
|
|
|
setBlack(xml_element.attribute("color") != "white");
|
2013-02-08 22:05:15 +00:00
|
|
|
setProperty("size" , font_size);
|
2007-06-30 17:41:07 +00:00
|
|
|
setPlainText(xml_element.attribute("text"));
|
2010-07-25 15:29:58 +00:00
|
|
|
|
|
|
|
qreal default_rotation_angle = 0.0;
|
|
|
|
if (QET::attributeIsAReal(xml_element, "rotation", &default_rotation_angle)) {
|
|
|
|
setRotationAngle(default_rotation_angle);
|
|
|
|
}
|
|
|
|
|
2007-06-30 17:41:07 +00:00
|
|
|
setPos(
|
|
|
|
xml_element.attribute("x").toDouble(),
|
|
|
|
xml_element.attribute("y").toDouble()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Exporte le texte statique en XML
|
|
|
|
@param xml_document Document XML a utiliser pour creer l'element XML
|
|
|
|
@return un element XML decrivant le texte statique
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
const QDomElement PartText::toXml(QDomDocument &xml_document) const {
|
|
|
|
QDomElement xml_element = xml_document.createElement("text");
|
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-06-30 17:41:07 +00:00
|
|
|
xml_element.setAttribute("text", toPlainText());
|
|
|
|
xml_element.setAttribute("size", font().pointSize());
|
2010-07-25 15:29:58 +00:00
|
|
|
// angle de rotation du champ de texte
|
|
|
|
if (rotationAngle()) {
|
|
|
|
xml_element.setAttribute("rotation", QString("%1").arg(rotationAngle()));
|
|
|
|
}
|
2012-03-31 22:48:40 +00:00
|
|
|
if (!isBlack()) {
|
|
|
|
xml_element.setAttribute("color", "white");
|
|
|
|
}
|
2007-06-30 17:41:07 +00:00
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-25 15:29:58 +00:00
|
|
|
@return l'angle de rotation de ce champ de texte
|
2007-06-30 17:41:07 +00:00
|
|
|
*/
|
2010-07-25 15:29:58 +00:00
|
|
|
qreal PartText::rotationAngle() const {
|
|
|
|
return(rotation());
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2010-07-25 15:29:58 +00:00
|
|
|
@param angle Le nouvel angle de rotation de ce champ de texte
|
2007-12-05 21:16:01 +00:00
|
|
|
*/
|
2010-07-25 15:29:58 +00:00
|
|
|
void PartText::setRotationAngle(const qreal &angle) {
|
|
|
|
setRotation(QET::correctAngle(angle));
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 22:48:40 +00:00
|
|
|
/**
|
|
|
|
@return true or false if this static text is rendered black or white,
|
|
|
|
respectively.
|
|
|
|
*/
|
|
|
|
bool PartText::isBlack() const {
|
|
|
|
return(defaultTextColor() == Qt::black);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param color whether this static text should be rendered black (true) or white
|
|
|
|
(false).
|
|
|
|
*/
|
|
|
|
void PartText::setBlack(bool color) {
|
|
|
|
setDefaultTextColor(color ? Qt::black : Qt::white);
|
|
|
|
}
|
|
|
|
|
2007-06-30 17:41:07 +00:00
|
|
|
/**
|
|
|
|
@return Les coordonnees du point situe en bas a gauche du texte.
|
|
|
|
*/
|
|
|
|
QPointF PartText::margin() const {
|
|
|
|
QFont used_font = font();
|
|
|
|
QFontMetrics qfm(used_font);
|
2009-08-31 21:26:08 +00:00
|
|
|
|
|
|
|
// marge du texte
|
|
|
|
#if QT_VERSION >= 0x040500
|
|
|
|
qreal document_margin = document() -> documentMargin();
|
|
|
|
#else
|
|
|
|
// il semblerait qu'avant Qt 4.5, ceci vaille non pas 4.0 mais 2.0
|
|
|
|
qreal document_margin = 2.0;
|
|
|
|
#endif
|
|
|
|
|
2007-06-30 17:41:07 +00:00
|
|
|
QPointF margin(
|
2009-08-25 23:31:31 +00:00
|
|
|
// marge autour du texte
|
2009-08-31 21:26:08 +00:00
|
|
|
document_margin,
|
2009-08-25 23:31:31 +00:00
|
|
|
// marge au-dessus du texte + distance entre le plafond du texte et la baseline
|
2009-08-31 21:26:08 +00:00
|
|
|
document_margin + qfm.ascent()
|
2007-06-30 17:41:07 +00:00
|
|
|
);
|
|
|
|
return(margin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-08 22:05:15 +00:00
|
|
|
@reimp QGraphicsItem::focusInEvent(QFocusEvent *)
|
|
|
|
@param e The QFocusEvent object describing the focus gain.
|
|
|
|
Start text edition when the item gains focus.
|
|
|
|
*/
|
|
|
|
void PartText::focusInEvent(QFocusEvent *e) {
|
|
|
|
startEdition();
|
|
|
|
QGraphicsTextItem::focusInEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@reimp QGraphicsItem::focusOutEvent(QFocusEvent *)
|
|
|
|
@param e The QFocusEvent object describing the focus loss.
|
|
|
|
End text edition when the item loses focus.
|
2007-06-30 17:41:07 +00:00
|
|
|
*/
|
|
|
|
void PartText::focusOutEvent(QFocusEvent *e) {
|
|
|
|
QGraphicsTextItem::focusOutEvent(e);
|
2013-02-08 22:05:15 +00:00
|
|
|
endEdition();
|
2007-06-30 17:41:07 +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 PartText::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e) {
|
|
|
|
QGraphicsTextItem::mouseDoubleClickEvent(e);
|
2013-02-08 22:05:15 +00:00
|
|
|
if (e -> button() == Qt::LeftButton) {
|
|
|
|
setEditable(true);
|
|
|
|
}
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Specifie la valeur d'une propriete donnee du texte statique
|
|
|
|
@param property propriete a modifier. Valeurs acceptees :
|
|
|
|
* x : abscisse de la position
|
|
|
|
* y : ordonnee de la position
|
|
|
|
* size : taille du texte
|
|
|
|
* text : texte
|
2010-07-25 15:29:58 +00:00
|
|
|
* "rotation angle" : amgle de rotation
|
2007-12-05 21:16:01 +00:00
|
|
|
@param value Valeur a attribuer a la propriete
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
void PartText::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()));
|
2013-02-08 22:05:15 +00:00
|
|
|
real_font_size_ = value.toInt();
|
|
|
|
} else if (property == "real_size") {
|
|
|
|
if (!value.canConvert(QVariant::Double)) return;
|
|
|
|
setFont(QETApp::diagramTextsFont(value.toInt()));
|
|
|
|
real_font_size_ = value.toDouble();
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "text") {
|
|
|
|
setPlainText(value.toString());
|
2010-07-25 15:29:58 +00:00
|
|
|
} else if (property == "rotation angle") {
|
|
|
|
setRotationAngle(value.toDouble());
|
2012-03-31 22:48:40 +00:00
|
|
|
} else if (property == "color") {
|
|
|
|
setBlack(value.toBool());
|
2007-08-25 03:43:05 +00:00
|
|
|
}
|
2012-05-27 15:18:21 +00:00
|
|
|
// adjust item position, especially useful when changing text or size
|
|
|
|
adjustItemPosition();
|
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 texte statique
|
|
|
|
@param property propriete lue. Valeurs acceptees :
|
|
|
|
* x : abscisse de la position
|
|
|
|
* y : ordonnee de la position
|
|
|
|
* size : taille du texte
|
|
|
|
* text : texte
|
2010-07-25 15:29:58 +00:00
|
|
|
* "rotation angle" : amgle de rotation
|
2007-12-05 21:16:01 +00:00
|
|
|
@return La valeur de la propriete property
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
QVariant PartText::property(const QString &property) {
|
|
|
|
if (property == "x") {
|
2010-07-25 15:29:58 +00:00
|
|
|
return(pos().x());
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "y") {
|
2010-07-25 15:29:58 +00:00
|
|
|
return(pos().y());
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "size") {
|
|
|
|
return(font().pointSize());
|
2013-02-08 22:05:15 +00:00
|
|
|
} else if (property == "real_size") {
|
|
|
|
return(real_font_size_);
|
2007-08-25 03:43:05 +00:00
|
|
|
} else if (property == "text") {
|
|
|
|
return(toPlainText());
|
2010-07-25 15:29:58 +00:00
|
|
|
} else if (property == "rotation angle") {
|
|
|
|
return(rotation());
|
2012-03-31 22:48:40 +00:00
|
|
|
} else if (property == "color") {
|
|
|
|
return(isBlack());
|
2007-08-25 03:43:05 +00:00
|
|
|
}
|
|
|
|
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-06-30 17:41:07 +00:00
|
|
|
QVariant PartText::itemChange(GraphicsItemChange change, const QVariant &value) {
|
2010-01-17 16:09:46 +00:00
|
|
|
if (change == QGraphicsItem::ItemPositionHasChanged || change == QGraphicsItem::ItemSceneHasChanged) {
|
2010-02-18 00:42:41 +00:00
|
|
|
updateCurrentPartEditor();
|
2010-01-17 16:09:46 +00:00
|
|
|
} else if (change == QGraphicsItem::ItemSelectedHasChanged) {
|
|
|
|
if (value.toBool() == true) {
|
2010-02-18 00:42:41 +00:00
|
|
|
updateCurrentPartEditor();
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(QGraphicsTextItem::itemChange(change, value));
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return le rectangle delimitant cette partie.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QRectF PartText::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-06-30 17:41:07 +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 texte statique n'est pas pertinent lorsque son texte est vide.
|
|
|
|
*/
|
|
|
|
bool PartText::isUseless() const {
|
|
|
|
return(toPlainText().isEmpty());
|
|
|
|
}
|
2009-08-25 23:25:28 +00:00
|
|
|
|
2013-02-08 22:05:12 +00:00
|
|
|
/**
|
|
|
|
Start the user-induced transformation, provided this primitive is contained
|
|
|
|
within the \a rect bounding rectangle.
|
|
|
|
*/
|
|
|
|
void PartText::startUserTransformation(const QRectF &rect) {
|
|
|
|
Q_UNUSED(rect)
|
|
|
|
saved_point_ = pos(); // scene coordinates, no need to mapFromScene()
|
2013-02-08 22:05:15 +00:00
|
|
|
saved_font_size_ = real_font_size_;
|
2013-02-08 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
|
|
|
|
*/
|
|
|
|
void PartText::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
|
|
|
|
// let's try the naive approach
|
|
|
|
QPointF new_pos = mapPoints(initial_selection_rect, new_selection_rect, QList<QPointF>() << saved_point_).first();
|
|
|
|
setPos(new_pos);
|
|
|
|
|
2013-02-08 22:05:15 +00:00
|
|
|
// adjust the font size following the vertical scale factor
|
2013-02-08 22:05:12 +00:00
|
|
|
qreal sy = new_selection_rect.height() / initial_selection_rect.height();
|
2013-02-08 22:05:15 +00:00
|
|
|
qreal new_font_size = saved_font_size_ * sy;
|
|
|
|
setProperty("real_size", qMax(1, qRound(new_font_size)));
|
2013-02-08 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
void PartText::paint(QPainter *painter, const QStyleOptionGraphicsItem *qsogi, QWidget *widget) {
|
2013-02-08 22:05:15 +00:00
|
|
|
// According to the source code of QGraphicsTextItem::paint(), this should
|
|
|
|
// avoid the drawing of the dashed rectangle around the text.
|
|
|
|
QStyleOptionGraphicsItem our_qsogi(*qsogi);
|
|
|
|
our_qsogi.state = QStyle::State_None;
|
|
|
|
|
|
|
|
QGraphicsTextItem::paint(painter, &our_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));
|
|
|
|
|
|
|
|
painter -> setPen(Qt::green);
|
|
|
|
drawPoint(painter, mapFromScene(pos()));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:15 +00:00
|
|
|
/**
|
|
|
|
Handle context menu events.
|
|
|
|
@param event Object describing the context menu event to handle.
|
|
|
|
*/
|
|
|
|
void PartText::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
|
|
|
|
Q_UNUSED(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Handle events generated when the mouse hovers over the decorator.
|
|
|
|
@param event Object describing the hover event.
|
|
|
|
*/
|
|
|
|
void PartText::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
|
|
|
|
// force the cursor when the text is being edited
|
|
|
|
if (hasFocus() && decorator_) {
|
|
|
|
decorator_ -> setCursor(Qt::IBeamCursor);
|
|
|
|
}
|
|
|
|
QGraphicsTextItem::hoverMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@reimp CustomElementPart::setDecorator(ElementPrimitiveDecorator *)
|
|
|
|
Install or remove a sceneEventFilter on the decorator and ensure it will
|
|
|
|
adjust itself while the text is being edited.
|
|
|
|
*/
|
|
|
|
void PartText::setDecorator(ElementPrimitiveDecorator *decorator) {
|
|
|
|
if (decorator) {
|
|
|
|
decorator -> installSceneEventFilter(this);
|
|
|
|
// ensure the decorator will adjust itself when the text area expands or shrinks
|
|
|
|
connect(document(), SIGNAL(contentsChanged()), decorator, SLOT(adjust()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
decorator_ -> removeSceneEventFilter(this);
|
|
|
|
endEdition();
|
|
|
|
}
|
|
|
|
decorator_ = decorator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@reimp QGraphicsItem::sceneEventFilter(QGraphicsItem *, QEvent *).
|
|
|
|
Intercepts events before they reach the watched target, i.e. typically the
|
|
|
|
primitives decorator.
|
|
|
|
This method mainly works with key strokes (F2, escape) and double clicks to
|
|
|
|
begin or end text edition.
|
|
|
|
*/
|
|
|
|
bool PartText::sceneEventFilter(QGraphicsItem *watched, QEvent *event) {
|
|
|
|
if (watched != decorator_) return(false);
|
|
|
|
|
|
|
|
QPointF event_scene_pos = QET::graphicsSceneEventPos(event);
|
|
|
|
if (!event_scene_pos.isNull()) {
|
|
|
|
if (contains(mapFromScene(event_scene_pos))) {
|
|
|
|
if (hasFocus()) {
|
|
|
|
return sceneEvent(event); // manually deliver the event to this item
|
|
|
|
return(true); // prevent this event from being delivered to any item
|
|
|
|
} else {
|
|
|
|
if (event -> type() == QEvent::GraphicsSceneMouseDoubleClick) {
|
|
|
|
mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event -> type() == QEvent::KeyRelease || event -> type() == QEvent::KeyPress) {
|
|
|
|
// Intercept F2 and escape keystrokes to focus in and out
|
|
|
|
QKeyEvent *key_event = static_cast<QKeyEvent *>(event);
|
2013-02-08 22:05:18 +00:00
|
|
|
if (!hasFocus() && key_event -> key() == Qt::Key_F2) {
|
2013-02-08 22:05:15 +00:00
|
|
|
setEditable(true);
|
|
|
|
QTextCursor qtc = textCursor();
|
|
|
|
qtc.setPosition(qMax(0, document()->characterCount() - 1));
|
|
|
|
setTextCursor(qtc);
|
|
|
|
} else if (hasFocus() && key_event -> key() == Qt::Key_Escape) {
|
|
|
|
endEdition();
|
|
|
|
}
|
2013-02-08 22:05:18 +00:00
|
|
|
if (hasFocus()) {
|
|
|
|
sceneEvent(event); // manually deliver the event to this item
|
|
|
|
return(true); // prevent this event from being delivered to any item
|
|
|
|
}
|
2013-02-08 22:05:15 +00:00
|
|
|
}
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
|
|
|
*/
|
|
|
|
bool PartText::singleItemPressEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
|
|
|
Q_UNUSED(decorator)
|
|
|
|
Q_UNUSED(event)
|
|
|
|
return(hasFocus());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
|
|
|
*/
|
|
|
|
bool PartText::singleItemMoveEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
|
|
|
Q_UNUSED(decorator)
|
|
|
|
Q_UNUSED(event)
|
|
|
|
return(hasFocus());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
|
|
|
*/
|
|
|
|
bool PartText::singleItemReleaseEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
|
|
|
Q_UNUSED(decorator)
|
|
|
|
Q_UNUSED(event)
|
|
|
|
return(hasFocus());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
|
|
|
*/
|
|
|
|
bool PartText::singleItemDoubleClickEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
|
|
|
Q_UNUSED(decorator)
|
|
|
|
// calling mouseDoubleClickEvent() will set this text item editable and grab keyboard focus
|
|
|
|
if (event -> button() == Qt::LeftButton) {
|
|
|
|
mouseDoubleClickEvent(event);
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2010-01-17 16:09:46 +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).
|
|
|
|
@param new_block_count Nombre de blocs dans le PartText
|
|
|
|
*/
|
|
|
|
void PartText::adjustItemPosition(int new_block_count) {
|
|
|
|
Q_UNUSED(new_block_count);
|
2010-07-25 15:29:58 +00:00
|
|
|
QPointF origin_offset = margin();
|
|
|
|
|
|
|
|
QTransform base_translation;
|
|
|
|
base_translation.translate(-origin_offset.x(), -origin_offset.y());
|
|
|
|
setTransform(base_translation, false);
|
|
|
|
setTransformOriginPoint(origin_offset);
|
2010-01-17 16:09:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:15 +00:00
|
|
|
/**
|
|
|
|
@param editable Whether this text item should be interactively editable.
|
|
|
|
*/
|
|
|
|
void PartText::setEditable(bool editable) {
|
|
|
|
if (editable) {
|
|
|
|
setFlag(QGraphicsItem::ItemIsFocusable, true);
|
|
|
|
setTextInteractionFlags(Qt::TextEditorInteraction);
|
|
|
|
setFocus(Qt::MouseFocusReason);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setTextInteractionFlags(Qt::NoTextInteraction);
|
|
|
|
setFlag(QGraphicsItem::ItemIsFocusable, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Start text edition by storing the former value of the text.
|
|
|
|
*/
|
|
|
|
void PartText::startEdition() {
|
|
|
|
// !previous_text.isNull() means the text is being edited
|
|
|
|
previous_text = toPlainText();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
End text edition, potentially generating a ChangePartCommand if the text
|
|
|
|
has changed.
|
|
|
|
*/
|
|
|
|
void PartText::endEdition() {
|
|
|
|
if (!previous_text.isNull()) {
|
|
|
|
// the text was being edited
|
|
|
|
QString new_text = toPlainText();
|
|
|
|
if (previous_text != new_text) {
|
|
|
|
// the text was changed
|
|
|
|
ChangePartCommand *text_change = new ChangePartCommand(
|
|
|
|
TextEditor::tr("contenu") + " " + name(),
|
|
|
|
this,
|
|
|
|
"text",
|
|
|
|
previous_text,
|
|
|
|
new_text
|
|
|
|
);
|
|
|
|
previous_text = QString();
|
|
|
|
undoStack().push(text_change);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deselectionne le texte
|
|
|
|
QTextCursor qtc = textCursor();
|
|
|
|
qtc.clearSelection();
|
|
|
|
setTextCursor(qtc);
|
|
|
|
|
|
|
|
setEditable(false);
|
|
|
|
}
|
|
|
|
|
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 PartText::drawPoint(QPainter *painter, const QPointF &point) {
|
|
|
|
qreal px = point.x();
|
|
|
|
qreal py = point.y();
|
|
|
|
qreal size_1 = 5.0;
|
|
|
|
qreal size_2 = 1.0;
|
2010-07-25 14:17:05 +00:00
|
|
|
painter -> drawLine(QLineF(px, py, px + size_1, py));
|
|
|
|
painter -> drawLine(QLineF(px + size_1 - size_2, py - size_2, px + size_1, py));
|
|
|
|
painter -> drawLine(QLineF(px + size_1 - size_2, py + size_2, px + size_1, py));
|
|
|
|
painter -> drawLine(QLineF(px, py, px, py + size_1));
|
|
|
|
painter -> drawLine(QLineF(px, py + size_1, px - size_2, py + size_1 - size_2));
|
|
|
|
painter -> drawLine(QLineF(px, py + size_1, px + size_2, py + size_1 - size_2));
|
2009-08-25 23:25:28 +00:00
|
|
|
}
|
|
|
|
#endif
|