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
qdesigner.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
4// designer
5#include "qdesigner.h"
10#include "mainwindow.h"
11#include <QtDesigner/abstractintegration.h>
12#include <QtDesigner/abstractformeditor.h>
13#include <qdesigner_propertysheet_p.h>
14
15#include <QtGui/qevent.h>
16#include <QtWidgets/qmessagebox.h>
17#include <QtGui/qicon.h>
18#include <QtWidgets/qerrormessage.h>
19#include <QtCore/qmetaobject.h>
20#include <QtCore/qdir.h>
21#include <QtCore/qfile.h>
22#include <QtCore/qlocale.h>
23#include <QtCore/qtextstream.h>
24#include <QtCore/qtimer.h>
25#include <QtCore/qtranslator.h>
26#include <QtCore/qfileinfo.h>
27#include <QtCore/qdebug.h>
28#include <QtCore/qcommandlineparser.h>
29#include <QtCore/qcommandlineoption.h>
30#include <QtCore/qvariant.h>
31
32#include <QtDesigner/QDesignerComponents>
33
34QT_BEGIN_NAMESPACE
35
36using namespace Qt::StringLiterals;
37
38static constexpr auto designerApplicationName = "Designer"_L1;
39static constexpr auto designerDisplayName = "Qt Widgets Designer"_L1;
40static constexpr auto designerWarningPrefix = "Designer: "_L1;
42
43static void designerMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
44{
45 // Only Designer warnings are displayed as box
46 QDesigner *designerApp = qDesigner;
47 if (type != QtWarningMsg || !designerApp || !msg.startsWith(designerWarningPrefix)) {
48 previousMessageHandler(type, context, msg);
49 return;
50 }
51 designerApp->showErrorMessage(msg);
52}
53
54QDesigner::QDesigner(int &argc, char **argv)
56{
57 setOrganizationName(u"QtProject"_s);
58 QGuiApplication::setApplicationDisplayName(designerDisplayName);
59 setApplicationName(designerApplicationName);
60 QDesignerComponents::initializeResources();
61
62#if !defined(Q_OS_MACOS) && !defined(Q_OS_WIN)
63 setWindowIcon(QIcon(u":/qt-project.org/designer/images/designer.png"_s));
64#endif
65}
66
68{
69 delete m_workbench;
70 delete m_server;
71 delete m_client;
72}
73
74void QDesigner::showErrorMessage(const QString &message)
75{
76 // strip the prefix
77 const QString qMessage =
78 message.right(message.size() - int(designerWarningPrefix.size()));
79 // If there is no main window yet, just store the message.
80 // The QErrorMessage would otherwise be hidden by the main window.
81 if (m_mainWindow) {
82 showErrorMessageBox(qMessage);
83 } else {
84 const QMessageLogContext emptyContext;
85 previousMessageHandler(QtWarningMsg, emptyContext, message); // just in case we crash
86 m_initializationErrors += qMessage;
87 m_initializationErrors += u'\n';
88 }
89}
90
91void QDesigner::showErrorMessageBox(const QString &msg)
92{
93 // Manually suppress consecutive messages.
94 // This happens if for example sth is wrong with custom widget creation.
95 // The same warning will be displayed by Widget box D&D and form Drop
96 // while trying to create instance.
97 if (m_errorMessageDialog && m_lastErrorMessage == msg)
98 return;
99
100 if (!m_errorMessageDialog) {
101 m_lastErrorMessage.clear();
102 m_errorMessageDialog = new QErrorMessage(m_mainWindow);
103 const QString title = QCoreApplication::translate("QDesigner", "%1 - warning").arg(designerApplicationName);
104 m_errorMessageDialog->setWindowTitle(title);
105 m_errorMessageDialog->setMinimumSize(QSize(600, 250));
106 }
107 m_errorMessageDialog->showMessage(msg);
108 m_lastErrorMessage = msg;
109}
110
112{
113 return m_workbench;
114}
115
117{
118 return m_server;
119}
120
121static void showHelp(QCommandLineParser &parser, const QString &errorMessage = QString())
122{
123 QString text;
124 QTextStream str(&text);
125 str << "<html><head/><body>";
126 if (!errorMessage.isEmpty())
127 str << "<p>" << errorMessage << "</p>";
128 str << "<pre>" << parser.helpText().toHtmlEscaped() << "</pre></body></html>";
129 QMessageBox box(errorMessage.isEmpty() ? QMessageBox::Information : QMessageBox::Warning,
130 QGuiApplication::applicationDisplayName(), text,
131 QMessageBox::Ok);
132 box.setTextInteractionFlags(Qt::TextBrowserInteraction);
133 box.exec();
134}
135
136static inline QDesigner::ParseArgumentsResult
137 parseDesignerCommandLineArguments(QCommandLineParser &parser, Options *options,
138 QString *errorMessage)
139{
140 parser.setApplicationDescription(u"Qt Widgets Designer " QT_VERSION_STR "\n\nUI designer for QWidget-based applications."_s);
141 const QCommandLineOption helpOption = parser.addHelpOption();
142 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
143 const QCommandLineOption serverOption(u"server"_s,
144 u"Server mode"_s);
145 parser.addOption(serverOption);
146 const QCommandLineOption clientOption(u"client"_s,
147 u"Client mode"_s,
148 u"port"_s);
149 parser.addOption(clientOption);
150 const QCommandLineOption webHelpOption(u"web-help"_s, u"Use the Web documentation"_s);
151 parser.addOption(webHelpOption);
152 const QCommandLineOption pythonHelpOption(u"python-help"_s, u"Use the Python documentation"_s);
153 parser.addOption(pythonHelpOption);
154 const QCommandLineOption resourceDirOption(u"resourcedir"_s,
155 u"Resource directory"_s,
156 u"directory"_s);
157 parser.addOption(resourceDirOption);
158 const QCommandLineOption internalDynamicPropertyOption(u"enableinternaldynamicproperties"_s,
159 u"Enable internal dynamic properties"_s);
160 parser.addOption(internalDynamicPropertyOption);
161 const QCommandLineOption pluginPathsOption(u"plugin-path"_s,
162 u"Default plugin path list"_s,
163 u"path"_s);
164 parser.addOption(pluginPathsOption);
165
166 const QCommandLineOption qtVersionOption(u"qt-version"_s,
167 u"Qt Version for writing .ui files"_s,
168 u"version"_s);
169 parser.addOption(qtVersionOption);
170
171 parser.addPositionalArgument(u"files"_s,
172 u"The UI files to open."_s);
173
174 if (!parser.parse(QCoreApplication::arguments())) {
175 *errorMessage = parser.errorText();
176 return QDesigner::ParseArgumentsError;
177 }
178
179 if (parser.isSet(helpOption))
180 return QDesigner::ParseArgumentsHelpRequested;
181 // There is no way to retrieve the complete help text from QCommandLineParser,
182 // so, call process() to display it.
183 if (parser.isSet(u"help-all"_s))
184 parser.process(QCoreApplication::arguments()); // exits
185 options->server = parser.isSet(serverOption);
186 if (parser.isSet(clientOption)) {
187 bool ok = false;
188 options->clientPort = parser.value(clientOption).toUShort(&ok);
189 if (!ok) {
190 *errorMessage = u"Non-numeric argument specified for -client"_s;
191 return QDesigner::ParseArgumentsError;
192 }
193 }
194 if (parser.isSet(webHelpOption))
196 else if (parser.isSet(pythonHelpOption))
198 if (parser.isSet(resourceDirOption))
199 options->resourceDir = parser.value(resourceDirOption);
200 const auto pluginPathValues = parser.values(pluginPathsOption);
201 for (const auto &pluginPath : pluginPathValues)
202 options->pluginPaths.append(pluginPath.split(QDir::listSeparator(), Qt::SkipEmptyParts));
203
204 if (parser.isSet(qtVersionOption))
205 options->qtVersion = QVersionNumber::fromString(parser.value(qtVersionOption));
206
207 options->enableInternalDynamicProperties = parser.isSet(internalDynamicPropertyOption);
208 options->files = parser.positionalArguments();
209 return QDesigner::ParseArgumentsSuccess;
210}
211
213{
214 QString errorMessage;
215 Options options;
216 QCommandLineParser parser;
217 const ParseArgumentsResult result = parseDesignerCommandLineArguments(parser, &options, &errorMessage);
218 if (result != ParseArgumentsSuccess) {
219 showHelp(parser, errorMessage);
220 return result;
221 }
222 // initialize the sub components
223 if (options.clientPort)
224 m_client = new QDesignerClient(options.clientPort, this);
225 if (options.server) {
226 m_server = new QDesignerServer();
227 printf("%d\n", m_server->serverPort());
228 fflush(stdout);
229 }
230 if (options.enableInternalDynamicProperties)
231 QDesignerPropertySheet::setInternalDynamicPropertiesEnabled(true);
232
233 std::unique_ptr<QTranslator> designerTranslator(new QTranslator(this));
234 if (designerTranslator->load(QLocale(), u"designer"_s, u"_"_s, options.resourceDir)) {
235 installTranslator(designerTranslator.release());
236 std::unique_ptr<QTranslator> qtTranslator(new QTranslator(this));
237 if (qtTranslator->load(QLocale(), u"qt"_s, u"_"_s, options.resourceDir))
238 installTranslator(qtTranslator.release());
239 }
240
241 m_workbench = new QDesignerWorkbench(options);
242
243 emit initialized();
244 previousMessageHandler = qInstallMessageHandler(designerMessageHandler); // Warn when loading faulty forms
245 Q_ASSERT(previousMessageHandler);
246
247 bool suppressNewFormShow = m_workbench->readInBackup();
248
249 for (auto fileName : std::as_const(options.files)) {
250 // Ensure absolute paths for recent file list to be unique
251 const QFileInfo fi(fileName);
252 if (fi.exists() && fi.isRelative())
253 fileName = fi.absoluteFilePath();
254 m_workbench->readInForm(fileName);
255 }
256
257 if (m_workbench->formWindowCount() > 0)
258 suppressNewFormShow = true;
259
260 if (options.qtVersion.has_value()) {
261#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
262 m_workbench->core()->integration()->setQtVersion(options.qtVersion.value());
263#else
264 auto version = QVariant::fromValue(options.qtVersion.value());
265 m_workbench->core()->integration()->setProperty("qtVersion", version);
266#endif
267 }
268
269 // Show up error box with parent now if something went wrong
270 if (m_initializationErrors.isEmpty()) {
271 if (!isServerOrClientEnabled() && !suppressNewFormShow)
272 m_workbench->showNewForm();
273 } else {
274 showErrorMessageBox(m_initializationErrors);
275 m_initializationErrors.clear();
276 }
277 return result;
278}
279
281{
282 return m_server || m_client;
283}
284
285bool QDesigner::event(QEvent *ev)
286{
287 bool eaten = false;
288 switch (ev->type()) {
289 case QEvent::FileOpen:
290 m_workbench->readInForm(static_cast<QFileOpenEvent *>(ev)->file());
291 m_workbench->requestActivate();
292 eaten = true;
293 break;
294 case QEvent::Close: {
295 auto *closeEvent = static_cast<QCloseEvent *>(ev);
296 closeEvent->setAccepted(m_workbench->handleClose());
297 if (closeEvent->isAccepted()) {
298 // We're going down, make sure that we don't get our settings saved twice.
299 if (m_mainWindow)
300 m_mainWindow->setCloseEventPolicy(MainWindowBase::AcceptCloseEvents);
301 QApplication::event(ev);
302 }
303 eaten = true;
304 break;
305 }
306 default:
307 eaten = QApplication::event(ev);
308 break;
309 }
310 return eaten;
311}
312
314{
315 m_mainWindow = tw;
316}
317
319{
320 return m_mainWindow;
321}
322
323QT_END_NAMESPACE
QDesignerFormEditorInterface * core() const
ParseArgumentsResult parseCommandLineArguments()
QDesigner(int &argc, char **argv)
Definition qdesigner.cpp:54
bool event(QEvent *ev) override
\reimp
QDesignerServer * server() const
void setMainWindow(MainWindowBase *tw)
QDesignerWorkbench * workbench() const
bool isServerOrClientEnabled() const
~QDesigner() override
Definition qdesigner.cpp:67
MainWindowBase * mainWindow() const
HelpClientType
Definition helpclient.h:16
static constexpr auto designerWarningPrefix
Definition qdesigner.cpp:40
static QDesigner::ParseArgumentsResult parseDesignerCommandLineArguments(QCommandLineParser &parser, Options *options, QString *errorMessage)
static void showHelp(QCommandLineParser &parser, const QString &errorMessage=QString())
static void designerMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Definition qdesigner.cpp:43
static constexpr auto designerApplicationName
Definition qdesigner.cpp:38
static QtMessageHandler previousMessageHandler
Definition qdesigner.cpp:41
static constexpr auto designerDisplayName
Definition qdesigner.cpp:39
#define qDesigner
Definition qdesigner.h:19
bool server
Definition qdesigner.h:37
bool enableInternalDynamicProperties
Definition qdesigner.h:39
HelpClientType helpMode
Definition qdesigner.h:40