diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index ff1642aa2..b4d5bc0d8 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -28,9 +28,9 @@ #include "xmlprojectelementcollectionitem.h" #include "qetproject.h" #include "qetelementeditor.h" +#include "elementstreeview.h" #include -#include #include #include #include @@ -112,7 +112,7 @@ void ElementsCollectionWidget::setUpWidget() 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); diff --git a/sources/ElementsCollection/elementscollectionwidget.h b/sources/ElementsCollection/elementscollectionwidget.h index ff49cdaa5..9f5697bee 100644 --- a/sources/ElementsCollection/elementscollectionwidget.h +++ b/sources/ElementsCollection/elementscollectionwidget.h @@ -22,13 +22,13 @@ #include 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 @@ class ElementsCollectionWidget : public QWidget 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; diff --git a/sources/ElementsCollection/elementstreeview.cpp b/sources/ElementsCollection/elementstreeview.cpp new file mode 100644 index 000000000..ad764b428 --- /dev/null +++ b/sources/ElementsCollection/elementstreeview.cpp @@ -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 . +*/ +#include "elementstreeview.h" +#include "elementcollectionitem.h" +#include "elementslocation.h" +#include "elementfactory.h" +#include "qeticons.h" +#include "element.h" + +#include + +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(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); +} diff --git a/sources/ElementsCollection/elementstreeview.h b/sources/ElementsCollection/elementstreeview.h new file mode 100644 index 000000000..408841112 --- /dev/null +++ b/sources/ElementsCollection/elementstreeview.h @@ -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 . +*/ +#ifndef ELEMENTSTREEVIEW_H +#define ELEMENTSTREEVIEW_H + +#include + +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 diff --git a/sources/diagramevent/diagrameventaddelement.cpp b/sources/diagramevent/diagrameventaddelement.cpp index bf7936ea8..c1cd14a2e 100644 --- a/sources/diagramevent/diagrameventaddelement.cpp +++ b/sources/diagramevent/diagrameventaddelement.cpp @@ -169,17 +169,13 @@ void DiagramEventAddElement::init() */ 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; diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 2b55a4900..3410923c1 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -139,21 +139,6 @@ QETProject::~QETProject() delete undo_stack_; } -/** - * @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 @@ -752,8 +737,7 @@ ElementsLocation QETProject::importElement(ElementsLocation &location) if (location.isFileSystem()) { import_path = "import/" + location.collectionPath(false); } - - if (location.isProject()) { + else if (location.isProject()) { if (location.project() == this) { return location; } diff --git a/sources/qetproject.h b/sources/qetproject.h index 58d0d5d6a..225532070 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -75,8 +75,6 @@ class QETProject : public QObject 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: