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
saveformastemplate.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// Qt-Security score:significant reason:default
4
7
8#include <QtCore/qfile.h>
9#include <QtWidgets/qfiledialog.h>
10#include <QtWidgets/qmessagebox.h>
11#include <QtWidgets/qpushbutton.h>
12
13#include <QtDesigner/abstractformeditor.h>
14#include <QtDesigner/abstractformwindow.h>
15
17
18using namespace Qt::StringLiterals;
19
20SaveFormAsTemplate::SaveFormAsTemplate(QDesignerFormEditorInterface *core,
21 QDesignerFormWindowInterface *formWindow,
22 QWidget *parent)
23 : QDialog(parent, Qt::Sheet),
24 m_core(core),
25 m_formWindow(formWindow)
26{
27 ui.setupUi(this);
28
29 ui.templateNameEdit->setText(formWindow->mainContainer()->objectName());
30 ui.templateNameEdit->selectAll();
31
32 ui.templateNameEdit->setFocus();
33
34 QStringList paths = QDesignerSettings(m_core).formTemplatePaths();
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,
41 this, &SaveFormAsTemplate::checkToAddPath);
42}
43
45
46void SaveFormAsTemplate::accept()
47{
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);
54
55 if (file.exists()) {
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);
61 msgBox.exec();
62 if (msgBox.clickedButton() != overwriteButton)
63 return;
64 }
65
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) {
71 return;
72 }
73 }
74
75 const QString origName = m_formWindow->fileName();
76 // ensure m_formWindow->contents() will convert properly resource paths to relative paths
77 // (relative to template location, not to the current form location)
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) {
86 file.close();
87 file.remove();
88 return;
89 }
90 file.reset();
91 }
92 // update the list of places too...
93 QStringList sl;
94 for (int i = 0; i < m_addPathIndex; ++i)
95 sl << ui.categoryCombo->itemText(i);
96
97 QDesignerSettings(m_core).setFormTemplatePaths(sl);
98
99 QDialog::accept();
100}
101
102void SaveFormAsTemplate::updateOKButton(const QString &str)
103{
104 QPushButton *okButton = ui.buttonBox->button(QDialogButtonBox::Ok);
105 okButton->setEnabled(!str.isEmpty());
106}
107
108QString SaveFormAsTemplate::chooseTemplatePath(QWidget *parent)
109{
110 QString rc = QFileDialog::getExistingDirectory(parent,
111 tr("Pick a directory to save templates in"));
112 if (rc.isEmpty())
113 return rc;
114
115 if (rc.endsWith(QDir::separator()))
116 rc.remove(rc.size() - 1, 1);
117 return rc;
118}
119
120void SaveFormAsTemplate::checkToAddPath(int itemIndex)
121{
122 if (itemIndex != m_addPathIndex)
123 return;
124
125 const QString dir = chooseTemplatePath(this);
126 if (dir.isEmpty()) {
127 ui.categoryCombo->setCurrentIndex(0);
128 return;
129 }
130
131 ui.categoryCombo->insertItem(m_addPathIndex, dir);
132 ui.categoryCombo->setCurrentIndex(m_addPathIndex);
133 ++m_addPathIndex;
134}
135
136QT_END_NAMESPACE
QDesignerSettings(QDesignerFormEditorInterface *core)
friend class QWidget
Definition qpainter.h:432
~SaveFormAsTemplate() override
Combined button and popup list for selecting options.