examples/xml/streambookmarks/xbelreader.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 "xbelreader.h"
       
    45 
       
    46 //! [0]
       
    47 XbelReader::XbelReader(QTreeWidget *treeWidget)
       
    48     : treeWidget(treeWidget)
       
    49 {
       
    50     QStyle *style = treeWidget->style();
       
    51 
       
    52     folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
       
    53                          QIcon::Normal, QIcon::Off);
       
    54     folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
       
    55                          QIcon::Normal, QIcon::On);
       
    56     bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
       
    57 }
       
    58 //! [0]
       
    59 
       
    60 //! [1]
       
    61 bool XbelReader::read(QIODevice *device)
       
    62 {
       
    63     xml.setDevice(device);
       
    64 
       
    65     if (xml.readNextStartElement()) {
       
    66         if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0")
       
    67             readXBEL();
       
    68         else
       
    69             xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
       
    70     }
       
    71 
       
    72     return !xml.error();
       
    73 }
       
    74 //! [1]
       
    75 
       
    76 //! [2]
       
    77 QString XbelReader::errorString() const
       
    78 {
       
    79     return QObject::tr("%1\nLine %2, column %3")
       
    80             .arg(xml.errorString())
       
    81             .arg(xml.lineNumber())
       
    82             .arg(xml.columnNumber());
       
    83 }
       
    84 //! [2]
       
    85 
       
    86 //! [3]
       
    87 void XbelReader::readXBEL()
       
    88 {
       
    89     Q_ASSERT(xml.isStartElement() && xml.name() == "xbel");
       
    90 
       
    91     while (xml.readNextStartElement()) {
       
    92         if (xml.name() == "folder")
       
    93             readFolder(0);
       
    94         else if (xml.name() == "bookmark")
       
    95             readBookmark(0);
       
    96         else if (xml.name() == "separator")
       
    97             readSeparator(0);
       
    98         else
       
    99             xml.skipCurrentElement();
       
   100     }
       
   101 }
       
   102 //! [3]
       
   103 
       
   104 //! [4]
       
   105 void XbelReader::readTitle(QTreeWidgetItem *item)
       
   106 {
       
   107     Q_ASSERT(xml.isStartElement() && xml.name() == "title");
       
   108 
       
   109     QString title = xml.readElementText();
       
   110     item->setText(0, title);
       
   111 }
       
   112 //! [4]
       
   113 
       
   114 //! [5]
       
   115 void XbelReader::readSeparator(QTreeWidgetItem *item)
       
   116 {
       
   117     Q_ASSERT(xml.isStartElement() && xml.name() == "separator");
       
   118 
       
   119     QTreeWidgetItem *separator = createChildItem(item);
       
   120     separator->setFlags(item->flags() & ~Qt::ItemIsSelectable);
       
   121     separator->setText(0, QString(30, 0xB7));
       
   122     xml.skipCurrentElement();
       
   123 }
       
   124 //! [5]
       
   125 
       
   126 void XbelReader::readFolder(QTreeWidgetItem *item)
       
   127 {
       
   128     Q_ASSERT(xml.isStartElement() && xml.name() == "folder");
       
   129 
       
   130     QTreeWidgetItem *folder = createChildItem(item);
       
   131     bool folded = (xml.attributes().value("folded") != "no");
       
   132     treeWidget->setItemExpanded(folder, !folded);
       
   133 
       
   134     while (xml.readNextStartElement()) {
       
   135         if (xml.name() == "title")
       
   136             readTitle(folder);
       
   137         else if (xml.name() == "folder")
       
   138             readFolder(folder);
       
   139         else if (xml.name() == "bookmark")
       
   140             readBookmark(folder);
       
   141         else if (xml.name() == "separator")
       
   142             readSeparator(folder);
       
   143         else
       
   144             xml.skipCurrentElement();
       
   145     }
       
   146 }
       
   147 
       
   148 void XbelReader::readBookmark(QTreeWidgetItem *item)
       
   149 {
       
   150     Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark");
       
   151 
       
   152     QTreeWidgetItem *bookmark = createChildItem(item);
       
   153     bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable);
       
   154     bookmark->setIcon(0, bookmarkIcon);
       
   155     bookmark->setText(0, QObject::tr("Unknown title"));
       
   156     bookmark->setText(1, xml.attributes().value("href").toString());
       
   157 
       
   158     while (xml.readNextStartElement()) {
       
   159         if (xml.name() == "title")
       
   160             readTitle(bookmark);
       
   161         else
       
   162             xml.skipCurrentElement();
       
   163     }
       
   164 }
       
   165 
       
   166 QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item)
       
   167 {
       
   168     QTreeWidgetItem *childItem;
       
   169     if (item) {
       
   170         childItem = new QTreeWidgetItem(item);
       
   171     } else {
       
   172         childItem = new QTreeWidgetItem(treeWidget);
       
   173     }
       
   174     childItem->setData(0, Qt::UserRole, xml.name().toString());
       
   175     return childItem;
       
   176 }