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
6
7
8#include <QtWidgets/qpushbutton.h>
9#include <QtWidgets/qtoolbutton.h>
10
11#include <QtGui/qevent.h>
12#include <QtGui/qshortcut.h>
13
15
16namespace qdesigner_internal {
17 InPlaceWidgetHelper::InPlaceWidgetHelper(QWidget *editorWidget, QWidget *parentWidget, QDesignerFormWindowInterface *fw)
18 : QObject(nullptr),
19 m_editorWidget(editorWidget),
20 m_parentWidget(parentWidget),
21 m_noChildEvent(m_parentWidget->testAttribute(Qt::WA_NoChildEventsForParent))
22 {
23 m_editorWidget->setAttribute(Qt::WA_DeleteOnClose);
24 m_editorWidget->setParent(m_parentWidget->window());
25 m_parentWidget->installEventFilter(this);
26 m_editorWidget->installEventFilter(this);
27 connect(m_editorWidget, &QObject::destroyed,
28 fw->mainContainer(), QOverload<>::of(&QWidget::setFocus));
29 }
30
32 {
33 if (m_parentWidget)
34 m_parentWidget->setAttribute(Qt::WA_NoChildEventsForParent, m_noChildEvent);
35 }
36
38 if (m_parentWidget->metaObject()->indexOfProperty("alignment") != -1)
39 return Qt::Alignment(m_parentWidget->property("alignment").toInt());
40
41 if (qobject_cast<const QPushButton *>(m_parentWidget)
42 || qobject_cast<const QToolButton *>(m_parentWidget) /* tool needs to be more complex */)
43 return Qt::AlignHCenter;
44
45 return Qt::AlignJustify;
46 }
47
48
49 bool InPlaceWidgetHelper::eventFilter(QObject *object, QEvent *e)
50 {
51 if (object == m_parentWidget) {
52 if (e->type() == QEvent::Resize) {
53 const QResizeEvent *event = static_cast<const QResizeEvent*>(e);
54 const QPoint localPos = m_parentWidget->geometry().topLeft();
55 const QPoint globalPos = m_parentWidget->parentWidget() ? m_parentWidget->parentWidget()->mapToGlobal(localPos) : localPos;
56 const QPoint newPos = (m_editorWidget->parentWidget() ? m_editorWidget->parentWidget()->mapFromGlobal(globalPos) : globalPos)
57 + m_posOffset;
58 const QSize newSize = event->size() + m_sizeOffset;
59 m_editorWidget->setGeometry(QRect(newPos, newSize));
60 }
61 } else if (object == m_editorWidget) {
62 if (e->type() == QEvent::ShortcutOverride) {
63 if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
64 e->accept();
65 return false;
66 }
67 } else if (e->type() == QEvent::KeyPress) {
68 if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
69 e->accept();
70 m_editorWidget->close();
71 return true;
72 }
73 } else if (e->type() == QEvent::Show) {
74 const QPoint localPos = m_parentWidget->geometry().topLeft();
75 const QPoint globalPos = m_parentWidget->parentWidget() ? m_parentWidget->parentWidget()->mapToGlobal(localPos) : localPos;
76 const QPoint newPos = m_editorWidget->parentWidget() ? m_editorWidget->parentWidget()->mapFromGlobal(globalPos) : globalPos;
77 m_posOffset = m_editorWidget->geometry().topLeft() - newPos;
78 m_sizeOffset = m_editorWidget->size() - m_parentWidget->size();
79 }
80 }
81
82 return QObject::eventFilter(object, e);
83 }
84}
85
86QT_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.