[qet] [4498] Bugfix : can't add element dragged from an embedded collection

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


Revision: 4498
Author:   blacksun
Date:     2016-05-22 16:51:09 +0200 (Sun, 22 May 2016)
Log Message:
-----------
Bugfix : can't add element dragged from an embedded collection
New element panel, D&D : the pixmap of dragged element is used instead of the Qt default pixmap 

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementscollectionwidget.cpp
    trunk/sources/ElementsCollection/elementscollectionwidget.h
    trunk/sources/diagramevent/diagrameventaddelement.cpp
    trunk/sources/qetproject.cpp
    trunk/sources/qetproject.h

Added Paths:
-----------
    trunk/sources/ElementsCollection/elementstreeview.cpp
    trunk/sources/ElementsCollection/elementstreeview.h

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-05-22 11:08:48 UTC (rev 4497)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-05-22 14:51:09 UTC (rev 4498)
@@ -28,9 +28,9 @@
 #include "xmlprojectelementcollectionitem.h"
 #include "qetproject.h"
 #include "qetelementeditor.h"
+#include "elementstreeview.h"
 
 #include <QVBoxLayout>
-#include <QTreeView>
 #include <QMenu>
 #include <QDesktopServices>
 #include <QUrl>
@@ -112,7 +112,7 @@
 	m_main_vlayout->addWidget(m_search_field);
 
 		//Setup the tree view
-	m_tree_view = new QTreeView(this);
+	m_tree_view = new ElementsTreeView(this);
 	m_tree_view->setHeaderHidden(true);
 	m_tree_view->setIconSize(QSize(50, 50));
 	m_tree_view->setDragDropMode(QAbstractItemView::DragDrop);

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.h
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.h	2016-05-22 11:08:48 UTC (rev 4497)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.h	2016-05-22 14:51:09 UTC (rev 4498)
@@ -22,13 +22,13 @@
 #include <QModelIndex>
 
 class ElementsCollectionModel;
-class QTreeView;
 class QVBoxLayout;
 class QMenu;
 class QLineEdit;
 class ElementCollectionItem;
 class QProgressBar;
 class QETProject;
+class ElementsTreeView;
 
 /**
  * @brief The ElementsCollectionWidget class
@@ -73,7 +73,7 @@
     private:
         ElementsCollectionModel *m_model;
 		QLineEdit *m_search_field;
-        QTreeView *m_tree_view;
+		ElementsTreeView *m_tree_view;
         QVBoxLayout *m_main_vlayout;
 		QMenu *m_context_menu;
 		QModelIndex m_index_at_context_menu;

Added: trunk/sources/ElementsCollection/elementstreeview.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementstreeview.cpp	                        (rev 0)
+++ trunk/sources/ElementsCollection/elementstreeview.cpp	2016-05-22 14:51:09 UTC (rev 4498)
@@ -0,0 +1,122 @@
+/*
+	Copyright 2006-2016 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 "elementstreeview.h"
+#include "elementcollectionitem.h"
+#include "elementslocation.h"
+#include "elementfactory.h"
+#include "qeticons.h"
+#include "element.h"
+
+#include <QDrag>
+
+static int MAX_DND_PIXMAP_WIDTH = 500;
+static int MAX_DND_PIXMAP_HEIGHT = 375;
+
+/**
+ * @brief ElementsTreeView::ElementsTreeView
+ * @param parent
+ */
+ElementsTreeView::ElementsTreeView(QWidget *parent) :
+	QTreeView(parent)
+{}
+
+/**
+ * @brief ElementsTreeView::startDrag
+ * Reimplemented from QTreeView
+ * @param supportedActions
+ */
+void ElementsTreeView::startDrag(Qt::DropActions supportedActions)
+{
+	QModelIndex index = currentIndex();
+
+	if (!index.isValid()) {
+		QTreeView::startDrag(supportedActions);
+		return;
+	}
+
+	ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(index.internalPointer());
+
+	if (!eci) {
+		QTreeView::startDrag(supportedActions);
+		return;
+	}
+
+	ElementsLocation loc (eci->collectionPath());
+	if (loc.exist())
+		startElementDrag(loc);
+	else
+		QTreeView::startDrag(supportedActions);
+}
+
+/**
+ * @brief ElementsTreeView::startElementDrag
+ * Build a QDrag according to the content of @location
+ * @param location : location to use for create the content of the QDrag
+ */
+void ElementsTreeView::startElementDrag(const ElementsLocation &location)
+{
+	if (!location.exist())
+		return;
+
+	QDrag *drag = new QDrag(this);
+
+	QString location_str = location.toString();
+	QMimeData *mime_data = new QMimeData();
+	mime_data->setText(location_str);
+
+	if (location.isDirectory())
+	{
+		mime_data->setData("application/x-qet-category-uri", location_str.toLatin1());
+		drag->setPixmap(QET::Icons::Folder.pixmap(22, 22));
+	}
+	else if (location.isElement())
+	{
+		mime_data->setData("application/x-qet-element-uri", location_str.toLatin1());
+
+			//Build the element for set the pixmap of the QDrag
+		int elmt_creation_state;
+		Element *temp_elmt = ElementFactory::Instance()->createElement(location, 0, &elmt_creation_state);
+		if (elmt_creation_state)
+		{
+			delete temp_elmt;
+			return;
+		}
+
+		QPixmap elmt_pixmap(temp_elmt->pixmap());
+		QPoint elmt_hotspot(temp_elmt->hotspot());
+
+			//Adjust the size of the pixmap if he is too big
+		QPoint elmt_pixmap_size(elmt_pixmap.width(), elmt_pixmap.height());
+		if (elmt_pixmap.width() > MAX_DND_PIXMAP_WIDTH || elmt_pixmap.height() > MAX_DND_PIXMAP_HEIGHT)
+		{
+			elmt_pixmap = elmt_pixmap.scaled(MAX_DND_PIXMAP_WIDTH, MAX_DND_PIXMAP_HEIGHT, Qt::KeepAspectRatio);
+			elmt_hotspot = QPoint(
+				elmt_hotspot.x() * elmt_pixmap.width() / elmt_pixmap_size.x(),
+				elmt_hotspot.y() * elmt_pixmap.height() / elmt_pixmap_size.y()
+			);
+		}
+
+		drag->setPixmap(elmt_pixmap);
+		drag->setHotSpot(elmt_hotspot);
+
+		delete temp_elmt;
+	}
+
+	drag->setMimeData(mime_data);
+	drag->exec(Qt::MoveAction | Qt::CopyAction);
+}

