examples/xml/dombookmarks/mainwindow.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 "xbeltree.h"
       
    46 
       
    47 MainWindow::MainWindow()
       
    48 {
       
    49     xbelTree = new XbelTree;
       
    50     setCentralWidget(xbelTree);
       
    51 
       
    52     createActions();
       
    53     createMenus();
       
    54 
       
    55     statusBar()->showMessage(tr("Ready"));
       
    56 
       
    57     setWindowTitle(tr("DOM Bookmarks"));
       
    58     resize(480, 320);
       
    59 }
       
    60 
       
    61 void MainWindow::open()
       
    62 {
       
    63     QString fileName =
       
    64             QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
       
    65                                          QDir::currentPath(),
       
    66                                          tr("XBEL Files (*.xbel *.xml)"));
       
    67     if (fileName.isEmpty())
       
    68         return;
       
    69 
       
    70     QFile file(fileName);
       
    71     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
    72         QMessageBox::warning(this, tr("SAX Bookmarks"),
       
    73                              tr("Cannot read file %1:\n%2.")
       
    74                              .arg(fileName)
       
    75                              .arg(file.errorString()));
       
    76         return;
       
    77     }
       
    78 
       
    79     if (xbelTree->read(&file))
       
    80         statusBar()->showMessage(tr("File loaded"), 2000);
       
    81 }
       
    82 
       
    83 void MainWindow::saveAs()
       
    84 {
       
    85     QString fileName =
       
    86             QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
       
    87                                          QDir::currentPath(),
       
    88                                          tr("XBEL Files (*.xbel *.xml)"));
       
    89     if (fileName.isEmpty())
       
    90         return;
       
    91 
       
    92     QFile file(fileName);
       
    93     if (!file.open(QFile::WriteOnly | QFile::Text)) {
       
    94         QMessageBox::warning(this, tr("SAX Bookmarks"),
       
    95                              tr("Cannot write file %1:\n%2.")
       
    96                              .arg(fileName)
       
    97                              .arg(file.errorString()));
       
    98         return;
       
    99     }
       
   100 
       
   101     if (xbelTree->write(&file))
       
   102         statusBar()->showMessage(tr("File saved"), 2000);
       
   103 }
       
   104 
       
   105 void MainWindow::about()
       
   106 {
       
   107    QMessageBox::about(this, tr("About DOM Bookmarks"),
       
   108                       tr("The <b>DOM Bookmarks</b> example demonstrates how to "
       
   109                          "use Qt's DOM classes to read and write XML "
       
   110                          "documents."));
       
   111 }
       
   112 
       
   113 void MainWindow::createActions()
       
   114 {
       
   115     openAct = new QAction(tr("&Open..."), this);
       
   116     openAct->setShortcuts(QKeySequence::Open);
       
   117     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
       
   118 
       
   119     saveAsAct = new QAction(tr("&Save As..."), this);
       
   120     saveAsAct->setShortcuts(QKeySequence::SaveAs);
       
   121     connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
       
   122 
       
   123     exitAct = new QAction(tr("E&xit"), this);
       
   124     exitAct->setShortcuts(QKeySequence::Quit);
       
   125     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
       
   126 
       
   127     aboutAct = new QAction(tr("&About"), this);
       
   128     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
       
   129 
       
   130     aboutQtAct = new QAction(tr("About &Qt"), this);
       
   131     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
       
   132 }
       
   133 
       
   134 void MainWindow::createMenus()
       
   135 {
       
   136     fileMenu = menuBar()->addMenu(tr("&File"));
       
   137     fileMenu->addAction(openAct);
       
   138     fileMenu->addAction(saveAsAct);
       
   139     fileMenu->addAction(exitAct);
       
   140 
       
   141     menuBar()->addSeparator();
       
   142 
       
   143     helpMenu = menuBar()->addMenu(tr("&Help"));
       
   144     helpMenu->addAction(aboutAct);
       
   145     helpMenu->addAction(aboutQtAct);
       
   146 }