1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 //! [0]
42 #include <QtGui>
43 
44 #include "mainwindow.h"
45 //! [0]
46 
47 //! [1]
MainWindow()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]
newLetter()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]
print()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]
save()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]
undo()159 void MainWindow::undo()
160 {
161     QTextDocument *document = textEdit->document();
162     document->undo();
163 }
164 //! [5]
165 
166 //! [6]
insertCustomer(const QString & customer)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]
addParagraph(const QString & paragraph)193 void MainWindow::addParagraph(const QString &paragraph)
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 
about()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 
createActions()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 
createMenus()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 
createToolBars()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]
createStatusBar()290 void MainWindow::createStatusBar()
291 {
292     statusBar()->showMessage(tr("Ready"));
293 }
294 //! [8]
295 
296 //! [9]
createDockWindows()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 
337     connect(customerList, SIGNAL(currentTextChanged(QString)),
338             this, SLOT(insertCustomer(QString)));
339     connect(paragraphsList, SIGNAL(currentTextChanged(QString)),
340             this, SLOT(addParagraph(QString)));
341 }
342 //! [9]
343