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

📄 qdirmodel.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    }    return QAbstractItemModel::headerData(section, orientation, role);}/*!  Returns true if the \a parent model item has children; otherwise  returns false.*/bool QDirModel::hasChildren(const QModelIndex &parent) const{    Q_D(const QDirModel);    if (parent.column() > 0)        return false;    if (!parent.isValid()) // the invalid index is the "My Computer" item        return true; // the drives    QDirModelPrivate::QDirNode *p = d->node(parent);    Q_ASSERT(p);    if (d->lazyChildCount) // optimization that only checks for children if the node has been populated        return p->info.isDir();    return p->info.isDir() && rowCount(parent) > 0;}/*!  Returns the item flags for the given \a index in the model.  \sa Qt::ItemFlags*/Qt::ItemFlags QDirModel::flags(const QModelIndex &index) const{    Q_D(const QDirModel);    Qt::ItemFlags flags = QAbstractItemModel::flags(index);    if (!d->indexValid(index))        return flags;    flags |= Qt::ItemIsDragEnabled;    if (d->readOnly)        return flags;    QDirModelPrivate::QDirNode *node = d->node(index);    if ((index.column() == 0) && node->info.isWritable()) {        flags |= Qt::ItemIsEditable;        if (fileInfo(index).isDir()) // is directory and is editable            flags |= Qt::ItemIsDropEnabled;    }    return flags;}/*!  Sort the model items in the \a column using the \a order given.  The order is a value defined in \l Qt::SortOrder.*/void QDirModel::sort(int column, Qt::SortOrder order){    QDir::SortFlags sort = QDir::DirsFirst | QDir::IgnoreCase;    if (order == Qt::DescendingOrder)        sort |= QDir::Reversed;    switch (column) {    case 0:        sort |= QDir::Name;        break;    case 1:        sort |= QDir::Size;        break;    case 2:        sort |= QDir::Type;        break;    case 3:        sort |= QDir::Time;        break;    default:        break;    }    setSorting(sort);}/*!    Returns a list of MIME types that can be used to describe a list of items    in the model.*/QStringList QDirModel::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 *QDirModel::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 QDirModel::dropMimeData(const QMimeData *data, Qt::DropAction action,                             int /* row */, int /* column */, const QModelIndex &parent){    Q_D(QDirModel);    if (!d->indexValid(parent) || 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;    }    if (success)        refresh(parent);    return success;}/*!  Returns the drop actions supported by this model.  \sa Qt::DropActions*/Qt::DropActions QDirModel::supportedDropActions() const{    return Qt::CopyAction | Qt::MoveAction; // FIXME: LinkAction is not supported yet}/*!  Sets the \a provider of file icons for the directory model.*/void QDirModel::setIconProvider(QFileIconProvider *provider){    Q_D(QDirModel);    d->iconProvider = provider;}/*!  Returns the file icon provider for this directory model.*/QFileIconProvider *QDirModel::iconProvider() const{    Q_D(const QDirModel);    return d->iconProvider;}/*!  Sets the name \a filters for the directory model.*/void QDirModel::setNameFilters(const QStringList &filters){    Q_D(QDirModel);    d->nameFilters = filters;    emit layoutAboutToBeChanged();    if (d->shouldStat)       refresh(QModelIndex());    else        d->invalidate();    emit layoutChanged();}/*!  Returns a list of filters applied to the names in the model.*/QStringList QDirModel::nameFilters() const{    Q_D(const QDirModel);    return d->nameFilters;}/*!  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 QDirModel won't be able to read the directory structure.  \sa QDir::Filters*/void QDirModel::setFilter(QDir::Filters filters){    Q_D(QDirModel);    d->filters = filters;    emit layoutAboutToBeChanged();    if (d->shouldStat)        refresh(QModelIndex());    else        d->invalidate();    emit layoutChanged();}/*!  Returns the filter specification for the directory model.  \sa QDir::Filters*/QDir::Filters QDirModel::filter() const{    Q_D(const QDirModel);    return d->filters;}/*!  Sets the directory model's sorting order to that specified by \a sort.  \sa QDir::SortFlags*/void QDirModel::setSorting(QDir::SortFlags sort){    Q_D(QDirModel);    d->sort = sort;    emit layoutAboutToBeChanged();    if (d->shouldStat)        refresh(QModelIndex());    else        d->invalidate();    emit layoutChanged();}/*!  Returns the sorting method used for the directory model.  \sa QDir::SortFlags */QDir::SortFlags QDirModel::sorting() const{    Q_D(const QDirModel);    return d->sort;}/*!    \property QDirModel::resolveSymlinks    \brief Whether the directory model should resolve symbolic links    This is only relevant on operating systems that support symbolic    links.*/void QDirModel::setResolveSymlinks(bool enable){    Q_D(QDirModel);    d->resolveSymlinks = enable;}bool QDirModel::resolveSymlinks() const{    Q_D(const QDirModel);    return d->resolveSymlinks;}/*!  \property QDirModel::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 QDirModel::setReadOnly(bool enable){    Q_D(QDirModel);    d->readOnly = enable;}bool QDirModel::isReadOnly() const{    Q_D(const QDirModel);    return d->readOnly;}/*!  \property QDirModel::lazyChildCount  \brief Whether the directory model optimizes the hasChildren function  to only check if the item is a directory.  If this property is set to false, the directory model will make sure that a directory  actually containes any files before reporting that it has children.  Otherwise the directory model will report that an item has children if the item  is a directory.  This property is false by default*/void QDirModel::setLazyChildCount(bool enable){    Q_D(QDirModel);    d->lazyChildCount = enable;}bool QDirModel::lazyChildCount() const{    Q_D(const QDirModel);    return d->lazyChildCount;}/*!  QDirModel caches file information. This function updates the  cache. The \a parent parameter is the directory from which the  model is updated; the default value will update the model from  root directory of the file system (the entire model).*/void QDirModel::refresh(const QModelIndex &parent){    Q_D(QDirModel);    QDirModelPrivate::QDirNode *n = d->indexValid(parent) ? d->node(parent) : &(d->root);    int rows = n->children.count();    if (rows == 0) {        emit layoutAboutToBeChanged();        n->stat = true; // make sure that next time we read all the info        n->populated = false;        emit layoutChanged();        return;    }    emit layoutAboutToBeChanged();    d->savePersistentIndexes();    d->rowsAboutToBeRemoved(parent, 0, rows - 1);    n->stat = true; // make sure that next time we read all the info    d->clear(n);    d->rowsRemoved(parent, 0, rows - 1);    d->restorePersistentIndexes();    emit layoutChanged();}/*!    \overload    Returns the model item index for the given \a path.*/QModelIndex QDirModel::index(const QString &path, int column) const{    Q_D(const QDirModel);    if (path.isEmpty() || path == QCoreApplication::translate("QFileDialog", "My Computer"))        return QModelIndex();    QString absolutePath = QDir(path).absolutePath();#ifdef Q_OS_WIN    absolutePath = absolutePath.toLower();    // On Windows, "filename......." and "filename" are equivalent    if (absolutePath.endsWith(QLatin1Char('.'))) {        int i;        for (i = absolutePath.count() - 1; i >= 0; --i) {            if (absolutePath.at(i) != QLatin1Char('.'))                break;        }        absolutePath = absolutePath.left(i+1);    }#endif    QStringList pathElements = absolutePath.split(QLatin1Char('/'), QString::SkipEmptyParts);    if ((pathElements.isEmpty() || !QFileInfo(path).exists())#ifndef Q_OS_WIN        && path != QLatin1String("/")#endif        )        return QModelIndex();    QModelIndex idx; // start with "My Computer"    if (!d->root.populated) // make sure the root is populated        d->populate(&d->root);#ifdef Q_OS_WIN    if (absolutePath.startsWith(QLatin1String("//"))) { // UNC path        QString host = pathElements.first();        int r = 0;        for (; r < d->root.children.count(); ++r)            if (d->root.children.at(r).info.fileName() == host)                break;        if (r >= d->root.children.count() && d->allowAppendChild)            d->appendChild(&d->root, QLatin1String("//") + host);        idx = index(r, 0, QModelIndex());        pathElements.pop_front();    } else if (pathElements.at(0).endsWith(QLatin1Char(':'))) {        pathElements[0] += QLatin1Char('/');    }#else    // add the "/" item, since it is a valid path element on unix    pathElements.prepend(QLatin1String("/"));#endif    for (int i = 0; i < pathElements.count(); ++i) {        Q_ASSERT(!pathElements.at(i).isEmpty());        QString element = pathElements.at(i);        QDirModelPrivate::QDirNode *parent = (idx.isValid() ? d->node(idx) : &d->root);        Q_ASSERT(parent);        if (!parent->populated)            d->populate(parent);        // search for the element in the child nodes first        int row = -1;        for (int j = parent->children.count() - 1; j >= 0; --j) {            const QFileInfo& fi = parent->children.at(j).info;            QString childFileName;#ifdef Q_OS_WIN            childFileName = idx.isValid() ? fi.fileName() : fi.absoluteFilePath();            childFileName = childFileName.toLower();#else            childFileName = idx.isValid() ? fi.fileName() : fi.absoluteFilePath();

⌨️ 快捷键说明

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