1 /****************************************************************************
2  *  **
3  *   ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
4  *    **
5  *     ** This file is part of the example classes of the Qt Toolkit.
6  *      **
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
13  *             **
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.
18  *                  **
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.
21  *                     **
22  *                      ****************************************************************************/
23 
24  #include <QtGui>
25 
26  #include "mainwindow.h"
27 
MainWindow()28  MainWindow::MainWindow()
29  {
30      textEdit = new QTextEdit;
31      setCentralWidget(textEdit);
32 
33      createActions();
34      createMenus();
35      createToolBars();
36      createStatusBar();
37      createDockWindows();
38 
39      setWindowTitle(tr("Dock Widgets"));
40 
41      newLetter();
42  }
43 
newLetter()44  void MainWindow::newLetter()
45  {
46      textEdit->clear();
47 
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);
54 
55      QTextCharFormat textFormat;
56      QTextCharFormat boldFormat;
57      boldFormat.setFontWeight(QFont::Bold);
58      QTextCharFormat italicFormat;
59      italicFormat.setFontItalic(true);
60 
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);
67      cursor.insertBlock();
68      cursor.insertText("321 City Street", textFormat);
69      cursor.insertBlock();
70      cursor.insertText("Industry Park");
71      cursor.insertBlock();
72      cursor.insertText("Some Country");
73      cursor.setPosition(topFrame->lastPosition());
74      cursor.insertText(QDate::currentDate().toString("d MMMM yyyy"), textFormat);
75      cursor.insertBlock();
76      cursor.insertBlock();
77      cursor.insertText("Dear ", textFormat);
78      cursor.insertText("NAME", italicFormat);
79      cursor.insertText(",", textFormat);
80      for (int i = 0; i < 3; ++i)
81          cursor.insertBlock();
82      cursor.insertText(tr("Yours sincerely,"), textFormat);
83      for (int i = 0; i < 3; ++i)
84          cursor.insertBlock();
85      cursor.insertText("The Boss", textFormat);
86      cursor.insertBlock();
87      cursor.insertText("ADDRESS", italicFormat);
88  }
89 
print()90  void MainWindow::print()
91  {
92      QTextDocument *document = textEdit->document();
93      QPrinter printer;
94 
95      QPrintDialog *dlg = new QPrintDialog(&printer, this);
96      if (dlg->exec() != QDialog::Accepted)
97          return;
98 
99      document->print(&printer);
100 
101      statusBar()->showMessage(tr("Ready"), 2000);
102  }
103 
save()104  void MainWindow::save()
105  {
106      QString fileName = QFileDialog::getSaveFileName(this,
107                          tr("Choose a file name"), ".",
108                          tr("HTML (*.html *.htm)"));
109      if (fileName.isEmpty())
110          return;
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.")
115                               .arg(fileName)
116                               .arg(file.errorString()));
117          return;
118      }
119 
120      QTextStream out(&file);
121      QApplication::setOverrideCursor(Qt::WaitCursor);
122      out << textEdit->toHtml();
123      QApplication::restoreOverrideCursor();
124 
125      statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000);
126  }
127 
undo()128  void MainWindow::undo()
129  {
130      QTextDocument *document = textEdit->document();
131      document->undo();
132  }
133 
insertCustomer(const QString & customer)134  void MainWindow::insertCustomer(const QString &customer)
135  {
136      if (customer.isEmpty())
137          return;
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));
150              }
151              cursor.endEditBlock();
152          }
153          else
154              oldcursor.endEditBlock();
155      }
156  }
157 
addParagraph(const QString & paragraph)158  void MainWindow::addParagraph(const QString &paragraph)
159  {
160      if (paragraph.isEmpty())
161          return;
162      QTextDocument *document = textEdit->document();
163      QTextCursor cursor = document->find(tr("Yours sincerely,"));
164      if (cursor.isNull())
165          return;
166      cursor.beginEditBlock();
167      cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2);
168      cursor.insertBlock();
169      cursor.insertText(paragraph);
170      cursor.insertBlock();
171      cursor.endEditBlock();
172 
173  }
174 
about()175  void MainWindow::about()
176  {
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."));
182  }
183 
createActions()184  void MainWindow::createActions()
185  {
186      newLetterAct = new QAction(QIcon(":/images/new.png"), tr("&New Letter"),
187                                 this);
188      newLetterAct->setShortcut(tr("Ctrl+N"));
189      newLetterAct->setStatusTip(tr("Create a new form letter"));
190      connect(newLetterAct, SIGNAL(triggered()), this, SLOT(newLetter()));
191 
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()));
196 
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()));
201 
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()));
206 
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()));
211 
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()));
215 
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()));
219  }
220 
createMenus()221  void MainWindow::createMenus()
222  {
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);
229 
230      editMenu = menuBar()->addMenu(tr("&Edit"));
231      editMenu->addAction(undoAct);
232 
233      viewMenu = menuBar()->addMenu(tr("&View"));
234 
235      menuBar()->addSeparator();
236 
237      helpMenu = menuBar()->addMenu(tr("&Help"));
238      helpMenu->addAction(aboutAct);
239      helpMenu->addAction(aboutQtAct);
240  }
241 
createToolBars()242  void MainWindow::createToolBars()
243  {
244      fileToolBar = addToolBar(tr("File"));
245      fileToolBar->addAction(newLetterAct);
246      fileToolBar->addAction(saveAct);
247      fileToolBar->addAction(printAct);
248 
249      editToolBar = addToolBar(tr("Edit"));
250      editToolBar->addAction(undoAct);
251  }
252 
createStatusBar()253  void MainWindow::createStatusBar()
254  {
255      statusBar()->showMessage(tr("Ready"));
256  }
257 
createDockWindows()258  void MainWindow::createDockWindows()
259  {
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());
273 
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 "
279                 "within 28 days."
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 "
288                 "your next bill."
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());
297 
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 &)));
302  }
303