1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at qt-sales@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \example xml/streambookmarks
|
---|
44 | \title QXmlStream Bookmarks Example
|
---|
45 |
|
---|
46 | The QXmlStream Bookmarks example provides a reader for XML Bookmark
|
---|
47 | Exchange Language (XBEL) files using Qt's QXmlStreamReader class
|
---|
48 | for reading, and QXmlStreamWriter class for writing the files.
|
---|
49 |
|
---|
50 | \image xmlstreamexample-screenshot.png
|
---|
51 |
|
---|
52 | \section1 XbelWriter Class Definition
|
---|
53 |
|
---|
54 | The \c XbelWriter class is a subclass of QXmlStreamReader, which provides
|
---|
55 | an XML parser with a streaming API. \c XbelWriter also contains a private
|
---|
56 | instance of QTreeWidget in order to display the bookmarks according to
|
---|
57 | hierarchies.
|
---|
58 |
|
---|
59 | \snippet examples/xml/streambookmarks/xbelwriter.h 0
|
---|
60 |
|
---|
61 | \section1 XbelWriter Class Implementation
|
---|
62 |
|
---|
63 | The \c XbelWriter constructor accepts a \a treeWidget to initialize within
|
---|
64 | its definition. We enable \l{QXmlStreamWriter}'s auto-formatting property
|
---|
65 | to ensure line-breaks and indentations are added automatically to empty
|
---|
66 | sections between elements, increasing readability as the data is split into
|
---|
67 | several lines.
|
---|
68 |
|
---|
69 | \snippet examples/xml/streambookmarks/xbelwriter.cpp 0
|
---|
70 |
|
---|
71 | The \c writeFile() function accepts a QIODevice object and sets it using
|
---|
72 | \c setDevice(). This function then writes the document type
|
---|
73 | definition(DTD), the start element, the version, and \c{treeWidget}'s
|
---|
74 | top-level items.
|
---|
75 |
|
---|
76 | \snippet examples/xml/streambookmarks/xbelwriter.cpp 1
|
---|
77 |
|
---|
78 | The \c writeItem() function accepts a QTreeWidget object and writes it
|
---|
79 | to the stream, depending on its \c tagName, which can either be a "folder",
|
---|
80 | "bookmark", or "separator".
|
---|
81 |
|
---|
82 | \snippet examples/xml/streambookmarks/xbelwriter.cpp 2
|
---|
83 |
|
---|
84 | \section1 XbelReader Class Definition
|
---|
85 |
|
---|
86 | The \c XbelReader class is a subclass of QXmlStreamReader, the pendent
|
---|
87 | class for QXmlStreamWriter. \c XbelReader contains a private instance
|
---|
88 | of QTreeWidget to group bookmarks according to their hierarchies.
|
---|
89 |
|
---|
90 | \snippet examples/xml/streambookmarks/xbelreader.h 0
|
---|
91 |
|
---|
92 | \section1 XbelReader Class Implementation
|
---|
93 |
|
---|
94 | The \c XbelReader constructor accepts a QTreeWidget to initialize the
|
---|
95 | \c treeWidget within its definition. A QStyle object is used to set
|
---|
96 | \c{treeWidget}'s style property. The \c folderIcon is set to QIcon::Normal
|
---|
97 | mode where the pixmap is only displayed when the user is not interacting
|
---|
98 | with the icon. The QStyle::SP_DirClosedIcon, QStyle::SP_DirOpenIcon, and
|
---|
99 | QStyle::SP_FileIcon correspond to standard pixmaps that follow the style
|
---|
100 | of your GUI.
|
---|
101 |
|
---|
102 | \snippet examples/xml/streambookmarks/xbelreader.cpp 0
|
---|
103 |
|
---|
104 | The \c read() function accepts a QIODevice and sets it using
|
---|
105 | \l{QXmlStreamReader::setDevice()}{setDevice()}. The actual process
|
---|
106 | of reading only takes place if the file is a valid XBEL 1.0 file.
|
---|
107 | Note that the XML input needs to be well-formed to be accepted by
|
---|
108 | QXmlStreamReader. Otherwise, the \l{QXmlStreamReader::raiseError()}
|
---|
109 | {raiseError()} function is used to display an error message.
|
---|
110 |
|
---|
111 | \snippet examples/xml/streambookmarks/xbelreader.cpp 1
|
---|
112 |
|
---|
113 | The \c readUnknownElement() function reads an unknown element. The
|
---|
114 | Q_ASSERT() macro is used to provide a pre-condition for the function.
|
---|
115 |
|
---|
116 | \snippet examples/xml/streambookmarks/xbelreader.cpp 2
|
---|
117 |
|
---|
118 | The \c readXBEL() function reads the name of a startElement and calls
|
---|
119 | the appropriate function to read it, depending on whether if its a
|
---|
120 | "folder", "bookmark" or "separator". Otherwise, it calls
|
---|
121 | \c readUnknownElement().
|
---|
122 |
|
---|
123 | \snippet examples/xml/streambookmarks/xbelreader.cpp 3
|
---|
124 |
|
---|
125 | The \c readTitle() function reads the bookmark's title.
|
---|
126 |
|
---|
127 | \snippet examples/xml/streambookmarks/xbelreader.cpp 4
|
---|
128 |
|
---|
129 | The \c readSeparator() function creates a separator and sets its flags.
|
---|
130 | The text is set to 30 "0xB7", the HEX equivalent for period, and then
|
---|
131 | read using \c readElementText().
|
---|
132 |
|
---|
133 | \snippet examples/xml/streambookmarks/xbelreader.cpp 5
|
---|
134 |
|
---|
135 | \section1 MainWindow Class Definition
|
---|
136 |
|
---|
137 | The \c MainWindow class is a subclass of QMainWindow, with a
|
---|
138 | \c File menu and a \c Help menu.
|
---|
139 |
|
---|
140 | \snippet examples/xml/streambookmarks/mainwindow.h 0
|
---|
141 |
|
---|
142 | \section1 MainWindow Class Implementation
|
---|
143 |
|
---|
144 | The \c MainWindow constructor instantiates the QTreeWidget object, \c
|
---|
145 | treeWidget and sets its header with a QStringList object, \c labels.
|
---|
146 | The constructor also invokes \c createActions() and \c createMenus()
|
---|
147 | to set up the menus and their corresponding actions. The \c statusBar()
|
---|
148 | is used to display the message "Ready" and the window's size is fixed
|
---|
149 | to 480x320 pixels.
|
---|
150 |
|
---|
151 | \snippet examples/xml/streambookmarks/mainwindow.cpp 0
|
---|
152 |
|
---|
153 | The \c open() function enables the user to open an XBEL file using
|
---|
154 | QFileDialog::getOpenFileName(). A warning message is displayed along
|
---|
155 | with the \c fileName and \c errorString if the file cannot be read or
|
---|
156 | if there is a parse error.
|
---|
157 |
|
---|
158 | \snippet examples/xml/streambookmarks/mainwindow.cpp 1
|
---|
159 |
|
---|
160 | The \c saveAs() function displays a QFileDialog, prompting the user for
|
---|
161 | a \c fileName using QFileDialog::getSaveFileName(). Similar to the
|
---|
162 | \c open() function, this function also displays a warning message if
|
---|
163 | the file cannot be written to.
|
---|
164 |
|
---|
165 | \snippet examples/xml/streambookmarks/mainwindow.cpp 2
|
---|
166 |
|
---|
167 | The \c about() function displays a QMessageBox with a brief description
|
---|
168 | of the example.
|
---|
169 |
|
---|
170 | \snippet examples/xml/streambookmarks/mainwindow.cpp 3
|
---|
171 |
|
---|
172 | In order to implement the \c open(), \c saveAs(), \c exit(), \c about()
|
---|
173 | and \c aboutQt() functions, we connect them to QAction objects and
|
---|
174 | add them to the \c fileMenu and \c helpMenu. The connections are as shown
|
---|
175 | below:
|
---|
176 |
|
---|
177 | \snippet examples/xml/streambookmarks/mainwindow.cpp 4
|
---|
178 |
|
---|
179 | The \c createMenus() function creates the \c fileMenu and \c helpMenu
|
---|
180 | and adds the QAction objects to them in order to create the menu shown
|
---|
181 | in the screenshot below:
|
---|
182 |
|
---|
183 | \table
|
---|
184 | \row
|
---|
185 | \o \inlineimage xmlstreamexample-filemenu.png
|
---|
186 | \o \inlineimage xmlstreamexample-helpmenu.png
|
---|
187 | \endtable
|
---|
188 |
|
---|
189 | \snippet examples/xml/streambookmarks/mainwindow.cpp 5
|
---|
190 |
|
---|
191 | \section1 \c{main()} Function
|
---|
192 |
|
---|
193 | The \c main() function instantiates \c MainWindow and invokes the \c show()
|
---|
194 | function.
|
---|
195 |
|
---|
196 | \snippet examples/xml/streambookmarks/main.cpp 0
|
---|
197 |
|
---|
198 | See the \l{http://pyxml.sourceforge.net/topics/xbel/}
|
---|
199 | {XML Bookmark Exchange Language Resource Page} for more information
|
---|
200 | about XBEL files.
|
---|
201 | */
|
---|