⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qfilesystemmodel.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
            return -1;        if (r > 0)            return 1;    }    // The two strings are the same (02 == 2) so fall back to the normal sort    return QString::compare(s1, s2, cs);}/*    \internal    Helper functor used by sort()*/class QFileSystemModelSorter{public:    inline QFileSystemModelSorter(int column) : sortColumn(column) {}    bool compareNodes(const QFileSystemModelPrivate::QFileSystemNode *l,                    const QFileSystemModelPrivate::QFileSystemNode *r) const    {        switch (sortColumn) {        case 0: {#ifndef Q_OS_MAC            // place directories before files            bool left = l->isDir();            bool right = r->isDir();            if (left ^ right)                return left;#endif            return QFileSystemModelPrivate::naturalCompare(l->fileName,                                                r->fileName, Qt::CaseInsensitive) < 0;                }        case 1:            // Directories go first            if (l->isDir() && !r->isDir())                return true;            return l->size() < r->size();        case 2:            return l->type() < r->type();        case 3:            return l->lastModified() < r->lastModified();        }        Q_ASSERT(false);        return false;    }    bool operator()(const QPair<const QFileSystemModelPrivate::QFileSystemNode*, int> &l,                           const QPair<const QFileSystemModelPrivate::QFileSystemNode*, int> &r) const    {        return compareNodes(l.first, r.first);    }private:    int sortColumn;};/*    \internal    Sort all of the children of parent (including their children)*/void QFileSystemModelPrivate::sortChildren(int column, Qt::SortOrder order, const QModelIndex &parent){    Q_Q(QFileSystemModel);    QFileSystemModelPrivate::QFileSystemNode *indexNode = node(parent);    if (indexNode->children.count() == 0)        return;    QList<QPair<const QFileSystemModelPrivate::QFileSystemNode*, int> > values;    for (int i = 0; i < indexNode->children.count(); ++i) {        if (filtersAcceptsNode(&indexNode->children.at(i)))            values.append(QPair<const QFileSystemModelPrivate::QFileSystemNode*, int>(&(indexNode->children[i]), i));    }    QFileSystemModelSorter ms(column);    qStableSort(values.begin(), values.end(), ms);    // First update the new visible list    indexNode->visibleChildren.clear();    for (int i = 0; i < values.count(); ++i)        indexNode->visibleChildren.append(values.at(i).second);    for (int i = 0; i < q->rowCount(parent); ++i)        sortChildren(column, order, q->index(i, 0, parent));}/*!    \reimp*/void QFileSystemModel::sort(int column, Qt::SortOrder order){    Q_D(QFileSystemModel);    if (d->sortOrder == order && d->sortColumn == column && !d->forceSort)        return;    emit layoutAboutToBeChanged();    QModelIndexList oldList = persistentIndexList();    QList<QPair<QFileSystemModelPrivate::QFileSystemNode*, int> > oldNodes;    for (int i = 0; i < oldList.count(); ++i) {        QPair<QFileSystemModelPrivate::QFileSystemNode*, int> pair(d->node(oldList.at(i)), oldList.at(i).column());        oldNodes.append(pair);    }    if (!(d->sortColumn == column && d->sortOrder != order && !d->forceSort)) {        d->sortChildren(column, order, QModelIndex());        d->sortColumn = column;        d->forceSort = false;    }    d->sortOrder = order;    QModelIndexList newList;    for (int i = 0; i < oldNodes.count(); ++i) {        QModelIndex idx = d->index(oldNodes.at(i).first);        idx = idx.sibling(idx.row(), oldNodes.at(i).second);        newList.append(idx);    }    changePersistentIndexList(oldList, newList);    emit layoutChanged();}/*!    Returns a list of MIME types that can be used to describe a list of items    in the model.*/QStringList QFileSystemModel::mimeTypes() const{    return QStringList(QLatin1String("text/uri-list"));}/*!    Returns an object that contains a serialized description of the specified    \a indexes. The format used to describe the items corresponding to the    indexes is obtained from the mimeTypes() function.    If the list of indexes is empty, 0 is returned rather than a serialized    empty list.*/QMimeData *QFileSystemModel::mimeData(const QModelIndexList &indexes) const{    QList<QUrl> urls;    QList<QModelIndex>::const_iterator it = indexes.begin();    for (; it != indexes.end(); ++it)        if ((*it).column() == 0)            urls << QUrl::fromLocalFile(filePath(*it));    QMimeData *data = new QMimeData();    data->setUrls(urls);    return data;}/*!    Handles the \a data supplied by a drag and drop operation that ended with    the given \a action over the row in the model specified by the \a row and    \a column and by the \a parent index.    \sa supportedDropActions()*/bool QFileSystemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,                             int row, int column, const QModelIndex &parent){    Q_UNUSED(row);    Q_UNUSED(column);    if (!parent.isValid() || isReadOnly())        return false;    bool success = true;    QString to = filePath(parent) + QDir::separator();    QList<QUrl> urls = data->urls();    QList<QUrl>::const_iterator it = urls.constBegin();    switch (action) {    case Qt::CopyAction:        for (; it != urls.constEnd(); ++it) {            QString path = (*it).toLocalFile();            success = QFile::copy(path, to + QFileInfo(path).fileName()) && success;        }        break;    case Qt::LinkAction:        for (; it != urls.constEnd(); ++it) {            QString path = (*it).toLocalFile();            success = QFile::link(path, to + QFileInfo(path).fileName()) && success;        }        break;    case Qt::MoveAction:        for (; it != urls.constEnd(); ++it) {            QString path = (*it).toLocalFile();            success = QFile::copy(path, to + QFileInfo(path).fileName())                      && QFile::remove(path) && success;        }        break;    default:        return false;    }    return success;}/*!    \reimp*/Qt::DropActions QFileSystemModel::supportedDropActions() const{    return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;}/*!    Returns the path of the item stored in the model under the    \a index given.*/QString QFileSystemModel::filePath(const QModelIndex &index) const{    Q_D(const QFileSystemModel);    QString fullPath = d->filePath(index);    QFileSystemModelPrivate::QFileSystemNode *dirNode = d->node(index);    if (dirNode->isSymLink() && d->fileInfoGatherer.resolveSymlinks()        && d->resolvedSymLinks.contains(QDir::fromNativeSeparators(fullPath))        && dirNode->isDir()) {        QFileInfo resolvedInfo(fullPath);        resolvedInfo = resolvedInfo.canonicalFilePath();        if (resolvedInfo.exists())            return resolvedInfo.filePath();    }    return fullPath;}QString QFileSystemModelPrivate::filePath(const QModelIndex &index) const{    Q_Q(const QFileSystemModel);    Q_UNUSED(q);    if (!index.isValid())        return QString();    Q_ASSERT(index.model() == q);    QStringList path;    QModelIndex idx = index;    while (idx.isValid()) {        QFileSystemModelPrivate::QFileSystemNode *dirNode = node(idx);        if (dirNode)            path.prepend(dirNode->fileName);        idx = idx.parent();    }    QString fullPath = path.join(QDir::separator());#ifndef Q_OS_WIN    if ((fullPath.length() > 2) && fullPath[0] == QLatin1Char('/') && fullPath[1] == QLatin1Char('/'))        fullPath = fullPath.mid(1);#endif    return QDir::toNativeSeparators(fullPath);}/*!    Create a directory with the name in the parent model item*/QModelIndex QFileSystemModel::mkdir(const QModelIndex &parent, const QString &name){    Q_D(QFileSystemModel);    if (!parent.isValid())        return parent;    QDir dir(filePath(parent));    if (!dir.mkdir(name))        return QModelIndex();    QFileSystemModelPrivate::QFileSystemNode *parentNode = d->node(parent);    d->addNode(parentNode, name);    int r = d->findChild(parentNode, QFileSystemModelPrivate::QFileSystemNode(name));    Q_ASSERT(r >= 0);    QFileSystemModelPrivate::QFileSystemNode *node = &parentNode->children[r];    node->populate(d->fileInfoGatherer.getInfo(QFileInfo(dir.absolutePath() + QDir::separator() + name)));    d->addVisibleFiles(parentNode, QStringList(name));    return d->index(node);}QFile::Permissions QFileSystemModel::permissions(const QModelIndex &index) const{    Q_D(const QFileSystemModel);    QFile::Permissions p = d->node(index)->permissions();    if (d->readOnly) {        p ^= (QFile::WriteOwner | QFile::WriteUser            | QFile::WriteGroup | QFile::WriteOther);    }    return p;}/*!    Sets the directory that is being watched by the model.    If the path is changed the model will be reset.  */QModelIndex QFileSystemModel::setRootPath(const QString &newPath){    Q_D(QFileSystemModel);#ifdef Q_OS_WIN    QString longNewPath = qt_GetLongPathName(newPath);#else    QString longNewPath = newPath;#endif    d->setRootPath = true;    if (d->rootDir.path() == longNewPath)        return d->index(rootPath());    QDir newPathDir(longNewPath);    bool showDrives = (longNewPath.isEmpty() || longNewPath == d->myComputer());    if (!showDrives && !newPathDir.exists())        return d->index(rootPath());    // We have a new valid root path    d->rootDir = newPathDir;    QModelIndex newRootIndex;    if (showDrives) {        // otherwise dir will become '.'        d->rootDir.setPath(QLatin1String(""));    } else {        newRootIndex = d->index(newPathDir.path());    }    fetchMore(newRootIndex);    emit rootPathChanged(newPath);    d->forceSort = true;    d->delayedSort();    return newRootIndex;}/*!    The currently set root path    \sa rootDirectory()*/QString QFileSystemModel::rootPath() const{    Q_D(const QFileSystemModel);    return d->rootDir.path();}/*!    The currently set directory    \sa rootPath();*/QDir QFileSystemModel::rootDirectory() const{    Q_D(const QFileSystemModel);    QDir dir(d->rootDir);    dir.setNameFilters(nameFilters());    dir.setFilter(filter());    return dir;}/*!    Sets the \a provider of file icons for the directory model.*/void QFileSystemModel::setIconProvider(QFileIconProvider *provider){    Q_D(QFileSystemModel);    d->fileInfoGatherer.setIconProvider(provider);}/*!    Returns the file icon provider for this directory model.*/QFileIconProvider *QFileSystemModel::iconProvider() const{    Q_D(const QFileSystemModel);    return d->fileInfoGatherer.iconProvider();}/*!    Sets the directory model's filter to that specified by \a filters.    Note that the filter you set should always include the QDir::AllDirs enum value,    otherwise QFileSystemModel won't be able to read the directory structure.    \sa QDir::Filters*/void QFileSystemModel::setFilter(QDir::Filters filters){    Q_D(QFileSystemModel);    d->filters = filters;    // CaseSensitivity might have changed    setNameFilters(nameFilters());    d->forceSort = true;    d->delayedSort();}/*!    Returns the filter specification for the directory model.    \sa QDir::Filters*/QDir::Filters QFileSystemModel::filter() const{    Q_D(const QFileSystemModel);    return d->filters;}/*!    \property QFileSystemModel::resolveSymlinks    \brief Whether the directory model should resolve symbolic links    This is only relevant on operating systems that support symbolic links.*/void QFileSystemModel::setResolveSymlinks(bool enable){    Q_D(QFileSystemModel);    d->fileInfoGatherer.setResolveSymlinks(enable);}bool QFileSystemModel::resolveSymlinks() const{    Q_D(const QFileSystemModel);    return d->fileInfoGatherer.resolveSymlinks();}/*!    \property QFileSystemModel::readOnly    \brief Whether the directory model allows writing to the file system    If this property is set to false, the directory model will allow renaming, copying    and deleting of files and directories.    This property is true by default*/void QFileSystemModel::setReadOnly(bool enable){    Q_D(QFileSystemModel);    d->readOnly = enable;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -