examples/xml/saxbookmarks/mainwindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 examples 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 "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 #if defined(Q_OS_SYMBIAN)
       
    70     // Always look for bookmarks on the same drive where the application is installed to.
       
    71     QString bookmarksFolder = QCoreApplication::applicationFilePath().left(1);
       
    72     bookmarksFolder.append(":/Data/qt/saxbookmarks");
       
    73     QDir::setCurrent(bookmarksFolder);
       
    74 #endif
       
    75     QString fileName =
       
    76             QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
       
    77                                          QDir::currentPath(),
       
    78                                          tr("XBEL Files (*.xbel *.xml)"));
       
    79     if (fileName.isEmpty())
       
    80         return;
       
    81 
       
    82     treeWidget->clear();
       
    83 
       
    84     XbelHandler handler(treeWidget);
       
    85     QXmlSimpleReader reader;
       
    86     reader.setContentHandler(&handler);
       
    87     reader.setErrorHandler(&handler);
       
    88 
       
    89     QFile file(fileName);
       
    90     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
    91         QMessageBox::warning(this, tr("SAX Bookmarks"),
       
    92                              tr("Cannot read file %1:\n%2.")
       
    93                              .arg(fileName)
       
    94                              .arg(file.errorString()));
       
    95         return;
       
    96     }
       
    97 
       
    98     QXmlInputSource xmlInputSource(&file);
       
    99     if (reader.parse(xmlInputSource))
       
   100         statusBar()->showMessage(tr("File loaded"), 2000);
       
   101 }
       
   102 
       
   103 void MainWindow::saveAs()
       
   104 {
       
   105     QString fileName =
       
   106             QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
       
   107                                          QDir::currentPath(),
       
   108                                          tr("XBEL Files (*.xbel *.xml)"));
       
   109     if (fileName.isEmpty())
       
   110         return;
       
   111 
       
   112     QFile file(fileName);
       
   113     if (!file.open(QFile::WriteOnly | QFile::Text)) {
       
   114         QMessageBox::warning(this, tr("SAX Bookmarks"),
       
   115                              tr("Cannot write file %1:\n%2.")
       
   116                              .arg(fileName)
       
   117                              .arg(file.errorString()));
       
   118         return;
       
   119     }
       
   120 
       
   121     XbelGenerator generator(treeWidget);
       
   122     if (generator.write(&file))
       
   123         statusBar()->showMessage(tr("File saved"), 2000);
       
   124 }
       
   125 
       
   126 void MainWindow::about()
       
   127 {
       
   128    QMessageBox::about(this, tr("About SAX Bookmarks"),
       
   129             tr("The <b>SAX Bookmarks</b> example demonstrates how to use Qt's "
       
   130                "SAX classes to read XML documents and how to generate XML by "
       
   131                "hand."));
       
   132 }
       
   133 
       
   134 void MainWindow::createActions()
       
   135 {
       
   136     openAct = new QAction(tr("&Open..."), this);
       
   137     openAct->setShortcuts(QKeySequence::Open);
       
   138     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
       
   139 
       
   140     saveAsAct = new QAction(tr("&Save As..."), this);
       
   141     saveAsAct->setShortcuts(QKeySequence::SaveAs);
       
   142     connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
       
   143 
       
   144     exitAct = new QAction(tr("E&xit"), this);
       
   145     exitAct->setShortcuts(QKeySequence::Quit);
       
   146     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
       
   147 
       
   148     aboutAct = new QAction(tr("&About"), this);
       
   149     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
       
   150 
       
   151     aboutQtAct = new QAction(tr("About &Qt"), this);
       
   152     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
       
   153 }
       
   154 
       
   155 void MainWindow::createMenus()
       
   156 {
       
   157     fileMenu = menuBar()->addMenu(tr("&File"));
       
   158     fileMenu->addAction(openAct);
       
   159     fileMenu->addAction(saveAsAct);
       
   160     fileMenu->addAction(exitAct);
       
   161 
       
   162     menuBar()->addSeparator();
       
   163 
       
   164     helpMenu = menuBar()->addMenu(tr("&Help"));
       
   165     helpMenu->addAction(aboutAct);
       
   166     helpMenu->addAction(aboutQtAct);
       
   167 }