1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Designer of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "formwindow_dnditem.h" |
30 | #include "formwindow.h" |
31 | |
32 | #include <private/ui4_p.h> |
33 | #include <qdesigner_resource.h> |
34 | #include <qtresourcemodel_p.h> |
35 | |
36 | #include <QtDesigner/abstractformeditor.h> |
37 | #include <QtDesigner/private/ui4_p.h> |
38 | |
39 | #include <QtWidgets/qlabel.h> |
40 | #include <QtGui/qpixmap.h> |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | using namespace qdesigner_internal; |
45 | |
46 | static QWidget *decorationFromWidget(QWidget *w) |
47 | { |
48 | QLabel *label = new QLabel(nullptr, Qt::ToolTip); |
49 | QPixmap pm = w->grab(rectangle: QRect(0, 0, -1, -1)); |
50 | label->setPixmap(pm); |
51 | label->resize((QSizeF(pm.size()) / pm.devicePixelRatio()).toSize()); |
52 | |
53 | return label; |
54 | } |
55 | |
56 | static DomUI *widgetToDom(QWidget *widget, FormWindow *form) |
57 | { |
58 | QDesignerResource builder(form); |
59 | builder.setSaveRelative(false); |
60 | return builder.copy(selection: FormBuilderClipboard(widget)); |
61 | } |
62 | |
63 | FormWindowDnDItem::FormWindowDnDItem(QDesignerDnDItemInterface::DropType type, FormWindow *form, |
64 | QWidget *widget, const QPoint &global_mouse_pos) |
65 | : QDesignerDnDItem(type, form) |
66 | { |
67 | QWidget *decoration = decorationFromWidget(w: widget); |
68 | QPoint pos = widget->mapToGlobal(QPoint(0, 0)); |
69 | decoration->move(pos); |
70 | |
71 | init(ui: nullptr, widget, decoration, global_mouse_pos); |
72 | } |
73 | |
74 | DomUI *FormWindowDnDItem::domUi() const |
75 | { |
76 | DomUI *result = QDesignerDnDItem::domUi(); |
77 | if (result != nullptr) |
78 | return result; |
79 | FormWindow *form = qobject_cast<FormWindow*>(object: source()); |
80 | if (widget() == nullptr || form == nullptr) |
81 | return nullptr; |
82 | |
83 | QtResourceModel *resourceModel = form->core()->resourceModel(); |
84 | QtResourceSet *currentResourceSet = resourceModel->currentResourceSet(); |
85 | /* Short: |
86 | * We need to activate the original resourceSet associated with a form |
87 | * to properly generate the dom resource includes. |
88 | * Long: |
89 | * widgetToDom() calls copy() on QDesignerResource. It generates the |
90 | * Dom structure. In order to create DomResources properly we need to |
91 | * have the associated ResourceSet active (QDesignerResource::saveResources() |
92 | * queries the resource model for a qrc path for the given resource file: |
93 | * qrcFile = m_core->resourceModel()->qrcPath(ri->text()); |
94 | * This works only when the resource file comes from the active |
95 | * resourceSet */ |
96 | resourceModel->setCurrentResourceSet(resourceSet: form->resourceSet()); |
97 | |
98 | result = widgetToDom(widget: widget(), form); |
99 | const_cast<FormWindowDnDItem*>(this)->setDomUi(result); |
100 | resourceModel->setCurrentResourceSet(resourceSet: currentResourceSet); |
101 | return result; |
102 | } |
103 | |
104 | QT_END_NAMESPACE |
105 | |