10#include <newformwidget_p.h>
12#include <QtDesigner/abstractformeditor.h>
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>
23#include <QtGui/qaction.h>
24#include <QtGui/qactiongroup.h>
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>
34using namespace Qt::StringLiterals;
37 : QDialog(parentWidget, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint),
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)))
45 setWindowTitle(tr(
"New Form"));
48 QVBoxLayout *vBoxLayout =
new QVBoxLayout;
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);
56 QFrame *horizontalLine =
new QFrame;
57 horizontalLine->setFrameShape(QFrame::HLine);
58 horizontalLine->setFrameShadow(QFrame::Sunken);
59 vBoxLayout->addWidget(horizontalLine);
62 vBoxLayout->addWidget(m_chkShowOnStartup);
64 m_buttonBox = createButtonBox();
65 vBoxLayout->addWidget(m_buttonBox);
66 setLayout(vBoxLayout);
69 slotCurrentTemplateChanged(m_newFormWidget->hasCurrentTemplate());
72QDialogButtonBox *
NewForm::createButtonBox()
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);
83 QMenu *recentFilesMenu =
new QMenu(tr(
"&Recent Forms"), m_recentButton);
86 for (
auto action : recentActions) {
87 recentFilesMenu->addAction(action);
88 connect(action, &QAction::triggered,
this, &NewForm::recentFileChosen);
90 m_recentButton->setMenu(recentFilesMenu);
91 connect(buttonBox, &QDialogButtonBox::clicked,
this, &NewForm::slotButtonBoxClicked);
101void NewForm::recentFileChosen()
103 QAction *action = qobject_cast<QAction *>(sender());
104 if (action && action->objectName() !=
"__qt_action_clear_menu_"_L1)
108void NewForm::slotCurrentTemplateChanged(
bool templateSelected)
110 if (templateSelected) {
111 m_createButton->setEnabled(
true);
112 m_createButton->setDefault(
true);
114 m_createButton->setEnabled(
false);
118void NewForm::slotTemplateActivated()
120 m_createButton->animateClick();
123void NewForm::slotButtonBoxClicked(QAbstractButton *btn)
125 switch (m_buttonBox->buttonRole(btn)) {
126 case QDialogButtonBox::RejectRole:
129 case QDialogButtonBox::ActionRole:
130 if (btn != m_recentButton) {
136 case QDialogButtonBox::AcceptRole: {
137 QString errorMessage;
138 if (openTemplate(&errorMessage)) {
141 QMessageBox::warning(
this, tr(
"Read error"), errorMessage);
150bool NewForm::openTemplate(QString *ptrToErrorMessage)
152 const QString contents = m_newFormWidget->currentTemplate(ptrToErrorMessage);
153 if (contents.isEmpty())
156 QString tempPattern = QDir::tempPath();
157 if (!tempPattern.endsWith(QDir::separator()))
158 tempPattern += QDir::separator();
159 tempPattern +=
"XXXXXX.ui"_L1;
160 QTemporaryFile tempFormFile(tempPattern);
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());
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());
175 tempFormFile.close();
176 return m_workbench->openTemplate(tempFormFileName, m_fileName, ptrToErrorMessage);
179QImage
NewForm::grabForm(QDesignerFormEditorInterface *core,
181 const QString &workingDir,
184 return qdesigner_internal::NewFormWidget::grabForm(core, file, workingDir, dp);
bool openForm(QWidget *parent)
QActionGroup * recentFilesActions() const
void setShowNewFormOnStartup(bool showIt)
QDesignerSettings(QDesignerFormEditorInterface *core)
bool showNewFormOnStartup() const
QDesignerActions * actionManager() const
QDesignerFormEditorInterface * core() const
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.