Element editor :

The font of the dynamic text field can be edited.
The font of the static text field can be edited.
The color of the static text field can be edited. 


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5775 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2019-03-10 17:58:33 +00:00
parent a56dc46c35
commit ad76d438ef
12 changed files with 573 additions and 415 deletions

View File

@ -33,8 +33,8 @@ PartDynamicTextField::PartDynamicTextField(QETElementEditor *editor, QGraphicsIt
setDefaultTextColor(Qt::black); setDefaultTextColor(Qt::black);
setFont(QETApp::dynamicTextsItemFont()); setFont(QETApp::dynamicTextsItemFont());
QSettings settings; QSettings settings;
setRotation(settings.value("dynamic_rotation", 0).toInt()); setRotation(settings.value("diagrameditor/dynamic_text_rotation", 0).toInt());
setTextWidth(settings.value("dynamic_with", -1).toInt()); setTextWidth(settings.value("diagrameditor/dynamic_text_width", -1).toInt());
setText("_"); setText("_");
setTextFrom(DynamicElementTextItem::UserText); setTextFrom(DynamicElementTextItem::UserText);
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsMovable); setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsMovable);
@ -94,8 +94,7 @@ const QDomElement PartDynamicTextField::toXml(QDomDocument &dom_doc) const
root_element.setAttribute("y", QString::number(pos().y())); root_element.setAttribute("y", QString::number(pos().y()));
root_element.setAttribute("z", QString::number(zValue())); root_element.setAttribute("z", QString::number(zValue()));
root_element.setAttribute("rotation", QString::number(QET::correctAngle(rotation()))); root_element.setAttribute("rotation", QString::number(QET::correctAngle(rotation())));
root_element.setAttribute("font_size", font().pointSize()); root_element.setAttribute("font", font().toString());
root_element.setAttribute("font_family", (QETApp::dynamicTextsItemFont().family()));
root_element.setAttribute("uuid", m_uuid.toString()); root_element.setAttribute("uuid", m_uuid.toString());
root_element.setAttribute("frame", m_frame? "true" : "false"); root_element.setAttribute("frame", m_frame? "true" : "false");
root_element.setAttribute("text_width", QString::number(m_text_width)); root_element.setAttribute("text_width", QString::number(m_text_width));
@ -165,7 +164,16 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt)
dom_elmt.attribute("y", QString::number(0)).toDouble()); dom_elmt.attribute("y", QString::number(0)).toDouble());
setZValue(dom_elmt.attribute("z", QString::number(zValue())).toDouble()); setZValue(dom_elmt.attribute("z", QString::number(zValue())).toDouble());
QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble()); QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble());
setFont(QETApp::diagramTextsFont(dom_elmt.attribute("font_size", QString::number(9)).toInt()));
if (dom_elmt.hasAttribute("font"))
{
QFont font_;
font_.fromString(dom_elmt.attribute("font"));
setFont(font_);
} else { //Keep compatibility TODO remove in futur
setFont(QETApp::dynamicTextsItemFont(9));
}
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString())); m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false); setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false);
setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble()); setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble());
@ -352,22 +360,6 @@ QColor PartDynamicTextField::color() const {
return defaultTextColor(); return defaultTextColor();
} }
/**
* @brief PartDynamicTextField::setFontSize
* @param s
*/
void PartDynamicTextField::setFontSize(int s)
{
prepareAlignment();
setFont(QETApp::dynamicTextsItemFont(s));
finishAlignment();
emit fontSizeChanged(s);
}
int PartDynamicTextField::fontSize() const {
return font().pointSize();
}
void PartDynamicTextField::setFrame(bool frame) void PartDynamicTextField::setFrame(bool frame)
{ {
m_frame = frame; m_frame = frame;
@ -425,6 +417,17 @@ Qt::Alignment PartDynamicTextField::alignment() const {
return m_alignment; return m_alignment;
} }
void PartDynamicTextField::setFont(const QFont &font)
{
if (font == this->font()) {
return;
}
prepareAlignment();
QGraphicsTextItem::setFont(font);
finishAlignment();
emit fontChanged(font);
}
/** /**
* @brief PartDynamicTextField::mouseMoveEvent * @brief PartDynamicTextField::mouseMoveEvent
* @param event * @param event
@ -501,13 +504,13 @@ void PartDynamicTextField::paint(QPainter *painter, const QStyleOptionGraphicsIt
if (m_frame) if (m_frame)
{ {
painter->save(); painter->save();
painter->setFont(QETApp::dynamicTextsItemFont(fontSize())); painter->setFont(this->font());
//Adjust the thickness according to the font size, //Adjust the thickness according to the font size,
qreal w=0.3; qreal w=0.3;
if(fontSize() >= 5) if(this->font().pointSize() >= 5)
{ {
w = (qreal)fontSize()*0.1; w = this->font().pointSizeF()*0.1;
if(w > 2.5) if(w > 2.5)
w = 2.5; w = 2.5;
} }
@ -534,7 +537,7 @@ void PartDynamicTextField::paint(QPainter *painter, const QStyleOptionGraphicsIt
pos.ry() -= size.height()/2; pos.ry() -= size.height()/2;
//Adjust the rounding of the rectangle according to the size of the font //Adjust the rounding of the rectangle according to the size of the font
qreal ro = (qreal)fontSize()/3; qreal ro = this->font().pointSizeF()/3;
painter->drawRoundedRect(QRectF(pos, size), ro, ro); painter->drawRoundedRect(QRectF(pos, size), ro, ro);
painter->restore(); painter->restore();

View File

@ -38,10 +38,10 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged) Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged)
Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged) Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged) Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged) Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
public: public:
///PROPERTY ///PROPERTY
@ -55,10 +55,10 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
void infoNameChanged(QString info); void infoNameChanged(QString info);
void compositeTextChanged(QString text); void compositeTextChanged(QString text);
void colorChanged(QColor color); void colorChanged(QColor color);
void fontSizeChanged(int size);
void frameChanged(bool frame); void frameChanged(bool frame);
void textWidthChanged(qreal width); void textWidthChanged(qreal width);
void alignmentChanged(Qt::Alignment alignment); void alignmentChanged(Qt::Alignment alignment);
void fontChanged(QFont font);
public: public:
PartDynamicTextField(QETElementEditor *editor, QGraphicsItem *parent = nullptr); PartDynamicTextField(QETElementEditor *editor, QGraphicsItem *parent = nullptr);
@ -88,14 +88,13 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
QString compositeText() const; QString compositeText() const;
void setColor(const QColor& color); void setColor(const QColor& color);
QColor color() const; QColor color() const;
void setFontSize(int s);
int fontSize()const;
void setFrame(bool frame); void setFrame(bool frame);
bool frame() const; bool frame() const;
void setTextWidth(qreal width); void setTextWidth(qreal width);
void setPlainText(const QString &text); void setPlainText(const QString &text);
void setAlignment(Qt::Alignment alignment); void setAlignment(Qt::Alignment alignment);
Qt::Alignment alignment() const; Qt::Alignment alignment() const;
void setFont(const QFont &font);
protected: protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

View File

@ -55,24 +55,32 @@ PartText::~PartText() {
Importe les proprietes d'un texte statique depuis un element XML Importe les proprietes d'un texte statique depuis un element XML
@param xml_element Element XML a lire @param xml_element Element XML a lire
*/ */
void PartText::fromXml(const QDomElement &xml_element) { void PartText::fromXml(const QDomElement &xml_element)
{
bool ok; bool ok;
int font_size = xml_element.attribute("size").toInt(&ok);
if (!ok || font_size < 1) font_size = 20; if (xml_element.hasAttribute("size"))
{
setBlack(xml_element.attribute("color") != "white"); int font_size = xml_element.attribute("size").toInt(&ok);
setProperty("size" , font_size); if (!ok || font_size < 1) {
setPlainText(xml_element.attribute("text")); font_size = 20;
}
qreal default_rotation_angle = 0.0; QFont font_ = this->font();
if (QET::attributeIsAReal(xml_element, "rotation", &default_rotation_angle)) { font_.setPointSize(font_size);
setRotation(default_rotation_angle); setFont(font_);
}
else if (xml_element.hasAttribute("font"))
{
QFont font_;
font_.fromString(xml_element.attribute("font"));
setFont(font_);
} }
setPos( setDefaultTextColor(QColor(xml_element.attribute("color", "#000000")));
xml_element.attribute("x").toDouble(), setPlainText(xml_element.attribute("text"));
xml_element.attribute("y").toDouble() setPos(xml_element.attribute("x").toDouble(),
); xml_element.attribute("y").toDouble());
setRotation(xml_element.attribute("rotation", QString::number(0)).toDouble());
} }
/** /**
@ -80,19 +88,17 @@ void PartText::fromXml(const QDomElement &xml_element) {
@param xml_document Document XML a utiliser pour creer l'element XML @param xml_document Document XML a utiliser pour creer l'element XML
@return un element XML decrivant le texte statique @return un element XML decrivant le texte statique
*/ */
const QDomElement PartText::toXml(QDomDocument &xml_document) const { const QDomElement PartText::toXml(QDomDocument &xml_document) const
QDomElement xml_element = xml_document.createElement("text"); {
xml_element.setAttribute("x", QString("%1").arg(pos().x())); QDomElement xml_element = xml_document.createElement(xmlName());
xml_element.setAttribute("y", QString("%1").arg(pos().y()));
xml_element.setAttribute("x", QString::number(pos().x()));
xml_element.setAttribute("y", QString::number(pos().y()));
xml_element.setAttribute("text", toPlainText()); xml_element.setAttribute("text", toPlainText());
xml_element.setAttribute("size", font().pointSize()); xml_element.setAttribute("font", font().toString());
// angle de rotation du champ de texte xml_element.setAttribute("rotation", QString::number(rotation()));
if (rotation()) { xml_element.setAttribute("color", defaultTextColor().name());
xml_element.setAttribute("rotation", QString("%1").arg(rotation()));
}
if (!isBlack()) {
xml_element.setAttribute("color", "white");
}
return(xml_element); return(xml_element);
} }
@ -228,6 +234,33 @@ void PartText::handleUserTransformation(const QRectF &initial_selection_rect, co
setProperty("real_size", qMax(1, qRound(new_font_size))); setProperty("real_size", qMax(1, qRound(new_font_size)));
} }
void PartText::setDefaultTextColor(const QColor &color)
{
if (color != this->defaultTextColor())
{
QGraphicsTextItem::setDefaultTextColor(color);
emit colorChanged(color);
}
}
void PartText::setPlainText(const QString &text)
{
if (text != this->toPlainText())
{
QGraphicsTextItem::setPlainText(text);
emit plainTextChanged(text);
}
}
void PartText::setFont(const QFont &font)
{
if (font != this->font())
{
QGraphicsTextItem::setFont(font);
emit fontChanged(font);
}
}
void PartText::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void PartText::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {

View File

@ -26,53 +26,55 @@ class ElementPrimitiveDecorator;
This class represents an static text primitive which may be used to compose This class represents an static text primitive which may be used to compose
the drawing of an electrical element within the element editor. the drawing of an electrical element within the element editor.
*/ */
class PartText : public QGraphicsTextItem, public CustomElementPart { class PartText : public QGraphicsTextItem, public CustomElementPart
{
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal real_size READ realSize WRITE setRealSize)
Q_PROPERTY(QColor color READ defaultTextColor WRITE setDefaultTextColor NOTIFY colorChanged)
Q_PROPERTY(QString text READ toPlainText WRITE setPlainText NOTIFY plainTextChanged)
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
signals:
void fontChanged(const QFont &font);
void colorChanged(const QColor &color);
void plainTextChanged(const QString &text);
// constructors, destructor // constructors, destructor
public: public:
PartText(QETElementEditor *, QGraphicsItem * = nullptr); PartText(QETElementEditor *, QGraphicsItem * = nullptr);
~PartText() override; ~PartText() override;
private: private:
PartText(const PartText &); PartText(const PartText &);
// methods // methods
public: public:
enum { Type = UserType + 1107 }; enum { Type = UserType + 1107 };
/** /**
Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a
PartText. PartText.
@return the QGraphicsItem type @return the QGraphicsItem type
*/ */
int type() const override { return Type; } int type() const override { return Type; }
QString name() const override { return(QObject::tr("texte", "element part name")); } QString name() const override { return(QObject::tr("texte", "element part name")); }
QString xmlName() const override { return(QString("text")); } QString xmlName() const override { return(QString("text")); }
void fromXml(const QDomElement &) override; void fromXml(const QDomElement &) override;
const QDomElement toXml(QDomDocument &) const override; const QDomElement toXml(QDomDocument &) const override;
void setRotation(qreal angle) {(QGraphicsObject::setRotation(QET::correctAngle(angle)));} void setRotation(qreal angle) {(QGraphicsObject::setRotation(QET::correctAngle(angle)));}
bool isUseless() const override; bool isUseless() const override;
QRectF sceneGeometricRect() const override; QRectF sceneGeometricRect() const override;
void startUserTransformation(const QRectF &) override; void startUserTransformation(const QRectF &) override;
void handleUserTransformation(const QRectF &, const QRectF &) override; void handleUserTransformation(const QRectF &, const QRectF &) override;
void setProperty(const char *name, const QVariant &value) override {QGraphicsTextItem::setProperty(name, value);}
QVariant property(const char *name) const override {return QGraphicsTextItem::property(name);}
///PROPERTY
void setProperty(const char *name, const QVariant &value) override {QGraphicsTextItem::setProperty(name, value);}
QVariant property(const char *name) const override {return QGraphicsTextItem::property(name);}
// Size value
Q_PROPERTY(qreal size READ size WRITE setSize)
qreal size () const {return font().pointSize();}
void setSize (qreal s) {setFont(QETApp::dynamicTextsItemFont(s));}
// Real size value
Q_PROPERTY(qreal real_size READ realSize WRITE setRealSize)
qreal realSize() const {return real_font_size_;} qreal realSize() const {return real_font_size_;}
void setRealSize(qreal rs) {real_font_size_ = rs;} void setRealSize(qreal rs) {real_font_size_ = rs;}
// Color value (true = black , false = white) void setDefaultTextColor(const QColor &color);
Q_PROPERTY(bool color READ isBlack WRITE setBlack) void setPlainText(const QString &text);
bool isBlack() const {return defaultTextColor() == Qt::black;} void setFont(const QFont &font);
void setBlack(bool b) {setDefaultTextColor(b ? Qt::black : Qt::white);}
// displayed string
Q_PROPERTY(QString text READ toPlainText WRITE setPlainText)
public slots: public slots:
void adjustItemPosition(int = 0); void adjustItemPosition(int = 0);

View File

@ -1,241 +0,0 @@
/*
Copyright 2006-2019 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 "texteditor.h"
#include "parttext.h"
#include "qetapp.h"
#include "qtextorientationspinboxwidget.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
/**
Constructeur
@param editor L'editeur d'element concerne
@param text Champ de texte a editer
@param parent QWidget parent de ce widget
*/
TextEditor::TextEditor(QETElementEditor *editor, PartText *text, QWidget *parent) :
ElementItemEditor(editor, parent),
part(text),
m_locked(false)
{
qle_x = new QDoubleSpinBox();
qle_y = new QDoubleSpinBox();
qle_text = new QLineEdit();
qle_text ->setClearButtonEnabled(true);
font_size = new QSpinBox();
font_size -> setRange(0, 144);
black_color_ = new QRadioButton(tr("Noir", "element text part color"));
white_color_ = new QRadioButton(tr("Blanc", "element text part color"));
color_ = new QButtonGroup(this);
color_ -> addButton(black_color_, true);
color_ -> addButton(white_color_, false);
connect(color_, SIGNAL(buttonClicked(int)), this, SLOT(updateTextC()));
QLabel *rotation_angle_label = new QLabel(tr("Angle de rotation : "));
rotation_angle_label -> setWordWrap(true);
rotation_angle_ = QETApp::createTextOrientationSpinBoxWidget();
qle_x -> setRange(-5000, 5000);
qle_y -> setRange(-5000, 5000);
QVBoxLayout *main_layout = new QVBoxLayout();
main_layout -> addWidget(new QLabel(tr("Position : ")));
QHBoxLayout *position = new QHBoxLayout();
position -> addWidget(new QLabel(tr("x : ")));
position -> addWidget(qle_x );
position -> addWidget(new QLabel(tr("y : ")));
position -> addWidget(qle_y );
main_layout -> addLayout(position);
QHBoxLayout *fs = new QHBoxLayout();
fs -> addWidget(new QLabel(tr("Taille : ")));
fs -> addWidget(font_size);
main_layout -> addLayout(fs);
QHBoxLayout *color_layout = new QHBoxLayout();
color_layout -> addWidget(new QLabel(tr("Couleur : ")));
color_layout -> addWidget(black_color_);
color_layout -> addWidget(white_color_);
color_layout -> addStretch();
main_layout -> addLayout(color_layout);
QHBoxLayout *t = new QHBoxLayout();
t -> addWidget(new QLabel(tr("Texte : ")));
t -> addWidget(qle_text);
QHBoxLayout *rotation_angle_layout = new QHBoxLayout();
rotation_angle_layout -> addWidget(rotation_angle_label);
rotation_angle_layout -> addWidget(rotation_angle_);
main_layout -> addLayout(t);
main_layout -> addLayout(rotation_angle_layout);
main_layout -> addStretch();
setLayout(main_layout);
updateForm();
}
/**
Destructeur
*/
TextEditor::~TextEditor() {
}
/**
Permet de specifier a cet editeur quelle primitive il doit editer. A noter
qu'un editeur peut accepter ou refuser d'editer une primitive.
L'editeur de texte statique acceptera d'editer la primitive new_part s'il
s'agit d'un objet de la classe PartText.
@param new_part Nouvelle primitive a editer
@return true si l'editeur a accepter d'editer la primitive, false sinon
*/
bool TextEditor::setPart(CustomElementPart *new_part)
{
if (!new_part)
{
part = nullptr;
return(true);
}
if (PartText *part_text = dynamic_cast<PartText *>(new_part))
{
if (part == part_text) return true;
part = part_text;
updateForm();
return(true);
}
return(false);
}
/**
@return la primitive actuellement editee, ou 0 si ce widget n'en edite pas
*/
CustomElementPart *TextEditor::currentPart() const {
return(part);
}
/// Met a jour le texte et cree un objet d'annulation
void TextEditor::updateTextT()
{
if(m_locked) return;
m_locked = true;
QString text = qle_text->text();
if (text != part->property("text"))
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "text", part->property("text"), text);
undo->setText(tr("Modifier le contenu d'un champ texte"));
undoStack().push(undo);
}
m_locked= false;
}
/// Met a jour la taille du texte et cree un objet d'annulation
void TextEditor::updateTextS()
{
if(m_locked) return;
m_locked = true;
int size = font_size->value();
if (size != part->property("size"))
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "size", part->property("size"), size);
undo->setText(tr("Modifier la taille d'un champ texte"));
undo->enableAnimation();
undoStack().push(undo);
}
m_locked= false;
}
/// Update the text color and create an undo object
void TextEditor::updateTextC()
{
if(m_locked) return;
m_locked = true;
int color = color_->checkedId();
if (color != part->property("color"))
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "color", part->property("color"), color);
undo->setText(tr("Modifier la couleur d'un champ texte"));
undoStack().push(undo);
}
m_locked= false;
}
/// Met a jour l'angle de rotation du champ de texte et cree un objet d'annulation
void TextEditor::updateTextRotationAngle()
{
if(m_locked) return;
m_locked = true;
double rot = rotation_angle_->value();
if (rot != part->property("rotation"))
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "rotation", part->property("rotation"), rot);
undo->setText(tr("Modifier l'angle d'un champ texte"));
undo->enableAnimation();
undoStack().push(undo);
}
m_locked= false;
}
void TextEditor::updatePos()
{
if(m_locked) return;
m_locked = true;
QPointF pos(qle_x->value(), qle_y->value());
if (pos != part->pos())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "pos", part->pos(), pos);
undo->setText(tr("Déplacer un champ texte"));
undo->enableAnimation();
undoStack().push(undo);
}
m_locked= false;
}
/**
Met a jour le formulaire a partir du champ de texte
*/
void TextEditor::updateForm() {
if (!part) return;
activeConnections(false);
qle_x -> setValue(part->property("x").toReal());
qle_y -> setValue(part->property("y").toReal());
qle_text -> setText(part -> property("text").toString());
font_size -> setValue(part -> property("size").toInt());
if (QAbstractButton *button = color_ -> button(part -> property("color").toBool())) {
button -> setChecked(true);
}
rotation_angle_ -> setValue(part -> property("rotation").toReal());
activeConnections(true);
}
void TextEditor::activeConnections(bool active)
{
if (active)
{
connect(qle_x, SIGNAL(editingFinished()), this, SLOT(updatePos()));
connect(qle_y, SIGNAL(editingFinished()), this, SLOT(updatePos()));
connect(qle_text, SIGNAL(editingFinished()), this, SLOT(updateTextT()));
connect(font_size, SIGNAL(editingFinished()), this, SLOT(updateTextS()));
connect(rotation_angle_, SIGNAL(editingFinished()), this, SLOT(updateTextRotationAngle()));
}
else
{
disconnect(qle_x, SIGNAL(editingFinished()), this, SLOT(updatePos()));
disconnect(qle_y, SIGNAL(editingFinished()), this, SLOT(updatePos()));
disconnect(qle_text, SIGNAL(editingFinished()), this, SLOT(updateTextT()));
disconnect(font_size, SIGNAL(editingFinished()), this, SLOT(updateTextS()));
disconnect(rotation_angle_, SIGNAL(editingFinished()), this, SLOT(updateTextRotationAngle()));
}
}

