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

📄 qdirmodel.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*!  Returns the number of rows in the \a parent model item.*/int QDirModel::rowCount(const QModelIndex &parent) const{    Q_D(const QDirModel);    if (parent.column() > 0)        return 0;    if (!parent.isValid()) {        if (!d->root.populated) // lazy population            d->populate(&d->root);        return d->root.children.count();    }    QDirModelPrivate::QDirNode *p = d->node(parent);    if (p->info.isDir() && !p->populated) // lazy population        d->populate(p);    return p->children.count();}/*!  Returns the number of columns in the \a parent model item.*/int QDirModel::columnCount(const QModelIndex &parent) const{    if (parent.column() > 0)        return 0;    return 4;}/*!  Returns the data for the model item \a index with the given \a role.*/QVariant QDirModel::data(const QModelIndex &index, int role) const{    Q_D(const QDirModel);    if (!index.isValid())        return QVariant();    if (role == Qt::DisplayRole || role == Qt::EditRole) {        switch (index.column()) {        case 0: return d->name(index);        case 1: return d->size(index);        case 2: return d->type(index);        case 3: return d->time(index);        default:            qWarning("data: invalid display value column %d", index.column());            return QVariant();        }    }    if (index.column() == 0) {        if (role == FileIconRole)            return fileIcon(index);        if (role == FilePathRole)            return filePath(index);        if (role == FileNameRole)            return fileName(index);    }    return QVariant();}/*!  Sets the data for the model item \a index with the given \a role to  the data referenced by the \a value. Returns true if successful;  otherwise returns false.  \sa Qt::ItemDataRole*/bool QDirModel::setData(const QModelIndex &index, const QVariant &value, int role){    Q_D(QDirModel);    if (!index.isValid() || index.column() != 0        || (flags(index) & Qt::ItemIsEditable) == 0 || role != Qt::EditRole)        return false;    QDirModelPrivate::QDirNode *node = d->node(index);    QDir dir = node->info.dir();    QString name = value.toString();    if (dir.rename(node->info.fileName(), name)) {        node->info = QFileInfo(dir, name);        QModelIndex sibling = index.sibling(index.row(), 3);        emit dataChanged(index, sibling);        d->toBeRefreshed = index.parent();        int slot = metaObject()->indexOfSlot("_q_refresh()");        QApplication::postEvent(this, new QMetaCallEvent(slot));        return true;    }    return false;}/*!  Returns the data stored under the given \a role for the specified \a section  of the header with the given \a orientation.*/QVariant QDirModel::headerData(int section, Qt::Orientation orientation, int role) const{    if (orientation == Qt::Horizontal) {        if (role != Qt::DisplayRole)            return QVariant();	switch (section) {        case 0: return tr("Name");        case 1: return tr("Size");        case 2: return tr("Type");        case 3: return tr("Modified");        default: return QVariant();        }    }    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 (!index.isValid())        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;    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){    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.begin();    switch (action) {    case Qt::CopyAction:        for (; it != urls.end(); ++it) {            QString path = (*it).toLocalFile();            success = QFile::copy(path, to + QFileInfo(path).fileName()) && success;        }        break;    case Qt::LinkAction:        for (; it != urls.end(); ++it) {            QString path = (*it).toLocalFile();            success = QFile::link(path, to + QFileInfo(path).fileName()) && success;        }        break;    case Qt::MoveAction:        for (; it != urls.end(); ++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;    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;    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;    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 true, 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;}/*!  Refreshes (rereads) the children of \a parent.*/void QDirModel::refresh(const QModelIndex &parent){    Q_D(QDirModel);    QDirModelPrivate::QDirNode *n = parent.isValid() ? d->node(parent) : &(d->root);    int rows = n->children.count();    if (rows == 0) {        n->stat = true; // make sure that next time we read all the info        n->populated = false;        emit layoutChanged();        return;    }    d->savePersistentIndexes();    beginRemoveRows(parent, 0, rows - 1);    n->stat = true; // make sure that next time we read all the info    d->clear(n);    endRemoveRows();    d->restorePersistentIndexes();}

⌨️ 快捷键说明

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