[qet] qet/qet: [5220] Dynamic element text item : Add a new option "width" for define the width of the text.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 5220
Author:   blacksun
Date:     2018-01-26 19:49:38 +0100 (Fri, 26 Jan 2018)
Log Message:
-----------
Dynamic element text item : Add a new option "width" for define the width of the text.
If the text is wider than the defined width, the text is broken into multiple line.

Modified Paths:
--------------
    trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
    trunk/sources/qetgraphicsitem/dynamicelementtextitem.h
    trunk/sources/qetgraphicsitem/element.cpp
    trunk/sources/qetgraphicsitem/elementtextitemgroup.cpp
    trunk/sources/ui/dynamicelementtextmodel.cpp
    trunk/sources/ui/dynamicelementtextmodel.h

Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp	2018-01-26 17:50:09 UTC (rev 5219)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp	2018-01-26 18:49:38 UTC (rev 5220)
@@ -52,6 +52,12 @@
 		}
 		
 	});
+	
+		//Option when text is displayed in multiple line
+	QTextOption option = document()->defaultTextOption();
+	option.setAlignment(Qt::AlignHCenter);
+	option.setWrapMode(QTextOption::WordWrap);
+	document()->setDefaultTextOption(option);
 }
 
 DynamicElementTextItem::~DynamicElementTextItem()
@@ -86,6 +92,7 @@
 	root_element.setAttribute("font_size", font().pointSize());
 	root_element.setAttribute("uuid", m_uuid.toString());
 	root_element.setAttribute("frame", m_frame? "true" : "false");
+	root_element.setAttribute("text_width", QString::number(m_text_width));
 	
 	QMetaEnum me = textFromMetaEnum();
 	root_element.setAttribute("text_from", me.valueToKey(m_text_from));
@@ -139,6 +146,7 @@
 	setFont(QETApp::diagramTextsFont(dom_elmt.attribute("font_size", QString::number(9)).toInt()));
 	m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
 	setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false);
+	setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble());
 	
 		//Text from
 	QMetaEnum me = textFromMetaEnum();
@@ -603,6 +611,7 @@
 	if (m_frame)
 	{
 		painter->save();
+		
 		painter->setFont(QETApp::diagramTextsFont(fontSize()));
 		
 			//Adjust the thickness according to the font size, 
@@ -620,17 +629,24 @@
 		painter->setPen(pen);
 		painter->setRenderHint(QPainter::Antialiasing);
 		
-			//Get the bounding rectangle of the text 
-		QRectF text_bounding = painter->boundingRect(boundingRect(), toPlainText());
-			//Center text_bounding in the bounding rect of this
-		text_bounding.moveTop((boundingRect().height()-text_bounding.height())/2);
-		text_bounding.moveLeft((boundingRect().width() - text_bounding.width())/2);
-			//adjust only for better visual
-		text_bounding.adjust(-2,0,2,0); 
+			//Get the bounding rectangle of the text
+		QSizeF size = document()->size();
+		size.setWidth(document()->idealWidth());
+			//Remove the margin. Size is exactly the bounding rect of the text
+		size.rheight() -= document()->documentMargin()*2;
+		size.rwidth() -= document()->documentMargin()*2;
+			//Add a little margin only for a better visual;
+		size.rheight() += 2;
+		size.rwidth() += 2;
 		
+			//The pos of the rect
+		QPointF pos = boundingRect().center();
+		pos.rx() -= size.width()/2;
+		pos.ry() -= size.height()/2;
+		
 			//Adjust the rounding of the rectangle according to the size of the font
 		qreal ro = (qreal)fontSize()/3;
-		painter->drawRoundedRect(text_bounding, ro, ro);
+		painter->drawRoundedRect(QRectF(pos, size), ro, ro);
 		
 		painter->restore();
 	}
@@ -1262,6 +1278,20 @@
 void DynamicElementTextItem::setPlainText(const QString &text)
 {
 	DiagramTextItem::setPlainText(text);
+	
+		//User define a text width
+	if(m_text_width > 0)
+	{
+		if(document()->size().width() > m_text_width)
+		{
+			document()->setTextWidth(m_text_width);
+			if(document()->size().width() > m_text_width)
+			{
+				document()->setTextWidth(document()->idealWidth());
+			}
+		}
+	}
+	
 	if(m_Xref_item)
 		m_Xref_item->autoPos();
 	else if(m_slave_Xref_item)
@@ -1273,3 +1303,10 @@
 	}
 }
 
