QElectroTech  0.70
elementstreeview.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "elementstreeview.h"
19 #include "elementcollectionitem.h"
20 #include "elementslocation.h"
21 #include "elementfactory.h"
22 #include "qeticons.h"
23 #include "element.h"
24 
25 #include <QDrag>
26 #include <QStandardItemModel>
27 
28 static int MAX_DND_PIXMAP_WIDTH = 500;
29 static int MAX_DND_PIXMAP_HEIGHT = 375;
30 
36  QTreeView(parent)
37 {}
38 
44 void ElementsTreeView::startDrag(Qt::DropActions supportedActions)
45 {
46  QModelIndex index = currentIndex();
47 
48  if (!index.isValid()) {
49  QTreeView::startDrag(supportedActions);
50  return;
51  }
52 
53  if (QStandardItemModel *qsim = static_cast<QStandardItemModel *>(model())) {
54  if (ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsim->itemFromIndex(index))) {
55  ElementsLocation loc (eci->collectionPath());
56  if (loc.exist()) {
57  startElementDrag(loc);
58  return;
59  }
60  }
61  }
62  QTreeView::startDrag(supportedActions);
63 }
64 
71 {
72  if (!location.exist())
73  return;
74 
75  QDrag *drag = new QDrag(this);
76 
77  QString location_str = location.toString();
78  QMimeData *mime_data = new QMimeData();
79  mime_data->setText(location_str);
80 
81  if (location.isDirectory())
82  {
83  mime_data->setData("application/x-qet-category-uri", location_str.toLatin1());
84  drag->setPixmap(QET::Icons::Folder.pixmap(22, 22));
85  }
86  else if (location.isElement())
87  {
88  mime_data->setData("application/x-qet-element-uri", location_str.toLatin1());
89 
90  //Build the element for set the pixmap of the QDrag
91  int elmt_creation_state;
92  Element *temp_elmt = ElementFactory::Instance()->createElement(location, nullptr, &elmt_creation_state);
93  if (elmt_creation_state)
94  {
95  delete temp_elmt;
96  return;
97  }
98 
99  QPixmap elmt_pixmap(temp_elmt->pixmap());
100  QPoint elmt_hotspot(temp_elmt->hotspot());
101 
102  //Adjust the size of the pixmap if he is too big
103  QPoint elmt_pixmap_size(elmt_pixmap.width(), elmt_pixmap.height());
104  if (elmt_pixmap.width() > MAX_DND_PIXMAP_WIDTH || elmt_pixmap.height() > MAX_DND_PIXMAP_HEIGHT)
105  {
106  elmt_pixmap = elmt_pixmap.scaled(MAX_DND_PIXMAP_WIDTH, MAX_DND_PIXMAP_HEIGHT, Qt::KeepAspectRatio);
107  elmt_hotspot = QPoint(
108  elmt_hotspot.x() * elmt_pixmap.width() / elmt_pixmap_size.x(),
109  elmt_hotspot.y() * elmt_pixmap.height() / elmt_pixmap_size.y()
110  );
111  }
112 
113  drag->setPixmap(elmt_pixmap);
114  drag->setHotSpot(elmt_hotspot);
115 
116 
117  delete temp_elmt;
118  }
119 
120  drag->setMimeData(mime_data);
121  drag->exec(Qt::CopyAction);
122 }
The ElementCollectionItem class This class represent a item (a directory or an element) in a element ...
QString toString() const
static int MAX_DND_PIXMAP_WIDTH
QIcon Folder
Definition: qeticons.cpp:94
static int MAX_DND_PIXMAP_HEIGHT
QPixmap pixmap()
Element::pixmap.
Definition: element.cpp:262
Element * createElement(const ElementsLocation &, QGraphicsItem *=nullptr, int *=nullptr)
ElementFactory::createElement.
QPoint hotspot() const
Definition: element.cpp:254
ElementsTreeView(QWidget *parent=nullptr)
ElementsTreeView::ElementsTreeView.
virtual void startElementDrag(const ElementsLocation &location)
ElementsTreeView::startElementDrag Build a QDrag according to the content of .
bool exist() const
ElementsLocation::exist.
bool isDirectory() const
ElementsLocation::isDirectory.
bool isElement() const
ElementsLocation::isElement.
void startDrag(Qt::DropActions supportedActions) override
ElementsTreeView::startDrag Reimplemented from QTreeView.
static ElementFactory * Instance()