1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "mainwindow.h"
|
---|
44 | #include "xbelreader.h"
|
---|
45 | #include "xbelwriter.h"
|
---|
46 |
|
---|
47 | //! [0]
|
---|
48 | MainWindow::MainWindow()
|
---|
49 | {
|
---|
50 | QStringList labels;
|
---|
51 | labels << tr("Title") << tr("Location");
|
---|
52 |
|
---|
53 | treeWidget = new QTreeWidget;
|
---|
54 | treeWidget->header()->setResizeMode(QHeaderView::Stretch);
|
---|
55 | treeWidget->setHeaderLabels(labels);
|
---|
56 | setCentralWidget(treeWidget);
|
---|
57 |
|
---|
58 | createActions();
|
---|
59 | createMenus();
|
---|
60 |
|
---|
61 | statusBar()->showMessage(tr("Ready"));
|
---|
62 |
|
---|
63 | setWindowTitle(tr("QXmlStream Bookmarks"));
|
---|
64 | resize(480, 320);
|
---|
65 | }
|
---|
66 | //! [0]
|
---|
67 |
|
---|
68 | //! [1]
|
---|
69 | void MainWindow::open()
|
---|
70 | {
|
---|
71 | QString fileName =
|
---|
72 | QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
|
---|
73 | QDir::currentPath(),
|
---|
74 | tr("XBEL Files (*.xbel *.xml)"));
|
---|
75 | if (fileName.isEmpty())
|
---|
76 | return;
|
---|
77 |
|
---|
78 | treeWidget->clear();
|
---|
79 |
|
---|
80 |
|
---|
81 | QFile file(fileName);
|
---|
82 | if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
---|
83 | QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
|
---|
84 | tr("Cannot read file %1:\n%2.")
|
---|
85 | .arg(fileName)
|
---|
86 | .arg(file.errorString()));
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | XbelReader reader(treeWidget);
|
---|
91 | if (!reader.read(&file)) {
|
---|
92 | QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
|
---|
93 | tr("Parse error in file %1:\n\n%2")
|
---|
94 | .arg(fileName)
|
---|
95 | .arg(reader.errorString()));
|
---|
96 | } else {
|
---|
97 | statusBar()->showMessage(tr("File loaded"), 2000);
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 | //! [1]
|
---|
102 |
|
---|
103 | //! [2]
|
---|
104 | void MainWindow::saveAs()
|
---|
105 | {
|
---|
106 | QString fileName =
|
---|
107 | QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
|
---|
108 | QDir::currentPath(),
|
---|
109 | tr("XBEL Files (*.xbel *.xml)"));
|
---|
110 | if (fileName.isEmpty())
|
---|
111 | return;
|
---|
112 |
|
---|
113 | QFile file(fileName);
|
---|
114 | if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
---|
115 | QMessageBox::warning(this, tr("QXmlStream Bookmarks"),
|
---|
116 | tr("Cannot write file %1:\n%2.")
|
---|
117 | .arg(fileName)
|
---|
118 | .arg(file.errorString()));
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | XbelWriter writer(treeWidget);
|
---|
123 | if (writer.writeFile(&file))
|
---|
124 | statusBar()->showMessage(tr("File saved"), 2000);
|
---|
125 | }
|
---|
126 | //! [2]
|
---|
127 |
|
---|
128 | //! [3]
|
---|
129 | void MainWindow::about()
|
---|
130 | {
|
---|
131 | QMessageBox::about(this, tr("About QXmlStream Bookmarks"),
|
---|
132 | tr("The <b>QXmlStream Bookmarks</b> example demonstrates how to use Qt's "
|
---|
133 | "QXmlStream classes to read and write XML documents."));
|
---|
134 | }
|
---|
135 | //! [3]
|
---|
136 |
|
---|
137 | //! [4]
|
---|
138 | void MainWindow::createActions()
|
---|
139 | {
|
---|
140 | openAct = new QAction(tr("&Open..."), this);
|
---|
141 | openAct->setShortcuts(QKeySequence::Open);
|
---|
142 | connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
---|
143 |
|
---|
144 | saveAsAct = new QAction(tr("&Save As..."), this);
|
---|
145 | saveAsAct->setShortcuts(QKeySequence::SaveAs);
|
---|
146 | connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
|
---|
147 |
|
---|
148 | exitAct = new QAction(tr("E&xit"), this);
|
---|
149 | exitAct->setShortcuts(QKeySequence::Quit);
|
---|
150 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
---|
151 |
|
---|
152 | aboutAct = new QAction(tr("&About"), this);
|
---|
153 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
---|
154 |
|
---|
155 | aboutQtAct = new QAction(tr("About &Qt"), this);
|
---|
156 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
157 | }
|
---|
158 | //! [4]
|
---|
159 |
|
---|
160 | //! [5]
|
---|
161 | void MainWindow::createMenus()
|
---|
162 | {
|
---|
163 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
164 | fileMenu->addAction(openAct);
|
---|
165 | fileMenu->addAction(saveAsAct);
|
---|
166 | fileMenu->addAction(exitAct);
|
---|
167 |
|
---|
168 | menuBar()->addSeparator();
|
---|
169 |
|
---|
170 | helpMenu = menuBar()->addMenu(tr("&Help"));
|
---|
171 | helpMenu->addAction(aboutAct);
|
---|
172 | helpMenu->addAction(aboutQtAct);
|
---|
173 | }
|
---|
174 | //! [5]
|
---|