Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

filetree.cpp Example File
xmlpatterns/filetree/filetree.cpp

 /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/

 #include <QtCore/QUrl>
 #include <QtCore/QVariant>
 #include <QtXmlPatterns/QXmlNamePool>
 #include "filetree.h"

 /*
 The model has two types of nodes: elements & attributes.

     <directory name="">
         <file name="">
         </file>
     </directory>

   In QXmlNodeModelIndex we store two values. QXmlNodeIndex::data()
   is treated as a signed int, and it is an index into m_fileInfos
   unless it is -1, in which case it has no meaning and the value
   of QXmlNodeModelIndex::additionalData() is a Type name instead.
  */

 /*!
   The constructor passes \a pool to the base class, then loads an
   internal vector with an instance of QXmlName for each of the
   strings "file", "directory", "fileName", "filePath", "size",
   "mimeType", and "suffix".
  */
 FileTree::FileTree(const QXmlNamePool& pool)
   : QSimpleXmlNodeModel(pool),
     m_filterAllowAll(QDir::AllEntries |
                      QDir::AllDirs |
                      QDir::NoDotAndDotDot |
                      QDir::Hidden),
     m_sortFlags(QDir::Name)
 {
     QXmlNamePool np = namePool();
     m_names.resize(7);
     m_names[File]               = QXmlName(np, QLatin1String("file"));
     m_names[Directory]          = QXmlName(np, QLatin1String("directory"));
     m_names[AttributeFileName]  = QXmlName(np, QLatin1String("fileName"));
     m_names[AttributeFilePath]  = QXmlName(np, QLatin1String("filePath"));
     m_names[AttributeSize]      = QXmlName(np, QLatin1String("size"));
     m_names[AttributeMIMEType]  = QXmlName(np, QLatin1String("mimeType"));
     m_names[AttributeSuffix]    = QXmlName(np, QLatin1String("suffix"));
 }

 /*!
   Returns the QXmlNodeModelIndex for the model node representing
   the directory \a dirName.

   It calls QDir::cleanPath(), because an instance of QFileInfo
   constructed for a path ending in '/' will return the empty string in
   fileName(), instead of the directory name.
 */
 QXmlNodeModelIndex FileTree::nodeFor(const QString& dirName) const
 {
     QFileInfo dirInfo(QDir::cleanPath(dirName));
     Q_ASSERT(dirInfo.exists());
     return toNodeIndex(dirInfo);
 }

 /*!
   Since the value will always be in m_fileInfos, it is safe for
   us to return a const reference to it.
  */
 const QFileInfo&
 FileTree::toFileInfo(const QXmlNodeModelIndex &nodeIndex) const
 {
     return m_fileInfos.at(nodeIndex.data());
 }

 /*!
   Returns the model node index for the node specified by the
   QFileInfo and node Type.
  */
 QXmlNodeModelIndex
 FileTree::toNodeIndex(const QFileInfo &fileInfo, Type attributeName) const
 {
     const int indexOf = m_fileInfos.indexOf(fileInfo);

     if (indexOf == -1) {
         m_fileInfos.append(fileInfo);
         return createIndex(m_fileInfos.count()-1, attributeName);
     }
     else
         return createIndex(indexOf, attributeName);
 }

 /*!
   Returns the model node index for the node specified by the
   QFileInfo, which must be a  Type::File or Type::Directory.
  */
 QXmlNodeModelIndex FileTree::toNodeIndex(const QFileInfo &fileInfo) const
 {
     return toNodeIndex(fileInfo, fileInfo.isDir() ? Directory : File);
 }

 /*!
   This private helper function is only called by nextFromSimpleAxis().
   It is called whenever nextFromSimpleAxis() is called with an axis
   parameter of either \c{PreviousSibling} or \c{NextSibling}.
  */
 QXmlNodeModelIndex FileTree::nextSibling(const QXmlNodeModelIndex &nodeIndex,
                                          const QFileInfo &fileInfo,
                                          qint8 offset) const
 {
     Q_ASSERT(offset == -1 || offset == 1);

     // Get the context node's parent.
     const QXmlNodeModelIndex parent(nextFromSimpleAxis(Parent, nodeIndex));

     if (parent.isNull())
         return QXmlNodeModelIndex();

     // Get the parent's child list.
     const QFileInfo parentFI(toFileInfo(parent));
     Q_ASSERT(Type(parent.additionalData()) == Directory);
     const QFileInfoList siblings(QDir(parentFI.absoluteFilePath()).entryInfoList(QStringList(),
                                                                                  m_filterAllowAll,
                                                                                  m_sortFlags));
     Q_ASSERT_X(!siblings.isEmpty(), Q_FUNC_INFO, "Can't happen! We started at a child.");

     // Find the index of the child where we started.
     const int indexOfMe = siblings.indexOf(fileInfo);

     // Apply the offset.
     const int siblingIndex = indexOfMe + offset;
     if (siblingIndex < 0 || siblingIndex > siblings.count() - 1)
         return QXmlNodeModelIndex();
     else
         return toNodeIndex(siblings.at(siblingIndex));
 }

 /*!
   This function is called by the QtXmlPatterns query engine when it
   wants to move to the next node in the model. It moves along an \a
   axis, \e from the node specified by \a nodeIndex.

   This function is usually the one that requires the most design and
   implementation work, because the implementation depends on the
   perhaps unique structure of your non-XML data.

   There are \l {QAbstractXmlNodeModel::SimpleAxis} {four values} for
   \a axis that the implementation must handle, but there are really
   only two axes, i.e., vertical and horizontal. Two of the four values
   specify direction on the vertical axis (\c{Parent} and
   \c{FirstChild}), and the other two values specify direction on the
   horizontal axis (\c{PreviousSibling} and \c{NextSibling}).

   The typical implementation will be a \c switch statement with
   a case for each of the four \a axis values.
  */
 QXmlNodeModelIndex
 FileTree::nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &nodeIndex) const
 {
     const QFileInfo fi(toFileInfo(nodeIndex));
     const Type type = Type(nodeIndex.additionalData());

     if (type != File && type != Directory) {
         Q_ASSERT_X(axis == Parent, Q_FUNC_INFO, "An attribute only has a parent!");
         return toNodeIndex(fi, Directory);
     }

     switch (axis) {
         case Parent:
             return toNodeIndex(QFileInfo(fi.path()), Directory);

         case FirstChild:
         {
             if (type == File) // A file has no children.
                 return QXmlNodeModelIndex();
             else {
                 Q_ASSERT(type == Directory);
                 Q_ASSERT_X(fi.isDir(), Q_FUNC_INFO, "It isn't really a directory!");
                 const QDir dir(fi.absoluteFilePath());
                 Q_ASSERT(dir.exists());

                 const QFileInfoList children(dir.entryInfoList(QStringList(),
                                                                m_filterAllowAll,
                                                                m_sortFlags));
                 if (children.isEmpty())
                     return QXmlNodeModelIndex();
                 const QFileInfo firstChild(children.first());
                 return toNodeIndex(firstChild);
             }
         }

         case PreviousSibling:
             return nextSibling(nodeIndex, fi, -1);

         case NextSibling:
             return nextSibling(nodeIndex, fi, 1);
     }

     Q_ASSERT_X(false, Q_FUNC_INFO, "Don't ever get here!");
     return QXmlNodeModelIndex();
 }

 /*!
   No matter what part of the file system we model (the whole file
   tree or a subtree), \a node will always have \c{file:///} as
   the document URI.
  */
 QUrl FileTree::documentUri(const QXmlNodeModelIndex &node) const
 {
     Q_UNUSED(node);
     return QUrl("file:///");
 }

 /*!
   This function returns QXmlNodeModelIndex::Element if \a node
   is a directory or a file, and QXmlNodeModelIndex::Attribute
   otherwise.
  */
 QXmlNodeModelIndex::NodeKind
 FileTree::kind(const QXmlNodeModelIndex &node) const
 {
     switch (Type(node.additionalData())) {
         case Directory:
         case File:
             return QXmlNodeModelIndex::Element;
         default:
             return QXmlNodeModelIndex::Attribute;
     }
 }

 /*!
   No order is defined for this example, so we always return
   QXmlNodeModelIndex::Precedes, just to keep everyone happy.
  */
 QXmlNodeModelIndex::DocumentOrder
 FileTree::compareOrder(const QXmlNodeModelIndex&,
                        const QXmlNodeModelIndex&) const
 {
     return QXmlNodeModelIndex::Precedes;
 }

 /*!
   Returns the name of \a node. The caller guarantees that \a node is
   not null and that it is contained in this node model.
  */
 QXmlName FileTree::name(const QXmlNodeModelIndex &node) const
 {
     return m_names.at(node.additionalData());
 }

 /*!
   Always returns the QXmlNodeModelIndex for the root of the
   file system, i.e. "/".
  */
 QXmlNodeModelIndex FileTree::root(const QXmlNodeModelIndex &node) const
 {
     Q_UNUSED(node);
     return toNodeIndex(QFileInfo(QLatin1String("/")));
 }

 /*!
   Returns the typed value for \a node, which must be either an
   attribute or an element. The QVariant returned represents the atomic
   value of an attribute or the atomic value contained in an element.

   If the QVariant is returned as a default constructed variant,
   it means that \a node has no typed value.
  */
 QVariant FileTree::typedValue(const QXmlNodeModelIndex &node) const
 {
     const QFileInfo &fi = toFileInfo(node);

     switch (Type(node.additionalData())) {
         case Directory:
             // deliberate fall through.
         case File:
             return QString();
         case AttributeFileName:
             return fi.fileName();
         case AttributeFilePath:
             return fi.filePath();
         case AttributeSize:
             return fi.size();
         case AttributeMIMEType:
             {
                 /* We don't have any MIME detection code currently, so return
                  * the most generic one. */
                 return QLatin1String("application/octet-stream");
             }
         case AttributeSuffix:
             return fi.suffix();
     }

     Q_ASSERT_X(false, Q_FUNC_INFO, "This line should never be reached.");
     return QString();
 }

 /*!
   Returns the attributes of \a element. The caller guarantees
   that \a element is an element in this node model.
  */
 QVector<QXmlNodeModelIndex>
 FileTree::attributes(const QXmlNodeModelIndex &element) const
 {
     QVector<QXmlNodeModelIndex> result;

     /* Both elements has this attribute. */
     const QFileInfo &forElement = toFileInfo(element);
     result.append(toNodeIndex(forElement, AttributeFilePath));
     result.append(toNodeIndex(forElement, AttributeFileName));

     if (Type(element.additionalData() == File)) {
         result.append(toNodeIndex(forElement, AttributeSize));
         result.append(toNodeIndex(forElement, AttributeSuffix));
         //result.append(toNodeIndex(forElement, AttributeMIMEType));
     }
     else {
         Q_ASSERT(element.additionalData() == Directory);
     }

     return result;
 }

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 102
  2. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 53
  3. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 73
  4. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 28
  5. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 11
Page suivante
  1. Linus Torvalds : le "C++ est un langage horrible", en justifiant le choix du C pour le système de gestion de version Git 100
  2. Comment prendre en compte l'utilisateur dans vos applications ? Pour un développeur, « 90 % des utilisateurs sont des idiots » 229
  3. Quel est LE livre que tout développeur doit lire absolument ? Celui qui vous a le plus marqué et inspiré 96
  4. Apple cède et s'engage à payer des droits à Nokia, le conflit des brevets entre les deux firmes s'achève 158
  5. Nokia porte à nouveau plainte contre Apple pour violation de sept nouveaux brevets 158
  6. Quel est le code dont vous êtes le plus fier ? Pourquoi l'avez-vous écrit ? Et pourquoi vous a-t-il donné autant de satisfaction ? 83
  7. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 101
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web