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