1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the Qt Designer of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "saveformastemplate.h"
|
---|
43 | #include "qdesigner_settings.h"
|
---|
44 |
|
---|
45 | #include <QtCore/QFile>
|
---|
46 | #include <QtGui/QFileDialog>
|
---|
47 | #include <QtGui/QMessageBox>
|
---|
48 | #include <QtGui/QPushButton>
|
---|
49 |
|
---|
50 | #include <QtDesigner/abstractformeditor.h>
|
---|
51 | #include <QtDesigner/abstractformwindow.h>
|
---|
52 |
|
---|
53 | QT_BEGIN_NAMESPACE
|
---|
54 |
|
---|
55 | SaveFormAsTemplate::SaveFormAsTemplate(QDesignerFormEditorInterface *core,
|
---|
56 | QDesignerFormWindowInterface *formWindow,
|
---|
57 | QWidget *parent)
|
---|
58 | : QDialog(parent, Qt::Sheet),
|
---|
59 | m_core(core),
|
---|
60 | m_formWindow(formWindow)
|
---|
61 | {
|
---|
62 | ui.setupUi(this);
|
---|
63 | setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
---|
64 |
|
---|
65 | ui.templateNameEdit->setText(formWindow->mainContainer()->objectName());
|
---|
66 | ui.templateNameEdit->selectAll();
|
---|
67 |
|
---|
68 | ui.templateNameEdit->setFocus();
|
---|
69 |
|
---|
70 | QStringList paths = QDesignerSettings(m_core).formTemplatePaths();
|
---|
71 | ui.categoryCombo->addItems(paths);
|
---|
72 | ui.categoryCombo->addItem(tr("Add path..."));
|
---|
73 | m_addPathIndex = ui.categoryCombo->count() - 1;
|
---|
74 | connect(ui.templateNameEdit, SIGNAL(textChanged(QString)),
|
---|
75 | this, SLOT(updateOKButton(QString)));
|
---|
76 | connect(ui.categoryCombo, SIGNAL(activated(int)), this, SLOT(checkToAddPath(int)));
|
---|
77 | }
|
---|
78 |
|
---|
79 | SaveFormAsTemplate::~SaveFormAsTemplate()
|
---|
80 | {
|
---|
81 | }
|
---|
82 |
|
---|
83 | void SaveFormAsTemplate::accept()
|
---|
84 | {
|
---|
85 | QString templateFileName = ui.categoryCombo->currentText();
|
---|
86 | templateFileName += QLatin1Char('/');
|
---|
87 | const QString name = ui.templateNameEdit->text();
|
---|
88 | templateFileName += name;
|
---|
89 | const QString extension = QLatin1String(".ui");
|
---|
90 | if (!templateFileName.endsWith(extension))
|
---|
91 | templateFileName.append(extension);
|
---|
92 | QFile file(templateFileName);
|
---|
93 |
|
---|
94 | if (file.exists()) {
|
---|
95 | QMessageBox msgBox(QMessageBox::Information, tr("Template Exists"),
|
---|
96 | tr("A template with the name %1 already exists.\n"
|
---|
97 | "Do you want overwrite the template?").arg(name), QMessageBox::Cancel, m_formWindow);
|
---|
98 | msgBox.setDefaultButton(QMessageBox::Cancel);
|
---|
99 | QPushButton *overwriteButton = msgBox.addButton(tr("Overwrite Template"), QMessageBox::AcceptRole);
|
---|
100 | msgBox.exec();
|
---|
101 | if (msgBox.clickedButton() != overwriteButton)
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | while (!file.open(QFile::WriteOnly)) {
|
---|
106 | if (QMessageBox::information(m_formWindow, tr("Open Error"),
|
---|
107 | tr("There was an error opening template %1 for writing. Reason: %2").arg(name).arg(file.errorString()),
|
---|
108 | QMessageBox::Retry|QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Cancel) {
|
---|
109 | return;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | const QString origName = m_formWindow->fileName();
|
---|
114 | // ensure m_formWindow->contents() will convert properly resource paths to relative paths
|
---|
115 | // (relative to template location, not to the current form location)
|
---|
116 | m_formWindow->setFileName(templateFileName);
|
---|
117 | QByteArray ba = m_formWindow->contents().toUtf8();
|
---|
118 | m_formWindow->setFileName(origName);
|
---|
119 | while (file.write(ba) != ba.size()) {
|
---|
120 | if (QMessageBox::information(m_formWindow, tr("Write Error"),
|
---|
121 | tr("There was an error writing the template %1 to disk. Reason: %2").arg(name).arg(file.errorString()),
|
---|
122 | QMessageBox::Retry|QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Cancel) {
|
---|
123 | file.close();
|
---|
124 | file.remove();
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | file.reset();
|
---|
128 | }
|
---|
129 | // update the list of places too...
|
---|
130 | QStringList sl;
|
---|
131 | for (int i = 0; i < m_addPathIndex; ++i)
|
---|
132 | sl << ui.categoryCombo->itemText(i);
|
---|
133 |
|
---|
134 | QDesignerSettings(m_core).setFormTemplatePaths(sl);
|
---|
135 |
|
---|
136 | QDialog::accept();
|
---|
137 | }
|
---|
138 |
|
---|
139 | void SaveFormAsTemplate::updateOKButton(const QString &str)
|
---|
140 | {
|
---|
141 | QPushButton *okButton = ui.buttonBox->button(QDialogButtonBox::Ok);
|
---|
142 | okButton->setEnabled(!str.isEmpty());
|
---|
143 | }
|
---|
144 |
|
---|
145 | QString SaveFormAsTemplate::chooseTemplatePath(QWidget *parent)
|
---|
146 | {
|
---|
147 | QString rc = QFileDialog::getExistingDirectory(parent,
|
---|
148 | tr("Pick a directory to save templates in"));
|
---|
149 | if (rc.isEmpty())
|
---|
150 | return rc;
|
---|
151 |
|
---|
152 | if (rc.endsWith(QDir::separator()))
|
---|
153 | rc.remove(rc.size() - 1, 1);
|
---|
154 | return rc;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void SaveFormAsTemplate::checkToAddPath(int itemIndex)
|
---|
158 | {
|
---|
159 | if (itemIndex != m_addPathIndex)
|
---|
160 | return;
|
---|
161 |
|
---|
162 | const QString dir = chooseTemplatePath(this);
|
---|
163 | if (dir.isEmpty()) {
|
---|
164 | ui.categoryCombo->setCurrentIndex(0);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | ui.categoryCombo->insertItem(m_addPathIndex, dir);
|
---|
169 | ui.categoryCombo->setCurrentIndex(m_addPathIndex);
|
---|
170 | ++m_addPathIndex;
|
---|
171 | }
|
---|
172 |
|
---|
173 | QT_END_NAMESPACE
|
---|