8#include <qdesigner_utils_p.h>
20#include <pluginmanager_p.h>
21#include <qdesigner_formbuilder_p.h>
22#include <iconloader_p.h>
23#include <previewmanager_p.h>
24#include <codedialog_p.h>
25#include <qdesigner_formwindowmanager_p.h>
28#include <QtDesigner/abstractformeditor.h>
29#include <QtDesigner/abstractformwindow.h>
30#include <QtDesigner/abstractintegration.h>
31#include <QtDesigner/abstractlanguage.h>
32#include <QtDesigner/abstractmetadatabase.h>
33#include <QtDesigner/abstractformwindowmanager.h>
34#include <QtDesigner/abstractformwindowcursor.h>
35#include <QtDesigner/abstractformeditorplugin.h>
36#include <QtDesigner/qextensionmanager.h>
38#include <QtDesigner/private/shared_settings_p.h>
39#include <QtDesigner/private/formwindowbase_p.h>
41#include <QtWidgets/qstylefactory.h>
42#include <QtWidgets/qfiledialog.h>
43#include <QtWidgets/qmenu.h>
44#include <QtWidgets/qmessagebox.h>
45#include <QtWidgets/qmdisubwindow.h>
46#include <QtWidgets/qpushbutton.h>
47#include <QtWidgets/qstatusbar.h>
49#include <QtGui/qaction.h>
50#include <QtGui/qactiongroup.h>
51#include <QtGui/qevent.h>
52#include <QtGui/qicon.h>
53#include <QtGui/qimage.h>
54#include <QtGui/qpixmap.h>
55#include <QtGui/qscreen.h>
56#if defined(QT_PRINTSUPPORT_LIB)
57# include <QtPrintSupport/qtprintsupportglobal.h>
58# if QT_CONFIG(printer) && QT_CONFIG(printdialog)
59# include <QtPrintSupport/qprinter.h>
60# include <QtPrintSupport/qprintdialog.h>
64#include <QtGui/qpainter.h>
65#include <QtGui/qtransform.h>
66#include <QtGui/qcursor.h>
68#include <QtCore/qdir.h>
69#include <QtCore/qsize.h>
70#include <QtCore/qlibraryinfo.h>
71#include <QtCore/qbuffer.h>
72#include <QtCore/qpluginloader.h>
73#include <QtCore/qdebug.h>
74#include <QtCore/qtimer.h>
75#include <QtCore/qtextstream.h>
76#include <QtCore/qmetaobject.h>
77#include <QtCore/qfileinfo.h>
78#include <QtCore/qsavefile.h>
79#include <QtXml/qdom.h>
87using namespace Qt::StringLiterals;
92# define NONMODAL_PREVIEW
97 auto *rc =
new QAction(parent);
98 rc->setSeparator(
true);
104 auto *rc =
new QActionGroup(parent);
105 rc->setExclusive(exclusive);
111 for (QAction *a : actions)
112 a->setShortcutContext(Qt::ApplicationShortcut);
117 return QDesignerActions::tr(
"Saved %1.").arg(fileName);
122 return QDesignerActions::tr(
"Designer UI files (*.%1);;All Files (*)").arg(extension);
126 const QDir& backupDir);
129 std::optional<QDir> alternativeDir = {})
131 QString contents = alternativeDir.has_value()
132 ? fixResourceFileBackupPath(fw, alternativeDir.value()) : fw->contents();
133 if (
const auto *fwb = qobject_cast<
const qdesigner_internal::FormWindowBase *>(fw)) {
134 if (fwb->lineTerminatorMode() == qdesigner_internal::FormWindowBase::CRLFLineTerminator)
135 contents.replace(u'\n',
"\r\n"_L1);
137 return contents.toUtf8();
142 auto *result =
new QFileDialog(parent, QDesignerActions::tr(
"Save Form As"),
143 dir, fileDialogFilters(extension));
144 result->setAcceptMode(QFileDialog::AcceptSave);
145 result->setDefaultSuffix(extension);
150 : QObject(workbench),
151 m_workbench(workbench),
154 m_helpClient(HelpClient::create(options.helpMode)),
155 m_backupTimer(
new QTimer(
this)),
156 m_fileActions(createActionGroup(
this)),
157 m_recentFilesActions(createActionGroup(
this)),
158 m_editActions(createActionGroup(
this)),
159 m_formActions(createActionGroup(
this)),
160 m_settingsActions(createActionGroup(
this)),
161 m_windowActions(createActionGroup(
this)),
162 m_toolActions(createActionGroup(
this,
true)),
163 m_editWidgetsAction(
new QAction(tr(
"Edit Widgets"),
this)),
164 m_newFormAction(
new QAction(qdesigner_internal::createIconSet(QIcon::ThemeIcon::DocumentNew,
166 tr(
"&New..."),
this)),
167 m_openFormAction(
new QAction(qdesigner_internal::createIconSet(QIcon::ThemeIcon::DocumentOpen,
169 tr(
"&Open..."),
this)),
170 m_saveFormAction(
new QAction(qdesigner_internal::createIconSet(QIcon::ThemeIcon::DocumentSave,
173 m_saveFormAsAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentSaveAs),
174 tr(
"Save &As..."),
this)),
175 m_saveAllFormsAction(
new QAction(tr(
"Save A&ll"),
this)),
176 m_saveFormAsTemplateAction(
new QAction(tr(
"Save As &Template..."),
this)),
177 m_closeFormAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::WindowClose),
178 tr(
"&Close"),
this)),
179 m_savePreviewImageAction(
new QAction(tr(
"Save &Image..."),
this)),
180 m_printPreviewAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentPrint),
181 tr(
"&Print..."),
this)),
182 m_quitAction(
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::ApplicationExit),
184 m_viewCppCodeAction(
new QAction(tr(
"View &C++ Code..."),
this)),
185 m_viewPythonCodeAction(
new QAction(tr(
"View &Python Code..."),
this)),
186 m_minimizeAction(
new QAction(tr(
"&Minimize"),
this)),
187 m_bringAllToFrontSeparator(createSeparator(
this)),
188 m_bringAllToFrontAction(
new QAction(tr(
"Bring All to Front"),
this)),
189 m_windowListSeparatorAction(createSeparator(
this)),
190 m_preferencesAction(
new QAction(tr(
"Preferences..."),
this)),
191 m_appFontAction(
new QAction(tr(
"Additional Fonts..."),
this))
193 Q_ASSERT(m_core !=
nullptr);
194 auto *ifwm = qobject_cast<qdesigner_internal::QDesignerFormWindowManager *>(m_core->formWindowManager());
196 m_previewManager = ifwm->previewManager();
197 m_previewFormAction = ifwm->action(QDesignerFormWindowManagerInterface::DefaultPreviewAction);
198 m_styleActions = ifwm->actionGroup(QDesignerFormWindowManagerInterface::StyledPreviewActionGroup);
199 connect(ifwm, &QDesignerFormWindowManagerInterface::formWindowSettingsChanged,
202 m_editWidgetsAction->setObjectName(u"__qt_edit_widgets_action"_s);
203 m_newFormAction->setObjectName(u"__qt_new_form_action"_s);
204 m_openFormAction->setObjectName(u"__qt_open_form_action"_s);
205 m_saveFormAction->setObjectName(u"__qt_save_form_action"_s);
206 m_saveFormAsAction->setObjectName(u"__qt_save_form_as_action"_s);
207 m_saveAllFormsAction->setObjectName(u"__qt_save_all_forms_action"_s);
208 m_saveFormAsTemplateAction->setObjectName(u"__qt_save_form_as_template_action"_s);
209 m_closeFormAction->setObjectName(u"__qt_close_form_action"_s);
210 m_quitAction->setObjectName(u"__qt_quit_action"_s);
211 m_previewFormAction->setObjectName(u"__qt_preview_form_action"_s);
212 m_viewCppCodeAction->setObjectName(u"__qt_preview_cpp_code_action"_s);
213 m_viewPythonCodeAction->setObjectName(u"__qt_preview_python_code_action"_s);
214 m_minimizeAction->setObjectName(u"__qt_minimize_action"_s);
215 m_bringAllToFrontAction->setObjectName(u"__qt_bring_all_to_front_action"_s);
216 m_preferencesAction->setObjectName(u"__qt_preferences_action"_s);
218 m_helpActions = createHelpActions();
224 QDesignerFormWindowManagerInterface *formWindowManager = m_core->formWindowManager();
225 Q_ASSERT(formWindowManager !=
nullptr);
230 m_newFormAction->setShortcut(QKeySequence::New);
232 m_fileActions->addAction(m_newFormAction);
234 m_openFormAction->setShortcut(QKeySequence::Open);
236 m_fileActions->addAction(m_openFormAction);
238 m_fileActions->addAction(createRecentFilesMenu());
239 m_fileActions->addAction(createSeparator(
this));
241 m_saveFormAction->setShortcut(QKeySequence::Save);
242 connect(m_saveFormAction, &QAction::triggered,
this,
243 QOverload<>::of(&QDesignerActions::saveForm));
244 m_fileActions->addAction(m_saveFormAction);
246 connect(m_saveFormAsAction, &QAction::triggered,
this,
247 QOverload<>::of(&QDesignerActions::saveFormAs));
248 m_fileActions->addAction(m_saveFormAsAction);
251 m_saveAllFormsAction->setShortcut(tr(
"ALT+CTRL+S"));
253 m_saveAllFormsAction->setShortcut(tr(
"CTRL+SHIFT+S"));
255 connect(m_saveAllFormsAction, &QAction::triggered,
this, &
QDesignerActions::saveAllForms);
256 m_fileActions->addAction(m_saveAllFormsAction);
258 connect(m_saveFormAsTemplateAction, &QAction::triggered,
this, &
QDesignerActions::saveFormAsTemplate);
259 m_fileActions->addAction(m_saveFormAsTemplateAction);
261 m_fileActions->addAction(createSeparator(
this));
263 m_printPreviewAction->setShortcut(QKeySequence::Print);
264 connect(m_printPreviewAction, &QAction::triggered,
this, &
QDesignerActions::printPreviewImage);
265 m_fileActions->addAction(m_printPreviewAction);
266 m_printPreviewAction->setObjectName(u"__qt_print_action"_s);
268 connect(m_savePreviewImageAction, &QAction::triggered,
this, &
QDesignerActions::savePreviewImage);
269 m_savePreviewImageAction->setObjectName(u"__qt_saveimage_action"_s);
270 m_fileActions->addAction(m_savePreviewImageAction);
271 m_fileActions->addAction(createSeparator(
this));
273 m_closeFormAction->setShortcut(QKeySequence::Close);
274 connect(m_closeFormAction, &QAction::triggered,
this, &
QDesignerActions::closeForm);
275 m_fileActions->addAction(m_closeFormAction);
278 m_fileActions->addAction(createSeparator(
this));
280 m_quitAction->setShortcuts(QKeySequence::Quit);
281 m_quitAction->setMenuRole(QAction::QuitRole);
282 connect(m_quitAction, &QAction::triggered,
this, &
QDesignerActions::shutdown);
283 m_fileActions->addAction(m_quitAction);
288 QAction *undoAction = formWindowManager->action(QDesignerFormWindowManagerInterface::UndoAction);
289 undoAction->setObjectName(u"__qt_undo_action"_s);
290 undoAction->setShortcut(QKeySequence::Undo);
291 m_editActions->addAction(undoAction);
293 QAction *redoAction = formWindowManager->action(QDesignerFormWindowManagerInterface::RedoAction);
294 redoAction->setObjectName(u"__qt_redo_action"_s);
295 redoAction->setShortcut(QKeySequence::Redo);
296 m_editActions->addAction(redoAction);
298 m_editActions->addAction(createSeparator(
this));
300#if QT_CONFIG(clipboard)
301 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::CutAction));
302 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::CopyAction));
303 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::PasteAction));
305 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::DeleteAction));
307 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SelectAllAction));
309 m_editActions->addAction(createSeparator(
this));
311 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::LowerAction));
312 m_editActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::RaiseAction));
321 m_editWidgetsAction->setCheckable(
true);
322 QList<QKeySequence> shortcuts;
323 shortcuts.append(QKeySequence(Qt::Key_F3));
324 shortcuts.append(QKeySequence(Qt::Key_Escape));
325 m_editWidgetsAction->setShortcuts(shortcuts);
326 m_editWidgetsAction->setIcon(qdesigner_internal::createIconSet(
"widgettool.png"_L1));
327 connect(m_editWidgetsAction, &QAction::triggered,
this, &
QDesignerActions::editWidgetsSlot);
328 m_editWidgetsAction->setChecked(
true);
329 m_editWidgetsAction->setEnabled(
false);
331 m_toolActions->addAction(m_editWidgetsAction);
333 connect(formWindowManager, &qdesigner_internal::QDesignerFormWindowManager::activeFormWindowChanged,
334 this, &QDesignerActions::activeFormWindowChanged);
336 const QObjectList builtinPlugins = QPluginLoader::staticInstances()
337 + m_core->pluginManager()->instances();
338 for (QObject *plugin : builtinPlugins) {
339 if (QDesignerFormEditorPluginInterface *formEditorPlugin = qobject_cast<QDesignerFormEditorPluginInterface*>(plugin)) {
340 if (QAction *action = formEditorPlugin->action()) {
341 m_toolActions->addAction(action);
342 action->setProperty(QDesignerActions::defaultToolbarPropertyName,
true);
343 action->setCheckable(
true);
348 connect(m_preferencesAction, &QAction::triggered,
this, &
QDesignerActions::showPreferencesDialog);
349 m_preferencesAction->setMenuRole(QAction::PreferencesRole);
350 m_settingsActions->addAction(m_preferencesAction);
352 connect(m_appFontAction, &QAction::triggered,
this, &
QDesignerActions::showAppFontDialog);
353 m_settingsActions->addAction(m_appFontAction);
358 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::HorizontalLayoutAction));
359 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::VerticalLayoutAction));
360 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SplitHorizontalAction));
361 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SplitVerticalAction));
362 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::GridLayoutAction));
363 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::FormLayoutAction));
364 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::BreakLayoutAction));
365 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::AdjustSizeAction));
366 m_formActions->addAction(formWindowManager->action(QDesignerFormWindowManagerInterface::SimplifyLayoutAction));
367 m_formActions->addAction(createSeparator(
this));
378 m_previewFormAction->setShortcut(tr(
"CTRL+R"));
379 m_formActions->addAction(m_previewFormAction);
380 connect(m_previewManager, &qdesigner_internal::PreviewManager::firstPreviewOpened,
382 connect(m_previewManager, &qdesigner_internal::PreviewManager::lastPreviewClosed,
385 connect(m_viewCppCodeAction, &QAction::triggered,
this,
387 connect(m_viewPythonCodeAction, &QAction::triggered,
this,
391 if (qt_extension<QDesignerLanguageExtension *>(m_core->extensionManager(), m_core) ==
nullptr) {
392 m_formActions->addAction(m_viewCppCodeAction);
393 m_formActions->addAction(m_viewPythonCodeAction);
396 m_formActions->addAction(createSeparator(
this));
398 m_formActions->addAction(ifwm->action(QDesignerFormWindowManagerInterface::FormWindowSettingsDialogAction));
402 m_minimizeAction->setEnabled(
false);
403 m_minimizeAction->setCheckable(
true);
404 m_minimizeAction->setShortcut(tr(
"CTRL+M"));
406 m_windowActions->addAction(m_minimizeAction);
408 m_windowActions->addAction(m_bringAllToFrontSeparator);
410 m_windowActions->addAction(m_bringAllToFrontAction);
411 m_windowActions->addAction(m_windowListSeparatorAction);
418 fixActionContext(m_fileActions->actions());
419 fixActionContext(m_editActions->actions());
420 fixActionContext(m_toolActions->actions());
421 fixActionContext(m_formActions->actions());
422 fixActionContext(m_windowActions->actions());
423 fixActionContext(m_helpActions->actions());
425 activeFormWindowChanged(
core()->formWindowManager()->activeFormWindow());
427 m_backupTimer->start(180000);
428 connect(m_backupTimer, &QTimer::timeout,
this, &
QDesignerActions::backupForms);
431 connect(formWindowManager, &QDesignerFormWindowManagerInterface::formWindowAdded,
433 connect(formWindowManager, &QDesignerFormWindowManagerInterface::formWindowRemoved,
435 formWindowCountChanged();
440 QActionGroup *helpActions = createActionGroup(
this);
442#ifndef QT_JAMBI_BUILD
443 auto *mainHelpAction =
new QAction(tr(
"Qt Widgets Designer &Help"),
this);
444 mainHelpAction->setObjectName(u"__qt_designer_help_action"_s);
445 connect(mainHelpAction, &QAction::triggered,
this, &
QDesignerActions::showDesignerHelp);
446 mainHelpAction->setShortcut(Qt::CTRL | Qt::Key_Question);
447 helpActions->addAction(mainHelpAction);
449 helpActions->addAction(createSeparator(
this));
450 auto *widgetHelp =
new QAction(tr(
"Current Widget Help"),
this);
451 widgetHelp->setObjectName(u"__qt_current_widget_help_action"_s);
452 widgetHelp->setShortcut(Qt::Key_F1);
453 connect(widgetHelp, &QAction::triggered,
this, &
QDesignerActions::showWidgetSpecificHelp);
454 helpActions->addAction(widgetHelp);
458 helpActions->addAction(createSeparator(
this));
459 auto *aboutPluginsAction =
new QAction(tr(
"About Plugins"),
this);
460 aboutPluginsAction->setObjectName(u"__qt_about_plugins_action"_s);
461 aboutPluginsAction->setMenuRole(QAction::ApplicationSpecificRole);
462 connect(aboutPluginsAction, &QAction::triggered,
463 m_core->formWindowManager(), &QDesignerFormWindowManagerInterface::showPluginDialog);
464 helpActions->addAction(aboutPluginsAction);
466 auto *aboutDesignerAction =
new QAction(tr(
"About Qt Widgets Designer"),
this);
467 aboutDesignerAction->setMenuRole(QAction::AboutRole);
468 aboutDesignerAction->setObjectName(u"__qt_about_designer_action"_s);
469 connect(aboutDesignerAction, &QAction::triggered,
this, &
QDesignerActions::aboutDesigner);
470 helpActions->addAction(aboutDesignerAction);
472 auto *aboutQtAction =
new QAction(tr(
"About Qt"),
this);
473 aboutQtAction->setMenuRole(QAction::AboutQtRole);
474 aboutQtAction->setObjectName(u"__qt_about_qt_action"_s);
475 connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt);
476 helpActions->addAction(aboutQtAction);
489 QDesignerLanguageExtension *lang
490 = qt_extension<QDesignerLanguageExtension *>(m_core->extensionManager(), m_core);
492 return lang->uiExtension();
498 m_recentMenu = std::make_unique<QMenu>();
501 for (
int i = 0; i < MaxRecentFiles; ++i) {
502 auto *recentAct =
new QAction(
this);
503 recentAct->setVisible(
false);
504 connect(recentAct, &QAction::triggered,
this, &
QDesignerActions::openRecentForm);
505 m_recentFilesActions->addAction(recentAct);
506 m_recentMenu->addAction(recentAct);
508 updateRecentFileActions();
509 m_recentMenu->addSeparator();
510 auto *act =
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::EditClear),
511 tr(
"Clear &Menu"),
this);
512 act->setObjectName(u"__qt_action_clear_menu_"_s);
513 connect(act, &QAction::triggered,
this, &
QDesignerActions::clearRecentFiles);
514 m_recentFilesActions->addAction(act);
515 m_recentMenu->addAction(act);
517 act =
new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentOpenRecent),
518 tr(
"&Recent Forms"),
this);
519 act->setMenu(m_recentMenu.get());
524{
return m_toolActions; }
527{
return m_workbench; }
533{
return m_fileActions; }
536{
return m_editActions; }
539{
return m_formActions; }
542{
return m_settingsActions; }
545{
return m_windowActions; }
548{
return m_helpActions; }
551{
return m_styleActions; }
554{
return m_previewFormAction; }
557{
return m_viewCppCodeAction; }
562 QDesignerFormWindowManagerInterface *formWindowManager =
core()->formWindowManager();
563 for (
int i=0; i<formWindowManager->formWindowCount(); ++i) {
564 QDesignerFormWindowInterface *formWindow = formWindowManager->formWindow(i);
565 formWindow->editWidgets();
571 showNewFormDialog(QString());
579 dlg->setAttribute(Qt::WA_DeleteOnClose);
580 dlg->setAttribute(Qt::WA_ShowModal);
582 dlg->setGeometry(fixDialogRect(dlg->rect()));
594 const QString extension = uiExtension();
595 const QStringList fileNames = QFileDialog::getOpenFileNames(parent, tr(
"Open Form"),
596 m_openDirectory, fileDialogFilters(extension),
nullptr);
598 if (fileNames.isEmpty())
601 bool atLeastOne =
false;
602 for (
const QString &fileName : fileNames) {
603 if (readInForm(fileName) && !atLeastOne)
612 const QString extension = uiExtension();
614 QString dir = fw->fileName();
618 if (!m_saveDirectory.isEmpty()) {
619 dir = m_saveDirectory;
622 if (!m_openDirectory.isEmpty()) {
623 dir = m_openDirectory;
626 dir = QDir::current().absolutePath();
628 dir += QDir::separator();
629 dir +=
"untitled."_L1;
633 std::unique_ptr<QFileDialog> saveAsDialog(createSaveAsDialog(fw, dir, extension));
634 if (saveAsDialog->exec() != QDialog::Accepted)
637 const QString saveFile = saveAsDialog->selectedFiles().constFirst();
638 saveAsDialog.reset();
640 fw->setFileName(saveFile);
641 return writeOutForm(fw, saveFile);
646 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow()) {
648 showStatusBarMessage(savedMessage(QFileInfo(fw->fileName()).fileName()));
655 QDesignerFormWindowManagerInterface *formWindowManager =
core()->formWindowManager();
656 if (
const int totalWindows = formWindowManager->formWindowCount()) {
657 const auto separator =
", "_L1;
658 for (
int i = 0; i < totalWindows; ++i) {
659 QDesignerFormWindowInterface *fw = formWindowManager->formWindow(i);
660 if (fw && fw->isDirty()) {
661 formWindowManager->setActiveFormWindow(fw);
663 if (!fileNames.isEmpty())
664 fileNames += separator;
665 fileNames += QFileInfo(fw->fileName()).fileName();
673 if (!fileNames.isEmpty()) {
674 showStatusBarMessage(savedMessage(fileNames));
680 return fw->fileName().isEmpty() ? saveFormAs(fw) : writeOutForm(fw, fw->fileName());
685 if (m_previewManager->previewCount()) {
690 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow())
691 if (
QWidget *parent = fw->parentWidget()) {
692 if (
auto *mdiSubWindow = qobject_cast<QMdiSubWindow *>(parent->parentWidget())) {
693 mdiSubWindow->close();
702 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow()) {
704 showStatusBarMessage(savedMessage(fw->fileName()));
710 if (QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow()) {
719 for (
auto it = result.begin(); it != result.end();) {
720 it.value() = QDir::cleanPath(it.value());
721 if (it.value().startsWith(m_backupPath) && QFile::exists(it.value()))
724 it = result.erase(it);
731 QMessageBox::information(core()->topLevel(), tr(
"Designer"), tr(
"Feature not implemented yet!"));
736 m_previewManager->closeAllPreviews();
741 QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow();
744 QString errorMessage;
745 if (!qdesigner_internal::CodeDialog::showCodeDialog(fw, language, fw, &errorMessage))
746 QMessageBox::warning(fw, tr(
"Code generation failed"), errorMessage);
751 QString fn = fileName;
754 QDesignerFormWindowManagerInterface *formWindowManager =
core()->formWindowManager();
755 const int totalWindows = formWindowManager->formWindowCount();
756 for (
int i = 0; i < totalWindows; ++i) {
757 QDesignerFormWindowInterface *w = formWindowManager->formWindow(i);
758 if (w->fileName() == fn) {
760 formWindowManager->setActiveFormWindow(w);
768 QString errorMessage;
771 m_openDirectory = QFileInfo(fn).absolutePath();
776 QMessageBox box(QMessageBox::Warning, tr(
"Read error"),
777 tr(
"%1\nDo you want to update the file location or generate a new form?").arg(errorMessage),
778 QMessageBox::Cancel, core()->topLevel());
780 QPushButton *updateButton = box.addButton(tr(
"&Update"), QMessageBox::ActionRole);
781 QPushButton *newButton = box.addButton(tr(
"&New Form"), QMessageBox::ActionRole);
783 if (box.clickedButton() == box.button(QMessageBox::Cancel))
786 if (box.clickedButton() == updateButton) {
787 fn = QFileDialog::getOpenFileName(core()->topLevel(),
788 tr(
"Open Form"), m_openDirectory,
789 fileDialogFilters(uiExtension()),
nullptr);
793 }
else if (box.clickedButton() == newButton) {
796 QString newFormFileName;
797 const QFileInfo fInfo(fn);
798 if (!fInfo.exists()) {
800 const QString directory = fInfo.absolutePath();
801 if (QDir(directory).exists())
802 newFormFileName = directory + u'/' + fInfo.fileName();
804 showNewFormDialog(newFormFileName);
811bool QDesignerActions::writeOutForm(QDesignerFormWindowInterface *fw,
const QString &saveFile,
bool check)
813 Q_ASSERT(fw && !saveFile.isEmpty());
816 const QStringList problems = fw->checkContents();
817 if (!problems.isEmpty())
818 QMessageBox::information(fw->window(), tr(
"Qt Widgets Designer"), problems.join(
"<br>"_L1));
823 QSaveFile f(saveFile);
824 while (!f.open(QFile::WriteOnly)) {
825 QMessageBox box(QMessageBox::Warning,
827 tr(
"Could not open file"),
828 QMessageBox::NoButton, fw);
830 box.setWindowModality(Qt::WindowModal);
831 box.setInformativeText(tr(
"The file %1 could not be opened."
833 "\nWould you like to retry or select a different file?")
834 .arg(f.fileName(), f.errorString()));
835 QPushButton *retryButton = box.addButton(QMessageBox::Retry);
836 retryButton->setDefault(
true);
837 QPushButton *switchButton = box.addButton(tr(
"Select New File"), QMessageBox::AcceptRole);
838 QPushButton *cancelButton = box.addButton(QMessageBox::Cancel);
841 if (box.clickedButton() == cancelButton)
843 if (box.clickedButton() == switchButton) {
844 std::unique_ptr<QFileDialog> saveAsDialog(
845 createSaveAsDialog(fw, QDir::currentPath(), uiExtension()));
846 if (saveAsDialog->exec() != QDialog::Accepted)
849 const QString fileName = saveAsDialog->selectedFiles().constFirst();
850 f.setFileName(fileName);
851 fw->setFileName(fileName);
855 f.write(formWindowContents(fw));
857 QMessageBox box(QMessageBox::Warning, tr(
"Save Form"),
858 tr(
"Could not write file"),
859 QMessageBox::Cancel, fw);
860 box.setWindowModality(Qt::WindowModal);
861 box.setInformativeText(tr(
"It was not possible to write the file %1 to disk."
863 .arg(f.fileName(), f.errorString()));
867 addRecentFile(saveFile);
868 m_saveDirectory = QFileInfo(f.fileName()).absolutePath();
871 fw->parentWidget()->setWindowModified(
false);
887 const bool enable = formWindow !=
nullptr;
888 m_saveFormAction->setEnabled(enable);
889 m_saveFormAsAction->setEnabled(enable);
890 m_saveAllFormsAction->setEnabled(enable);
891 m_saveFormAsTemplateAction->setEnabled(enable);
892 m_closeFormAction->setEnabled(enable);
893 m_savePreviewImageAction->setEnabled(enable);
894 m_printPreviewAction->setEnabled(enable);
896 m_editWidgetsAction->setEnabled(enable);
898 m_previewFormAction->setEnabled(enable);
899 m_viewCppCodeAction->setEnabled(enable);
900 m_viewPythonCodeAction->setEnabled(enable);
901 m_styleActions->setEnabled(enable);
904void QDesignerActions::formWindowSettingsChanged(QDesignerFormWindowInterface *fw)
907 window->updateChanged();
912 QStringList files = m_settings.recentFilesList();
913 auto existingEnd =
std::remove_if(files.begin(), files.end(),
914 [] (
const QString &f) {
return !QFileInfo::exists(f); });
915 if (existingEnd != files.end()) {
916 files.erase(existingEnd, files.end());
917 m_settings.setRecentFilesList(files);
920 const auto recentFilesActs = m_recentFilesActions->actions();
922 for (QAction *action : recentFilesActs) {
923 if (i < files.size()) {
924 const QString &file = files.at(i);
925 action->setText(QFileInfo(file).fileName());
926 action->setIconText(file);
927 action->setVisible(
true);
929 action->setVisible(
false);
937 if (
const auto *action = qobject_cast<
const QAction *>(sender())) {
938 if (!readInForm(action->iconText()))
939 updateRecentFileActions();
945 m_settings.setRecentFilesList(QStringList());
946 updateRecentFileActions();
951 return m_recentFilesActions;
956 QStringList files = m_settings.recentFilesList();
957 files.removeAll(fileName);
958 files.prepend(fileName);
959 while (files.size() > MaxRecentFiles)
962 m_settings.setRecentFilesList(files);
963 updateRecentFileActions();
968 return m_openFormAction;
973 return m_closeFormAction;
978 return m_minimizeAction;
983 showHelp(m_helpClient->designerManualUrl() +
"qtdesigner-manual.html"_L1);
988 showHelp(m_helpClient->documentUrl(manual) + document);
993 QString errorMessage;
994 if (!m_helpClient->showPage(url, &errorMessage))
995 QMessageBox::warning(core()->topLevel(), tr(
"Assistant"), errorMessage);
1000 VersionDialog mb(
core()->topLevel());
1001 mb.setWindowTitle(tr(
"About Qt Widgets Designer"));
1003 QMessageBox messageBox(QMessageBox::Information, u"Easter Egg"_s,
1004 u"Easter Egg"_s, QMessageBox::Ok, core()->topLevel());
1005 messageBox.setInformativeText(u"The Easter Egg has been removed."_s);
1012 return m_editWidgetsAction;
1017 const QString helpId = core()->integration()->contextHelpId();
1019 if (helpId.isEmpty()) {
1024 QString errorMessage;
1025 const bool rc = m_helpClient->activateIdentifier(helpId, &errorMessage);
1027 QMessageBox::warning(core()->topLevel(), tr(
"Assistant"), errorMessage);
1032 if (m_previewManager->previewCount()) {
1033 m_closeFormAction->setText(tr(
"&Close Preview"));
1035 m_closeFormAction->setText(tr(
"&Close"));
1042 if (!count || !ensureBackupDirectories())
1046 QMap<QString, QString> backupMap;
1047 QDir backupDir(m_backupPath);
1048 for (
int i = 0; i < count; ++i) {
1050 QDesignerFormWindowInterface *fwi = fw
->editor();
1052 QString formBackupName = m_backupPath +
"/backup"_L1 + QString::number(i) +
".bak"_L1;
1054 QString fwn = QDir::toNativeSeparators(fwi->fileName());
1056 fwn = fw->windowTitle();
1058 backupMap.insert(fwn, formBackupName);
1061 QSaveFile file(formBackupName);
1062 if (file.open(QFile::WriteOnly)) {
1063 file.write(formWindowContents(fw->editor(), backupDir));
1067 backupMap.remove(fwn);
1069 arg(QDir::toNativeSeparators(file.fileName()),
1070 file.errorString())
);
1074 if (!backupMap.isEmpty())
1075 m_settings.setBackup(backupMap);
1079 const QDir& backupDir)
1081 const QString content = fwi->contents();
1082 QDomDocument domDoc(u"backup"_s);
1083 if(!domDoc.setContent(content))
1086 const QDomNodeList list = domDoc.elementsByTagName(u"resources"_s);
1090 for (
int i = 0; i < list.count(); i++) {
1091 const QDomNode node = list.at(i);
1092 if (!node.isNull()) {
1093 const QDomElement element = node.toElement();
1094 if (!element.isNull() && element.tagName() ==
"resources"_L1) {
1095 QDomNode childNode = element.firstChild();
1096 while (!childNode.isNull()) {
1097 QDomElement childElement = childNode.toElement();
1098 if (!childElement.isNull() && childElement.tagName() ==
"include"_L1) {
1099 const QString attr = childElement.attribute(u"location"_s);
1100 const QString path = fwi->absoluteDir().absoluteFilePath(attr);
1101 childElement.setAttribute(u"location"_s, backupDir.relativeFilePath(path));
1103 childNode = childNode.nextSibling();
1110 return domDoc.toString();
1115 QRect frameGeometry;
1116 const QRect availableGeometry = core()->topLevel()->screen()->geometry();
1119 frameGeometry =
core()->topLevel()->frameGeometry();
1121 frameGeometry = availableGeometry;
1123 QRect dlgRect = rect;
1124 dlgRect.moveCenter(frameGeometry.center());
1127 dlgRect.moveBottom(qMin(dlgRect.bottom(), availableGeometry.bottom()));
1128 dlgRect.moveRight(qMin(dlgRect.right(), availableGeometry.right()));
1129 dlgRect.moveLeft(qMax(dlgRect.left(), availableGeometry.left()));
1130 dlgRect.moveTop(qMax(dlgRect.top(), availableGeometry.top()));
1138 QStatusBar *bar =
qDesigner->mainWindow()->statusBar();
1139 if (bar && !bar->isHidden())
1140 bar->showMessage(message, 3000);
1146 m_bringAllToFrontSeparator->setVisible(visible);
1147 m_bringAllToFrontAction->setVisible(visible);
1152 m_windowListSeparatorAction->setVisible(visible);
1157 if (m_backupPath.isEmpty())
1158 m_backupPath = qdesigner_internal::dataDirectory() + u"/backup"_s;
1161 const QDir backupDir(m_backupPath);
1163 if (!backupDir.exists()) {
1164 if (!backupDir.mkpath(m_backupPath)) {
1166 .arg(QDir::toNativeSeparators(m_backupPath))
);
1177 preferencesDialog.exec();
1184 if (!m_appFontDialog)
1185 m_appFontDialog =
new AppFontDialog(core()->topLevel());
1186 m_appFontDialog->show();
1187 m_appFontDialog->raise();
1190QPixmap
QDesignerActions::createPreviewPixmap(QDesignerFormWindowInterface *fw)
1192 const QCursor oldCursor =
core()->topLevel()->cursor();
1193 core()->topLevel()->setCursor(Qt::WaitCursor);
1195 QString errorMessage;
1196 const QPixmap pixmap = m_previewManager->createPreviewPixmap(fw, QString(), &errorMessage);
1197 core()->topLevel()->setCursor(oldCursor);
1198 if (pixmap.isNull()) {
1199 QMessageBox::warning(fw, tr(
"Preview failed"), errorMessage);
1208 if (settings.isCustomPreviewConfigurationEnabled())
1209 pc = settings.customPreviewConfiguration();
1215 const char *format =
"png";
1217 QDesignerFormWindowInterface *fw =
core()->formWindowManager()->activeFormWindow();
1222 const QString extension = QString::fromLatin1(format);
1223 const QString filter = tr(
"Image files (*.%1)").arg(extension);
1225 QString suggestion = fw->fileName();
1226 if (!suggestion.isEmpty())
1227 suggestion = QFileInfo(suggestion).baseName() + u'.' + extension;
1229 QFileDialog dialog(fw, tr(
"Save Image"), suggestion, filter);
1230 dialog.setAcceptMode(QFileDialog::AcceptSave);
1231 dialog.setDefaultSuffix(extension);
1234 if (dialog.exec() != QDialog::Accepted)
1236 const QString fileName = dialog.selectedFiles().constFirst();
1238 if (image.isNull()) {
1239 const QPixmap pixmap = createPreviewPixmap(fw);
1240 if (pixmap.isNull())
1243 image = pixmap.toImage();
1246 if (image.save(fileName, format)) {
1247 showStatusBarMessage(tr(
"Saved image %1.").arg(QFileInfo(fileName).fileName()));
1251 QMessageBox box(QMessageBox::Warning, tr(
"Save Image"),
1252 tr(
"The file %1 could not be written.").arg( fileName),
1253 QMessageBox::Retry|QMessageBox::Cancel, fw);
1254 if (box.exec() == QMessageBox::Cancel)
1261 const bool enabled = m_core->formWindowManager()->formWindowCount() == 0;
1263
1264
1265 static const QString disabledTip = tr(
"Please close all forms to enable the loading of additional fonts.");
1266 m_appFontAction->setEnabled(enabled);
1267 m_appFontAction->setStatusTip(enabled ? QString() : disabledTip);
1273 QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow();
1278 m_printer =
new QPrinter(QPrinter::HighResolution);
1280 m_printer->setFullPage(
false);
1283 const QPixmap pixmap = createPreviewPixmap(fw);
1284 if (pixmap.isNull())
1287 const QSizeF pixmapSize = pixmap.size();
1289 m_printer->setPageOrientation(pixmapSize.width() > pixmapSize.height() ?
1290 QPageLayout::Landscape : QPageLayout::Portrait);
1293 QPrintDialog dialog(m_printer, fw);
1297 const QCursor oldCursor = core()->topLevel()->cursor();
1298 core()->topLevel()->setCursor(Qt::WaitCursor);
1300 const double suggestedScaling =
static_cast<
double>(m_printer->physicalDpiX()) /
static_cast<
double>(fw->physicalDpiX());
1302 QPainter painter(m_printer);
1303 painter.setRenderHint(QPainter::SmoothPixmapTransform);
1306 const QRectF page = painter.viewport();
1307 const double maxScaling = qMin(page.size().width() / pixmapSize.width(), page.size().height() / pixmapSize.height());
1308 const double scaling = qMin(suggestedScaling, maxScaling);
1310 const double xOffset = page.left() + qMax(0.0, (page.size().width() - scaling * pixmapSize.width()) / 2.0);
1311 const double yOffset = page.top() + qMax(0.0, (page.size().height() - scaling * pixmapSize.height()) / 2.0);
1314 painter.translate(xOffset, yOffset);
1315 painter.scale(scaling, scaling);
1316 painter.drawPixmap(0, 0, pixmap);
1317 core()->topLevel()->setCursor(oldCursor);
1319 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)