| 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 examples 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 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "mainwindow.h" | 
|---|
| 45 | #include "xbelgenerator.h" | 
|---|
| 46 | #include "xbelhandler.h" | 
|---|
| 47 |  | 
|---|
| 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("SAX Bookmarks")); | 
|---|
| 64 | resize(480, 320); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void MainWindow::open() | 
|---|
| 68 | { | 
|---|
| 69 | QString fileName = | 
|---|
| 70 | QFileDialog::getOpenFileName(this, tr("Open Bookmark File"), | 
|---|
| 71 | QDir::currentPath(), | 
|---|
| 72 | tr("XBEL Files (*.xbel *.xml)")); | 
|---|
| 73 | if (fileName.isEmpty()) | 
|---|
| 74 | return; | 
|---|
| 75 |  | 
|---|
| 76 | treeWidget->clear(); | 
|---|
| 77 |  | 
|---|
| 78 | XbelHandler handler(treeWidget); | 
|---|
| 79 | QXmlSimpleReader reader; | 
|---|
| 80 | reader.setContentHandler(&handler); | 
|---|
| 81 | reader.setErrorHandler(&handler); | 
|---|
| 82 |  | 
|---|
| 83 | QFile file(fileName); | 
|---|
| 84 | if (!file.open(QFile::ReadOnly | QFile::Text)) { | 
|---|
| 85 | QMessageBox::warning(this, tr("SAX Bookmarks"), | 
|---|
| 86 | tr("Cannot read file %1:\n%2.") | 
|---|
| 87 | .arg(fileName) | 
|---|
| 88 | .arg(file.errorString())); | 
|---|
| 89 | return; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | QXmlInputSource xmlInputSource(&file); | 
|---|
| 93 | if (reader.parse(xmlInputSource)) | 
|---|
| 94 | statusBar()->showMessage(tr("File loaded"), 2000); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void MainWindow::saveAs() | 
|---|
| 98 | { | 
|---|
| 99 | QString fileName = | 
|---|
| 100 | QFileDialog::getSaveFileName(this, tr("Save Bookmark File"), | 
|---|
| 101 | QDir::currentPath(), | 
|---|
| 102 | tr("XBEL Files (*.xbel *.xml)")); | 
|---|
| 103 | if (fileName.isEmpty()) | 
|---|
| 104 | return; | 
|---|
| 105 |  | 
|---|
| 106 | QFile file(fileName); | 
|---|
| 107 | if (!file.open(QFile::WriteOnly | QFile::Text)) { | 
|---|
| 108 | QMessageBox::warning(this, tr("SAX Bookmarks"), | 
|---|
| 109 | tr("Cannot write file %1:\n%2.") | 
|---|
| 110 | .arg(fileName) | 
|---|
| 111 | .arg(file.errorString())); | 
|---|
| 112 | return; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | XbelGenerator generator(treeWidget); | 
|---|
| 116 | if (generator.write(&file)) | 
|---|
| 117 | statusBar()->showMessage(tr("File saved"), 2000); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | void MainWindow::about() | 
|---|
| 121 | { | 
|---|
| 122 | QMessageBox::about(this, tr("About SAX Bookmarks"), | 
|---|
| 123 | tr("The <b>SAX Bookmarks</b> example demonstrates how to use Qt's " | 
|---|
| 124 | "SAX classes to read XML documents and how to generate XML by " | 
|---|
| 125 | "hand.")); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void MainWindow::createActions() | 
|---|
| 129 | { | 
|---|
| 130 | openAct = new QAction(tr("&Open..."), this); | 
|---|
| 131 | openAct->setShortcut(tr("Ctrl+O")); | 
|---|
| 132 | connect(openAct, SIGNAL(triggered()), this, SLOT(open())); | 
|---|
| 133 |  | 
|---|
| 134 | saveAsAct = new QAction(tr("&Save As..."), this); | 
|---|
| 135 | saveAsAct->setShortcut(tr("Ctrl+S")); | 
|---|
| 136 | connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); | 
|---|
| 137 |  | 
|---|
| 138 | exitAct = new QAction(tr("E&xit"), this); | 
|---|
| 139 | exitAct->setShortcut(tr("Ctrl+Q")); | 
|---|
| 140 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); | 
|---|
| 141 |  | 
|---|
| 142 | aboutAct = new QAction(tr("&About"), this); | 
|---|
| 143 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); | 
|---|
| 144 |  | 
|---|
| 145 | aboutQtAct = new QAction(tr("About &Qt"), this); | 
|---|
| 146 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | void MainWindow::createMenus() | 
|---|
| 150 | { | 
|---|
| 151 | fileMenu = menuBar()->addMenu(tr("&File")); | 
|---|
| 152 | fileMenu->addAction(openAct); | 
|---|
| 153 | fileMenu->addAction(saveAsAct); | 
|---|
| 154 | fileMenu->addAction(exitAct); | 
|---|
| 155 |  | 
|---|
| 156 | menuBar()->addSeparator(); | 
|---|
| 157 |  | 
|---|
| 158 | helpMenu = menuBar()->addMenu(tr("&Help")); | 
|---|
| 159 | helpMenu->addAction(aboutAct); | 
|---|
| 160 | helpMenu->addAction(aboutQtAct); | 
|---|
| 161 | } | 
|---|