[qet] [1593] Diagram editor: users may now enter visualisation mode by pressing Ctrl and Shift.

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


Revision: 1593
Author:   xavier
Date:     2012-03-24 15:34:25 +0100 (Sat, 24 Mar 2012)
Log Message:
-----------
Diagram editor: users may now enter visualisation mode by pressing Ctrl and Shift.

Modified Paths:
--------------
    branches/0.3/sources/diagramview.cpp
    branches/0.3/sources/diagramview.h

Modified: branches/0.3/sources/diagramview.cpp
===================================================================
--- branches/0.3/sources/diagramview.cpp	2012-03-20 16:25:39 UTC (rev 1592)
+++ branches/0.3/sources/diagramview.cpp	2012-03-24 14:34:25 UTC (rev 1593)
@@ -43,7 +43,7 @@
 	@param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise
 	@param parent Le QWidget parent de cette vue de schema
 */
-DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : QGraphicsView(parent), is_adding_text(false) {
+DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : QGraphicsView(parent), is_adding_text(false), is_moving_view_(false) {
 	setAttribute(Qt::WA_DeleteOnClose, true);
 	setInteractive(true);
 	
@@ -364,7 +364,7 @@
 	@param clipboard_mode Type de presse-papier a prendre en compte
 */
 void DiagramView::paste(const QPointF &pos, QClipboard::Mode clipboard_mode) {
-	if (scene -> isReadOnly()) return;
+	if (!isInteractive() || scene -> isReadOnly()) return;
 	
 	QString texte_presse_papier = QApplication::clipboard() -> text(clipboard_mode);
 	if ((texte_presse_papier).isEmpty()) return;
@@ -397,15 +397,21 @@
 	 *  le clic pour ajouter un champ de texte independant
 */
 void DiagramView::mousePressEvent(QMouseEvent *e) {
-	if (e -> buttons() == Qt::MidButton) {
-		paste(mapToScene(e -> pos()), QClipboard::Selection);
-	} else {
-		if (!scene -> isReadOnly() && is_adding_text && e -> buttons() == Qt::LeftButton) {
-			addDiagramTextAtPos(mapToScene(e -> pos()));
-			is_adding_text = false;
+	if (fresh_focus_in_) {
+		switchToVisualisationModeIfNeeded(e);
+		fresh_focus_in_ = false;
+	}
+	if (isInteractive() && !scene -> isReadOnly()) {
+		if (e -> buttons() == Qt::MidButton) {
+			paste(mapToScene(e -> pos()), QClipboard::Selection);
+		} else {
+			if (is_adding_text && e -> buttons() == Qt::LeftButton) {
+				addDiagramTextAtPos(mapToScene(e -> pos()));
+				is_adding_text = false;
+			}
 		}
-		QGraphicsView::mousePressEvent(e);
 	}
+	QGraphicsView::mousePressEvent(e);
 }
 
 /**
@@ -426,6 +432,35 @@
 }
 
 /**
+	Handles "Focus in" events. Reimplemented here to store the fact the focus
+	was freshly acquired again using the mouse. This information is later used
+	in DiagramView::mousePressEvent().
+*/
+void DiagramView::focusInEvent(QFocusEvent *e) {
+	if (e -> reason() == Qt::MouseFocusReason) {
+		fresh_focus_in_ = true;
+	}
+}
+
+/**
+	Handles "key press" events. Reimplemented here to switch to visualisation
+	mode if needed.
+*/
+void DiagramView::keyPressEvent(QKeyEvent *e) {
+	switchToVisualisationModeIfNeeded(e);
+	QGraphicsView::keyPressEvent(e);
+}
+
+/**
+	Handles "key release" events. Reimplemented here to switch to selection
+	mode if needed.
+*/
+void DiagramView::keyReleaseEvent(QKeyEvent *e) {
+	switchToSelectionModeIfNeeded(e);
+	QGraphicsView::keyReleaseEvent(e);
+}
+
+/**
 	@return le titre de cette vue ; cela correspond au titre du schema
 	visualise precede de la mention "Schema". Si le titre du schema est vide,
 	la mention "Schema sans titre" est utilisee
@@ -1005,9 +1040,7 @@
 	// vue plutot que de remonter vers les QMenu / QAction
 	if (
 		e -> type() == QEvent::ShortcutOverride &&
-		scene -> hasFocus() &&
-		scene -> focusItem() &&
-		scene -> focusItem() -> isSelected()
+		selectedItemHasFocus()
 	) {
 		e -> accept();
 		return(true);
@@ -1016,6 +1049,66 @@
 }
 
 /**
+	Switch to visualisation mode if the user is pressing Ctrl and Shift.
+	@return true if the view was switched to visualisation mode, false
+	otherwise.
+*/
+bool DiagramView::switchToVisualisationModeIfNeeded(QInputEvent *e) {
+	if (isCtrlShifting(e) && !selectedItemHasFocus()) {
+		if (dragMode() != QGraphicsView::ScrollHandDrag) {
+			is_moving_view_ = true;
+			setVisualisationMode();
+			return(true);
+		}
+	}
+	return(false);
+}
+
+/**
+	Switch back to selection mode if the user is not pressing Ctrl and Shift.
+	@return true if the view was switched to selection mode, false
+	otherwise.
+*/
+bool DiagramView::switchToSelectionModeIfNeeded(QInputEvent *e) {
+	if (is_moving_view_ && !selectedItemHasFocus() && !isCtrlShifting(e)) {
+		setSelectionMode();
+		is_moving_view_ = false;
+		return(true);
+	}
+	return(false);
+}
+
+/**
+	@return true if the user is pressing Ctrl and Shift simultaneously.
+*/
+bool DiagramView::isCtrlShifting(QInputEvent *e) {
+	bool result = false;
+	// note: QInputEvent::modifiers and QKeyEvent::modifiers() do not return the
+	// same values, hence the casts
+	if (e -> type() == QEvent::KeyPress || e -> type() == QEvent::KeyRelease) {
+		if (QKeyEvent *ke = static_cast<QKeyEvent *>(e)) {
+			result = (ke -> modifiers() == (Qt::ControlModifier | Qt::ShiftModifier));
+		}
+	} else if (e -> type() >= QEvent::MouseButtonPress && e -> type() <= QEvent::MouseMove) {
+		if (QMouseEvent *me = static_cast<QMouseEvent *>(e)) {
+			result = (me -> modifiers() == (Qt::ControlModifier | Qt::ShiftModifier));
+		}
+	}
+	return(result);
+}
+
+/**
+	@return true if there is a selected item and that item has the focus.
+*/
+bool DiagramView::selectedItemHasFocus() {
+	return(
+		scene -> hasFocus() &&
+		scene -> focusItem() &&
+		scene -> focusItem() -> isSelected()
+	);
+}
+
+/**
 	Passe le DiagramView en mode "ajout de texte". Un clic cree alors un
 	nouveau champ de texte.
 */
@@ -1031,6 +1124,8 @@
 	@return le champ de texte ajoute
 */
 IndependentTextItem *DiagramView::addDiagramTextAtPos(const QPointF &pos) {
+	if (!isInteractive() || scene -> isReadOnly()) return(0);
+	
 	// cree un nouveau champ de texte
 	IndependentTextItem *iti = new IndependentTextItem("_");
 	

Modified: branches/0.3/sources/diagramview.h
===================================================================
--- branches/0.3/sources/diagramview.h	2012-03-20 16:25:39 UTC (rev 1592)
+++ branches/0.3/sources/diagramview.h	2012-03-24 14:34:25 UTC (rev 1593)
@@ -47,6 +47,8 @@
 	QAction *find_element_;
 	QPoint paste_here_pos;
 	bool is_adding_text;
+	bool is_moving_view_;               ///< Indicate whether the visualisation mode has been enabled due to mouse/keyboard interactions
+	bool fresh_focus_in_;               ///< Indicate the focus was freshly gained
 	ElementsLocation next_location_;
 	QPoint next_position_;
 	
@@ -71,7 +73,14 @@
 	virtual void mouseDoubleClickEvent(QMouseEvent *);
 	virtual void contextMenuEvent(QContextMenuEvent *);
 	virtual void wheelEvent(QWheelEvent *);
+	virtual void focusInEvent(QFocusEvent *);
+	virtual void keyPressEvent(QKeyEvent *);
+	virtual void keyReleaseEvent(QKeyEvent *);
 	virtual bool event(QEvent *);
+	virtual bool switchToVisualisationModeIfNeeded(QInputEvent *e);
+	virtual bool switchToSelectionModeIfNeeded(QInputEvent *e);
+	virtual bool isCtrlShifting(QInputEvent *);
+	virtual bool selectedItemHasFocus();
 	
 	private:
 	void mousePressEvent(QMouseEvent *);


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