1 /****************************************************************************
3 * ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
5 * ** This file is part of the example classes of the Qt Toolkit.
7 * ** This file may be used under the terms of the GNU General Public
8 * ** License version 2.0 as published by the Free Software Foundation
9 * ** and appearing in the file LICENSE.GPL included in the packaging of
10 * ** this file. Please review the following information to ensure GNU
11 * ** General Public Licensing requirements will be met:
12 * ** http://www.trolltech.com/products/qt/opensource.html
14 * ** If you are unsure which license is appropriate for your use, please
15 * ** review the following information:
16 * ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 * ** sales department at sales@trolltech.com.
19 * ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 * ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * ****************************************************************************/
26 #include "mainwindow.h"
28 MainWindow::MainWindow()
30 textEdit = new QTextEdit;
31 setCentralWidget(textEdit);
39 setWindowTitle(tr("Dock Widgets"));
44 void MainWindow::newLetter()
48 QTextCursor cursor(textEdit->textCursor());
49 cursor.movePosition(QTextCursor::Start);
50 QTextFrame *topFrame = cursor.currentFrame();
51 QTextFrameFormat topFrameFormat = topFrame->frameFormat();
52 topFrameFormat.setPadding(16);
53 topFrame->setFrameFormat(topFrameFormat);
55 QTextCharFormat textFormat;
56 QTextCharFormat boldFormat;
57 boldFormat.setFontWeight(QFont::Bold);
58 QTextCharFormat italicFormat;
59 italicFormat.setFontItalic(true);
61 QTextTableFormat tableFormat;
62 tableFormat.setBorder(1);
63 tableFormat.setCellPadding(16);
64 tableFormat.setAlignment(Qt::AlignRight);
65 cursor.insertTable(1, 1, tableFormat);
66 cursor.insertText("The Firm", boldFormat);
68 cursor.insertText("321 City Street", textFormat);
70 cursor.insertText("Industry Park");
72 cursor.insertText("Some Country");
73 cursor.setPosition(topFrame->lastPosition());
74 cursor.insertText(QDate::currentDate().toString("d MMMM yyyy"), textFormat);
77 cursor.insertText("Dear ", textFormat);
78 cursor.insertText("NAME", italicFormat);
79 cursor.insertText(",", textFormat);
80 for (int i = 0; i < 3; ++i)
82 cursor.insertText(tr("Yours sincerely,"), textFormat);
83 for (int i = 0; i < 3; ++i)
85 cursor.insertText("The Boss", textFormat);
87 cursor.insertText("ADDRESS", italicFormat);
90 void MainWindow::print()
92 QTextDocument *document = textEdit->document();
95 QPrintDialog *dlg = new QPrintDialog(&printer, this);
96 if (dlg->exec() != QDialog::Accepted)
99 document->print(&printer);
101 statusBar()->showMessage(tr("Ready"), 2000);
104 void MainWindow::save()
106 QString fileName = QFileDialog::getSaveFileName(this,
107 tr("Choose a file name"), ".",
108 tr("HTML (*.html *.htm)"));
109 if (fileName.isEmpty())
111 QFile file(fileName);
112 if (!file.open(QFile::WriteOnly | QFile::Text)) {
113 QMessageBox::warning(this, tr("Dock Widgets"),
114 tr("Cannot write file %1:\n%2.")
116 .arg(file.errorString()));
120 QTextStream out(&file);
121 QApplication::setOverrideCursor(Qt::WaitCursor);
122 out << textEdit->toHtml();
123 QApplication::restoreOverrideCursor();
125 statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000);
128 void MainWindow::undo()
130 QTextDocument *document = textEdit->document();
134 void MainWindow::insertCustomer(const QString &customer)
136 if (customer.isEmpty())
138 QStringList customerList = customer.split(", ");
139 QTextDocument *document = textEdit->document();
140 QTextCursor cursor = document->find("NAME");
141 if (!cursor.isNull()) {
142 cursor.beginEditBlock();
143 cursor.insertText(customerList.at(0));
144 QTextCursor oldcursor = cursor;
145 cursor = document->find("ADDRESS");
146 if (!cursor.isNull()) {
147 for (int i = 1; i < customerList.size(); ++i) {
148 cursor.insertBlock();
149 cursor.insertText(customerList.at(i));
151 cursor.endEditBlock();
154 oldcursor.endEditBlock();
158 void MainWindow::addParagraph(const QString ¶graph)
160 if (paragraph.isEmpty())
162 QTextDocument *document = textEdit->document();
163 QTextCursor cursor = document->find(tr("Yours sincerely,"));
166 cursor.beginEditBlock();
167 cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2);
168 cursor.insertBlock();
169 cursor.insertText(paragraph);
170 cursor.insertBlock();
171 cursor.endEditBlock();
175 void MainWindow::about()
177 QMessageBox::about(this, tr("About Dock Widgets"),
178 tr("The <b>Dock Widgets</b> example demonstrates how to "
179 "use Qt's dock widgets. You can enter your own text, "
180 "click a customer to add a customer name and "
181 "address, and click standard paragraphs to add them."));
184 void MainWindow::createActions()
186 newLetterAct = new QAction(QIcon(":/images/new.png"), tr("&New Letter"),
188 newLetterAct->setShortcut(tr("Ctrl+N"));
189 newLetterAct->setStatusTip(tr("Create a new form letter"));
190 connect(newLetterAct, SIGNAL(triggered()), this, SLOT(newLetter()));
192 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save..."), this);
193 saveAct->setShortcut(tr("Ctrl+S"));
194 saveAct->setStatusTip(tr("Save the current form letter"));
195 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
197 printAct = new QAction(QIcon(":/images/print.png"), tr("&Print..."), this);
198 printAct->setShortcut(tr("Ctrl+P"));
199 printAct->setStatusTip(tr("Print the current form letter"));
200 connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
202 undoAct = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this);
203 undoAct->setShortcut(tr("Ctrl+Z"));
204 undoAct->setStatusTip(tr("Undo the last editing action"));
205 connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
207 quitAct = new QAction(tr("&Quit"), this);
208 quitAct->setShortcut(tr("Ctrl+Q"));
209 quitAct->setStatusTip(tr("Quit the application"));
210 connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
212 aboutAct = new QAction(tr("&About"), this);
213 aboutAct->setStatusTip(tr("Show the application's About box"));
214 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
216 aboutQtAct = new QAction(tr("About &Qt"), this);
217 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
218 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
221 void MainWindow::createMenus()
223 fileMenu = menuBar()->addMenu(tr("&File"));
224 fileMenu->addAction(newLetterAct);
225 fileMenu->addAction(saveAct);
226 fileMenu->addAction(printAct);
227 fileMenu->addSeparator();
228 fileMenu->addAction(quitAct);
230 editMenu = menuBar()->addMenu(tr("&Edit"));
231 editMenu->addAction(undoAct);
233 viewMenu = menuBar()->addMenu(tr("&View"));
235 menuBar()->addSeparator();
237 helpMenu = menuBar()->addMenu(tr("&Help"));
238 helpMenu->addAction(aboutAct);
239 helpMenu->addAction(aboutQtAct);
242 void MainWindow::createToolBars()
244 fileToolBar = addToolBar(tr("File"));
245 fileToolBar->addAction(newLetterAct);
246 fileToolBar->addAction(saveAct);
247 fileToolBar->addAction(printAct);
249 editToolBar = addToolBar(tr("Edit"));
250 editToolBar->addAction(undoAct);
253 void MainWindow::createStatusBar()
255 statusBar()->showMessage(tr("Ready"));
258 void MainWindow::createDockWindows()
260 QDockWidget *dock = new QDockWidget(tr("Customers"), this);
261 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
262 customerList = new QListWidget(dock);
263 customerList->addItems(QStringList()
264 << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
265 << "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
266 << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
267 << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
268 << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
269 << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
270 dock->setWidget(customerList);
271 addDockWidget(Qt::RightDockWidgetArea, dock);
272 viewMenu->addAction(dock->toggleViewAction());
274 dock = new QDockWidget(tr("Paragraphs"), this);
275 paragraphsList = new QListWidget(dock);
276 paragraphsList->addItems(QStringList()
277 << "Thank you for your payment which we have received today."
278 << "Your order has been dispatched and should be with you "
280 << "We have dispatched those items that were in stock. The "
281 "rest of your order will be dispatched once all the "
282 "remaining items have arrived at our warehouse. No "
283 "additional shipping charges will be made."
284 << "You made a small overpayment (less than $5) which we "
285 "will keep on account for you, or return at your request."
286 << "You made a small underpayment (less than $1), but we have "
287 "sent your order anyway. We'll add this underpayment to "
289 << "Unfortunately you did not send enough money. Please remit "
290 "an additional $. Your order will be dispatched as soon as "
291 "the complete amount has been received."
292 << "You made an overpayment (more than $5). Do you wish to "
293 "buy more items, or should we return the excess to you?");
294 dock->setWidget(paragraphsList);
295 addDockWidget(Qt::RightDockWidgetArea, dock);
296 viewMenu->addAction(dock->toggleViewAction());
298 connect(customerList, SIGNAL(currentTextChanged(const QString &)),
299 this, SLOT(insertCustomer(const QString &)));
300 connect(paragraphsList, SIGNAL(currentTextChanged(const QString &)),
301 this, SLOT(addParagraph(const QString &)));