View File

@ -73,7 +73,7 @@ bool DynamicTextFieldEditor::setPart(CustomElementPart *part)
//Setup the connection //Setup the connection
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::colorChanged, [this](){this->updateForm();}); m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::colorChanged, [this](){this->updateForm();});
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::fontSizeChanged, [this](){this->updateForm();}); m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::fontChanged, [this](){this->updateForm();});
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::taggChanged, [this](){this->updateForm();}); m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::taggChanged, [this](){this->updateForm();});
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textFromChanged, [this](){this->updateForm();}); m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textFromChanged, [this](){this->updateForm();});
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textChanged, [this](){this->updateForm();}); m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textChanged, [this](){this->updateForm();});
@ -104,9 +104,10 @@ void DynamicTextFieldEditor::updateForm()
ui->m_rotation_sb->setValue(QET::correctAngle(m_text_field.data()->rotation())); ui->m_rotation_sb->setValue(QET::correctAngle(m_text_field.data()->rotation()));
ui->m_frame_cb->setChecked(m_text_field.data()->frame()); ui->m_frame_cb->setChecked(m_text_field.data()->frame());
ui->m_user_text_le->setText(m_text_field.data()->text()); ui->m_user_text_le->setText(m_text_field.data()->text());
ui->m_size_sb->setValue(m_text_field.data()->fontSize()); ui->m_size_sb->setValue(m_text_field.data()->font().pointSize());
setColorPushButton(m_text_field.data()->color()); setColorPushButton(m_text_field.data()->color());
ui->m_width_sb->setValue(m_text_field.data()->textWidth()); ui->m_width_sb->setValue(m_text_field.data()->textWidth());
ui->m_font_pb->setText(m_text_field->font().family());
switch (m_text_field.data()->textFrom()) switch (m_text_field.data()->textFrom())
{ {
@ -117,12 +118,10 @@ void DynamicTextFieldEditor::updateForm()
{ {
ui->m_text_from_cb->setCurrentIndex(1); ui->m_text_from_cb->setCurrentIndex(1);
ui->m_elmt_info_cb->setCurrentIndex(ui->m_elmt_info_cb->findData(m_text_field.data()->infoName())); ui->m_elmt_info_cb->setCurrentIndex(ui->m_elmt_info_cb->findData(m_text_field.data()->infoName()));
}
break; break;
}
case DynamicElementTextItem::CompositeText: case DynamicElementTextItem::CompositeText:
ui->m_text_from_cb->setCurrentIndex(2); ui->m_text_from_cb->setCurrentIndex(2);
default:
break;
} }
on_m_text_from_cb_activated(ui->m_text_from_cb->currentIndex()); //For enable the good widget on_m_text_from_cb_activated(ui->m_text_from_cb->currentIndex()); //For enable the good widget
@ -195,9 +194,10 @@ void DynamicTextFieldEditor::on_m_user_text_le_editingFinished()
void DynamicTextFieldEditor::on_m_size_sb_editingFinished() void DynamicTextFieldEditor::on_m_size_sb_editingFinished()
{ {
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "fontSize", m_text_field.data()->fontSize(), ui->m_size_sb->value()); QFont font_ = m_text_field->font();
undo->setText(tr("Modifier la taille d'un champ texte")); font_.setPointSize(ui->m_size_sb->value());
undo->enableAnimation(true); QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "font", m_text_field.data()->font(), font_);
undo->setText(tr("Modifier la police d'un champ texte"));
undoStack().push(undo); undoStack().push(undo);
} }
@ -306,3 +306,18 @@ void DynamicTextFieldEditor::on_m_alignment_pb_clicked()
undoStack().push(undo); undoStack().push(undo);
} }
} }
void DynamicTextFieldEditor::on_m_font_pb_clicked()
{
bool ok;
QFont font_ = QFontDialog::getFont(&ok, m_text_field->font(), this);
if (ok && font_ != this->font())
{
ui->m_font_pb->setText(font_.family());
ui->m_size_sb->setValue(font_.pointSize());
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field.data(), "font", m_text_field->font(), font_);
undo->setText(tr("Modifier la police d'un champ texte"));
undoStack().push(undo);
}
}

