|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "codedialog_p.h" |
|
43 #include "qdesigner_utils_p.h" |
|
44 #include "iconloader_p.h" |
|
45 |
|
46 #include <texteditfindwidget.h> |
|
47 |
|
48 #include <QtGui/QAction> |
|
49 #include <QtGui/QApplication> |
|
50 #include <QtGui/QClipboard> |
|
51 #include <QtGui/QDialogButtonBox> |
|
52 #include <QtGui/QFileDialog> |
|
53 #include <QtGui/QIcon> |
|
54 #include <QtGui/QKeyEvent> |
|
55 #include <QtGui/QMessageBox> |
|
56 #include <QtGui/QPushButton> |
|
57 #include <QtGui/QTextEdit> |
|
58 #include <QtGui/QToolBar> |
|
59 #include <QtGui/QVBoxLayout> |
|
60 |
|
61 #include <QtCore/QDebug> |
|
62 #include <QtCore/QDir> |
|
63 #include <QtCore/QTemporaryFile> |
|
64 |
|
65 QT_BEGIN_NAMESPACE |
|
66 |
|
67 namespace qdesigner_internal { |
|
68 // ----------------- CodeDialogPrivate |
|
69 struct CodeDialog::CodeDialogPrivate { |
|
70 CodeDialogPrivate(); |
|
71 |
|
72 QTextEdit *m_textEdit; |
|
73 TextEditFindWidget *m_findWidget; |
|
74 QString m_formFileName; |
|
75 }; |
|
76 |
|
77 CodeDialog::CodeDialogPrivate::CodeDialogPrivate() |
|
78 : m_textEdit(new QTextEdit) |
|
79 , m_findWidget(new TextEditFindWidget) |
|
80 { |
|
81 } |
|
82 |
|
83 // ----------------- CodeDialog |
|
84 CodeDialog::CodeDialog(QWidget *parent) : |
|
85 QDialog(parent), |
|
86 m_impl(new CodeDialogPrivate) |
|
87 { |
|
88 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
|
89 QVBoxLayout *vBoxLayout = new QVBoxLayout; |
|
90 |
|
91 // Edit tool bar |
|
92 QToolBar *toolBar = new QToolBar; |
|
93 |
|
94 const QIcon saveIcon = createIconSet(QLatin1String("filesave.png")); |
|
95 QAction *saveAction = toolBar->addAction(saveIcon, tr("Save...")); |
|
96 connect(saveAction, SIGNAL(triggered()), this, SLOT(slotSaveAs())); |
|
97 |
|
98 const QIcon copyIcon = createIconSet(QLatin1String("editcopy.png")); |
|
99 QAction *copyAction = toolBar->addAction(copyIcon, tr("Copy All")); |
|
100 connect(copyAction, SIGNAL(triggered()), this, SLOT(copyAll())); |
|
101 |
|
102 QAction *findAction = toolBar->addAction( |
|
103 TextEditFindWidget::findIconSet(), |
|
104 tr("&Find in Text..."), |
|
105 m_impl->m_findWidget, SLOT(activate())); |
|
106 findAction->setShortcut(QKeySequence::Find); |
|
107 |
|
108 vBoxLayout->addWidget(toolBar); |
|
109 |
|
110 // Edit |
|
111 m_impl->m_textEdit->setReadOnly(true); |
|
112 m_impl->m_textEdit->setMinimumSize(QSize( |
|
113 m_impl->m_findWidget->minimumSize().width(), |
|
114 500)); |
|
115 vBoxLayout->addWidget(m_impl->m_textEdit); |
|
116 |
|
117 // Find |
|
118 m_impl->m_findWidget->setTextEdit(m_impl->m_textEdit); |
|
119 vBoxLayout->addWidget(m_impl->m_findWidget); |
|
120 |
|
121 // Button box |
|
122 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); |
|
123 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); |
|
124 |
|
125 // Disable auto default |
|
126 QPushButton *closeButton = buttonBox->button(QDialogButtonBox::Close); |
|
127 closeButton->setAutoDefault(false); |
|
128 vBoxLayout->addWidget(buttonBox); |
|
129 |
|
130 setLayout(vBoxLayout); |
|
131 } |
|
132 |
|
133 CodeDialog::~CodeDialog() |
|
134 { |
|
135 delete m_impl; |
|
136 } |
|
137 |
|
138 void CodeDialog::setCode(const QString &code) |
|
139 { |
|
140 m_impl->m_textEdit->setPlainText(code); |
|
141 } |
|
142 |
|
143 QString CodeDialog::code() const |
|
144 { |
|
145 return m_impl->m_textEdit->toPlainText(); |
|
146 } |
|
147 |
|
148 void CodeDialog::setFormFileName(const QString &f) |
|
149 { |
|
150 m_impl->m_formFileName = f; |
|
151 } |
|
152 |
|
153 QString CodeDialog::formFileName() const |
|
154 { |
|
155 return m_impl->m_formFileName; |
|
156 } |
|
157 |
|
158 bool CodeDialog::generateCode(const QDesignerFormWindowInterface *fw, |
|
159 QString *code, |
|
160 QString *errorMessage) |
|
161 { |
|
162 // Generate temporary file name similar to form file name |
|
163 // (for header guards) |
|
164 QString tempPattern = QDir::tempPath(); |
|
165 if (!tempPattern.endsWith(QDir::separator())) // platform-dependant |
|
166 tempPattern += QDir::separator(); |
|
167 const QString fileName = fw->fileName(); |
|
168 if (fileName.isEmpty()) { |
|
169 tempPattern += QLatin1String("designer"); |
|
170 } else { |
|
171 tempPattern += QFileInfo(fileName).baseName(); |
|
172 } |
|
173 tempPattern += QLatin1String("XXXXXX.ui"); |
|
174 // Write to temp file |
|
175 QTemporaryFile tempFormFile(tempPattern); |
|
176 |
|
177 tempFormFile.setAutoRemove(true); |
|
178 if (!tempFormFile.open()) { |
|
179 *errorMessage = tr("A temporary form file could not be created in %1.").arg(QDir::tempPath()); |
|
180 return false; |
|
181 } |
|
182 const QString tempFormFileName = tempFormFile.fileName(); |
|
183 tempFormFile.write(fw->contents().toUtf8()); |
|
184 if (!tempFormFile.flush()) { |
|
185 *errorMessage = tr("The temporary form file %1 could not be written.").arg(tempFormFileName); |
|
186 return false; |
|
187 } |
|
188 tempFormFile.close(); |
|
189 // Run uic |
|
190 QByteArray rc; |
|
191 if (!runUIC(tempFormFileName, UIC_GenerateCode, rc, *errorMessage)) |
|
192 return false; |
|
193 *code = QString::fromUtf8(rc); |
|
194 return true; |
|
195 } |
|
196 |
|
197 bool CodeDialog::showCodeDialog(const QDesignerFormWindowInterface *fw, |
|
198 QWidget *parent, |
|
199 QString *errorMessage) |
|
200 { |
|
201 QString code; |
|
202 if (!generateCode(fw, &code, errorMessage)) |
|
203 return false; |
|
204 |
|
205 CodeDialog dialog(parent); |
|
206 dialog.setWindowTitle(tr("%1 - [Code]").arg(fw->mainContainer()->windowTitle())); |
|
207 dialog.setCode(code); |
|
208 dialog.setFormFileName(fw->fileName()); |
|
209 dialog.exec(); |
|
210 return true; |
|
211 } |
|
212 |
|
213 void CodeDialog::slotSaveAs() |
|
214 { |
|
215 // build the default relative name 'ui_sth.h' |
|
216 const QString headerSuffix = QString(QLatin1Char('h')); |
|
217 QString filter; |
|
218 const QString uiFile = formFileName(); |
|
219 |
|
220 if (!uiFile.isEmpty()) { |
|
221 filter = QLatin1String("ui_"); |
|
222 filter += QFileInfo(uiFile).baseName(); |
|
223 filter += QLatin1Char('.'); |
|
224 filter += headerSuffix; |
|
225 } |
|
226 // file dialog |
|
227 while (true) { |
|
228 const QString fileName = |
|
229 QFileDialog::getSaveFileName (this, tr("Save Code"), filter, tr("Header Files (*.%1)").arg(headerSuffix)); |
|
230 if (fileName.isEmpty()) |
|
231 break; |
|
232 |
|
233 QFile file(fileName); |
|
234 if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) { |
|
235 warning(tr("The file %1 could not be opened: %2").arg(fileName).arg(file.errorString())); |
|
236 continue; |
|
237 } |
|
238 file.write(code().toUtf8()); |
|
239 if (!file.flush()) { |
|
240 warning(tr("The file %1 could not be written: %2").arg(fileName).arg(file.errorString())); |
|
241 continue; |
|
242 } |
|
243 file.close(); |
|
244 break; |
|
245 } |
|
246 } |
|
247 |
|
248 void CodeDialog::warning(const QString &msg) |
|
249 { |
|
250 QMessageBox::warning( |
|
251 this, tr("%1 - Error").arg(windowTitle()), |
|
252 msg, QMessageBox::Close); |
|
253 } |
|
254 |
|
255 void CodeDialog::copyAll() |
|
256 { |
|
257 QApplication::clipboard()->setText(code()); |
|
258 } |
|
259 |
|
260 } // namespace qdesigner_internal |
|
261 |
|
262 QT_END_NAMESPACE |