[qet] [2026] Added startUserTransformation() and handleUserTransformation() methods to the CustomElementPart class and all of its subclasses.

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


Revision: 2026
Author:   xavier
Date:     2013-02-08 23:05:12 +0100 (Fri, 08 Feb 2013)
Log Message:
-----------
Added startUserTransformation() and handleUserTransformation() methods to the CustomElementPart class and all of its subclasses.

Modified Paths:
--------------
    trunk/sources/editor/customelementpart.cpp
    trunk/sources/editor/customelementpart.h
    trunk/sources/editor/partarc.cpp
    trunk/sources/editor/partarc.h
    trunk/sources/editor/partcircle.cpp
    trunk/sources/editor/partcircle.h
    trunk/sources/editor/partellipse.cpp
    trunk/sources/editor/partellipse.h
    trunk/sources/editor/partline.cpp
    trunk/sources/editor/partline.h
    trunk/sources/editor/partpolygon.cpp
    trunk/sources/editor/partpolygon.h
    trunk/sources/editor/partrectangle.cpp
    trunk/sources/editor/partrectangle.h
    trunk/sources/editor/partterminal.cpp
    trunk/sources/editor/partterminal.h
    trunk/sources/editor/parttext.cpp
    trunk/sources/editor/parttext.h
    trunk/sources/editor/parttextfield.cpp
    trunk/sources/editor/parttextfield.h

Modified: trunk/sources/editor/customelementpart.cpp
===================================================================
--- trunk/sources/editor/customelementpart.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/customelementpart.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -43,3 +43,33 @@
 QUndoStack &CustomElementPart::undoStack() const {
 	return(elementScene() -> undoStack());
 }
+
+/**
+	Helper method to map points in CustomElementPart::handleUserTransformation()
+	@param initial_selection_rect Selection rectangle when the movement started, in scene coordinates
+	@param new_selection_rect New selection rectangle, in scene coordinates
+	@param points List of points when the movement started, in scene coordinates.
+	@return The list of points mapped from initial_selection_rect to new_selection_rect
+*/
+QList<QPointF> CustomElementPart::mapPoints(const QRectF &initial_selection_rect, const QRectF &new_selection_rect, const QList<QPointF> &points) {
+	QList<QPointF> new_points;
+	if (!points.count()) return(new_points);
+	
+	// compare the new selection rectangle with the stored one to get the scaling ratio
+	qreal sx = new_selection_rect.width() / initial_selection_rect.width();
+	qreal sy = new_selection_rect.height() / initial_selection_rect.height();
+	
+	QPointF initial_top_left = initial_selection_rect.topLeft();
+	qreal new_top_left_x = new_selection_rect.x();
+	qreal new_top_left_y = new_selection_rect.y();
+	
+	foreach (QPointF point, points) {
+		QPointF point_offset = point - initial_top_left;
+		new_points << QPointF(
+			new_top_left_x + (point_offset.rx() * sx),
+			new_top_left_y + (point_offset.y() * sy)
+		);
+	}
+	
+	return(new_points);
+}

Modified: trunk/sources/editor/customelementpart.h
===================================================================
--- trunk/sources/editor/customelementpart.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/customelementpart.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -71,6 +71,14 @@
 		Typically, useless primitives are discarded when saving the element.
 	*/
 	virtual bool isUseless() const = 0;
+	/**
+		Inform this part a user-induced transformation is about to begin. This method can be used to save data required by handleUserTransformation().
+	*/
+	virtual void startUserTransformation(const QRectF &) = 0;
+	/**
+		Make this part fit into the provided rectangle.
+	*/
+	virtual void handleUserTransformation(const QRectF &, const QRectF &) = 0;
 	/// @return a pointer to the parent element editor
 	virtual QETElementEditor *elementEditor() const;
 	/**
@@ -86,5 +94,8 @@
 	virtual QString name() const = 0;
 	/// @return the name that will be used as XML tag when exporting the primitive
 	virtual QString xmlName() const = 0;
+	
+	protected:
+	QList<QPointF> mapPoints(const QRectF &, const QRectF &, const QList<QPointF> &);
 };
 #endif

Modified: trunk/sources/editor/partarc.cpp
===================================================================
--- trunk/sources/editor/partarc.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partarc.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -257,6 +257,24 @@
 }
 
 /**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartArc::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	saved_points_.clear();
+	saved_points_ << mapToScene(rect().topLeft()) << mapToScene(rect().bottomRight());
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartArc::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
+	setRect(QRectF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1))));
+}
+
+/**
 	@return le rectangle delimitant cette partie.
 */
 QRectF PartArc::boundingRect() const {

Modified: trunk/sources/editor/partarc.h
===================================================================
--- trunk/sources/editor/partarc.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partarc.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -60,8 +60,13 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	
 	protected:
 	QVariant itemChange(GraphicsItemChange, const QVariant &);
+	
+	private:
+	QList<QPointF> saved_points_;
 };
 #endif

