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
newform.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// Qt-Security score:significant reason:default
4
5#include "newform.h"
10
11#include <newformwidget_p.h>
12
13#include <QtDesigner/abstractformeditor.h>
14
15#include <QtWidgets/qapplication.h>
16#include <QtWidgets/qboxlayout.h>
17#include <QtWidgets/qpushbutton.h>
18#include <QtWidgets/qdialogbuttonbox.h>
19#include <QtWidgets/qmenu.h>
20#include <QtWidgets/qcheckbox.h>
21#include <QtWidgets/qframe.h>
22#include <QtWidgets/qmessagebox.h>
23
24#include <QtGui/qaction.h>
25#include <QtGui/qactiongroup.h>
26
27#include <QtCore/qdir.h>
28#include <QtCore/qfileinfo.h>
29#include <QtCore/qdebug.h>
30#include <QtCore/qtemporaryfile.h>
31
33
34using namespace Qt::StringLiterals;
35
36NewForm::NewForm(QDesignerWorkbench *workbench, QWidget *parentWidget, const QString &fileName)
37 : QDialog(parentWidget, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint),
38 m_fileName(fileName),
39 m_newFormWidget(QDesignerNewFormWidgetInterface::createNewFormWidget(workbench->core())),
40 m_workbench(workbench),
41 m_chkShowOnStartup(new QCheckBox(tr("Show this Dialog on Startup"))),
42 m_createButton(new QPushButton(QApplication::translate("NewForm", "C&reate", nullptr))),
43 m_recentButton(new QPushButton(QApplication::translate("NewForm", "Recent", nullptr)))
44{
45 setWindowTitle(tr("New Form"));
46 QDesignerSettings settings(m_workbench->core());
47
48 auto *vBoxLayout = new QVBoxLayout(this);
49
50 connect(m_newFormWidget, &QDesignerNewFormWidgetInterface::templateActivated,
51 this, &NewForm::slotTemplateActivated);
52 connect(m_newFormWidget, &QDesignerNewFormWidgetInterface::currentTemplateChanged,
53 this, &NewForm::slotCurrentTemplateChanged);
54 vBoxLayout->addWidget(m_newFormWidget);
55
56 auto *horizontalLine = new QFrame;
57 horizontalLine->setFrameShape(QFrame::HLine);
58 horizontalLine->setFrameShadow(QFrame::Sunken);
59 vBoxLayout->addWidget(horizontalLine);
60
61 m_chkShowOnStartup->setChecked(settings.showNewFormOnStartup());
62 vBoxLayout->addWidget(m_chkShowOnStartup);
63
64 m_buttonBox = createButtonBox();
65 vBoxLayout->addWidget(m_buttonBox);
66
67 resize(500, 400);
68 slotCurrentTemplateChanged(m_newFormWidget->hasCurrentTemplate());
69}
70
71QDialogButtonBox *NewForm::createButtonBox()
72{
73 // Dialog buttons with 'recent files'
74 auto *buttonBox = new QDialogButtonBox;
75 buttonBox->addButton(QApplication::translate("NewForm", "&Close", nullptr),
76 QDialogButtonBox::RejectRole);
77 buttonBox->addButton(m_createButton, QDialogButtonBox::AcceptRole);
78 buttonBox->addButton(QApplication::translate("NewForm", "&Open...", nullptr),
79 QDialogButtonBox::ActionRole);
80 buttonBox->addButton(m_recentButton, QDialogButtonBox::ActionRole);
81 QDesignerActions *da = m_workbench->actionManager();
82 auto *recentFilesMenu = new QMenu(tr("&Recent Forms"), m_recentButton);
83 // Pop the "Recent Files" stuff in here.
84 const auto recentActions = da->recentFilesActions()->actions();
85 for (auto *action : recentActions) {
86 recentFilesMenu->addAction(action);
87 connect(action, &QAction::triggered, this, &NewForm::recentFileChosen);
88 }
89 m_recentButton->setMenu(recentFilesMenu);
90 connect(buttonBox, &QDialogButtonBox::clicked, this, &NewForm::slotButtonBoxClicked);
91 return buttonBox;
92}
93
95{
96 QDesignerSettings settings (m_workbench->core());
97 settings.setShowNewFormOnStartup(m_chkShowOnStartup->isChecked());
98}
99
100void NewForm::recentFileChosen()
101{
102 auto *action = qobject_cast<QAction *>(sender());
103 if (action && action->objectName() != "__qt_action_clear_menu_"_L1)
104 close();
105}
106
107void NewForm::slotCurrentTemplateChanged(bool templateSelected)
108{
109 if (templateSelected) {
110 m_createButton->setEnabled(true);
111 m_createButton->setDefault(true);
112 } else {
113 m_createButton->setEnabled(false);
114 }
115}
116
117void NewForm::slotTemplateActivated()
118{
119 m_createButton->animateClick();
120}
121
122void NewForm::slotButtonBoxClicked(QAbstractButton *btn)
123{
124 switch (m_buttonBox->buttonRole(btn)) {
125 case QDialogButtonBox::RejectRole:
126 reject();
127 break;
128 case QDialogButtonBox::ActionRole:
129 if (btn != m_recentButton) {
130 m_fileName.clear();
131 if (m_workbench->actionManager()->openForm(this))
132 accept();
133 }
134 break;
135 case QDialogButtonBox::AcceptRole: {
136 QString errorMessage;
137 if (openTemplate(&errorMessage)) {
138 accept();
139 } else {
140 QMessageBox::warning(this, tr("Read error"), errorMessage);
141 }
142 }
143 break;
144 default:
145 break;
146 }
147}
148
149bool NewForm::openTemplate(QString *ptrToErrorMessage)
150{
151 const QString contents = m_newFormWidget->currentTemplate(ptrToErrorMessage);
152 if (contents.isEmpty())
153 return false;
154 // Write to temporary file and open
155 QString tempPattern = QDir::tempPath();
156 if (!tempPattern.endsWith(QDir::separator())) // platform-dependant
157 tempPattern += QDir::separator();
158 tempPattern += "XXXXXX.ui"_L1;
159 QTemporaryFile tempFormFile(tempPattern);
160
161 tempFormFile.setAutoRemove(true);
162 if (!tempFormFile.open()) {
163 *ptrToErrorMessage = tr("A temporary form file could not be created in %1: %2")
164 .arg(QDir::toNativeSeparators(QDir::tempPath()), tempFormFile.errorString());
165 return false;
166 }
167 const QString tempFormFileName = tempFormFile.fileName();
168 tempFormFile.write(contents.toUtf8());
169 if (!tempFormFile.flush()) {
170 *ptrToErrorMessage = tr("The temporary form file %1 could not be written: %2")
171 .arg(QDir::toNativeSeparators(tempFormFileName), tempFormFile.errorString());
172 return false;
173 }
174 tempFormFile.close();
175 return m_workbench->openTemplate(tempFormFileName, m_fileName, ptrToErrorMessage);
176}
177
178QImage NewForm::grabForm(QDesignerFormEditorInterface *core,
179 QIODevice &file,
180 const QString &workingDir,
181 const qdesigner_internal::DeviceProfile &dp)
182{
183 return qdesigner_internal::NewFormWidget::grabForm(core, file, workingDir, dp);
184}
185
186QT_END_NAMESPACE
~NewForm() override
Definition newform.cpp:94
bool openForm(QWidget *parent)
QActionGroup * recentFilesActions() const
void setShowNewFormOnStartup(bool showIt)
QDesignerSettings(QDesignerFormEditorInterface *core)
bool showNewFormOnStartup() const
QDesignerActions * actionManager() const
QDesignerFormEditorInterface * core() const
friend class QWidget
Definition qpainter.h:432
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.