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