20 QDesignerFormWindowInterface *formWindow,
22 : QDialog(parent, Qt::Sheet),
24 m_formWindow(formWindow)
28 ui.templateNameEdit->setText(formWindow->mainContainer()->objectName());
29 ui.templateNameEdit->selectAll();
31 ui.templateNameEdit->setFocus();
33 QStringList paths = QDesignerSettings(m_core).formTemplatePaths();
34 ui.categoryCombo->addItems(paths);
35 ui.categoryCombo->addItem(tr(
"Add path..."));
36 m_addPathIndex = ui.categoryCombo->count() - 1;
37 connect(ui.templateNameEdit, &QLineEdit::textChanged,
38 this, &SaveFormAsTemplate::updateOKButton);
39 connect(ui.categoryCombo, &QComboBox::activated,
40 this, &SaveFormAsTemplate::checkToAddPath);
47 const QString name = ui.templateNameEdit->text();
48 QString templateFileName = ui.categoryCombo->currentText() + u'/' + name;
49 const auto extension =
".ui"_L1;
50 if (!templateFileName.endsWith(extension))
51 templateFileName.append(extension);
52 QFile file(templateFileName);
55 QMessageBox msgBox(QMessageBox::Information, tr(
"Template Exists"),
56 tr(
"A template with the name %1 already exists.\n"
57 "Do you want overwrite the template?").arg(name), QMessageBox::Cancel, m_formWindow);
58 msgBox.setDefaultButton(QMessageBox::Cancel);
59 QPushButton *overwriteButton = msgBox.addButton(tr(
"Overwrite Template"), QMessageBox::AcceptRole);
61 if (msgBox.clickedButton() != overwriteButton)
65 while (!file.open(QFile::WriteOnly)) {
66 if (QMessageBox::information(m_formWindow, tr(
"Open Error"),
67 tr(
"There was an error opening template %1 for writing. Reason: %2")
68 .arg(name, file.errorString()),
69 QMessageBox::Retry|QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Cancel) {
74 const QString origName = m_formWindow->fileName();
77 m_formWindow->setFileName(templateFileName);
78 QByteArray ba = m_formWindow->contents().toUtf8();
79 m_formWindow->setFileName(origName);
80 while (file.write(ba) != ba.size()) {
81 if (QMessageBox::information(m_formWindow, tr(
"Write Error"),
82 tr(
"There was an error writing the template %1 to disk. Reason: %2")
83 .arg(name, file.errorString()),
84 QMessageBox::Retry|QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Cancel) {
93 for (
int i = 0; i < m_addPathIndex; ++i)
94 sl << ui.categoryCombo->itemText(i);