Modified: trunk/sources/editor/partcircle.cpp
===================================================================
--- trunk/sources/editor/partcircle.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partcircle.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -198,3 +198,30 @@
 bool PartCircle::isUseless() const {
 	return(rect().isNull());
 }
+
+/**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartCircle::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	saved_center_ = mapToScene(rect().center());
+	saved_diameter_ = rect().width();
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartCircle::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QPointF new_center = mapPoints(initial_selection_rect, new_selection_rect, QList<QPointF>() << saved_center_).first();
+	
+	qreal sx = new_selection_rect.width() / initial_selection_rect.width();
+	qreal sy = new_selection_rect.height() / initial_selection_rect.height();
+	qreal smallest_scale_factor = sx > sy ? sy : sx;
+	qreal new_diameter = saved_diameter_ * smallest_scale_factor;
+	
+	QRectF new_rect(QPointF(), QSizeF(new_diameter, new_diameter));
+	new_rect.moveCenter(mapFromScene(new_center));
+	
+	setRect(new_rect);
+}

Modified: trunk/sources/editor/partcircle.h
===================================================================
--- trunk/sources/editor/partcircle.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partcircle.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -52,8 +52,14 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	
 	protected:
 	QVariant itemChange(GraphicsItemChange, const QVariant &);
+	
+	private:
+	QPointF saved_center_;
+	qreal saved_diameter_;
 };
 #endif

Modified: trunk/sources/editor/partellipse.cpp
===================================================================
--- trunk/sources/editor/partellipse.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partellipse.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -190,6 +190,25 @@
 }
 
 /**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartEllipse::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	// we keep track of our own rectangle at the moment in scene coordinates too
+	saved_points_.clear();
+	saved_points_ << mapToScene(rect().topLeft()) << mapToScene(rect().bottomRight());
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartEllipse::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
+	setRect(QRectF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1))));
+}
+
+/**
 	@return le rectangle delimitant cette partie.
 */
 QRectF PartEllipse::boundingRect() const {

Modified: trunk/sources/editor/partellipse.h
===================================================================
--- trunk/sources/editor/partellipse.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partellipse.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -51,8 +51,13 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	
 	protected:
 	QVariant itemChange(GraphicsItemChange, const QVariant &);
+	
+	private:
+	QList<QPointF> saved_points_;
 };
 #endif

Modified: trunk/sources/editor/partline.cpp
===================================================================
--- trunk/sources/editor/partline.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partline.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -477,6 +477,24 @@
 }
 
 /**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartLine::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	saved_points_.clear();
+	saved_points_ << sceneP1() << sceneP2();
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartLine::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
+	setLine(QLineF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1))));
+}
+
+/**
 	@param end_type nouveau type d'embout pour l'extremite 1
 */
 void PartLine::setFirstEndType(const QET::EndType &end_type) {

Modified: trunk/sources/editor/partline.h
===================================================================
--- trunk/sources/editor/partline.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partline.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -44,6 +44,7 @@
 	qreal first_length;
 	QET::EndType second_end;
 	qreal second_length;
+	QList<QPointF> saved_points_;
 	
 	// methods
 	public:
@@ -67,6 +68,8 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	virtual void setFirstEndType(const QET::EndType &);
 	virtual QET::EndType firstEndType() const;
 	virtual void setSecondEndType(const QET::EndType &);

Modified: trunk/sources/editor/partpolygon.cpp
===================================================================
--- trunk/sources/editor/partpolygon.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partpolygon.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -164,6 +164,23 @@
 }
 
 /**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartPolygon::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	saved_points_ = mapToScene(polygon()).toList();
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartPolygon::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
+	setPolygon(mapFromScene(QPolygonF(mapped_points.toVector())));
+}
+
+/**
 	@return le rectangle delimitant cette partie.
 */
 QRectF PartPolygon::boundingRect() const {

Modified: trunk/sources/editor/partpolygon.h
===================================================================
--- trunk/sources/editor/partpolygon.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partpolygon.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -56,9 +56,14 @@
 	void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	
 	protected:
 	QVariant itemChange(GraphicsItemChange, const QVariant &);
+	
+	private:
+	QList<QPointF> saved_points_;
 };
 
 /**

Modified: trunk/sources/editor/partrectangle.cpp
===================================================================
--- trunk/sources/editor/partrectangle.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partrectangle.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -197,6 +197,25 @@
 }
 
 /**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartRectangle::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	// we keep track of our own rectangle at the moment in scene coordinates too
+	saved_points_.clear();
+	saved_points_ << mapToScene(rect().topLeft()) << mapToScene(rect().bottomRight());
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartRectangle::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
+	setRect(QRectF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1))));
+}
+
+/**
 	@return le rectangle delimitant cette partie.
 */
 QRectF PartRectangle::boundingRect() const {

Modified: trunk/sources/editor/partrectangle.h
===================================================================
--- trunk/sources/editor/partrectangle.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partrectangle.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -51,8 +51,13 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	
 	protected:
 	QVariant itemChange(GraphicsItemChange, const QVariant &);
+	
+	private:
+	QList<QPointF> saved_points_;
 };
 #endif

