author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
//! [0] |
|
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "mainwindow.h" |
|
45 |
//! [0] |
|
46 |
||
47 |
//! [1] |
|
48 |
MainWindow::MainWindow() |
|
49 |
{ |
|
50 |
textEdit = new QTextEdit; |
|
51 |
setCentralWidget(textEdit); |
|
52 |
||
53 |
createActions(); |
|
54 |
createMenus(); |
|
55 |
createToolBars(); |
|
56 |
createStatusBar(); |
|
57 |
createDockWindows(); |
|
58 |
||
59 |
setWindowTitle(tr("Dock Widgets")); |
|
60 |
||
61 |
newLetter(); |
|
62 |
setUnifiedTitleAndToolBarOnMac(true); |
|
63 |
} |
|
64 |
//! [1] |
|
65 |
||
66 |
//! [2] |
|
67 |
void MainWindow::newLetter() |
|
68 |
{ |
|
69 |
textEdit->clear(); |
|
70 |
||
71 |
QTextCursor cursor(textEdit->textCursor()); |
|
72 |
cursor.movePosition(QTextCursor::Start); |
|
73 |
QTextFrame *topFrame = cursor.currentFrame(); |
|
74 |
QTextFrameFormat topFrameFormat = topFrame->frameFormat(); |
|
75 |
topFrameFormat.setPadding(16); |
|
76 |
topFrame->setFrameFormat(topFrameFormat); |
|
77 |
||
78 |
QTextCharFormat textFormat; |
|
79 |
QTextCharFormat boldFormat; |
|
80 |
boldFormat.setFontWeight(QFont::Bold); |
|
81 |
QTextCharFormat italicFormat; |
|
82 |
italicFormat.setFontItalic(true); |
|
83 |
||
84 |
QTextTableFormat tableFormat; |
|
85 |
tableFormat.setBorder(1); |
|
86 |
tableFormat.setCellPadding(16); |
|
87 |
tableFormat.setAlignment(Qt::AlignRight); |
|
88 |
cursor.insertTable(1, 1, tableFormat); |
|
89 |
cursor.insertText("The Firm", boldFormat); |
|
90 |
cursor.insertBlock(); |
|
91 |
cursor.insertText("321 City Street", textFormat); |
|
92 |
cursor.insertBlock(); |
|
93 |
cursor.insertText("Industry Park"); |
|
94 |
cursor.insertBlock(); |
|
95 |
cursor.insertText("Some Country"); |
|
96 |
cursor.setPosition(topFrame->lastPosition()); |
|
97 |
cursor.insertText(QDate::currentDate().toString("d MMMM yyyy"), textFormat); |
|
98 |
cursor.insertBlock(); |
|
99 |
cursor.insertBlock(); |
|
100 |
cursor.insertText("Dear ", textFormat); |
|
101 |
cursor.insertText("NAME", italicFormat); |
|
102 |
cursor.insertText(",", textFormat); |
|
103 |
for (int i = 0; i < 3; ++i) |
|
104 |
cursor.insertBlock(); |
|
105 |
cursor.insertText(tr("Yours sincerely,"), textFormat); |
|
106 |
for (int i = 0; i < 3; ++i) |
|
107 |
cursor.insertBlock(); |
|
108 |
cursor.insertText("The Boss", textFormat); |
|
109 |
cursor.insertBlock(); |
|
110 |
cursor.insertText("ADDRESS", italicFormat); |
|
111 |
} |
|
112 |
//! [2] |
|
113 |
||
114 |
//! [3] |
|
115 |
void MainWindow::print() |
|
116 |
{ |
|
117 |
#ifndef QT_NO_PRINTDIALOG |
|
118 |
QTextDocument *document = textEdit->document(); |
|
119 |
QPrinter printer; |
|
120 |
||
121 |
QPrintDialog *dlg = new QPrintDialog(&printer, this); |
|
122 |
if (dlg->exec() != QDialog::Accepted) |
|
123 |
return; |
|
124 |
||
125 |
document->print(&printer); |
|
126 |
||
127 |
statusBar()->showMessage(tr("Ready"), 2000); |
|
128 |
#endif |
|
129 |
} |
|
130 |
//! [3] |
|
131 |
||
132 |
//! [4] |
|
133 |
void MainWindow::save() |
|
134 |
{ |
|
135 |
QString fileName = QFileDialog::getSaveFileName(this, |
|
136 |
tr("Choose a file name"), ".", |
|
137 |
tr("HTML (*.html *.htm)")); |
|
138 |
if (fileName.isEmpty()) |
|
139 |
return; |
|
140 |
QFile file(fileName); |
|
141 |
if (!file.open(QFile::WriteOnly | QFile::Text)) { |
|
142 |
QMessageBox::warning(this, tr("Dock Widgets"), |
|
143 |
tr("Cannot write file %1:\n%2.") |
|
144 |
.arg(fileName) |
|
145 |
.arg(file.errorString())); |
|
146 |
return; |
|
147 |
} |
|
148 |
||
149 |
QTextStream out(&file); |
|
150 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
|
151 |
out << textEdit->toHtml(); |
|
152 |
QApplication::restoreOverrideCursor(); |
|
153 |
||
154 |
statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000); |
|
155 |
} |
|
156 |
//! [4] |
|
157 |
||
158 |
//! [5] |
|
159 |
void MainWindow::undo() |
|
160 |
{ |
|
161 |
QTextDocument *document = textEdit->document(); |
|
162 |
document->undo(); |
|
163 |
} |
|
164 |
//! [5] |
|
165 |
||
166 |
//! [6] |
|
167 |
void MainWindow::insertCustomer(const QString &customer) |
|
168 |
{ |
|
169 |
if (customer.isEmpty()) |
|
170 |
return; |
|
171 |
QStringList customerList = customer.split(", "); |
|
172 |
QTextDocument *document = textEdit->document(); |
|
173 |
QTextCursor cursor = document->find("NAME"); |
|
174 |
if (!cursor.isNull()) { |
|
175 |
cursor.beginEditBlock(); |
|
176 |
cursor.insertText(customerList.at(0)); |
|
177 |
QTextCursor oldcursor = cursor; |
|
178 |
cursor = document->find("ADDRESS"); |
|
179 |
if (!cursor.isNull()) { |
|
180 |
for (int i = 1; i < customerList.size(); ++i) { |
|
181 |
cursor.insertBlock(); |
|
182 |
cursor.insertText(customerList.at(i)); |
|
183 |
} |
|
184 |
cursor.endEditBlock(); |
|
185 |
} |
|
186 |
else |
|
187 |
oldcursor.endEditBlock(); |
|
188 |
} |
|
189 |
} |
|
190 |
//! [6] |
|
191 |
||
192 |
//! [7] |
|
193 |
void MainWindow::addParagraph(const QString ¶graph) |
|
194 |
{ |
|
195 |
if (paragraph.isEmpty()) |
|
196 |
return; |
|
197 |
QTextDocument *document = textEdit->document(); |
|
198 |
QTextCursor cursor = document->find(tr("Yours sincerely,")); |
|
199 |
if (cursor.isNull()) |
|
200 |
return; |
|
201 |
cursor.beginEditBlock(); |
|
202 |
cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2); |
|
203 |
cursor.insertBlock(); |
|
204 |
cursor.insertText(paragraph); |
|
205 |
cursor.insertBlock(); |
|
206 |
cursor.endEditBlock(); |
|
207 |
||
208 |
} |
|
209 |
//! [7] |
|
210 |
||
211 |
void MainWindow::about() |
|
212 |
{ |
|
213 |
QMessageBox::about(this, tr("About Dock Widgets"), |
|
214 |
tr("The <b>Dock Widgets</b> example demonstrates how to " |
|
215 |
"use Qt's dock widgets. You can enter your own text, " |
|
216 |
"click a customer to add a customer name and " |
|
217 |
"address, and click standard paragraphs to add them.")); |
|
218 |
} |
|
219 |
||
220 |
void MainWindow::createActions() |
|
221 |
{ |
|
222 |
newLetterAct = new QAction(QIcon(":/images/new.png"), tr("&New Letter"), |
|
223 |
this); |
|
224 |
newLetterAct->setShortcuts(QKeySequence::New); |
|
225 |
newLetterAct->setStatusTip(tr("Create a new form letter")); |
|
226 |
connect(newLetterAct, SIGNAL(triggered()), this, SLOT(newLetter())); |
|
227 |
||
228 |
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save..."), this); |
|
229 |
saveAct->setShortcuts(QKeySequence::Save); |
|
230 |
saveAct->setStatusTip(tr("Save the current form letter")); |
|
231 |
connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); |
|
232 |
||
233 |
printAct = new QAction(QIcon(":/images/print.png"), tr("&Print..."), this); |
|
234 |
printAct->setShortcuts(QKeySequence::Print); |
|
235 |
printAct->setStatusTip(tr("Print the current form letter")); |
|
236 |
connect(printAct, SIGNAL(triggered()), this, SLOT(print())); |
|
237 |
||
238 |
undoAct = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this); |
|
239 |
undoAct->setShortcuts(QKeySequence::Undo); |
|
240 |
undoAct->setStatusTip(tr("Undo the last editing action")); |
|
241 |
connect(undoAct, SIGNAL(triggered()), this, SLOT(undo())); |
|
242 |
||
243 |
quitAct = new QAction(tr("&Quit"), this); |
|
244 |
quitAct->setShortcuts(QKeySequence::Quit); |
|
245 |
quitAct->setStatusTip(tr("Quit the application")); |
|
246 |
connect(quitAct, SIGNAL(triggered()), this, SLOT(close())); |
|
247 |
||
248 |
aboutAct = new QAction(tr("&About"), this); |
|
249 |
aboutAct->setStatusTip(tr("Show the application's About box")); |
|
250 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
|
251 |
||
252 |
aboutQtAct = new QAction(tr("About &Qt"), this); |
|
253 |
aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); |
|
254 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|
255 |
} |
|
256 |
||
257 |
void MainWindow::createMenus() |
|
258 |
{ |
|
259 |
fileMenu = menuBar()->addMenu(tr("&File")); |
|
260 |
fileMenu->addAction(newLetterAct); |
|
261 |
fileMenu->addAction(saveAct); |
|
262 |
fileMenu->addAction(printAct); |
|
263 |
fileMenu->addSeparator(); |
|
264 |
fileMenu->addAction(quitAct); |
|
265 |
||
266 |
editMenu = menuBar()->addMenu(tr("&Edit")); |
|
267 |
editMenu->addAction(undoAct); |
|
268 |
||
269 |
viewMenu = menuBar()->addMenu(tr("&View")); |
|
270 |
||
271 |
menuBar()->addSeparator(); |
|
272 |
||
273 |
helpMenu = menuBar()->addMenu(tr("&Help")); |
|
274 |
helpMenu->addAction(aboutAct); |
|
275 |
helpMenu->addAction(aboutQtAct); |
|
276 |
} |
|
277 |
||
278 |
void MainWindow::createToolBars() |
|
279 |
{ |
|
280 |
fileToolBar = addToolBar(tr("File")); |
|
281 |
fileToolBar->addAction(newLetterAct); |
|
282 |
fileToolBar->addAction(saveAct); |
|
283 |
fileToolBar->addAction(printAct); |
|
284 |
||
285 |
editToolBar = addToolBar(tr("Edit")); |
|
286 |
editToolBar->addAction(undoAct); |
|
287 |
} |
|
288 |
||
289 |
//! [8] |
|
290 |
void MainWindow::createStatusBar() |
|
291 |
{ |
|
292 |
statusBar()->showMessage(tr("Ready")); |
|
293 |
} |
|
294 |
//! [8] |
|
295 |
||
296 |
//! [9] |
|
297 |
void MainWindow::createDockWindows() |
|
298 |
{ |
|
299 |
QDockWidget *dock = new QDockWidget(tr("Customers"), this); |
|
300 |
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); |
|
301 |
customerList = new QListWidget(dock); |
|
302 |
customerList->addItems(QStringList() |
|
303 |
<< "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton" |
|
304 |
<< "Jane Doe, Memorabilia, 23 Watersedge, Beaton" |
|
305 |
<< "Tammy Shea, Tiblanka, 38 Sea Views, Carlton" |
|
306 |
<< "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal" |
|
307 |
<< "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston" |
|
308 |
<< "Sally Hobart, Tiroli Tea, 67 Long River, Fedula"); |
|
309 |
dock->setWidget(customerList); |
|
310 |
addDockWidget(Qt::RightDockWidgetArea, dock); |
|
311 |
viewMenu->addAction(dock->toggleViewAction()); |
|
312 |
||
313 |
dock = new QDockWidget(tr("Paragraphs"), this); |
|
314 |
paragraphsList = new QListWidget(dock); |
|
315 |
paragraphsList->addItems(QStringList() |
|
316 |
<< "Thank you for your payment which we have received today." |
|
317 |
<< "Your order has been dispatched and should be with you " |
|
318 |
"within 28 days." |
|
319 |
<< "We have dispatched those items that were in stock. The " |
|
320 |
"rest of your order will be dispatched once all the " |
|
321 |
"remaining items have arrived at our warehouse. No " |
|
322 |
"additional shipping charges will be made." |
|
323 |
<< "You made a small overpayment (less than $5) which we " |
|
324 |
"will keep on account for you, or return at your request." |
|
325 |
<< "You made a small underpayment (less than $1), but we have " |
|
326 |
"sent your order anyway. We'll add this underpayment to " |
|
327 |
"your next bill." |
|
328 |
<< "Unfortunately you did not send enough money. Please remit " |
|
329 |
"an additional $. Your order will be dispatched as soon as " |
|
330 |
"the complete amount has been received." |
|
331 |
<< "You made an overpayment (more than $5). Do you wish to " |
|
332 |
"buy more items, or should we return the excess to you?"); |
|
333 |
dock->setWidget(paragraphsList); |
|
334 |
addDockWidget(Qt::RightDockWidgetArea, dock); |
|
335 |
viewMenu->addAction(dock->toggleViewAction()); |
|
336 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
connect(customerList, SIGNAL(currentTextChanged(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
338 |
this, SLOT(insertCustomer(QString))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
339 |
connect(paragraphsList, SIGNAL(currentTextChanged(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
this, SLOT(addParagraph(QString))); |
0 | 341 |
} |
342 |
//! [9] |