View File

@ -58,8 +58,9 @@ class DynamicTextFieldEditor : public ElementItemEditor
void on_m_text_from_cb_activated(int index); void on_m_text_from_cb_activated(int index);
void on_m_composite_text_pb_clicked(); void on_m_composite_text_pb_clicked();
void on_m_alignment_pb_clicked(); void on_m_alignment_pb_clicked();
void on_m_font_pb_clicked();
private:
private:
Ui::DynamicTextFieldEditor *ui; Ui::DynamicTextFieldEditor *ui;
QPointer<PartDynamicTextField> m_text_field; QPointer<PartDynamicTextField> m_text_field;
QList<QMetaObject::Connection> m_connection_list; QList<QMetaObject::Connection> m_connection_list;

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>299</width> <width>344</width>
<height>332</height> <height>285</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -76,13 +76,10 @@
<item row="6" column="0"> <item row="6" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>Taille</string> <string>Police</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="1" colspan="2">
<widget class="QSpinBox" name="m_size_sb"/>
</item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
<property name="text"> <property name="text">
@ -206,6 +203,16 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="6" column="1">
<widget class="QSpinBox" name="m_size_sb"/>
</item>
<item row="6" column="2">
<widget class="QPushButton" name="m_font_pb">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View File

@ -0,0 +1,224 @@
/*
Copyright 2006-2019 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 "texteditor.h"
#include "ui_texteditor.h"
#include "parttext.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
/**
* @brief TextEditor::TextEditor
* Default constructor
* @param editor : the element editor who use this editor
* @param text : the text to edit
* @param parent : the parent widget
*/
TextEditor::TextEditor(QETElementEditor *editor, PartText *text, QWidget *parent) :
ElementItemEditor(editor, parent),
ui(new Ui::TextEditor)
{
ui->setupUi(this);
setUpEditConnection();
if (text)
{
setPart(text);
updateForm();
}
}
/**
* @brief TextEditor::~TextEditor
*/
TextEditor::~TextEditor() {
delete ui;
}
/**
* @brief TextEditor::updateForm
* Update the gui
*/
void TextEditor::updateForm()
{
if (m_text.isNull()) {
return;
}
for (QMetaObject::Connection c : m_edit_connection) {
disconnect(c);
}
m_edit_connection.clear();
ui->m_line_edit->setText(m_text->toPlainText());
ui->m_x_sb->setValue(m_text->pos().x());
ui->m_y_sb->setValue(m_text->pos().y());
ui->m_rotation_sb->setValue(m_text->rotation());
ui->m_size_sb->setValue(m_text->font().pointSize());
ui->m_font_pb->setText(m_text->font().family());
ui->m_color_pb->setColor(m_text->defaultTextColor());
setUpEditConnection();
}
/**
* @brief TextEditor::setPart
* Set the current text to edit.
* Set @part to nullptr to clear the current text.
* @param part : part to edit
* @return : return if @part is a partext or nullptr, else return false
*/
bool TextEditor::setPart(CustomElementPart *part)
{
if (!part)
{
m_text = nullptr;
for (QMetaObject::Connection c : m_change_connection) {
disconnect(c);
}
m_change_connection.clear();
return true;
}
if (PartText *part_text = dynamic_cast<PartText *>(part))
{
if (part_text == m_text) {
return true;
}
m_text = part_text;
m_change_connection.clear();
m_change_connection << connect(part_text, &PartText::plainTextChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::xChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::yChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::rotationChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::fontChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::colorChanged, this, &TextEditor::updateForm);
updateForm();
return true;
}
return false;
}
/**
* @brief TextEditor::currentPart
* @return The current part
*/
CustomElementPart *TextEditor::currentPart() const {
return m_text;
}
/**
* @brief TextEditor::setUpEditConnection
* Setup the connection between the widgets of this editor and the undo command
* use to apply the change to the edited text.
*/
void TextEditor::setUpEditConnection()
{
for (QMetaObject::Connection c : m_edit_connection) {
disconnect(c);
}
m_edit_connection.clear();
m_edit_connection << connect(ui->m_line_edit, &QLineEdit::textEdited, [this]()
{
QString text_ = ui->m_line_edit->text();
if (text_ != m_text->toPlainText())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "text", m_text->toPlainText(), text_);
undo->setText(tr("Modifier le contenu d'un champ texte"));
undoStack().push(undo);
}
});
m_edit_connection << connect(ui->m_x_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{
QPointF pos(ui->m_x_sb->value(), ui->m_y_sb->value());
if (pos != m_text->pos())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "pos", m_text->pos(), pos);
undo->setText(tr("Déplacer un champ texte"));
undo->setAnimated(true, false);
undoStack().push(undo);
}
});
m_edit_connection << connect(ui->m_y_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{
QPointF pos(ui->m_x_sb->value(), ui->m_y_sb->value());
if (pos != m_text->pos())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "pos", m_text->pos(), pos);
undo->setText(tr("Déplacer un champ texte"));
undo->setAnimated(true, false);
undoStack().push(undo);
}
});
m_edit_connection << connect(ui->m_rotation_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{
if (ui->m_rotation_sb->value() != m_text->rotation())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "rotation", m_text->rotation(), ui->m_rotation_sb->value());
undo->setText(tr("Pivoter un champ texte"));
undo->setAnimated(true, false);
undoStack().push(undo);
}
});
m_edit_connection << connect(ui->m_size_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{
if (m_text->font().pointSize() != ui->m_size_sb->value())
{
QFont font_ = m_text->font();
font_.setPointSize(ui->m_size_sb->value());
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "font", m_text->font(), font_);
undo->setText(tr("Modifier la police d'un texte"));
undoStack().push(undo);
}
});
}
/**
* @brief TextEditor::on_m_font_pb_clicked
*/
void TextEditor::on_m_font_pb_clicked()
{
bool ok;
QFont font_ = QFontDialog::getFont(&ok, m_text->font(), this);
if (ok && font_ != m_text->font())
{
ui->m_size_sb->blockSignals(true);
ui->m_size_sb->setValue(font_.pointSize());
ui->m_size_sb->blockSignals(false);
ui->m_font_pb->setText(font_.family());
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "font", m_text->font(), font_);
undo->setText(tr("Modifier la police d'un texte"));
undoStack().push(undo);
}
}
/**
* @brief TextEditor::on_m_color_pb_changed
* @param newColor
*/
void TextEditor::on_m_color_pb_changed(const QColor &newColor)
{
if (newColor != m_text->defaultTextColor())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "color", m_text->defaultTextColor(), newColor);
undo->setText(tr("Modifier la couleur d'un texte"));
undoStack().push(undo);
}
}

