2016-05-22 14:51:09 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2016-05-22 14:51:09 +00:00
|
|
|
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"
|
2020-12-08 19:57:35 +01:00
|
|
|
|
|
|
|
#include "../qetgraphicsitem/element.h"
|
2020-12-09 15:28:43 +01:00
|
|
|
#include "../qeticons.h"
|
2016-05-22 14:51:09 +00:00
|
|
|
#include "elementcollectionitem.h"
|
|
|
|
#include "elementfactory.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
#include "elementslocation.h"
|
2016-05-22 14:51:09 +00:00
|
|
|
|
|
|
|
#include <QDrag>
|
2016-06-05 16:34:46 +00:00
|
|
|
#include <QStandardItemModel>
|
2016-05-22 14:51:09 +00:00
|
|
|
|
|
|
|
static int MAX_DND_PIXMAP_WIDTH = 500;
|
|
|
|
static int MAX_DND_PIXMAP_HEIGHT = 375;
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsTreeView::ElementsTreeView
|
|
|
|
@param parent
|
|
|
|
*/
|
2016-05-22 14:51:09 +00:00
|
|
|
ElementsTreeView::ElementsTreeView(QWidget *parent) :
|
|
|
|
QTreeView(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsTreeView::startDrag
|
|
|
|
Reimplemented from QTreeView
|
|
|
|
@param supportedActions
|
|
|
|
*/
|
2016-05-22 14:51:09 +00:00
|
|
|
void ElementsTreeView::startDrag(Qt::DropActions supportedActions)
|
|
|
|
{
|
|
|
|
QModelIndex index = currentIndex();
|
|
|
|
|
|
|
|
if (!index.isValid()) {
|
|
|
|
QTreeView::startDrag(supportedActions);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-30 15:24:29 +00:00
|
|
|
if (QStandardItemModel *qsim = static_cast<QStandardItemModel *>(model())) {
|
|
|
|
if (ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsim->itemFromIndex(index))) {
|
2016-06-05 16:34:46 +00:00
|
|
|
ElementsLocation loc (eci->collectionPath());
|
|
|
|
if (loc.exist()) {
|
|
|
|
startElementDrag(loc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 14:51:09 +00:00
|
|
|
}
|
2016-06-05 16:34:46 +00:00
|
|
|
QTreeView::startDrag(supportedActions);
|
2016-05-22 14:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsTreeView::startElementDrag
|
2020-08-18 20:07:55 +02:00
|
|
|
Build a QDrag according to the content of location
|
2020-08-16 11:19:36 +02:00
|
|
|
@param location : location to use for create the content of the QDrag
|
|
|
|
*/
|
2016-05-22 14:51:09 +00:00
|
|
|
void ElementsTreeView::startElementDrag(const ElementsLocation &location)
|
|
|
|
{
|
2020-10-30 21:07:12 +01:00
|
|
|
if (! location.exist()) return;
|
2016-05-22 14:51:09 +00:00
|
|
|
|
2020-10-30 21:07:12 +01:00
|
|
|
QScopedPointer<QDrag> drag(new QDrag(this));
|
2016-05-22 14:51:09 +00:00
|
|
|
|
|
|
|
QString location_str = location.toString();
|
2018-07-30 15:24:29 +00:00
|
|
|
QMimeData *mime_data = new QMimeData();
|
2016-05-22 14:51:09 +00:00
|
|
|
mime_data->setText(location_str);
|
|
|
|
|
|
|
|
if (location.isDirectory())
|
|
|
|
{
|
2020-08-18 20:08:32 +02:00
|
|
|
mime_data->setData("application/x-qet-category-uri",
|
|
|
|
location_str.toLatin1());
|
2016-05-22 14:51:09 +00:00
|
|
|
drag->setPixmap(QET::Icons::Folder.pixmap(22, 22));
|
|
|
|
}
|
|
|
|
else if (location.isElement())
|
|
|
|
{
|
2020-08-18 20:08:32 +02:00
|
|
|
mime_data->setData("application/x-qet-element-uri",
|
|
|
|
location_str.toLatin1());
|
2016-05-22 14:51:09 +00:00
|
|
|
|
|
|
|
//Build the element for set the pixmap of the QDrag
|
|
|
|
int elmt_creation_state;
|
2020-10-30 21:07:12 +01:00
|
|
|
QScopedPointer<Element> temp_elmt(
|
|
|
|
ElementFactory::Instance()->createElement(
|
|
|
|
location,
|
|
|
|
nullptr,
|
|
|
|
&elmt_creation_state));
|
|
|
|
if (elmt_creation_state) { return; }
|
2016-05-22 14:51:09 +00:00
|
|
|
|
|
|
|
QPixmap elmt_pixmap(temp_elmt->pixmap());
|
|
|
|
QPoint elmt_hotspot(temp_elmt->hotspot());
|
|
|
|
|
|
|
|
//Adjust the size of the pixmap if he is too big
|
2020-08-18 20:08:32 +02:00
|
|
|
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)
|
2016-05-22 14:51:09 +00:00
|
|
|
{
|
2020-08-18 20:08:32 +02:00
|
|
|
elmt_pixmap = elmt_pixmap.scaled(
|
|
|
|
MAX_DND_PIXMAP_WIDTH,
|
|
|
|
MAX_DND_PIXMAP_HEIGHT,
|
|
|
|
Qt::KeepAspectRatio);
|
2016-05-22 14:51:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
drag->setMimeData(mime_data);
|
2016-05-22 16:13:08 +00:00
|
|
|
drag->exec(Qt::CopyAction);
|
2016-05-22 14:51:09 +00:00
|
|
|
}
|