+void DynamicElementTextItem::setTextWidth(qreal width)
+{
+	this->document()->setTextWidth(width);
+	m_text_width = width;
+	emit textWidthChanged(width);
+}
+

Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.h	2018-01-26 17:50:09 UTC (rev 5219)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.h	2018-01-26 18:49:38 UTC (rev 5220)
@@ -1,4 +1,4 @@
-/*
+/*
 	Copyright 2006-2017 The QElectroTech Team
 	This file is part of QElectroTech.
 	
@@ -47,6 +47,7 @@
 	Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged)
 	Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged)
 	Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
+	Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
 	
 	public:
 		Q_ENUMS(TextFrom)
@@ -65,6 +66,7 @@
 		void compositeTextChanged(QString text);
 		void frameChanged(bool frame);
 		void plainTextChanged();
+		void textWidthChanged(qreal width);
 	
 	public:
 		DynamicElementTextItem(Element *parent_element);
@@ -97,6 +99,7 @@
 		QUuid uuid() const;
 		void updateXref();
 		void setPlainText(const QString &text);
+		void setTextWidth(qreal width);
 		
 	protected:
 		void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
@@ -148,6 +151,7 @@
 			 m_first_scene_change = true;
 		CrossRefItem *m_Xref_item = nullptr;
 		QGraphicsTextItem *m_slave_Xref_item = nullptr;
+		qreal m_text_width = -1;
 };
 
 #endif // DYNAMICELEMENTTEXTITEM_H

Modified: trunk/sources/qetgraphicsitem/element.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/element.cpp	2018-01-26 17:50:09 UTC (rev 5219)
+++ trunk/sources/qetgraphicsitem/element.cpp	2018-01-26 18:49:38 UTC (rev 5220)
@@ -665,6 +665,7 @@
 						comment_text->setInfoName("comment");
 						comment_text->setFontSize(6);
 						comment_text->setFrame(true);
+						comment_text->setTextWidth(70);
 						comment_text->setPos(deti->x(), deti->y()+10); //+10 is arbitrary, comment_text must be below deti
 						addDynamicTextItem(comment_text);
 					}
@@ -676,6 +677,7 @@
 						location_text->setTextFrom(DynamicElementTextItem::ElementInfo);
 						location_text->setInfoName("location");
 						location_text->setFontSize(6);
+						location_text->setTextWidth(70);
 						location_text->setPos(deti->x(), deti->y()+20); //+20 is arbitrary, location_text must be below deti and comment
 						addDynamicTextItem(location_text);
 					}

Modified: trunk/sources/qetgraphicsitem/elementtextitemgroup.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/elementtextitemgroup.cpp	2018-01-26 17:50:09 UTC (rev 5219)
+++ trunk/sources/qetgraphicsitem/elementtextitemgroup.cpp	2018-01-26 18:49:38 UTC (rev 5220)
@@ -75,6 +75,7 @@
 		connect(deti, &DynamicElementTextItem::infoNameChanged,      this, &ElementTextItemGroup::updateAlignment);
 		connect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
 		connect(deti, &DynamicElementTextItem::plainTextChanged,     this, &ElementTextItemGroup::updateAlignment);
+		connect(deti, &DynamicElementTextItem::textWidthChanged,     this, &ElementTextItemGroup::updateAlignment);
 		
 		connect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateXref);
 		connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateXref);
@@ -105,6 +106,7 @@
 		disconnect(deti, &DynamicElementTextItem::infoNameChanged,      this, &ElementTextItemGroup::updateAlignment);
 		disconnect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
 		disconnect(deti, &DynamicElementTextItem::plainTextChanged,     this, &ElementTextItemGroup::updateAlignment);
+		disconnect(deti, &DynamicElementTextItem::textWidthChanged,     this, &ElementTextItemGroup::updateAlignment);
 		
 		disconnect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateXref);
 		disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateXref);

Modified: trunk/sources/ui/dynamicelementtextmodel.cpp
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.cpp	2018-01-26 17:50:09 UTC (rev 5219)
+++ trunk/sources/ui/dynamicelementtextmodel.cpp	2018-01-26 18:49:38 UTC (rev 5220)
@@ -41,9 +41,10 @@
 int size_txt_row  = 4;
 int color_txt_row = 5;
 int frame_txt_row = 6;
-int x_txt_row     = 7;
-int y_txt_row     = 8;
-int rot_txt_row   = 9;
+int width_txt_row = 7;
+int x_txt_row     = 8;
+int y_txt_row     = 9;
+int rot_txt_row   = 10;
 
 int align_grp_row  = 0;
 int rot_grp_row    = 1;
@@ -222,6 +223,19 @@
 	qsi_list << frame << frame_a;
 	qsi->appendRow(qsi_list);
 	
+		//Width
+	QStandardItem *width = new QStandardItem(tr("Largeur"));
+	width->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
+	
+	QStandardItem *width_a = new QStandardItem;
+	width_a->setData(deti->textWidth(), Qt::EditRole);
+	width_a->setData(DynamicElementTextModel::textWidth, Qt::UserRole+1);
+	width_a->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
+	
+	qsi_list.clear();
+	qsi_list << width << width_a;
+	qsi->appendRow(qsi_list);
+	
 	if(deti->parentGroup() == nullptr)
 	{
 			//X pos
@@ -457,6 +471,13 @@
 		quc->setText(tr("Modifier le cadre d'un texte d'élément"));
 	}
 	
+	qreal text_width = text_qsi->child(width_txt_row, 1)->data(Qt::EditRole).toDouble();
+	if(text_width != deti->textWidth())
+	{
+		QUndoCommand *quc = new QPropertyUndoCommand(deti, "textWidth", QVariant(deti->textWidth()), QVariant(text_width), undo);
+		quc->setText(tr("Modifier la largeur d'un texte d'élément"));
+	}
+	
 		//When text is in a group, they're isn't item for position of the text
 	if(text_qsi->child(x_txt_row,1) && text_qsi->child(y_txt_row,1))
 	{
@@ -1054,6 +1075,7 @@
 		connection_list << connect(deti, &DynamicElementTextItem::yChanged,        [deti,this](){this->updateDataFromText(deti, pos);});
 		connection_list << connect(deti, &DynamicElementTextItem::frameChanged,    [deti,this](){this->updateDataFromText(deti, frame);});
 		connection_list << connect(deti, &DynamicElementTextItem::rotationChanged, [deti,this](){this->updateDataFromText(deti, rotation);});
+		connection_list << connect(deti, &DynamicElementTextItem::textWidthChanged,[deti,this](){this->updateDataFromText(deti, textWidth);});
 		connection_list << connect(deti, &DynamicElementTextItem::compositeTextChanged, [deti, this]() {this->updateDataFromText(deti, compositeText);});
 		
 		m_hash_text_connect.insert(deti, connection_list);
@@ -1177,6 +1199,11 @@
 				qsi->child(rot_txt_row,1)->setData(deti->rotation(), Qt::EditRole);
 			break;
 		}
+		case textWidth:
+		{
+			qsi->child(width_txt_row,1)->setData(deti->textWidth(), Qt::EditRole);
+			break;
+		}
 	}
 	
 	m_block_dataChanged = false;
@@ -1314,6 +1341,15 @@
 			sb->setSuffix(" °");
 			return sb;
 		}
+		case DynamicElementTextModel::textWidth:
+		{
+			QSpinBox *sb = new QSpinBox(parent);
+			sb->setObjectName("width_spinbox");
+			sb->setRange(-1, 500);
+			sb->setFrame(false);
+			sb->setSuffix(" px");
+			return sb;
+		}
 		case DynamicElementTextModel::grp_alignment:
 		{
 			QComboBox *qcb = new QComboBox(parent);
@@ -1431,7 +1467,7 @@
 		//With this hack the value is commited each time the value change without the need to validate.
 		//then the change is apply in live
 	if(object->objectName() == "pos_dialog" || object->objectName() == "font_size" || object->objectName() == "rot_spinbox" || \
-	   object->objectName() == "group_rotation" || object->objectName() == "group_v_adjustment")
+	   object->objectName() == "group_rotation" || object->objectName() == "group_v_adjustment" || object->objectName() == "width_spinbox")
 	{
 		object->event(event);
 		

Modified: trunk/sources/ui/dynamicelementtextmodel.h
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.h	2018-01-26 17:50:09 UTC (rev 5219)
+++ trunk/sources/ui/dynamicelementtextmodel.h	2018-01-26 18:49:38 UTC (rev 5220)
@@ -46,7 +46,8 @@
             color,
 			pos,
 			frame,
-			rotation, 
+			rotation,
+			textWidth,
 			grp_alignment,
 			grp_rotation,
 			grp_v_adjust,


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/