source: trunk/examples/xml/dombookmarks/mainwindow.cpp@ 500

Last change on this file since 500 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
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 [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "mainwindow.h"
45#include "xbeltree.h"
46
47MainWindow::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
61void 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
83void 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
105void 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
113void MainWindow::createActions()
114{
115 openAct = new QAction(tr("&Open..."), this);
116 openAct->setShortcut(tr("Ctrl+O"));
117 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
118
119 saveAsAct = new QAction(tr("&Save As..."), this);
120 saveAsAct->setShortcut(tr("Ctrl+S"));
121 connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
122
123 exitAct = new QAction(tr("E&xit"), this);
124 exitAct->setShortcut(tr("Ctrl+Q"));
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
134void 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}
Note: See TracBrowser for help on using the repository browser.