Merge branch 'master' of git://labs.trolltech.com/qtscriptgenerator
[qtscriptgenerator/amarok.git] / examples / StreamBookmarks.qs
blob7f23552a150748c3437f4f67ce758e53baa1bcfa
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file.  Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function tr(s) { return s; }
27 function XbelWriter(treeWidget)
29     QXmlStreamWriter.call(this);
30     this.treeWidget = treeWidget;
31     this.setAutoFormatting(true);
34 XbelWriter.prototype = new QXmlStreamWriter();
36 XbelWriter.prototype.writeFile = function(device)
38     this.setDevice(device);
40     this.writeStartDocument();
41     this.writeDTD("<!DOCTYPE xbel>");
42     this.writeStartElement("xbel");
43     this.writeAttribute("version", "1.0");
44     for (var i = 0; i < this.treeWidget.topLevelItemCount; ++i)
45         this.writeItem(this.treeWidget.topLevelItem(i));
47     this.writeEndDocument();
48     return true;
51 XbelWriter.prototype.writeItem = function(item)
53     var tagName = item.data(0, Qt.UserRole);
54     if (tagName == "folder") {
55         var folded = !item.isExpanded();
56         this.writeStartElement(tagName);
57         this.writeAttribute("folded", folded ? "yes" : "no");
58         this.writeTextElement("title", item.text(0));
59         for (var i = 0; i < item.childCount(); ++i)
60             this.writeItem(item.child(i));
61         this.writeEndElement();
62     } else if (tagName == "bookmark") {
63         this.writeStartElement(tagName);
64         if (item.text(1) != "")
65             this.writeAttribute("href", item.text(1));
66         this.writeTextElement("title", item.text(0));
67         this.writeEndElement();
68     } else if (tagName == "separator") {
69         this.writeEmptyElement(tagName);
70     }
75 function XbelReader(treeWidget)
77     QXmlStreamReader.call(this);
78     this.treeWidget = treeWidget;
80     var style = treeWidget.style();
82     this.folderIcon = new QIcon();
83     this.folderIcon.addPixmap(style.standardPixmap(QStyle.SP_DirClosedIcon),
84                          QIcon.Normal, QIcon.Off);
85     this.folderIcon.addPixmap(style.standardPixmap(QStyle.SP_DirOpenIcon),
86                          QIcon.Normal, QIcon.On);
87     this.bookmarkIcon = new QIcon();
88     this.bookmarkIcon.addPixmap(style.standardPixmap(QStyle.SP_FileIcon));
91 XbelReader.prototype = new QXmlStreamReader();
93 XbelReader.prototype.read = function(device)
95     this.setDevice(device);
97     while (!this.atEnd()) {
98         this.readNext();
100         if (this.isStartElement()) {
101             if (this.name() == "xbel" && this.attributes().value("version") == "1.0")
102                 this.readXBEL();
103             else
104                 this.raiseError(tr("The file is not an XBEL version 1.0 file."));
105         }
106     }
108     return this.error() == QXmlStreamReader.NoError;
111 XbelReader.prototype.readUnknownElement = function()
113     while (!this.atEnd()) {
114         this.readNext();
116         if (this.isEndElement())
117             break;
119         if (this.isStartElement())
120             this.readUnknownElement();
121     }
124 XbelReader.prototype.readXBEL = function()
126 //    Q_ASSERT(isStartElement() && name() == "xbel");
128     while (!this.atEnd()) {
129         this.readNext();
131         if (this.isEndElement())
132             break;
134         if (this.isStartElement()) {
135             if (this.name() == "folder")
136                 this.readFolder(null);
137             else if (name() == "bookmark")
138                 this.readBookmark(null);
139             else if (name() == "separator")
140                 this.readSeparator(null);
141             else
142                 this.readUnknownElement();
143         }
144     }
147 XbelReader.prototype.readTitle = function(item)
149 //    Q_ASSERT(isStartElement() && name() == "title");
151     var title = this.readElementText();
152     item.setText(0, title);
155 XbelReader.prototype.readSeparator = function(item)
157     var separator = this.createChildItem(item);
158     separator.setFlags(Qt.ItemFlags(item.flags() & ~Qt.ItemIsSelectable));
159     separator.setText(0, "---" /*QString(30, 0xB7)*/);
160     this.readElementText();
163 XbelReader.prototype.readFolder = function(item)
165 //    Q_ASSERT(isStartElement() && name() == "folder");
167     var folder = this.createChildItem(item);
168     var folded = (this.attributes().value("folded") != "no");
169     folder.setExpanded(!folded);
171     while (!this.atEnd()) {
172         this.readNext();
174         if (this.isEndElement())
175             break;
177         if (this.isStartElement()) {
178             if (this.name() == "title")
179                 this.readTitle(folder);
180             else if (this.name() == "folder")
181                 this.readFolder(folder);
182             else if (this.name() == "bookmark")
183                 this.readBookmark(folder);
184             else if (this.name() == "separator")
185                 this.readSeparator(folder);
186             else
187                 this.readUnknownElement();
188         }
189     }
192 XbelReader.prototype.readBookmark = function(item)
194 //    Q_ASSERT(isStartElement() && name() == "bookmark");
196     var bookmark = this.createChildItem(item);
197     bookmark.setFlags(Qt.ItemFlags(bookmark.flags() | Qt.ItemIsEditable));
198     bookmark.setIcon(0, this.bookmarkIcon);
199     bookmark.setText(0, tr("Unknown title"));
200     bookmark.setText(1, this.attributes().value("href"));
201     while (!this.atEnd()) {
202         this.readNext();
204         if (this.isEndElement())
205             break;
207         if (this.isStartElement()) {
208             if (this.name() == "title")
209                 this.readTitle(bookmark);
210             else
211                 this.readUnknownElement();
212         }
213     }
216 XbelReader.prototype.createChildItem = function(item)
218     var childItem;
219     if (item) {
220         childItem = new QTreeWidgetItem(item);
221     } else {
222 // ###       childItem = new QTreeWidgetItem(this.treeWidget);
223         childItem = new QTreeWidgetItem();
224         this.treeWidget.addTopLevelItem(childItem);
225     }
226     childItem.setData(0, Qt.UserRole, this.name());
227     return childItem;
232 function MainWindow()
234     QMainWindow.call(this);
236     var labels = new Array();
237     labels.push(tr("Title"));
238     labels.push(tr("Location"));
240     this.treeWidget = new QTreeWidget();
241     this.treeWidget.header().setResizeMode(QHeaderView.Stretch);
242     this.treeWidget.setHeaderLabels(labels);
243     this.setCentralWidget(this.treeWidget);
245     this.createActions();
246     this.createMenus();
248     this.statusBar().showMessage(tr("Ready"));
250     this.windowTitle = tr("QXmlStream Bookmarks");
251     this.resize(480, 320);
254 MainWindow.prototype = new QMainWindow();
256 MainWindow.prototype.open = function()
258     var fileName =
259             QFileDialog.getOpenFileName(this, tr("Open Bookmark File"),
260                                          QDir.currentPath(),
261                                          tr("XBEL Files (*.xbel *.xml)"));
262     if (fileName == "")
263         return;
265     this.treeWidget.clear();
268     var file = new QFile(fileName);
269     if (!file.open(QIODevice.OpenMode(QIODevice.ReadOnly, QIODevice.Text))) {
270         QMessageBox.warning(this, tr("QXmlStream Bookmarks"),
271                              tr("Cannot read file %1:\n%2.")
272                              .arg(fileName)
273                              .arg(file.errorString()));
274         return;
275     }
277     var reader = new XbelReader(this.treeWidget);
278     if (!reader.read(file)) {
279         QMessageBox.warning(this, tr("QXmlStream Bookmarks"),
280                             tr("Parse error in file " + fileName + " at line " + reader.lineNumber()
281                                + ", column " + reader.columnNumber() + ":\n" + reader.errorString()));
282     } else {
283         this.statusBar().showMessage(tr("File loaded"), 2000);
284     }
288 MainWindow.prototype.saveAs = function()
290     var fileName =
291             QFileDialog.getSaveFileName(this, tr("Save Bookmark File"),
292                                          QDir.currentPath(),
293                                          tr("XBEL Files (*.xbel *.xml)"));
294     if (fileName == "")
295         return;
297     var file = new QFile(fileName);
298     if (!file.open(QIODevice.OpenMode(QIODevice.WriteOnly, QIODevice.Text))) {
299         QMessageBox.warning(this, tr("QXmlStream Bookmarks"),
300                              tr("Cannot write file %1:\n%2.")
301                              .arg(fileName)
302                              .arg(file.errorString()));
303         return;
304     }
306     var writer = new XbelWriter(this.treeWidget);
307     if (writer.writeFile(file))
308         this.statusBar().showMessage(tr("File saved"), 2000);
311 MainWindow.prototype.about = function()
313    QMessageBox.about(this, tr("About QXmlStream Bookmarks"),
314             tr("The <b>QXmlStream Bookmarks</b> example demonstrates how to use Qt's QXmlStream classes to read and write XML documents."));
317 MainWindow.prototype.createActions = function()
319     this.openAct = new QAction(tr("&Open..."), this);
320     this.openAct.shortcut = tr("Ctrl+O");
321     this.openAct.triggered.connect(this, this.open);
323     this.saveAsAct = new QAction(tr("&Save As..."), this);
324     this.saveAsAct.shortcut = tr("Ctrl+S");
325     this.saveAsAct.triggered.connect(this, this.saveAs);
327     this.exitAct = new QAction(tr("E&xit"), this);
328     this.exitAct.shortcut = tr("Ctrl+Q");
329     this.exitAct.triggered.connect(this, this.close);
331     this.aboutAct = new QAction(tr("&About"), this);
332     this.aboutAct.triggered.connect(this, this.about);
334     this.aboutQtAct = new QAction(tr("About &Qt"), this);
335 // ###    this.aboutQtAct.triggered.connect(QApplication.aboutQt);
336     this.aboutQtAct.triggered.connect(qApp.aboutQt);
339 MainWindow.prototype.createMenus = function()
341     this.fileMenu = this.menuBar().addMenu(tr("&File"));
343 // ### working around bug in QMenu.prototype.addAction
344     QMenu.prototype.addAction = QWidget.prototype.addAction;
346     this.fileMenu.addAction(this.openAct);
347     this.fileMenu.addAction(this.saveAsAct);
348     this.fileMenu.addAction(this.exitAct);
350     this.menuBar().addSeparator();
352     this.helpMenu = this.menuBar().addMenu(tr("&Help"));
353     this.helpMenu.addAction(this.aboutAct);
354     this.helpMenu.addAction(this.aboutQtAct);
358 var mainWin = new MainWindow();
359 mainWin.show();
360 mainWin.open();
361 QCoreApplication.exec();