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