[qet] qet/qet: [5362] Element editor : alignment of text field can be edited

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


Revision: 5362
Author:   blacksun
Date:     2018-05-13 21:03:38 +0200 (Sun, 13 May 2018)
Log Message:
-----------
Element editor : alignment of text field can be edited

Modified Paths:
--------------
    trunk/sources/editor/graphicspart/partdynamictextfield.cpp
    trunk/sources/editor/graphicspart/partdynamictextfield.h
    trunk/sources/editor/ui/dynamictextfieldeditor.cpp
    trunk/sources/editor/ui/dynamictextfieldeditor.h
    trunk/sources/editor/ui/dynamictextfieldeditor.ui
    trunk/sources/ui/alignmenttextdialog.cpp
    trunk/sources/ui/alignmenttextdialog.h
    trunk/sources/ui/dynamicelementtextmodel.cpp

Modified: trunk/sources/editor/graphicspart/partdynamictextfield.cpp
===================================================================
--- trunk/sources/editor/graphicspart/partdynamictextfield.cpp	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/editor/graphicspart/partdynamictextfield.cpp	2018-05-13 19:03:38 UTC (rev 5362)
@@ -99,6 +99,21 @@
 	QMetaEnum me = DynamicElementTextItem::textFromMetaEnum();
 	root_element.setAttribute("text_from", me.valueToKey(m_text_from));
 	
+	me = QMetaEnum::fromType<Qt::Alignment>();
+	if(this->alignment() &Qt::AlignRight)
+		root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignRight));
+	else if(this->alignment() &Qt::AlignLeft)
+		root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignLeft));
+	else if(this->alignment() &Qt::AlignHCenter)
+		root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignHCenter));
+	
+	if(this->alignment() &Qt::AlignBottom)
+		root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignBottom));
+	else if(this->alignment() & Qt::AlignTop)
+		root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignTop));
+	else if(this->alignment() &Qt::AlignVCenter)
+		root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignVCenter));
+	
     QDomElement dom_text = dom_doc.createElement("text");
     dom_text.appendChild(dom_doc.createTextNode(toPlainText()));
     root_element.appendChild(dom_text);
@@ -152,6 +167,12 @@
 	
 	QMetaEnum me = DynamicElementTextItem::textFromMetaEnum();
 	m_text_from = DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data()));
+	
+	me = QMetaEnum::fromType<Qt::Alignment>();
+	if(dom_elmt.hasAttribute("Halignment"))
+		setAlignment(Qt::Alignment(me.keyToValue(dom_elmt.attribute("Halignment").toStdString().data())));
+	if(dom_elmt.hasAttribute(("Valignment")))
+		setAlignment(Qt::Alignment(me.keyToValue(dom_elmt.attribute("Valignment").toStdString().data())) | this->alignment());
 
 		//Text
     QDomElement dom_text = dom_elmt.firstChildElement("text");
@@ -158,7 +179,9 @@
 	if (!dom_text.isNull())
 	{
 		m_text = dom_text.text();
+		m_block_alignment = true;
 		setPlainText(m_text);
+		m_block_alignment = false;
 	}
 	
 		//Info name
@@ -363,6 +386,10 @@
 
 void PartDynamicTextField::setPlainText(const QString &text)
 {
+	if(toPlainText() == text)
+		return;
+	
+	prepareAlignment();
 	QGraphicsTextItem::setPlainText(text);
 	
 		//User define a text width
@@ -377,8 +404,19 @@
 			}
 		}
 	}
+	finishAlignment();
 }
 