Added: trunk/sources/ElementsCollection/elementstreeview.h
===================================================================
--- trunk/sources/ElementsCollection/elementstreeview.h	                        (rev 0)
+++ trunk/sources/ElementsCollection/elementstreeview.h	2016-05-22 14:51:09 UTC (rev 4498)
@@ -0,0 +1,41 @@
+/*
+	Copyright 2006-2016 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/>.
+*/
+#ifndef ELEMENTSTREEVIEW_H
+#define ELEMENTSTREEVIEW_H
+
+#include <QTreeView>
+
+class ElementsLocation;
+
+/**
+ * @brief The ElementsTreeView class
+ * This class just reimplement startDrag from QTreeView, for set a custom pixmap.
+ * This class must be used when the tree view have an ElementsCollectionModel as model.
+ * The pixmap used is the pixmap of the dragged element or a directory pixmap.
+ */
+class ElementsTreeView : public QTreeView
+{
+	public:
+		ElementsTreeView(QWidget *parent = nullptr);
+
+	protected:
+		virtual void startDrag(Qt::DropActions supportedActions);
+		virtual void startElementDrag(const ElementsLocation &location);
+};
+
+#endif // ELEMENTSTREEVIEW_H

Modified: trunk/sources/diagramevent/diagrameventaddelement.cpp
===================================================================
--- trunk/sources/diagramevent/diagrameventaddelement.cpp	2016-05-22 11:08:48 UTC (rev 4497)
+++ trunk/sources/diagramevent/diagrameventaddelement.cpp	2016-05-22 14:51:09 UTC (rev 4498)
@@ -169,18 +169,14 @@
  */
 bool DiagramEventAddElement::buildElement()
 {
-	if (QETProject::integrateElementToProject(m_location, m_diagram -> project()))
-	{
-		ElementsLocation loc = m_diagram->project()->importElement(m_location);
-		if (loc.exist()) {
-			m_integrate_path = loc.projectCollectionPath();
-		}
-		else {
-			qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element.";
-			return false;
-		}
-
+	ElementsLocation import_loc = m_diagram->project()->importElement(m_location);
+	if (import_loc.exist()) {
+		m_integrate_path = import_loc.projectCollectionPath();
 	}
+	else {
+		qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element.";
+		return false;
+	}
 
 	int state;
 	ElementsLocation loc(m_integrate_path);

Modified: trunk/sources/qetproject.cpp
===================================================================
--- trunk/sources/qetproject.cpp	2016-05-22 11:08:48 UTC (rev 4497)
+++ trunk/sources/qetproject.cpp	2016-05-22 14:51:09 UTC (rev 4498)
@@ -140,21 +140,6 @@
 }
 
 /**
- * @brief QETProject::integrateElementToProject
- * Return true if we must to integarte the element to the project otherwise false
- * @param location : element location
- * @param project : project to test
- * @return
- */
-bool QETProject::integrateElementToProject(const ElementsLocation &location, const QETProject *project)
-{
-	if (location.isFileSystem()) {return true;}
-	if (location.isProject() && (location.project() != project)) {return true;}
-
-	return false;
-}
-
-/**
 	Cette methode peut etre utilisee pour tester la bonne ouverture d'un projet
 	@return l'etat du projet
 	@see ProjectState
@@ -752,8 +737,7 @@
 	if (location.isFileSystem()) {
 		import_path = "import/" + location.collectionPath(false);
 	}
-
-	if (location.isProject()) {
+	else if (location.isProject()) {
 		if (location.project() == this) {
 			return location;
 		}

Modified: trunk/sources/qetproject.h
===================================================================
--- trunk/sources/qetproject.h	2016-05-22 11:08:48 UTC (rev 4497)
+++ trunk/sources/qetproject.h	2016-05-22 14:51:09 UTC (rev 4498)
@@ -75,8 +75,6 @@
 		ProjectParsingFailed  = 4, /// the parsing of the XML content failed
 		FileOpenDiscard       = 5  /// the user cancelled the file opening
 	};
-
-	static bool integrateElementToProject (const ElementsLocation &location, const QETProject *project);
 	
 		// methods
 	public:


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