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
qaxwidgettaskmenu.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
8
9#include <QtDesigner/abstractformwindow.h>
10#include <QtDesigner/abstractformwindowcursor.h>
11#include <QtDesigner/abstractformeditor.h>
12#include <QtDesigner/qextensionmanager.h>
13
14#include <QtAxContainer/qaxselect.h>
15
16#include <QtWidgets/qmessagebox.h>
17#include <QtGui/qundostack.h>
18
19#include <QtGui/qaction.h>
20
21#include <QtCore/qt_windows.h>
22#include <QtCore/quuid.h>
23
24#include <olectl.h>
25
27
28using namespace Qt::StringLiterals;
29
30/* SetControlCommand: An undo commands that sets a control bypassing
31 Designer's property system which cannot handle the changing
32 of the 'control' property's index and other cached information
33 when modifying it. */
34
36{
37public:
38 SetControlCommand(QDesignerAxWidget *ax, QDesignerFormWindowInterface *core, const QString &newClsid = QString());
39
40 virtual void redo() override { apply(m_newClsid); }
41 virtual void undo() override { apply(m_oldClsid); }
42
43private:
44 bool apply(const QString &clsid);
45
46 QDesignerAxWidget *m_axWidget;
47 QDesignerFormWindowInterface *m_formWindow;
48 QString m_oldClsid;
49 QString m_newClsid;
50};
51
52SetControlCommand::SetControlCommand(QDesignerAxWidget *ax, QDesignerFormWindowInterface *fw, const QString &newClsid) :
53 m_axWidget(ax),
54 m_formWindow(fw),
57{
58 if (m_newClsid.isEmpty())
59 setText(QDesignerAxWidget::tr("Reset control"));
60 else
61 setText(QDesignerAxWidget::tr("Set control"));
62}
63
64bool SetControlCommand::apply(const QString &clsid)
65{
66 if (m_oldClsid == m_newClsid)
67 return true;
68
69 QObject *ext = m_formWindow->core()->extensionManager()->extension(
70 m_axWidget, Q_TYPEID(QDesignerPropertySheetExtension));
71 auto sheet = qobject_cast<QAxWidgetPropertySheet *>(ext);
72 if (!sheet)
73 return false;
74
75 const bool hasClsid = !clsid.isEmpty();
76 const int index = sheet->indexOf(QLatin1String(QAxWidgetPropertySheet::controlPropertyName));
77 if (hasClsid)
78 sheet->setProperty(index, clsid);
79 else
80 sheet->reset(index);
81 return true;
82}
83
84// -------------------- QAxWidgetTaskMenu
85QAxWidgetTaskMenu::QAxWidgetTaskMenu(QDesignerAxWidget *object, QObject *parent) :
86 QObject(parent),
87 m_axwidget(object),
88 m_setAction(new QAction(tr("Set Control"), this)),
89 m_resetAction(new QAction(tr("Reset Control"), this))
90{
91 connect(m_setAction, &QAction::triggered, this, &QAxWidgetTaskMenu::setActiveXControl);
92 connect(m_resetAction, &QAction::triggered, this, &QAxWidgetTaskMenu::resetActiveXControl);
93 m_taskActions.push_back(m_setAction);
94 m_taskActions.push_back(m_resetAction);
95}
96
98
100{
101 const bool loaded = m_axwidget->loaded();
102 m_setAction->setEnabled(!loaded);
103 m_resetAction->setEnabled(loaded);
104 return m_taskActions;
105}
106
107void QAxWidgetTaskMenu::resetActiveXControl()
108{
109 auto formWin = QDesignerFormWindowInterface::findFormWindow(m_axwidget);
110 Q_ASSERT(formWin != nullptr);
111 formWin->commandHistory()->push(new SetControlCommand(m_axwidget, formWin));
112}
113
114void QAxWidgetTaskMenu::setActiveXControl()
115{
116 QAxSelect dialog(m_axwidget->topLevelWidget());
117 if (dialog.exec() != QDialog::Accepted)
118 return;
119
120 const auto clsid = QUuid::fromString(dialog.clsid());
121 QString key;
122
123 IClassFactory2 *cf2 = nullptr;
124 CoGetClassObject(clsid, CLSCTX_SERVER, 0, IID_IClassFactory2, reinterpret_cast<void **>(&cf2));
125
126 if (cf2) {
127 BSTR bKey;
128 HRESULT hres = cf2->RequestLicKey(0, &bKey);
129 if (hres == CLASS_E_NOTLICENSED) {
130 QMessageBox::warning(m_axwidget->topLevelWidget(), tr("Licensed Control"),
131 tr("The control requires a design-time license"));
132 cf2->Release();
133 return;
134 }
135
136 key = QString::fromWCharArray(bKey);
137 cf2->Release();
138 }
139
140 auto formWin = QDesignerFormWindowInterface::findFormWindow(m_axwidget);
141
142 Q_ASSERT(formWin != nullptr);
143 QString value = clsid.toString();
144 if (!key.isEmpty())
145 value += u':' + key;
146 formWin->commandHistory()->push(new SetControlCommand(m_axwidget, formWin, value));
147}
148
149QT_END_NAMESPACE
static const char * controlPropertyName
virtual ~QAxWidgetTaskMenu()
QList< QAction * > taskActions() const override
virtual void redo() override
Applies a change to the document.
virtual void undo() override
Reverts a change to the document.
SetControlCommand(QDesignerAxWidget *ax, QDesignerFormWindowInterface *core, const QString &newClsid=QString())
Combined button and popup list for selecting options.