mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 12:40:35 +01:00
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 git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4498 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -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 @@ 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);
|
||||
|
||||
@@ -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 @@ 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;
|
||||
|
||||
122
sources/ElementsCollection/elementstreeview.cpp
Normal file
122
sources/ElementsCollection/elementstreeview.cpp
Normal file
@@ -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);
|
||||
}
|
||||
41
sources/ElementsCollection/elementstreeview.h
Normal file
41
sources/ElementsCollection/elementstreeview.h
Normal file
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user