View File

@ -1,73 +1,58 @@
/* /*
Copyright 2006-2019 The QElectroTech Team Copyright 2006-2019 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef TEXT_EDITOR_H #ifndef TEXTEDITOR_H
#define TEXT_EDITOR_H #define TEXTEDITOR_H
#include "elementitemeditor.h" #include "elementitemeditor.h"
class PartText; #include <QWidget>
class QTextOrientationSpinBoxWidget; #include <QPointer>
class QLineEdit;
class QDoubleSpinBox; class PartText;
class QSpinBox;
class QButtonGroup; namespace Ui {
class QRadioButton; class TextEditor;
}
/**
This class provides a widget to edit static texts within the element
editor.
*/
class TextEditor : public ElementItemEditor class TextEditor : public ElementItemEditor
{ {
Q_OBJECT Q_OBJECT
// Constructors, destructor
public: public:
TextEditor(QETElementEditor *, PartText * = nullptr, QWidget * = nullptr); explicit TextEditor(QETElementEditor *editor, PartText *text = nullptr, QWidget *parent = nullptr);
~TextEditor() override; ~TextEditor() override;
private:
TextEditor(const TextEditor &);
// attributes
private:
PartText *part;
QLineEdit *qle_text;
QDoubleSpinBox *qle_x, *qle_y;
QSpinBox *font_size;
QButtonGroup *color_;
QRadioButton *black_color_, *white_color_;
QTextOrientationSpinBoxWidget *rotation_angle_;
bool m_locked;
// methods
public:
bool setPart(CustomElementPart *) override;
CustomElementPart *currentPart() const override;
public slots:
void updateTextT();
void updateTextS();
void updateTextC();
void updateTextRotationAngle();
void updatePos();
void updateForm() override; void updateForm() override;
bool setPart(CustomElementPart *part) override;
CustomElementPart *currentPart() const override;
private slots:
void on_m_font_pb_clicked();
void on_m_color_pb_changed(const QColor &newColor);
private: private:
void activeConnections(bool); void setUpEditConnection();
private:
Ui::TextEditor *ui;
QPointer <PartText> m_text;
QList <QMetaObject::Connection> m_edit_connection;
QList <QMetaObject::Connection> m_change_connection;
}; };
#endif
#endif // TEXTEDITOR_H

View File

@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TextEditor</class>
<widget class="QWidget" name="TextEditor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>378</width>
<height>133</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="3">
<widget class="QSpinBox" name="m_y_sb">
<property name="minimum">
<number>-10000</number>
</property>
<property name="maximum">
<number>10000</number>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Y :</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Police :</string>
</property>
</widget>
</item>
<item row="1" column="5">
<widget class="QSpinBox" name="m_rotation_sb">
<property name="wrapping">
<bool>true</bool>
</property>
<property name="suffix">
<string>°</string>
</property>
<property name="maximum">
<number>360</number>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Rotation :</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="m_x_sb">
<property name="minimum">
<number>-10000</number>
</property>
<property name="maximum">
<number>10000</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>X :</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="m_size_sb"/>
</item>
<item row="0" column="0" colspan="6">
<widget class="QLineEdit" name="m_line_edit">
<property name="placeholderText">
<string>Entrer votre texte ici</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="5">
<widget class="KColorButton" name="m_color_pb"/>
</item>
<item row="2" column="4">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Couleur :</string>
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<widget class="QPushButton" name="m_font_pb">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>KColorButton</class>
<extends>QPushButton</extends>
<header>kcolorbutton.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -470,13 +470,8 @@ void ElementPictureFactory::parsePolygon(const QDomElement &dom, QPainter &paint
void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter, ElementPictureFactory::primitives &prim) const void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter, ElementPictureFactory::primitives &prim) const
{ {
Q_UNUSED(prim); Q_UNUSED(prim);
qreal pos_x, pos_y; if (dom.tagName() != "text") {
int size;
if (!QET::attributeIsAReal(dom, "x", &pos_x) ||\
!QET::attributeIsAReal(dom, "y", &pos_y) ||\
!QET::attributeIsAnInteger(dom, "size", &size) ||\
!dom.hasAttribute("text")) {
return; return;
} }
@ -484,23 +479,25 @@ void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter,
setPainterStyle(dom, painter); setPainterStyle(dom, painter);
//Get the font and metric //Get the font and metric
QFont used_font = QETApp::diagramTextsFont(size); QFont font_;
QFontMetrics qfm(used_font); if (dom.hasAttribute("size")) {
QColor text_color = (dom.attribute("color") != "white"? Qt::black : Qt::white); font_ = QETApp::diagramTextsFont(dom.attribute("size").toDouble());
}
else if (dom.hasAttribute("font")) {
font_.fromString(dom.attribute("font"));
}
QColor text_color(dom.attribute("color", "#000000"));
//Instanciate a QTextDocument (like the QGraphicsTextItem class) //Instanciate a QTextDocument (like the QGraphicsTextItem class)
//for generate the graphics rendering of the text //for generate the graphics rendering of the text
QTextDocument text_document; QTextDocument text_document;
text_document.setDefaultFont(used_font); text_document.setDefaultFont(font_);
text_document.setPlainText(dom.attribute("text")); text_document.setPlainText(dom.attribute("text"));
painter.setTransform(QTransform(), false); painter.setTransform(QTransform(), false);
painter.translate(pos_x, pos_y); painter.translate(dom.attribute("x").toDouble(), dom.attribute("y").toDouble());
painter.rotate(dom.attribute("rotation", "0").toDouble());
qreal default_rotation_angle = 0.0;
if (QET::attributeIsAReal(dom, "rotation", &default_rotation_angle)) {
painter.rotate(default_rotation_angle);
}
/* /*
Deplace le systeme de coordonnees du QPainter pour effectuer le rendu au Deplace le systeme de coordonnees du QPainter pour effectuer le rendu au
@ -508,6 +505,7 @@ void ElementPictureFactory::parseText(const QDomElement &dom, QPainter &painter,
determiner le coin superieur gauche du texte alors que la position determiner le coin superieur gauche du texte alors que la position
indiquee correspond a la baseline. indiquee correspond a la baseline.
*/ */
QFontMetrics qfm(font_);
QPointF qpainter_offset(0.0, -qfm.ascent()); QPointF qpainter_offset(0.0, -qfm.ascent());
//adjusts the offset by the margin of the text document //adjusts the offset by the margin of the text document