358 lines
10 KiB
C++
Raw Normal View History

/*
2020-06-15 17:42:37 +02:00
Copyright 2006-2020 The QElectroTech Team
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/>.
*/
#include "parttext.h"
#include "texteditor.h"
#include "elementprimitivedecorator.h"
#include "elementscene.h"
#include "qetapp.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
/**
Constructeur
@param editor L'editeur d'element concerne
@param parent Le QGraphicsItem parent de ce texte statique
*/
PartText::PartText(QETElementEditor *editor, QGraphicsItem *parent) :
Merge Qt5 branch sources folder to trunk -Cette ligne, et les suivantes ci-dessous, seront ignorées-- M sources/aboutqet.cpp M sources/bordertitleblock.cpp M sources/conductorproperties.h M sources/configdialog.cpp M sources/configpages.cpp M sources/configpages.h M sources/createdxf.h M sources/diagram.cpp M sources/diagram.h M sources/diagramcommands.cpp M sources/diagramcommands.h M sources/diagramprintdialog.cpp M sources/diagramprintdialog.h M sources/diagramschooser.cpp M sources/diagramschooser.h M sources/diagramview.cpp M sources/diagramview.h M sources/dvevent/dveventaddimage.cpp M sources/dvevent/dveventaddshape.cpp M sources/editor/arceditor.cpp M sources/editor/arceditor.h M sources/editor/editorcommands.cpp M sources/editor/editorcommands.h M sources/editor/elementitemeditor.h M sources/editor/elementprimitivedecorator.cpp M sources/editor/elementscene.cpp M sources/editor/elementscene.h M sources/editor/elementview.cpp M sources/editor/ellipseeditor.cpp M sources/editor/ellipseeditor.h M sources/editor/esevent/eseventaddtext.cpp M sources/editor/esevent/eseventaddtextfield.cpp M sources/editor/esevent/eseventinterface.cpp M sources/editor/graphicspart/customelementpart.h M sources/editor/graphicspart/parttext.cpp M sources/editor/graphicspart/parttext.h M sources/editor/graphicspart/parttextfield.cpp M sources/editor/graphicspart/parttextfield.h M sources/editor/lineeditor.cpp M sources/editor/lineeditor.h M sources/editor/polygoneditor.cpp M sources/editor/qetelementeditor.cpp M sources/editor/qetelementeditor.h M sources/editor/rectangleeditor.cpp M sources/editor/rectangleeditor.h M sources/editor/styleeditor.cpp M sources/editor/styleeditor.h M sources/editor/terminaleditor.cpp M sources/editor/terminaleditor.h M sources/editor/texteditor.cpp M sources/editor/texteditor.h M sources/editor/textfieldeditor.cpp M sources/editor/textfieldeditor.h M sources/editor/ui/elementpropertieseditorwidget.cpp M sources/elementdefinition.cpp M sources/elementdeleter.cpp M sources/elementdeleter.h M sources/elementdialog.cpp M sources/elementscategorieslist.h M sources/elementscategorieswidget.cpp M sources/elementscategorieswidget.h M sources/elementscategory.cpp M sources/elementscategorydeleter.cpp M sources/elementscategorydeleter.h M sources/elementscategoryeditor.cpp M sources/elementscategoryeditor.h M sources/elementscollection.cpp M sources/elementscollectioncache.cpp M sources/elementspanel.cpp M sources/elementspanel.h M sources/elementspanelwidget.cpp M sources/elementspanelwidget.h M sources/elementtextsmover.h M sources/exportdialog.cpp M sources/exportdialog.h M sources/exportproperties.cpp M sources/exportpropertieswidget.cpp M sources/exportpropertieswidget.h M sources/genericpanel.cpp M sources/integrationmoveelementshandler.cpp M sources/integrationmoveelementshandler.h M sources/interactivemoveelementshandler.cpp M sources/nameslistwidget.cpp M sources/nameslistwidget.h M sources/newelementwizard.cpp M sources/newelementwizard.h M sources/nomenclature.cpp M sources/nomenclature.h M sources/projectconfigpages.cpp M sources/projectview.cpp M sources/projectview.h M sources/qet.cpp M sources/qetapp.cpp M sources/qetapp.h M sources/qetdiagrameditor.cpp M sources/qetdiagrameditor.h M sources/qetgraphicsitem/conductor.cpp M sources/qetgraphicsitem/conductortextitem.cpp M sources/qetgraphicsitem/customelement.cpp M sources/qetgraphicsitem/diagramimageitem.cpp M sources/qetgraphicsitem/diagramtextitem.cpp M sources/qetgraphicsitem/diagramtextitem.h M sources/qetgraphicsitem/element.cpp M sources/qetgraphicsitem/ghostelement.cpp M sources/qetgraphicsitem/qetshapeitem.cpp M sources/qetgraphicsitem/terminal.cpp M sources/qetgraphicsitem/terminal.h M sources/qeticons.cpp M sources/qeticons.h M sources/qetmainwindow.cpp M sources/qetmessagebox.cpp M sources/qetmessagebox.h M sources/qetprintpreviewdialog.cpp M sources/qetprintpreviewdialog.h M sources/qetproject.cpp M sources/qetsingleapplication.cpp M sources/qettabbar.h M sources/qfilenameedit.cpp M sources/qtextorientationspinboxwidget.cpp M sources/qtextorientationspinboxwidget.h M sources/qtextorientationwidget.cpp M sources/qtextorientationwidget.h M sources/richtext/richtexteditor.cpp M sources/richtext/richtexteditor_p.h M sources/richtext/ui_addlinkdialog.h M sources/titleblock/dimensionwidget.h M sources/titleblock/gridlayoutanimation.h M sources/titleblock/helpercell.h M sources/titleblock/integrationmovetemplateshandler.cpp M sources/titleblock/integrationmovetemplateshandler.h M sources/titleblock/qettemplateeditor.cpp M sources/titleblock/qettemplateeditor.h M sources/titleblock/templatecellsset.h M sources/titleblock/templatecellwidget.cpp M sources/titleblock/templatecellwidget.h M sources/titleblock/templatecommands.cpp M sources/titleblock/templatedeleter.cpp M sources/titleblock/templatedeleter.h M sources/titleblock/templatelocationchooser.cpp M sources/titleblock/templatelocationchooser.h M sources/titleblock/templatelocationsaver.cpp M sources/titleblock/templatelocationsaver.h M sources/titleblock/templatelogomanager.cpp M sources/titleblock/templatelogomanager.h M sources/titleblock/templateview.cpp M sources/titleblock/templatevisualcell.h M sources/titleblockcell.cpp M sources/titleblocktemplate.cpp M sources/treecoloranimation.h M sources/ui/conductorpropertieswidget.cpp M sources/ui/diagrampropertiesdialog.cpp M sources/ui/diagramselection.cpp M sources/ui/dialogautonum.cpp M sources/ui/dialogwaiting.cpp M sources/ui/elementpropertieswidget.cpp M sources/ui/elementselectorwidget.cpp M sources/ui/linksingleelementwidget.cpp M sources/ui/masterpropertieswidget.cpp M sources/ui/potentialtextsdialog.cpp M sources/ui/projectpropertiesdialog.cpp M sources/ui/selectautonumw.cpp M sources/ui/titleblockpropertieswidget.cpp M sources/ui/xrefpropertieswidget.cpp M sources/undocommand/changeelementinformationcommand.cpp git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3783 bfdf4180-ca20-0410-9c96-a3a8aa849046
2015-03-02 20:14:56 +00:00
QGraphicsTextItem(parent),
CustomElementPart(editor),
2020-08-13 12:01:27 +01:00
previous_text()
{
document() -> setDocumentMargin(1.0);
setDefaultTextColor(Qt::black);
setFont(QETApp::diagramTextsFont());
real_font_size_ = font().pointSize();
2020-08-20 21:58:23 +02:00
setFlags(QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemSendsGeometryChanges
| QGraphicsItem::ItemIsMovable);
setAcceptHoverEvents(true);
setDefaultTextColor(Qt::black);
2020-08-20 21:58:23 +02:00
setPlainText(QObject::tr(
"T",
"default text when adding a text in the element editor"));
2020-08-13 12:01:27 +01:00
adjustItemPosition(1);
// adjust textfield position after line additions/deletions
2020-08-20 21:58:23 +02:00
connect(document(),
SIGNAL(blockCountChanged(int)),
this,
SLOT(adjustItemPosition(int)));
connect(document(),
SIGNAL(contentsChanged()),
this,
SLOT(adjustItemPosition()));
}
/// Destructeur
PartText::~PartText() {
}
/**
Importe les proprietes d'un texte statique depuis un element XML
@param xml_element Element XML a lire
*/
2020-08-13 12:01:27 +01:00
void PartText::fromXml(const QDomElement &xml_element) {
bool ok;
2020-08-13 12:01:27 +01:00
if (xml_element.hasAttribute("size")) {
int font_size = xml_element.attribute("size").toInt(&ok);
if (!ok || font_size < 1) {
font_size = 20;
}
2020-08-13 12:01:27 +01:00
QFont font_ = this -> font();
font_.setPointSize(font_size);
setFont(font_);
}
2020-08-13 12:01:27 +01:00
else if (xml_element.hasAttribute("font")) {
QFont font_;
font_.fromString(xml_element.attribute("font"));
setFont(font_);
}
2020-08-13 12:01:27 +01:00
setDefaultTextColor(QColor(xml_element.attribute("color", "#000000")));
setPlainText(xml_element.attribute("text"));
setPos(xml_element.attribute("x").toDouble(),
2020-08-13 12:01:27 +01:00
xml_element.attribute("y").toDouble());
setRotation(xml_element.attribute("rotation", QString::number(0)).toDouble());
}
/**
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
*/
2020-08-13 12:01:27 +01:00
const QDomElement PartText::toXml(QDomDocument &xml_document) const {
QDomElement xml_element = xml_document.createElement(xmlName());
xml_element.setAttribute("x", QString::number(pos().x()));
xml_element.setAttribute("y", QString::number(pos().y()));
xml_element.setAttribute("text", toPlainText());
xml_element.setAttribute("font", font().toString());
xml_element.setAttribute("rotation", QString::number(rotation()));
xml_element.setAttribute("color", defaultTextColor().name());
return(xml_element);
}
/**
@return Les coordonnees du point situe en bas a gauche du texte.
*/
2020-08-13 12:01:27 +01:00
QPointF PartText::margin() const {
QFont used_font = font();
QFontMetrics qfm(used_font);
qreal document_margin = document() -> documentMargin();
2020-08-13 12:01:27 +01:00
QPointF margin(
// marge autour du texte
document_margin,
// marge au-dessus du texte + distance entre le plafond du texte et la baseline
document_margin + qfm.ascent()
);
return(margin);
}
/**
2020-08-20 21:57:35 +02:00
@brief PartText::focusInEvent
@param e : The QFocusEvent object describing the focus gain.
Start text edition when the item gains focus.
2020-08-20 21:57:35 +02:00
@see QGraphicsItem::focusInEvent(QFocusEvent *)
*/
void PartText::focusInEvent(QFocusEvent *e) {
startEdition();
QGraphicsTextItem::focusInEvent(e);
}
/**
2020-08-20 21:57:35 +02:00
@brief PartText::focusOutEvent
@param e : The QFocusEvent object describing the focus loss.
End text edition when the item loses focus.
2020-08-20 21:57:35 +02:00
@see QGraphicsItem::focusOutEvent(QFocusEvent *)
*/
void PartText::focusOutEvent(QFocusEvent *e) {
QGraphicsTextItem::focusOutEvent(e);
endEdition();
}
/**
2020-08-20 21:57:35 +02:00
@brief PartText::keyPressEvent
Used to handle the escape key when the event is delivered to the field,
not to the decorator.
@param event
@see QGraphicsTextItem::keyPressEvent()
*/
void PartText::keyPressEvent(QKeyEvent *event) {
if (event -> key() == Qt::Key_Escape) {
endEdition();
}
else {
QGraphicsTextItem::keyPressEvent(event);
}
}
/**
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);
if (e -> button() == Qt::LeftButton) {
setEditable(true);
}
}
/**
Gere les changements intervenant sur cette partie
@param change Type de changement
@param value Valeur numerique relative au changement
*/
QVariant PartText::itemChange(GraphicsItemChange change, const QVariant &value) {
2020-08-13 12:01:27 +01:00
if (change == QGraphicsItem::ItemPositionHasChanged ||
change == QGraphicsItem::ItemSceneHasChanged ||
change == QGraphicsItem::ItemSelectedHasChanged) {
updateCurrentPartEditor();
}
return(QGraphicsTextItem::itemChange(change, value));
}
/**
@return le rectangle delimitant cette partie.
*/
QRectF PartText::boundingRect() const {
QRectF r = QGraphicsTextItem::boundingRect();
r.adjust(0.0, -1.1, 0.0, 0.0);
return(r);
}
/**
@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());
}
/**
@return the minimum, margin-less rectangle this part can fit into, in scene
coordinates. It is different from boundingRect() because it is not supposed
to imply any margin, and it is different from shape because it is a regular
rectangle, not a complex shape.
*/
QRectF PartText::sceneGeometricRect() const {
return(sceneBoundingRect());
}
/**
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()
saved_font_size_ = real_font_size_;
}
/**
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);
2020-08-13 12:01:27 +01:00
// adjust the font size following the vertical scale factor
qreal sy = new_selection_rect.height() / initial_selection_rect.height();
qreal new_font_size = saved_font_size_ * sy;
setProperty("real_size", qMax(1, qRound(new_font_size)));
}
2020-08-13 12:01:27 +01:00
void PartText::setDefaultTextColor(const QColor &color) {
if (color != this -> defaultTextColor()) {
QGraphicsTextItem::setDefaultTextColor(color);
emit colorChanged(color);
}
}
2020-08-13 12:01:27 +01:00
void PartText::setPlainText(const QString &text) {
if (text != this -> toPlainText()) {
QGraphicsTextItem::setPlainText(text);
emit plainTextChanged(text);
}
}
2020-08-13 12:01:27 +01:00
void PartText::setFont(const QFont &font) {
if (font != this -> font()) {
QGraphicsTextItem::setFont(font);
emit fontChanged(font);
}
}
2020-08-13 12:01:27 +01:00
void PartText::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if((event -> buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
QPointF pos = event -> scenePos() + (m_origine_pos - event -> buttonDownScenePos(Qt::LeftButton));
event -> modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene() -> snapToGrid(pos));
}
else {
QGraphicsObject::mouseMoveEvent(event);
}
}
2020-08-13 12:01:27 +01:00
void PartText::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if(event -> button() == Qt::LeftButton)
m_origine_pos = this -> pos();
2020-08-13 12:01:27 +01:00
QGraphicsObject::mousePressEvent(event);
}
2020-08-13 12:01:27 +01:00
void PartText::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if((event -> button() & Qt::LeftButton) &&
(flags() & QGraphicsItem::ItemIsMovable) &&
m_origine_pos != pos())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(this, "pos", QVariant(m_origine_pos), QVariant(pos()));
undo -> setText(tr("Déplacer un texte"));
undo -> enableAnimation();
elementScene() -> undoStack().push(undo);
}
QGraphicsObject::mouseReleaseEvent(event);
}
/**
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);
QPointF origin_offset = margin();
2020-08-13 12:01:27 +01:00
QTransform base_translation;
base_translation.translate(-origin_offset.x(), -origin_offset.y());
setTransform(base_translation, false);
setTransformOriginPoint(origin_offset);
}
/**
@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.
*/
2020-08-13 12:01:27 +01:00
void PartText::endEdition() {
if (!previous_text.isNull()) {
// the text was being edited
QString new_text = toPlainText();
2020-08-13 12:01:27 +01:00
if (previous_text != new_text) {
QPropertyUndoCommand *undo = new QPropertyUndoCommand(this, "text", previous_text, new_text);
2020-08-13 12:01:27 +01:00
undo -> setText(tr("Modifier un champ texte"));
undoStack().push(undo);
previous_text = QString();
}
}
// deselectionne le texte
QTextCursor qtc = textCursor();
qtc.clearSelection();
setTextCursor(qtc);
setEditable(false);
}