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 "abstractformwindow.h" |
30 | #include "inplace_widget_helper.h" |
31 | |
32 | #include <QtGui/qevent.h> |
33 | #include <QtWidgets/qpushbutton.h> |
34 | #include <QtWidgets/qtoolbutton.h> |
35 | #include <QtWidgets/qshortcut.h> |
36 | |
37 | QT_BEGIN_NAMESPACE |
38 | |
39 | namespace qdesigner_internal { |
40 | InPlaceWidgetHelper::InPlaceWidgetHelper(QWidget *editorWidget, QWidget *parentWidget, QDesignerFormWindowInterface *fw) |
41 | : QObject(nullptr), |
42 | m_editorWidget(editorWidget), |
43 | m_parentWidget(parentWidget), |
44 | m_noChildEvent(m_parentWidget->testAttribute(attribute: Qt::WA_NoChildEventsForParent)) |
45 | { |
46 | m_editorWidget->setAttribute(Qt::WA_DeleteOnClose); |
47 | m_editorWidget->setParent(m_parentWidget->window()); |
48 | m_parentWidget->installEventFilter(filterObj: this); |
49 | m_editorWidget->installEventFilter(filterObj: this); |
50 | connect(sender: m_editorWidget, signal: &QObject::destroyed, |
51 | receiver: fw->mainContainer(), slot: QOverload<>::of(ptr: &QWidget::setFocus)); |
52 | } |
53 | |
54 | InPlaceWidgetHelper::~InPlaceWidgetHelper() |
55 | { |
56 | if (m_parentWidget) |
57 | m_parentWidget->setAttribute(Qt::WA_NoChildEventsForParent, on: m_noChildEvent); |
58 | } |
59 | |
60 | Qt::Alignment InPlaceWidgetHelper::alignment() const { |
61 | if (m_parentWidget->metaObject()->indexOfProperty(name: "alignment" ) != -1) |
62 | return Qt::Alignment(m_parentWidget->property(name: "alignment" ).toInt()); |
63 | |
64 | if (qobject_cast<const QPushButton *>(object: m_parentWidget) |
65 | || qobject_cast<const QToolButton *>(object: m_parentWidget) /* tool needs to be more complex */) |
66 | return Qt::AlignHCenter; |
67 | |
68 | return Qt::AlignJustify; |
69 | } |
70 | |
71 | |
72 | bool InPlaceWidgetHelper::eventFilter(QObject *object, QEvent *e) |
73 | { |
74 | if (object == m_parentWidget) { |
75 | if (e->type() == QEvent::Resize) { |
76 | const QResizeEvent *event = static_cast<const QResizeEvent*>(e); |
77 | const QPoint localPos = m_parentWidget->geometry().topLeft(); |
78 | const QPoint globalPos = m_parentWidget->parentWidget() ? m_parentWidget->parentWidget()->mapToGlobal(localPos) : localPos; |
79 | const QPoint newPos = (m_editorWidget->parentWidget() ? m_editorWidget->parentWidget()->mapFromGlobal(globalPos) : globalPos) |
80 | + m_posOffset; |
81 | const QSize newSize = event->size() + m_sizeOffset; |
82 | m_editorWidget->setGeometry(QRect(newPos, newSize)); |
83 | } |
84 | } else if (object == m_editorWidget) { |
85 | if (e->type() == QEvent::ShortcutOverride) { |
86 | if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) { |
87 | e->accept(); |
88 | return false; |
89 | } |
90 | } else if (e->type() == QEvent::KeyPress) { |
91 | if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) { |
92 | e->accept(); |
93 | m_editorWidget->close(); |
94 | return true; |
95 | } |
96 | } else if (e->type() == QEvent::Show) { |
97 | const QPoint localPos = m_parentWidget->geometry().topLeft(); |
98 | const QPoint globalPos = m_parentWidget->parentWidget() ? m_parentWidget->parentWidget()->mapToGlobal(localPos) : localPos; |
99 | const QPoint newPos = m_editorWidget->parentWidget() ? m_editorWidget->parentWidget()->mapFromGlobal(globalPos) : globalPos; |
100 | m_posOffset = m_editorWidget->geometry().topLeft() - newPos; |
101 | m_sizeOffset = m_editorWidget->size() - m_parentWidget->size(); |
102 | } |
103 | } |
104 | |
105 | return QObject::eventFilter(watched: object, event: e); |
106 | } |
107 | } |
108 | |
109 | QT_END_NAMESPACE |
110 | |