source: trunk/examples/mainwindows/dockwidgets/mainwindow.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 11.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples 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 [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//! [0]
43#include <QtGui>
44
45#include "mainwindow.h"
46//! [0]
47
48//! [1]
49MainWindow::MainWindow()
50{
51 textEdit = new QTextEdit;
52 setCentralWidget(textEdit);
53
54 createActions();
55 createMenus();
56 createToolBars();
57 createStatusBar();
58 createDockWindows();
59
60 setWindowTitle(tr("Dock Widgets"));
61
62 newLetter();
63 setUnifiedTitleAndToolBarOnMac(true);
64}
65//! [1]
66
67//! [2]
68void MainWindow::newLetter()
69{
70 textEdit->clear();
71
72 QTextCursor cursor(textEdit->textCursor());
73 cursor.movePosition(QTextCursor::Start);
74 QTextFrame *topFrame = cursor.currentFrame();
75 QTextFrameFormat topFrameFormat = topFrame->frameFormat();
76 topFrameFormat.setPadding(16);
77 topFrame->setFrameFormat(topFrameFormat);
78
79 QTextCharFormat textFormat;
80 QTextCharFormat boldFormat;
81 boldFormat.setFontWeight(QFont::Bold);
82 QTextCharFormat italicFormat;
83 italicFormat.setFontItalic(true);
84
85 QTextTableFormat tableFormat;
86 tableFormat.setBorder(1);
87 tableFormat.setCellPadding(16);
88 tableFormat.setAlignment(Qt::AlignRight);
89 cursor.insertTable(1, 1, tableFormat);
90 cursor.insertText("The Firm", boldFormat);
91 cursor.insertBlock();
92 cursor.insertText("321 City Street", textFormat);
93 cursor.insertBlock();
94 cursor.insertText("Industry Park");
95 cursor.insertBlock();
96 cursor.insertText("Some Country");
97 cursor.setPosition(topFrame->lastPosition());
98 cursor.insertText(QDate::currentDate().toString("d MMMM yyyy"), textFormat);
99 cursor.insertBlock();
100 cursor.insertBlock();
101 cursor.insertText("Dear ", textFormat);
102 cursor.insertText("NAME", italicFormat);
103 cursor.insertText(",", textFormat);
104 for (int i = 0; i < 3; ++i)
105 cursor.insertBlock();
106 cursor.insertText(tr("Yours sincerely,"), textFormat);
107 for (int i = 0; i < 3; ++i)
108 cursor.insertBlock();
109 cursor.insertText("The Boss", textFormat);
110 cursor.insertBlock();
111 cursor.insertText("ADDRESS", italicFormat);
112}
113//! [2]
114
115//! [3]
116void MainWindow::print()
117{
118#ifndef QT_NO_PRINTDIALOG
119 QTextDocument *document = textEdit->document();
120 QPrinter printer;
121
122 QPrintDialog *dlg = new QPrintDialog(&printer, this);
123 if (dlg->exec() != QDialog::Accepted)
124 return;
125
126 document->print(&printer);
127
128 statusBar()->showMessage(tr("Ready"), 2000);
129#endif
130}
131//! [3]
132
133//! [4]
134void MainWindow::save()
135{
136 QString fileName = QFileDialog::getSaveFileName(this,
137 tr("Choose a file name"), ".",
138 tr("HTML (*.html *.htm)"));
139 if (fileName.isEmpty())
140 return;
141 QFile file(fileName);
142 if (!file.open(QFile::WriteOnly | QFile::Text)) {
143 QMessageBox::warning(this, tr("Dock Widgets"),
144 tr("Cannot write file %1:\n%2.")
145 .arg(fileName)
146 .arg(file.errorString()));
147 return;
148 }
149
150 QTextStream out(&file);
151 QApplication::setOverrideCursor(Qt::WaitCursor);
152 out << textEdit->toHtml();
153 QApplication::restoreOverrideCursor();
154
155 statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000);
156}
157//! [4]
158
159//! [5]
160void MainWindow::undo()
161{
162 QTextDocument *document = textEdit->document();
163 document->undo();
164}
165//! [5]
166
167//! [6]
168void MainWindow::insertCustomer(const QString &customer)
169{
170 if (customer.isEmpty())
171 return;
172 QStringList customerList = customer.split(", ");
173 QTextDocument *document = textEdit->document();
174 QTextCursor cursor = document->find("NAME");
175 if (!cursor.isNull()) {
176 cursor.beginEditBlock();
177 cursor.insertText(customerList.at(0));
178 QTextCursor oldcursor = cursor;
179 cursor = document->find("ADDRESS");
180 if (!cursor.isNull()) {
181 for (int i = 1; i < customerList.size(); ++i) {
182 cursor.insertBlock();
183 cursor.insertText(customerList.at(i));
184 }
185 cursor.endEditBlock();
186 }
187 else
188 oldcursor.endEditBlock();
189 }
190}
191//! [6]
192
193//! [7]
194void MainWindow::addParagraph(const QString &paragraph)
195{
196 if (paragraph.isEmpty())
197 return;
198 QTextDocument *document = textEdit->document();
199 QTextCursor cursor = document->find(tr("Yours sincerely,"));
200 if (cursor.isNull())
201 return;
202 cursor.beginEditBlock();
203 cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2);
204 cursor.insertBlock();
205 cursor.insertText(paragraph);
206 cursor.insertBlock();
207 cursor.endEditBlock();
208
209}
210//! [7]
211
212void MainWindow::about()
213{
214 QMessageBox::about(this, tr("About Dock Widgets"),
215 tr("The <b>Dock Widgets</b> example demonstrates how to "
216 "use Qt's dock widgets. You can enter your own text, "
217 "click a customer to add a customer name and "
218 "address, and click standard paragraphs to add them."));
219}
220
221void MainWindow::createActions()
222{
223 newLetterAct = new QAction(QIcon(":/images/new.png"), tr("&New Letter"),
224 this);
225 newLetterAct->setShortcuts(QKeySequence::New);
226 newLetterAct->setStatusTip(tr("Create a new form letter"));
227 connect(newLetterAct, SIGNAL(triggered()), this, SLOT(newLetter()));
228
229 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save..."), this);
230 saveAct->setShortcuts(QKeySequence::Save);
231 saveAct->setStatusTip(tr("Save the current form letter"));
232 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
233
234 printAct = new QAction(QIcon(":/images/print.png"), tr("&Print..."), this);
235 printAct->setShortcuts(QKeySequence::Print);
236 printAct->setStatusTip(tr("Print the current form letter"));
237 connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
238
239 undoAct = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this);
240 undoAct->setShortcuts(QKeySequence::Undo);
241 undoAct->setStatusTip(tr("Undo the last editing action"));
242 connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
243
244 quitAct = new QAction(tr("&Quit"), this);
245 quitAct->setShortcuts(QKeySequence::Quit);
246 quitAct->setStatusTip(tr("Quit the application"));
247 connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
248
249 aboutAct = new QAction(tr("&About"), this);
250 aboutAct->setStatusTip(tr("Show the application's About box"));
251 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
252
253 aboutQtAct = new QAction(tr("About &Qt"), this);
254 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
255 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
256}
257
258void MainWindow::createMenus()
259{
260 fileMenu = menuBar()->addMenu(tr("&File"));
261 fileMenu->addAction(newLetterAct);
262 fileMenu->addAction(saveAct);
263 fileMenu->addAction(printAct);
264 fileMenu->addSeparator();
265 fileMenu->addAction(quitAct);
266
267 editMenu = menuBar()->addMenu(tr("&Edit"));
268 editMenu->addAction(undoAct);
269
270 viewMenu = menuBar()->addMenu(tr("&View"));
271
272 menuBar()->addSeparator();
273
274 helpMenu = menuBar()->addMenu(tr("&Help"));
275 helpMenu->addAction(aboutAct);
276 helpMenu->addAction(aboutQtAct);
277}
278
279void MainWindow::createToolBars()
280{
281 fileToolBar = addToolBar(tr("File"));
282 fileToolBar->addAction(newLetterAct);
283 fileToolBar->addAction(saveAct);
284 fileToolBar->addAction(printAct);
285
286 editToolBar = addToolBar(tr("Edit"));
287 editToolBar->addAction(undoAct);
288}
289
290//! [8]
291void MainWindow::createStatusBar()
292{
293 statusBar()->showMessage(tr("Ready"));
294}
295//! [8]
296
297//! [9]
298void MainWindow::createDockWindows()
299{
300 QDockWidget *dock = new QDockWidget(tr("Customers"), this);
301 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
302 customerList = new QListWidget(dock);
303 customerList->addItems(QStringList()
304 << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
305 << "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
306 << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
307 << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
308 << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
309 << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
310 dock->setWidget(customerList);
311 addDockWidget(Qt::RightDockWidgetArea, dock);
312 viewMenu->addAction(dock->toggleViewAction());
313
314 dock = new QDockWidget(tr("Paragraphs"), this);
315 paragraphsList = new QListWidget(dock);
316 paragraphsList->addItems(QStringList()
317 << "Thank you for your payment which we have received today."
318 << "Your order has been dispatched and should be with you "
319 "within 28 days."
320 << "We have dispatched those items that were in stock. The "
321 "rest of your order will be dispatched once all the "
322 "remaining items have arrived at our warehouse. No "
323 "additional shipping charges will be made."
324 << "You made a small overpayment (less than $5) which we "
325 "will keep on account for you, or return at your request."
326 << "You made a small underpayment (less than $1), but we have "
327 "sent your order anyway. We'll add this underpayment to "
328 "your next bill."
329 << "Unfortunately you did not send enough money. Please remit "
330 "an additional $. Your order will be dispatched as soon as "
331 "the complete amount has been received."
332 << "You made an overpayment (more than $5). Do you wish to "
333 "buy more items, or should we return the excess to you?");
334 dock->setWidget(paragraphsList);
335 addDockWidget(Qt::RightDockWidgetArea, dock);
336 viewMenu->addAction(dock->toggleViewAction());
337
338 connect(customerList, SIGNAL(currentTextChanged(QString)),
339 this, SLOT(insertCustomer(QString)));
340 connect(paragraphsList, SIGNAL(currentTextChanged(QString)),
341 this, SLOT(addParagraph(QString)));
342}
343//! [9]
Note: See TracBrowser for help on using the repository browser.