//! [5] 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)); }
//! [4] 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(); }
/*! 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; }
//! [0] QXmlNodeModelIndex MyTreeModel::nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &ni) const { // Convert the QXmlNodeModelIndex to a value that is specific to what we represent. const MyValue value = toMyValue(ni); switch(axis) { case Parent: return toNodeIndex(value.parent()); case FirstChild: case PreviousSibling: case NextSibling: // and so on ; } return QXmlNodeModelIndex(); }
/*! 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); }
/*! 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("/"))); }
//! [0] QXmlNodeModelIndex FileTree::toNodeIndex(const QFileInfo &fileInfo) const { return toNodeIndex(fileInfo, fileInfo.isDir() ? Directory : File); }