Modified: trunk/sources/editor/partterminal.cpp
===================================================================
--- trunk/sources/editor/partterminal.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partterminal.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -219,3 +219,20 @@
 bool PartTerminal::isUseless() const {
 	return(false);
 }
+
+/**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartTerminal::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	saved_position_ = scenePos();
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartTerminal::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
+	QPointF mapped_point = mapPoints(initial_selection_rect, new_selection_rect, QList<QPointF>() << saved_position_).first();
+	setPos(mapped_point);
+}

Modified: trunk/sources/editor/partterminal.h
===================================================================
--- trunk/sources/editor/partterminal.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/partterminal.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -57,11 +57,16 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	
 	protected:
 	QVariant itemChange(GraphicsItemChange, const QVariant &);
 	
 	private:
 	void updateSecondPoint();
+	
+	private:
+	QPointF saved_position_;
 };
 #endif

Modified: trunk/sources/editor/parttext.cpp
===================================================================
--- trunk/sources/editor/parttext.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/parttext.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -286,6 +286,32 @@
 }
 
 /**
+	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_ = font().pointSize();
+}
+
+/**
+	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);
+	
+	// adjust the font size following the smallest scale factor
+	qreal sx = new_selection_rect.width() / initial_selection_rect.width();
+	qreal sy = new_selection_rect.height() / initial_selection_rect.height();
+	qreal smallest_scale_factor = sx > sy ? sy : sx;
+	qreal new_font_size = saved_font_size_ * smallest_scale_factor;
+	setProperty("size", qMax(1, qRound(new_font_size)));
+}
+
+/**
 	Dessine le texte statique.
 	@param painter QPainter a utiliser pour effectuer le rendu
 	@param qsogi   Pptions de dessin

Modified: trunk/sources/editor/parttext.h
===================================================================
--- trunk/sources/editor/parttext.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/parttext.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -55,6 +55,8 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget * = 0 );
 	
 	public slots:
@@ -72,5 +74,7 @@
 	void drawPoint(QPainter *, const QPointF &);
 #endif
 	QString previous_text;
+	QPointF saved_point_;
+	int saved_font_size_;
 };
 #endif

Modified: trunk/sources/editor/parttextfield.cpp
===================================================================
--- trunk/sources/editor/parttextfield.cpp	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/parttextfield.cpp	2013-02-08 22:05:12 UTC (rev 2026)
@@ -269,6 +269,31 @@
 }
 
 /**
+	Start the user-induced transformation, provided this primitive is contained
+	within the \a initial_selection_rect bounding rectangle.
+*/
+void PartTextField::startUserTransformation(const QRectF &initial_selection_rect) {
+	Q_UNUSED(initial_selection_rect)
+	saved_point_ = pos(); // scene coordinates, no need to mapFromScene()
+	saved_font_size_ = font().pointSize();
+}
+
+/**
+	Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
+*/
+void PartTextField::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);
+	
+	// adjust the font size following the smallest scale factor
+	qreal sx = new_selection_rect.width() / initial_selection_rect.width();
+	qreal sy = new_selection_rect.height() / initial_selection_rect.height();
+	qreal smallest_scale_factor = sx > sy ? sy : sx;
+	qreal new_font_size = saved_font_size_ * smallest_scale_factor;
+	setProperty("size", qMax(1, qRound(new_font_size)));
+}
+/**
 	Dessine le texte statique.
 	@param painter QPainter a utiliser pour effectuer le rendu
 	@param qsogi   Pptions de dessin

Modified: trunk/sources/editor/parttextfield.h
===================================================================
--- trunk/sources/editor/parttextfield.h	2013-02-08 22:05:08 UTC (rev 2025)
+++ trunk/sources/editor/parttextfield.h	2013-02-08 22:05:12 UTC (rev 2026)
@@ -61,6 +61,8 @@
 	virtual void setProperty(const QString &, const QVariant &);
 	virtual QVariant property(const QString &);
 	virtual bool isUseless() const;
+	virtual void startUserTransformation(const QRectF &);
+	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget * = 0 );
 	
 	public slots:
@@ -78,5 +80,7 @@
 	void drawPoint(QPainter *, const QPointF &);
 #endif
 	QString previous_text;
+	QPointF saved_point_;
+	int saved_font_size_;
 };
 #endif


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