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