7#include <qdesigner_utils_p.h>
19#include <pluginmanager_p.h>
20#include <qdesigner_formbuilder_p.h>
21#include <iconloader_p.h>
22#include <previewmanager_p.h>
23#include <codedialog_p.h>
24#include <qdesigner_formwindowmanager_p.h>
27#include <QtDesigner/abstractformeditor.h>
28#include <QtDesigner/abstractformwindow.h>
29#include <QtDesigner/abstractintegration.h>
30#include <QtDesigner/abstractlanguage.h>
31#include <QtDesigner/abstractmetadatabase.h>
32#include <QtDesigner/abstractformwindowmanager.h>
33#include <QtDesigner/abstractformwindowcursor.h>
34#include <QtDesigner/abstractformeditorplugin.h>
35#include <QtDesigner/qextensionmanager.h>
37#include <QtDesigner/private/shared_settings_p.h>
38#include <QtDesigner/private/formwindowbase_p.h>
40#include <QtWidgets/qstylefactory.h>
41#include <QtWidgets/qfiledialog.h>
42#include <QtWidgets/qmenu.h>
43#include <QtWidgets/qmessagebox.h>
44#include <QtWidgets/qmdisubwindow.h>
45#include <QtWidgets/qpushbutton.h>
46#include <QtWidgets/qstatusbar.h>
48#include <QtGui/qaction.h>
49#include <QtGui/qactiongroup.h>
50#include <QtGui/qevent.h>
51#include <QtGui/qicon.h>
52#include <QtGui/qimage.h>
53#include <QtGui/qpixmap.h>
54#include <QtGui/qscreen.h>
55#if defined(QT_PRINTSUPPORT_LIB)
56# include <QtPrintSupport/qtprintsupportglobal.h>
57# if QT_CONFIG(printer) && QT_CONFIG(printdialog)
58# include <QtPrintSupport/qprinter.h>
59# include <QtPrintSupport/qprintdialog.h>
63#include <QtGui/qpainter.h>
64#include <QtGui/qtransform.h>
65#include <QtGui/qcursor.h>
67#include <QtCore/qdir.h>
68#include <QtCore/qsize.h>
69#include <QtCore/qlibraryinfo.h>
70#include <QtCore/qbuffer.h>
71#include <QtCore/qpluginloader.h>
72#include <QtCore/qdebug.h>
73#include <QtCore/qtimer.h>
74#include <QtCore/qtextstream.h>
75#include <QtCore/qmetaobject.h>
76#include <QtCore/qfileinfo.h>
77#include <QtCore/qsavefile.h>
78#include <QtXml/qdom.h>
86using namespace Qt::StringLiterals;
91# define NONMODAL_PREVIEW
96 auto *rc =
new QAction(parent);
97 rc->setSeparator(
true);
103 auto *rc =
new QActionGroup(parent);
104 rc->setExclusive(exclusive);
110 for (QAction *a : actions)
111 a->setShortcutContext(Qt::ApplicationShortcut);
116 return QDesignerActions::tr(
"Saved %1.").arg(fileName);
121 return QDesignerActions::tr(
"Designer UI files (*.%1);;All Files (*)").arg(extension);
125 const QDir& backupDir);
128 std::optional<QDir> alternativeDir = {})
130 QString contents = alternativeDir.has_value()
131 ? fixResourceFileBackupPath(fw, alternativeDir.value()) : fw->contents();
132 if (
const auto *fwb = qobject_cast<
const qdesigner_internal::FormWindowBase *>(fw)) {
133 if (fwb->lineTerminatorMode() == qdesigner_internal::FormWindowBase::CRLFLineTerminator)
134 contents.replace(u'\n',
"\r\n"_L1);
136 return contents.toUtf8();
141 auto *result =
new QFileDialog(parent, QDesignerActions::tr(
"Save Form As"),
142 dir, fileDialogFilters(extension));
143 result->setAcceptMode(QFileDialog::AcceptSave);
144 result->setDefaultSuffix(extension);
149 : QObject(workbench),
150 m_workbench(workbench),
153 m_helpClient(HelpClient::create(options.helpMode)),
154 m_backupTimer(
new QTimer(
this)),
155 m_fileActions(createActionGroup(
this)),
156 m_recentFilesActions(createActionGroup(
this)),
157 m_editActions(createActionGroup(
this)),
158 m_formActions(createActionGroup(
this)),
159 m_settingsActions(createActionGroup(
this)),
160 m_windowActions(createActionGroup(
this)),
161 m_toolActions(createActionGroup(
this,
true)),
162 m_editWidgetsAction(
new QAction(tr(
"Edit Widgets"),
this)),
163 m_newFormAction(
new QAction(qdesigner_internal::createIconSet(QIcon::ThemeIcon::DocumentNew,
165 tr(
"&New..."),
this)),
166 m_openFormAction(
new QAction(qdesigner_internal::createIconSet(QIcon::ThemeIcon::DocumentOpen,
168 tr(
"&Open..."),
this)),
169 m_saveFormAction(
new QAction(qdesigner_internal::createIconSet(QIcon::ThemeIcon::DocumentSave,
172 m_saveFormAsAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentSaveAs),
173 tr(
"Save &As..."),
this)),
174 m_saveAllFormsAction(
new QAction(tr(
"Save A&ll"),
this)),
175 m_saveFormAsTemplateAction(
new QAction(tr(
"Save As &Template..."),
this)),
176 m_closeFormAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::WindowClose),
177 tr(
"&Close"),
this)),
178 m_savePreviewImageAction(
new QAction(tr(
"Save &Image..."),
this)),
179 m_printPreviewAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentPrint),
180 tr(
"&Print..."),
this)),
181 m_quitAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::ApplicationExit),
183 m_viewCppCodeAction(
new QAction(tr(
"View &C++ Code..."),
this)),
184 m_viewPythonCodeAction(
new QAction(tr(
"View &Python Code..."),
this)),
185 m_minimizeAction(
new QAction(tr(
"&Minimize"),
this)),
186 m_bringAllToFrontSeparator(createSeparator(
this)),
187 m_bringAllToFrontAction(
new QAction(tr(
"Bring All to Front"),
this)),
188 m_windowListSeparatorAction(createSeparator(
this)),
189 m_preferencesAction(
new QAction(tr(
"Preferences..."),
this)),
190 m_appFontAction(
new QAction(tr(
"Additional Fonts..."),
this))
192 Q_ASSERT(m_core !=
nullptr);
193 auto *ifwm = qobject_cast<qdesigner_internal::QDesignerFormWindowManager *>(m_core->formWindowManager());
195 m_previewManager = ifwm->previewManager();
196 m_previewFormAction = ifwm->action(QDesignerFormWindowManagerInterface::DefaultPreviewAction);
197 m_styleActions = ifwm->actionGroup(QDesignerFormWindowManagerInterface::StyledPreviewActionGroup);
198 connect(ifwm, &QDesignerFormWindowManagerInterface::formWindowSettingsChanged,
201 m_editWidgetsAction->setObjectName(u"__qt_edit_widgets_action"_s);
202 m_newFormAction->setObjectName(u"__qt_new_form_action"_s);
203 m_openFormAction->setObjectName(u"__qt_open_form_action"_s);
204 m_saveFormAction->setObjectName(u"__qt_save_form_action"_s);
205 m_saveFormAsAction->setObjectName(u"__qt_save_form_as_action"_s);
206 m_saveAllFormsAction->setObjectName(u"__qt_save_all_forms_action"_s);
207 m_saveFormAsTemplateAction->setObjectName(u"__qt_save_form_as_template_action"_s);
208 m_closeFormAction->setObjectName(u"__qt_close_form_action"_s);
209 m_quitAction->setObjectName(u"__qt_quit_action"_s);
210 m_previewFormAction->setObjectName(u"__qt_preview_form_action"_s);
211 m_viewCppCodeAction->setObjectName(u"__qt_preview_cpp_code_action"_s);
212 m_viewPythonCodeAction->setObjectName(u"__qt_preview_python_code_action"_s);
213 m_minimizeAction->setObjectName(u"__qt_minimize_action"_s);
214 m_bringAllToFrontAction->setObjectName(u"__qt_bring_all_to_front_action"_s);
215 m_preferencesAction->setObjectName(u"__qt_preferences_action"_s);
217 m_helpActions = createHelpActions();
223 QDesignerFormWindowManagerInterface *formWindowManager = m_core->formWindowManager();
224 Q_ASSERT(formWindowManager !=
nullptr);
229 m_newFormAction->setShortcut(QKeySequence::New);
231 m_fileActions->addAction(m_newFormAction);
233 m_openFormAction->setShortcut(QKeySequence::Open);
235 m_fileActions->addAction(m_openFormAction);
237 m_fileActions->addAction(createRecentFilesMenu());
238 m_fileActions->addAction(createSeparator(
this));
240 m_saveFormAction->setShortcut(QKeySequence::Save);
241 connect(m_saveFormAction, &QAction::triggered,
this,
242 QOverload<>::of(&QDesignerActions::saveForm));
243 m_fileActions->addAction(m_saveFormAction);
245 connect(m_saveFormAsAction, &QAction::triggered,
this,
246 QOverload<>::of(&QDesignerActions::saveFormAs));
247 m_fileActions->addAction(m_saveFormAsAction);
250 m_saveAllFormsAction->setShortcut(tr(
"ALT+CTRL+S"));
252 m_saveAllFormsAction->setShortcut(tr(
"CTRL+SHIFT+S"));
254 connect(m_saveAllFormsAction, &QAction::triggered,
this, &
QDesignerActions::saveAllForms);
255 m_fileActions->addAction(m_saveAllFormsAction);
257 connect(m_saveFormAsTemplateAction, &QAction::triggered,
this, &
QDesignerActions::saveFormAsTemplate);
258 m_fileActions->addAction(m_saveFormAsTemplateAction);
260 m_fileActions->addAction(createSeparator(
this));
262 m_printPreviewAction->setShortcut(QKeySequence::Print);
263 connect(m_printPreviewAction, &QAction::triggered,
this, &
QDesignerActions::printPreviewImage);
264 m_fileActions->addAction(m_printPreviewAction);
265 m_printPreviewAction->setObjectName(u"__qt_print_action"_s);
267 connect(m_savePreviewImageAction, &QAction::triggered,
this, &
QDesignerActions::savePreviewImage);
268 m_savePreviewImageAction->setObjectName(u"__qt_saveimage_action"_s);
269 m_fileActions->addAction(m_savePreviewImageAction);
270 m_fileActions->addAction(createSeparator(
this));
272 m_closeFormAction->setShortcut(QKeySequence::Close);
273 connect(m_closeFormAction, &QAction::triggered,
this, &
QDesignerActions::closeForm);
274 m_fileActions->addAction(m_closeFormAction);
277 m_fileActions->addAction(createSeparator(
this));
279 m_quitAction->setShortcuts(QKeySequence::Quit);
280 m_quitAction->setMenuRole(QAction::QuitRole);
281 connect(m_quitAction, &QAction::triggered,
this, &
QDesignerActions::shutdown);
282 m_fileActions->addAction(m_quitAction);
287 QAction *undoAction = formWindowManager->action(QDesignerFormWindowManagerInterface::UndoAction);
288 undoAction->setObjectName(u"__qt_undo_action"_s);
289 undoAction->setShortcut(QKeySequence::Undo);
290 m_editActions->addAction(undoAction);
292 QAction *redoAction = formWindowManager->action(QDesignerFormWindowManagerInterface::RedoAction);
293 redoAction->setObjectName(u"__qt_redo_action"_s);
294 redoAction->setShortcut(QKeySequence::Redo);
295 m_editActions->addAction(redoAction);
297 m_editActions->addAction(createSeparator(
this));
299#if QT_CONFIG(clipboard)
300 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::CutAction));
301 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::CopyAction));
302 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::PasteAction));
304 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::DeleteAction));
306 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SelectAllAction));
308 m_editActions->addAction(createSeparator(
this));
310 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::LowerAction));
311 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::RaiseAction));
320 m_editWidgetsAction->setCheckable(
true);
321 QList<QKeySequence> shortcuts;
322 shortcuts.append(QKeySequence(Qt::Key_F3));
323 shortcuts.append(QKeySequence(Qt::Key_Escape));
324 m_editWidgetsAction->setShortcuts(shortcuts);
325 m_editWidgetsAction->setIcon(qdesigner_internal::createIconSet(
"widgettool.png"_L1));
326 connect(m_editWidgetsAction, &QAction::triggered,
this, &
QDesignerActions::editWidgetsSlot);
327 m_editWidgetsAction->setChecked(
true);
328 m_editWidgetsAction->setEnabled(
false);
330 m_toolActions->addAction(m_editWidgetsAction);
332 connect(formWindowManager, &qdesigner_internal::QDesignerFormWindowManager::activeFormWindowChanged,
333 this, &QDesignerActions::activeFormWindowChanged);
335 const QObjectList builtinPlugins = QPluginLoader::staticInstances()
336 + m_core->pluginManager()->instances();
337 for (QObject *plugin : builtinPlugins) {
338 if (QDesignerFormEditorPluginInterface *formEditorPlugin = qobject_cast<QDesignerFormEditorPluginInterface*>(plugin)) {
339 if (QAction *action = formEditorPlugin->action()) {
340 m_toolActions->addAction(action);
341 action->setProperty(QDesignerActions::defaultToolbarPropertyName,
true);
342 action->setCheckable(
true);
347 connect(m_preferencesAction, &QAction::triggered,
this, &
QDesignerActions::showPreferencesDialog);
348 m_preferencesAction->setMenuRole(QAction::PreferencesRole);
349 m_settingsActions->addAction(m_preferencesAction);
351 connect(m_appFontAction, &QAction::triggered,
this, &
QDesignerActions::showAppFontDialog);
352 m_settingsActions->addAction(m_appFontAction);
357 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::HorizontalLayoutAction));
358 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::VerticalLayoutAction));
359 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SplitHorizontalAction));
360 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SplitVerticalAction));
361 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::GridLayoutAction));
362 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::FormLayoutAction));
363 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::BreakLayoutAction));
364 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::AdjustSizeAction));
365 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SimplifyLayoutAction));
366 m_formActions->addAction(createSeparator(
this));
377 m_previewFormAction->setShortcut(tr(
"CTRL+R"));
378 m_formActions->addAction(m_previewFormAction);
379 connect(m_previewManager, &qdesigner_internal::PreviewManager::firstPreviewOpened,
381 connect(m_previewManager, &qdesigner_internal::PreviewManager::lastPreviewClosed,
384 connect(m_viewCppCodeAction, &QAction::triggered,
this,
386 connect(m_viewPythonCodeAction, &QAction::triggered,
this,
390 if (qt_extension<QDesignerLanguageExtension *>(m_core->extensionManager(), m_core) ==
nullptr) {
391 m_formActions->addAction(m_viewCppCodeAction);
392 m_formActions->addAction(m_viewPythonCodeAction);
395 m_formActions->addAction(createSeparator(
this));
397 m_formActions->addAction(ifwm->action(QDesignerFormWindowManagerInterface::FormWindowSettingsDialogAction));
401 m_minimizeAction->setEnabled(
false);
402 m_minimizeAction->setCheckable(
true);
403 m_minimizeAction->setShortcut(tr(
"CTRL+M"));
405 m_windowActions->addAction(m_minimizeAction);
407 m_windowActions->addAction(m_bringAllToFrontSeparator);
409 m_windowActions->addAction(m_bringAllToFrontAction);
410 m_windowActions->addAction(m_windowListSeparatorAction);
417 fixActionContext(m_fileActions->actions());
418 fixActionContext(m_editActions->actions());
419 fixActionContext(m_toolActions->actions());
420 fixActionContext(m_formActions->actions());
421 fixActionContext(m_windowActions->actions());
422 fixActionContext(m_helpActions->actions());
424 activeFormWindowChanged(
core()->formWindowManager()->activeFormWindow());
426 m_backupTimer->start(180000);
427 connect(m_backupTimer, &QTimer::timeout,
this, &
QDesignerActions::backupForms);
430 connect(formWindowManager, &QDesignerFormWindowManagerInterface::formWindowAdded,
432 connect(formWindowManager, &QDesignerFormWindowManagerInterface::formWindowRemoved,
434 formWindowCountChanged();
439 QActionGroup *helpActions = createActionGroup(
this);
441#ifndef QT_JAMBI_BUILD
442 auto *mainHelpAction =
new QAction(tr(
"Qt Widgets Designer &Help"),
this);
443 mainHelpAction->setObjectName(u"__qt_designer_help_action"_s);
444 connect(mainHelpAction, &QAction::triggered,
this, &
QDesignerActions::showDesignerHelp);
445 mainHelpAction->setShortcut(Qt::CTRL | Qt::Key_Question);
446 helpActions->addAction(mainHelpAction);
448 helpActions->addAction(createSeparator(
this));
449 auto *widgetHelp =
new QAction(tr(
"Current Widget Help"),
this);
450 widgetHelp->setObjectName(u"__qt_current_widget_help_action"_s);
451 widgetHelp->setShortcut(Qt::Key_F1);
452 connect(widgetHelp, &QAction::triggered,
this, &
QDesignerActions::showWidgetSpecificHelp);
453 helpActions->addAction(widgetHelp);
457 helpActions->addAction(createSeparator(
this));
458 auto *aboutPluginsAction =
new QAction(tr(
"About Plugins"),
this);
459 aboutPluginsAction->setObjectName(u"__qt_about_plugins_action"_s);
460 aboutPluginsAction->setMenuRole(QAction::ApplicationSpecificRole);
461 connect(aboutPluginsAction, &QAction::triggered,
462 m_core->formWindowManager(), &QDesignerFormWindowManagerInterface::showPluginDialog);
463 helpActions->addAction(aboutPluginsAction);
465 auto *aboutDesignerAction =
new QAction(tr(
"About Qt Widgets Designer"),
this);
466 aboutDesignerAction->setMenuRole(QAction::AboutRole);
467 aboutDesignerAction->setObjectName(u"__qt_about_designer_action"_s);
468 connect(aboutDesignerAction, &QAction::triggered,
this, &
QDesignerActions::aboutDesigner);
469 helpActions->addAction(aboutDesignerAction);
471 auto *aboutQtAction =
new QAction(tr(
"About Qt"),
this);
472 aboutQtAction->setMenuRole(QAction::AboutQtRole);
473 aboutQtAction->setObjectName(u"__qt_about_qt_action"_s);
474 connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt);
475 helpActions->addAction(aboutQtAction);
488 QDesignerLanguageExtension *lang
489 = qt_extension<QDesignerLanguageExtension *>(m_core->extensionManager(), m_core);
491 return lang->uiExtension();
497 m_recentMenu = std::make_unique<QMenu>();
500 for (
int i = 0; i < MaxRecentFiles; ++i) {
501 auto *recentAct =
new QAction(
this);
502 recentAct->setVisible(
false);
503 connect(recentAct, &QAction::triggered,
this, &
QDesignerActions::openRecentForm);
504 m_recentFilesActions->addAction(recentAct);
505 m_recentMenu->addAction(recentAct);
507 updateRecentFileActions();
508 m_recentMenu->addSeparator();
509 auto *act =
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::EditClear),
510 tr(
"Clear &Menu"),
this);
511 act->setObjectName(u"__qt_action_clear_menu_"_s);
512 connect(act, &QAction::triggered,
this, &
QDesignerActions::clearRecentFiles);
513 m_recentFilesActions->addAction(act);
514 m_recentMenu->addAction(act);
516 act =
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentOpenRecent),
517 tr(
"&Recent Forms"),
this);
518 act->setMenu(m_recentMenu.get());
523{
return m_toolActions; }
526{
return m_workbench; }
532{
return m_fileActions; }
535{
return m_editActions; }
538{
return m_formActions; }
541{
return m_settingsActions; }
544{
return m_windowActions; }
547{
return m_helpActions; }
550{
return m_styleActions; }
553{
return m_previewFormAction; }
556{
return m_viewCppCodeAction; }
561 QDesignerFormWindowManagerInterface *formWindowManager =
core()->formWindowManager();
562 for (
int i=0; i<formWindowManager->formWindowCount(); ++i) {
563 QDesignerFormWindowInterface *formWindow = formWindowManager->formWindow(i);
564 formWindow->editWidgets();
570 showNewFormDialog(QString());
578 dlg->setAttribute(Qt::WA_DeleteOnClose);
579 dlg->setAttribute(Qt::WA_ShowModal);
581 dlg->setGeometry(fixDialogRect(dlg->rect()));
593 const QString extension = uiExtension();
594 const QStringList fileNames = QFileDialog::getOpenFileNames(parent, tr(
"Open Form"),
595 m_openDirectory, fileDialogFilters(extension),
nullptr);
597 if (fileNames.isEmpty())
600 bool atLeastOne =
false;
601 for (
const QString &fileName : fileNames) {
602 if (readInForm(fileName) && !atLeastOne)
611 const QString extension = uiExtension();
613 QString dir = fw->fileName();
617 if (!m_saveDirectory.isEmpty()) {
618 dir = m_saveDirectory;
621 if (!m_openDirectory.isEmpty()) {
622 dir = m_openDirectory;
625 dir = QDir::current().absolutePath();
627 dir += QDir::separator();
628 dir +=
"untitled."_L1;
632 std::unique_ptr<QFileDialog> saveAsDialog(createSaveAsDialog(fw, dir, extension));
633 if (saveAsDialog->exec() != QDialog::Accepted)
636 const QString saveFile = saveAsDialog->selectedFiles().constFirst();
637 saveAsDialog.reset();
639 fw->setFileName(saveFile);
640 return writeOutForm(fw, saveFile);
645 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow()) {
647 showStatusBarMessage(savedMessage(QFileInfo(fw->fileName()).fileName()));
654 QDesignerFormWindowManagerInterface *formWindowManager =
core()->formWindowManager();
655 if (
const int totalWindows = formWindowManager->formWindowCount()) {
656 const auto separator =
", "_L1;
657 for (
int i = 0; i < totalWindows; ++i) {
658 QDesignerFormWindowInterface *fw = formWindowManager->formWindow(i);
659 if (fw && fw->isDirty()) {
660 formWindowManager->setActiveFormWindow(fw);
662 if (!fileNames.isEmpty())
663 fileNames += separator;
664 fileNames += QFileInfo(fw->fileName()).fileName();
672 if (!fileNames.isEmpty()) {
673 showStatusBarMessage(savedMessage(fileNames));
679 return fw->fileName().isEmpty() ? saveFormAs(fw) : writeOutForm(fw, fw->fileName());
684 if (m_previewManager->previewCount()) {
689 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow())
690 if (
QWidget *parent = fw->parentWidget()) {
691 if (
auto *mdiSubWindow = qobject_cast<QMdiSubWindow *>(parent->parentWidget())) {
692 mdiSubWindow->close();
701 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow()) {
703 showStatusBarMessage(savedMessage(fw->fileName()));
709 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow()) {
718 for (
auto it = result.begin(); it != result.end();) {
719 it.value() = QDir::cleanPath(it.value());
720 if (it.value().startsWith(m_backupPath) && QFile::exists(it.value()))
723 it = result.erase(it);
730 QMessageBox::information(core()->topLevel(), tr(
"Designer"), tr(
"Feature not implemented yet!"));
735 m_previewManager->closeAllPreviews();
740 QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow();
743 QString errorMessage;
744 if (!qdesigner_internal::CodeDialog::showCodeDialog(fw, language, fw, &errorMessage))
745 QMessageBox::warning(fw, tr(
"Code generation failed"), errorMessage);
750 QString fn = fileName;
753 QDesignerFormWindowManagerInterface *formWindowManager =
core()->formWindowManager();
754 const int totalWindows = formWindowManager->formWindowCount();
755 for (
int i = 0; i < totalWindows; ++i) {
756 QDesignerFormWindowInterface *w = formWindowManager->formWindow(i);
757 if (w->fileName() == fn) {
759 formWindowManager->setActiveFormWindow(w);
767 QString errorMessage;
770 m_openDirectory = QFileInfo(fn).absolutePath();
775 QMessageBox box(QMessageBox::Warning, tr(
"Read error"),
776 tr(
"%1\nDo you want to update the file location or generate a new form?").arg(errorMessage),
777 QMessageBox::Cancel, core()->topLevel());
779 QPushButton *updateButton = box.addButton(tr(
"&Update"), QMessageBox::ActionRole);
780 QPushButton *newButton = box.addButton(tr(
"&New Form"), QMessageBox::ActionRole);
782 if (box.clickedButton() == box.button(QMessageBox::Cancel))
785 if (box.clickedButton() == updateButton) {
786 fn = QFileDialog::getOpenFileName(core()->topLevel(),
787 tr(
"Open Form"), m_openDirectory,
788 fileDialogFilters(uiExtension()),
nullptr);
792 }
else if (box.clickedButton() == newButton) {
795 QString newFormFileName;
796 const QFileInfo fInfo(fn);
797 if (!fInfo.exists()) {
799 const QString directory = fInfo.absolutePath();
800 if (QDir(directory).exists())
801 newFormFileName = directory + u'/' + fInfo.fileName();
803 showNewFormDialog(newFormFileName);
810bool QDesignerActions::writeOutForm(QDesignerFormWindowInterface *fw,
const QString &saveFile,
bool check)
812 Q_ASSERT(fw && !saveFile.isEmpty());
815 const QStringList problems = fw->checkContents();
816 if (!problems.isEmpty())
817 QMessageBox::information(fw->window(), tr(
"Qt Widgets Designer"), problems.join(
"<br>"_L1));
822 QSaveFile f(saveFile);
823 while (!f.open(QFile::WriteOnly)) {
824 QMessageBox box(QMessageBox::Warning,
826 tr(
"Could not open file"),
827 QMessageBox::NoButton, fw);
829 box.setWindowModality(Qt::WindowModal);
830 box.setInformativeText(tr(
"The file %1 could not be opened."
832 "\nWould you like to retry or select a different file?")
833 .arg(f.fileName(), f.errorString()));
834 QPushButton *retryButton = box.addButton(QMessageBox::Retry);
835 retryButton->setDefault(
true);
836 QPushButton *switchButton = box.addButton(tr(
"Select New File"), QMessageBox::AcceptRole);
837 QPushButton *cancelButton = box.addButton(QMessageBox::Cancel);
840 if (box.clickedButton() == cancelButton)
842 if (box.clickedButton() == switchButton) {
843 std::unique_ptr<QFileDialog> saveAsDialog(
844 createSaveAsDialog(fw, QDir::currentPath(), uiExtension()));
845 if (saveAsDialog->exec() != QDialog::Accepted)
848 const QString fileName = saveAsDialog->selectedFiles().constFirst();
849 f.setFileName(fileName);
850 fw->setFileName(fileName);
854 f.write(formWindowContents(fw));
856 QMessageBox box(QMessageBox::Warning, tr(
"Save Form"),
857 tr(
"Could not write file"),
858 QMessageBox::Cancel, fw);
859 box.setWindowModality(Qt::WindowModal);
860 box.setInformativeText(tr(
"It was not possible to write the file %1 to disk."
862 .arg(f.fileName(), f.errorString()));
866 addRecentFile(saveFile);
867 m_saveDirectory = QFileInfo(f.fileName()).absolutePath();
870 fw->parentWidget()->setWindowModified(
false);
886 const bool enable = formWindow !=
nullptr;
887 m_saveFormAction->setEnabled(enable);
888 m_saveFormAsAction->setEnabled(enable);
889 m_saveAllFormsAction->setEnabled(enable);
890 m_saveFormAsTemplateAction->setEnabled(enable);
891 m_closeFormAction->setEnabled(enable);
892 m_savePreviewImageAction->setEnabled(enable);
893 m_printPreviewAction->setEnabled(enable);
895 m_editWidgetsAction->setEnabled(enable);
897 m_previewFormAction->setEnabled(enable);
898 m_viewCppCodeAction->setEnabled(enable);
899 m_viewPythonCodeAction->setEnabled(enable);
900 m_styleActions->setEnabled(enable);
903void QDesignerActions::formWindowSettingsChanged(QDesignerFormWindowInterface *fw)
906 window->updateChanged();
911 QStringList files = m_settings.recentFilesList();
912 auto existingEnd =
std::remove_if(files.begin(), files.end(),
913 [] (
const QString &f) {
return !QFileInfo::exists(f); });
914 if (existingEnd != files.end()) {
915 files.erase(existingEnd, files.end());
916 m_settings.setRecentFilesList(files);
919 const auto recentFilesActs = m_recentFilesActions->actions();
921 for (QAction *action : recentFilesActs) {
922 if (i < files.size()) {
923 const QString &file = files.at(i);
924 action->setText(QFileInfo(file).fileName());
925 action->setIconText(file);
926 action->setVisible(
true);
928 action->setVisible(
false);
936 if (
const auto *action = qobject_cast<
const QAction *>(sender())) {
937 if (!readInForm(action->iconText()))
938 updateRecentFileActions();
944 m_settings.setRecentFilesList(QStringList());
945 updateRecentFileActions();
950 return m_recentFilesActions;
955 QStringList files = m_settings.recentFilesList();
956 files.removeAll(fileName);
957 files.prepend(fileName);
958 while (files.size() > MaxRecentFiles)
961 m_settings.setRecentFilesList(files);
962 updateRecentFileActions();
967 return m_openFormAction;
972 return m_closeFormAction;
977 return m_minimizeAction;
982 showHelp(m_helpClient->designerManualUrl() +
"qtdesigner-manual.html"_L1);
987 showHelp(m_helpClient->documentUrl(manual) + document);
992 QString errorMessage;
993 if (!m_helpClient->showPage(url, &errorMessage))
994 QMessageBox::warning(core()->topLevel(), tr(
"Assistant"), errorMessage);
999 VersionDialog mb(
core()->topLevel());
1000 mb.setWindowTitle(tr(
"About Qt Widgets Designer"));
1002 QMessageBox messageBox(QMessageBox::Information, u"Easter Egg"_s,
1003 u"Easter Egg"_s, QMessageBox::Ok, core()->topLevel());
1004 messageBox.setInformativeText(u"The Easter Egg has been removed."_s);
1011 return m_editWidgetsAction;
1016 const QString helpId = core()->integration()->contextHelpId();
1018 if (helpId.isEmpty()) {
1023 QString errorMessage;
1024 const bool rc = m_helpClient->activateIdentifier(helpId, &errorMessage);
1026 QMessageBox::warning(core()->topLevel(), tr(
"Assistant"), errorMessage);
1031 if (m_previewManager->previewCount()) {
1032 m_closeFormAction->setText(tr(
"&Close Preview"));
1034 m_closeFormAction->setText(tr(
"&Close"));
1041 if (!count || !ensureBackupDirectories())
1045 QMap<QString, QString> backupMap;
1046 QDir backupDir(m_backupPath);
1047 for (
int i = 0; i < count; ++i) {
1049 QDesignerFormWindowInterface *fwi = fw
->editor();
1051 QString formBackupName = m_backupPath +
"/backup"_L1 + QString::number(i) +
".bak"_L1;
1053 QString fwn = QDir::toNativeSeparators(fwi->fileName());
1055 fwn = fw->windowTitle();
1057 backupMap.insert(fwn, formBackupName);
1060 QSaveFile file(formBackupName);
1061 if (file.open(QFile::WriteOnly)) {
1062 file.write(formWindowContents(fw->editor(), backupDir));
1066 backupMap.remove(fwn);
1068 arg(QDir::toNativeSeparators(file.fileName()),
1069 file.errorString())
);
1073 if (!backupMap.isEmpty())
1074 m_settings.setBackup(backupMap);
1078 const QDir& backupDir)
1080 const QString content = fwi->contents();
1081 QDomDocument domDoc(u"backup"_s);
1082 if(!domDoc.setContent(content))
1085 const QDomNodeList list = domDoc.elementsByTagName(u"resources"_s);
1089 for (
int i = 0; i < list.count(); i++) {
1090 const QDomNode node = list.at(i);
1091 if (!node.isNull()) {
1092 const QDomElement element = node.toElement();
1093 if (!element.isNull() && element.tagName() ==
"resources"_L1) {
1094 QDomNode childNode = element.firstChild();
1095 while (!childNode.isNull()) {
1096 QDomElement childElement = childNode.toElement();
1097 if (!childElement.isNull() && childElement.tagName() ==
"include"_L1) {
1098 const QString attr = childElement.attribute(u"location"_s);
1099 const QString path = fwi->absoluteDir().absoluteFilePath(attr);
1100 childElement.setAttribute(u"location"_s, backupDir.relativeFilePath(path));
1102 childNode = childNode.nextSibling();
1109 return domDoc.toString();
1114 QRect frameGeometry;
1115 const QRect availableGeometry = core()->topLevel()->screen()->geometry();
1118 frameGeometry =
core()->topLevel()->frameGeometry();
1120 frameGeometry = availableGeometry;
1122 QRect dlgRect = rect;
1123 dlgRect.moveCenter(frameGeometry.center());
1126 dlgRect.moveBottom(qMin(dlgRect.bottom(), availableGeometry.bottom()));
1127 dlgRect.moveRight(qMin(dlgRect.right(), availableGeometry.right()));
1128 dlgRect.moveLeft(qMax(dlgRect.left(), availableGeometry.left()));
1129 dlgRect.moveTop(qMax(dlgRect.top(), availableGeometry.top()));
1137 QStatusBar *bar =
qDesigner->mainWindow()->statusBar();
1138 if (bar && !bar->isHidden())
1139 bar->showMessage(message, 3000);
1145 m_bringAllToFrontSeparator->setVisible(visible);
1146 m_bringAllToFrontAction->setVisible(visible);
1151 m_windowListSeparatorAction->setVisible(visible);
1156 if (m_backupPath.isEmpty())
1157 m_backupPath = qdesigner_internal::dataDirectory() + u"/backup"_s;
1160 const QDir backupDir(m_backupPath);
1162 if (!backupDir.exists()) {
1163 if (!backupDir.mkpath(m_backupPath)) {
1165 .arg(QDir::toNativeSeparators(m_backupPath))
);
1176 preferencesDialog.exec();
1183 if (!m_appFontDialog)
1184 m_appFontDialog =
new AppFontDialog(core()->topLevel());
1185 m_appFontDialog->show();
1186 m_appFontDialog->raise();
1189QPixmap
QDesignerActions::createPreviewPixmap(QDesignerFormWindowInterface *fw)
1191 const QCursor oldCursor =
core()->topLevel()->cursor();
1192 core()->topLevel()->setCursor(Qt::WaitCursor);
1194 QString errorMessage;
1195 const QPixmap pixmap = m_previewManager->createPreviewPixmap(fw, QString(), &errorMessage);
1196 core()->topLevel()->setCursor(oldCursor);
1197 if (pixmap.isNull()) {
1198 QMessageBox::warning(fw, tr(
"Preview failed"), errorMessage);
1207 if (settings.isCustomPreviewConfigurationEnabled())
1208 pc = settings.customPreviewConfiguration();
1214 const char *format =
"png";
1216 QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow();
1221 const QString extension = QString::fromLatin1(format);
1222 const QString filter = tr(
"Image files (*.%1)").arg(extension);
1224 QString suggestion = fw->fileName();
1225 if (!suggestion.isEmpty())
1226 suggestion = QFileInfo(suggestion).baseName() + u'.' + extension;
1228 QFileDialog dialog(fw, tr(
"Save Image"), suggestion, filter);
1229 dialog.setAcceptMode(QFileDialog::AcceptSave);
1230 dialog.setDefaultSuffix(extension);
1233 if (dialog.exec() != QDialog::Accepted)
1235 const QString fileName = dialog.selectedFiles().constFirst();
1237 if (image.isNull()) {
1238 const QPixmap pixmap = createPreviewPixmap(fw);
1239 if (pixmap.isNull())
1242 image = pixmap.toImage();
1245 if (image.save(fileName, format)) {
1246 showStatusBarMessage(tr(
"Saved image %1.").arg(QFileInfo(fileName).fileName()));
1250 QMessageBox box(QMessageBox::Warning, tr(
"Save Image"),
1251 tr(
"The file %1 could not be written.").arg( fileName),
1252 QMessageBox::Retry|QMessageBox::Cancel, fw);
1253 if (box.exec() == QMessageBox::Cancel)
1260 const bool enabled = m_core->formWindowManager()->formWindowCount() == 0;
1262
1263
1264 static const QString disabledTip = tr(
"Please close all forms to enable the loading of additional fonts.");
1265 m_appFontAction->setEnabled(enabled);
1266 m_appFontAction->setStatusTip(enabled ? QString() : disabledTip);
1272 QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow();
1277 m_printer =
new QPrinter(QPrinter::HighResolution);
1279 m_printer->setFullPage(
false);
1282 const QPixmap pixmap = createPreviewPixmap(fw);
1283 if (pixmap.isNull())
1286 const QSizeF pixmapSize = pixmap.size();
1288 m_printer->setPageOrientation(pixmapSize.width() > pixmapSize.height() ?
1289 QPageLayout::Landscape : QPageLayout::Portrait);
1292 QPrintDialog dialog(m_printer, fw);
1296 const QCursor oldCursor = core()->topLevel()->cursor();
1297 core()->topLevel()->setCursor(Qt::WaitCursor);
1299 const double suggestedScaling =
static_cast<
double>(m_printer->physicalDpiX()) /
static_cast<
double>(fw->physicalDpiX());
1301 QPainter painter(m_printer);
1302 painter.setRenderHint(QPainter::SmoothPixmapTransform);
1305 const QRectF page = painter.viewport();
1306 const double maxScaling = qMin(page.size().width() / pixmapSize.width(), page.size().height() / pixmapSize.height());
1307 const double scaling = qMin(suggestedScaling, maxScaling);
1309 const double xOffset = page.left() + qMax(0.0, (page.size().width() - scaling * pixmapSize.width()) / 2.0);
1310 const double yOffset = page.top() + qMax(0.0, (page.size().height() - scaling * pixmapSize.height()) / 2.0);
1313 painter.translate(xOffset, yOffset);
1314 painter.scale(scaling, scaling);
1315 painter.drawPixmap(0, 0, pixmap);
1316 core()->topLevel()->setCursor(oldCursor);
1318 showStatusBarMessage(tr(
"Printed %1.").arg(QFileInfo(fw->fileName()).fileName()));
QActionGroup * helpActions() const
QActionGroup * fileActions() const
QActionGroup * settingsActions() const
QAction * minimizeAction() const
QActionGroup * styleActions() const
QMap< QString, QString > pendingBackups() const
~QDesignerActions() override
QActionGroup * editActions() const
QString uiExtension() const
QActionGroup * formActions() const
bool openForm(QWidget *parent)
QDesignerFormEditorInterface * core() const
QActionGroup * toolActions() const
QAction * previewFormAction() const
QActionGroup * windowActions() const
QAction * closeFormAction() const
void setWindowListSeparatorVisible(bool visible)
bool saveForm(QDesignerFormWindowInterface *fw)
QAction * editWidgets() const
QAction * openFormAction() const
QAction * viewCodeAction() const
QDesignerWorkbench * workbench() const
static const char * defaultToolbarPropertyName
void setBringAllToFrontVisible(bool visible)
QActionGroup * recentFilesActions() const
QDesignerSettings(QDesignerFormEditorInterface *core)
int formWindowCount() const
void toggleFormMinimizationState()
QDesignerFormEditorInterface * core() const
QDesignerFormWindow * formWindow(int index) const
void updateBackup(QDesignerFormWindowInterface *fwi)
Auxiliary methods to store/retrieve settings.
QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message)
static QByteArray formWindowContents(const QDesignerFormWindowInterface *fw, std::optional< QDir > alternativeDir={})
static QString fixResourceFileBackupPath(const QDesignerFormWindowInterface *fwi, const QDir &backupDir)
static QString fileDialogFilters(const QString &extension)
static QString savedMessage(const QString &fileName)
static QAction * createSeparator(QObject *parent)
QFileDialog * createSaveAsDialog(QWidget *parent, const QString &dir, const QString &extension)
static QActionGroup * createActionGroup(QObject *parent, bool exclusive=false)
static void fixActionContext(const QList< QAction * > &actions)