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