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 <QtWidgets/qaction.h>
30
31#include "buddyeditor_plugin.h"
32#include "buddyeditor_tool.h"
33
34#include <QtDesigner/abstractformwindow.h>
35#include <QtDesigner/abstractformwindowmanager.h>
36#include <QtDesigner/abstractformeditor.h>
37
38QT_BEGIN_NAMESPACE
39
40using namespace qdesigner_internal;
41
42BuddyEditorPlugin::BuddyEditorPlugin() = default;
43
44BuddyEditorPlugin::~BuddyEditorPlugin() = default;
45
46bool BuddyEditorPlugin::isInitialized() const
47{
48 return m_initialized;
49}
50
51void BuddyEditorPlugin::initialize(QDesignerFormEditorInterface *core)
52{
53 Q_ASSERT(!isInitialized());
54
55 m_action = new QAction(tr(s: "Edit Buddies"), this);
56 m_action->setObjectName(QStringLiteral("__qt_edit_buddies_action"));
57 QIcon buddyIcon = QIcon::fromTheme(QStringLiteral("designer-edit-buddy"),
58 fallback: QIcon(core->resourceLocation() + QStringLiteral("/buddytool.png")));
59 m_action->setIcon(buddyIcon);
60 m_action->setEnabled(false);
61
62 setParent(core);
63 m_core = core;
64 m_initialized = true;
65
66 connect(sender: core->formWindowManager(), signal: &QDesignerFormWindowManagerInterface::formWindowAdded,
67 receiver: this, slot: &BuddyEditorPlugin::addFormWindow);
68
69 connect(sender: core->formWindowManager(), signal: &QDesignerFormWindowManagerInterface::formWindowRemoved,
70 receiver: this, slot: &BuddyEditorPlugin::removeFormWindow);
71
72 connect(sender: core->formWindowManager(), signal: &QDesignerFormWindowManagerInterface::activeFormWindowChanged,
73 receiver: this, slot: &BuddyEditorPlugin::activeFormWindowChanged);
74}
75
76QDesignerFormEditorInterface *BuddyEditorPlugin::core() const
77{
78 return m_core;
79}
80
81void BuddyEditorPlugin::addFormWindow(QDesignerFormWindowInterface *formWindow)
82{
83 Q_ASSERT(formWindow != nullptr);
84 Q_ASSERT(m_tools.contains(formWindow) == false);
85
86 BuddyEditorTool *tool = new BuddyEditorTool(formWindow, this);
87 m_tools[formWindow] = tool;
88 connect(sender: m_action, signal: &QAction::triggered, receiver: tool->action(), slot: &QAction::trigger);
89 formWindow->registerTool(tool);
90}
91
92void BuddyEditorPlugin::removeFormWindow(QDesignerFormWindowInterface *formWindow)
93{
94 Q_ASSERT(formWindow != nullptr);
95 Q_ASSERT(m_tools.contains(formWindow) == true);
96
97 BuddyEditorTool *tool = m_tools.value(akey: formWindow);
98 m_tools.remove(akey: formWindow);
99 disconnect(sender: m_action, signal: &QAction::triggered, receiver: tool->action(), slot: &QAction::trigger);
100 // ### FIXME disable the tool
101
102 delete tool;
103}
104
105QAction *BuddyEditorPlugin::action() const
106{
107 return m_action;
108}
109
110void BuddyEditorPlugin::activeFormWindowChanged(QDesignerFormWindowInterface *formWindow)
111{
112 m_action->setEnabled(formWindow != nullptr);
113}
114
115QT_END_NAMESPACE
116

source code of qttools/src/designer/src/components/buddyeditor/buddyeditor_plugin.cpp