Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
inplace_widget_helper.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
7
8
9#include <QtWidgets/qpushbutton.h>
10#include <QtWidgets/qtoolbutton.h>
11
12#include <QtGui/qevent.h>
13#include <QtGui/qshortcut.h>
14
16
17namespace qdesigner_internal {
18 InPlaceWidgetHelper::InPlaceWidgetHelper(QWidget *editorWidget, QWidget *parentWidget, QDesignerFormWindowInterface *fw)
19 : QObject(nullptr),
20 m_editorWidget(editorWidget),
21 m_parentWidget(parentWidget),
22 m_noChildEvent(m_parentWidget->testAttribute(Qt::WA_NoChildEventsForParent))
23 {
24 m_editorWidget->setAttribute(Qt::WA_DeleteOnClose);
25 m_editorWidget->setParent(m_parentWidget->window());
26 m_parentWidget->installEventFilter(this);
27 m_editorWidget->installEventFilter(this);
28 connect(m_editorWidget, &QObject::destroyed,
29 fw->mainContainer(), QOverload<>::of(&QWidget::setFocus));
30 }
31
33 {
34 if (m_parentWidget)
35 m_parentWidget->setAttribute(Qt::WA_NoChildEventsForParent, m_noChildEvent);
36 }
37
39 if (m_parentWidget->metaObject()->indexOfProperty("alignment") != -1)
40 return Qt::Alignment(m_parentWidget->property("alignment").toInt());
41
42 if (qobject_cast<const QPushButton *>(m_parentWidget)
43 || qobject_cast<const QToolButton *>(m_parentWidget) /* tool needs to be more complex */)
44 return Qt::AlignHCenter;
45
46 return Qt::AlignJustify;
47 }
48
49
50 bool InPlaceWidgetHelper::eventFilter(QObject *object, QEvent *e)
51 {
52 if (object == m_parentWidget) {
53 if (e->type() == QEvent::Resize) {
54 const QResizeEvent *event = static_cast<const QResizeEvent*>(e);
55 const QPoint localPos = m_parentWidget->geometry().topLeft();
56 const QPoint globalPos = m_parentWidget->parentWidget() ? m_parentWidget->parentWidget()->mapToGlobal(localPos) : localPos;
57 const QPoint newPos = (m_editorWidget->parentWidget() ? m_editorWidget->parentWidget()->mapFromGlobal(globalPos) : globalPos)
58 + m_posOffset;
59 const QSize newSize = event->size() + m_sizeOffset;
60 m_editorWidget->setGeometry(QRect(newPos, newSize));
61 }
62 } else if (object == m_editorWidget) {
63 if (e->type() == QEvent::ShortcutOverride) {
64 if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
65 e->accept();
66 return false;
67 }
68 } else if (e->type() == QEvent::KeyPress) {
69 if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
70 e->accept();
71 m_editorWidget->close();
72 return true;
73 }
74 } else if (e->type() == QEvent::Show) {
75 const QPoint localPos = m_parentWidget->geometry().topLeft();
76 const QPoint globalPos = m_parentWidget->parentWidget() ? m_parentWidget->parentWidget()->mapToGlobal(localPos) : localPos;
77 const QPoint newPos = m_editorWidget->parentWidget() ? m_editorWidget->parentWidget()->mapFromGlobal(globalPos) : globalPos;
78 m_posOffset = m_editorWidget->geometry().topLeft() - newPos;
79 m_sizeOffset = m_editorWidget->size() - m_parentWidget->size();
80 }
81 }
82
83 return QObject::eventFilter(object, e);
84 }
85}
86
87QT_END_NAMESPACE
bool eventFilter(QObject *object, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.