doc/src/snippets/textdocument-frames/mainwindow.cpp
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "mainwindow.h"
       
    45 #include "xmlwriter.h"
       
    46 
       
    47 MainWindow::MainWindow()
       
    48 {
       
    49     QMenu *fileMenu = new QMenu(tr("&File"));
       
    50 
       
    51     QAction *saveAction = fileMenu->addAction(tr("&Save..."));
       
    52     saveAction->setShortcut(tr("Ctrl+S"));
       
    53 
       
    54     QAction *quitAction = fileMenu->addAction(tr("E&xit"));
       
    55     quitAction->setShortcut(tr("Ctrl+Q"));
       
    56 
       
    57     menuBar()->addMenu(fileMenu);
       
    58     editor = new QTextEdit();
       
    59 
       
    60     QTextCursor cursor(editor->textCursor());
       
    61     cursor.movePosition(QTextCursor::Start); 
       
    62 
       
    63     QTextFrame *mainFrame = cursor.currentFrame();
       
    64     
       
    65     QTextCharFormat plainCharFormat;
       
    66     QTextCharFormat boldCharFormat;
       
    67     boldCharFormat.setFontWeight(QFont::Bold);
       
    68 /*  main frame
       
    69 //! [0]
       
    70     QTextFrame *mainFrame = cursor.currentFrame();
       
    71     cursor.insertText(...);
       
    72 //! [0]
       
    73 */
       
    74     cursor.insertText("Text documents are represented by the "
       
    75                       "QTextDocument class, rather than by QString objects. "
       
    76                       "Each QTextDocument object contains information about "
       
    77                       "the document's internal representation, its structure, "
       
    78                       "and keeps track of modifications to provide undo/redo "
       
    79                       "facilities. This approach allows features such as the "
       
    80                       "layout management to be delegated to specialized "
       
    81                       "classes, but also provides a focus for the framework.",
       
    82                       plainCharFormat);
       
    83 
       
    84 //! [1]
       
    85     QTextFrameFormat frameFormat;
       
    86     frameFormat.setMargin(32);
       
    87     frameFormat.setPadding(8);
       
    88     frameFormat.setBorder(4);
       
    89 //! [1]
       
    90     cursor.insertFrame(frameFormat);
       
    91 
       
    92 /*  insert frame
       
    93 //! [2]
       
    94     cursor.insertFrame(frameFormat);
       
    95     cursor.insertText(...);
       
    96 //! [2]
       
    97 */
       
    98     cursor.insertText("Documents are either converted from external sources "
       
    99                       "or created from scratch using Qt. The creation process "
       
   100                       "can done by an editor widget, such as QTextEdit, or by "
       
   101                       "explicit calls to the Scribe API.", boldCharFormat);
       
   102 
       
   103     cursor = mainFrame->lastCursorPosition();
       
   104 /*  last cursor
       
   105 //! [3]
       
   106     cursor = mainFrame->lastCursorPosition();
       
   107     cursor.insertText(...);
       
   108 //! [3]
       
   109 */
       
   110     cursor.insertText("There are two complementary ways to visualize the "
       
   111                       "contents of a document: as a linear buffer that is "
       
   112                       "used by editors to modify the contents, and as an "
       
   113                       "object hierarchy containing structural information "
       
   114                       "that is useful to layout engines. In the hierarchical "
       
   115                       "model, the objects generally correspond to visual "
       
   116                       "elements such as frames, tables, and lists. At a lower "
       
   117                       "level, these elements describe properties such as the "
       
   118                       "style of text used and its alignment. The linear "
       
   119                       "representation of the document is used for editing and "
       
   120                       "manipulation of the document's contents.",
       
   121                       plainCharFormat);
       
   122 
       
   123 
       
   124     connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
       
   125     connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
       
   126 
       
   127     setCentralWidget(editor);
       
   128     setWindowTitle(tr("Text Document Frames"));
       
   129 }
       
   130 
       
   131 void MainWindow::saveFile()
       
   132 {
       
   133     QString fileName = QFileDialog::getSaveFileName(this,
       
   134         tr("Save document as:"), "", tr("XML (*.xml)"));
       
   135 
       
   136     if (!fileName.isEmpty()) {
       
   137         if (writeXml(fileName))
       
   138             setWindowTitle(fileName);
       
   139         else
       
   140             QMessageBox::warning(this, tr("Warning"),
       
   141                 tr("Failed to save the document."), QMessageBox::Cancel,
       
   142                 QMessageBox::NoButton);
       
   143     }
       
   144 }
       
   145 bool MainWindow::writeXml(const QString &fileName)
       
   146 {
       
   147     XmlWriter documentWriter(editor->document());
       
   148 
       
   149     QDomDocument *domDocument = documentWriter.toXml();
       
   150     QFile file(fileName);
       
   151 
       
   152     if (file.open(QFile::WriteOnly)) {
       
   153         QTextStream textStream(&file);
       
   154         textStream.setCodec(QTextCodec::codecForName("UTF-8"));
       
   155         
       
   156         textStream << domDocument->toString(1).toUtf8();
       
   157         file.close();
       
   158         return true;
       
   159     }
       
   160     else
       
   161         return false;
       
   162 }