+void PartDynamicTextField::setAlignment(Qt::Alignment alignment)
+{
+	m_alignment = alignment;
+	emit alignmentChanged(m_alignment);
+}
+
+Qt::Alignment PartDynamicTextField::alignment() const {
+	return m_alignment;
+}
+
 /**
  * @brief PartDynamicTextField::mouseMoveEvent
  * @param event
@@ -509,3 +547,48 @@
 	else if (m_text_from == DynamicElementTextItem::CompositeText && elementScene())
 		setPlainText(autonum::AssignVariables::replaceVariable(m_composite_text, elementScene()->elementInformation()));
 }
+
+void PartDynamicTextField::prepareAlignment()
+{
+	m_alignment_rect = boundingRect();
+}
+
+void PartDynamicTextField::finishAlignment()
+{
+	if(m_block_alignment)
+		return;
+	
+	QTransform transform;
+	transform.rotate(this->rotation());
+	qreal x,xa, y,ya;
+	x=xa=0;
+	y=ya=0;
+
+	if(m_alignment &Qt::AlignRight)
+	{
+		x = m_alignment_rect.right();
+		xa = boundingRect().right();
+	}
+	else if(m_alignment &Qt::AlignHCenter)
+	{
+		x = m_alignment_rect.center().x();
+		xa = boundingRect().center().x();
+	}
+	
+	if(m_alignment &Qt::AlignBottom)
+	{
+		y = m_alignment_rect.bottom();
+		ya = boundingRect().bottom();
+	}
+	else if(m_alignment &Qt::AlignVCenter)
+	{
+		y = m_alignment_rect.center().y();
+		ya = boundingRect().center().y();
+	}
+
+	QPointF p = transform.map(QPointF(x,y));
+	QPointF pa = transform.map(QPointF(xa,ya));
+	QPointF diff = pa-p;
+	
+	setPos(this->pos() - diff);
+}

Modified: trunk/sources/editor/graphicspart/partdynamictextfield.h
===================================================================
--- trunk/sources/editor/graphicspart/partdynamictextfield.h	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/editor/graphicspart/partdynamictextfield.h	2018-05-13 19:03:38 UTC (rev 5362)
@@ -41,6 +41,7 @@
 	Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
 	Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
 	Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
+	Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
 	
 	public:
 			///PROPERTY
@@ -57,6 +58,7 @@
 		void fontSizeChanged(int size);
 		void frameChanged(bool frame);
 		void textWidthChanged(qreal width);
+		void alignmentChanged(Qt::Alignment alignment);
 	
 	public:
 		PartDynamicTextField(QETElementEditor *editor, QGraphicsItem *parent = nullptr);
@@ -92,6 +94,8 @@
 		bool frame() const;
 		void setTextWidth(qreal width);
 		void setPlainText(const QString &text);
+		void setAlignment(Qt::Alignment alignment);
+		Qt::Alignment alignment() const;
 		
 	protected:
 		void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
@@ -103,6 +107,9 @@
 	private:
 		void elementInfoChanged();
 		
+		void prepareAlignment();
+		void finishAlignment();
+		
 	private:
 		QPointF m_origine_pos,
 				m_saved_point;
@@ -112,8 +119,11 @@
 		DynamicElementTextItem::TextFrom m_text_from = DynamicElementTextItem::UserText;
 		QUuid m_uuid;
 		bool m_frame = false,
-			 m_first_add = true;
+			 m_first_add = true,
+			 m_block_alignment = false;
 		qreal m_text_width = -1;
+		Qt::Alignment m_alignment = Qt::AlignTop|Qt::AlignLeft;
+		QRectF m_alignment_rect;
 };
 
 #endif // PARTDYNAMICTEXTFIELD_H

Modified: trunk/sources/editor/ui/dynamictextfieldeditor.cpp
===================================================================
--- trunk/sources/editor/ui/dynamictextfieldeditor.cpp	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/editor/ui/dynamictextfieldeditor.cpp	2018-05-13 19:03:38 UTC (rev 5362)
@@ -23,6 +23,7 @@
 #include "qetelementeditor.h"
 #include "qetapp.h"
 #include "compositetexteditdialog.h"
+#include "alignmenttextdialog.h"
 
 #include <QPointer>
 #include <QGraphicsItem>
@@ -292,3 +293,16 @@
 		}
 	}
 }
+
+void DynamicTextFieldEditor::on_m_alignment_pb_clicked()
+{
+    AlignmentTextDialog atd(m_text_field.data()->alignment(), this);
+	atd.exec();
+
+	if(atd.alignment() != m_text_field.data()->alignment())
+	{
+		QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field.data(), "alignment", QVariant(m_text_field.data()->alignment()), QVariant(atd.alignment()));
+		undo->setText(tr("Modifier l'alignement d'un champ texte"));
+		undoStack().push(undo);
+	}
+}

Modified: trunk/sources/editor/ui/dynamictextfieldeditor.h
===================================================================
--- trunk/sources/editor/ui/dynamictextfieldeditor.h	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/editor/ui/dynamictextfieldeditor.h	2018-05-13 19:03:38 UTC (rev 5362)
@@ -56,8 +56,8 @@
 		void on_m_width_sb_editingFinished();
 		void on_m_elmt_info_cb_activated(const QString &arg1);
 		void on_m_text_from_cb_activated(int index);
-		
 		void on_m_composite_text_pb_clicked();
+		void on_m_alignment_pb_clicked();
 		
 		private:
 		Ui::DynamicTextFieldEditor *ui;

Modified: trunk/sources/editor/ui/dynamictextfieldeditor.ui
===================================================================
--- trunk/sources/editor/ui/dynamictextfieldeditor.ui	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/editor/ui/dynamictextfieldeditor.ui	2018-05-13 19:03:38 UTC (rev 5362)
@@ -196,6 +196,13 @@
         </property>
        </widget>
       </item>
+      <item row="1" column="6">
+       <widget class="QPushButton" name="m_alignment_pb">
+        <property name="text">
+         <string>Alignement</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>

Modified: trunk/sources/ui/alignmenttextdialog.cpp
===================================================================
--- trunk/sources/ui/alignmenttextdialog.cpp	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/ui/alignmenttextdialog.cpp	2018-05-13 19:03:38 UTC (rev 5362)
@@ -19,30 +19,29 @@
 #include "ui_alignmenttextdialog.h"
 #include "dynamicelementtextitem.h"
 
-AlignmentTextDialog::AlignmentTextDialog(DynamicElementTextItem *text, QWidget *parent) :
+AlignmentTextDialog::AlignmentTextDialog(Qt::Alignment alignment, QWidget *parent) :
 	QDialog(parent),
 	ui(new Ui::AlignmentTextDialog)
 {
 	ui->setupUi(this);
 	
-	Qt::Alignment align = text->alignment();
-	if(align == (Qt::AlignTop|Qt::AlignLeft))
+	if(alignment == (Qt::AlignTop|Qt::AlignLeft))
 		ui->top_left->setChecked(true);
-	else if(align == (Qt::AlignTop|Qt::AlignHCenter))
+	else if(alignment == (Qt::AlignTop|Qt::AlignHCenter))
 		ui->top->setChecked(true);
-	else if(align == (Qt::AlignTop|Qt::AlignRight))
+	else if(alignment == (Qt::AlignTop|Qt::AlignRight))
 		ui->top_right->setChecked(true);
-	else if(align == (Qt::AlignVCenter|Qt::AlignLeft))
+	else if(alignment == (Qt::AlignVCenter|Qt::AlignLeft))
 		ui->left->setChecked(true);
-	else if(align == Qt::AlignCenter)
+	else if(alignment == Qt::AlignCenter)
 		ui->center->setChecked(true);
-	else if(align == (Qt::AlignVCenter|Qt::AlignRight))
+	else if(alignment == (Qt::AlignVCenter|Qt::AlignRight))
 		ui->right->setChecked(true);
-	else if(align == (Qt::AlignBottom|Qt::AlignLeft))
+	else if(alignment == (Qt::AlignBottom|Qt::AlignLeft))
 		ui->bottom_left->setChecked(true);
-	else if(align == (Qt::AlignBottom|Qt::AlignHCenter))
+	else if(alignment == (Qt::AlignBottom|Qt::AlignHCenter))
 		ui->bottom->setChecked(true);
-	else if(align == (Qt::AlignBottom|Qt::AlignRight))
+	else if(alignment == (Qt::AlignBottom|Qt::AlignRight))
 		ui->bottom_right->setChecked(true);
 }
 
@@ -81,7 +80,7 @@
 
 bool AlignmentTextDialog::event(QEvent *event)
 {
-		//Little hack to set focus to a radio button
+		//Little hack when this dialog is called from a QAbstractItemModel, to set focus to a radio button
 		//if we not do that, when the user click on the title bar (for move the dialog) or try to resize the dialog,
 		//the dialog lose focus and close.
 	if(event->type() == QEvent::Show && m_first_show)

Modified: trunk/sources/ui/alignmenttextdialog.h
===================================================================
--- trunk/sources/ui/alignmenttextdialog.h	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/ui/alignmenttextdialog.h	2018-05-13 19:03:38 UTC (rev 5362)
@@ -20,8 +20,6 @@
 
 #include <QDialog>
 
-class DynamicElementTextItem;
-
 namespace Ui {
 	class AlignmentTextDialog;
 }
@@ -31,7 +29,7 @@
 	Q_OBJECT
 	
 	public:
-		explicit AlignmentTextDialog(DynamicElementTextItem *text, QWidget *parent = nullptr);
+		explicit AlignmentTextDialog(Qt::Alignment alignment, QWidget *parent = nullptr);
 		~AlignmentTextDialog();
 	
 		Qt::Alignment alignment() const;

Modified: trunk/sources/ui/dynamicelementtextmodel.cpp
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.cpp	2018-05-13 05:37:20 UTC (rev 5361)
+++ trunk/sources/ui/dynamicelementtextmodel.cpp	2018-05-13 19:03:38 UTC (rev 5362)
@@ -1406,7 +1406,7 @@
 			if(!deti)
 				break;
 			
-			AlignmentTextDialog *atd = new AlignmentTextDialog(deti, parent);
+			AlignmentTextDialog *atd = new AlignmentTextDialog(deti->alignment(), parent);
 			atd->setObjectName("alignment_text");
 			return atd;
 		}


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